-
Notifications
You must be signed in to change notification settings - Fork 1
/
processing_era5.py
264 lines (239 loc) · 8.19 KB
/
processing_era5.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# Python script that converts variables from era5 to forcing for FESOM2
# Based on work done by Lorenzo Zampieri
# Import modules
import sys
import os
import shutil
import logging
from rich.logging import RichHandler
log = logging.getLogger("rich")
# import xarray as xr
from cdo import *
cdo = Cdo() # python version of cdo
# Some initial checks
if len(sys.argv) != 3:
print("Error: check the number of input parameters")
sys.exit(1)
# Read var and year as strings from input
var = str(sys.argv[1])
yyyy = str(sys.argv[2])
# List of variables
var_list = ["q", "u", "v", "t2m", "sp", "tcc", "ssrd", "strd", "sf", "rf"]
if int(yyyy) > 2023 or int(yyyy) < 1950:
print("Error: year out of range [1950,2020]")
sys.exit(1)
if var not in var_list:
print("Error: inappropriate variable")
sys.exit(1)
data_folder = "/pool/data/AWICM/FESOM2/FORCING/era5/data/"
forcing_folder = "/pool/data/AWICM/FESOM2/FORCING/era5/forcing/"
simple_vars = {"u": "165", "v": "166", "t2m": "167", "sp": "134"}
radiation_vars = {"ssrd": "169", "strc": "211", "strd": "175"}
if var in simple_vars:
nvar = simple_vars[var]
oldvar = "var" + nvar
infile = f"{data_folder}/{yyyy}/{nvar}/var{nvar}.{yyyy}.nc"
outfile = f"{forcing_folder}/inverted/{var}.{yyyy}.nc"
cdo.setname(
var,
input="-invertlat -setreftime,1900-01-01,00:00:00,1day " + infile,
output=outfile,
options="-f nc4 -b F32 -P 8",
)
if var == "q":
nvar1 = "168"
nvar2 = "134"
log.info(f"Processing variable {var}")
log.info(f"Source GRIB vars: {nvar1} and {nvar2}")
tmp_folder = forcing_folder + "/tmp" + var + yyyy
if not os.path.exists(tmp_folder):
os.mkdir(tmp_folder)
infile1 = f"{data_folder}/{yyyy}/{nvar1}/var{nvar1}.{yyyy}.nc"
infile2 = f"{data_folder}/{yyyy}/{nvar2}/var{nvar2}.{yyyy}.nc"
tmp1 = f"{forcing_folder}/tmp" + var + yyyy + "/tmp1.nc"
tmp2 = tmp_folder + "/tmp2.nc"
tmp3 = tmp_folder + "/tmp3.nc" # e
tmp4 = tmp_folder + "/tmp4.nc"
outfile = f"{forcing_folder}/inverted/{var}.{yyyy}.nc"
# dst = xr.open_dataset(infile1)
# dsp = xr.open_dataset(infile2)
a1, a3, a4, t0, Rdv, mRdv = 611.21, 17.502, 32.19, 273.16, 0.62198, 0.37802
cdo.subc(t0, input=infile1, output=tmp1, options="-f nc4 -b F32 -P 8")
cdo.subc(a4, input=infile1, output=tmp2, options="-f nc4 -b F32 -P 8")
cdo.mulc(
a1,
input="-exp -mulc," + str(a3) + " -div " + tmp1 + " " + tmp2,
output=tmp3,
options="-f nc4 -b F32 -P 8",
)
cdo.setname(
var,
input="-invertlat -setreftime,1900-01-01,00:00:00,1day -mulc,"
+ str(Rdv)
+ " -div "
+ tmp3
+ " -sub "
+ infile2
+ " -mulc,"
+ str(mRdv)
+ " "
+ tmp3,
output=outfile,
options="-f nc4 -b F32 -P 8",
)
shutil.rmtree(tmp_folder)
if var in radiation_vars:
log.info(f"Processing variable {var}")
tmp_folder = forcing_folder + "/tmp" + var + yyyy
outfile = f"{forcing_folder}/inverted/{var}.{yyyy}.nc"
if not os.path.exists(tmp_folder):
os.mkdir(tmp_folder)
# Current year
nvar1 = radiation_vars[var]
infile1 = f"{data_folder}/{yyyy}/{nvar1}/var{nvar1}.{yyyy}.nc"
outfile_p1 = f"{tmp_folder}/{var}.{yyyy}.nc"
cdo.setname(
var,
input="-divc,3600 " + infile1,
output=outfile_p1,
options="-f nc4 -b F32 -P 8",
)
# Previous year
infile2 = f"{data_folder}/{int(yyyy)-1}/{nvar1}/var{nvar1}.{int(yyyy)-1}.nc"
if os.path.exists(infile2):
log.info(f"Previous year available for variable {var}")
outfile_p2 = f"{tmp_folder}/{var}.{int(yyyy)-1}.nc"
cdo.setname(
var,
input="-divc,3600 " + infile2,
output=outfile_p2,
options="-f nc4 -b F32 -P 8",
)
cdo.selyear(
yyyy,
input="-invertlat -setreftime,1900-01-01,00:00:00,1day -settaxis,"
+ yyyy
+ "-01-01,00:30:00,1hour -mergetime -seltimestep,8755/8760 "
+ outfile_p2
+ " "
+ outfile_p1,
output=outfile,
options="-f nc4 -b F32 -P 8",
)
else:
# this is the first year of the time series
print(f"File {infile2} does not exist, only using {infile1}")
cdo.selyear(
yyyy,
input="-invertlat -setreftime,1900-01-01,00:00:00,1day -settaxis,"
+ yyyy
+ "-01-01,00:30:00,1hour -seltimestep,8755/8760 "
+ outfile_p1,
output=outfile,
options="-f nc4 -b F32 -P 8",
)
shutil.rmtree(tmp_folder)
if var == "rf":
nvar1 = "142"
nvar2 = "143"
log.info(f"Processing variable {var}")
log.info(f"Source GRIB vars: {nvar1} and {nvar2}")
tmp_folder = forcing_folder + "/tmp" + var + yyyy
outfile = f"{forcing_folder}/inverted/{var}.{yyyy}.nc"
if not os.path.exists(tmp_folder):
os.mkdir(tmp_folder)
# Current year
infile1 = f"{data_folder}/{yyyy}/{nvar1}/var{nvar1}.{yyyy}.nc"
infile2 = f"{data_folder}/{yyyy}/{nvar2}/var{nvar2}.{yyyy}.nc"
outfile_p1 = f"{tmp_folder}/{var}.{yyyy}.nc"
cdo.setname(
var,
input="-divc,3.6 -add " + infile1 + " " + infile2,
output=outfile_p1,
options="-f nc4 -b F32 -P 8",
)
# Previous year
infile11 = f"{data_folder}/{int(yyyy)-1}/{nvar1}/var{nvar1}.{int(yyyy)-1}.nc"
infile22 = f"{data_folder}/{int(yyyy)-1}/{nvar2}/var{nvar2}.{int(yyyy)-1}.nc"
if os.path.exists(infile11) and os.path.exists(infile22):
log.info(f"Previous year available for variable {var}")
outfile_p2 = f"{tmp_folder}/{var}.{int(yyyy)-1}.nc"
cdo.setname(
var,
input="-divc,3.6 -add " + infile11 + " " + infile22,
output=outfile_p2,
options="-f nc4 -b F32 -P 8",
)
cdo.selyear(
yyyy,
input="-invertlat -setreftime,1900-01-01,00:00:00,1day -settaxis,"
+ yyyy
+ "-01-01,00:30:00,1hour -mergetime -seltimestep,8755/8760 "
+ outfile_p2
+ " "
+ outfile_p1,
output=outfile,
options="-f nc4 -b F32 -P 8",
)
else:
print("Previous year not available")
cdo.selyear(
yyyy,
input="-invertlat -setreftime,1900-01-01,00:00:00,1day -settaxis,"
+ yyyy
+ "-01-01,00:30:00,1hour "
+ outfile_p1,
output=outfile,
options="-f nc4 -b F32 -P 8",
)
shutil.rmtree(tmp_folder)
if var == "sf":
log.info(f"Processing variable {var}")
tmp_folder = forcing_folder + "/tmp" + var + yyyy
outfile = f"{forcing_folder}/inverted/{var}.{yyyy}.nc"
if not os.path.exists(tmp_folder):
os.mkdir(tmp_folder)
# Current year
nvar1 = "144"
infile1 = f"{data_folder}/{yyyy}/{nvar1}/var{nvar1}.{yyyy}.nc"
outfile_p1 = f"{tmp_folder}/{var}.{yyyy}.nc"
cdo.setname(
var,
input="-divc,3.6 " + infile1,
output=outfile_p1,
options="-f nc4 -b F32 -P 8",
)
# Previous year
infile11 = f"{data_folder}/{int(yyyy)-1}/{nvar1}/var{nvar1}.{int(yyyy)-1}.nc"
if os.path.exists(infile11):
log.info(f"Previous year available for variable {var}")
outfile_p2 = f"{tmp_folder}/{var}.{int(yyyy)-1}.nc"
cdo.setname(
var,
input="-divc,3.6 " + infile11,
output=outfile_p2,
options="-f nc4 -b F32 -P 8",
)
cdo.selyear(
yyyy,
input="-invertlat -setreftime,1900-01-01,00:00:00,1day -settaxis,"
+ yyyy
+ "-01-01,00:30:00,1hour -mergetime -seltimestep,8755/8760 "
+ outfile_p2
+ " "
+ outfile_p1,
output=outfile,
options="-f nc4 -b F32 -P 8",
)
else:
print("Previous year not available")
cdo.selyear(
yyyy,
input="-invertlat -setreftime,1900-01-01,00:00:00,1day -settaxis,"
+ yyyy
+ "-01-01,00:30:00,1hour "
+ outfile_p1,
output=outfile,
options="-f nc4 -b F32 -P 8",
)
shutil.rmtree(tmp_folder)