1 | #!/usr/bin/env python |
---|
2 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- |
---|
3 | |
---|
4 | # Post-diagnostic of STATION_ASF / L. Brodeau, 2019 |
---|
5 | |
---|
6 | import sys |
---|
7 | from os import path as path |
---|
8 | #from string import replace |
---|
9 | import math |
---|
10 | import numpy as nmp |
---|
11 | #import scipy.signal as signal |
---|
12 | from netCDF4 import Dataset |
---|
13 | import matplotlib as mpl |
---|
14 | mpl.use('Agg') |
---|
15 | import matplotlib.pyplot as plt |
---|
16 | import matplotlib.dates as mdates |
---|
17 | #from string import find |
---|
18 | #import warnings |
---|
19 | #warnings.filterwarnings("ignore") |
---|
20 | #import time |
---|
21 | |
---|
22 | #import barakuda_plot as bp |
---|
23 | #import barakuda_tool as bt |
---|
24 | |
---|
25 | reload(sys) |
---|
26 | sys.setdefaultencoding('utf8') |
---|
27 | |
---|
28 | cy1 = '2016' ; # First year |
---|
29 | cy2 = '2018' ; # Last year |
---|
30 | |
---|
31 | jt0 = 0 |
---|
32 | jt0 = 17519 |
---|
33 | |
---|
34 | |
---|
35 | dir_figs='.' |
---|
36 | size_fig=(13,7) |
---|
37 | fig_ext='png' |
---|
38 | |
---|
39 | clr_red = '#AD0000' |
---|
40 | clr_sat = '#ffed00' |
---|
41 | clr_mod = '#008ab8' |
---|
42 | |
---|
43 | rDPI=200. |
---|
44 | |
---|
45 | L_ALGOS = [ 'COARE3p6' , 'ECMWF' , 'NCAR' ] |
---|
46 | l_xtrns = [ '-noskin' , '-noskin' , '' ] ; # string to add to algo name (L_ALGOS) to get version without skin params turned on |
---|
47 | l_color = [ '#ffed00' , '#008ab8' , '0.4' ] ; # colors to differentiate algos on the plot |
---|
48 | l_width = [ 3 , 2 , 1 ] ; # line-width to differentiate algos on the plot |
---|
49 | l_style = [ '-' , '-' , '--' ] ; # line-style |
---|
50 | |
---|
51 | L_VNEM = [ 'qla' , 'qsb' , 'qt' , 'qlw' , 'taum' , 'dt_skin' ] |
---|
52 | L_VARO = [ 'Qlat' , 'Qsen' , 'Qnet' , 'Qlw' , 'Tau' , 'dT_skin' ] ; # name of variable on figure |
---|
53 | L_VARL = [ r'$Q_{lat}$', r'$Q_{sens}$' , r'$Q_{net}$' , r'$Q_{lw}$' , r'$|\tau|$' , r'$\Delta T_{skin}$' ] ; # name of variable in latex mode |
---|
54 | L_VUNT = [ r'$W/m^2$' , r'$W/m^2$' , r'$W/m^2$' , r'$W/m^2$' , r'$N/m^2$' , 'K' ] |
---|
55 | L_VMAX = [ 75. , 75. , 800. , 25. , 1.2 , -0.7 ] |
---|
56 | L_VMIN = [ -250. , -125. , -400. , -150. , 0. , 0.7 ] |
---|
57 | L_ANOM = [ True , True , True , True , True , False ] |
---|
58 | |
---|
59 | #L_VNEM = [ 'qlw' ] |
---|
60 | #L_VARO = [ 'Qlw' ] ; # name of variable on figure |
---|
61 | #L_VARL = [ r'$Q_{lw}$' ] ; # name of variable in latex mode |
---|
62 | #L_VUNT = [ r'$W/m^2$' ] |
---|
63 | #L_VMAX = [ 25. ] |
---|
64 | #L_VMIN = [ -150. ] |
---|
65 | #L_ANOM = [ True ] |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | nb_algos = len(L_ALGOS) ; print(nb_algos) |
---|
70 | |
---|
71 | # Getting arguments: |
---|
72 | narg = len(sys.argv) |
---|
73 | if narg != 2: |
---|
74 | print 'Usage: '+sys.argv[0]+' <DIR_OUT_SASF>'; sys.exit(0) |
---|
75 | cdir_data = sys.argv[1] |
---|
76 | |
---|
77 | |
---|
78 | |
---|
79 | # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
---|
80 | # Populating and checking existence of files to be read |
---|
81 | # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
---|
82 | def chck4f(cf): |
---|
83 | cmesg = 'ERROR: File '+cf+' does not exist !!!' |
---|
84 | if not path.exists(cf): print cmesg ; sys.exit(0) |
---|
85 | |
---|
86 | ###cf_in = nmp.empty((), dtype="S10") |
---|
87 | cf_in = [] ; cf_in_ns = [] |
---|
88 | for ja in range(nb_algos): |
---|
89 | cfi = cdir_data+'/output/'+'STATION_ASF-'+L_ALGOS[ja]+'_1h_'+cy1+'0101_'+cy2+'1231_gridT.nc' |
---|
90 | chck4f(cfi) |
---|
91 | cf_in.append(cfi) |
---|
92 | # Same but without skin params: |
---|
93 | for ja in range(nb_algos): |
---|
94 | cfi = cdir_data+'/output/'+'STATION_ASF-'+L_ALGOS[ja]+l_xtrns[ja]+'_1h_'+cy1+'0101_'+cy2+'1231_gridT.nc' |
---|
95 | chck4f(cfi) |
---|
96 | cf_in_ns.append(cfi) |
---|
97 | print('Files we are goin to use:') |
---|
98 | for ja in range(nb_algos): print(cf_in[ja]) |
---|
99 | print(' --- same without cool-skin/warm-layer:') |
---|
100 | for ja in range(nb_algos): print(cf_in_ns[ja]) |
---|
101 | #----------------------------------------------------------------- |
---|
102 | |
---|
103 | |
---|
104 | # Getting time array from the first file: |
---|
105 | id_in = Dataset(cf_in[0]) |
---|
106 | vt = id_in.variables['time_counter'][jt0:] |
---|
107 | cunit_t = id_in.variables['time_counter'].units ; print(' "time_counter" is in "'+cunit_t+'"') |
---|
108 | id_in.close() |
---|
109 | nbr = len(vt) |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | vtime = nmp.zeros(nbr) |
---|
115 | |
---|
116 | vt = vt + 1036800. + 30.*60. # BUG!??? don't get why false in epoch to date conversion, and yet ncview gets it right! |
---|
117 | for jt in range(nbr): vtime[jt] = mdates.epoch2num(vt[jt]) |
---|
118 | |
---|
119 | ii=nbr/300 |
---|
120 | ib=max(ii-ii%10,1) |
---|
121 | xticks_d=int(30*ib) |
---|
122 | |
---|
123 | font_inf = { 'fontname':'Open Sans', 'fontweight':'normal', 'fontsize':14 } |
---|
124 | |
---|
125 | nb_var = len(L_VNEM) |
---|
126 | |
---|
127 | xF = nmp.zeros((nbr,nb_algos)) |
---|
128 | xFa = nmp.zeros((nbr,nb_algos)) |
---|
129 | |
---|
130 | |
---|
131 | for ctest in ['skin','noskin']: |
---|
132 | |
---|
133 | for jv in range(nb_var): |
---|
134 | print('\n *** Treating variable: '+L_VARO[jv]+' ('+ctest+') !') |
---|
135 | |
---|
136 | for ja in range(nb_algos): |
---|
137 | # |
---|
138 | if ctest == 'skin': id_in = Dataset(cf_in[ja]) |
---|
139 | if ctest == 'noskin': id_in = Dataset(cf_in_ns[ja]) |
---|
140 | xF[:,ja] = id_in.variables[L_VNEM[jv]][jt0:,1,1] # only the center point of the 3x3 spatial domain! |
---|
141 | if ja == 0: cvar_lnm = id_in.variables[L_VNEM[jv]].long_name |
---|
142 | id_in.close() |
---|
143 | |
---|
144 | fig = plt.figure(num = jv, figsize=size_fig, facecolor='w', edgecolor='k') |
---|
145 | |
---|
146 | ax1 = plt.axes([0.07, 0.22, 0.9, 0.75]) |
---|
147 | |
---|
148 | ax1.set_xticks(vtime[::xticks_d]) |
---|
149 | ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S')) |
---|
150 | plt.xticks(rotation='60') |
---|
151 | |
---|
152 | for ja in range(nb_algos): |
---|
153 | plt.plot(vtime, xF[:,ja], '-', color=l_color[ja], linestyle=l_style[ja], linewidth=l_width[ja], label=L_ALGOS[ja], zorder=10+ja) |
---|
154 | |
---|
155 | ax1.set_ylim(L_VMIN[jv], L_VMAX[jv]) ; ax1.set_xlim(vtime[0],vtime[nbr-1]) |
---|
156 | plt.ylabel(L_VARL[jv]+' ['+L_VUNT[jv]+']') |
---|
157 | |
---|
158 | ax1.grid(color='k', linestyle='-', linewidth=0.3) |
---|
159 | plt.legend(bbox_to_anchor=(0.45, 0.2), ncol=1, shadow=True, fancybox=True) |
---|
160 | ax1.annotate(cvar_lnm+' ('+ctest+')', xy=(0.3, 0.97), xycoords='axes fraction', bbox={'facecolor':'w', 'alpha':1., 'pad':10}, zorder=50, **font_inf) |
---|
161 | plt.savefig(L_VARO[jv]+'_'+ctest+'.'+fig_ext, dpi=int(rDPI), transparent=False) |
---|
162 | plt.close(jv) |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | if L_ANOM[jv]: |
---|
167 | |
---|
168 | for ja in range(nb_algos): xFa[:,ja] = xF[:,ja] - nmp.mean(xF,axis=1) |
---|
169 | |
---|
170 | if nmp.sum(xFa[:,:]) == 0.0: |
---|
171 | print(' Well! Seems that for variable '+L_VARO[jv]+', choice of algo has no impact a all!') |
---|
172 | print(' ==> skipping anomaly plot...') |
---|
173 | |
---|
174 | else: |
---|
175 | |
---|
176 | # Want a symetric y-range that makes sense for the anomaly we're looking at: |
---|
177 | rmax = nmp.max(xFa) ; rmin = nmp.min(xFa) |
---|
178 | rmax = max( abs(rmax) , abs(rmin) ) |
---|
179 | romagn = math.floor(math.log(rmax, 10)) ; # order of magnitude of the anomaly we're dealing with |
---|
180 | rmlt = 10.**(int(romagn)) / 2. |
---|
181 | yrng = math.copysign( math.ceil(abs(rmax)/rmlt)*rmlt , rmax) |
---|
182 | #print 'yrng = ', yrng ; #sys.exit(0) |
---|
183 | |
---|
184 | fig = plt.figure(num = 10+jv, figsize=size_fig, facecolor='w', edgecolor='k') |
---|
185 | ax1 = plt.axes([0.07, 0.22, 0.9, 0.75]) |
---|
186 | |
---|
187 | ax1.set_xticks(vtime[::xticks_d]) |
---|
188 | ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S')) |
---|
189 | plt.xticks(rotation='60') |
---|
190 | |
---|
191 | for ja in range(nb_algos): |
---|
192 | plt.plot(vtime, xFa[:,ja], '-', color=l_color[ja], linewidth=l_width[ja], label=L_ALGOS[ja], zorder=10+ja) |
---|
193 | |
---|
194 | ax1.set_ylim(-yrng,yrng) ; ax1.set_xlim(vtime[0],vtime[nbr-1]) |
---|
195 | plt.ylabel(L_VARL[jv]+' ['+L_VUNT[jv]+']') |
---|
196 | ax1.grid(color='k', linestyle='-', linewidth=0.3) |
---|
197 | plt.legend(bbox_to_anchor=(0.45, 0.2), ncol=1, shadow=True, fancybox=True) |
---|
198 | ax1.annotate('Anomaly of '+cvar_lnm+' ('+ctest+')', xy=(0.3, 0.97), xycoords='axes fraction', bbox={'facecolor':'w', 'alpha':1., 'pad':10}, zorder=50, **font_inf) |
---|
199 | plt.savefig(L_VARO[jv]+'_'+ctest+'_anomaly.'+fig_ext, dpi=int(rDPI), transparent=False) |
---|
200 | plt.close(10+jv) |
---|
201 | |
---|
202 | |
---|
203 | |
---|
204 | |
---|
205 | # Difference skin vs noskin: |
---|
206 | xFns = nmp.zeros((nbr,nb_algos)) |
---|
207 | |
---|
208 | for jv in range(nb_var-1): |
---|
209 | print('\n *** Treating variable: '+L_VARO[jv]+' ('+ctest+') !') |
---|
210 | |
---|
211 | for ja in range(nb_algos-1): |
---|
212 | id_in = Dataset(cf_in[ja]) |
---|
213 | xF[:,ja] = id_in.variables[L_VNEM[jv]][jt0:,1,1] # only the center point of the 3x3 spatial domain! |
---|
214 | if ja == 0: cvar_lnm = id_in.variables[L_VNEM[jv]].long_name |
---|
215 | id_in.close() |
---|
216 | # |
---|
217 | id_in = Dataset(cf_in_ns[ja]) |
---|
218 | xFns[:,ja] = id_in.variables[L_VNEM[jv]][jt0:,1,1] # only the center point of the 3x3 spatial domain! |
---|
219 | if ja == 0: cvar_lnm = id_in.variables[L_VNEM[jv]].long_name |
---|
220 | id_in.close() |
---|
221 | |
---|
222 | xFa[:,ja] = xF[:,ja] - xFns[:,ja] ; # difference! |
---|
223 | |
---|
224 | |
---|
225 | # Want a symetric y-range that makes sense for the anomaly we're looking at: |
---|
226 | rmax = nmp.max(xFa) ; rmin = nmp.min(xFa) |
---|
227 | rmax = max( abs(rmax) , abs(rmin) ) |
---|
228 | romagn = math.floor(math.log(rmax, 10)) ; # order of magnitude of the anomaly we're dealing with |
---|
229 | rmlt = 10.**(int(romagn)) / 2. |
---|
230 | yrng = math.copysign( math.ceil(abs(rmax)/rmlt)*rmlt , rmax) |
---|
231 | print 'yrng = ', yrng ; #sys.exit(0) |
---|
232 | |
---|
233 | |
---|
234 | |
---|
235 | |
---|
236 | for ja in range(nb_algos-1): |
---|
237 | |
---|
238 | calgo = L_ALGOS[ja] |
---|
239 | |
---|
240 | if nmp.sum(xFa[:,ja]) == 0.0: |
---|
241 | print(' Well! Seems that for variable '+L_VARO[jv]+', and algo '+calgo+', skin param has no impact') |
---|
242 | print(' ==> skipping difference plot...') |
---|
243 | |
---|
244 | else: |
---|
245 | |
---|
246 | fig = plt.figure(num = jv, figsize=size_fig, facecolor='w', edgecolor='k') |
---|
247 | ax1 = plt.axes([0.07, 0.22, 0.9, 0.75]) |
---|
248 | |
---|
249 | ax1.set_xticks(vtime[::xticks_d]) |
---|
250 | ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S')) |
---|
251 | plt.xticks(rotation='60') |
---|
252 | |
---|
253 | plt.plot(vtime, xFa[:,ja], '-', color=l_color[ja], linestyle=l_style[ja], linewidth=l_width[ja], label=None, zorder=10+ja) |
---|
254 | |
---|
255 | ax1.set_ylim(-yrng,yrng) ; ax1.set_xlim(vtime[0],vtime[nbr-1]) |
---|
256 | plt.ylabel(L_VARL[jv]+' ['+L_VUNT[jv]+']') |
---|
257 | |
---|
258 | ax1.grid(color='k', linestyle='-', linewidth=0.3) |
---|
259 | #plt.legend(bbox_to_anchor=(0.45, 0.2), ncol=1, shadow=True, fancybox=True) |
---|
260 | ax1.annotate(cvar_lnm+' ('+ctest+')', xy=(0.3, 0.97), xycoords='axes fraction', bbox={'facecolor':'w', 'alpha':1., 'pad':10}, zorder=50, **font_inf) |
---|
261 | plt.savefig('diff_skin-noskin_'+L_VARO[jv]+'_'+calgo+'_'+ctest+'.'+fig_ext, dpi=int(rDPI), transparent=False) |
---|
262 | plt.close(jv) |
---|