-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathera5.py
635 lines (541 loc) · 19.9 KB
/
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
"""
ERA5 reanalysis is downloaded via the Copernicus Data store.
The scripts use ERA5 re-analysis data which are available:
<https://cds.climate.copernicus.eu>. You will need to create
an account and a credentials file to use the API included in
the code (see <https://cds.climate.copernicus.eu/api-how-to>).
"""
import os
import glob
import datetime
import numpy as np
import xarray as xr
import pandas as pd
import cdsapi
import metpy.calc
from metpy.units import units
import load.location_sel as ls
from load.noaa_indices import indice_downloader
from load import data_dir
def collect_ERA5(location: str or tuple, minyear: str, maxyear: str, all_var=False) -> xr.DataArray:
"""
Download data from ERA5 for a given location
Args:
location (str or tuple): location string or lat/lon coordinate tuple
minyear (float): start date in years
maxyear (float): end date in years
Returns:
xr.DataArray: ERA5 data
"""
if type(location) == str:
era5_ds = download_data(location, xarray=True, all_var=all_var)
loc_ds = ls.select_basin(era5_ds, location)
else:
era5_ds = download_data('indus', xarray=True, all_var=all_var)
lon, lat = location
loc_ds = era5_ds.interp(
coords={"lon": lon, "lat": lat}, method="nearest")
tim_ds = loc_ds.sel(time=slice(minyear, maxyear))
ds = tim_ds.assign_attrs(plot_legend="ERA5") # in mm/day
return ds
def gauge_download(station: str, minyear: str, maxyear: str) -> xr.Dataset:
""" Download and format ERA5 data for a given station name in the Beas and Sutlej basins."""
# Load data
era5_da = download_data('beas_sutlej', xarray=True)
era5_ds = era5_da[['tp', 'z']]
# Interpolate at location
all_station_dict = pd.read_csv(
data_dir + 'bs_gauges/gauge_info.csv', index_col='station').T
print(all_station_dict)
lat, lon, _elv = all_station_dict[station]
loc_ds = era5_ds.interp(coords={"lon": lon, "lat": lat}, method="nearest")
tim_ds = loc_ds.sel(time=slice(minyear, maxyear))
return tim_ds
def gauges_download(stations: list, minyear: str, maxyear: str) -> pd.DataFrame:
""" Download and format ERA5 data for a given station list in the Beas and Sutlej basins."""
# Load data
era5_da = download_data('indus', xarray=True)
era5_ds = era5_da[['tp']]
# Interpolate at location
all_station_dict = pd.read_csv(
data_dir + 'bs_gauges/gauge_info.csv', index_col='station').T
loc_list = []
for station in stations:
lat, lon, _elv = all_station_dict[station]
loc_ds = era5_ds.interp(
coords={"lon": lon, "lat": lat}, method="nearest")
tim_ds = loc_ds.sel(time=slice(minyear, maxyear))
loc_df = tim_ds.to_dataframe().reset_index().dropna()
loc_df['z'] = np.ones(len(loc_df)) * _elv
loc_list.append(loc_df)
df = pd.concat(loc_list)
return df
def value_gauge_download(stations: list, minyear: str, maxyear: str) -> xr.DataArray:
"""
Download and format ERA5 data for a given station name in the VALUE dataset.
Args:
stations (list): station names (with first letter capitalised)
minyear (float): start date in years
maxyear (float): end date in years
Returns:
xr.DataArray: ERA5 precipitation values at VALUE stations
"""
# Load data
era5_da = download_data('value', xarray=True)
era5_ds = era5_da[['tp']]
tim_ds = era5_ds.sel(time=slice(minyear, maxyear))
loc_list = []
for station in stations:
# print(station)
station_name = station.upper()
# Interpolate at location
all_station_dict = pd.read_csv(
data_dir + 'VALUE_ECA_86_v2/stations.txt', index_col='name', sep='\t', lineterminator='\r').T
# print(all_station_dict)
_, lon, lat, _elv, _ = all_station_dict[station_name]
loc_ds = tim_ds.interp(
coords={"lon": lon, "lat": lat}, method="nearest")
loc_df = loc_ds.to_dataframe()
loc_df['z'] = np.ones(len(loc_df)) * _elv
loc_list.append(loc_df)
df = pd.concat(loc_list)
return df
def download_data(location, xarray=False, ensemble=False, all_var=False, latest=False):
"""
Downloads data for prepearation or analysis
Inputs
basin_filepath: string
xarray: boolean
ensemble: boolean
all_var: boolean
Returns
df: DataFrame of data, or
ds: DataArray of data
"""
basin = ls.basin_finder(location)
print(basin)
path = data_dir + "ERA5/"
now = datetime.datetime.now()
if latest == False:
if ensemble is True:
# choose first file or make up filename
try:
filepath = glob.glob(
path + "combi_data_ensemble" + "_" + basin + "*.csv")[0]
except:
latest = True
if all_var is True:
try:
filepath = glob.glob(path + "all_data" +
"_" + basin + "*.csv")[0]
except:
latest = True
if ensemble is False:
try:
filepath = glob.glob(path + "combi_data" +
"_" + basin + "*.csv")[0]
except:
latest = True
if latest == True:
if ensemble is True:
filename = "combi_data_ensemble" + "_" + \
basin + "_" + now.strftime("%Y-%m") + ".csv"
if all_var is True:
filename = "all_data" + "_" + basin + \
"_" + now.strftime("%Y-%m") + ".csv"
if ensemble is False:
filename = "combi_data" + "_" + basin + \
"_" + now.strftime("%Y-%m") + ".csv"
filepath = os.path.expanduser(path + filename)
print(filepath)
if not os.path.exists(filepath):
# print(basin)
# Orography, humidity, precipitation and indices
cds_df = cds_downloader(basin, ensemble=ensemble, all_var=all_var)
ind_df = indice_downloader(all_var=all_var)
df_combined = pd.merge_ordered(
cds_df, ind_df, on="time", suffixes=("", "_y"))
# Other variables not used in the GP
if all_var is True:
mean_df = mean_downloader(basin)
uib_eofs_df = eof_downloader(basin, all_var=all_var)
# Combine
df_combined2 = pd.merge_ordered(df_combined, mean_df, on="time")
df_combined = pd.merge_ordered(
df_combined2, uib_eofs_df, on=["time", "latitude", "longitude"]
)
# Choose experiment version 1
expver1 = [c for c in df_combined.columns if c[-1] != '5']
df_expver1 = df_combined[expver1]
df_expver1.columns = df_expver1.columns.str.strip('_0001')
# Pre pre-processing and save
df_clean = df_expver1.dropna() # .drop("expver", axis=1)
dates = pd.to_datetime(df_clean.time)
df_clean['time'] = dates.astype("datetime64[M]")
u = units.meter * units.meter / units.second / units.second
geopot_u = df_clean['z'].values * u
z_u = metpy.calc.geopotential_to_height(geopot_u)
df_clean['z'] = z_u
df_clean["tp"] *= 1000 # to mm/day
df_clean = df_clean.rename(
columns={'latitude': 'lat', 'longitude': 'lon'})
#df_clean = df_clean.astype("float64")
df_clean.to_csv(filepath)
if xarray is True:
if ensemble is True:
df_multi = df_clean.set_index(
["time", "long", "lat", "number"]
)
else:
df_multi = df_clean.set_index(["time", "lon", "lat"])
ds = df_multi.to_xarray()
'''
# Standardise time resolution
maxyear = float(ds.time.max())
minyear = float(ds.time.min())
time_arr = np.arange(round(minyear) + 1./24., maxyear+0.05, 1./12.)
print(ds)
ds['time'] = time_arr
'''
return ds
else:
return df_clean
else:
df = pd.read_csv(filepath)
df_clean = df.drop(columns=["Unnamed: 0"])
if xarray is True:
if ensemble is True:
df_multi = df_clean.set_index(
["time", "lon", "lat", "number"]
)
else:
df_multi = df_clean.set_index(["time", "lon", "lat"])
ds = df_multi.to_xarray()
'''
# Standardise time resolution
maxyear = float(ds.time.max())
minyear = float(ds.time.min())
time_arr = np.arange(round(minyear) + 1./24.,
maxyear + 0.05, 1./12.)
ds['time'] = time_arr
'''
return ds
else:
return df_clean
def mean_downloader(basin):
def mean_formatter(filepath, coords=None, name=None):
""" Returns dataframe averaged data over a optionally given area """
da = xr.open_dataset(filepath)
if "expver" in list(da.dims):
da = da.sel(expver=1)
da = da.drop(["expver"])
if coords is not None:
da = da.sel(
latitude=slice(coords[0], coords[2]),
longitude=slice(coords[1], coords[3]),
)
mean_da = da.mean(dim=["longitude", "latitude"], skipna=True)
clean_da = mean_da.assign_coords(
time=(mean_da.time.astype("datetime64")))
multiindex_df = clean_da.to_dataframe()
df = multiindex_df # .reset_index()
if name is not None:
df.rename(columns={"EOF": name}, inplace=True)
return df
# Temperature
temp_filepath = update_cds_monthly_data(
variables=["2m_temperature"], area=basin, qualifier="temp"
)
temp_df = mean_formatter(temp_filepath)[['t2m']]
#temp_df.rename(columns={"t2m": "d2m"}, inplace=True)
temp_df.reset_index(inplace=True)
# EOFs for 200hPa
eof1_z200_c = mean_formatter(
data_dir + "ERA5/global_200_EOF1.nc",
coords=[40, 60, 35, 70], name="EOF200C1")
eof1_z200_b = mean_formatter(
data_dir + "ERA5/global_200_EOF1.nc",
coords=[19, 83, 16, 93], name="EOF200B1")
eof2_z200_c = mean_formatter(
data_dir + "ERA5/global_200_EOF2.nc",
coords=[40, 60, 35, 70], name="EOF200C2")
eof2_z200_b = mean_formatter(
data_dir + "ERA5/global_200_EOF2.nc",
coords=[19, 83, 16, 93], name="EOF200B2")
# EOFs for 500hPa
eof1_z500_c = mean_formatter(
data_dir + "ERA5/global_500_EOF1.nc",
coords=[40, 60, 35, 70], name="EOF500C1")
eof1_z500_b = mean_formatter(
data_dir + "ERA5/global_500_EOF1.nc",
coords=[19, 83, 16, 93], name="EOF500B1")
eof2_z500_c = mean_formatter(
data_dir + "ERA5/global_500_EOF2.nc",
coords=[40, 60, 35, 70], name="EOF500C2")
eof2_z500_b = mean_formatter(
data_dir + "ERA5/global_500_EOF2.nc",
coords=[19, 83, 16, 93], name="EOF500B2")
# EOFs for 850hPa
eof1_z850_c = mean_formatter(
data_dir + "ERA5/global_850_EOF1.nc",
coords=[40, 60, 35, 70], name="EOF850C1")
eof1_z850_b = mean_formatter(
data_dir + "ERA5/global_850_EOF1.nc",
coords=[19, 83, 16, 93], name="EOF850B1")
eof2_z850_c = mean_formatter(
data_dir + "ERA5/global_850_EOF2.nc",
coords=[40, 60, 35, 70], name="EOF850C2")
eof2_z850_b = mean_formatter(
data_dir + "ERA5/global_850_EOF2.nc",
coords=[19, 83, 16, 93], name="EOF850B2")
eof_df = pd.concat(
[
eof1_z200_b,
eof1_z200_c,
eof2_z200_b,
eof2_z200_c,
eof1_z500_b,
eof1_z500_c,
eof2_z500_b,
eof2_z500_c,
eof1_z850_b,
eof1_z850_c,
eof2_z850_b,
eof2_z850_c,
],
axis=1,
)
eof_df.reset_index(inplace=True)
mean_df = pd.merge_ordered(temp_df, eof_df, on="time")
return mean_df
def eof_downloader(basin, all_var=False):
def eof_formatter(filepath, basin, name=None):
""" Returns DataFrame of EOF over UIB """
da = xr.open_dataset(filepath)
if "expver" in list(da.dims):
da = da.sel(expver=1)
(latmax, lonmin, latmin, lonmax) = ls.basin_extent(basin)
sliced_da = da.sel(latitude=slice(latmax, latmin),
longitude=slice(lonmin, lonmax))
eof_ds = sliced_da.EOF
eof2 = eof_ds.assign_coords(time=(eof_ds.time.astype("datetime64")))
eof_multiindex_df = eof2.to_dataframe()
eof_df = eof_multiindex_df.dropna()
eof_df.rename(columns={"EOF": name}, inplace=True)
return eof_df
# EOF UIB
eof1_z200_u = eof_formatter(
data_dir + "ERA5/global_200_EOF1.nc", basin, name="EOF200U1"
)
eof1_z500_u = eof_formatter(
data_dir + "ERA5/global_500_EOF1.nc", basin, name="EOF500U1"
)
eof1_z850_u = eof_formatter(
data_dir + "ERA5/global_850_EOF1.nc", basin, name="EOF850U1"
)
eof2_z200_u = eof_formatter(
data_dir + "ERA5/global_200_EOF2.nc", basin, name="EOF200U2"
)
eof2_z500_u = eof_formatter(
data_dir + "ERA5/global_500_EOF2.nc", basin, name="EOF500U2"
)
eof2_z850_u = eof_formatter(
data_dir + "ERA5/global_850_EOF2.nc", basin, name="EOF850U2"
)
uib_eofs = pd.concat(
[eof1_z200_u, eof2_z200_u, eof1_z500_u,
eof2_z500_u, eof1_z850_u, eof2_z850_u],
axis=1,
)
return uib_eofs
def cds_downloader(basin, ensemble=False, all_var=False):
""" Return CDS Dataframe """
if ensemble is False:
cds_filepath = update_cds_monthly_data(area=basin)
else:
cds_filepath = update_cds_monthly_data(
product_type="monthly_averaged_ensemble_members", area=basin)
da = xr.open_dataset(cds_filepath)
if "expver" in list(da.dims):
da = da.sel(expver=1)
multiindex_df = da.to_dataframe()
cds_df = multiindex_df.reset_index()
return cds_df
def standardised_time(dataset):
"""
FOR ARCHIVE ONLY - NOT IN USE
Returns array of standardised times to plot.
"""
try:
utime = dataset.time.values.astype(int)/(1e9 * 60 * 60 * 24 * 365)
except Exception:
time = np.array([d.strftime() for d in dataset.time.values])
time2 = np.array([datetime.datetime.strptime(
d, "%Y-%m-%d %H:%M:%S") for d in time])
utime = np.array([d.timestamp() for d in time2]) / (60 * 60 * 24 * 365)
return (utime + 1970)
def update_cds_monthly_data(
dataset_name="reanalysis-era5-single-levels-monthly-means",
product_type="monthly_averaged_reanalysis",
variables=[
"geopotential",
"2m_dewpoint_temperature",
"angle_of_sub_gridscale_orography",
"slope_of_sub_gridscale_orography",
"total_column_water_vapour",
"total_precipitation",
],
area=[40, 70, 30, 85],
pressure_level=None,
path=data_dir + "ERA5/",
qualifier=None):
"""
Imports the most recent version of the given monthly ERA5 dataset as a
netcdf from the CDS API.
Inputs:
dataset_name: str
prduct_type: str
variables: list of strings
pressure_level: str or None
area: list of scalars
path: str
qualifier: str
Returns: local filepath to netcdf.
"""
if type(area) == str:
area_extent = ls.basin_extent(area)
now = datetime.datetime.now()
if qualifier is None:
filename = (
dataset_name + "_" + product_type +
"_" + area + "_"+ now.strftime("%m-%Y") + ".nc"
)
else:
filename = (
dataset_name
+ "_"
+ product_type
+ "_"
+ qualifier
+ "_"
+ area
+ "_"
+ now.strftime("%m-%Y")
+ ".nc"
)
filepath = os.path.expanduser(path + filename)
# Only download if updated file is not present locally
if not os.path.exists(filepath):
current_year = now.strftime("%Y")
years = np.arange(1970, int(current_year) + 1, 1).astype(str)
months = ['01', '02', '03', '04', '05', '06',
'07', '08', '09', '10', '11', '12']
c = cdsapi.Client()
if pressure_level is None:
print(product_type, variables, pressure_level,
years.tolist(), months, area_extent)
c.retrieve(
'reanalysis-era5-single-levels-monthly-means',
{
"format": "netcdf",
"product_type": product_type,
"variable": variables,
"year": years.tolist(),
"time": "00:00",
"month": months,
"area": area_extent,
},
filepath,
)
else:
c.retrieve("reanalysis-era5-single-levels-monthly-means",
{
"format": "netcdf",
"product_type": product_type,
"variable": variables,
"pressure_level": pressure_level,
"year": years.tolist(),
"time": "00:00",
"month": months,
"area": area_extent,
},
filepath,)
return filepath
def update_cds_hourly_data(
dataset_name="reanalysis-era5-pressure-levels",
product_type="reanalysis",
variables=["geopotential"],
pressure_level="200",
area=[90, -180, -90, 180],
path=data_dir + "ERA5/",
qualifier=None):
"""
Imports the most recent version of the given hourly ERA5 dataset as a
netcdf from the CDS API.
Inputs:
dataset_name: str
prduct_type: str
variables: list of strings
area: list of scalars
pressure_level: str or None
path: str
qualifier: str
Returns: local filepath to netcdf.
"""
now = datetime.datetime.now()
if qualifier is None:
filename = (
dataset_name + "_" + product_type + "_" + pressure_level +
"_" + now.strftime("%m-%Y") + ".nc"
)
else:
filename = (
dataset_name
+ "_"
+ product_type
+ "_"
+ now.strftime("%m-%Y")
+ "_"
+ qualifier
+ ".nc"
)
filepath = path + filename
# Only download if updated file is not present locally
if not os.path.exists(filepath):
current_year = now.strftime("%Y")
years = np.arange(1970, 2020, 1).astype(str)
months = np.arange(1, 13, 1).astype(str)
days = np.arange(1, 32, 1).astype(str)
c = cdsapi.Client()
if pressure_level is None:
c.retrieve(
dataset_name,
{
"format": "netcdf",
"product_type": product_type,
"variable": variables,
"year": years.tolist(),
"time": "00:00",
"month": months.tolist(),
"day": days.tolist(),
"area": area,
},
filepath,
)
else:
c.retrieve(
dataset_name,
{
"format": "netcdf",
"product_type": product_type,
"variable": variables,
"pressure_level": pressure_level,
"year": years.tolist(),
"time": "00:00",
"month": months.tolist(),
"day": days.tolist(),
"area": area,
},
filepath,
)
return filepath