-
Notifications
You must be signed in to change notification settings - Fork 1
/
cfsites_forcing_reader.py
190 lines (125 loc) · 4.93 KB
/
cfsites_forcing_reader.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
import netCDF4 as nc
#import pylab as plt
import numpy as np
import os
from scipy.signal import savgol_filter
from scipy.interpolate import pchip
from scipy.integrate import cumtrapz
def compute_derivative(xp, yp):
dyp_dxp = np.diff(yp)/np.diff(xp)
return dyp_dxp, 0.5 * (xp[1:] + xp[:-1])
def mdi_interp(x, y, xn):
xp = x.filled()
yp = y.filled()
p_interp_val = pchip(xp, yp)
dyp_dxp, dir_loc = compute_derivative(xp, yp)
p_interp1= pchip(dir_loc, dyp_dxp)
cumtrapz(p_interp1(xn), xn, initial= 0.0)
#return cumtrapz(np.interp(xn, dir_loc, dyp_dxp)[::--1, xn, initial= 0.0), xn
#print np.array(cumtrapz(p_interp1(xn)[::-1], xn[::-1], initial= 0.0)[::-1] + p_interp_val(np.max(xn)))
return np.array(cumtrapz(p_interp1(xn)[::-1], xn[::-1], initial= 0.0)[::-1] + p_interp_val(np.max(xn))), xn
class cfreader:
def __init__(self, path, site):
self.file = path
self.op_grp = 'site' + str(site)
return
def get_profile_mean(self, var, zero_bottom=False):
rt_grp = nc.Dataset(self.file, 'r')
op_grp = rt_grp[self.op_grp]
var_handle = op_grp.variables[var]
assert (('time','lev') == var_handle.dimensions or ('lev',) == var_handle.dimensions)
data = np.mean(var_handle[:, :], axis=0)
rt_grp.close()
#print var, data
if zero_bottom:
return np.append(data, 0.0)[:-1]
else:
return np.append(data, data[-1])[:-1]
def get_timeseries_mean(self, var):
rt_grp = nc.Dataset(self.file, 'r')
op_grp = rt_grp[self.op_grp]
var_handle = op_grp.variables[var]
assert(('time',) == var_handle.dimensions)
data = var_handle[:]
rt_grp.close()
return np.mean(data, axis=0)
def get_interp_profile(self, var, z, zero_bottom=False, filter=True):
'''
:param var: name of variable in fms data
:param z: interpolation points
:param zero_bottom: bool to specify if bottom boundary is set to zero if not then take value of one point above
:return: array of var interpolated onto z
'''
data = self.get_profile_mean(var, zero_bottom)
z_gcm = self.get_profile_mean('height', zero_bottom=True)
yn, dir_loc = mdi_interp(z_gcm, data, z)
return yn
def get_interp_profile_old(self, var, z, zero_bottom=False, filter=False):
'''
:param var: name of variable in fms data
:param z: interpolation points
:param zero_bottom: bool to specify if bottom boundary is set to zero if not then take value of one point above
:return: array of var interpolated onto z
'''
data = self.get_profile_mean(var, zero_bottom)
z_gcm = self.get_profile_mean('height', zero_bottom=True)
#print data
#print z_gcm
p_interp1= pchip(z_gcm[:].filled(), data[:].filled())#np.interp(z, z_gcm[:], data[:])
z_interp1 = np.linspace(0.0, np.max(z_gcm), 2560)
data_interp1 = p_interp1(z_interp1)
#plt.figure()
#plt.plot(data_interp1, z_interp1)
if not filter:
return p_interp1(z)
if filter:
data_interp1 = savgol_filter(data_interp1, 37, 3)
p_interp2 = pchip(z_interp1.filled(), data_interp1.filled())
return p_interp2(z)
#plt.plot(data_interp1, z_interp1)#
#
#plt.show()
#
#import sys; sys.exit()
#f not filter:
# return pinter
#else:
# return savgol_filter(data_interp, 37, 3)
def get_value(self, var):
rt_grp = nc.Dataset(self.file, 'r')
op_grp = rt_grp[self.op_grp]
var_handle = op_grp.variables[var]
assert(('pt',) == var_handle.dimensions)
return var_handle[0]
def main():
return
if __name__ == "__main__":
path = './cfsites_forcing.nc'
site = 20
rdr = cfreader(path, site)
#t = rdr.get_profile_mean('temp')
#sphum = rdr.get_profile_mean('sphum')
#ucomp = rdr.get_profile_mean('ucomp')
#vcomp = rdr.get_profile_mean('vcomp')
#alpha = rdr.get_profile_mean('alpha')
#Test interpolation
interp_test_dir = './InterpTests/'
if not os.path.exists(interp_test_dir):
os.makedirs(interp_test_dir )
vars = ['temp', 'sphum',
'ucomp', 'vcomp']
height_gcm = rdr.get_profile_mean('height')
height_les = np.linspace(0.0, 25600.0, 256)
for v in vars:
var_gcm = rdr.get_profile_mean(v)
var_les_filt = rdr.get_interp_profile(v, height_les)
var_les = rdr.get_interp_profile(v, height_les, filter=False)
plt.figure()
plt.plot(var_gcm, height_gcm, 'o')
plt.plot(var_les, height_les)
plt.plot(var_les_filt, height_les, '.')
plt.savefig(os.path.join(interp_test_dir, v + '_linear.pdf'))
plt.close()
ts_mean = rdr.get_timeseries_mean('t_surf')
print(ts_mean)
main()