From 833e197ec88f91f05ba9378e7915998ffe5525b8 Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@nibio.no>
Date: Wed, 20 Mar 2024 10:28:51 +0100
Subject: [PATCH] Add recurring start date setting

---
 PSILARTEMP.py | 9 ++++++++-
 env-sample    | 2 ++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/PSILARTEMP.py b/PSILARTEMP.py
index b3fd6c1..6f7158a 100755
--- a/PSILARTEMP.py
+++ b/PSILARTEMP.py
@@ -32,6 +32,7 @@ config = configparser.ConfigParser()
 config.read("PSILARTEMP.cfg")
 
 year = os.getenv("YEAR")
+recurring_start_date = os.getenv("RECURRING_START_DATE")
 weatherdata_path = os.getenv("WEATHER_DATA_DIR")
 tmp_path = "tmp/"
 out_path = os.getenv("DATA_DIR")
@@ -39,11 +40,17 @@ out_path = os.getenv("DATA_DIR")
 # TODO: Put timezone out in .env
 local_timezone = pytz.timezone("Europe/Oslo")
 
+# 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:
+    print(f"Today is before the configured start time of {start_time}. Exiting.")
+    exit(0)
+    
 # """
 # Calculate cumulated degree days above 5 degrees after 1st of April
 # Remove all values before April 1st
 subprocess.run(
-    f"cdo -selname,TM -seldate,{year}-04-01T00:00:00,{year}-12-31T00:00:00 {weatherdata_path}met_1_0km_nordic-{year}.nc {tmp_path}TM_from_april.nc",
+    f"cdo -selname,TM -seldate,{year}-{recurring_start_date}T00:00:00,{year}-12-31T00:00:00 {weatherdata_path}met_1_0km_nordic-{year}.nc {tmp_path}TM_from_april.nc",
     shell=True,
 )
 # Subtracting 5 deg C from all cells
diff --git a/env-sample b/env-sample
index 0388874..e0f2cad 100644
--- a/env-sample
+++ b/env-sample
@@ -2,6 +2,8 @@
 
 # Year to run for
 YEAR=2024
+# Recurring start date (MM-DD)
+RECURRING_START_DATE=04-01
 # Where your application resides
 HOME_DIR=/home/foo/
 # Path to the weather data
-- 
GitLab