-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet_ML_Heights.py
More file actions
197 lines (141 loc) · 7 KB
/
Get_ML_Heights.py
File metadata and controls
197 lines (141 loc) · 7 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
from netCDF4 import Dataset
import glob,os.path
import numpy as np
from scipy.interpolate import UnivariateSpline
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib import ticker
import site
site.addsitedir('/tera2/nchaparr/Dec252013/python')
import sys
#sys.path.insert(0, '/tera/phil/nchaparr/python')
import nchap_fun as nc
from Make_Timelist import *
import fastfit as fsft
"""
Gets 2d horizontal domain of ML heights
and plots pcolor plots thereof.
"""
def get_ticks(mean, stddev, max, min):
"""
gets ticks and tick lavels for contour plot based on mean and standard deviation
Arguments:
mean, stddev, max, min
Returns:
ticks, tick_labels
"""
tick_list = []
label_list = []
int1=int(np.ceil((mean-min)/stddev))
int2=int(np.ceil((max-mean)/stddev))
for i in range(int1):
if int1==1:
tick_list.append(min)
label_list.append(r'$\mu - %.1f \sigma$' %((mean-min)/stddev))
elif i > 0:
tick_list.append(mean - (int1-i)*stddev)
label_list.append(r'$\mu - %.1f \sigma$' %(int1-i))
#else:
#tick_list.append(min)
#label_list.append(r'$\mu - %.1f \sigma$' %((mean-min)/stddev))
tick_list.append(mean)
label_list.append(r'$\mu$')
for i in range(int2):
if int2==1:
tick_list.append(max)
label_list.append(r'$\mu + %.1f \sigma$' %((max-mean)/stddev))
elif i< int2-1:
tick_list.append(mean + (i+1)*stddev)
label_list.append(r'$\mu + %.1f \sigma$' %(i+1))
#else:
#tick_list.append(max)
#label_list.append(r'$\mu + %.1f \sigma$' %((max-mean)/stddev))
return label_list, tick_list
def Main_Fun(dump_time_index, case, date):
"""Loops over ensemble cases. Pulls temperature, pressure, height from nc output files using nchap_class
gets height of mixed layer at each horizontal point using fast_fit and saves as a txt file.
Arguments:
dump_time -- time of output eg '0000000720'
case -- integer
Returns:
ML_Heights -- 128*192 array as txt file
"""
dump_time_list, time_hrs = Make_Timelists(1, 3600, 28800)
ncfile = "/tera2/nchaparr/"+ date +"/runs/sam_case" + str(case) + "/OUT_3D/keep/NCHAPP1_testing_doscamiopdata_24_" + dump_time_list[dump_time_index] + ".nc"
thefile = ncfile
print thefile
ncdata = Dataset(thefile,'r')
press = np.squeeze(ncdata.variables['p'][...])#pressure already horizontally averaged
height = np.squeeze(ncdata.variables['z'][...])
temp = np.squeeze(ncdata.variables['TABS'][...])
#tracer = np.squeeze(ncdata.variables['TRACER'][...])
ncdata.close()
#calculate thetas
theta = np.zeros_like(temp)
thetafact = np.array([(1.0*1000/k)**(1.0*287/1004) for k in press])
for j in range(312):
theta[j, :, :] = temp[j, :, :]*thetafact[j]
ML_Heights = np.empty([128, 192])
for i in range(128): #change to 128
for j in range(192):#change to 192
top = np.where(np.abs(height-2300)<100)[0][0]
RSS, J, K = fsft.get_fit(theta[:, i, j], height, top)
print i, j, J
ML_Heights[i, j] = height[J]
#if height[J] < 500:
#print i, j, J, height[J]#TODO:this could definitely be a function
b_1 = (np.sum(np.multiply(height[:J], theta[:J, i, j])) - 1.0/J*np.sum(height[:J])*np.sum(theta[:J, i, j]))/(np.sum(height[:J]**2) - 1.0/J*np.sum(height[:J])**2)
a_1 = np.sum(np.multiply(height[:J], theta[:J, i, j]))/np.sum(height[:J]) - b_1*np.sum(height[:J]**2)/np.sum(height[:J])
b_2 = (np.sum(theta[J:K, i, j]) - (K-J)*(a_1+b_1*height[J]))/(np.sum(height[J:K]) - (K-J)*height[J])
a_2 = np.sum(np.multiply(height[J:K], theta[J:K, i, j]))/np.sum(height[J:K]) - b_2*np.sum(height[J:K]**2)/np.sum(height[J:K])
b_3 = (np.sum(theta[K:290, i, j]) - (290-K)*(a_2+b_2*height[K]))/(np.sum(height[K:290]) - (290-K)*height[K])
if b_3-b_2>.002:
#print i, j, J, height[J], height[K], b_3, b_2, b_3-b_2
ML_Heights[i, j] = height[K]
print '/tera/phil/nchaparr/python/Plotting/'+date+'/data/mixed_layer_height_'+ str(case) + '_' + dump_time_list[dump_time_index]
np.savetxt('/tera/phil/nchaparr/python/Plotting/'+date+'/data/mixed_layer_height_'+ str(case) + '_' + dump_time_list[dump_time_index], ML_Heights, delimiter=' ')
return ML_Heights
def Call_Main_Fun(date):
dump_time_no = 8
case_no = 1
date = 'Dec252013'
for i in range(dump_time_no):
if i == 1:
for j in range(case_no):
ML_Heights = Main_Fun(i, j+1, date)
theFig = plt.figure(i)
theFig.clf()
theAx = theFig.add_subplot(111)
theAx.set_title(r"$Contour \ of \ Local \ h \ after \ " + str(time_hrs[i]) +" \ hours$")
theAx.set_xlabel(r"$x \ (m)$")
theAx.set_ylabel(r"$y \ (m)$")
v_max, v_min, mean, stddev = np.amax(ML_Heights), np.amin(ML_Heights), np.mean(ML_Heights), np.std(ML_Heights)
filler_array = np.zeros([64, 192])
ML_Heights = np.vstack((ML_Heights, filler_array))
x = np.arange(0, 4800, 25)
y = np.arange(0, 4800, 25)
X,Y = np.meshgrid(x, y)
im = theAx.pcolor(X, Y, np.transpose(ML_Heights), cmap=cm.hot, vmax=v_max, vmin=v_min)
bar = plt.colorbar(im)
plt.xlim(0, 3200)
plt.ylim(0, 4800)
#label_list, tick_list = get_ticks(mean, stddev,v_max, v_min)
#bar.locator = ticker.FixedLocator(tick_list)
#bar.formatter= ticker.FixedFormatter(label_list)
#bar.update_ticks()
plt.show()
#print time_hrs[i]
theFig.savefig('/tera/phil/nchaparr/python/Plotting/'+date+'/pngs/h_cont_'+str(time_hrs[i])+'_hrs.png')
dump_time_list, time_hrs = Make_Timelists(1, 3600, 28800)
print time_hrs
#date = "Mar12014"
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--run_name", type=str, help='name of run')
args = parser.parse_args()
date = args.run_name
print date
Call_Main_Fun(date)
#set up plot