-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathget_nudging_data.ERA5.py
executable file
·92 lines (88 loc) · 5.13 KB
/
get_nudging_data.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
#!/usr/bin/env python
#---------------------------------------------------------------------------------------------------
# links to CDS web interface:
# https://cds.climate.copernicus.eu/cdsapp#!/dataset/reanalysis-era5-pressure-levels
# https://cds.climate.copernicus.eu/cdsapp#!/dataset/reanalysis-era5-single-levels
# A list of available variables can also be found in the ERA5 documentation:
# https://confluence.ecmwf.int/display/CKB/ERA5%3A+data+documentation
#---------------------------------------------------------------------------------------------------
import os, cdsapi, datetime, pandas as pd
server = cdsapi.Client()
#---------------------------------------------------------------------------------------------------
class clr:END,RED,GREEN,MAGENTA,CYAN = '\033[0m','\033[31m','\033[32m','\033[35m','\033[36m'
#---------------------------------------------------------------------------------------------------
usage = f'''
This script is intended to streamline the aquisition of ERA5 pressure level
and surface data for processing data for E3SM atmospheric nudging with HICCUP
Minimum example of requesting single data file for single initialization date/time:
{clr.GREEN}python get_nudging_data.ERA5.py --start-date=<yyyymmdd>{clr.END}
Example of requesting multiple data files spanning a range of dates/times every 3-hours:
{clr.GREEN}python get_nudging_data.ERA5.py --start-date=<yyyymmdd> --final-date=<yyyymmdd> --start-hour=<hh> --final-hour=<hh> --data-freq=3h --output-root=<path>{clr.END}
'''
from optparse import OptionParser
parser = OptionParser(usage=usage)
parser.add_option('--start-date', dest='start_date', default=None, help='date of first file [yyyymmdd]')
parser.add_option('--final-date', dest='final_date', default=None, help='date of last file [yyyymmdd] (optional)')
parser.add_option('--start-hour', dest='start_hour', default='00', help='UTC hour of first file (default=00Z)')
parser.add_option('--final-hour', dest='final_hour', default='00', help='UTC hour of last file (default=00Z)')
parser.add_option('--data-freq', dest='data_freq', default='24h', help='frequency of data files (default=24h)')
parser.add_option('--output-root', dest='output_root', default='./', help='Output path for data files (default is PWD)')
(opts, args) = parser.parse_args()
#---------------------------------------------------------------------------------------------------
# check that input arguments are valid
if opts.start_date is None: raise ValueError(f'{clr.RED}initialization date was not specified{clr.END}')
if opts.final_date is None: opts.final_date = opts.start_date
#---------------------------------------------------------------------------------------------------
# build list of dates and times from input arguments
beg_date = datetime.datetime.strptime(f'{opts.start_date} {opts.start_hour}', '%Y%m%d %H')
end_date = datetime.datetime.strptime(f'{opts.final_date} {opts.final_hour}', '%Y%m%d %H')
datetime_list = pd.date_range(beg_date, end_date, freq=opts.data_freq)
#---------------------------------------------------------------------------------------------------
# Request all available pressure levels
lev = [ '1', '2', '3', '5', '7', '10', '20', '30', '50', '70','100','125',
'150','175','200','225','250','300','350','400','450','500','550','600',
'650','700','750','775','800','825','850','875','900','925','950','975','1000']
#---------------------------------------------------------------------------------------------------
# loop over dates
get_atm = True
get_sfc = True
for t in datetime_list:
#-----------------------------------------------------------------------------
# parse date/time information
yr = t.strftime("%Y")
mn = t.strftime("%m")
dy = t.strftime("%d")
hr = t.strftime("%H")
hr_min = f'{hr}:00'
#-----------------------------------------------------------------------------
# Specify output file names
output_file_plv = f'{opts.output_root}/ERA5.atm.{yr}-{mn}-{dy}.{hr}.nc'
output_file_mlv = output_file_plv.replace('.atm.','.mlev.')
output_file_sfc = output_file_plv.replace('.atm.','.sfc.')
output_file_lnd = output_file_plv.replace('.atm.','.lnd.')
#-----------------------------------------------------------------------------
# atmossphere pressure level data
if get_atm:
server.retrieve('reanalysis-era5-pressure-levels',{
'product_type' : 'reanalysis',
'format' : 'netcdf',
'pressure_level': lev,
'time' : hr_min,
'day' : dy,
'month' : mn,
'year' : yr,
'variable' : ['u_component_of_wind', 'v_component_of_wind']
}, output_file_plv)
#-----------------------------------------------------------------------------
# surface data
if get_sfc:
server.retrieve('reanalysis-era5-single-levels',{
'product_type' : 'reanalysis',
'format' : 'netcdf',
'time' : hr_min,
'day' : dy,
'month' : mn,
'year' : yr,
'variable' : ['surface_pressure']
}, output_file_sfc)
#-----------------------------------------------------------------------------