-
Notifications
You must be signed in to change notification settings - Fork 1
/
viking2017.py
123 lines (105 loc) · 3.93 KB
/
viking2017.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
# -*- coding: utf-8 -*-
'''
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 mpl_toolkits.basemap import Basemap
from scipy.interpolate import griddata # here should remove nans or empty profiles
import netCDF4
#import matplotlib
#matplotlib.interactive(True)
# For plots
font = {'family' : 'DejaVu Sans',
'weight' : 'bold',
'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/2017_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_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_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')
Vsig = np.arange(21,27)
Vtemp = np.arange(-2, 20, .5)
Vsal = np.arange(29.5, 34, .25)
XLIM = [datetime.date(2017, 7, 01), datetime.date(2017, 12, 31)]
## ---- 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.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 T(^{\circ}C)$', fontsize=12, fontweight='normal')
# Save Figure
fig.set_size_inches(w=12, h=6)
fig.set_dpi(200)
outfile = 'Viking2017_temp.png'
fig.savefig(outfile)
# Save French Figure
plt.sca(ax)
plt.xlabel(u'Période', fontsize=15, fontweight='bold')
plt.ylabel('Profondeur (m)', fontsize=15, fontweight='bold')
fig.set_size_inches(w=12, h=6)
fig.set_dpi(200)
outfile = 'Viking2017_temp_FR.png'
fig.savefig(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 = 'Viking2017_sal.png'
fig.savefig(outfile)
# Save French Figure
plt.sca(ax)
plt.xlabel(u'Période', fontsize=15, fontweight='bold')
plt.ylabel('Profondeur (m)', fontsize=15, fontweight='bold')
fig.set_size_inches(w=12, h=6)
fig.set_dpi(200)
outfile = 'Viking2017_sal_FR.png'
fig.savefig(outfile)
#os.system('convert -trim ' + outfile + ' ' + outfile)