diff --git a/PSILARTEMP.py b/PSILARTEMP.py index 47fe34c326b1821b5e09799ea67305446861a598..5dd05bfb742266fd3364e1d013e03009fb7f0d85 100755 --- a/PSILARTEMP.py +++ b/PSILARTEMP.py @@ -18,6 +18,7 @@ import subprocess import os +import sys from dotenv import load_dotenv from datetime import datetime import pytz @@ -31,15 +32,27 @@ load_dotenv() config = configparser.ConfigParser() 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") -weatherdata_path = os.getenv("WEATHER_DATA_DIR") +weatherdata_path = f'{os.getenv("WEATHER_DATA_DIR")}{year}/' 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 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 start_time = datetime.strptime(f"{year}-{recurring_start_date}","%Y-%m-%d") if datetime.now() <= start_time: @@ -174,6 +187,7 @@ output = template.render( "language_codes": language_codes, } ) -mapfile_outdir = os.getenv("MAPFILE_DIR") -with open(f"{mapfile_outdir}/PSILARTEMP.map", "w") as f: +mapfile_outdir = f'{os.getenv("MAPFILE_DIR")}{year}/' +os.makedirs(mapfile_outdir, exist_ok=True) +with open(f"{mapfile_outdir}PSILARTEMP.map", "w") as f: f.write(output) diff --git a/README.md b/README.md index 48bde67456d9b80fbe150e307a90bfd91d31a97e..5ddfa378f9837cbc935fa2cddc480a2d8ebbe3c2 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ It is required that you have set the following environment variables: ```bash # Where your application resides -HOME_DIR=/home/foo/ +HOME_DIR=/home/foo/PSILARTEMP/ # Path to the weather data WEATHER_DATA_DIR=in/ # Path to optional CSV file with polygons for masking result. @@ -68,12 +68,12 @@ MAPSERVER_EXTENT="-1.5831861262936526 52.4465003983706595 39.2608060398730458 71 ```bash $ ./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 -$ ./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 @@ -180,4 +180,4 @@ else // Fallback to auto generated legend { document.getElementById("layerLegend").innerHTML='<img id="layerLegendImg" src="' + currentLayer.Style[0].LegendURL[0].OnlineResource + '"/>'; } -``` \ No newline at end of file +``` diff --git a/env-sample b/env-sample index e0f2cad3d82eff856d6ae045a5d097f0680e5ff2..7eea5788ece57b669f8bbe23e59d1579a80a3a5a 100644 --- a/env-sample +++ b/env-sample @@ -1,11 +1,9 @@ # 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=04-01 # Where your application resides -HOME_DIR=/home/foo/ +HOME_DIR=/home/foo/PSILARTEMP/ # Path to the weather data WEATHER_DATA_DIR=in/ # Path to optional CSV file with polygons for masking result. @@ -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 MAPSERVER_IMAGE_PATH=/foo/mapserver/tmp/ # 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" \ No newline at end of file +MAPSERVER_EXTENT="-1.5831861262936526 52.4465003983706595 39.2608060398730458 71.7683216082912736" diff --git a/run_PSILARTEMP.sh b/run_PSILARTEMP.sh index 9b2c45642e732ea431476d3f5e11e58ef2f11e82..d96ca060f0ea20c8ea68078374c477b7a3e66ad6 100755 --- a/run_PSILARTEMP.sh +++ b/run_PSILARTEMP.sh @@ -18,9 +18,25 @@ # Configures environment and logging before running the model # @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 then echo "ERROR: CDO could not be found. Exiting." @@ -44,11 +60,10 @@ then fi # Paths to scripts and requirements -APP_PATH=${HOME_DIR}PSILARTEMP/ -LOG_FILE=${APP_PATH}log/PSILARTEMP.log -REQUIREMENTS=${APP_PATH}requirements.txt +LOG_FILE=${HOME_DIR}log/PSILARTEMP.log +REQUIREMENTS=${HOME_DIR}requirements.txt -cd $APP_PATH +cd $HOME_DIR # Create and activate the virtual environment python3 -m venv .venv @@ -57,9 +72,16 @@ python3 -m pip install -q --upgrade pip pip install -q -r $REQUIREMENTS # Run the model -echo "==== `date`: Running model" &>> "$LOG_FILE" -python3 $APP_PATH/PSILARTEMP.py &>> "$LOG_FILE" -echo "==== `date`: DONE running model" &>> "$LOG_FILE" + +if [ -z "${year}" ]; then + 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