@@ -164,17 +164,30 @@ def download_era5() -> None:
164
164
return
165
165
166
166
print ('converting to daily cumulative' )
167
- for downloaded_file in downloaded_files :
168
- daily_cumulative_file_name = os .path .basename (downloaded_file ).replace ('.nc' , '_daily_cumulative.nc' )
169
- with xr .open_dataset (downloaded_file ) as ds :
170
- print (f'processing { downloaded_file } ' )
171
-
172
- if ds ['time' ].shape [0 ] == 0 :
173
- print (f'No time steps were downloaded- the shape of the time array is 0.' )
174
- print (f'Removing { downloaded_file } ' )
175
- os .remove (downloaded_file )
176
- continue
177
-
167
+ year_1 = year_month_combos [0 ][0 ]
168
+ month_1 = year_month_combos [0 ][1 ]
169
+ if len (year_month_combos ) > 1 :
170
+ year_2 = year_month_combos [1 ][0 ]
171
+ month_2 = year_month_combos [1 ][1 ]
172
+ else :
173
+ year_2 = year_1
174
+ month_2 = month_1
175
+ day_1 = min ({d .day for d in date_range if d .year == year_1 and d .month == month_1 })
176
+ day_2 = max ({d .day for d in date_range if d .year == year_2 and d .month == month_2 })
177
+ daily_cumulative_file_name = f'era5_{ year_1 } { str (month_1 ).zfill (2 )} { str (day_1 ).zfill (2 )} -{ year } { str (month_2 ).zfill (2 )} { str (day_2 ).zfill (2 )} _daily_cumulative.nc'
178
+ with xr .open_mfdataset (downloaded_files ,
179
+ concat_dim = 'time' ,
180
+ combine = 'nested' ,
181
+ parallel = True ,
182
+ chunks = {'time' :'auto' , 'lat' :'auto' ,'lon' :'auto' }, # Included to prevent weird slicing behavior and missing data
183
+ ) as ds :
184
+ print (f'processing { ", " .join (downloaded_files )} ' )
185
+
186
+ if ds ['time' ].shape [0 ] == 0 :
187
+ print (f'No time steps were downloaded- the shape of the time array is 0.' )
188
+ print (f'Removing { ", " .join (downloaded_files )} ' )
189
+ {os .remove (downloaded_file ) for downloaded_file in downloaded_files }
190
+ else :
178
191
if 'expver' in ds .dims :
179
192
print ('expver in dims' )
180
193
# find the time steps where the runoff is not nan when expver=1
@@ -211,10 +224,10 @@ def download_era5() -> None:
211
224
ds .to_netcdf (os .path .join (era_dir , daily_cumulative_file_name ))
212
225
print (f'uploading { daily_cumulative_file_name } ' )
213
226
subprocess .call (['aws' , 's3' , 'cp' , os .path .join (era_dir , daily_cumulative_file_name ),
214
- os .path .join (s3_era_bucket , os .path .basename (downloaded_file ))])
227
+ os .path .join (s3_era_bucket , os .path .basename (daily_cumulative_file_name ))])
215
228
216
- # remove the original file
217
- os .remove (downloaded_file )
229
+ # remove the original files
230
+ { os .remove (downloaded_file ) for downloaded_file in downloaded_files }
218
231
219
232
# remove the consolidated file
220
233
os .remove (os .path .join (era_dir , daily_cumulative_file_name ))
0 commit comments