-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocean_state.py
196 lines (153 loc) · 6.44 KB
/
ocean_state.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
import numpy as np
import matplotlib.pyplot as plt
import netCDF4 as nc4
import numpy.ma as ma
# Setup parameters and constants
make_clim = 1
make_tran = True
# Select the background map library
map_type = 'nomap'
#map_type = 'basemap'
#map_type = 'cartopy'
if map_type == 'basemap':
from mpl_toolkits.basemap import Basemap
# Define a Basemap object for plotting
def map_setup(lon1,lon2,lat1,lat2,col_con,col_lake,col_sea,col_bound):
mymap = Basemap(projection='cyl',llcrnrlon=lon1, urcrnrlon=lon2, \
llcrnrlat=lat1, urcrnrlat=lat2, \
lon_0=0, lat_0=0, resolution='c')
# Add coastlines, meridian and parallel lines
mymap.drawcoastlines(color=col_bound,linewidth=.35)
mymap.drawmeridians(np.arange(0,360,30),color='gray',linewidth=.25)
mymap.drawparallels(np.arange(-90,90,30),color='gray',linewidth=.25)
return mymap
if map_type == 'cartopy':
import cartopy.crs as ccrs
def map_setup():
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180.))
ax.coastlines()
return ax
# Compute annual and zonal means, and deviations
def comp_stat(var):
var_am = np.mean(var,axis=0)
var_zm = np.mean(var,axis=3)
var_pp = var - var_am[None,:,:,:] # prime
var_ss = var - var_zm[:,:,:,None] # star
return var_am, var_zm, var_pp, var_ss
# Open oceanic data
data_path = './atmo_ocean_data/'
var_file = nc4.Dataset(data_path+'ucur.2012.nc','r')
# time/lev/lat/lon
uo = var_file.variables['ucur'][:]
uo = ma.masked_array(uo,mask=uo>3.9)
tim = var_file.variables['timePlot'][:]
lev = var_file.variables['level'][:]
lat = var_file.variables['lat'][:]
lon = var_file.variables['lon'][:]
var_file.close()
var_file = nc4.Dataset(data_path+'vcur.2012.nc','r')
# time/lev/lat/lon
vo = var_file.variables['vcur'][:]
vo = ma.masked_array(vo,mask=vo>3.9)
var_file.close()
var_file = nc4.Dataset(data_path+'pottmp.2012.nc','r')
# time/lev/lat/lon
theta = var_file.variables['pottmp'][:]
theta = ma.masked_array(theta,mask=theta>330)
var_file.close()
# Select here the longitude band (vo = vo[:,:,:,-60:0])
theta_am, theta_zm, theta_pp, theta_ss = comp_stat(theta)
vo_am, vo_zm, vo_pp, vo_ss = comp_stat(vo)
if make_clim is True:
# Zonal mean plot of temperature(s)
plt.figure()
imon = 0 # CHANGEME (0 = Jan, 11 = Dec)
#cont_t_plot = plt.contour(lat,lev,ta_zm[imon,:,:],np.linspace(200,300,11),colors='k')
#plt.clabel(cont_t_plot,fmt='%1.0i')
plt.contourf(lat,lev,theta_zm[imon,:,:],np.linspace(260,300,11))
plt.ylim([max(lev),min(lev)])
plt.title('Temperature month= %i' % (imon+1))
plt.colorbar()
plt.xlabel('Latitude (deg)')
plt.ylabel('Depth (m)')
# Lon/lat currents plot
plt.figure()
imon = 7 # CHANGEME (0 = Jan, 11 = Dec)
jlev = 10 # CHANGEME (0 = 5 m, 39 = 5000 m)
quiv_int = 5
if map_type == 'basemap':
map_setup(0,360,-90,90,'none','none','none','black')
if map_type == 'cartopy': # use transform keyword
map_setup()
plt.contourf(lon,lat,np.sqrt(uo[imon,jlev,:,:]**2+vo[imon,jlev,:,:]**2),
levels=np.arange(10)*0.1,cmap='Reds',transform=ccrs.PlateCarree())
if map_type in ['nomap','basemap']: # no transform
plt.contourf(lon,lat,np.sqrt(uo[imon,jlev,:,:]**2+vo[imon,jlev,:,:]**2),
levels=np.arange(10)*0.1,cmap='Reds')
plt.colorbar(orientation='horizontal')
#scale_arr = 1
#curr_map = plt.quiver(lon,lat,ucur[imon,jlev,:,:],vcur[imon,jlev,:,:],units='xy')
#plt.quiverkey(curr_map,0.9,1.1,scale_arr,str(scale_arr))
plt.title('Currents month= %i @ %i m' % (imon+1,lev[jlev]))
# Lon/lat theta plot
plt.figure()
imon = 7 # CHANGEME (0 = Jan, 11 = Dec)
jlev = 39 # CHANGEME (0 = 5 m, 39 = 5000 m)
if map_type == 'basemap':
map_setup(0,360,-90,90,'none','none','none','black')
if map_type == 'cartopy': # use transform keyword
map_setup()
plt.contourf(lon,lat,theta[imon,jlev,:,:],levels=270+np.arange(11)*3,
cmap='bwr',extend='both',transform=ccrs.PlateCarree())
if map_type in ['nomap','basemap']: # no transform
plt.contourf(lon,lat,theta[imon,jlev,:,:],levels=270+np.arange(11)*3,
cmap='bwr',extend='both')
plt.colorbar(orientation='horizontal')
plt.title('Theta month= %i @ %i m' % (imon+1,lev[jlev]))
plt.show()
if make_tran is True:
# Fig 13.5 PO92
var_am = theta_am; var_pp = theta_pp; var_ss = theta_ss; var_name = 'Heat'; var_uni = 'K m/s'
plt.figure()
clevs = np.linspace(-.1,.1,11)
plt.contour(lat,lev,np.mean(np.mean(vo_pp*var_pp,axis=0),axis=2),levels=clevs,colors='k')
plt.contourf(lat,lev,np.mean(np.mean(vo_pp*var_pp,axis=0),axis=2),levels=clevs,cmap='bwr')
plt.xlim([-80,80])
#plt.ylim([max(lev),min(lev)])
plt.ylim([400,0])
plt.colorbar(orientation='horizontal')
plt.xlabel('Latitude (deg)')
plt.ylabel('Depth (m)')
plt.title(var_name+' -- transient eddies ('+var_uni+')')
plt.figure()
clevs = np.linspace(-.1,.1,11)
plt.contour(lat,lev,np.mean(np.mean(vo_ss,axis=0)*np.mean(var_ss,axis=0),axis=2),levels=clevs,colors='k')
plt.contourf(lat,lev,np.mean(np.mean(vo_ss,axis=0)*np.mean(var_ss,axis=0),axis=2),levels=clevs,cmap='bwr')
plt.xlim([-80,80])
#plt.ylim([max(lev),min(lev)])
plt.ylim([400,0])
plt.colorbar(orientation='horizontal')
plt.xlabel('Latitude (deg)')
plt.ylabel('Depth (m)')
plt.title(var_name+' -- stationary eddies ('+var_uni+')')
plt.figure()
clevs = np.linspace(-5,5,11)
#plt.contour(lat,lev,np.mean(vo_am,axis=2)*np.mean(var_am,axis=2),levels=clevs,colors='k')
#plt.contourf(lat,lev,np.mean(vo_am,axis=2)*np.mean(var_am,axis=2),levels=clevs,cmap='bwr')
plt.contourf(lat,lev,np.mean(vo_am,axis=2)*np.mean(var_am,axis=2),levels=clevs,cmap='bwr',extend='both')
plt.xlim([-80,80])
#plt.ylim([max(lev),min(lev)])
plt.ylim([400,0])
plt.colorbar(orientation='horizontal')
plt.xlabel('Latitude (deg)')
plt.ylabel('Depth (m)')
plt.title(var_name+' -- mean circulation ('+var_uni+')')
# TODO add meridional profiles
# Layer thickness
#levp = np.append(lev[1::],lev[-1])
#dlev = levp-lev
#plt.figure()
#plt.plot(lat,vo_am[23,:,-30]*var_am[23,:,-30])
#plt.plot(lat,np.sum(np.mean(vo_am,axis=2)*np.mean(var_am,axis=2)*dlev[:,None],axis=0))
#plt.plot(lat,np.sum((vo_am[:,:,-33]*var_am[:,:,-33]*dlev[:,None])[0:15],axis=0))
plt.show()