-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck.py
167 lines (138 loc) · 5.97 KB
/
check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#%%
import xarray as xr
import dask
dask.config.set(scheduler='threads')
file_pet = r'/p/11208719-interreg/data/racmo/members_bias_corrected_revised/b_preprocess/hourly/r4i1p5f1/pet/pet.KNMI-2004.r4i1p5f1_regrid_meuse.nc'
file_temp = r'/p/11208719-interreg/data/racmo/members_bias_corrected_revised/b_preprocess/hourly/r4i1p5f1/t2m/t2m.KNMI-2004.r4i1p5f1_regrid_meuse.nc'
file_precip = r'/p/11208719-interreg/data/racmo/members_bias_corrected_revised/b_preprocess/hourly/r4i1p5f1/precip/precip.KNMI-2004.r4i1p5f1_regrid_meuse.nc'
outfile_path = r'/p/11208719-interreg/data/racmo/members_bias_corrected_revised/c_wflow/hourly/r4i1p5f1/ds_merged_2004.nc'
#%%
path_dict={'pet': file_pet,
't2m': file_temp,
'precip': file_precip}
conv_params = {
'pet': {
'daily': 86400,
'hourly': 3600,
'conversion': "multiplicative",
'expected_original_units': ['kg m-2 s-1','kg/m2/s'] , #Attributes of the units in the cdo netcdf file
'new_units': 'mm/timestep',
'wflow_name': "pet"},
'precip':{
'daily': 86400,
'hourly': 3600,
'conversion': "multiplicative",
'expected_original_units': ['kg m-2 s-1','kg/m2/s'],
'new_units': 'mm/timestep',
'wflow_name': "precip"},
't2m':{
'daily': -273.15,
'hourly': -273.15,
'conversion': "additive",
'expected_original_units': ['K'],
'new_units': 'C',
'wflow_name': "temp"}}
#%%
dt = 'hourly'
#%% Functions
def check_units(ds, dict_vars):
'''
Checking if the units of each variables is what we expect before doing the transformation
ds: merged netcdf file with the name of the original variables
dict_vars: dictionnary with as major key the name of the original variable and subkey 'expected_original_units' that mentions the units of the variable
'''
for var in ds.data_vars:
print(var)
if ds[var].attrs['units'] in dict_vars[var]['expected_original_units']:
print("units is good")
continue
else:
raise ValueError("Units do not match - check f{var}")
def convert_to_wflow_units(ds, dict_vars, dt):
'''
Converting the original units to wflow units as indicated in the snakemake config file and updating the units attributes
ds: merged netcdf file with the name of the original variables
dict_vars: dictionnary with as major key the name of the original variable and subkeys indicate the timestep and conversion type (multiplicative or additive)
'''
for var in ds.data_vars:
print(var)
if dict_vars[var]['conversion'] == 'additive':
ds[var] = ds[var] + dict_vars[var][dt] #going from Kelvin to C
ds[var].attrs['units'] = dict_vars[var]['new_units']
print("additive conversion done")
elif dict_vars[var]['conversion'] == 'multiplicative':
ds[var] = ds[var] * dict_vars[var][dt] #going from kg/m2/s to mm/tijdstap
ds[var].attrs['units'] = dict_vars[var]['new_units']
print("multiplicative conversion done")
else:
print(f"No conversion happened for {var}")
return ds
#%%
%%time
#We open the datasets
ds_merge = xr.open_mfdataset([file_temp, file_pet, file_precip], chunks='auto', combine='nested') #.load() # chunks = {"time":1000} #ds.chunks
#ds_merge = xr.open_mfdataset([file_temp, file_pet, file_precip], chunks='auto')
if 'height' in ds_merge.coords.keys():
ds_merge = ds_merge.squeeze().drop_vars('height') #We remove the height dimension - not needed here
#We remove the units attributes from the main attributes as this is confusing
del ds_merge.attrs['units']
#We add each attributes as variables attributes
for var in ds_merge.data_vars:
print(var)
ds_var = xr.open_dataset(path_dict[var], chunks='auto')
ds_merge[var].attrs = ds_var.attrs
ds_var.close()
#We check the units in the original file
check_units(ds_merge, conv_params)
#We do the conversion according to the values in the snakemake config file
convert_to_wflow_units(ds_merge, conv_params, dt)
#We rename the variables in accordance to wflow standards
for var in ds_merge.data_vars:
print(var)
ds_merge = ds_merge.rename({var: conv_params[var]['wflow_name']})
#encoding otherwise wflow doesn't run
chunksizes = (1, ds_merge.lat.size, ds_merge.lon.size)
encoding = {
v: {"zlib": True, "dtype": "float32", "chunksizes": chunksizes}
for v in ds_merge.data_vars.keys()
}
encoding["time"] = {"_FillValue": None}
#%% We save the output
ds_merge.to_netcdf(outfile_path, encoding=encoding)
ds_merge.close()
print("Done")
# %%
%%time
#We open the datasets
ds_merge = xr.open_mfdataset([file_temp, file_pet, file_precip], chunks = {"time":2000}, combine='nested') # chunks = {"time":1000} #ds.chunks
if 'height' in ds_merge.coords.keys():
ds_merge = ds_merge.squeeze().drop_vars('height') #We remove the height dimension - not needed here
#We remove the units attributes from the main attributes as this is confusing
del ds_merge.attrs['units']
#We add each attributes as variables attributes
for var in ds_merge.data_vars:
print(var)
ds_var = xr.open_dataset(path_dict[var], chunks = {"time":2000})
ds_merge[var].attrs = ds_var.attrs
ds_var.close()
#We check the units in the original file
check_units(ds_merge, conv_params)
#We do the conversion according to the values in the snakemake config file
convert_to_wflow_units(ds_merge, conv_params, dt)
#We rename the variables in accordance to wflow standards
for var in ds_merge.data_vars:
print(var)
ds_merge = ds_merge.rename({var: conv_params[var]['wflow_name']})
ds_merge.compute()
#encoding otherwise wflow doesn't run
chunksizes = (1, ds_merge.lat.size, ds_merge.lon.size)
encoding = {
v: {"zlib": True, "dtype": "float32", "chunksizes": chunksizes}
for v in ds_merge.data_vars.keys()
}
encoding["time"] = {"_FillValue": None}
#%% We save the output
ds_merge.to_netcdf(outfile_path, encoding=encoding)
ds_merge.close()
print("Done")
# %%