From 747128b6825047a0ac7ef4ed8d5c7d7b38057c98 Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@nibio.no>
Date: Fri, 17 Nov 2023 10:38:32 +0100
Subject: [PATCH] Check weatherdata source files for timesteps. Close a file.

---
 SEPTREFHUM.py | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/SEPTREFHUM.py b/SEPTREFHUM.py
index 4c1da68..3be0c99 100755
--- a/SEPTREFHUM.py
+++ b/SEPTREFHUM.py
@@ -73,6 +73,14 @@ for file_path in weatherdata_files:
     # Only process files from the three last days (if this is not a work from scratch)
     if start_date is not None and wh_sum_date < start_date:
          continue
+    
+    # Check that the file has at least 23 timesteps
+    with nc.Dataset(file_path, 'r') as weatherdata_file:
+        file_timesteps = len(weatherdata_file.variables["time"])
+        if  file_timesteps < 23:
+            print(f"{file_path} has {file_timesteps} timesteps. Skipping it.")
+            continue
+
     # 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")
@@ -98,6 +106,7 @@ for timestep in timesteps:
             print(f"ERROR: Missing weather data between {previous_timestep_str} and {timestep_str}. Exiting.", file=sys.stderr)
             exit(1)
     previous_timestep = timestep
+wh_daysum.close()
 
 # From here, no additive calculation; we calculate from the beginning of the w_daysum.nc file every time. 
 
-- 
GitLab