Skip to content
Snippets Groups Projects
Commit 6f1681b1 authored by Brita Linnestad's avatar Brita Linnestad
Browse files

Change logic when counting tif-files for mapfile creation

parent 9591fc27
No related branches found
No related tags found
No related merge requests found
......@@ -39,13 +39,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"{outtmp_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')
......@@ -82,9 +82,10 @@ def create_dataset():
create_matrix()
create_warning_status(start_date)
def create_warning_status(start_date):
risk_files = glob.glob(f"{outfile_path}/RISK_*.nc")
final_files = glob.glob(f"{outtmp_path}/final_*.nc")
# Classifying warning status for the WHS model
# 0 == RISK --> Grey
......@@ -92,15 +93,12 @@ def create_warning_status(start_date):
# 1 <= RISK < 2.5 --> Orange
# 2.5 <= RISK --> Red
timestep_dates = [] # Used in the mapfile template
for file in risk_files:
for file in final_files:
subprocess.run(f'cdo -aexpr,"WARNING_STATUS = RISK < 0 ? 0 : -1; WARNING_STATUS = RISK <= 1 && WARNING_STATUS == -1 ? 2 : WARNING_STATUS; WARNING_STATUS = RISK <= 2.5 && WARNING_STATUS == -1 ? 3 : WARNING_STATUS;WARNING_STATUS = RISK >= 2.5 && WARNING_STATUS == -1 ? 4 : WARNING_STATUS" -timmax {file} {tmpfile_path}result_unmasked.nc', shell=True)
file_name = os.path.basename(file)
file_date = file_name[file_name.index("RISK_result_")+12:file_name.index("RISK_result_")+22]
file_date = file_name[file_name.index("final_")+6:file_name.index("final_")+16]
timestep_dates.append(file_date)
# Mask results using a CSV file with polygons
# Env variable MASK_FILE must be set
if os.getenv("MASK_FILE") is not None:
......@@ -122,6 +120,14 @@ def create_warning_status(start_date):
subprocess.run(f'gdalwarp -t_srs EPSG:4326 {tmpfile_path}RISK_result_{file_date}.tif {outfile_path}RISK_result_{file_date}.tif', shell=True, stdout=devnull)
subprocess.run(f'gdalwarp -t_srs EPSG:4326 {tmpfile_path}IR_result_{file_date}.tif {outfile_path}IR_result_{file_date}.tif', shell=True, stdout=devnull)
#Count tif-files to add to mapfile
risk_files = glob.glob(f"{outfile_path}/RISK_*.tif")
timestep_dates = [] # Used in the mapfile template
for file in risk_files:
file_name = os.path.basename(file)
file_date = file_name[file_name.index("RISK_result_")+12:file_name.index("RISK_result_")+22]
timestep_dates.append(file_date)
# Generate mapfile
# Building data sets for language specific legends
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment