Skip to content

Commit 1da96b3

Browse files
committed
add hurs calculation from t2m and d2m
1 parent 931eefd commit 1da96b3

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import operations
2+
import pandas as pd
3+
import xarray as xr
4+
import glob
5+
import os
6+
import logging
7+
from pathlib import Path
8+
import sys
9+
sys.path.append('../utilities')
10+
from utils import load_output_path_from_row, require_single_row
11+
12+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
13+
14+
def main():
15+
dataset="derived-era5-single-levels-daily-statistics"
16+
variables_file_path = f"../../requests/{dataset}.csv"
17+
df_parameters = pd.read_csv(variables_file_path)
18+
derived_variables = df_parameters[df_parameters['product_type'] == 'derived']['filename_variable']
19+
derived_variables_list = derived_variables.tolist()
20+
logging.info(f"Derived variables to process: {derived_variables_list}")
21+
for var in derived_variables_list:
22+
logging.info(f"Calculating {var}")
23+
mask_var = (df_parameters['filename_variable'] == var) & (df_parameters['product_type'] == 'derived')
24+
var_row = require_single_row(df_parameters, mask_var, f"{var}/derived")
25+
26+
# Create a list of years from start to end
27+
year_list = list(range(var_row["cds_years_start"].squeeze() , var_row["cds_years_end"].squeeze() + 1))
28+
for year in year_list:
29+
30+
if var == "hurs":
31+
input_row_d2m = require_single_row(df_parameters, (df_parameters['filename_variable'] == "d2m") & (df_parameters['product_type'] == 'raw'), "d2m/raw")
32+
input_row_t2m = require_single_row(df_parameters, (df_parameters['filename_variable'] == "t2m") & (df_parameters['product_type'] == 'raw'), "t2m/raw")
33+
# Use utility function to load input paths
34+
d2m_download_path = load_output_path_from_row(input_row_d2m, dataset)
35+
d2m_file = glob.glob(f"{d2m_download_path}/*{year}*.nc")[0]
36+
t2m_download_path = load_output_path_from_row(input_row_t2m, dataset)
37+
t2m_file = glob.glob(f"{t2m_download_path}/*{year}*.nc")[0]
38+
# Use utility function to build output path
39+
dest_dir = load_output_path_from_row(var_row, dataset)
40+
os.makedirs(dest_dir, exist_ok=True)
41+
hurs_file = os.path.basename(d2m_file).replace("d2m", "hurs")
42+
output_file=Path(f"{dest_dir}/{hurs_file}")
43+
logging.info(f"output_file: {output_file}")
44+
if output_file.exists():
45+
logging.info(f"File {output_file} already exists. Skipping...")
46+
continue
47+
logging.info(f"Calculating hurs from {d2m_file} and {t2m_file}")
48+
ds_d2m = xr.open_dataset(d2m_file)
49+
ds_t2m = xr.open_dataset(t2m_file)
50+
ds_merge = xr.merge([ds_d2m, ds_t2m])
51+
hurs = operations.rh_from_thermofeel(ds_merge, "d2m", "t2m")
52+
53+
logging.info(f"Saving calculated hurs to {dest_dir}")
54+
hurs.to_netcdf(output_file)
55+
56+
ds_d2m.close()
57+
ds_t2m.close()
58+
hurs.close()
59+
del ds_d2m, ds_t2m, hurs
60+
61+
if __name__ == "__main__":
62+
main()

scripts/derived/operations.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import numpy as np
44
from thermofeel.thermofeel import calculate_relative_humidity_percent
55

6-
# Import the original computation (the function lives in the same package)
7-
86

97
def rh_from_thermofeel(ds: xr.Dataset, td_var: str, t2_var: str) -> xr.Dataset:
108
"""
@@ -47,17 +45,18 @@ def rh_from_thermofeel(ds: xr.Dataset, td_var: str, t2_var: str) -> xr.Dataset:
4745
dask="parallelized",
4846
output_dtypes=[float],
4947
)
50-
48+
# Ensure that RH values are within physical bounds [0, 100]
49+
rh_da = rh_da.clip(min=0.0, max=100.0)
5150
# Name and attributes for the output
52-
rh_da.name = "relative_humidity"
51+
rh_da.name = "hurs"
5352
rh_da.attrs["units"] = "%"
5453
rh_da.attrs["long_name"] = "Relative Humidity"
5554

5655
# Build output dataset (copy ds so we keep coords and any ancillary variables)
5756
ds_out = ds.copy()
5857

5958
# Add the new variable and remove the original ones
60-
ds_out["relative_humidity"] = rh_da
59+
ds_out["hurs"] = rh_da
6160
ds_out = ds_out.drop_vars([td_var, t2_var])
6261

6362
return ds_out

scripts/derived/reanalysis-cerra-land_accumulation.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,15 @@ def accumulation(ds,var):
165165
if output_file.exists():
166166
logging.info(f"File {output_file} already exists. Skipping...")
167167
continue
168-
next_file=var_files[i+1] if i+1 < len(var_files) else None
169-
logging.info(f"Processing file {file} and next file {next_file}")
170-
171-
check_time_gap(file, next_file, expected_timestep='1h')
172-
ds_var = xr.open_mfdataset([file,next_file],concat_dim='valid_time', combine='nested')
168+
if i+2 < len(var_files):
169+
next_file=var_files[i+1]
170+
logging.info(f"Processing file {file} and next file {next_file}")
171+
check_time_gap(file, next_file, expected_timestep='1h')
172+
file_list=[file,next_file]
173+
else:
174+
file_list=[file]
175+
176+
ds_var = xr.open_mfdataset(file_list,concat_dim='valid_time', combine='nested')
173177
ds_accumulated=accumulation(ds_var,var)
174178
first_month_data = get_first_month_accumulated(ds_accumulated)
175179

0 commit comments

Comments
 (0)