source: NEMO/branches/2020/dev_r12563_ASINTER-06_ABL_improvement/tests/STATION_ASF/EXPREF/plot_station_asf.py @ 13159

Last change on this file since 13159 was 12031, checked in by laurent, 11 months ago

Keep up with r12030 of trunk + latex doc for SBCBLK!

  • Property svn:executable set to *
File size: 9.9 KB
Line 
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
6import sys
7from os import path as path
8#from string import replace
9import math
10import numpy as nmp
11#import scipy.signal as signal
12from netCDF4 import Dataset
13import matplotlib as mpl
14mpl.use('Agg')
15import matplotlib.pyplot as plt
16import 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
25reload(sys)
26sys.setdefaultencoding('utf8')
27
28cy1     = '2016' ; # First year
29cy2     = '2018' ; # Last year
30
31jt0 = 0
32jt0 = 17519
33
34
35dir_figs='.'
36size_fig=(13,7)
37fig_ext='png'
38
39clr_red = '#AD0000'
40clr_sat = '#ffed00'
41clr_mod = '#008ab8'
42
43rDPI=200.
44
45L_ALGOS = [ 'COARE3p6' , 'ECMWF'   , 'NCAR' ]
46l_xtrns = [ '-noskin'  , '-noskin' ,  ''    ] ; # string to add to algo name (L_ALGOS) to get version without skin params turned on
47l_color = [  '#ffed00' , '#008ab8' , '0.4'  ] ; # colors to differentiate algos on the plot
48l_width = [     3      ,    2      ,  1     ] ; # line-width to differentiate algos on the plot
49l_style = [    '-'     ,   '-'     , '--'   ] ; # line-style
50
51L_VNEM  = [   'qla'     ,     'qsb'     ,     'qt'     ,   'qlw'     ,  'taum'     ,    'dt_skin'         ]
52L_VARO  = [   'Qlat'    ,    'Qsen'     ,     'Qnet'   ,   'Qlw'     ,  'Tau'      ,    'dT_skin'         ] ; # name of variable on figure
53L_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
54L_VUNT  = [ r'$W/m^2$'  , r'$W/m^2$'    , r'$W/m^2$'   , r'$W/m^2$'  , r'$N/m^2$'  ,      'K'             ]
55L_VMAX  = [     75.     ,     75.       ,    800.      ,     25.     ,    1.2      ,      -0.7            ]
56L_VMIN  = [   -250.     ,   -125.       ,   -400.      ,   -150.     ,    0.       ,       0.7            ]
57L_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
69nb_algos = len(L_ALGOS) ; print(nb_algos)
70
71# Getting arguments:
72narg = len(sys.argv)
73if narg != 2:
74    print 'Usage: '+sys.argv[0]+' <DIR_OUT_SASF>'; sys.exit(0)
75cdir_data = sys.argv[1]
76
77
78
79# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
80# Populating and checking existence of files to be read
81# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
82def 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")
87cf_in = [] ; cf_in_ns = []
88for 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:
93for 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)
97print('Files we are goin to use:')
98for ja in range(nb_algos): print(cf_in[ja])
99print('   --- same without cool-skin/warm-layer:')
100for ja in range(nb_algos): print(cf_in_ns[ja])
101#-----------------------------------------------------------------
102
103
104# Getting time array from the first file:
105id_in = Dataset(cf_in[0])
106vt = id_in.variables['time_counter'][jt0:]
107cunit_t = id_in.variables['time_counter'].units ; print(' "time_counter" is in "'+cunit_t+'"')
108id_in.close()
109nbr = len(vt)
110
111
112
113
114vtime = nmp.zeros(nbr)
115
116vt = vt + 1036800. + 30.*60. # BUG!??? don't get why false in epoch to date conversion, and yet ncview gets it right!
117for jt in range(nbr): vtime[jt] = mdates.epoch2num(vt[jt])
118
119ii=nbr/300
120ib=max(ii-ii%10,1)
121xticks_d=int(30*ib)
122
123font_inf = { 'fontname':'Open Sans', 'fontweight':'normal', 'fontsize':14 }
124
125nb_var = len(L_VNEM)
126
127xF  = nmp.zeros((nbr,nb_algos))
128xFa = nmp.zeros((nbr,nb_algos))
129
130
131for 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:
206xFns = nmp.zeros((nbr,nb_algos))
207
208for 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)
Note: See TracBrowser for help on using the repository browser.