-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmeans_pics.py
241 lines (203 loc) · 6.76 KB
/
kmeans_pics.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#This program loads all of the data, splits it by pattern type and then prints pics for each
#Import a bunch of stuff
import numpy as np
import matplotlib as plt
from netCDF4 import Dataset
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import os
import datetime
import pickle
import csv
import scipy as sp
import scipy.ndimage as ndimage
import scipy.io as sio
from sklearn.metrics import mean_squared_error
from math import sqrt
#Check to see if out directory exists and if not, make it
outdir = "C:/Users/CoeFamily/Documents/MATLAB/SON_new/"
if not os.path.exists(outdir):
os.makedirs(outdir)
missing = 1 * 10**15
#Load in the Matlab File
mat_contents = sio.loadmat('C:/Users/CoeFamily/Documents/MATLAB/SON_new/SONdailymean_95/CI_results.mat')
#Put Variables into an array
K = mat_contents['K']
ci = 9
#Ask user which CI value to make pics for
#ci = int(raw_input("Enter CI value (Between 1-25)"))
#Place data into a new array (need to subtract 1 from the CI due to index differences)
dset1 = K[:,ci-1]
#open the lat lon files
latlon1 = Dataset("G:/Merra_new/19790101.nc4", mode='r')
#put the lat and lon data into arrays based on which grid type it is
lats = np.squeeze(latlon1.variables['YDim'][:])
lons = np.squeeze(latlon1.variables['XDim'][:])
# mesh the grids together for plotting purposes
LON,LAT = np.meshgrid(lons,lats)
#close the lat lon files
latlon1.close()
#preallocate all of our multi-dimensional lists
h500 = np.zeros((dset1.shape[0],lats.shape[0],lons.shape[0]))
SLP = np.zeros((dset1.shape[0],lats.shape[0],lons.shape[0]))
u = np.zeros((dset1.shape[0],lats.shape[0],lons.shape[0]))
v = np.zeros((dset1.shape[0],lats.shape[0],lons.shape[0]))
h500t = np.zeros((lats.shape[0],lons.shape[0]))
SLPt = np.zeros((lats.shape[0],lons.shape[0]))
ut = np.zeros((lats.shape[0],lons.shape[0]))
vt = np.zeros((lats.shape[0],lons.shape[0]))
#enter the start and end date for the loop to put all of the data into an array where ( row equals the date, columns equal the lats and pages equal the lons)
tnum = '1979'
enum = '2011'
m = 1
d = 1
year = 1979
p = 0
while tnum != enum:
if m < 10:
strm = '0' + str(m)
else:
strm = str(m)
if d < 10:
strd = '0' + str(d)
else:
strd = str(d)
stry = str(year)
str1 = stry + strm + strd
if(strm == '09' or strm == '10' or strm == '11'):
filename = 'G:/Merra_new/'+ str1 +'.nc4'
filename1 = 'G:/Merra_new/' + str1 + '.nc4'
tmp = Dataset(filename1, mode='r')
h500m = tmp.variables['H'][4,4,:,:]
SLPm = tmp.variables['SLP'][4,:,:]
um = tmp.variables['U'][4,2,:,:]
vm = tmp.variables['V'][4,2,:,:]
for mm in range(lats.shape[0]):
for nn in range(lons.shape[0]):
h500[p][mm][nn] = h500m[mm][nn]
SLP[p][mm][nn] = SLPm[mm][nn]
u[p][mm][nn] = um[mm][nn]
v[p][mm][nn] = vm[mm][nn]
if(u[p][mm][nn] == missing):
u[p][mm][nn] = 0
if(v[p][mm][nn] == missing):
v[p][mm][nn] = 0
if(SLP[p][mm][nn] == missing):
SLP[p][mm][nn] = 0
if(h500[p][mm][nn] == missing):
h500[p][mm][nn] = 0
p = p + 1
#modulo operator to check for a leap year and add the extra date if needed
modulo = year % 4
if(m == 1 and d < 31):
d = d + 1
elif( m == 1 and d == 31):
m = m + 1
d = 1
elif( m == 2 and d < 28):
d = d + 1
elif( m == 2 and d == 28 and modulo == 0):
d = d + 1
elif( m == 2 and d == 28 and modulo != 0):
m = m + 1
d = 1
elif( m == 2 and d > 28):
m = 3
d = 1
elif( m == 3 and d < 31):
d = d + 1
elif( m == 3 and d == 31):
m = m + 1
d = 1
elif( m == 4 and d < 30):
d = d + 1
elif( m == 4 and d == 30):
m = m + 1
d = 1
elif( m == 5 and d < 31):
d = d + 1
elif( m == 5 and d == 31):
m = m + 1
d = 1
elif( m == 6 and d < 30):
d = d + 1
elif( m == 6 and d == 30):
d = 1
m = m + 1
elif( m == 7 and d < 31):
d = d + 1
elif( m == 7 and d == 31):
d = 1
m = m + 1
elif( m == 8 and d < 31):
d = d + 1
elif( m == 8 and d == 31):
d = 1
m = m + 1
elif( m == 9 and d < 30):
d = d + 1
elif( m == 9 and d == 30):
d = 1
m = m + 1
elif( m == 10 and d < 31):
d = d + 1
elif( m == 10 and d == 31):
d = 1
m = m + 1
elif( m == 11 and d < 30):
d = d + 1
elif( m == 11 and d == 30):
d = 1
m = m + 1
elif( m == 12 and d < 31):
d = d + 1
else:
m = 1
d = 1
year = year + 1
tnum = str(year)
i = 1
#Plot the images for each of the clusters
while i <= ci:
m = Basemap(projection='merc',
resolution='h',area_thresh = 1000.0,lat_0=0,lon_0=100., llcrnrlon=-90,
llcrnrlat=30,urcrnrlon=-60,urcrnrlat=50)
m.drawcoastlines()
m.drawcountries()
m.drawstates()
#Average the data based on the CI Value
H = np.zeros((lats.shape[0], lons.shape[0]))
S = np.zeros((lats.shape[0], lons.shape[0]))
U = np.zeros((lats.shape[0], lons.shape[0]))
V = np.zeros((lats.shape[0], lons.shape[0]))
count = 0
for q in range(dset1.shape[0]):
if(dset1[q] == i):
for mm in range (lats.shape[0]):
for nn in range( lons.shape[0]):
H[mm][nn] = H[mm][nn]+ h500[q][mm][nn]
S[mm][nn] = S[mm][nn] + SLP[q][mm][nn]
U[mm][nn] = U[mm][nn] + u[q][mm][nn]
V[mm][nn] = V[mm][nn] + v[q][mm][nn]
count = count + 1
for mm in range(lats.shape[0]):
for nn in range(lons.shape[0]):
H[mm][nn] = H[mm][nn] / (count*10)
S[mm][nn] = S[mm][nn] / (count*100)
U[mm][nn] = U[mm][nn] / count
V[mm][nn] = V[mm][nn] / count
x,y = m(LON,LAT)
Z_500 = ndimage.gaussian_filter(S[:][:], sigma=5, order=0)
cd = m.contour(x,y,H[:][:],colors='blue',)
plt.clabel(cd, inline=True, fmt='%1.0f', fontsize=10, colors='black')
m.quiver(x[:,:],y[:,:],U[:][:],V[:][:],scale=None, scale_units='inches')
cs = m.contour(x,y,Z_500, colors='black',)
plt.clabel(cs, inline=True, fmt='%1.0f', fontsize=12, colors='k')
m.drawparallels(np.arange(-80.,81.,10.), labels=[1,0,0,0],fontsize=10)
m.drawmeridians(np.arange(-180.,181.,10.), labels=[0,0,0,1], fontsize = 10)
title = 'Cluster ' + str(i)
title1 = outdir + '/_' + str(i)
plt.title(title)
plt.savefig(title1, bbox_inches='tight')
plt.gcf().clear()
i = i + 1