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

chore: Log output of cdo command

parent 10f2a4e6
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment