-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnchap_class.py
More file actions
204 lines (165 loc) · 8.09 KB
/
nchap_class.py
File metadata and controls
204 lines (165 loc) · 8.09 KB
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
from netCDF4 import Dataset
import numpy as np
import nchap_fun as nc
class For_Plots:
"""
for pulling data for plot_height.py ie plots representing all runs on one ax instance
"""
def __init__(self, Run_Date):
self.Run_Date = Run_Date
self.path = "/newtera/tera/phil/nchaparr/python/Plotting/" + Run_Date + "/data/"
def get_file(self, dump_time, filename):
the_file = self.path + filename + dump_time
return the_file
def save_file(self, array, filename):
np.savetxt(self.path + filename, array, delimiter=' ')
def dhdtplot(self):
dhdtplot = np.genfromtxt(self.path + "dhdtinvriplt.txt")
return dhdtplot
def HistVars(self):
HistVars = np.genfromtxt(self.path + "ml_height_hist_vars")
return HistVars
def AvProfVars(self):
AvProfVars = np.genfromtxt(self.path + "AvProfLims")
return AvProfVars
def rinovals(self):
rinovals = np.genfromtxt(self.path + "invrinos")
return rinovals
def Deltah(self):
AvProfVars = np.genfromtxt(self.path + "AvProfLims")
Deltah = np.subtract(AvProfVars[:,2], AvProfVars[:,0])
#Deltah0 = np.divide(Deltah0, AvProfVars[:,1])
return Deltah
def Deltah_over_h(self):
AvProfVars = np.genfromtxt(self.path + "AvProfLims")
Deltah = np.subtract(AvProfVars[:,2], AvProfVars[:,0])
Deltah_over_h = np.divide(Deltah, AvProfVars[:,1])
return Deltah_over_h
def get_dhdt(self, Times, start_index, end_index):
"""
polyfits the h vs time plot to get we for the scaled
we vs invri plot.
Arguments:
times -- list of times in hours
heights--array of boundary layer heights
Returns:
dhdtinvriplt -- 2d array for the scaledweinvri plot
"""
AvProfVars = np.genfromtxt(self.path + "AvProfLims")
rinovals = np.genfromtxt(self.path + "invrinos")
FitFunc=np.polyfit(Times[start_index:end_index], AvProfVars5[start_index:end_index, 1], 2, full=False)
Fit = FitFunc[0]*Times[start_index:end_index]**2 + FitFunc[1]*Times[start_index:end_index] + FitFunc[2]
dhdt =1.0*(2*FitFunc[0]*Times[start_index:end_index] + FitFunc[1])/3600
scaled_dhdt = np.divide(dhdt, rinovals[start_index:end_index, 2])
dhdtinvriplt = np.vstack((rinovals[start_index:end_index, 1], scaled_dhdt))
save_file(dhdtinvriplt, "dhdtinvriplt")
return dhdtinvriplt
class Get_Var_Arrays1:
"""
for pulling velociy perturpations, temperature and pressure
from nc files from the ensemble
"""
def __init__(self, path1, path2, dump_time):
self.path1 = path1
self.path2 = path2
self.dump_time = dump_time
self.nc_file_list = [path1 + str(i+1) + path2 + dump_time + ".nc" for i in range(10)]
def get_wvelperts(self):
wvelperts_list = []
for i in range(len(self.nc_file_list)): #loop over list of nc files, not efficient to do this for each variable but can't think of better way right now
thefile = self.nc_file_list[i]
#print thefile
ncdata = Dataset(thefile,'r')
uvelperts_list.append(np.squeeze(ncdata.variables['W'][...]))
ncdata.close()
return wvelperts_list
def get_uvelperts(self):
uvelperts_list = []
for i in range(len(self.nc_file_list)): #loop over list of nc files, not efficient to do this for each variable but can't think of better way right now
thefile = self.nc_file_list[i]
#print thefile
ncdata = Dataset(thefile,'r')
uvelperts_list.append(np.squeeze(ncdata.variables['U'][...]))
ncdata.close()
return uvelperts_list
def get_vvelperts(self):
vvelperts_list = []
for i in range(len(self.nc_file_list)): #loop over list of nc files, not efficient to do this for each variable but can't think of better way right now
thefile = self.nc_file_list[i]
#print thefile
ncdata = Dataset(thefile,'r')
vvelperts_list.append(np.squeeze(ncdata.variables['V'][...]))
ncdata.close()
return vvelperts_list
def get_thetas(self):
thetas_list = []
press_list = []
for i in range(len(self.nc_file_list)): #loop over list of nc files, not efficient to do this for each variable but can't think of better way right now
thefile = self.nc_file_list[i]
#print thefile
ncdata = Dataset(thefile,'r')
temp = np.squeeze(ncdata.variables['TABS'][...])
press = np.squeeze(ncdata.variables['p'][...])
[znum, ynum, xnum] = temp.shape
theta = np.zeros_like(temp) #TODO: make this into a function
thetafact = np.array([(1.0*1000/k)**(1.0*287/1004) for k in press])
for j in range(znum):
theta[j, :, :] = temp[j, :, :]*thetafact[j]
thetas_list.append(theta)
press_list.append(press)
ncdata.close()
return thetas_list, press_list
def get_height(self):
thefile = self.nc_file_list[0]
#print thefile
ncdata = Dataset(thefile,'r')
height = np.squeeze(ncdata.variables['z'][...])
ncdata.close()
return height
def get_wvelthetaperts(self):
wvels_list = self.get_wvelperts()
thetas_list, press_list = self.get_thetas()
##print 'checking thetas_list', len(thetas_list), thetas_list[0].shape
ens_avthetas = nc.Ensemble1_Average(thetas_list)
wvelthetaperts_list = []
for i in range(len(wvels_list)):
[znum, ynum, xnum] = wvels_list[i].shape
thetapert_rough = np.subtract(thetas_list[i], ens_avthetas)
thetapert = np.zeros_like(thetapert_rough)
for j in range(znum):#something like this is done in statistics.f90, staggered grid!
if j == 0:
thetapert[j,:,:] = thetapert_rough[j,:,:]
else:
thetapert[j,:,:] = 0.5*np.add(thetapert_rough[j,:,:], thetapert_rough[j-1,:,:])
wvelpert = wvels_list[i]
wvelthetapert = np.multiply(wvelpert, thetapert)
wvelthetaperts_list.append(wvelthetapert)
return wvelthetaperts_list
def get_thetaperts(self):
thetas_list, press_list = self.get_thetas()
ens_avthetas = nc.Ensemble1_Average(thetas_list)
thetaperts_list=[]
for i in range(len(thetas_list)):
[znum, ynum, xnum] = thetas_list[i].shape
thetapert_rough = np.subtract(thetas_list[i], ens_avthetas)
thetapert = np.zeros_like(thetapert_rough)
for j in range(znum):#something like this is done in statistics.f90, staggered grid!
if j == 0:
thetapert[j,:,:] = thetapert_rough[j,:,:]
else:
thetapert[j,:,:] = 0.5*np.add(thetapert_rough[j,:,:], thetapert_rough[j-1,:,:])
thetaperts_list.append(thetapert)
return thetaperts_list
def get_sqvel(self, vel_dir):
if vel_dir == 'w':
vel_list = self.get_wvelperts()
elif vel_dir == 'v':
vel_list = self.get_vvelperts()
elif vel_dir == 'u':
vel_list = self.get_uvelperts()
velpertsq_list = []
for i in range(len(vel_list)):
velpert = vel_list[i]
velpertsq = np.multiply(velpert, velpert)
velpertsq_list.append(velpertsq)
return velpertsq_list