From 271ed5058ea6bccb90753eab48472caf4876b1b0 Mon Sep 17 00:00:00 2001
From: Lene Wasskog <lene.wasskog@nibio.no>
Date: Thu, 27 Feb 2025 08:53:38 +0100
Subject: [PATCH] chore: Log output of cdo command

---
 SEPTREFHUM.py | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/SEPTREFHUM.py b/SEPTREFHUM.py
index 59c93d8..97408ef 100755
--- a/SEPTREFHUM.py
+++ b/SEPTREFHUM.py
@@ -32,6 +32,28 @@ import configparser
 import logging
 import shutil
 
+def run_command(command, stdout=subprocess.PIPE):
+    logging.debug(f"{command}")
+    try:
+        result = subprocess.run(
+            command,
+            stdout=stdout,
+            stderr=stdout,
+            shell=True,
+            text=True,
+            check=True,
+        )
+        if result.stdout:
+            result_lines = result.stdout.splitlines()
+            for line in result_lines:
+                logging.debug(line.strip())
+            return result_lines
+        if result.stderr:
+            for line in result.stderr.splitlines():
+                logging.error(line.strip())
+    except subprocess.CalledProcessError as e:
+        logging.error(f"{e}")
+
 # Paths config
 # Create a .env file from dotenv-sample
 load_dotenv()
@@ -87,7 +109,7 @@ logging.basicConfig(
 start_date = None
 last_wh_date = None
 #"""
-for wh_file in glob.glob(f"{tmpfile_path}wh_2[0-9][0-9][0-9]-[01][0-9]-[0123][0-9].nc"):          
+for wh_file in glob.glob(f"{tmpfile_path}wh_{year}-[01][0-9]-[0123][0-9].nc"):
     current_wh_file_date = local_timezone.localize(datetime.strptime(f"{wh_file[12:22]}", "%Y-%m-%d"))
     if last_wh_date is None or last_wh_date < current_wh_file_date:
        last_wh_date = current_wh_file_date
@@ -124,11 +146,9 @@ for file_path in sorted(weatherdata_files):
     # Produce daily files with WH_SUM, which is the number of "Wet hours" (WH) for a given day
     # WH is defined as RR > 0.2 || UM > 88.0
     wh_sum_date_str = wh_sum_date.strftime("%Y-%m-%d")
-    wh_sum_hour_str = wh_sum_date.strftime("%H") 
-    subprocess.run(
-        f'cdo -s -O -setdate,{wh_sum_date_str} -settime,{wh_sum_hour_str}:00:00 -chname,WH,WH_DAYSUM -timsum -selname,WH -aexpr,"WH = ({TM} >= {TEMPERATURE_THRESHOLD} && ({RR} > 0.2 || {UM} > 88.0)) ? 1 : 0;" {file_path} {tmpfile_path}wh_{wh_sum_date_str}.nc',
-        shell=True
-        )
+    wh_sum_hour_str = wh_sum_date.strftime("%H")
+    logging.info("Run WH calculations for " + wh_sum_date_str)
+    run_command(command=f'cdo -s -O -setdate,{wh_sum_date_str} -settime,{wh_sum_hour_str}:00:00 -chname,WH,WH_DAYSUM -timsum -selname,WH -aexpr,"WH = ({TM} >= {TEMPERATURE_THRESHOLD} && ({RR} > 0.2 || {UM} > 88.0)) ? 1 : 0;" {file_path} {tmpfile_path}wh_{wh_sum_date_str}.nc')
 
 # Concatenate daily files > one file with daily values
 # Additive calculation - this file can be stored between calculations
-- 
GitLab