From c08ea4dff0a1b912fa1d0024d31881d022710e08 Mon Sep 17 00:00:00 2001 From: brli <brita.linnestad@nibio.no> Date: Mon, 27 Nov 2023 14:49:40 +0100 Subject: [PATCH] Use numpy to count number of timesteps --- NAERSTADMO.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/NAERSTADMO.py b/NAERSTADMO.py index 6f90f94..9dcec55 100644 --- a/NAERSTADMO.py +++ b/NAERSTADMO.py @@ -9,6 +9,7 @@ import logging import pytz import netCDF4 as nc +import numpy as np logging.basicConfig(level=logging.INFO) @@ -25,8 +26,6 @@ prepareWHS = (f'{tmpfile_path}prepare_WHS.nc') utc_offset = "+02:00" local_timezone = pytz.timezone("Europe/Oslo") -subprocess.run(f"rm {tmpfile_path}*", shell=True) - filename = (f'{tmpfile_path}weather_data.nc') def create_dataset(): @@ -34,12 +33,13 @@ def create_dataset(): # Find the latest file from previous run to create a start date last_final_date = None list_of_files = glob.glob(f"{outfile_path}final_2[0-9][0-9][0-9]-[01][0-9]-[0123][0-9].nc", recursive=True) - + print(list_of_files) if list_of_files: latest_file = max(list_of_files,key=os.path.getctime) file_name = os.path.basename(latest_file) file_date = file_name[file_name.index("final_")+6:file_name.index("final_")+16] last_final_date=file_date + print(last_final_date) if last_final_date is None or last_final_date < file_date: start_date = datetime.strptime(os.getenv("START_DATE"),'%Y-%m-%d') @@ -57,7 +57,7 @@ def create_dataset(): end_date = None end_date = start_date + timedelta(days=5) - if(file_date > start_date.strftime('%Y-%m-%d') and file_date < end_date.strftime('%Y-%m-%d')): + if(file_date >= start_date.strftime('%Y-%m-%d') and file_date <= end_date.strftime('%Y-%m-%d')): if(os.path.exists(f'{tmpfile_path}weather_data.nc') != True): subprocess.run(f'cp {file} {tmpfile_path}weather_data.nc', shell=True) @@ -66,6 +66,8 @@ def create_dataset(): subprocess.run(f'cdo -f nc2 mergetime {file} {tmpfile_path}tmpfile.nc {tmpfile_path}weather_data.nc', shell=True) subprocess.run(f'rm {tmpfile_path}tmpfile.nc',shell=True) + subprocess.run(f'rm {outfile_path}final_*',shell=True) + create_saturation() create_pressure() create_wvd() @@ -103,6 +105,7 @@ def create_warning_status(start_date): os.rename(f"{tmpfile_path}result_unmasked.nc", f"{tmpfile_path}result{file_date}.nc") if(file_date > start_date.strftime('%Y-%m-%d')): + with open("/dev/null", "w") as devnull: subprocess.run(f'gdal_translate -ot Int16 -of GTiff NETCDF:"{tmpfile_path}result_{file_date}.nc":WARNING_STATUS {tmpfile_path}result_WARNING_STATUS_{file_date}.tif', shell=True) subprocess.run(f'gdal_translate -ot Float32 -of GTiff NETCDF:"{tmpfile_path}result_{file_date}.nc":RISK {tmpfile_path}RISK_result_{file_date}.tif', shell=True) @@ -140,11 +143,17 @@ def create_warning_status(start_date): with open(f"{mapfile_outdir}/NAERSTADMO.map", 'w') as f: f.write(output) + subprocess.run(f"rm {tmpfile_path}*", shell=True) + def create_matrix(): - for time in range(160): + dataset=nc.Dataset(f'{tmpfile_path}background_data.nc', 'r') + data = dataset.variables['time'][:] + ntimesteps = np.size(data) + + for time in range(ntimesteps-5): # Need to cut the last day before the end du to WH calculations # For the first hour, all LastHour values are set to 0 if time==0: -- GitLab