[11930] | 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 |
---|
[11996] | 7 | from os import path as path |
---|
[11930] | 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 | |
---|
[11996] | 28 | cy1 = '2016' ; # First year |
---|
[11930] | 29 | cy2 = '2018' ; # Last year |
---|
| 30 | |
---|
[12031] | 31 | jt0 = 0 |
---|
| 32 | jt0 = 17519 |
---|
| 33 | |
---|
| 34 | |
---|
[11930] | 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' ] |
---|
[11996] | 46 | l_xtrns = [ '-noskin' , '-noskin' , '' ] ; # string to add to algo name (L_ALGOS) to get version without skin params turned on |
---|
[11930] | 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 | |
---|
[11996] | 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 ] |
---|
[11930] | 58 | |
---|
[11996] | 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 | |
---|
[11930] | 69 | nb_algos = len(L_ALGOS) ; print(nb_algos) |
---|
| 70 | |
---|
| 71 | # Getting arguments: |
---|
| 72 | narg = len(sys.argv) |
---|
[11996] | 73 | if narg != 2: |
---|
| 74 | print 'Usage: '+sys.argv[0]+' <DIR_OUT_SASF>'; sys.exit(0) |
---|
[11962] | 75 | cdir_data = sys.argv[1] |
---|
[11930] | 76 | |
---|
| 77 | |
---|
| 78 | |
---|
[11996] | 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) |
---|
[11930] | 97 | print('Files we are goin to use:') |
---|
| 98 | for ja in range(nb_algos): print(cf_in[ja]) |
---|
[11996] | 99 | print(' --- same without cool-skin/warm-layer:') |
---|
| 100 | for ja in range(nb_algos): print(cf_in_ns[ja]) |
---|
| 101 | #----------------------------------------------------------------- |
---|
[11930] | 102 | |
---|
| 103 | |
---|
| 104 | # Getting time array from the first file: |
---|
| 105 | id_in = Dataset(cf_in[0]) |
---|
[12031] | 106 | vt = id_in.variables['time_counter'][jt0:] |
---|
[11930] | 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 | |
---|
[12031] | 116 | vt = vt + 1036800. + 30.*60. # BUG!??? don't get why false in epoch to date conversion, and yet ncview gets it right! |
---|
[11930] | 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 | |
---|
[11996] | 131 | for ctest in ['skin','noskin']: |
---|
[11930] | 132 | |
---|
[11996] | 133 | for jv in range(nb_var): |
---|
| 134 | print('\n *** Treating variable: '+L_VARO[jv]+' ('+ctest+') !') |
---|
[11930] | 135 | |
---|
[11996] | 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]) |
---|
[12031] | 140 | xF[:,ja] = id_in.variables[L_VNEM[jv]][jt0:,1,1] # only the center point of the 3x3 spatial domain! |
---|
[11996] | 141 | if ja == 0: cvar_lnm = id_in.variables[L_VNEM[jv]].long_name |
---|
| 142 | id_in.close() |
---|
[11930] | 143 | |
---|
[11996] | 144 | fig = plt.figure(num = jv, figsize=size_fig, facecolor='w', edgecolor='k') |
---|
[11930] | 145 | |
---|
[11996] | 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): |
---|
[11930] | 212 | id_in = Dataset(cf_in[ja]) |
---|
[12031] | 213 | xF[:,ja] = id_in.variables[L_VNEM[jv]][jt0:,1,1] # only the center point of the 3x3 spatial domain! |
---|
[11930] | 214 | if ja == 0: cvar_lnm = id_in.variables[L_VNEM[jv]].long_name |
---|
[11996] | 215 | id_in.close() |
---|
| 216 | # |
---|
| 217 | id_in = Dataset(cf_in_ns[ja]) |
---|
[12031] | 218 | xFns[:,ja] = id_in.variables[L_VNEM[jv]][jt0:,1,1] # only the center point of the 3x3 spatial domain! |
---|
[11996] | 219 | if ja == 0: cvar_lnm = id_in.variables[L_VNEM[jv]].long_name |
---|
| 220 | id_in.close() |
---|
[11930] | 221 | |
---|
[11996] | 222 | xFa[:,ja] = xF[:,ja] - xFns[:,ja] ; # difference! |
---|
[11930] | 223 | |
---|
| 224 | |
---|
[11996] | 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) |
---|
[11930] | 232 | |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | |
---|
[11996] | 236 | for ja in range(nb_algos-1): |
---|
[11930] | 237 | |
---|
[11996] | 238 | calgo = L_ALGOS[ja] |
---|
[11930] | 239 | |
---|
[11996] | 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...') |
---|
[11930] | 243 | |
---|
[11996] | 244 | else: |
---|
[11930] | 245 | |
---|
[11996] | 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]) |
---|
[11930] | 248 | |
---|
[11996] | 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') |
---|
[11930] | 252 | |
---|
[11996] | 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) |
---|