-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_full_data_old.py
268 lines (222 loc) · 9.96 KB
/
plot_full_data_old.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 11 12:04:17 2016
@author: siirias
"""
import math
import datetime
import matplotlib as mp
import matplotlib.pyplot as plt
import numpy as np
from scipy.io import netcdf
from mpl_toolkits.basemap import Basemap
def plot_full_data( value="temp",col_map='cool', time_highlight=None, \
set_no=None, high_no=None, vmin=None, vmax=None, \
new_fig=None, save_file=None, plot_contour=False, \
ref_point=None, ref_dist=None,tmin=None,tmax=None, \
use_converted=False, show_colorbar=True):
def distance(origin, destination):
lat1, lon1 = origin
lat2, lon2 = destination
radius = 6371 # km
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
return d
NONUM_DEF=99999.0
# files=["noora_6902014_20160615160609287test.nc", \
# "noora_6902019_20160614135812934test.nc", \
# "noora_6902020_20170828072009212test.nc"]
files=["6902014_20161123144244280.nc", \
"IM_6902019_20140821_20150805.nc", \
"6902020_20161123123226453.nc"]
#6902014_20161123144244280.nc
#6902019_20161123144137259.nc
#6902020_20161123123226453.nc
#IM_6902014_20130814_20140821.nc
#IM_6902019_20140821_20150805.nc
#IM_6902020_20150805_20160331_active.nc
if(new_fig!=None):
try:
fig=plt.figure(figsize=new_fig)
plt.clf()
except:
fig=plt.figure()
if(time_highlight!=None):
time_highlight=mp.dates.date2num(datetime.datetime.strptime(time_highlight,"%Y%m%d"))
kuva=value
if(value=='temp'):
kuva='temp_b'
if(value=='salt'):
kuva='salt_b'
if(value=='temp_coarse'):
kuva='temp_a'
if(value=='salt_coarse'):
kuva='salt_a'
if(value=='oxygen'):
kuva='oxyg_b'
# if(value=='scatter'): #This one requires bit more tinkering below
# kuva='scat_a'
current_set=-1
time_min=None
time_max=None
for file_n in files:
current_set+=1
fmk=netcdf.netcdf_file(file_n,'r')
if(use_converted==False):
# temp=fmk.variables['TEMP'][:].copy()
# salt=fmk.variables['PSAL'][:].copy()
# press=fmk.variables['PRES'][:].copy()
# oxyg=fmk.variables['DOXY'][:].copy()
# scat=fmk.variables['SCATTERING'][:].copy()
temp=fmk.variables['TEMP_ADJUSTED'][:].copy()
salt=fmk.variables['PSAL_ADJUSTED'][:].copy()
press=fmk.variables['PRES_ADJUSTED'][:].copy()
oxyg=fmk.variables['DOXY_ADJUSTED'][:].copy()
scat=fmk.variables['SCATTERING_ADJUSTED'][:].copy()
reftime = datetime.datetime.strptime(fmk.variables['REFERENCE_DATE_TIME'][:].tostring(), '%Y%m%d%H%M%S') #"YYYYMMDDHHMISS"
jultime = fmk.variables['JULD'][:].tolist()
apetime = np.array([mp.dates.date2num(reftime + datetime.timedelta(days=x)) for x in jultime])
else:
temp=fmk.variables['var26'][:].copy()
salt=fmk.variables['var19'][:].copy()
press=fmk.variables['var23'][:].copy()
oxyg=fmk.variables['metavar1'][:].copy()
scat=fmk.variables['var32'][:].copy()
reftime = datetime.datetime.strptime(fmk.variables['REFERENCE_DATE_TIME'][:].tostring(), '%Y%m%d%H%M%S') #"YYYYMMDDHHMISS"
jultime = fmk.variables['JULD'][:].tolist()
apetime = np.array([mp.dates.date2num(reftime + datetime.timedelta(days=x)) for x in jultime])
if(value=='oxygen' and file_n==files[1]):
kuva='oxyg_a' #Himmeä purkka, kosk afilet on erilaisia...
if(value=='oxygen' and file_n!=files[1]):
kuva='oxyg_b' #Himmeä purkka, kosk afilet on erilaisia...
mask=press>9000
if(ref_point is not None and ref_dist is not None):
#We'll limit shown data by distance of the given point
lats=fmk.variables['LATITUDE'][:].copy()
lons=fmk.variables['LONGITUDE'][:].copy()
for i in range(np.size(lats)):
if(distance(ref_point,(lats[i],lons[i]))>ref_dist):
mask[i]=True
press_m=press[:].copy()
press_m[mask]=np.nan
temp_m=temp[:].copy()
temp_m[mask]=np.nan
salt_m=salt[:].copy()
salt_m[mask]=np.nan
oxyg_m=oxyg[:].copy()
oxyg_m[mask]=np.nan
scat_m=scat[:].copy()
scat_m[mask]=np.nan
apetime_a=apetime[::2].copy()
apetime_b=apetime[1::2].copy()
press_a=press_m[::2][:].copy()
press_b=press_m[1::2][:].copy()
salt_a=salt_m[::2][:].copy()
salt_b=salt_m[1::2][:].copy()
temp_a=temp_m[::2][:].copy()
temp_b=temp_m[1::2][:].copy()
oxyg_a=oxyg_m[::2][:].copy()
oxyg_b=oxyg_m[1::2][:].copy()
scat_a=scat_m[::2][:].copy()
scat_b=scat_m[1::2][:].copy()
sa_mean=scat_a[~np.isnan(scat_a)].mean()
sb_mean=scat_b[~np.isnan(scat_b)].mean()
print "scat_a variance:", sa_mean
print "scat_b variance:", sb_mean ,"\n"
scat_gludge=scat_a;
press_gludge=press_a;
apetime_gludge=apetime_a
if(sa_mean>NONUM_DEF*0.9):
scat_gludge=scat_b
press_gludge=press_b;
apetime_gludge=apetime_b
if kuva=='salt_a':
z_source=salt_a;y_source=press_a;tt=apetime_a;title_txt='PSU'
if kuva=='salt_b':
z_source=salt_b;y_source=press_b;tt=apetime_b;title_txt='PSU'
if kuva=='temp_a':
z_source=temp_a;y_source=press_a;tt=apetime_a;title_txt='Temperature [$^\circ$C]'
if kuva=='temp_b':
z_source=temp_b;y_source=press_b;tt=apetime_b;title_txt='Temperature [$^\circ$C]'
if kuva=='oxyg_a':
z_source=oxyg_a;y_source=press_a;tt=apetime_a;title_txt='Oxygen [$\mu mol/kg$]'
if kuva=='oxyg_b':
z_source=oxyg_b;y_source=press_b;tt=apetime_b;title_txt='Oxygen [$\mu mol/kg$]'
if kuva=='scat_a':
z_source=scat_a;y_source=press_a;tt=apetime_a;title_txt='Scattering [$M^{-1} sr^{-1}$]'
if kuva=='scat_b':
z_source=scat_b;y_source=press_b;tt=apetime_b;title_txt='Scattering [$M^{-1} sr^{-1}$]'
if kuva=='scatter':
z_source=scat_gludge;y_source=press_gludge;tt=apetime_gludge;title_txt='Scattering [$M^{-1} sr^{-1}$]'
x_source=np.tile(tt,(z_source.shape[1],1))
x,y=np.mgrid[0:z_source.shape[0],0:z_source.shape[1]]
x=x.T
y=-1*y.T
dat=np.ma.masked_invalid(z_source.T) # z_source.T
pre=np.ma.masked_invalid(y_source.T)
time=x_source
if(set_no is not None and high_no is not None):
if(set_no==current_set):
time_highlight=time[0][high_no]
#plt.pcolor(x,y,np.ma.masked_invalid(np.rot90(temp_m)))
# plt.figure()
# plt.pcolor(x,y,dat)
# plt.gca().set_axis_bgcolor('gray')
# plt.colorbar()
# plt.figure()
#ACTUAL PLOT
#plt.pcolor(time,pre,dat,cmap=col_map)
try:
if(plot_contour):
plt.pcolor( np.ma.masked_invalid(time), \
np.ma.masked_invalid(pre), \
np.ma.masked_invalid(dat), \
cmap=col_map,vmin=vmin,vmax=vmax)
plt.contourf( np.ma.masked_invalid(time), \
np.ma.masked_invalid(pre), \
np.ma.masked_invalid(dat), \
cmap=col_map,vmin=vmin,vmax=vmax)
else:
plt.pcolor( np.ma.masked_invalid(time), \
np.ma.masked_invalid(pre), \
np.ma.masked_invalid(dat), \
cmap=col_map,vmin=vmin,vmax=vmax)
except:
pass
ax=plt.gca()
ax.set_axis_bgcolor('gray')
plt.ylabel('Pressure [dbar]')
plt.xlabel('Time [month-year]')
plt.ylim((0,250))
#plt.title(title_txt)
# plt.gca().xaxis.set_major_locator(mp.dates.MonthLocator(range(1,12,2)))
plt.gca().xaxis.set_major_locator(mp.dates.MonthLocator(range(1,12,1)))
plt.gca().xaxis.set_major_formatter(mp.dates.DateFormatter('%m-%y'))
#plt.set_cmap('gist_stern')
if time_highlight is not None:
plt.plot([time_highlight,time_highlight],[plt.ylim()[0],plt.ylim()[1]] \
,color="#ff0000",linewidth=3)
if(time_min is None or time.min()<time_min):
time_min=time.min()
if(time_max is None or time.max()>time_max):
time_max=time.max()
if(tmin is not None):
time_min=tmin
if(tmax is not None):
time_max=tmax
print time_max,time_min
print mp.dates.num2date(time_min)
print mp.dates.num2date(time_max)
plt.xlim((time_min,time_max))
plt.gca().invert_yaxis()
if(show_colorbar):
plt.colorbar(label=title_txt)
locs,labels = plt.xticks()
plt.setp(labels,rotation=45)
#plt.savefig('%s.png' % (kuva),dpi=300)
if(save_file!=None):
plt.savefig(save_file,dpi=300)