-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_era5.py
137 lines (96 loc) · 3.51 KB
/
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
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
#! /usr/bin/env python
#==============================================================================
# data_era5.py
"""
Python module.
This module creates a dataera5 class to retrieve data from ERA5 grib
files.
"""
#==============================================================================
import numpy
n = numpy
import cdms2
import get_ab_era5
class dataera5:
"""
TBD.
"""
def __init__(self, state_xmlfile_dir, state_xmlfile_name):
#---------------------------------------------------------
"""
Initializes dataera5 class.
state_files_dir # TBD.
state_file_name #
"""
self.state_xmlfile_dir = state_xmlfile_dir
self.state_xmlfile_name = state_xmlfile_name
# These are to conform with ERA5 names.
#---------------------------------------
self.var_dict = {'PHIS': 'PHIS', 'PS': 'PS', 'Q': 'Q',
'V': 'V', 'U': 'U', 'T': 'T'}
fin = \
cdms2.open(self.state_xmlfile_dir + '/' + self.state_xmlfile_name)
zsfc = fin.variables['PHIS']
# Get coordinates.
#-----------------
lats = zsfc.getLatitude()[:]
lons = zsfc.getLongitude()[:]
self.lons = lons
self.lats = lats
# Create a CDMS grid object, so can use later for regridding.
#------------------------------------------------------------
self.grid = cdms2.createGenericGrid(self.lats, self.lons)
# Get hybrid coordinate information.
#-----------------------------------
p0, hyam, hybm, hyai, hybi = get_ab_era5.get_ab_era5()
self.p0 = p0
self.hyam = hyam
self.hybm = hybm
self.hyai = hyai
self.hybi = hybi
self.fin = fin
def get_const_data(self, var):
#-----------------------------
"""
TBD.
Only constant is surface geopotential.
var # TBD.
"""
const_data = None
if var != 'PHIS':
mystr = '\nONLY DOES PHIS - EXITING!\n\n'
raise SystemExit(mystr)
phis = self.fin.variables[self.var_dict[var]][0]
const_data = n.squeeze(phis)
return const_data
def get_state_data(self, var, time, regrid_func=None):
#-----------------------------------------------------
"""
Returns ERA5 state data.
State_data assumed to be (time, level, lat, lon).
var # State variable to get.
time # cdtime component time.
regrid_func # CDMS regrid function.
"""
state_data = None
if var[:8] != 'pressure':
state_data = self.fin(self.var_dict[var], time=time, squeeze=1)
if var == 'PS':
state_data = n.exp(state_data).filled()
if regrid_func:
state_data = regrid_func(state_data.filled())
else:
ps = self.fin('PS', time=time, squeeze=1)
ps = n.exp(ps)
if regrid_func:
ps = regrid_func(ps.filled())
if var == 'pressure_interface':
edges = (self.hyai[:,n.newaxis] * self.p0) + \
n.outer(self.hybi, ps)
edges.shape = (self.hyai.shape[0], ps.shape[0], ps.shape[1])
else:
edges = (self.hyam[:,n.newaxis] * self.p0) + \
n.outer(self.hybm, ps)
edges.shape = (self.hyam.shape[0], ps.shape[0], ps.shape[1])
state_data = edges
return state_data