Skip to content
Snippets Groups Projects
Commit 05334de4 authored by Lene Wasskog's avatar Lene Wasskog
Browse files

feat: Add year as param to script, default is current year.

Result files (mapfile and tifs) are saved to path including the year
Env var HOME_DIR now includes model name
Minor changes to script to make it run on Mac
parent 7d890039
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
import subprocess import subprocess
import os import os
import sys
from dotenv import load_dotenv from dotenv import load_dotenv
from datetime import datetime from datetime import datetime
import pytz import pytz
...@@ -31,15 +32,27 @@ load_dotenv() ...@@ -31,15 +32,27 @@ load_dotenv()
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read("PSILARTEMP.cfg") config.read("PSILARTEMP.cfg")
year = os.getenv("YEAR") today = datetime.now().date()
if len(sys.argv) > 1:
year = int(sys.argv[1])
else:
year = today.year
recurring_start_date = os.getenv("RECURRING_START_DATE") recurring_start_date = os.getenv("RECURRING_START_DATE")
weatherdata_path = os.getenv("WEATHER_DATA_DIR") weatherdata_path = f'{os.getenv("WEATHER_DATA_DIR")}{year}/'
tmp_path = "tmp/" tmp_path = "tmp/"
out_path = os.getenv("DATA_DIR")
out_path = f'{os.getenv("DATA_DIR")}{year}/'
os.makedirs(out_path, exist_ok=True)
# TODO: Put timezone out in .env # TODO: Put timezone out in .env
local_timezone = pytz.timezone("Europe/Oslo") local_timezone = pytz.timezone("Europe/Oslo")
# Ensure that model is not run for future year
if year > today.year:
print(f"Cannot run model for future year ({year}). Quit.")
exit(0)
# If we are before the recurring start date, exit nicely # If we are before the recurring start date, exit nicely
start_time = datetime.strptime(f"{year}-{recurring_start_date}","%Y-%m-%d") start_time = datetime.strptime(f"{year}-{recurring_start_date}","%Y-%m-%d")
if datetime.now() <= start_time: if datetime.now() <= start_time:
...@@ -174,6 +187,7 @@ output = template.render( ...@@ -174,6 +187,7 @@ output = template.render(
"language_codes": language_codes, "language_codes": language_codes,
} }
) )
mapfile_outdir = os.getenv("MAPFILE_DIR") mapfile_outdir = f'{os.getenv("MAPFILE_DIR")}{year}/'
with open(f"{mapfile_outdir}/PSILARTEMP.map", "w") as f: os.makedirs(mapfile_outdir, exist_ok=True)
with open(f"{mapfile_outdir}PSILARTEMP.map", "w") as f:
f.write(output) f.write(output)
...@@ -44,7 +44,7 @@ It is required that you have set the following environment variables: ...@@ -44,7 +44,7 @@ It is required that you have set the following environment variables:
```bash ```bash
# Where your application resides # Where your application resides
HOME_DIR=/home/foo/ HOME_DIR=/home/foo/PSILARTEMP/
# Path to the weather data # Path to the weather data
WEATHER_DATA_DIR=in/ WEATHER_DATA_DIR=in/
# Path to optional CSV file with polygons for masking result. # Path to optional CSV file with polygons for masking result.
...@@ -68,12 +68,12 @@ MAPSERVER_EXTENT="-1.5831861262936526 52.4465003983706595 39.2608060398730458 71 ...@@ -68,12 +68,12 @@ MAPSERVER_EXTENT="-1.5831861262936526 52.4465003983706595 39.2608060398730458 71
```bash ```bash
$ ./run_PSILARTEMP.sh $ ./run_PSILARTEMP.sh
``` ```
This creates a Python virtualenv, installs all the Python dependencies, runs the model and stores output in a log file. This creates a Python virtualenv, installs all the Python dependencies, runs the model and stores output in a log file.
Alternatively, primarily for development purposes, you can run the Python script PSILARTEMP directly: The script can be run for a specific year like this:
```bash ```bash
$ ./PSILARTEMP.py $ ./run_PSILARTEMP.sh 2024
``` ```
All intermediary files are stored in the `tmp/` folder, and they are all deleted when the model is done calculating. The GeoTIFF files are stored in the `out/` folder, and the generated mapfile is stored in the `mapfile/` folder All intermediary files are stored in the `tmp/` folder, and they are all deleted when the model is done calculating. The GeoTIFF files are stored in the `out/` folder, and the generated mapfile is stored in the `mapfile/` folder
...@@ -180,4 +180,4 @@ else // Fallback to auto generated legend ...@@ -180,4 +180,4 @@ else // Fallback to auto generated legend
{ {
document.getElementById("layerLegend").innerHTML='<img id="layerLegendImg" src="' + currentLayer.Style[0].LegendURL[0].OnlineResource + '"/>'; document.getElementById("layerLegend").innerHTML='<img id="layerLegendImg" src="' + currentLayer.Style[0].LegendURL[0].OnlineResource + '"/>';
} }
``` ```
\ No newline at end of file
# Use this as an example to create your own .env file # Use this as an example to create your own .env file
# Year to run for
YEAR=2024
# Recurring start date (MM-DD) # Recurring start date (MM-DD)
RECURRING_START_DATE=04-01 RECURRING_START_DATE=04-01
# Where your application resides # Where your application resides
HOME_DIR=/home/foo/ HOME_DIR=/home/foo/PSILARTEMP/
# Path to the weather data # Path to the weather data
WEATHER_DATA_DIR=in/ WEATHER_DATA_DIR=in/
# Path to optional CSV file with polygons for masking result. # Path to optional CSV file with polygons for masking result.
...@@ -23,4 +21,4 @@ MAPSERVER_LOG_FILE=/foo/log/PSILARTEMP.log ...@@ -23,4 +21,4 @@ MAPSERVER_LOG_FILE=/foo/log/PSILARTEMP.log
# Path to the temporary directory for writing temporary files and images. Must be writable by the user the web server is running as # Path to the temporary directory for writing temporary files and images. Must be writable by the user the web server is running as
MAPSERVER_IMAGE_PATH=/foo/mapserver/tmp/ MAPSERVER_IMAGE_PATH=/foo/mapserver/tmp/
# The value of the EXTENT parameter in Mapserver's mapfile. Units are DD (Decimal degrees) # The value of the EXTENT parameter in Mapserver's mapfile. Units are DD (Decimal degrees)
MAPSERVER_EXTENT="-1.5831861262936526 52.4465003983706595 39.2608060398730458 71.7683216082912736" MAPSERVER_EXTENT="-1.5831861262936526 52.4465003983706595 39.2608060398730458 71.7683216082912736"
\ No newline at end of file
...@@ -18,9 +18,25 @@ ...@@ -18,9 +18,25 @@
# Configures environment and logging before running the model # Configures environment and logging before running the model
# @author: Tor-Einar Skog <tor-einar.skog@nibio.no> # @author: Tor-Einar Skog <tor-einar.skog@nibio.no>
validate_year() {
if [[ $1 =~ ^[0-9]{4}$ ]]; then
return 0
else
return 1
fi
}
# Check if the year parameter is passed and validate it
if [ -n "$1" ]; then
if validate_year "$1"; then
year=$1
else
echo "Invalid year: $1. Please provide a valid 4-digit year."
exit 1
fi
fi
# First: Test that we have CDO and GDAL installed # Test that we have CDO and GDAL installed
if ! command -v cdo &> /dev/null if ! command -v cdo &> /dev/null
then then
echo "ERROR: CDO could not be found. Exiting." echo "ERROR: CDO could not be found. Exiting."
...@@ -44,11 +60,10 @@ then ...@@ -44,11 +60,10 @@ then
fi fi
# Paths to scripts and requirements # Paths to scripts and requirements
APP_PATH=${HOME_DIR}PSILARTEMP/ LOG_FILE=${HOME_DIR}log/PSILARTEMP.log
LOG_FILE=${APP_PATH}log/PSILARTEMP.log REQUIREMENTS=${HOME_DIR}requirements.txt
REQUIREMENTS=${APP_PATH}requirements.txt
cd $APP_PATH cd $HOME_DIR
# Create and activate the virtual environment # Create and activate the virtual environment
python3 -m venv .venv python3 -m venv .venv
...@@ -57,9 +72,16 @@ python3 -m pip install -q --upgrade pip ...@@ -57,9 +72,16 @@ python3 -m pip install -q --upgrade pip
pip install -q -r $REQUIREMENTS pip install -q -r $REQUIREMENTS
# Run the model # Run the model
echo "==== `date`: Running model" &>> "$LOG_FILE"
python3 $APP_PATH/PSILARTEMP.py &>> "$LOG_FILE" if [ -z "${year}" ]; then
echo "==== `date`: DONE running model" &>> "$LOG_FILE" echo "==== $(date): Running model for ${year}" >> "$LOG_FILE" 2>&1
python3 ${HOME_DIR}PSILARTEMP.py >> "$LOG_FILE" 2>&1
else
echo "==== $(date): Running model for current year" >> "$LOG_FILE" 2>&1
python3 ${HOME_DIR}PSILARTEMP.py "$year" >> "$LOG_FILE" 2>&1
fi
echo "==== $(date): Done running model" >> "$LOG_FILE" 2>&1
# Deactivate the virtual environment # Deactivate the virtual environment
deactivate deactivate
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment