-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviking2019.py
197 lines (170 loc) · 6.85 KB
/
viking2019.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
'''
This is a test with xarray, see if I can manipulate a lot of netCDF files.
Try this script in /home/cyrf0006/research/AZMP_database/2017_data
'''
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import xarray as xr
import datetime
import matplotlib.pyplot as plt
from scipy.interpolate import griddata # here should remove nans or empty profiles
import netCDF4
import os
from matplotlib import dates
#import matplotlib
#matplotlib.interactive(True)
# For plots
font = {'family' : 'times',
'weight' : 'normal',
'size' : 14}
plt.rc('font', **font)
# This is a dataset
# in /home/cyrf0006/research/AZMP_database/2017_data
ds = xr.open_dataset('/home/cyrf0006/data/dev_database/viking_nc/2019_viking.nc')
# Some utils:
# np.unique(ds['instrument_ID'].values)
# np.unique(ds['survey_ID'].values)
#da_sal = ds_sub['salinity']
#df_sal = da_sal.to_pandas()
## ---- Select variables ---- ##
df_temp = ds['temperature'].to_dataframe()
df_temp = df_temp.unstack()
df_temp = df_temp.resample('1D').mean()
df_temp = df_temp.dropna(how='all')
df_temp = df_temp.droplevel(None, axis=1)
df_sal = ds['salinity'].to_dataframe()
df_sal = df_sal.unstack()
df_sal = df_sal.resample('1D').mean()
df_sal = df_sal.dropna(how='all')
df_sal = df_sal.droplevel(None, axis=1)
df_sig = ds['sigma-t'].to_dataframe()
df_sig = df_sig.unstack()
df_sig = df_sig.resample('1D').mean()
df_sig = df_sig.dropna(how='all')
df_sig = df_sig.droplevel(None, axis=1)
df_fluo = ds['fluorescence'].to_dataframe()
df_fluo = df_fluo.unstack()
df_fluo = df_fluo.resample('1D').mean()
df_fluo = df_fluo.dropna(how='all')
df_fluo = df_fluo.droplevel(None, axis=1)
Vsig = np.arange(21,27)
Vtemp = np.arange(-2, 20, .5)
Vsal = np.arange(29.5, 34, .25)
XLIM = [datetime.date(2019, 6, 1), datetime.date(2019, 11, 1)]
## Dorian mixing ##
df_sigH = ds['sigma-t'].to_dataframe()
df_sigH = df_sigH.unstack()
df_sigH = df_sigH.resample('1H').mean()
df_sigH = df_sigH.dropna(how='all')
df_sigH = df_sigH.droplevel(None, axis=1)
A = df_sigH[df_sigH.index.month==9]
B = A[(A.index.day<=9) & (A.index.day>=6) ]
B.T.plot()
plt.close('all')
plt.plot(B.iloc[1].values, B.columns.values)
plt.plot(B.iloc[6].values, B.columns.values)
plt.plot(B.iloc[8].values, B.columns.values)
plt.plot(B.iloc[10].values, B.columns.values)
plt.gca().invert_yaxis()
plt.grid()
plt.xlabel(r'$\sigma_0 (\rm{kg\,m^{-1}})$')
plt.legend(['6 Sept. 12:00','7 Sept. 20:00','9 Sept. 2:00', '9 sept. 21:00'])
## ---- plot temperature ---- ##
fig, ax = plt.subplots(nrows=1, ncols=1)
c = plt.contourf(df_temp.index, np.array(df_temp.columns.levels[1], dtype=float), df_temp.values.T, Vtemp, extend='both', cmap=plt.cm.RdBu_r)
cc = plt.contour(df_sig.index, np.array(df_sig.columns.levels[1], dtype=float), df_sig.values.T, Vsig, colors='k')
plt.plot(df_temp.index, np.repeat(0, df_temp.index.size), '|k', markersize=20)
plt.ylim([0, 175])
plt.xlim([XLIM[0], XLIM[1]])
plt.clabel(cc, inline=1, fontsize=10, colors='k', fmt='%1.1f')
plt.grid('on')
#plt.xlabel('Time', fontsize=15, fontweight='bold')
plt.ylabel('Depth (m)', fontsize=15, fontweight='bold')
plt.ylim([0, 175])
plt.gca().invert_yaxis()
cax = fig.add_axes([0.91, .15, 0.01, 0.7])
cb = plt.colorbar(c, cax=cax, orientation='vertical')
cb.set_label(r'$\rm T(^{\circ}C)$', fontsize=12, fontweight='normal')
# Save Figure
outfile = 'Viking2019_temp.png'
fig.savefig(outfile, dpi=200)
os.system('convert -trim ' + outfile + ' ' + outfile)
# Save Figure in French
outfile = 'Viking2019_temp_FR.png'
plt.ylabel('Profondeur (m)', fontsize=15, fontweight='bold')
fig.savefig(outfile, dpi=200)
os.system('convert -trim ' + outfile + ' ' + outfile)
## ---- plot Salinity ---- ##
fig, ax = plt.subplots(nrows=1, ncols=1)
c = plt.contourf(df_sal.index, np.array(df_sal.columns.levels[1], dtype=float), df_sal.values.T, Vsal, extend='both', cmap=plt.cm.RdBu_r)
cc = plt.contour(df_sig.index, np.array(df_sig.columns.levels[1], dtype=float), df_sig.values.T, Vsig, colors='k')
plt.plot(df_sal.index, np.repeat(0, df_sal.index.size), '|k', markersize=20)
plt.ylim([0, 175])
#plt.clabel(cc, inline=1, fontsize=10, colors='k', fmt='%1.1f')
plt.grid('on')
plt.xlabel('Time', fontsize=15, fontweight='bold')
plt.ylabel('Depth (m)', fontsize=15, fontweight='bold')
plt.ylim([0, 175])
plt.xlim([XLIM[0], XLIM[1]])
plt.gca().invert_yaxis()
cax = fig.add_axes([0.91, .15, 0.01, 0.7])
cb = plt.colorbar(c, cax=cax, orientation='vertical')
cb.set_label(r'$\rm S$', fontsize=12, fontweight='normal')
# Save Figure
fig.set_size_inches(w=12, h=6)
fig.set_dpi(200)
outfile = 'Viking2019_sal.png'
fig.savefig(outfile)
os.system('convert -trim ' + outfile + ' ' + outfile)
# Save Figure in French
outfile = 'Viking2019_sal_FR.png'
plt.ylabel('Profondeur (m)', fontsize=15, fontweight='bold')
fig.savefig(outfile, dpi=200)
os.system('convert -trim ' + outfile + ' ' + outfile)
## ---- plot both as suubplots ---- ##
fig = plt.figure()
# ax1
ax = plt.subplot2grid((2, 1), (0, 0))
c = plt.contourf(df_temp.index, np.array(df_temp.columns.levels[1], dtype=float), df_temp.values.T, Vtemp, extend='both', cmap=plt.cm.RdBu_r)
cc = plt.contour(df_sig.index, np.array(df_sig.columns.levels[1], dtype=float), df_sig.values.T, Vsig, colors='k')
plt.plot(df_temp.index, np.repeat(0, df_temp.index.size), '|k', markersize=20)
plt.ylim([0, 175])
plt.xlim([XLIM[0], XLIM[1]])
plt.clabel(cc, inline=1, fontsize=10, colors='k', fmt='%1.1f')
plt.grid('on')
plt.ylabel('Depth (m)', fontsize=15, fontweight='bold')
plt.ylim([0, 175])
plt.gca().invert_yaxis()
cax = fig.add_axes([0.91, .55, 0.01, 0.32])
cb = plt.colorbar(c, cax=cax, orientation='vertical')
ax.xaxis.label.set_visible(False)
ax.tick_params(labelbottom='off')
cb.set_label(r'$\rm T(^{\circ}C)$', fontsize=12, fontweight='normal')
# ax2
ax2 = plt.subplot2grid((2, 1), (1, 0))
c = plt.contourf(df_sal.index, np.array(df_sal.columns.levels[1], dtype=float), df_sal.values.T, Vsal, extend='both', cmap=plt.cm.RdBu_r)
cc = plt.contour(df_sig.index, np.array(df_sig.columns.levels[1], dtype=float), df_sig.values.T, Vsig, colors='k')
plt.plot(df_sal.index, np.repeat(0, df_sal.index.size), '|k', markersize=20)
plt.ylim([0, 175])
plt.grid('on')
#plt.xlabel('Time', fontsize=15, fontweight='bold')
plt.ylabel('Depth (m)', fontsize=15, fontweight='bold')
plt.ylim([0, 175])
plt.xlim([XLIM[0], XLIM[1]])
plt.gca().invert_yaxis()
cax2 = fig.add_axes([0.91, .12, 0.01, 0.32])
cb = plt.colorbar(c, cax=cax2, orientation='vertical')
cb.set_label(r'$\rm S$', fontsize=12, fontweight='normal')
# Save Figure
fig.set_size_inches(w=12, h=12)
outfile = 'Viking2019.png'
fig.savefig(outfile, dpi=200)
os.system('convert -trim ' + outfile + ' ' + outfile)
# French Figure
fig.set_size_inches(w=12, h=12)
ax.set_ylabel('Profondeur (m)', fontsize=15, fontweight='bold')
ax2.set_ylabel('Profondeur (m)', fontsize=15, fontweight='bold')
outfile = 'Viking2019_FR.png'
fig.savefig(outfile, dpi=200)
os.system('convert -trim ' + outfile + ' ' + outfile)