[192] | 1 | import numpy as np |
---|
| 2 | import os |
---|
| 3 | from netCDF4 import Dataset as NetCDFFile |
---|
| 4 | import drawing_tools as drw |
---|
[773] | 5 | import generic_tools as gen |
---|
[192] | 6 | from optparse import OptionParser |
---|
| 7 | import sys |
---|
| 8 | from cStringIO import StringIO |
---|
| 9 | |
---|
[201] | 10 | ## e.g. # drawing.py -f /media/data2/etudes/WRF_LMDZ/WL_HyMeX/IIphase/medic950116/wlmdza/wrfout/wrfout_d01_1995-01-13_00:00:00 -o create_movie -S 'draw_2D_shad#Time@WRFTimes@10@95@191@1#tas:East_West|-1,North_South|-1,Time|2:longitude:latitude:Summer:270.,300.:tas|at|t=0:pdf:None:None' -v T2 |
---|
| 11 | ## e.g. # drawing.py -f wrfout_d01_1980-03-01_00\:00\:00_Time_B0-E48-I1.nc -o draw_2D_shad -S 'tas:East_West|-1,North_South|-1,Time|2:longitude:latitude:Summer:270.,300.:tas|at|t=0:pdf:None:cyl,i' -v T2 |
---|
| 12 | ## e.g. # drawing.py -f ~/etudes/domains/MEDCORDEX/geo_em.d01.nc -o draw_2D_shad_cont -S 'landmask,height:Time|0:Time|0:XLONG_M:XLAT_M:rainbow:fixc,k:%.2f:0,1:0.,3000.,10:landmask & height:pdf:False:lcc,i' -v LANDMASK,HGT_M |
---|
| 13 | ## e.g. # drawing.py -f ~/etudes/domains/MEDCORDEX/geo_em.d01.nc -o draw_2D_shad_cont -S 'height,landmask:Time|0:Time|0:XLONG_M:XLAT_M:terrain:fixc,k:None:0.,3000.:0,1,10:MEDCORDEX height & landmask:pdf:False:lcc,i' -v HGT_M,LANDMASK |
---|
| 14 | ## e.g. # drawing.py -o draw_2D_shad_line -f 'mean_dtcon-pluc-pres_lat.nc,mean_dtcon-pluc-pres_lat.nc' -S 'dtcon,prc:bottom_top|-1,south_north|-1:latmean:presmean:seismic,k:-5.,5.:monthly|dtcon|&|prc:pdf:flip@y:None:True' -v 'dtconmean,prcmean' |
---|
| 15 | ## e.g. # drawing.py -f 'geo_em.d02.nc' -o draw_topo_geogrid -S '0.,3000.:None:FF_3dom d02:png:cyl,i' |
---|
[655] | 16 | ## e.g. # drawing.py -o draw_topo_geogrid_boxes -S '0.,3000.:None:FF domains:pdf:lcc,i:d01,d02,d03:0' -f geo_em.d01.nc,geo_em.d02.nc,geo_em.d03.nc |
---|
[201] | 17 | ## e.g. # drawing.py -o draw_trajectories -f 'WRF/control/trajectory.dat@-1@/home/lluis/etudes/domains/WL_HyMeX_HighRes_C/geo_em.d03.nc#XLONG_M#XLAT_M,WRF_LMDZ/wlmdza/trajectory.dat@-1@/home/lluis/etudes/domains/WL_HyMeX_C/geo_em.d01.nc#XLONG_M#XLAT_M,WRF_LMDZ/wlmdzb/trajectory.dat@-1@/home/lluis/etudes/domains/WL_HyMeX_C/geo_em.d01.nc#XLONG_M#XLAT_M,WRF_LMDZ/wlmdzb_ii/trajectory.dat@-1@/home/lluis/etudes/domains/WL_HyMeX_C/geo_em.d01.nc#XLONG_M#XLAT_M' -S '$WRF_{CRM}$,$LMDZ_{AR4.0}$,$LMDZ_{NPv3.1}$,$LMDZ_{NPv3.1b}$|None|medicane trajectories|pdf|cyl,i' -v obs/trajectory.dat,satellite,-1 |
---|
| 18 | ## e.g. # drawing.py -o draw_vals_trajectories -f WRF_LMDZ/wlmdza/tevolboxtraj_T2.nc,WRF_LMDZ/wlmdzb/tevolboxtraj_T2.nc,WRF/control/tevolboxtraj_T2.nc -S 'mean:-1:$WRF_{CRM}$,$LMDZ_{AR4.0}$,$LMDZ_{NPv3.1}$@4:tas:time|($[DD]^[HH]$):exct,6,h:$%d^{%H}$:trajectory|following|mean:pdf' -v T2 |
---|
| 19 | ## e.g. # drawing.py -o draw_2D_shad_time -f 'netcdf_concatenated.nc' -S 'dtcon:Time|-1,bottom_top|-1:presmean:time:seismic:-3.e-6,3.e-6:monthly|dtcon:pdf:transpose:time|hours!since!1949-12-01|exct,2,d|%d|date!([DD])|x:True' -v 'dtconmean' |
---|
| 20 | ## e.g. # drawing.py -o variable_values -S PSFC |
---|
| 21 | ## e.g. # drawing.py -o draw_timeSeries -f wrfout_d01_1979-12-01_00:00:00_bottom_top_B6-E6-I1_south_north_B3-E3-I1_west_east_B26-E26-I1.nc -S 'dt_con:time|($[DD]^{[HH]}$):exct,12,h:$%d^{%H}$:time|evolution|at|-1|6|3|26:1:pdf' -v 'LDQCON,time' |
---|
| 22 | ## e.g. # drawing.py -f wrfout_d01_1979-12-01_00:00:00 -o draw_Neighbourghood_evol -S 'q:Time|-1|Times,bottom_top|6|ZNU,south_north|3|XLAT,west_east|26|XLONG:south_north,west_east:5:auto:time|($[DD]^{[HH]}$),time|($[DD]^{[HH]}$):exct,2,h|exct,1,d:$%d^{%H}$,$%d^{%H}$:5|pts|neighbourghood|temporal|evolution:0.0,0.004:BuPu:pdf:True' -v QVAPOR |
---|
| 23 | ## e.g. # drawing.py -o draw_lines_time -f wrfout_d01_1980-03-01_00:00:00_Time_B0-E48-I1_south_north_B15-E15-I1_west_east_B15-E15-I1.nc -S 'time;y;time ([DD]${[HH]}$);file1;tas;evolution;time|hours!since!1949-12-01_00:00:00|exct,12,h|%d$^{%H}$;pdf' -v T2 |
---|
[541] | 24 | ## e.g. # drawing.py -o draw_barbs -f ERAI_pl199501_131-132.nc -S 'X|lon|lon|-1,Y|lat|lat|-1,Z|lev|lev|4,T|time|time|0:auto,auto,auto:wind,ms-1:cyl,c:ERA-Interim|winds|at|1000|hPa|on|1996|January|1st|at|00|UTC:pdf:ERAI_pl199501_131-132' -v var131,var132 |
---|
[549] | 25 | ## e.g. # ~/etudes/WRF_LMDZ/svn/LMDZ_WRF/tools/drawing.py -o draw_points -S 'tslist.dat,#,3,2,1:SuperStorm|sfc|stations:cyl,i:legend:auto:None:0:png:stations_loc' -f $HOME'/etudes/domains/WRFdynamicoSENS_SuperStorm/geo_em.d02.nc,XLONG_M,XLAT_M,HGT_M,Time|0,height,0.,3000.,terrain,m' |
---|
| 26 | ## e.g. # drawing.py -o draw_points -S 'tslist.dat,#,3,2,1:SuperStorm|sfc|stations:cyl,i:labelled,8,black:auto:None:0:png:stations_loc' -f $HOME'/etudes/domains/WRFdynamicoSENS_SuperStorm/geo_em_west_east_B25-E180-I1_south_north_B160-E262-I1.nc,XLONG_M,XLAT_M,HGT_M,Time|0,height,0.,1500.,terrain,m' |
---|
[651] | 27 | ## e.g. # drawing.py -o draw_ptZvals -f geo_v2_2012102123_RR1.nc -S 'pracc:lon,lat:o:80:2,42,7,47,:values!of!values:Blues:cyl,l:pdf' -v pr |
---|
[668] | 28 | ## e.g. # drawing.py -f carteveg5km.nc -o draw_points_lonlat -S 'longitude:latitude:pdf:points!veget|type:green:.:0.5:None:0:legend' |
---|
[698] | 29 | ## e.g. # drawing.py -o draw_vectors -f wrfout_d01_2001-11-11_00:00:00 -S 'T|Time|Times|2,Y|south_north|XLAT|-1,X|west_east|XLONG|-1:3@3,wind@rainbow,9:10m wind,ms-1:cyl,l:WRF 10 m winds:pdf:winds' -v U10,V10 |
---|
[704] | 30 | ## e.g. # drawing.py -o draw_basins -f routing.py -S 'Y|y|nav_lat|-1,X|x|nav_lon|-1:1@1,rainbow,9:basins,-:cyl,l:ORCDHIEE river-basins:pdf:basins_named' -v nav_lon,nav_lat,trip,basins |
---|
[705] | 31 | ## e.g. # drawing.py -o draw_river_desc -f diver_desc.nc -S 'Y|lat|lat|-1,X|lon|lon|-1:red,green:Blues:cyl,l:ORCDHIEE rivers:pdf:0:or_rivers -v Amazon |
---|
[774] | 32 | ## e.g. # drawing.py -o draw_vertical_levels -f wrfout_d01_2001-11-11_00:00:00 -S 'true:false:wrfout!vertical!levels!(standard!40):png:4' -v WRFz |
---|
[651] | 33 | |
---|
[192] | 34 | main = 'drawing.py' |
---|
| 35 | |
---|
| 36 | errormsg = 'ERROR -- error -- ERROR -- error' |
---|
| 37 | warnmsg = 'WARNING -- waring -- WARNING -- warning' |
---|
| 38 | fillValue=1.e20 |
---|
| 39 | |
---|
| 40 | namegraphics = ['create_movie', 'draw_2D_shad', 'draw_2D_shad_time', \ |
---|
| 41 | 'draw_2D_shad_cont', 'draw_2D_shad_cont_time', 'draw_2D_shad_line', \ |
---|
[704] | 42 | 'draw_2D_shad_line_time', 'draw_barbs', 'draw_basins', \ |
---|
[698] | 43 | 'draw_lines', 'draw_lines_time', 'draw_Neighbourghood_evol', \ |
---|
| 44 | 'draw_points', 'draw_points_lonlat', \ |
---|
[705] | 45 | 'draw_ptZvals', 'draw_river_desc', 'draw_timeSeries', \ |
---|
[651] | 46 | 'draw_topo_geogrid', \ |
---|
[192] | 47 | 'draw_topo_geogrid_boxes', 'draw_trajectories', 'draw_vals_trajectories', \ |
---|
[773] | 48 | 'draw_vectors', 'draw_vertical_levels', 'list_graphics', 'variable_values'] |
---|
[192] | 49 | |
---|
| 50 | def draw_2D_shad(ncfile, values, varn): |
---|
| 51 | """ plotting a fields with shading |
---|
| 52 | draw_2D_shad(ncfile, values, varn) |
---|
| 53 | ncfile= file to use |
---|
| 54 | values=[vnamefs]:[dimvals]:[dimxvn]:[dimyvn]:[colorbar]:[sminv],[smaxv]:[figt]: |
---|
| 55 | [kindfig]:[reverse]:[mapv]:[close] |
---|
| 56 | [vnamefs]: Name in the figure of the variable to be shaded |
---|
| 57 | [dimvals]: ',' list of [dimname]|[value] telling at which dimension of the |
---|
| 58 | variable a given value is required (-1, all the length) |
---|
| 59 | [dimx/yvn]: name of the variables with the values of the final dimensions (x,y) |
---|
| 60 | [colorbar]: name of the color bar |
---|
| 61 | [smin/axv]: minimum and maximum value for the shading or: |
---|
| 62 | 'Srange': for full range |
---|
| 63 | 'Saroundmean@val': for mean-xtrm,mean+xtrm where xtrm = np.min(mean-min@val,max@val-mean) |
---|
| 64 | 'Saroundminmax@val': for min*val,max*val |
---|
| 65 | 'Saroundpercentile@val': for median-xtrm,median+xtrm where xtrm = np.min(median-percentile_(val), |
---|
| 66 | percentile_(100-val)-median) |
---|
| 67 | 'Smean@val': for -xtrm,xtrm where xtrm = np.min(mean-min*@val,max*@val-mean) |
---|
| 68 | 'Smedian@val': for -xtrm,xtrm where xtrm = np.min(median-min@val,max@val-median) |
---|
| 69 | 'Spercentile@val': for -xtrm,xtrm where xtrm = np.min(median-percentile_(val), |
---|
| 70 | percentile_(100-val)-median) |
---|
| 71 | [figt]: title of the figure ('|' for spaces) |
---|
| 72 | [kindfig]: kind of figure |
---|
| 73 | [reverse]: Transformation of the values |
---|
| 74 | * 'transpose': reverse the axes (x-->y, y-->x) |
---|
| 75 | * 'flip'@[x/y]: flip the axis x or y |
---|
| 76 | [mapv]: map characteristics: [proj],[res] |
---|
| 77 | see full documentation: http://matplotlib.org/basemap/ |
---|
| 78 | [proj]: projection |
---|
| 79 | * 'cyl', cilindric |
---|
| 80 | * 'lcc', lamvbert conformal |
---|
| 81 | [res]: resolution: |
---|
| 82 | * 'c', crude |
---|
| 83 | * 'l', low |
---|
| 84 | * 'i', intermediate |
---|
| 85 | * 'h', high |
---|
| 86 | * 'f', full |
---|
| 87 | valules= 'rh:z|-1,x|-1:z|-1,x|-1:lat:pressure:BuPu:0.,100.:rh:pdf:flip@y:None' |
---|
| 88 | varn= [varsn] name of the variable to plot with shading |
---|
| 89 | """ |
---|
| 90 | |
---|
| 91 | fname = 'draw_2D_shad' |
---|
| 92 | if values == 'h': |
---|
| 93 | print fname + '_____________________________________________________________' |
---|
| 94 | print draw_2D_shad.__doc__ |
---|
| 95 | quit() |
---|
| 96 | |
---|
[690] | 97 | expectargs = '[vnamefs]:[dimvals]:[dimxvn]:[dimyvn]:[colorbar]:[sminv],[smaxv]'+\ |
---|
| 98 | ':[figt]:[kindfig]:[reverse]:[mapv]:[close]' |
---|
[192] | 99 | |
---|
[690] | 100 | drw.check_arguments(fname,values,expectargs,':') |
---|
[192] | 101 | |
---|
| 102 | vnamesfig = values.split(':')[0] |
---|
| 103 | dimvals= values.split(':')[1].replace('|',':') |
---|
| 104 | vdimxn = values.split(':')[2] |
---|
| 105 | vdimyn = values.split(':')[3] |
---|
| 106 | colbarn = values.split(':')[4] |
---|
| 107 | shadminmax = values.split(':')[5] |
---|
| 108 | figtitle = values.split(':')[6].replace('|',' ') |
---|
| 109 | figkind = values.split(':')[7] |
---|
| 110 | revals = values.split(':')[8] |
---|
| 111 | mapvalue = values.split(':')[9] |
---|
| 112 | # varn = values.split(':')[10] |
---|
| 113 | |
---|
| 114 | ncfiles = ncfile |
---|
| 115 | |
---|
| 116 | if not os.path.isfile(ncfiles): |
---|
| 117 | print errormsg |
---|
| 118 | print ' ' + fname + ': shading file "' + ncfiles + '" does not exist !!' |
---|
| 119 | quit(-1) |
---|
| 120 | |
---|
| 121 | objsf = NetCDFFile(ncfiles, 'r') |
---|
| 122 | |
---|
| 123 | varns = varn.split(',')[0] |
---|
| 124 | |
---|
| 125 | if not objsf.variables.has_key(varns): |
---|
| 126 | print errormsg |
---|
| 127 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 128 | '" does not have variable "' + varns + '" !!' |
---|
| 129 | quit(-1) |
---|
| 130 | |
---|
| 131 | # Variables' values |
---|
| 132 | objvars = objsf.variables[varns] |
---|
| 133 | |
---|
| 134 | valshad, dimsshad = drw.slice_variable(objvars, dimvals.replace(',','|')) |
---|
| 135 | |
---|
| 136 | # Dimensions names |
---|
| 137 | ## print fname + ' obj dimnames: ', objvars.dimensions, dimvals, len(dimvals.split(',')) |
---|
| 138 | ## dimnamesv = [] |
---|
| 139 | ## for idd in range(len(objvars.dimensions)): |
---|
| 140 | ## cutdim = False |
---|
| 141 | ## for idc in range(len(dimvals.split(','))): |
---|
| 142 | ## dimcutn = dimvals.split(',')[idc].split(':')[0] |
---|
| 143 | ## print objvars.dimensions[idd], dimcutn |
---|
| 144 | ## if objvars.dimensions[idd] == dimcutn: |
---|
| 145 | ## cutdim = True |
---|
| 146 | ## break |
---|
| 147 | ## if not cutdim: dimnamesv.append(objvars.dimensions[idd]) |
---|
| 148 | dimnamesv = [vdimyn, vdimxn] |
---|
| 149 | |
---|
| 150 | if drw.searchInlist(objvars.ncattrs(),'units'): |
---|
| 151 | varunits = objvars.getncattr('units') |
---|
| 152 | else: |
---|
| 153 | print warnmsg |
---|
| 154 | print ' ' + fname + ": variable '" + varn + "' without units!!" |
---|
| 155 | varunits = '-' |
---|
| 156 | |
---|
| 157 | if not objsf.variables.has_key(vdimxn): |
---|
| 158 | print errormsg |
---|
| 159 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 160 | '" does not have dimension variable "' + vdimxn + '" !!' |
---|
| 161 | quit(-1) |
---|
| 162 | if not objsf.variables.has_key(vdimyn): |
---|
| 163 | print errormsg |
---|
| 164 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 165 | '" does not have dimension variable "' + vdimyn + '" !!' |
---|
| 166 | quit(-1) |
---|
| 167 | |
---|
| 168 | objdimx = objsf.variables[vdimxn] |
---|
| 169 | objdimy = objsf.variables[vdimyn] |
---|
| 170 | if drw.searchInlist(objdimx.ncattrs(),'units'): |
---|
| 171 | odimxu = objdimx.getncattr('units') |
---|
| 172 | else: |
---|
| 173 | print warnmsg |
---|
| 174 | print ' ' + fname + ": variable dimension '" + vdimxn + "' without units!!" |
---|
| 175 | odimxu = '-' |
---|
| 176 | |
---|
| 177 | if drw.searchInlist(objdimy.ncattrs(),'units'): |
---|
| 178 | odimyu = objdimy.getncattr('units') |
---|
| 179 | else: |
---|
| 180 | print warnmsg |
---|
| 181 | print ' ' + fname + ": variable dimension '" + vdimyn + "' without units!!" |
---|
| 182 | odimyu = '-' |
---|
| 183 | |
---|
[477] | 184 | odimxv, odimyv = drw.dxdy_lonlatDIMS(objdimx[:], objdimy[:], objdimx.dimensions, \ |
---|
| 185 | objdimy.dimensions, dimvals.replace(':','|').split(',')) |
---|
[192] | 186 | |
---|
[477] | 187 | |
---|
[476] | 188 | # if len(objdimx.shape) <= 2: |
---|
| 189 | ## odimxv = objdimx[valshad.shape] |
---|
| 190 | ## odimyv = objdimy[valshad.shape] |
---|
| 191 | # odimxv = objdimx[:] |
---|
| 192 | # odimyv = objdimy[:] |
---|
[192] | 193 | |
---|
[476] | 194 | # elif len(objdimx.shape) == 3: |
---|
| 195 | ## dimcut = [0, slice(0,valshad.shape[0]), slice(0,valshad.shape[1])] |
---|
| 196 | ## odimxv = objdimx[tuple(dimcut)] |
---|
| 197 | ## odimyv = objdimy[tuple(dimcut)] |
---|
| 198 | # odimxv = objdimx[0,:] |
---|
| 199 | # odimyv = objdimy[0,:] |
---|
| 200 | # else: |
---|
| 201 | # print errormsg |
---|
| 202 | # print ' ' + fname + ': shape of dimension variable:', objdimx.shape, \ |
---|
| 203 | # ' not ready!!' |
---|
| 204 | # quit(-1) |
---|
| 205 | |
---|
[192] | 206 | shading_nx = [] |
---|
| 207 | if shadminmax.split(',')[0][0:1] != 'S': |
---|
| 208 | shading_nx.append(np.float(shadminmax.split(',')[0])) |
---|
| 209 | else: |
---|
| 210 | shading_nx.append(shadminmax.split(',')[0]) |
---|
| 211 | |
---|
| 212 | if shadminmax.split(',')[1][0:1] != 'S': |
---|
| 213 | shading_nx.append(np.float(shadminmax.split(',')[1])) |
---|
| 214 | else: |
---|
| 215 | shading_nx.append(shadminmax.split(',')[1]) |
---|
| 216 | |
---|
| 217 | if mapvalue == 'None': mapvalue = None |
---|
| 218 | |
---|
| 219 | drw.plot_2D_shadow(valshad, vnamesfig, odimxv, odimyv, odimxu, odimyu, dimnamesv,\ |
---|
| 220 | colbarn, shading_nx, varunits, figtitle, figkind, revals, mapvalue, True) |
---|
| 221 | |
---|
| 222 | return |
---|
| 223 | |
---|
| 224 | def draw_2D_shad_time(ncfile, values, varn): |
---|
| 225 | """ plotting a fields with shading with time values |
---|
| 226 | draw_2D_shad(ncfile, values, varn) |
---|
| 227 | ncfile= file to use |
---|
[638] | 228 | values=[vnamefs]~[dimvals]~[dimxvn]~[dimyvn]~[colorbar]~[sminv],[smaxv]~[figt]~ |
---|
| 229 | [kindfig]~[reverse]~[timevals]~[close] |
---|
[192] | 230 | [vnamefs]: Name in the figure of the variable to be shaded |
---|
| 231 | [dimvals]: ',' list of [dimname]|[value] telling at which dimension of the |
---|
[638] | 232 | variable a given value is required (-1, all the length, [beg]@[end] for an interval) |
---|
[192] | 233 | [dimx/yvn]: name of the variables with the values of the final dimensions (x,y) |
---|
| 234 | [colorbar]: name of the color bar |
---|
| 235 | [smin/axv]: minimum and maximum value for the shading or: |
---|
| 236 | 'Srange': for full range |
---|
| 237 | 'Saroundmean@val': for mean-xtrm,mean+xtrm where xtrm = np.min(mean-min@val,max@val-mean) |
---|
| 238 | 'Saroundminmax@val': for min*val,max*val |
---|
| 239 | 'Saroundpercentile@val': for median-xtrm,median+xtrm where xtrm = np.min(median-percentile_(val), |
---|
| 240 | percentile_(100-val)-median) |
---|
| 241 | 'Smean@val': for -xtrm,xtrm where xtrm = np.min(mean-min*@val,max*@val-mean) |
---|
| 242 | 'Smedian@val': for -xtrm,xtrm where xtrm = np.min(median-min@val,max@val-median) |
---|
| 243 | 'Spercentile@val': for -xtrm,xtrm where xtrm = np.min(median-percentile_(val), |
---|
| 244 | percentile_(100-val)-median) |
---|
| 245 | [figt]: title of the figure ('|' for spaces) |
---|
| 246 | [kindfig]: kind of figure |
---|
| 247 | [reverse]: Transformation of the values |
---|
| 248 | * 'transpose': reverse the axes (x-->y, y-->x) |
---|
| 249 | * 'flip'@[x/y]: flip the axis x or y |
---|
| 250 | [timevals]: [timen]|[units]|[kind]|[tfmt]|[label]|[timeaxis] time labels characteristics |
---|
| 251 | [timen]; name of the time variable |
---|
| 252 | [units]; units string according to CF conventions ([tunits] since |
---|
| 253 | [YYYY]-[MM]-[DD] [[HH]:[MI]:[SS]], '!' for spaces) |
---|
| 254 | [kind]; kind of output |
---|
| 255 | 'Nval': according to a given number of values as 'Nval',[Nval] |
---|
| 256 | 'exct': according to an exact time unit as 'exct',[tunit]; |
---|
| 257 | tunit= [Nunits],[tu]; [tu]= 'c': centuries, 'y': year, 'm': month, |
---|
| 258 | 'w': week, 'd': day, 'h': hour, 'i': minute, 's': second, |
---|
| 259 | 'l': milisecond |
---|
| 260 | [tfmt]; desired format |
---|
| 261 | [label]; label at the graph ('!' for spaces) |
---|
| 262 | [close]: should figure be closed (finished) |
---|
[638] | 263 | values='dtcon~Time|-1,bottom_top|-1~presmean~time~seismic~-3.e-6,3.e-6~monthly|' |
---|
| 264 | 'dtcon~pdf~transpose~time|hours!since!1949-12-01|exct,2,d|%d|date!([DD])~True |
---|
[192] | 265 | varn= [varsn] name of the variable to plot with shading |
---|
| 266 | """ |
---|
| 267 | fname = 'draw_2D_shad_time' |
---|
| 268 | |
---|
| 269 | if values == 'h': |
---|
| 270 | print fname + '_____________________________________________________________' |
---|
| 271 | print draw_2D_shad_time.__doc__ |
---|
| 272 | quit() |
---|
| 273 | |
---|
[690] | 274 | farguments = '[vnamefs]~[dimvals]~[dimxvn]~[dimyvn]~[colorbar]~[sminv],[smaxv]~'+\ |
---|
| 275 | '[figt]~[kindfig]~[reverse]~[timevals]~[close]' |
---|
| 276 | drw.check_arguments(fname,values,farguments,'~') |
---|
[192] | 277 | |
---|
[638] | 278 | vnamesfig = values.split('~')[0] |
---|
| 279 | dimvals= values.split('~')[1].replace('|',':') |
---|
| 280 | vdimxn = values.split('~')[2] |
---|
| 281 | vdimyn = values.split('~')[3] |
---|
| 282 | colbarn = values.split('~')[4] |
---|
| 283 | shadminmax = values.split('~')[5] |
---|
| 284 | figtitle = values.split('~')[6].replace('|',' ') |
---|
| 285 | figkind = values.split('~')[7] |
---|
| 286 | revals = values.split('~')[8] |
---|
| 287 | timevals = values.split('~')[9] |
---|
| 288 | close = values.split('~')[10] |
---|
[192] | 289 | |
---|
| 290 | ncfiles = ncfile |
---|
| 291 | |
---|
| 292 | if not os.path.isfile(ncfiles): |
---|
| 293 | print errormsg |
---|
| 294 | print ' ' + fname + ': shading file "' + ncfiles + '" does not exist !!' |
---|
| 295 | quit(-1) |
---|
| 296 | |
---|
| 297 | objsf = NetCDFFile(ncfiles, 'r') |
---|
| 298 | |
---|
| 299 | varns = varn.split(',')[0] |
---|
| 300 | |
---|
| 301 | if not objsf.variables.has_key(varns): |
---|
| 302 | print errormsg |
---|
| 303 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 304 | '" does not have variable "' + varns + '" !!' |
---|
| 305 | quit(-1) |
---|
| 306 | |
---|
| 307 | # Variables' values |
---|
| 308 | objvars = objsf.variables[varns] |
---|
| 309 | |
---|
| 310 | valshad, dimsshad = drw.slice_variable(objvars, dimvals.replace(',','|')) |
---|
| 311 | |
---|
| 312 | dimnamesv = [vdimyn, vdimxn] |
---|
| 313 | |
---|
| 314 | varunits = objvars.getncattr('units') |
---|
| 315 | |
---|
| 316 | if not objsf.variables.has_key(vdimxn): |
---|
| 317 | print errormsg |
---|
| 318 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 319 | '" does not have dimension variable "' + vdimxn + '" !!' |
---|
| 320 | quit(-1) |
---|
| 321 | if not objsf.variables.has_key(vdimyn): |
---|
| 322 | print errormsg |
---|
| 323 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 324 | '" does not have dimensino variable "' + vdimyn + '" !!' |
---|
| 325 | quit(-1) |
---|
| 326 | |
---|
| 327 | objdimx = objsf.variables[vdimxn] |
---|
| 328 | objdimy = objsf.variables[vdimyn] |
---|
| 329 | odimxu = objdimx.getncattr('units') |
---|
| 330 | odimyu = objdimy.getncattr('units') |
---|
| 331 | |
---|
| 332 | if len(objdimx.shape) <= 2: |
---|
[639] | 333 | odimxv0 = objdimx[:] |
---|
| 334 | odimyv0 = objdimy[:] |
---|
[192] | 335 | |
---|
| 336 | elif len(objdimx.shape) == 3: |
---|
[639] | 337 | odimxv0 = objdimx[0,:] |
---|
| 338 | odimyv0 = objdimy[0,:] |
---|
[192] | 339 | else: |
---|
| 340 | print errormsg |
---|
| 341 | print ' ' + fname + ': shape of dimension variable:', objdimx.shape, \ |
---|
| 342 | ' not ready!!' |
---|
| 343 | quit(-1) |
---|
| 344 | |
---|
| 345 | timename = timevals.split('|')[0] |
---|
| 346 | timeunit = timevals.split('|')[1].replace('!',' ') |
---|
| 347 | timekind = timevals.split('|')[2] |
---|
| 348 | timefmt = timevals.split('|')[3] |
---|
| 349 | timelabel = timevals.split('|')[4].replace('!',' ') |
---|
| 350 | |
---|
[639] | 351 | # Dimensional values |
---|
[641] | 352 | odxv, dimsdxv = drw.slice_variable(objsf.variables[vdimxn], dimvals.replace(',','|')) |
---|
| 353 | odyv, dimsdyv = drw.slice_variable(objsf.variables[vdimyn], dimvals.replace(',','|')) |
---|
[639] | 354 | |
---|
[192] | 355 | if vdimxn == timename: |
---|
[641] | 356 | odimxv = objsf.variables[vdimxn][:] |
---|
[192] | 357 | odimxu = timelabel |
---|
| 358 | timeaxis = 'x' |
---|
[641] | 359 | odimyv = objsf.variables[vdimyn] |
---|
[192] | 360 | odimyu = odimyv.getncattr('units') |
---|
[641] | 361 | timepos, timelabels = drw.CFtimes_plot(odxv, timeunit, timekind, timefmt) |
---|
[192] | 362 | elif vdimyn == timename: |
---|
[641] | 363 | odimyv = objsf.variables[vdimyn] |
---|
[192] | 364 | odimyu = timelabel |
---|
| 365 | timeaxis = 'y' |
---|
[641] | 366 | odimxv = objsf.variables[vdimxn] |
---|
[192] | 367 | odimxu = odimxv.getncattr('units') |
---|
[641] | 368 | timepos, timelabels = drw.CFtimes_plot(odyv, timeunit, timekind, timefmt) |
---|
[192] | 369 | else: |
---|
| 370 | print errormsg |
---|
| 371 | print ' ' + fname + ": time variable '" + timename + "' not found!!" |
---|
| 372 | quit(-1) |
---|
| 373 | |
---|
| 374 | shading_nx = [] |
---|
| 375 | if shadminmax.split(',')[0][0:1] != 'S': |
---|
| 376 | shading_nx.append(np.float(shadminmax.split(',')[0])) |
---|
| 377 | else: |
---|
| 378 | shading_nx.append(shadminmax.split(',')[0]) |
---|
| 379 | |
---|
| 380 | if shadminmax.split(',')[1][0:1] != 'S': |
---|
| 381 | shading_nx.append(np.float(shadminmax.split(',')[1])) |
---|
| 382 | else: |
---|
| 383 | shading_nx.append(shadminmax.split(',')[1]) |
---|
| 384 | |
---|
| 385 | closeval = drw.Str_Bool(close) |
---|
| 386 | |
---|
[641] | 387 | drw.plot_2D_shadow_time(valshad, vnamesfig, odxv, odyv, odimxu, odimyu, \ |
---|
[192] | 388 | dimnamesv, colbarn, shading_nx, varunits, figtitle, figkind, revals, timeaxis, \ |
---|
| 389 | timepos, timelabels, closeval) |
---|
| 390 | |
---|
| 391 | return |
---|
| 392 | |
---|
| 393 | def draw_2D_shad_cont(ncfile, values, varn): |
---|
| 394 | """ plotting two fields, one with shading and the other with contour lines |
---|
| 395 | draw_2D_shad_cont(ncfile, values, varn) |
---|
| 396 | ncfile= [ncfilevars],[ncfilevarc] files to use (one value, same file) |
---|
| 397 | values=[vnamefs]:[dimvals]:[dimvalc]:[dimxvn]:[dimyvn]:[colorbar]:[ckind]:[clabfmt]:[sminv],[smaxv]:[sminc],[smaxv],[Nlev]:[figt]:[kindfig]:[reverse]:[mapv] |
---|
| 398 | [vnamefs],[vnamefc]: Name in the figure of the shaded and the contour variables |
---|
| 399 | [dimvals/c]: list of [dimname]|[value] telling at which dimension of the |
---|
| 400 | variable a given value is required (no dimension name, all the length) |
---|
[381] | 401 | [dimx/yvn]: names of the variables with the values of the dimensions for the plot |
---|
[192] | 402 | [colorbar]: name of the color bar |
---|
| 403 | [ckind]: kind of contours |
---|
| 404 | 'cmap': as it gets from colorbar |
---|
| 405 | 'fixc,[colname]': fixed color [colname], all stright lines |
---|
| 406 | 'fixsigc,[colname]': fixed color [colname], >0 stright, <0 dashed line |
---|
| 407 | [clabfmt]: format of the labels in the contour (None, also possible) |
---|
| 408 | [smin/axv]: minimum and maximum value for the shading |
---|
| 409 | [sminc]:[smaxv]:[Nlev]: minimum, maximum and number of values for the contour |
---|
| 410 | [figt]: title of the figure ('|' for spaces) |
---|
| 411 | [kindfig]: kind of figure |
---|
| 412 | [reverse]: does the values be transposed? 'True/False', |
---|
| 413 | [mapv]: map characteristics: [proj],[res] |
---|
| 414 | see full documentation: http://matplotlib.org/basemap/ |
---|
| 415 | [proj]: projection |
---|
| 416 | * 'cyl', cilindric |
---|
| 417 | * 'lcc', lamvbert conformal |
---|
| 418 | [res]: resolution: |
---|
| 419 | * 'c', crude |
---|
| 420 | * 'l', low |
---|
| 421 | * 'i', intermediate |
---|
| 422 | * 'h', high |
---|
| 423 | * 'f', full |
---|
| 424 | valules= 'rh,ta:z|-1,x|-1:z|-1,x|-1:lat:pressure:BuPu:fixsigc,black:%d:0.,100.:195.,305.,7:Meridonal|average|of|rh|&|ta:pdf:flip@y:None' |
---|
| 425 | varn= [varsn],[varcn] name of the variable to plot with shading variable with contour |
---|
| 426 | """ |
---|
| 427 | |
---|
| 428 | fname = 'draw_2D_shad_cont' |
---|
| 429 | if values == 'h': |
---|
| 430 | print fname + '_____________________________________________________________' |
---|
| 431 | print draw_2D_shad_cont.__doc__ |
---|
| 432 | quit() |
---|
| 433 | |
---|
[478] | 434 | expectargs = '[vnamefs]:[dimvals]:[dimvalc]:[dimxvn]:[dimyvn]:[colorbar]:' |
---|
| 435 | expectargs = expectargs + '[ckind]:[clabfmt]:[sminv],[smaxv]:[sminc],[smaxv],' |
---|
| 436 | expectargs = expectargs + '[Nlev]:[figt]:[kindfig]:[reverse]:[mapv]' |
---|
| 437 | |
---|
[690] | 438 | drw.check_arguments(fname,values,expectargs,':') |
---|
[478] | 439 | |
---|
[192] | 440 | vnamesfig = values.split(':')[0].split(',') |
---|
| 441 | dimvals= values.split(':')[1].replace('|',':') |
---|
| 442 | dimvalc= values.split(':')[2].replace('|',':') |
---|
| 443 | vdimxn = values.split(':')[3] |
---|
| 444 | vdimyn = values.split(':')[4] |
---|
| 445 | colbarn = values.split(':')[5] |
---|
| 446 | countkind = values.split(':')[6] |
---|
| 447 | countlabelfmt = values.split(':')[7] |
---|
| 448 | shadminmax = values.split(':')[8] |
---|
| 449 | contlevels = values.split(':')[9] |
---|
| 450 | figtitle = values.split(':')[10].replace('|',' ') |
---|
| 451 | figkind = values.split(':')[11] |
---|
| 452 | revals = values.split(':')[12] |
---|
| 453 | mapvalue = values.split(':')[13] |
---|
| 454 | |
---|
| 455 | if2filenames = ncfile.find(',') |
---|
| 456 | |
---|
| 457 | if if2filenames != -1: |
---|
| 458 | ncfiles = ncfile.split(',')[0] |
---|
| 459 | ncfilec = ncfile.split(',')[1] |
---|
| 460 | else: |
---|
| 461 | ncfiles = ncfile |
---|
| 462 | ncfilec = ncfile |
---|
| 463 | |
---|
| 464 | if not os.path.isfile(ncfiles): |
---|
| 465 | print errormsg |
---|
| 466 | print ' ' + fname + ': shading file "' + ncfiles + '" does not exist !!' |
---|
| 467 | quit(-1) |
---|
| 468 | |
---|
| 469 | if not os.path.isfile(ncfilec): |
---|
| 470 | print errormsg |
---|
| 471 | print ' ' + fname + ': contour file "' + ncfilec + '" does not exist !!' |
---|
| 472 | quit(-1) |
---|
| 473 | |
---|
| 474 | objsf = NetCDFFile(ncfiles, 'r') |
---|
| 475 | objcf = NetCDFFile(ncfilec, 'r') |
---|
| 476 | |
---|
| 477 | varns = varn.split(',')[0] |
---|
| 478 | varnc = varn.split(',')[1] |
---|
| 479 | |
---|
| 480 | if not objsf.variables.has_key(varns): |
---|
| 481 | print errormsg |
---|
| 482 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 483 | '" does not have variable "' + varns + '" !!' |
---|
| 484 | quit(-1) |
---|
| 485 | |
---|
| 486 | if not objcf.variables.has_key(varnc): |
---|
| 487 | print errormsg |
---|
| 488 | print ' ' + fname + ': contour file "' + ncfilec + \ |
---|
| 489 | '" does not have variable "' + varnc + '" !!' |
---|
| 490 | quit(-1) |
---|
| 491 | |
---|
| 492 | # Variables' values |
---|
| 493 | objvars = objsf.variables[varns] |
---|
| 494 | objvarc = objcf.variables[varnc] |
---|
| 495 | |
---|
| 496 | if len(objvars.shape) != len(objvarc.shape): |
---|
| 497 | print errormsg |
---|
| 498 | print ' ' + fname + ': shading variable "' + varns + '" has a shape: ', \ |
---|
| 499 | objvars.shape, 'different than contour variable "' + varnc + '": ', \ |
---|
| 500 | objvarc.shape,' !!!' |
---|
| 501 | quit(-1) |
---|
| 502 | |
---|
| 503 | for idim in range(len(objvars.shape)): |
---|
| 504 | if objvars.shape[idim] != objvarc.shape[idim]: |
---|
| 505 | print errormsg |
---|
| 506 | print ' ' + fname + ': shading variable "' + varns + '" has a shape: ', \ |
---|
| 507 | objvars.shape, 'different than contour variable "' + varnc + '": ', \ |
---|
| 508 | objvarc.shape,' !!!' |
---|
| 509 | quit(-1) |
---|
| 510 | |
---|
| 511 | valshad, dimsshad = drw.slice_variable(objvars, dimvals.replace(',','|')) |
---|
| 512 | valcont, dimscont = drw.slice_variable(objvarc, dimvalc.replace(',','|')) |
---|
| 513 | |
---|
| 514 | # Dimensions names |
---|
| 515 | ## print fname + ' obj dimnames: ', objvars.dimensions, dimvals, len(dimvals.split(',')) |
---|
| 516 | ## dimnamesv = [] |
---|
| 517 | ## for idd in range(len(objvars.dimensions)): |
---|
| 518 | ## cutdim = False |
---|
| 519 | ## for idc in range(len(dimvals.split(','))): |
---|
| 520 | ## dimcutn = dimvals.split(',')[idc].split(':')[0] |
---|
| 521 | ## print objvars.dimensions[idd], dimcutn |
---|
| 522 | ## if objvars.dimensions[idd] == dimcutn: |
---|
| 523 | ## cutdim = True |
---|
| 524 | ## break |
---|
| 525 | ## if not cutdim: dimnamesv.append(objvars.dimensions[idd]) |
---|
| 526 | dimnamesv = [vdimyn, vdimxn] |
---|
| 527 | |
---|
| 528 | varunits = [] |
---|
| 529 | varunits.append(objvars.getncattr('units')) |
---|
| 530 | varunits.append(objvarc.getncattr('units')) |
---|
| 531 | |
---|
| 532 | if not objsf.variables.has_key(vdimxn): |
---|
| 533 | print errormsg |
---|
| 534 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 535 | '" does not have dimension variable "' + vdimxn + '" !!' |
---|
| 536 | quit(-1) |
---|
| 537 | if not objsf.variables.has_key(vdimyn): |
---|
| 538 | print errormsg |
---|
| 539 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 540 | '" does not have dimensino variable "' + vdimyn + '" !!' |
---|
| 541 | quit(-1) |
---|
| 542 | |
---|
| 543 | objdimx = objsf.variables[vdimxn] |
---|
| 544 | objdimy = objsf.variables[vdimyn] |
---|
| 545 | odimxu = objdimx.getncattr('units') |
---|
| 546 | odimyu = objdimy.getncattr('units') |
---|
| 547 | |
---|
[396] | 548 | # Getting only that dimensions with coincident names |
---|
[489] | 549 | odimxv, odimyv = drw.dxdy_lonlatDIMS(objdimx[:], objdimy[:], objdimx.dimensions, \ |
---|
| 550 | objdimy.dimensions, dimvals.replace(':','|').split(',')) |
---|
[192] | 551 | |
---|
[489] | 552 | # dimnvx = objdimx.dimensions |
---|
| 553 | # cutslice = [] |
---|
| 554 | # for idimn in objdimx.dimensions: |
---|
| 555 | # found = False |
---|
| 556 | # for dimsn in dimsshad: |
---|
| 557 | # if idimn == dimsn: |
---|
| 558 | # cutslice.append(slice(0,len(objsf.dimensions[idimn]))) |
---|
| 559 | # found = True |
---|
| 560 | # if not found: cutslice.append(0) |
---|
| 561 | # |
---|
| 562 | # odimxv = objdimx[tuple(cutslice)] |
---|
| 563 | # |
---|
| 564 | # dimnvy = objdimy.dimensions |
---|
| 565 | # cutslice = [] |
---|
| 566 | # for idimn in objdimy.dimensions: |
---|
| 567 | # found = False |
---|
| 568 | # for dimsn in dimsshad: |
---|
| 569 | # if idimn == dimsn: |
---|
| 570 | # cutslice.append(slice(0,len(objsf.dimensions[idimn]))) |
---|
| 571 | # found = True |
---|
| 572 | # if not found: cutslice.append(0) |
---|
| 573 | # |
---|
| 574 | # odimyv = objdimy[tuple(cutslice)] |
---|
[192] | 575 | |
---|
[381] | 576 | # if len(objdimx.shape) <= 2: |
---|
| 577 | # odimxv = objdimx[:] |
---|
| 578 | # odimyv = objdimy[:] |
---|
| 579 | # elif len(objdimx.shape) == 3: |
---|
| 580 | # odimxv = objdimx[0,:] |
---|
| 581 | # odimyv = objdimy[0,:] |
---|
| 582 | # else: |
---|
| 583 | # print errormsg |
---|
| 584 | # print ' ' + fname + ': shape of dimension variable:', objdimx.shape, \ |
---|
| 585 | # ' not ready!!' |
---|
| 586 | # quit(-1) |
---|
| 587 | |
---|
[192] | 588 | if countlabelfmt == 'None': |
---|
| 589 | countlfmt = None |
---|
| 590 | else: |
---|
| 591 | countlfmt = countlabelfmt |
---|
| 592 | |
---|
| 593 | shading_nx = np.zeros((2), dtype=np.float) |
---|
| 594 | shading_nx[0] = np.float(shadminmax.split(',')[0]) |
---|
| 595 | shading_nx[1] = np.float(shadminmax.split(',')[1]) |
---|
| 596 | |
---|
| 597 | clevmin = np.float(contlevels.split(',')[0]) |
---|
| 598 | clevmax = np.float(contlevels.split(',')[1]) |
---|
| 599 | Nclevels = int(contlevels.split(',')[2]) |
---|
| 600 | |
---|
| 601 | levels_cont = drw.pretty_int(clevmin, clevmax, Nclevels) |
---|
| 602 | |
---|
| 603 | if len(levels_cont) <= 1: |
---|
| 604 | print warnmsg |
---|
| 605 | print ' ' + fname + ': wrong contour levels:', levels_cont,' !!' |
---|
| 606 | del(levels_cont) |
---|
| 607 | levels_cont = np.zeros((Nclevels), dtype=np.float) |
---|
| 608 | levels_cont = np.arange(7)*(clevmax - clevmin)/(Nclevels-1) |
---|
| 609 | print ' generating default ones: ',levels_cont |
---|
| 610 | |
---|
| 611 | if mapvalue == 'None': mapvalue = None |
---|
| 612 | |
---|
| 613 | drw.plot_2D_shadow_contour(valshad, valcont, vnamesfig, odimxv, odimyv, odimxu, \ |
---|
| 614 | odimyu, dimnamesv, colbarn, countkind, countlfmt, shading_nx, levels_cont, \ |
---|
| 615 | varunits, figtitle, figkind, revals, mapvalue) |
---|
| 616 | |
---|
| 617 | return |
---|
| 618 | |
---|
| 619 | def draw_2D_shad_cont_time(ncfile, values, varn): |
---|
| 620 | """ plotting two fields, one with shading and the other with contour lines being |
---|
| 621 | one of the dimensions of time characteristics |
---|
| 622 | draw_2D_shad_cont(ncfile, values, varn) |
---|
| 623 | ncfile= [ncfilevars],[ncfilevarc] files to use (one value, same file) |
---|
| 624 | values=[vnamefs]:[dimvals]:[dimvalc]:[dimxvn]:[dimyvn]:[colorbar]:[ckind]:[clabfmt]:[sminv],[smaxv]:[sminc],[smaxv],[Nlev]:[figt]:[kindfig]:[reverse]:[timevals]:[mapv] |
---|
| 625 | [vnamefs],[vnamefc]: Name in the figure of the shaded and the contour variables |
---|
| 626 | [dimvals/c]: list of [dimname]|[value] telling at which dimension of the |
---|
| 627 | variable a given value is required (no dimension name, all the length) |
---|
| 628 | [dimx/yvn]: ',' list with the name of the variables with the values of the dimensions |
---|
| 629 | [colorbar]: name of the color bar |
---|
| 630 | [ckind]: kind of contours |
---|
| 631 | 'cmap': as it gets from colorbar |
---|
| 632 | 'fixc,[colname]': fixed color [colname], all stright lines |
---|
| 633 | 'fixsigc,[colname]': fixed color [colname], >0 stright, <0 dashed line |
---|
| 634 | [clabfmt]: format of the labels in the contour (None, also possible) |
---|
| 635 | [smin/axv]: minimum and maximum value for the shading |
---|
| 636 | [sminc]:[smaxv]:[Nlev]: minimum, maximum and number of values for the contour |
---|
| 637 | [figt]: title of the figure ('|' for spaces) |
---|
| 638 | [kindfig]: kind of figure |
---|
| 639 | [reverse]: modification to the dimensions: |
---|
| 640 | 'transposed': transpose matrices |
---|
| 641 | 'flip',[x/y]: flip only the dimension [x] or [y] |
---|
| 642 | [timevals]: [timen]|[units]|[kind]|[tfmt]|[label] time labels characteristics |
---|
| 643 | [timen]; name of the time variable |
---|
| 644 | [units]; units string according to CF conventions ([tunits] since |
---|
| 645 | [YYYY]-[MM]-[DD] [[HH]:[MI]:[SS]], '!' for spaces) |
---|
| 646 | [kind]; kind of output |
---|
| 647 | 'Nval': according to a given number of values as 'Nval',[Nval] |
---|
| 648 | 'exct': according to an exact time unit as 'exct',[tunit]; |
---|
| 649 | tunit= [Nunits],[tu]; [tu]= 'c': centuries, 'y': year, 'm': month, |
---|
| 650 | 'w': week, 'd': day, 'h': hour, 'i': minute, 's': second, |
---|
| 651 | 'l': milisecond |
---|
| 652 | [tfmt]; desired format |
---|
| 653 | [label]; label at the graph ('!' for spaces) |
---|
| 654 | [mapv]: map characteristics: [proj],[res] |
---|
| 655 | see full documentation: http://matplotlib.org/basemap/ |
---|
| 656 | [proj]: projection |
---|
| 657 | * 'cyl', cilindric |
---|
| 658 | * 'lcc', lamvbert conformal |
---|
| 659 | [res]: resolution: |
---|
| 660 | * 'c', crude |
---|
| 661 | * 'l', low |
---|
| 662 | * 'i', intermediate |
---|
| 663 | * 'h', high |
---|
| 664 | * 'f', full |
---|
| 665 | valules= 'rh,ta:z|-1,x|-1:z|-1,x|-1:lat:pressure:BuPu:fixsigc,black:%d:0.,100.:195.,305.,7:Meridonal|average|of|rh|&|ta:pdf:flip@y:time!hours!since!1949/12/01|exct,5d|%d|date!([DD]):None' |
---|
| 666 | varn= [varsn],[varcn] name of the variable to plot with shading variable with contour |
---|
| 667 | """ |
---|
| 668 | |
---|
| 669 | fname = 'draw_2D_shad_cont_time' |
---|
| 670 | if values == 'h': |
---|
| 671 | print fname + '_____________________________________________________________' |
---|
| 672 | print draw_2D_shad_cont_time.__doc__ |
---|
| 673 | quit() |
---|
| 674 | |
---|
[690] | 675 | expectargs = '[vnamefs]:[dimvals]:[dimvalc]:[dimxvn]:[dimyvn]:[colorbar]:' + \ |
---|
| 676 | '[ckind]:[clabfmt]:[sminv],[smaxv]:[sminc],[smaxv],[Nlev]:[figt]:[kindfig]:' + \ |
---|
| 677 | '[reverse]:[timevals]:[mapv]' |
---|
[192] | 678 | |
---|
[690] | 679 | drw.check_arguments(fname,values,expectargs,':') |
---|
[192] | 680 | |
---|
| 681 | vnamesfig = values.split(':')[0].split(',') |
---|
| 682 | dimvals= values.split(':')[1].replace('|',':') |
---|
| 683 | dimvalc= values.split(':')[2].replace('|',':') |
---|
| 684 | vdimxn = values.split(':')[3] |
---|
| 685 | vdimyn = values.split(':')[4] |
---|
| 686 | colbarn = values.split(':')[5] |
---|
| 687 | countkind = values.split(':')[6] |
---|
| 688 | countlabelfmt = values.split(':')[7] |
---|
| 689 | shadminmax = values.split(':')[8] |
---|
| 690 | contlevels = values.split(':')[9] |
---|
| 691 | figtitle = values.split(':')[10].replace('|',' ') |
---|
| 692 | figkind = values.split(':')[11] |
---|
| 693 | revals = values.split(':')[12] |
---|
| 694 | timevals = values.split(':')[13] |
---|
| 695 | mapvalue = values.split(':')[14] |
---|
| 696 | |
---|
| 697 | if2filenames = ncfile.find(',') |
---|
| 698 | |
---|
| 699 | if if2filenames != -1: |
---|
| 700 | ncfiles = ncfile.split(',')[0] |
---|
| 701 | ncfilec = ncfile.split(',')[1] |
---|
| 702 | else: |
---|
| 703 | ncfiles = ncfile |
---|
| 704 | ncfilec = ncfile |
---|
| 705 | |
---|
| 706 | if not os.path.isfile(ncfiles): |
---|
| 707 | print errormsg |
---|
| 708 | print ' ' + fname + ': shading file "' + ncfiles + '" does not exist !!' |
---|
| 709 | quit(-1) |
---|
| 710 | |
---|
| 711 | if not os.path.isfile(ncfilec): |
---|
| 712 | print errormsg |
---|
| 713 | print ' ' + fname + ': contour file "' + ncfilec + '" does not exist !!' |
---|
| 714 | quit(-1) |
---|
| 715 | |
---|
| 716 | objsf = NetCDFFile(ncfiles, 'r') |
---|
| 717 | objcf = NetCDFFile(ncfilec, 'r') |
---|
| 718 | |
---|
| 719 | varns = varn.split(',')[0] |
---|
| 720 | varnc = varn.split(',')[1] |
---|
| 721 | |
---|
| 722 | if not objsf.variables.has_key(varns): |
---|
| 723 | print errormsg |
---|
| 724 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 725 | '" does not have variable "' + varns + '" !!' |
---|
| 726 | quit(-1) |
---|
| 727 | |
---|
| 728 | if not objcf.variables.has_key(varnc): |
---|
| 729 | print errormsg |
---|
| 730 | print ' ' + fname + ': contour file "' + ncfilec + \ |
---|
| 731 | '" does not have variable "' + varnc + '" !!' |
---|
| 732 | quit(-1) |
---|
| 733 | |
---|
| 734 | # Variables' values |
---|
| 735 | objvars = objsf.variables[varns] |
---|
| 736 | objvarc = objcf.variables[varnc] |
---|
| 737 | |
---|
| 738 | if len(objvars.shape) != len(objvarc.shape): |
---|
| 739 | print errormsg |
---|
| 740 | print ' ' + fname + ': shading variable "' + varns + '" has a shape: ', \ |
---|
| 741 | objvars.shape, 'different than contour variable "' + varnc + '": ', \ |
---|
| 742 | objvarc.shape,' !!!' |
---|
| 743 | quit(-1) |
---|
| 744 | |
---|
| 745 | for idim in range(len(objvars.shape)): |
---|
| 746 | if objvars.shape[idim] != objvarc.shape[idim]: |
---|
| 747 | print errormsg |
---|
| 748 | print ' ' + fname + ': shading variable "' + varns + '" has a shape: ', \ |
---|
| 749 | objvars.shape, 'different than contour variable "' + varnc + '": ', \ |
---|
| 750 | objvarc.shape,' !!!' |
---|
| 751 | quit(-1) |
---|
| 752 | |
---|
| 753 | valshad, dimsshad = drw.slice_variable(objvars, dimvals.replace(',','|')) |
---|
| 754 | valcont, dimscont = drw.slice_variable(objvarc, dimvalc.replace(',','|')) |
---|
| 755 | |
---|
| 756 | # Dimensions names |
---|
| 757 | ## print fname + ' obj dimnames: ', objvars.dimensions, dimvals, len(dimvals.split(',')) |
---|
| 758 | ## dimnamesv = [] |
---|
| 759 | ## for idd in range(len(objvars.dimensions)): |
---|
| 760 | ## cutdim = False |
---|
| 761 | ## for idc in range(len(dimvals.split(','))): |
---|
| 762 | ## dimcutn = dimvals.split(',')[idc].split(':')[0] |
---|
| 763 | ## print objvars.dimensions[idd], dimcutn |
---|
| 764 | ## if objvars.dimensions[idd] == dimcutn: |
---|
| 765 | ## cutdim = True |
---|
| 766 | ## break |
---|
| 767 | ## if not cutdim: dimnamesv.append(objvars.dimensions[idd]) |
---|
| 768 | dimnamesv = [vdimyn, vdimxn] |
---|
| 769 | |
---|
| 770 | varunits = [] |
---|
| 771 | varunits.append(objvars.getncattr('units')) |
---|
| 772 | varunits.append(objvarc.getncattr('units')) |
---|
| 773 | |
---|
| 774 | if not objsf.variables.has_key(vdimxn): |
---|
| 775 | print errormsg |
---|
| 776 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 777 | '" does not have dimension variable "' + vdimxn + '" !!' |
---|
| 778 | quit(-1) |
---|
| 779 | if not objsf.variables.has_key(vdimyn): |
---|
| 780 | print errormsg |
---|
| 781 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 782 | '" does not have dimension variable "' + vdimyn + '" !!' |
---|
| 783 | quit(-1) |
---|
| 784 | |
---|
| 785 | timename = timevals.split('|')[0] |
---|
| 786 | timeunit = timevals.split('|')[1].replace('!',' ') |
---|
| 787 | timekind = timevals.split('|')[2] |
---|
| 788 | timefmt = timevals.split('|')[3] |
---|
| 789 | timelabel = timevals.split('|')[4].replace('!',' ') |
---|
| 790 | |
---|
| 791 | if vdimxn == timename: |
---|
| 792 | timevals = objsf.variables[vdimxn][:] |
---|
[396] | 793 | timedims = objsf.variables[vdimxn].dimensions |
---|
[192] | 794 | dimt = 'x' |
---|
| 795 | ovalaxis = objsf.variables[vdimyn] |
---|
| 796 | ovalu = ovalaxis.getncattr('units') |
---|
| 797 | elif vdimyn == timename: |
---|
| 798 | timevals = objsf.variables[vdimyn][:] |
---|
[396] | 799 | timedims = objsf.variables[vdimyn].dimensions |
---|
[192] | 800 | dimt = 'y' |
---|
| 801 | ovalaxis = objsf.variables[vdimxn] |
---|
| 802 | ovalu = ovalaxis.getncattr('units') |
---|
| 803 | else: |
---|
| 804 | print errormsg |
---|
| 805 | print ' ' + fname + ": time variable '" + timename + "' not found!!" |
---|
| 806 | quit(-1) |
---|
| 807 | |
---|
| 808 | timepos, timelabels = drw.CFtimes_plot(timevals, timeunit, timekind, timefmt) |
---|
| 809 | |
---|
[396] | 810 | # Getting only that dimensions with coincident names |
---|
| 811 | dimnvx = ovalaxis.dimensions |
---|
[192] | 812 | |
---|
[396] | 813 | cutslice = [] |
---|
| 814 | for idimn in dimsshad: |
---|
| 815 | found = False |
---|
| 816 | for dimsn in dimnvx: |
---|
| 817 | if idimn == dimsn: |
---|
| 818 | cutslice.append(slice(0,len(objsf.dimensions[idimn]))) |
---|
| 819 | found = True |
---|
| 820 | if not found: cutslice.append(0) |
---|
[192] | 821 | |
---|
[396] | 822 | ovalaxisv = ovalaxis[tuple(cutslice)] |
---|
| 823 | |
---|
| 824 | ## if len(ovalaxis.shape) <= 2: |
---|
| 825 | ## ovalaxisv = ovalaxis[:] |
---|
| 826 | |
---|
| 827 | ## elif len(ovalaxis.shape) == 3: |
---|
| 828 | ## ovalaxisv = ovalaxis[0,:] |
---|
| 829 | ## else: |
---|
| 830 | ## print errormsg |
---|
| 831 | ## print ' ' + fname + ': shape of dimension variable:', ovalaxis.shape, \ |
---|
| 832 | ## ' not ready!!' |
---|
| 833 | ## quit(-1) |
---|
| 834 | |
---|
[192] | 835 | if countlabelfmt == 'None': |
---|
| 836 | countlfmt = None |
---|
| 837 | else: |
---|
| 838 | countlfmt = countlabelfmt |
---|
| 839 | |
---|
| 840 | shading_nx = np.zeros((2), dtype=np.float) |
---|
| 841 | shading_nx[0] = np.float(shadminmax.split(',')[0]) |
---|
| 842 | shading_nx[1] = np.float(shadminmax.split(',')[1]) |
---|
| 843 | |
---|
| 844 | clevmin = np.float(contlevels.split(',')[0]) |
---|
| 845 | clevmax = np.float(contlevels.split(',')[1]) |
---|
| 846 | Nclevels = int(contlevels.split(',')[2]) |
---|
| 847 | |
---|
| 848 | levels_cont = drw.pretty_int(clevmin, clevmax, Nclevels) |
---|
| 849 | |
---|
| 850 | if len(levels_cont) <= 1: |
---|
| 851 | print warnmsg |
---|
| 852 | print ' ' + fname + ': wrong contour levels:', levels_cont,' !!' |
---|
| 853 | del(levels_cont) |
---|
| 854 | levels_cont = np.zeros((Nclevels), dtype=np.float) |
---|
| 855 | levels_cont = np.arange(7)*(clevmax - clevmin)/(Nclevels-1) |
---|
| 856 | print ' generating default ones: ',levels_cont |
---|
| 857 | |
---|
| 858 | if mapvalue == 'None': mapvalue = None |
---|
| 859 | |
---|
| 860 | drw.plot_2D_shadow_contour_time(valshad, valcont, vnamesfig, ovalaxisv, \ |
---|
| 861 | timevals, timepos, timelabels, ovalu, timelabel, dimt, dimnamesv, colbarn, \ |
---|
| 862 | countkind, countlfmt, shading_nx, levels_cont, varunits, figtitle, figkind, \ |
---|
| 863 | revals, mapvalue) |
---|
| 864 | |
---|
| 865 | return |
---|
| 866 | |
---|
| 867 | def draw_2D_shad_line(ncfile, values, varn): |
---|
| 868 | """ plotting a fields with shading and another with line |
---|
| 869 | draw_2D_shad_line(ncfile, values, varn) |
---|
| 870 | ncfile= [ncfiles],[ncfilel] file to use for the shading and for the line |
---|
| 871 | values=[vnamefs],[vnamefl]:[dimvals]:[dimxvn]:[dimyvn]:[colorbar],[colline]:[sminv],[smaxv]:[figt]: |
---|
| 872 | [kindfig]:[reverse]:[mapv]:[close] |
---|
| 873 | [vnamefs]: Name in the figure of the variable to be shaded |
---|
| 874 | [vnamefl]: Name in the figure of the variable to be lined |
---|
| 875 | [dimvals]: ',' list of [dimname]|[value] telling at which dimension of the |
---|
| 876 | variable a given value is required (-1, all the length) |
---|
| 877 | [dimx/yvn]: name of the variables with the values of the final dimensions (x,y) |
---|
| 878 | [colorbar]: name of the color bar |
---|
| 879 | [colline]: name of the color for the line |
---|
| 880 | [smin/axv]: minimum and maximum value for the shading |
---|
| 881 | [figt]: title of the figure ('|' for spaces) |
---|
| 882 | [kindfig]: kind of figure |
---|
| 883 | [reverse]: Transformation of the values |
---|
| 884 | * 'transpose': reverse the axes (x-->y, y-->x) |
---|
| 885 | * 'flip'@[x/y]: flip the axis x or y |
---|
| 886 | [mapv]: map characteristics: [proj],[res] |
---|
| 887 | see full documentation: http://matplotlib.org/basemap/ |
---|
| 888 | [proj]: projection |
---|
| 889 | * 'cyl', cilindric |
---|
| 890 | * 'lcc', lamvbert conformal |
---|
| 891 | [res]: resolution: |
---|
| 892 | * 'c', crude |
---|
| 893 | * 'l', low |
---|
| 894 | * 'i', intermediate |
---|
| 895 | * 'h', high |
---|
| 896 | * 'f', full |
---|
| 897 | valules= 'rh:z|-1,x|-1:z|-1,x|-1:lat:pressure:BuPu:0.,100.:rh:pdf:flip@y:None' |
---|
| 898 | varn= [varsn],[varnl] name of the variable to plot with shading and with line |
---|
| 899 | """ |
---|
| 900 | |
---|
| 901 | fname = 'draw_2D_shad_line' |
---|
| 902 | if values == 'h': |
---|
| 903 | print fname + '_____________________________________________________________' |
---|
| 904 | print draw_2D_shad_line.__doc__ |
---|
| 905 | quit() |
---|
| 906 | |
---|
[690] | 907 | farguments = '[vnamefs],[vnamefl]:[dimvals]:[dimxvn]:[dimyvn]:' + \ |
---|
| 908 | '[colorbar],[colline]:[sminv],[smaxv]:[figt]:[kindfig]:[reverse]:' + \ |
---|
| 909 | '[mapv]:[close]' |
---|
| 910 | drw.check_arguments(fname,values,farguments,':') |
---|
[192] | 911 | |
---|
| 912 | vnamesfig = values.split(':')[0].split(',')[0] |
---|
| 913 | dimvals= values.split(':')[1].replace('|',':') |
---|
| 914 | vdimxn = values.split(':')[2] |
---|
| 915 | vdimyn = values.split(':')[3] |
---|
| 916 | colbarn = values.split(':')[4].split(',')[0] |
---|
| 917 | shadminmax = values.split(':')[5] |
---|
| 918 | figtitle = values.split(':')[6].replace('|',' ') |
---|
| 919 | figkind = values.split(':')[7] |
---|
| 920 | revals = values.split(':')[8] |
---|
| 921 | mapvalue = values.split(':')[9] |
---|
| 922 | # varn = values.split(':')[10] |
---|
| 923 | |
---|
| 924 | ncfiles = ncfile.split(',')[0] |
---|
| 925 | |
---|
| 926 | if not os.path.isfile(ncfiles): |
---|
| 927 | print errormsg |
---|
| 928 | print ' ' + fname + ': shading file "' + ncfiles + '" does not exist !!' |
---|
| 929 | quit(-1) |
---|
| 930 | |
---|
| 931 | objsf = NetCDFFile(ncfiles, 'r') |
---|
| 932 | |
---|
| 933 | varns = varn.split(',')[0] |
---|
| 934 | |
---|
| 935 | if not objsf.variables.has_key(varns): |
---|
| 936 | print errormsg |
---|
| 937 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 938 | '" does not have variable "' + varns + '" !!' |
---|
| 939 | quit(-1) |
---|
| 940 | |
---|
| 941 | # Variables' values |
---|
| 942 | objvars = objsf.variables[varns] |
---|
| 943 | |
---|
| 944 | valshad, dimsshad = drw.slice_variable(objvars, dimvals.replace(',','|')) |
---|
| 945 | |
---|
| 946 | # Dimensions names |
---|
| 947 | ## print fname + ' obj dimnames: ', objvars.dimensions, dimvals, len(dimvals.split(',')) |
---|
| 948 | ## dimnamesv = [] |
---|
| 949 | ## for idd in range(len(objvars.dimensions)): |
---|
| 950 | ## cutdim = False |
---|
| 951 | ## for idc in range(len(dimvals.split(','))): |
---|
| 952 | ## dimcutn = dimvals.split(',')[idc].split(':')[0] |
---|
| 953 | ## print objvars.dimensions[idd], dimcutn |
---|
| 954 | ## if objvars.dimensions[idd] == dimcutn: |
---|
| 955 | ## cutdim = True |
---|
| 956 | ## break |
---|
| 957 | ## if not cutdim: dimnamesv.append(objvars.dimensions[idd]) |
---|
| 958 | dimnamesv = [vdimyn, vdimxn] |
---|
| 959 | |
---|
| 960 | varunits = objvars.getncattr('units') |
---|
| 961 | |
---|
| 962 | if not objsf.variables.has_key(vdimxn): |
---|
| 963 | print errormsg |
---|
| 964 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 965 | '" does not have dimension variable "' + vdimxn + '" !!' |
---|
| 966 | quit(-1) |
---|
| 967 | if not objsf.variables.has_key(vdimyn): |
---|
| 968 | print errormsg |
---|
| 969 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 970 | '" does not have dimensino variable "' + vdimyn + '" !!' |
---|
| 971 | quit(-1) |
---|
| 972 | |
---|
| 973 | objdimx = objsf.variables[vdimxn] |
---|
| 974 | objdimy = objsf.variables[vdimyn] |
---|
| 975 | odimxu = objdimx.getncattr('units') |
---|
| 976 | odimyu = objdimy.getncattr('units') |
---|
| 977 | |
---|
| 978 | if len(objdimx.shape) <= 2: |
---|
| 979 | # odimxv = objdimx[valshad.shape] |
---|
| 980 | # odimyv = objdimy[valshad.shape] |
---|
| 981 | odimxv = objdimx[:] |
---|
| 982 | odimyv = objdimy[:] |
---|
| 983 | |
---|
| 984 | elif len(objdimx.shape) == 3: |
---|
| 985 | # dimcut = [0, slice(0,valshad.shape[0]), slice(0,valshad.shape[1])] |
---|
| 986 | # odimxv = objdimx[tuple(dimcut)] |
---|
| 987 | # odimyv = objdimy[tuple(dimcut)] |
---|
| 988 | odimxv = objdimx[0,:] |
---|
| 989 | odimyv = objdimy[0,:] |
---|
| 990 | else: |
---|
| 991 | print errormsg |
---|
| 992 | print ' ' + fname + ': shape of dimension variable:', objdimx.shape, \ |
---|
| 993 | ' not ready!!' |
---|
| 994 | quit(-1) |
---|
| 995 | |
---|
| 996 | shading_nx = np.zeros((2), dtype=np.float) |
---|
| 997 | shading_nx[0] = np.float(shadminmax.split(',')[0]) |
---|
| 998 | shading_nx[1] = np.float(shadminmax.split(',')[1]) |
---|
| 999 | |
---|
| 1000 | if mapvalue == 'None': mapvalue = None |
---|
| 1001 | |
---|
| 1002 | # line plot |
---|
| 1003 | ## |
---|
| 1004 | ncfilel = ncfile.split(',')[1] |
---|
| 1005 | vnamelfig = values.split(':')[0].split(',')[1] |
---|
| 1006 | varnl = varn.split(',')[1] |
---|
| 1007 | colline = values.split(':')[4].split(',')[1] |
---|
| 1008 | |
---|
| 1009 | objlf = NetCDFFile(ncfilel,'r') |
---|
| 1010 | objlvar = objlf.variables[varnl] |
---|
| 1011 | |
---|
| 1012 | linevals = objlvar[:] |
---|
| 1013 | varlunits = objlvar.units |
---|
| 1014 | |
---|
| 1015 | drw.plot_2D_shadow_line(valshad, linevals, vnamesfig, vnamelfig, odimxv, odimyv, \ |
---|
| 1016 | odimxu, odimyu, dimnamesv, colbarn, colline, shading_nx, varunits, varlunits, \ |
---|
| 1017 | figtitle, figkind, revals, mapvalue, True) |
---|
| 1018 | |
---|
| 1019 | objsf.close() |
---|
| 1020 | objlf.close() |
---|
| 1021 | |
---|
| 1022 | return |
---|
| 1023 | |
---|
| 1024 | def draw_2D_shad_line_time(ncfile, values, varn): |
---|
| 1025 | """ plotting a fields with shading and a line with time values |
---|
| 1026 | draw_2D_shad_line(ncfile, values, varn) |
---|
| 1027 | ncfile= [ncfiles],[ncfilel] files to use to draw with shading and the line |
---|
| 1028 | values= [vnamefs],[vanemefl]:[dimvals]:[dimxvn]:[dimyvn]:[colorbar]:[sminv],[smaxv]:[figt]: |
---|
| 1029 | [kindfig]:[reverse]:[timevals]:[close] |
---|
| 1030 | [vnamefs]: Name in the figure of the variable to be shaded |
---|
| 1031 | [vnamefl]: Name in the figure of the variable to be lined |
---|
| 1032 | [dimvals]: ',' list of [dimname]|[value] telling at which dimension of the |
---|
| 1033 | variable a given value is required (-1, all the length) |
---|
| 1034 | [dimx/yvn]: name of the variables with the values of the final dimensions (x,y) |
---|
| 1035 | [colorbar]: name of the color bar |
---|
| 1036 | [smin/axv]: minimum and maximum value for the shading |
---|
| 1037 | [figt]: title of the figure ('|' for spaces) |
---|
| 1038 | [kindfig]: kind of figure |
---|
| 1039 | [reverse]: Transformation of the values |
---|
| 1040 | * 'transpose': reverse the axes (x-->y, y-->x) |
---|
| 1041 | * 'flip'@[x/y]: flip the axis x or y |
---|
| 1042 | [timevals]: [timen]|[units]|[kind]|[tfmt]|[label]|[timeaxis] time labels characteristics |
---|
| 1043 | [timen]; name of the time variable |
---|
| 1044 | [units]; units string according to CF conventions ([tunits] since |
---|
| 1045 | [YYYY]-[MM]-[DD] [[HH]:[MI]:[SS]], '!' for spaces) |
---|
| 1046 | [kind]; kind of output |
---|
| 1047 | 'Nval': according to a given number of values as 'Nval',[Nval] |
---|
| 1048 | 'exct': according to an exact time unit as 'exct',[tunit]; |
---|
| 1049 | tunit= [Nunits],[tu]; [tu]= 'c': centuries, 'y': year, 'm': month, |
---|
| 1050 | 'w': week, 'd': day, 'h': hour, 'i': minute, 's': second, |
---|
| 1051 | 'l': milisecond |
---|
| 1052 | [tfmt]; desired format |
---|
| 1053 | [label]; label at the graph ('!' for spaces) |
---|
| 1054 | [close]: should figure be closed (finished) |
---|
| 1055 | values='dtcon,prc:Time|-1,bottom_top|-1:presmean:time:seismic:-3.e-6,3.e-6:monthly|' |
---|
| 1056 | 'dtcon:pdf:transpose:time|hours!since!1949-12-01|exct,2,d|%d|date!([DD])|x:True |
---|
| 1057 | varn= [varsn].[varln] name of the variable to plot with shading and to plot with line |
---|
| 1058 | """ |
---|
| 1059 | fname = 'draw_2D_shad_line_time' |
---|
| 1060 | if values == 'h': |
---|
| 1061 | print fname + '_____________________________________________________________' |
---|
| 1062 | print draw_2D_shad__line_time.__doc__ |
---|
| 1063 | quit() |
---|
| 1064 | |
---|
[690] | 1065 | farguments = '[vnamefs],[vanemefl]:[dimvals]:[dimxvn]:[dimyvn]:' + \ |
---|
| 1066 | '[colorbar]:[sminv],[smaxv]:[figt]:[kindfig]:[reverse]:[timevals]:[close]' |
---|
| 1067 | drw.check_arguments(fname,values,farguments,':') |
---|
[192] | 1068 | |
---|
| 1069 | vnamesfig = values.split(':')[0].split(',')[0] |
---|
| 1070 | dimvals= values.split(':')[1].replace('|',':') |
---|
| 1071 | vdimxn = values.split(':')[2] |
---|
| 1072 | vdimyn = values.split(':')[3] |
---|
| 1073 | colbarn = values.split(':')[4] |
---|
| 1074 | shadminmax = values.split(':')[5] |
---|
| 1075 | figtitle = values.split(':')[6].replace('|',' ') |
---|
| 1076 | figkind = values.split(':')[7] |
---|
| 1077 | revals = values.split(':')[8] |
---|
| 1078 | timevals = values.split(':')[9] |
---|
| 1079 | close = values.split(':')[10] |
---|
| 1080 | |
---|
| 1081 | ncfiles = ncfile.split(',')[0] |
---|
| 1082 | |
---|
| 1083 | if not os.path.isfile(ncfiles): |
---|
| 1084 | print errormsg |
---|
| 1085 | print ' ' + fname + ': shading file "' + ncfiles + '" does not exist !!' |
---|
| 1086 | quit(-1) |
---|
| 1087 | |
---|
| 1088 | objsf = NetCDFFile(ncfiles, 'r') |
---|
| 1089 | |
---|
| 1090 | varns = varn.split(',')[0] |
---|
| 1091 | |
---|
| 1092 | if not objsf.variables.has_key(varns): |
---|
| 1093 | print errormsg |
---|
| 1094 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 1095 | '" does not have variable "' + varns + '" !!' |
---|
| 1096 | quit(-1) |
---|
| 1097 | |
---|
| 1098 | # Variables' values |
---|
| 1099 | objvars = objsf.variables[varns] |
---|
| 1100 | |
---|
| 1101 | valshad, dimsshad = drw.slice_variable(objvars, dimvals.replace(',','|')) |
---|
| 1102 | |
---|
| 1103 | dimnamesv = [vdimyn, vdimxn] |
---|
| 1104 | |
---|
| 1105 | varunits = objvars.getncattr('units') |
---|
| 1106 | |
---|
| 1107 | if not objsf.variables.has_key(vdimxn): |
---|
| 1108 | print errormsg |
---|
| 1109 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 1110 | '" does not have dimension variable "' + vdimxn + '" !!' |
---|
| 1111 | quit(-1) |
---|
| 1112 | if not objsf.variables.has_key(vdimyn): |
---|
| 1113 | print errormsg |
---|
| 1114 | print ' ' + fname + ': shading file "' + ncfiles + \ |
---|
| 1115 | '" does not have dimensino variable "' + vdimyn + '" !!' |
---|
| 1116 | quit(-1) |
---|
| 1117 | |
---|
| 1118 | objdimx = objsf.variables[vdimxn] |
---|
| 1119 | objdimy = objsf.variables[vdimyn] |
---|
| 1120 | odimxu = objdimx.getncattr('units') |
---|
| 1121 | odimyu = objdimy.getncattr('units') |
---|
| 1122 | |
---|
| 1123 | if len(objdimx.shape) <= 2: |
---|
| 1124 | odimxv = objdimx[:] |
---|
| 1125 | odimyv = objdimy[:] |
---|
| 1126 | |
---|
| 1127 | elif len(objdimx.shape) == 3: |
---|
| 1128 | odimxv = objdimx[0,:] |
---|
| 1129 | odimyv = objdimy[0,:] |
---|
| 1130 | else: |
---|
| 1131 | print errormsg |
---|
| 1132 | print ' ' + fname + ': shape of dimension variable:', objdimx.shape, \ |
---|
| 1133 | ' not ready!!' |
---|
| 1134 | quit(-1) |
---|
| 1135 | |
---|
| 1136 | timename = timevals.split('|')[0] |
---|
| 1137 | timeunit = timevals.split('|')[1].replace('!',' ') |
---|
| 1138 | timekind = timevals.split('|')[2] |
---|
| 1139 | timefmt = timevals.split('|')[3] |
---|
| 1140 | timelabel = timevals.split('|')[4].replace('!',' ') |
---|
| 1141 | |
---|
| 1142 | if vdimxn == timename: |
---|
| 1143 | odimxv = objsf.variables[vdimxn][:] |
---|
| 1144 | odimxu = timelabel |
---|
| 1145 | timeaxis = 'x' |
---|
| 1146 | odimyv = objsf.variables[vdimyn] |
---|
| 1147 | odimyu = odimyv.getncattr('units') |
---|
| 1148 | timepos, timelabels = drw.CFtimes_plot(odimxv, timeunit, timekind, timefmt) |
---|
| 1149 | elif vdimyn == timename: |
---|
| 1150 | odimyv = objsf.variables[vdimyn][:] |
---|
| 1151 | odimyu = timelabel |
---|
| 1152 | timeaxis = 'y' |
---|
| 1153 | odimxv = objsf.variables[vdimxn] |
---|
| 1154 | odimxu = odimxv.getncattr('units') |
---|
| 1155 | timepos, timelabels = drw.CFtimes_plot(odimyv, timeunit, timekind, timefmt) |
---|
| 1156 | else: |
---|
| 1157 | print errormsg |
---|
| 1158 | print ' ' + fname + ": time variable '" + timename + "' not found!!" |
---|
| 1159 | quit(-1) |
---|
| 1160 | |
---|
| 1161 | shading_nx = np.zeros((2), dtype=np.float) |
---|
| 1162 | shading_nx[0] = np.float(shadminmax.split(',')[0]) |
---|
| 1163 | shading_nx[1] = np.float(shadminmax.split(',')[1]) |
---|
| 1164 | |
---|
| 1165 | closeval = drw.Str_Bool(close) |
---|
| 1166 | |
---|
| 1167 | drw.plot_2D_shadow_time(valshad, vnamesfig, odimxv, odimyv, odimxu, odimyu, \ |
---|
| 1168 | dimnamesv, colbarn, shading_nx, varunits, figtitle, figkind, revals, timeaxis, \ |
---|
| 1169 | timepos, timelabels, False) |
---|
| 1170 | |
---|
| 1171 | # Line values |
---|
| 1172 | ## |
---|
| 1173 | ncfilel = ncfile.split(',')[1] |
---|
| 1174 | |
---|
| 1175 | vnamelfig = values.split(':')[0].split(',')[1] |
---|
| 1176 | varnl = varn.split(',')[1] |
---|
| 1177 | |
---|
| 1178 | objlf = NetCDFFile(ncfilel,'r') |
---|
| 1179 | objlvar = objlf.variables[varnl] |
---|
| 1180 | |
---|
| 1181 | linevals = objlvar[:] |
---|
| 1182 | if reva0 == 'tranpose': |
---|
| 1183 | plt.plot (linevals, odimxv, '-', color='k') |
---|
| 1184 | else: |
---|
| 1185 | plt.plot (odimxv, linevals, '-', color='k') |
---|
| 1186 | |
---|
| 1187 | objsf.close() |
---|
| 1188 | objsl.close() |
---|
| 1189 | |
---|
| 1190 | return |
---|
| 1191 | |
---|
[540] | 1192 | def draw_barbs(ncfile, values, varns): |
---|
[539] | 1193 | """ Function to plot wind barbs |
---|
[540] | 1194 | values= [dimname]|[vardimname]|[value]:[vecvals]:[windlabs]:[mapvalues]: |
---|
[539] | 1195 | [gtit]:[kindfig]:[figuren] |
---|
[540] | 1196 | 'X/Y/Z/T'|[dimname]|[vardimname]|[value]: ',', list for each basic dimension '|' separated of: |
---|
[539] | 1197 | [dimname]: name of the dimension in the file |
---|
| 1198 | [vardimname]: name of the variable with the values for the dimension in the file |
---|
| 1199 | [value]: which value of the given dimension (-1, all; [ibeg]@[iend], i-range beginning, end) |
---|
| 1200 | No value takes all the range of the dimension |
---|
| 1201 | [vecvals]= [frequency],[color],[length] |
---|
[540] | 1202 | [frequency]: [xfreq]@[yfreq] frequency of values allong each axis ('None', all grid points; |
---|
[539] | 1203 | 'auto', computed automatically to have 20 vectors along each axis) |
---|
[540] | 1204 | [color]: color of the vectors ('auto', for 'red') |
---|
| 1205 | [length]: length of the wind barbs ('auto', for 9) |
---|
[539] | 1206 | [windlabs]= [windname],[windunits] |
---|
| 1207 | [windname]: name of the wind variable in the graph |
---|
| 1208 | [windunits]: units of the wind variable in the graph ('None', for the value in the file) |
---|
| 1209 | [mapvalues]= map characteristics: [proj],[res] |
---|
| 1210 | see full documentation: http://matplotlib.org/basemap/ |
---|
| 1211 | [proj]: projection |
---|
| 1212 | * 'cyl', cilindric |
---|
| 1213 | * 'lcc', lambert conformal |
---|
| 1214 | [res]: resolution: |
---|
| 1215 | * 'c', crude |
---|
| 1216 | * 'l', low |
---|
| 1217 | * 'i', intermediate |
---|
| 1218 | * 'h', high |
---|
| 1219 | * 'f', full |
---|
| 1220 | gtit= title of the graph ('|', for spaces) |
---|
| 1221 | kindfig= kind of figure |
---|
| 1222 | figuren= name of the figure |
---|
| 1223 | ncfile= file to use |
---|
| 1224 | varns= [uwind],[ywind] ',' list of the name of the variables with the u-wind,y-wind component |
---|
| 1225 | """ |
---|
| 1226 | fname = 'draw_barbs' |
---|
| 1227 | |
---|
| 1228 | if values == 'h': |
---|
| 1229 | print fname + '_____________________________________________________________' |
---|
| 1230 | print draw_barbs.__doc__ |
---|
| 1231 | quit() |
---|
| 1232 | |
---|
| 1233 | expectargs = '[X/Y/Z/T]|[dimname]|[vardimname]|[value]:[vecvals]:[windlabs]:' + \ |
---|
| 1234 | '[mapvalues]:[gtit]:[kindfig]:[figuren]' |
---|
| 1235 | |
---|
[690] | 1236 | drw.check_arguments(fname,values,expectargs,':') |
---|
[539] | 1237 | |
---|
| 1238 | dimvals = values.split(':')[0] |
---|
| 1239 | vecvals = values.split(':')[1] |
---|
[540] | 1240 | windlabels = values.split(':')[2] |
---|
[539] | 1241 | mapvalues = values.split(':')[3] |
---|
| 1242 | gtit = values.split(':')[4] |
---|
| 1243 | kindfig = values.split(':')[5] |
---|
| 1244 | figuren = values.split(':')[6] |
---|
| 1245 | |
---|
| 1246 | of = NetCDFFile(ncfile,'r') |
---|
| 1247 | |
---|
| 1248 | dims = {} |
---|
| 1249 | for dimv in dimvals.split(','): |
---|
| 1250 | dns = dimv.split('|') |
---|
[540] | 1251 | dims[dns[0]] = [dns[1], dns[2], dns[3]] |
---|
[539] | 1252 | |
---|
| 1253 | varNs = [] |
---|
| 1254 | for dn in dims.keys(): |
---|
| 1255 | if dn == 'X': |
---|
[540] | 1256 | varNs.append(dims[dn][1]) |
---|
| 1257 | dimx = len(of.dimensions[dims[dn][0]]) |
---|
[539] | 1258 | elif dn == 'Y': |
---|
[540] | 1259 | varNs.append(dims[dn][1]) |
---|
| 1260 | dimy = len(of.dimensions[dims[dn][0]]) |
---|
[539] | 1261 | |
---|
| 1262 | ivar = 0 |
---|
| 1263 | for wvar in varns.split(','): |
---|
| 1264 | if not drw.searchInlist(of.variables.keys(), wvar): |
---|
| 1265 | print errormsg |
---|
| 1266 | print ' ' + fname + ": file does not have variable '" + wvar + "' !!" |
---|
| 1267 | quit(-1) |
---|
| 1268 | if ivar == 0: |
---|
| 1269 | varNs.append(wvar) |
---|
| 1270 | else: |
---|
| 1271 | varNs.append(wvar) |
---|
| 1272 | |
---|
| 1273 | ivar = 0 |
---|
| 1274 | for varN in varNs: |
---|
| 1275 | varslice = [] |
---|
| 1276 | |
---|
| 1277 | ovarN = of.variables[varN] |
---|
[540] | 1278 | vard = ovarN.dimensions |
---|
[539] | 1279 | for vdn in vard: |
---|
[540] | 1280 | found = False |
---|
[539] | 1281 | for dd in dims.keys(): |
---|
[540] | 1282 | if dims[dd][0] == vdn: |
---|
| 1283 | if dims[dd][2].find('@') != -1: |
---|
| 1284 | rvals = dims[dd][2].split('@') |
---|
[539] | 1285 | varslice.append(slice(int(rvals[0]), int(rvals[1]))) |
---|
[540] | 1286 | elif dims[dd][2] == '-1': |
---|
| 1287 | varslice.append(slice(0,len(of.dimensions[dims[dd][0]]))) |
---|
| 1288 | else: |
---|
| 1289 | varslice.append(int(dims[dd][2])) |
---|
| 1290 | |
---|
| 1291 | found = True |
---|
[539] | 1292 | break |
---|
[540] | 1293 | if not found: |
---|
| 1294 | varslice.append(slice(0,len(of.dimensions[dims[dd][0]]))) |
---|
[539] | 1295 | |
---|
[540] | 1296 | if varN == dims['X'][1]: |
---|
| 1297 | lonvals0 = np.squeeze(ovarN[tuple(varslice)]) |
---|
| 1298 | elif varN == dims['Y'][1]: |
---|
| 1299 | latvals0 = np.squeeze(ovarN[tuple(varslice)]) |
---|
[539] | 1300 | elif ivar == 2: |
---|
[540] | 1301 | uwvals = np.squeeze(np.array(ovarN[tuple(varslice)])) |
---|
[539] | 1302 | elif ivar == 3: |
---|
[698] | 1303 | vwvals = np.squeeze(ovarN[tuple(varslice)]) |
---|
[539] | 1304 | |
---|
[540] | 1305 | ivar = ivar + 1 |
---|
| 1306 | |
---|
| 1307 | # print 'Final shapes:',lonvals0.shape,':',latvals0.shape,':',uwvals.shape,':', |
---|
| 1308 | # vwvals.shape |
---|
| 1309 | |
---|
| 1310 | if len(uwvals.shape) != 2 or len(vwvals.shape) != 2: |
---|
| 1311 | print errormsg |
---|
| 1312 | print ' ' + fname + ': wrong size of the wind fields! they must be ' + \ |
---|
| 1313 | '2-dimensional!' |
---|
| 1314 | print ' u-winds shape:',uwvals.shape,'dims:',of.variables[varNs[2]] |
---|
| 1315 | print ' v-winds shape:',vwvals.shape,'dims:',of.variables[varNs[3]] |
---|
| 1316 | print ' provide more values for their dimensions!!' |
---|
| 1317 | quit(-1) |
---|
| 1318 | |
---|
[539] | 1319 | if len(lonvals0.shape) == 1: |
---|
| 1320 | lonvals, latvals = np.meshgrid(lonvals0, latvals0) |
---|
[540] | 1321 | else: |
---|
| 1322 | lonvals = lonvals0 |
---|
| 1323 | latvals = latvals0 |
---|
[539] | 1324 | |
---|
| 1325 | # Vecor values |
---|
[540] | 1326 | if vecvals.split(',')[0] == 'None': |
---|
| 1327 | freqv = None |
---|
| 1328 | else: |
---|
| 1329 | freqv = vecvals.split(',')[0] |
---|
[539] | 1330 | colorv = vecvals.split(',')[1] |
---|
| 1331 | lengthv = vecvals.split(',')[2] |
---|
| 1332 | |
---|
| 1333 | # Vector labels |
---|
| 1334 | windname = windlabels.split(',')[0] |
---|
| 1335 | windunits = windlabels.split(',')[1] |
---|
| 1336 | |
---|
| 1337 | drw.plot_barbs(lonvals, latvals, uwvals, vwvals, freqv, colorv, lengthv, |
---|
| 1338 | windname, windunits, mapvalues, gtit, kindfig, figuren) |
---|
| 1339 | |
---|
| 1340 | return |
---|
| 1341 | |
---|
[192] | 1342 | def draw_topo_geogrid(ncfile, values): |
---|
| 1343 | """ plotting geo_em.d[nn].nc topography from WPS files |
---|
| 1344 | draw_topo_geogrid(ncfile, values) |
---|
| 1345 | ncfile= geo_em.d[nn].nc file to use |
---|
| 1346 | values= [minTopo],[maxTopo]:[lonlatL]:[title]:[graphic_kind]:[mapvalues] |
---|
| 1347 | [min/max]Topo: minimum and maximum values of topography to draw |
---|
| 1348 | lonlatL: limits of longitudes and latitudes [lonmin, latmin, lonmax, latmax] or None |
---|
[773] | 1349 | title: title of the graph ('!' for spaces) |
---|
[192] | 1350 | graphic_kind: kind of figure (jpg, pdf, png) |
---|
| 1351 | mapvalues: map characteristics [proj],[res] |
---|
| 1352 | see full documentation: http://matplotlib.org/basemap/ |
---|
| 1353 | [proj]: projection |
---|
| 1354 | * 'cyl', cilindric |
---|
| 1355 | * 'lcc', lambert conformal |
---|
| 1356 | [res]: resolution: |
---|
| 1357 | * 'c', crude |
---|
| 1358 | * 'l', low |
---|
| 1359 | * 'i', intermediate |
---|
| 1360 | * 'h', high |
---|
| 1361 | * 'f', full |
---|
| 1362 | """ |
---|
| 1363 | fname = 'draw_topo_geogrid' |
---|
| 1364 | |
---|
| 1365 | if values == 'h': |
---|
| 1366 | print fname + '_____________________________________________________________' |
---|
| 1367 | print draw_topo_geogrid.__doc__ |
---|
| 1368 | quit() |
---|
| 1369 | |
---|
[773] | 1370 | expectargs = '[minTopo],[maxTopo]:[lonlatL]:[title]:[graphic_kind]:[mapvalues]' |
---|
[192] | 1371 | |
---|
[690] | 1372 | drw.check_arguments(fname,values,expectargs,':') |
---|
[192] | 1373 | |
---|
[664] | 1374 | mintopo = np.float(values.split(':')[0].split(',')[0]) |
---|
| 1375 | maxtopo = np.float(values.split(':')[0].split(',')[1]) |
---|
[192] | 1376 | |
---|
| 1377 | lonlatLS = values.split(':')[1] |
---|
| 1378 | lonlatLv = lonlatLS.split(',')[0] |
---|
| 1379 | |
---|
| 1380 | if lonlatLv == 'None': |
---|
| 1381 | lonlatL = None |
---|
| 1382 | else: |
---|
| 1383 | lonlatL = np.zeros((4), dtype=np.float) |
---|
| 1384 | lonlatL[0] = np.float(lonlatLS.split(',')[0]) |
---|
| 1385 | lonlatL[1] = np.float(lonlatLS.split(',')[1]) |
---|
| 1386 | lonlatL[2] = np.float(lonlatLS.split(',')[2]) |
---|
| 1387 | lonlatL[3] = np.float(lonlatLS.split(',')[3]) |
---|
| 1388 | |
---|
[773] | 1389 | grtit = values.split(':')[2].replace('!',' ') |
---|
[192] | 1390 | kindfig = values.split(':')[3] |
---|
| 1391 | mapvalues = values.split(':')[4] |
---|
| 1392 | |
---|
| 1393 | if not os.path.isfile(ncfile): |
---|
| 1394 | print errormsg |
---|
| 1395 | print ' ' + fname + ': domain file "' + ncfile + '" does not exist !!' |
---|
| 1396 | quit(-1) |
---|
| 1397 | |
---|
| 1398 | objdomf = NetCDFFile(ncfile, 'r') |
---|
| 1399 | |
---|
| 1400 | objhgt = objdomf.variables['HGT_M'] |
---|
| 1401 | objlon = objdomf.variables['XLONG_M'] |
---|
| 1402 | objlat = objdomf.variables['XLAT_M'] |
---|
| 1403 | |
---|
| 1404 | topography = objhgt[0,:,:] |
---|
| 1405 | |
---|
| 1406 | drw.plot_topo_geogrid(topography, objlon, objlat, mintopo, maxtopo, lonlatL, \ |
---|
| 1407 | grtit, kindfig, mapvalues, True) |
---|
| 1408 | |
---|
| 1409 | objdomf.close() |
---|
| 1410 | |
---|
| 1411 | return |
---|
| 1412 | |
---|
| 1413 | def draw_topo_geogrid_boxes(ncfiles, values): |
---|
| 1414 | """ plotting different geo_em.d[nn].nc topography from WPS files |
---|
| 1415 | draw_topo_geogrid_boxes(ncfile, values) |
---|
| 1416 | ncfiles= ',' list of geo_em.d[nn].nc files to use (fisrt as topographyc reference) |
---|
[655] | 1417 | values= [minTopo],[maxTopo]:[lonlatL]:[title]:[graphic_kind]:[mapvalues]:[labels]:[legloc] |
---|
[192] | 1418 | [min/max]Topo: minimum and maximum values of topography to draw |
---|
| 1419 | lonlatL: limits of longitudes and latitudes [lonmin, latmin, lonmax, latmax] or None |
---|
[773] | 1420 | title: title of the graph ('!' for spaces) |
---|
[192] | 1421 | graphic_kind: kind of figure (jpg, pdf, png) |
---|
| 1422 | mapvalues: map characteristics [proj],[res] |
---|
| 1423 | see full documentation: http://matplotlib.org/basemap/ |
---|
| 1424 | [proj]: projection |
---|
| 1425 | * 'cyl', cilindric |
---|
| 1426 | * 'lcc', lambert conformal |
---|
| 1427 | [res]: resolution: |
---|
| 1428 | * 'c', crude |
---|
| 1429 | * 'l', low |
---|
| 1430 | * 'i', intermediate |
---|
| 1431 | * 'h', high |
---|
| 1432 | * 'f', full |
---|
[655] | 1433 | legloc= location of the legend (0, autmoatic) |
---|
| 1434 | 1: 'upper right', 2: 'upper left', 3: 'lower left', 4: 'lower right', |
---|
| 1435 | 5: 'right', 6: 'center left', 7: 'center right', 8: 'lower center', |
---|
| 1436 | 9: 'upper center', 10: 'center' |
---|
[192] | 1437 | labels= labels to write in the graph |
---|
| 1438 | """ |
---|
| 1439 | # import matplotlib as mpl |
---|
| 1440 | # mpl.use('Agg') |
---|
| 1441 | import matplotlib.pyplot as plt |
---|
| 1442 | |
---|
| 1443 | fname = 'draw_topo_geogrid_boxes' |
---|
| 1444 | |
---|
| 1445 | if values == 'h': |
---|
| 1446 | print fname + '_____________________________________________________________' |
---|
| 1447 | print draw_topo_geogrid_boxes.__doc__ |
---|
| 1448 | quit() |
---|
| 1449 | |
---|
[664] | 1450 | mintopo = np.float(values.split(':')[0].split(',')[0]) |
---|
| 1451 | maxtopo = np.float(values.split(':')[0].split(',')[1]) |
---|
[192] | 1452 | |
---|
| 1453 | lonlatLS = values.split(':')[1] |
---|
| 1454 | lonlatLv = lonlatLS.split(',')[0] |
---|
| 1455 | |
---|
| 1456 | if lonlatLv == 'None': |
---|
| 1457 | lonlatL = None |
---|
| 1458 | else: |
---|
| 1459 | lonlatL = np.zeros((4), dtype=np.float) |
---|
| 1460 | lonlatL[0] = np.float(lonlatLS.split(',')[0]) |
---|
| 1461 | lonlatL[1] = np.float(lonlatLS.split(',')[1]) |
---|
| 1462 | lonlatL[2] = np.float(lonlatLS.split(',')[2]) |
---|
| 1463 | lonlatL[3] = np.float(lonlatLS.split(',')[3]) |
---|
| 1464 | |
---|
[773] | 1465 | grtit = values.split(':')[2].replace('!', ' ') |
---|
[192] | 1466 | kindfig = values.split(':')[3] |
---|
| 1467 | mapvalues = values.split(':')[4] |
---|
| 1468 | labels = values.split(':')[5] |
---|
[655] | 1469 | legloc = int(values.split(':')[6]) |
---|
[192] | 1470 | |
---|
| 1471 | ncfile = ncfiles.split(',')[0] |
---|
| 1472 | if not os.path.isfile(ncfile): |
---|
| 1473 | print errormsg |
---|
| 1474 | print ' ' + fname + ': domain file "' + ncfile + '" does not exist !!' |
---|
| 1475 | quit(-1) |
---|
| 1476 | |
---|
| 1477 | objdomf = NetCDFFile(ncfile, 'r') |
---|
| 1478 | |
---|
| 1479 | objhgt = objdomf.variables['HGT_M'] |
---|
| 1480 | objlon0 = objdomf.variables['XLONG_M'] |
---|
| 1481 | objlat0 = objdomf.variables['XLAT_M'] |
---|
| 1482 | |
---|
| 1483 | topography = objhgt[0,:,:] |
---|
| 1484 | |
---|
| 1485 | Nfiles = len(ncfiles.split(',')) |
---|
| 1486 | boxlabels = labels.split(',') |
---|
| 1487 | |
---|
| 1488 | Xboxlines = [] |
---|
| 1489 | Yboxlines = [] |
---|
| 1490 | |
---|
| 1491 | for ifile in range(Nfiles): |
---|
| 1492 | ncfile = ncfiles.split(',')[ifile] |
---|
| 1493 | # print ifile, ncfile |
---|
| 1494 | if not os.path.isfile(ncfile): |
---|
| 1495 | print errormsg |
---|
| 1496 | print ' ' + fname + ': domain file "' + ncfile + '" does not exist !!' |
---|
| 1497 | quit(-1) |
---|
| 1498 | |
---|
| 1499 | objdomfi = NetCDFFile(ncfile, 'r') |
---|
| 1500 | |
---|
| 1501 | objlon = objdomfi.variables['XLONG_M'] |
---|
| 1502 | objlat = objdomfi.variables['XLAT_M'] |
---|
| 1503 | |
---|
| 1504 | dx = objlon.shape[2] |
---|
| 1505 | dy = objlon.shape[1] |
---|
| 1506 | |
---|
| 1507 | Xboxlines.append(objlon[0,0,:]) |
---|
| 1508 | Yboxlines.append(objlat[0,0,:]) |
---|
| 1509 | Xboxlines.append(objlon[0,dy-1,:]) |
---|
| 1510 | Yboxlines.append(objlat[0,dy-1,:]) |
---|
| 1511 | Xboxlines.append(objlon[0,:,0]) |
---|
| 1512 | Yboxlines.append(objlat[0,:,0]) |
---|
| 1513 | Xboxlines.append(objlon[0,:,dx-1]) |
---|
| 1514 | Yboxlines.append(objlat[0,:,dx-1]) |
---|
| 1515 | |
---|
| 1516 | objdomfi.close() |
---|
| 1517 | |
---|
| 1518 | drw.plot_topo_geogrid_boxes(topography, Xboxlines, Yboxlines, boxlabels, \ |
---|
[655] | 1519 | objlon0, objlat0, mintopo, maxtopo, lonlatL, grtit, kindfig, mapvalues, legloc,\ |
---|
| 1520 | True) |
---|
[192] | 1521 | |
---|
| 1522 | objdomf.close() |
---|
| 1523 | |
---|
| 1524 | return |
---|
| 1525 | |
---|
| 1526 | def movievalslice(origslice, dimmovien, framenum): |
---|
| 1527 | """ Function to provide variable slice according to a geneation of a movie |
---|
| 1528 | movievals(origslice, dimmovien, framenum) |
---|
| 1529 | [origslice]= slice original as [dimname1]|[val1],[...,[dimnameN]|[valN]] |
---|
| 1530 | ([val] = -1, full length) |
---|
| 1531 | [dimmovien]= name of the dimension to produce the movie |
---|
| 1532 | [framenum]= value of the frame to substitue in [origslice] as |
---|
| 1533 | [dimmovien]|[framenum] |
---|
| 1534 | >>> movievalslice('East_West|-1,North_South|-1,Time|2','Time',0) |
---|
| 1535 | East_West|-1,North_South|-1,Time|0 |
---|
| 1536 | """ |
---|
| 1537 | |
---|
| 1538 | fname = 'movievalslice' |
---|
| 1539 | |
---|
| 1540 | if origslice == 'h': |
---|
| 1541 | print fname + '_____________________________________________________________' |
---|
| 1542 | print movievalslice.__doc__ |
---|
| 1543 | quit() |
---|
| 1544 | |
---|
| 1545 | dims = origslice.split(',') |
---|
| 1546 | |
---|
| 1547 | movieslice = '' |
---|
| 1548 | idim = 0 |
---|
| 1549 | |
---|
| 1550 | for dimn in dims: |
---|
| 1551 | dn = dimn.split('|')[0] |
---|
| 1552 | if dn == dimmovien: |
---|
| 1553 | movieslice = movieslice + dn + '|' + str(framenum) |
---|
| 1554 | else: |
---|
| 1555 | movieslice = movieslice + dimn |
---|
| 1556 | if idim < len(dims)-1: movieslice = movieslice + ',' |
---|
| 1557 | |
---|
| 1558 | idim = idim + 1 |
---|
| 1559 | |
---|
| 1560 | return movieslice |
---|
| 1561 | |
---|
| 1562 | class Capturing(list): |
---|
| 1563 | """ Class to capture function output as a list |
---|
| 1564 | from: http://stackoverflow.com/questions/16571150/how-to-capture-stdout-output-from-a-python-function-call |
---|
| 1565 | """ |
---|
| 1566 | # from cStringIO import StringIO |
---|
| 1567 | |
---|
| 1568 | def __enter__(self): |
---|
| 1569 | self._stdout = sys.stdout |
---|
| 1570 | sys.stdout = self._stringio = StringIO() |
---|
| 1571 | return self |
---|
| 1572 | def __exit__(self, *args): |
---|
| 1573 | self.extend(self._stringio.getvalue().splitlines()) |
---|
| 1574 | sys.stdout = self._stdout |
---|
| 1575 | |
---|
| 1576 | def create_movie(netcdfile, values, variable): |
---|
| 1577 | """ Function to create a movie assuming ImageMagick installed! |
---|
| 1578 | values= [graph]#[movie_dimension]#[graph_values] |
---|
| 1579 | [graph]: which graphic |
---|
| 1580 | [movie_dimension]: [dimnmovie]@[dimvmovie]@[moviedelay]@[interval] |
---|
| 1581 | [dimnmovie]; name of the dimension from which make the movie |
---|
| 1582 | [dimvmovie]; name of the variable with the values of the dimension |
---|
| 1583 | [moviedelay]; delay between frames |
---|
| 1584 | [interval]; [beg]@[end]@[freq] or -1 (all) |
---|
| 1585 | [graph_values]: values to generate the graphic |
---|
| 1586 | netcdfile= netCDF file |
---|
| 1587 | variable= variable to use (when applicable) |
---|
| 1588 | """ |
---|
| 1589 | fname = 'create_movie' |
---|
| 1590 | |
---|
| 1591 | if values == 'h': |
---|
| 1592 | print fname + '_____________________________________________________________' |
---|
| 1593 | print create_movie.__doc__ |
---|
| 1594 | quit() |
---|
| 1595 | |
---|
| 1596 | graph = values.split('#')[0] |
---|
| 1597 | movie_dim = values.split('#')[1] |
---|
| 1598 | graph_vals = values.split('#')[2] |
---|
| 1599 | |
---|
| 1600 | ncobj = NetCDFFile(netcdfile, 'r') |
---|
| 1601 | |
---|
| 1602 | # Movie dimension |
---|
| 1603 | ## |
---|
| 1604 | dimnmovie = movie_dim.split('@')[0] |
---|
| 1605 | dimvmovie = movie_dim.split('@')[1] |
---|
| 1606 | moviedelay = movie_dim.split('@')[2] |
---|
| 1607 | moviebeg = int(movie_dim.split('@')[3]) |
---|
| 1608 | |
---|
| 1609 | if not drw.searchInlist(ncobj.dimensions.keys(),dimnmovie): |
---|
| 1610 | print errormsg |
---|
| 1611 | print ' ' + fname + ": file '" + netcdfile + "' has not dimension '" + \ |
---|
| 1612 | dimnmovie + "' !!!" |
---|
| 1613 | quit(-1) |
---|
| 1614 | |
---|
| 1615 | objdmovie = ncobj.dimensions[dimnmovie] |
---|
| 1616 | dmovie = len(objdmovie) |
---|
| 1617 | if moviebeg != -1: |
---|
| 1618 | moviend = int(movie_dim.split('@')[4]) |
---|
| 1619 | moviefreq = int(movie_dim.split('@')[5]) |
---|
| 1620 | else: |
---|
| 1621 | moviebeg = 0 |
---|
| 1622 | moviend = dmovie |
---|
| 1623 | moviefreq = 1 |
---|
| 1624 | |
---|
| 1625 | if dimvmovie == 'WRFTimes': |
---|
| 1626 | objvdmovie = ncobj.variables['Times'] |
---|
| 1627 | vdmovieunits = '' |
---|
| 1628 | valsdmovie = [] |
---|
| 1629 | for it in range(objvdmovie.shape[0]): |
---|
| 1630 | valsdmovie.append(drw.datetimeStr_conversion(objvdmovie[it,:], \ |
---|
| 1631 | 'WRFdatetime', 'Y/m/d H-M-S')) |
---|
| 1632 | elif dimvmovie == 'CFtime': |
---|
| 1633 | objvdmovie = ncobj.variables['time'] |
---|
| 1634 | vdmovieunits = '' |
---|
| 1635 | print objvdmovie.units |
---|
| 1636 | valsdmovie0 = drw.netCDFdatetime_realdatetime(objvdmovie.units, 'standard', \ |
---|
| 1637 | objvdmovie[:]) |
---|
| 1638 | valsdmovie = [] |
---|
| 1639 | for it in range(objvdmovie.shape[0]): |
---|
| 1640 | valsdmovie.append(drw.datetimeStr_conversion(valsdmovie0[it,:], \ |
---|
| 1641 | 'matYmdHMS', 'Y/m/d H-M-S')) |
---|
| 1642 | else: |
---|
| 1643 | if not drw.searchInlist(ncobj.variables.keys(),dimvmovie): |
---|
| 1644 | print errormsg |
---|
| 1645 | print ' ' + fname + ": file '" + netcdfile + "' has not variable '" + \ |
---|
| 1646 | dimvmovie + "' !!!" |
---|
| 1647 | quit(-1) |
---|
| 1648 | vdmovieunits = objvdmovie.getncattr('units') |
---|
| 1649 | objvdmovie = ncobj.variables[dimvmovie] |
---|
| 1650 | if len(objvdmovie.shape) == 1: |
---|
| 1651 | vasldmovie = objvdmovie[:] |
---|
| 1652 | else: |
---|
| 1653 | print errormsg |
---|
| 1654 | print ' ' + fname + ': shape', objvdmovie.shape, 'of variable with ' + \ |
---|
| 1655 | 'dimension movie values not ready!!!' |
---|
| 1656 | quit(-1) |
---|
| 1657 | |
---|
| 1658 | ncobj.close() |
---|
| 1659 | os.system('rm frame_*.png > /dev/null') |
---|
| 1660 | |
---|
| 1661 | # graphic |
---|
| 1662 | ## |
---|
| 1663 | if graph == 'draw_2D_shad': |
---|
| 1664 | graphvals = graph_vals.split(':') |
---|
| 1665 | |
---|
| 1666 | for iframe in range(moviebeg,moviend,moviefreq): |
---|
| 1667 | iframeS = str(iframe).zfill(4) |
---|
| 1668 | |
---|
| 1669 | drw.percendone((iframe-moviebeg)/moviefreq,(moviend-moviebeg)/moviefreq, \ |
---|
| 1670 | 5, 'frames') |
---|
| 1671 | titgraph = dimnmovie + '|=|' + str(valsdmovie[iframe]) + '|' + \ |
---|
| 1672 | vdmovieunits |
---|
| 1673 | |
---|
| 1674 | graphvals[1] = movievalslice(graphvals[1],dimnmovie,iframe) |
---|
| 1675 | graphvals[6] = titgraph |
---|
| 1676 | graphvals[7] = 'png' |
---|
| 1677 | |
---|
| 1678 | graphv = drw.numVector_String(graphvals, ":") |
---|
| 1679 | |
---|
| 1680 | with Capturing() as output: |
---|
| 1681 | draw_2D_shad(netcdfile, graphv, variable) |
---|
| 1682 | |
---|
| 1683 | os.system('mv 2Dfields_shadow.png frame_' + iframeS + '.png') |
---|
| 1684 | else: |
---|
| 1685 | print errormsg |
---|
| 1686 | print ' ' + fname + ": graphic '" + graph + "' not defined !!!" |
---|
| 1687 | quit(-1) |
---|
| 1688 | |
---|
| 1689 | os.system('convert -delay ' + moviedelay + ' -loop 0 frame_*.png create_movie.gif') |
---|
| 1690 | |
---|
| 1691 | print "Succesfuly creation of movie file 'create_movie.gif' !!!" |
---|
| 1692 | |
---|
| 1693 | return |
---|
| 1694 | |
---|
| 1695 | def draw_lines(ncfilens, values, varname): |
---|
| 1696 | """ Function to draw different lines at the same time from different files |
---|
[194] | 1697 | draw_lines(ncfilens, values, varname): |
---|
[192] | 1698 | ncfilens= [filen] ',' separated list of netCDF files |
---|
[218] | 1699 | values= [dimvname]:[valuesaxis]:[dimtit]:[leglabels]:[vtit]:[title]:[locleg]:[graphk] |
---|
[470] | 1700 | [dimvname]: ',' list of names of the variable with he values of the common dimension |
---|
[192] | 1701 | [valuesaxis]: which axis will be used for the values ('x', or 'y') |
---|
| 1702 | [dimtit]: title for the common dimension |
---|
| 1703 | [leglabels]: ',' separated list of names for the legend |
---|
| 1704 | [vartit]: name of the variable in the graph |
---|
[193] | 1705 | [title]: title of the plot ('|' for spaces) |
---|
[655] | 1706 | [locleg]: location of the legend (0, autmoatic) |
---|
[218] | 1707 | 1: 'upper right', 2: 'upper left', 3: 'lower left', 4: 'lower right', |
---|
| 1708 | 5: 'right', 6: 'center left', 7: 'center right', 8: 'lower center', |
---|
| 1709 | 9: 'upper center', 10: 'center' |
---|
[192] | 1710 | [graphk]: kind of the graphic |
---|
| 1711 | varname= variable to plot |
---|
| 1712 | values= 'XLAT:x:latitude:32x32:$wss^{*}$:wss Taylor's turbulence term:pdf' |
---|
| 1713 | """ |
---|
| 1714 | |
---|
| 1715 | fname = 'draw_lines' |
---|
| 1716 | |
---|
| 1717 | if values == 'h': |
---|
| 1718 | print fname + '_____________________________________________________________' |
---|
| 1719 | print draw_lines.__doc__ |
---|
| 1720 | quit() |
---|
| 1721 | |
---|
[218] | 1722 | expectargs = '[dimvname]:[valuesaxis]:[dimtit]:[leglabels]:[vtit]:[title]:[locleg]:[graphk]' |
---|
[690] | 1723 | drw.check_arguments(fname,values,expectargs,':') |
---|
[192] | 1724 | |
---|
| 1725 | ncfiles = ncfilens.split(',') |
---|
[470] | 1726 | dimvnames = values.split(':')[0] |
---|
[192] | 1727 | valuesaxis = values.split(':')[1] |
---|
| 1728 | dimtit = values.split(':')[2] |
---|
[193] | 1729 | leglabels = values.split(':')[3].replace('_','\_') |
---|
[192] | 1730 | vartit = values.split(':')[4] |
---|
[193] | 1731 | title = values.split(':')[5].replace('|',' ') |
---|
[472] | 1732 | locleg = values.split(':')[6] |
---|
| 1733 | graphk = values.split(':')[7] |
---|
[192] | 1734 | |
---|
| 1735 | Nfiles = len(ncfiles) |
---|
| 1736 | |
---|
| 1737 | # Getting trajectotries |
---|
| 1738 | ## |
---|
| 1739 | |
---|
| 1740 | varvalues = [] |
---|
| 1741 | dimvalues = [] |
---|
| 1742 | |
---|
| 1743 | print ' ' + fname |
---|
| 1744 | ifn = 0 |
---|
| 1745 | for ifile in ncfiles: |
---|
| 1746 | filen = ifile.split('@')[0] |
---|
| 1747 | |
---|
| 1748 | print ' filen:',filen |
---|
| 1749 | |
---|
| 1750 | if not os.path.isfile(filen): |
---|
| 1751 | print errormsg |
---|
| 1752 | print ' ' + fname + ": netCDF file '" + filen + "' does not exist !!" |
---|
| 1753 | quit(-1) |
---|
| 1754 | |
---|
| 1755 | objfile = NetCDFFile(filen, 'r') |
---|
| 1756 | |
---|
[470] | 1757 | if dimvnames.find(',') != -1: |
---|
| 1758 | dimvname = dimvnames.split(',') |
---|
| 1759 | else: |
---|
| 1760 | dimvname = [dimvnames] |
---|
| 1761 | |
---|
| 1762 | found = False |
---|
| 1763 | for dvn in dimvname: |
---|
| 1764 | if objfile.variables.has_key(dvn): |
---|
| 1765 | found = True |
---|
| 1766 | break |
---|
| 1767 | |
---|
| 1768 | if not found: |
---|
[192] | 1769 | print errormsg |
---|
| 1770 | print ' ' + fname + ": netCDF file '" + filen + \ |
---|
[472] | 1771 | "' does not have variables '" + dimvnames + "' !!" |
---|
[192] | 1772 | quit(-1) |
---|
| 1773 | |
---|
| 1774 | if not objfile.variables.has_key(varname): |
---|
| 1775 | print errormsg |
---|
| 1776 | print ' ' + fname + ": netCDF file '" + filen + \ |
---|
| 1777 | "' does not have variable '" + varname + "' !!" |
---|
| 1778 | quit(-1) |
---|
| 1779 | |
---|
| 1780 | vvobj = objfile.variables[varname] |
---|
| 1781 | if len(vvobj.shape) != 1: |
---|
| 1782 | print errormsg |
---|
| 1783 | print ' ' + fname + ': wrong shape:',vvobj.shape," of variable '" + \ |
---|
| 1784 | varname + "' !!" |
---|
| 1785 | quit(-1) |
---|
| 1786 | |
---|
[470] | 1787 | for dimvn in dimvname: |
---|
[471] | 1788 | if drw.searchInlist(objfile.variables, dimvn): |
---|
[472] | 1789 | vdobj = objfile.variables[dimvn] |
---|
[470] | 1790 | if len(vdobj.shape) != 1: |
---|
| 1791 | print errormsg |
---|
| 1792 | print ' ' + fname + ': wrong shape:',vdobj.shape, \ |
---|
| 1793 | " of variable '" + dimvn + "' !!" |
---|
| 1794 | quit(-1) |
---|
| 1795 | break |
---|
[192] | 1796 | |
---|
| 1797 | varvalues.append(vvobj[:]) |
---|
| 1798 | dimvalues.append(vdobj[:]) |
---|
| 1799 | |
---|
| 1800 | if ifn == 0: |
---|
| 1801 | varunits = vvobj.units |
---|
| 1802 | |
---|
| 1803 | objfile.close() |
---|
| 1804 | |
---|
| 1805 | ifn = ifn + 1 |
---|
| 1806 | |
---|
| 1807 | drw.plot_lines(dimvalues, varvalues, valuesaxis, dimtit, leglabels.split(','), \ |
---|
[472] | 1808 | vartit, varunits, title, locleg, graphk) |
---|
[192] | 1809 | |
---|
| 1810 | return |
---|
| 1811 | |
---|
[531] | 1812 | def draw_lines_time(ncfilens, values, varname0): |
---|
[194] | 1813 | """ Function to draw different lines at the same time from different files with times |
---|
| 1814 | draw_lines_time(ncfilens, values, varname): |
---|
| 1815 | ncfilens= [filen] ',' separated list of netCDF files |
---|
[531] | 1816 | values= [dimvname];[valuesaxis];[dimtit];[leglabels];[vtit];[title];[timevals];[locleg]; |
---|
[687] | 1817 | [graphk];[collines];[points];[linewidths];[pointsizes];[pointfreq];[period] |
---|
[531] | 1818 | [dimvname]: ',' list of names of the variables with he values of the common dimension |
---|
[194] | 1819 | [valuesaxis]: which axis will be used for the values ('x', or 'y') |
---|
[561] | 1820 | [dimtit]: title for the common dimension ('|' for spaces) |
---|
[562] | 1821 | [leglabels]: ',' separated list of names for the legend ('None', no legend) |
---|
[194] | 1822 | [vartit]: name of the variable in the graph |
---|
| 1823 | [title]: title of the plot ('|' for spaces) |
---|
| 1824 | [timevals]: [timen]|[units]|[kind]|[tfmt] time labels characteristics |
---|
| 1825 | [timen]; name of the time variable |
---|
| 1826 | [units]; units string according to CF conventions ([tunits] since |
---|
| 1827 | [YYYY]-[MM]-[DD] [[HH]:[MI]:[SS]], '!' for spaces) |
---|
| 1828 | [kind]; kind of output |
---|
| 1829 | 'Nval': according to a given number of values as 'Nval',[Nval] |
---|
| 1830 | 'exct': according to an exact time unit as 'exct',[tunit]; |
---|
| 1831 | tunit= [Nunits],[tu]; [tu]= 'c': centuries, 'y': year, 'm': month, |
---|
| 1832 | 'w': week, 'd': day, 'h': hour, 'i': minute, 's': second, |
---|
| 1833 | 'l': milisecond |
---|
| 1834 | [tfmt]; desired format |
---|
[655] | 1835 | [locleg]: location of the legend (0, autmoatic) |
---|
[204] | 1836 | 1: 'upper right', 2: 'upper left', 3: 'lower left', 4: 'lower right', |
---|
| 1837 | 5: 'right', 6: 'center left', 7: 'center right', 8: 'lower center', |
---|
| 1838 | 9: 'upper center', 10: 'center' |
---|
[194] | 1839 | [graphk]: kind of the graphic |
---|
[684] | 1840 | [lines]: ',' list of type of lines, None for automatic, single value all the same |
---|
[620] | 1841 | [collines]: ',' list of colors for the lines, None for automatic, single |
---|
| 1842 | value all the same |
---|
| 1843 | [points]: ',' list of type of points for the lines, None for automatic, single |
---|
| 1844 | value all the same |
---|
[687] | 1845 | [linewidths]: ',' list of widths for the lines, None for automatic, single |
---|
| 1846 | value all the same |
---|
| 1847 | [pointsizes]: ',' list of widths for the lines, None for automatic, single |
---|
| 1848 | value all the same |
---|
[679] | 1849 | [pointfreq]: frequency of point plotting, 'all' for all time steps |
---|
[561] | 1850 | [period]: which period to plot |
---|
| 1851 | '-1': all period |
---|
| 1852 | [beg],[end]: beginning and end of the period in reference time-units of first file |
---|
[531] | 1853 | varname0= ',' list of variable names to plot (assuming only 1 variable per file) |
---|
[204] | 1854 | values= 'time;y;time ([DD]${[HH]}$);32x32;$wss^{*}$;wss Taylor's turbulence term;time|hours!since!1949-12-01_00:00:00;exct,12,h|%d$^{%H}$;2;pdf' |
---|
[194] | 1855 | """ |
---|
| 1856 | |
---|
| 1857 | fname = 'draw_lines_time' |
---|
| 1858 | |
---|
| 1859 | if values == 'h': |
---|
| 1860 | print fname + '_____________________________________________________________' |
---|
[347] | 1861 | print draw_lines_time.__doc__ |
---|
[194] | 1862 | quit() |
---|
| 1863 | |
---|
| 1864 | expectargs = '[dimvname];[valuesaxis];[dimtit];[leglabels];[vtit];[title];' |
---|
[684] | 1865 | expectargs = expectargs + '[timevals];[locleg];[graphk];[lines];[collines];[points];' |
---|
[687] | 1866 | expectargs = expectargs + '[linewidths];[pointsizes];[pointfreq];[period]' |
---|
[690] | 1867 | drw.check_arguments(fname,values,expectargs,';') |
---|
[194] | 1868 | |
---|
| 1869 | ncfiles = ncfilens.split(',') |
---|
[531] | 1870 | dimvname0 = values.split(';')[0] |
---|
[194] | 1871 | valuesaxis = values.split(';')[1] |
---|
[561] | 1872 | dimtit = values.split(';')[2].replace('|',' ') |
---|
[194] | 1873 | leglabels = values.split(';')[3].replace('_','\_') |
---|
| 1874 | vartit = values.split(';')[4] |
---|
| 1875 | title = values.split(';')[5].replace('|',' ') |
---|
| 1876 | timevals = values.split(';')[6] |
---|
[204] | 1877 | locleg = int(values.split(';')[7]) |
---|
| 1878 | graphk = values.split(';')[8] |
---|
[684] | 1879 | lines0 = values.split(';')[9] |
---|
| 1880 | collines0 = values.split(';')[10] |
---|
| 1881 | points0 = values.split(';')[11] |
---|
[687] | 1882 | linewidths0 = values.split(';')[12] |
---|
| 1883 | pointsizes0 = values.split(';')[13] |
---|
| 1884 | pointfreq0 = values.split(';')[14] |
---|
| 1885 | period = values.split(';')[15] |
---|
[194] | 1886 | |
---|
| 1887 | Nfiles = len(ncfiles) |
---|
| 1888 | |
---|
[531] | 1889 | # Multiple variable-dimension names? |
---|
| 1890 | if dimvname0.find(',') != -1: |
---|
| 1891 | dimvname = dimvname0.split(',') |
---|
| 1892 | else: |
---|
| 1893 | dimvname = [dimvname0] |
---|
| 1894 | |
---|
| 1895 | # Multiple variables? |
---|
| 1896 | if varname0.find(',') != -1: |
---|
| 1897 | varname = varname0.split(',') |
---|
| 1898 | else: |
---|
| 1899 | varname = [varname0] |
---|
| 1900 | |
---|
[684] | 1901 | # Multiple lines types? |
---|
| 1902 | if lines0.find(',') != -1: |
---|
| 1903 | lines = lines0.split(',') |
---|
| 1904 | elif lines0 == 'None': |
---|
| 1905 | lines = None |
---|
| 1906 | else: |
---|
| 1907 | lines = [] |
---|
| 1908 | for il in range(Nfiles): |
---|
| 1909 | lines.append(lines0) |
---|
| 1910 | |
---|
[531] | 1911 | # Multiple color names? |
---|
| 1912 | if collines0.find(',') != -1: |
---|
| 1913 | collines = collines0.split(',') |
---|
[687] | 1914 | elif collines0 == 'None': |
---|
[620] | 1915 | collines = None |
---|
[531] | 1916 | else: |
---|
[620] | 1917 | collines = [] |
---|
| 1918 | for ip in range(Nfiles): |
---|
| 1919 | collines.append(collines0) |
---|
[531] | 1920 | |
---|
[620] | 1921 | # Multiple point types? |
---|
| 1922 | if points0.find(',') != -1: |
---|
| 1923 | points = points0.split(',') |
---|
| 1924 | elif points0 == 'None': |
---|
| 1925 | points = None |
---|
| 1926 | else: |
---|
| 1927 | points = [] |
---|
| 1928 | for ip in range(Nfiles): |
---|
[684] | 1929 | points.append(points0) |
---|
[620] | 1930 | |
---|
[687] | 1931 | # Multiple line sizes? |
---|
| 1932 | if linewidths0.find(',') != -1: |
---|
| 1933 | linewidths = [] |
---|
| 1934 | Nlines = len(linewidths0.split(',')) |
---|
| 1935 | for il in Nlines: |
---|
| 1936 | linewidths.append(np.float(linewidths0.split(',')[il])) |
---|
| 1937 | elif linewidths0 == 'None': |
---|
| 1938 | linewidths = None |
---|
| 1939 | else: |
---|
| 1940 | linewidths = [np.float(linewidths0)] |
---|
| 1941 | |
---|
| 1942 | # Multiple point sizes? |
---|
| 1943 | if pointsizes0.find(',') != -1: |
---|
| 1944 | pointsizes = [] |
---|
| 1945 | Npts = len(pointsizes0.split(',')) |
---|
| 1946 | for ip in Npts: |
---|
| 1947 | pointsizes.append(np.float(pointsizes0.split(',')[ip])) |
---|
| 1948 | elif pointsizes0 == 'None': |
---|
| 1949 | pointsizes = None |
---|
| 1950 | else: |
---|
| 1951 | pointsizes = [np.float(pointsizes0)] |
---|
| 1952 | |
---|
[801] | 1953 | timename = timevals.split('|')[0] |
---|
| 1954 | timeunit = timevals.split('|')[1].replace('!',' ') |
---|
| 1955 | timekind = timevals.split('|')[2] |
---|
| 1956 | timefmt = timevals.split('|')[3] |
---|
| 1957 | |
---|
[194] | 1958 | # Getting values |
---|
| 1959 | ## |
---|
| 1960 | varvalues = [] |
---|
| 1961 | dimvalues = [] |
---|
| 1962 | timvalues = [] |
---|
| 1963 | timvals0 = timvalues |
---|
| 1964 | |
---|
| 1965 | print ' ' + fname |
---|
| 1966 | ifn = 0 |
---|
[203] | 1967 | mintval = 1.e20 |
---|
| 1968 | maxtval = -1.e20 |
---|
[201] | 1969 | |
---|
[194] | 1970 | for ifile in ncfiles: |
---|
| 1971 | filen = ifile.split('@')[0] |
---|
| 1972 | |
---|
| 1973 | print ' filen:',filen |
---|
| 1974 | |
---|
| 1975 | if not os.path.isfile(filen): |
---|
| 1976 | print errormsg |
---|
| 1977 | print ' ' + fname + ": netCDF file '" + filen + "' does not exist !!" |
---|
| 1978 | quit(-1) |
---|
| 1979 | |
---|
| 1980 | objfile = NetCDFFile(filen, 'r') |
---|
| 1981 | |
---|
[531] | 1982 | founddvar = False |
---|
| 1983 | for dvar in dimvname: |
---|
| 1984 | if objfile.variables.has_key(dvar): |
---|
| 1985 | founddvar = True |
---|
| 1986 | vdobj = objfile.variables[dvar] |
---|
| 1987 | if len(vdobj.shape) != 1: |
---|
| 1988 | print errormsg |
---|
| 1989 | print ' ' + fname + ': wrong shape:',vdobj.shape," of " + \ |
---|
| 1990 | "variable '" + dvar + "' !!" |
---|
| 1991 | quit(-1) |
---|
| 1992 | break |
---|
| 1993 | if not founddvar: |
---|
[194] | 1994 | print errormsg |
---|
| 1995 | print ' ' + fname + ": netCDF file '" + filen + \ |
---|
[531] | 1996 | "' has any variable '", dimvname, "' !!" |
---|
[194] | 1997 | quit(-1) |
---|
| 1998 | |
---|
[531] | 1999 | foundvar = False |
---|
| 2000 | for var in varname: |
---|
| 2001 | if objfile.variables.has_key(var): |
---|
| 2002 | foundvar = True |
---|
| 2003 | vvobj = objfile.variables[var] |
---|
| 2004 | if len(vvobj.shape) != 1: |
---|
| 2005 | print errormsg |
---|
| 2006 | print ' ' + fname + ': wrong shape:',vvobj.shape," of " + \ |
---|
| 2007 | "variable '" + var + "' !!" |
---|
| 2008 | quit(-1) |
---|
| 2009 | |
---|
| 2010 | break |
---|
| 2011 | if not foundvar: |
---|
[194] | 2012 | print errormsg |
---|
| 2013 | print ' ' + fname + ": netCDF file '" + filen + \ |
---|
[531] | 2014 | "' has any variable '", varname, "' !!" |
---|
[194] | 2015 | quit(-1) |
---|
| 2016 | |
---|
| 2017 | |
---|
[561] | 2018 | # Getting period |
---|
[801] | 2019 | if ifn > 0: |
---|
| 2020 | # Referring all times to the same reference time! |
---|
| 2021 | reftvals = drw.coincident_CFtimes(vdobj[:], timeunit, vdobj.units) |
---|
| 2022 | else: |
---|
| 2023 | reftvals = vdobj[:] |
---|
| 2024 | |
---|
[561] | 2025 | dimt = len(vdobj[:]) |
---|
[194] | 2026 | |
---|
[604] | 2027 | if period == '-1': |
---|
[561] | 2028 | varvalues.append(vvobj[:]) |
---|
[801] | 2029 | dimvalues.append(reftvals) |
---|
| 2030 | mindvals = np.min(reftvals) |
---|
| 2031 | maxdvals = np.max(reftvals) |
---|
[561] | 2032 | else: |
---|
| 2033 | ibeg=-1 |
---|
| 2034 | iend=-1 |
---|
| 2035 | tbeg = np.float(period.split(',')[0]) |
---|
| 2036 | tend = np.float(period.split(',')[1]) |
---|
| 2037 | |
---|
| 2038 | for it in range(dimt-1): |
---|
[801] | 2039 | if reftvals[it] <= tbeg and reftvals[it+1] > tbeg: ibeg = it |
---|
| 2040 | if reftvals[it] <= tend and reftvals[it+1] > tend: iend = it + 1 |
---|
[561] | 2041 | if ibeg != -1 and iend != -1: break |
---|
| 2042 | |
---|
| 2043 | if ibeg == -1 and iend == -1: |
---|
[801] | 2044 | print warnmsg |
---|
[561] | 2045 | print ' ' + fname + ': Period:',tbeg,',',tend,'not found!!' |
---|
| 2046 | print ' ibeg:',ibeg,'iend:',iend |
---|
[801] | 2047 | print ' period in file:',np.min(reftvals), np.max(reftvals) |
---|
| 2048 | print ' getting all the period in file !!!' |
---|
| 2049 | ibeg = 0 |
---|
| 2050 | iend = dimt |
---|
[561] | 2051 | elif iend == -1: |
---|
| 2052 | iend = dimt |
---|
| 2053 | print warnmsg |
---|
| 2054 | print ' ' + fname + ': end of Period:',tbeg,',',tend,'not found!!' |
---|
| 2055 | print ' getting last available time instead' |
---|
| 2056 | print ' ibeg:',ibeg,'iend:',iend |
---|
[801] | 2057 | print ' period in file:',np.min(reftvals), np.max(reftvals) |
---|
[561] | 2058 | elif ibeg == -1: |
---|
| 2059 | ibeg = 0 |
---|
| 2060 | print warnmsg |
---|
| 2061 | print ' ' + fname + ': beginning of Period:',tbeg,',',tend, \ |
---|
| 2062 | 'not found!!' |
---|
| 2063 | print ' getting first available time instead' |
---|
| 2064 | print ' ibeg:',ibeg,'iend:',iend |
---|
[801] | 2065 | print ' period in file:',np.min(reftvals), np.max(reftvals) |
---|
[561] | 2066 | |
---|
| 2067 | varvalues.append(vvobj[ibeg:iend]) |
---|
[801] | 2068 | dimvalues.append(reftvals[ibeg:iend]) |
---|
| 2069 | mindvals = np.min(reftvals[ibeg:iend]) |
---|
| 2070 | maxdvals = np.max(reftvals[ibeg:iend]) |
---|
[561] | 2071 | |
---|
| 2072 | dimt = iend - ibeg |
---|
| 2073 | |
---|
[201] | 2074 | if mindvals < mintval: mintval = mindvals |
---|
| 2075 | if maxdvals > maxtval: maxtval = maxdvals |
---|
[801] | 2076 | # print ' ' + fname + ": file '" + filen + "' period:", ibeg, '->', iend, \ |
---|
| 2077 | # reftvals[ibeg], '->', reftvals[np.min([iend,dimt-1])], timeunit |
---|
[201] | 2078 | |
---|
[194] | 2079 | if ifn == 0: |
---|
[348] | 2080 | varunits = drw.units_lunits(vvobj.units) |
---|
[194] | 2081 | |
---|
| 2082 | objfile.close() |
---|
| 2083 | |
---|
| 2084 | ifn = ifn + 1 |
---|
| 2085 | |
---|
| 2086 | # Times |
---|
| 2087 | |
---|
[558] | 2088 | dtvals = (maxtval - mintval)/dimt |
---|
[801] | 2089 | # dti = mintval-dtvals/2. |
---|
| 2090 | # dte = maxtval+dtvals/2. |
---|
| 2091 | dti = mintval |
---|
| 2092 | dte = maxtval |
---|
| 2093 | tvals = np.arange(dti, dte, dtvals) |
---|
[194] | 2094 | |
---|
[801] | 2095 | dtiS = drw.datetimeStr_conversion(str(dti) + ',' + timeunit, 'cfTime', \ |
---|
| 2096 | 'Y/m/d H-M-S') |
---|
| 2097 | dteS = drw.datetimeStr_conversion(str(dte) + ',' + timeunit, 'cfTime', \ |
---|
| 2098 | 'Y/m/d H-M-S') |
---|
| 2099 | |
---|
| 2100 | print ' ' + fname + ': plotting from: ' + dtiS + ' to ' + dteS |
---|
| 2101 | |
---|
[194] | 2102 | timepos, timelabels = drw.CFtimes_plot(tvals, timeunit, timekind, timefmt) |
---|
| 2103 | |
---|
[801] | 2104 | # print 'Lluis min/max tval +/- dtval/2:', mintval-dtvals/2., maxtval+dtvals/2.,'dt:', len(tvals) |
---|
| 2105 | # for it in range(len(timepos)): |
---|
| 2106 | # print timepos[it], timelabels[it] |
---|
| 2107 | |
---|
[655] | 2108 | if leglabels != 'None': |
---|
| 2109 | legvals = leglabels.split(',') |
---|
[562] | 2110 | else: |
---|
[655] | 2111 | legvals = None |
---|
| 2112 | |
---|
[679] | 2113 | if pointfreq0 == 'all': |
---|
| 2114 | pointfreq = None |
---|
| 2115 | else: |
---|
| 2116 | pointfreq = int(pointfreq0) |
---|
| 2117 | |
---|
[655] | 2118 | drw.plot_lines_time(dimvalues, varvalues, valuesaxis, dimtit, legvals, vartit, \ |
---|
[684] | 2119 | varunits, timepos, timelabels, title, locleg, graphk, lines, collines, points, \ |
---|
[687] | 2120 | linewidths, pointsizes, pointfreq) |
---|
[655] | 2121 | |
---|
[194] | 2122 | return |
---|
| 2123 | |
---|
[192] | 2124 | def draw_Neighbourghood_evol(filen, values, variable): |
---|
| 2125 | """ Function to draw the temporal evolution of a neighbourghood around a point |
---|
| 2126 | draw_Neighbourghood_evol(filen, values, variable) |
---|
| 2127 | filen= netCDF file name |
---|
| 2128 | values= [gvarname]:[dimsval]:[neigdims]:[Nneig]:[Ncol]:[timetits]:[tkinds]: |
---|
| 2129 | [timefmts]:[gtitle]:[shadxtrms]:[cbar]:[gkind]:[ofile] |
---|
| 2130 | [dimsval]: [dimn1]|[val1]|[dimv1],...,[dimnN]|[valN]|[dimvN] dimension names, values to get |
---|
| 2131 | (-1, for all; no name/value pair given full length) and variable with values of the dimension |
---|
| 2132 | NOTE: when dimsval[X,Y] == neigdims[X,Y], valX,valY --> valX,valY-Nneig/2, valX,valY+Nneig/2 |
---|
| 2133 | [neigdims]: [dimnX],[dimnY] dimensions mnames along which the neigbourghood should be defined |
---|
| 2134 | [Nneig]: Number of grid points of the full side of the box (odd value) |
---|
| 2135 | [Ncol]: Number of columns ('auto': square final plot) |
---|
| 2136 | [gvarname]: name of the variable to appear in the graph |
---|
| 2137 | [timetits]: [titX],[titY] titles of the axes ('|' for spaces) |
---|
| 2138 | [tkinds]: [tkindX]|[tkindY] kinds of time to appear in the graph |
---|
| 2139 | 'Nval': according to a given number of values as 'Nval',[Nval] |
---|
| 2140 | 'exct': according to an exact time unit as 'exct',[tunit]; |
---|
| 2141 | tunit= [Nunits],[tu]; [tu]= 'c': centuries, 'y': year, 'm': month, |
---|
| 2142 | 'w': week, 'd': day, 'h': hour, 'i': minute, 's': second, |
---|
| 2143 | 'l': milisecond |
---|
| 2144 | [timefmts]: [tfmtX],[tfmtY] format of the time labels |
---|
| 2145 | [gtitle]: title of the graphic ('|' for spaces) |
---|
| 2146 | [shadxtrms]: Extremes for the shading |
---|
| 2147 | [cbar]: colorbar to use |
---|
| 2148 | [gkind]: kind of graphical output |
---|
| 2149 | [ofile]: True/False whether the netcdf with data should be created or not |
---|
| 2150 | variable= name of the variable |
---|
| 2151 | values = 'q:Time|-1|Times,bottom_top|6|ZNU,south_north|3|XLAT,west_east|26|XLONG:south_north,west_east:5:auto:time|($[DD]^{[HH]}$),time|($[DD]^{[HH]}$):exct,2,h|exct,1,d:$%d^{%H}$,$%d^{%H}$:5|pts|neighbourghood|temporal|evolution:0.0,0.004:BuPu:pdf:True' |
---|
| 2152 | """ |
---|
| 2153 | |
---|
| 2154 | fname = 'draw_Neighbourghood_evol' |
---|
| 2155 | |
---|
| 2156 | if values == 'h': |
---|
| 2157 | print fname + '_____________________________________________________________' |
---|
| 2158 | print draw_Neighbourghood_evol.__doc__ |
---|
| 2159 | quit() |
---|
| 2160 | |
---|
[690] | 2161 | expectargs = '[gvarname]:[dimsval]:[neigdims]:[Nneig]:[Ncol]:' + \ |
---|
| 2162 | '[timetits]:[tkinds]:[timefmts]:[gtitle]:[shadxtrms]:[cbar]:[gkind]:[ofile]' |
---|
[192] | 2163 | |
---|
[690] | 2164 | drw.check_arguments(fname,values,expectargs,':') |
---|
[192] | 2165 | |
---|
| 2166 | gvarname = values.split(':')[0] |
---|
| 2167 | dimsval = values.split(':')[1].split(',') |
---|
| 2168 | neigdims = values.split(':')[2].split(',') |
---|
| 2169 | Nneig = int(values.split(':')[3]) |
---|
| 2170 | Ncol0 = values.split(':')[4] |
---|
| 2171 | timetits = values.split(':')[5].split(',') |
---|
| 2172 | timekinds = values.split(':')[6].split('|') |
---|
| 2173 | timefmts = values.split(':')[7].split(',') |
---|
| 2174 | gtitle = values.split(':')[8].replace('|',' ') |
---|
| 2175 | shadxtrms = values.split(':')[9].split(',') |
---|
| 2176 | cbar = values.split(':')[10] |
---|
| 2177 | gkind = values.split(':')[11] |
---|
| 2178 | ofile = values.split(':')[12] |
---|
| 2179 | |
---|
| 2180 | if Ncol0 != 'auto': |
---|
| 2181 | Ncol = int(Ncol0) |
---|
| 2182 | else: |
---|
| 2183 | Ncol = Ncol0 |
---|
| 2184 | |
---|
| 2185 | timetits[0] = timetits[0].replace('|',' ') |
---|
| 2186 | timetits[1] = timetits[1].replace('|',' ') |
---|
| 2187 | |
---|
| 2188 | if np.mod(Nneig,2) == 0: |
---|
| 2189 | print errormsg |
---|
| 2190 | print ' ' + fname + ": an odd value for 'Nneig':",Nneig,'is required !!!' |
---|
| 2191 | quit(-1) |
---|
| 2192 | |
---|
| 2193 | Nneig2 = int(Nneig/2) |
---|
| 2194 | |
---|
| 2195 | # Values to slice the variable |
---|
| 2196 | dimvslice = {} |
---|
| 2197 | dimvvalues = {} |
---|
| 2198 | for dimvs in dimsval: |
---|
| 2199 | dimn = dimvs.split('|')[0] |
---|
| 2200 | dimv = int(dimvs.split('|')[1]) |
---|
| 2201 | dimnv = dimvs.split('|')[2] |
---|
| 2202 | |
---|
| 2203 | dimvvalues[dimn] = dimnv |
---|
| 2204 | dimvslice[dimn] = dimv |
---|
| 2205 | |
---|
| 2206 | ncobj = NetCDFFile(filen, 'r') |
---|
| 2207 | |
---|
| 2208 | varobj = ncobj.variables[variable] |
---|
| 2209 | |
---|
| 2210 | slicevar = [] |
---|
| 2211 | newdimn = [] |
---|
| 2212 | newdimsvar = {} |
---|
| 2213 | |
---|
| 2214 | for dimn in varobj.dimensions: |
---|
| 2215 | if not drw.searchInlist(dimvslice.keys(), dimn): |
---|
| 2216 | dimsize = len(ncobj.dimensions[dimn]) |
---|
| 2217 | slicevar.append(slice(0, dimsize+1)) |
---|
| 2218 | newdimn.append(dimn) |
---|
| 2219 | newdimsvar[dimn] = dimseize |
---|
| 2220 | |
---|
| 2221 | for dimslicen in dimvslice.keys(): |
---|
| 2222 | if dimn == dimslicen: |
---|
| 2223 | if dimvslice[dimn] != -1: |
---|
| 2224 | if drw.searchInlist(neigdims, dimn): |
---|
| 2225 | slicevar.append(slice(dimvslice[dimn]-Nneig2, \ |
---|
| 2226 | dimvslice[dimn]+Nneig2+1)) |
---|
| 2227 | newdimn.append(dimn) |
---|
| 2228 | newdimsvar[dimn] = Nneig |
---|
| 2229 | break |
---|
| 2230 | else: |
---|
| 2231 | slicevar.append(slice(dimvslice[dimn], dimvslice[dimn]+1)) |
---|
| 2232 | break |
---|
| 2233 | else: |
---|
| 2234 | dimsize = len(ncobj.dimensions[dimn]) |
---|
| 2235 | slicevar.append(slice(0, dimsize+1)) |
---|
| 2236 | newdimn.append(dimn) |
---|
| 2237 | newdimsvar[dimn] = dimsize |
---|
| 2238 | break |
---|
| 2239 | |
---|
| 2240 | varv = varobj[tuple(slicevar)] |
---|
| 2241 | |
---|
| 2242 | if len(newdimn) != 3: |
---|
| 2243 | print errormsg |
---|
| 2244 | print ' ' + fname + ': sliced variable with shape=', varv.shape, \ |
---|
| 2245 | ' must have three dimensions',len(varv.shape),'given !!' |
---|
| 2246 | quit(-1) |
---|
| 2247 | |
---|
| 2248 | newdims = [] |
---|
| 2249 | for nwdims in newdimn: |
---|
| 2250 | newdims.append(newdimsvar[nwdims]) |
---|
| 2251 | |
---|
| 2252 | # The dimension which is not in the neighbourhood dimensions must be time! |
---|
| 2253 | for dim1 in newdimn: |
---|
| 2254 | if not drw.searchInlist(neigdims, dim1): |
---|
| 2255 | dimt = newdimsvar[dim1] |
---|
| 2256 | dimtime = dim1 |
---|
| 2257 | |
---|
| 2258 | if Ncol == 'auto': |
---|
| 2259 | dimtsqx = int(np.sqrt(dimt)) + 1 |
---|
| 2260 | dimtsqy = int(np.sqrt(dimt)) + 1 |
---|
| 2261 | else: |
---|
| 2262 | dimtsqx = int(Ncol) |
---|
| 2263 | dimtsqy = dimt/dimtsqx + 1 |
---|
| 2264 | |
---|
| 2265 | neighbourghood = np.ones((dimtsqy*Nneig,dimtsqx*Nneig), dtype=np.float)*fillValue |
---|
| 2266 | |
---|
| 2267 | for it in range(dimt): |
---|
| 2268 | ity = int(it/dimtsqx) |
---|
| 2269 | itx = it-ity*dimtsqx |
---|
| 2270 | |
---|
| 2271 | itty = (dimtsqy - ity - 1)*Nneig + Nneig2 |
---|
| 2272 | ittx = itx*Nneig + Nneig2 |
---|
| 2273 | |
---|
| 2274 | neighbourghood[itty-Nneig2:itty+Nneig2+1,ittx-Nneig2:ittx+Nneig2+1]= \ |
---|
| 2275 | varv[it,::-1,:] |
---|
| 2276 | |
---|
| 2277 | variablevals = drw.variables_values(variable) |
---|
| 2278 | if drw.searchInlist(varobj.ncattrs(), 'units'): |
---|
| 2279 | vunits = varobj.units |
---|
| 2280 | else: |
---|
| 2281 | vunits = variablevals[5] |
---|
| 2282 | |
---|
| 2283 | # Time values at the X/Y axes |
---|
| 2284 | if ncobj.variables[dimvvalues[dimtime]].dtype == '|S1': |
---|
| 2285 | print ' ' + fname + ': WRF time variable!' |
---|
| 2286 | refdate = '19491201000000' |
---|
| 2287 | tunitsval = 'hours' |
---|
| 2288 | dimtvalues = np.zeros((dimt), dtype=np.float) |
---|
| 2289 | tvals = ncobj.variables[dimvvalues[dimtime]] |
---|
| 2290 | yrref=refdate[0:4] |
---|
| 2291 | monref=refdate[4:6] |
---|
| 2292 | dayref=refdate[6:8] |
---|
| 2293 | horref=refdate[8:10] |
---|
| 2294 | minref=refdate[10:12] |
---|
| 2295 | secref=refdate[12:14] |
---|
| 2296 | |
---|
| 2297 | refdateS = yrref + '/' + monref + '/' + dayref + '_' + horref + ':' + \ |
---|
| 2298 | minref + ':' + secref |
---|
| 2299 | tunits = tunitsval + ' since ' + refdateS |
---|
| 2300 | for it in range(dimt): |
---|
| 2301 | wrfdates = drw.datetimeStr_conversion(tvals[it,:],'WRFdatetime', 'matYmdHMS') |
---|
| 2302 | dimtvalues[it] = drw.realdatetime1_CFcompilant(wrfdates, refdate, tunitsval) |
---|
| 2303 | else: |
---|
| 2304 | dimtvalues = ncobj.variables[dimvvalues[dimtime]][:] |
---|
| 2305 | tunits = ncobj.variables[newdimsvar[dimtime]].units |
---|
| 2306 | |
---|
| 2307 | dimxv = dimtvalues[0:dimtsqx] |
---|
| 2308 | dimyv = dimtvalues[0:dimt:dimtsqx] |
---|
| 2309 | |
---|
| 2310 | dimn = ['time','time'] |
---|
| 2311 | |
---|
| 2312 | if ofile == 'True': |
---|
| 2313 | ofilen = 'Neighbourghood_evol.nc' |
---|
| 2314 | newnc = NetCDFFile(ofilen, 'w') |
---|
| 2315 | # Dimensions |
---|
| 2316 | newdim = newnc.createDimension('time',None) |
---|
| 2317 | newdim = newnc.createDimension('y',dimtsqy*Nneig) |
---|
| 2318 | newdim = newnc.createDimension('x',dimtsqx*Nneig) |
---|
| 2319 | # Dimension values |
---|
| 2320 | newvar = newnc.createVariable('time','f8',('time')) |
---|
| 2321 | newvar[:] = np.arange(dimt) |
---|
| 2322 | newattr = drw.basicvardef(newvar, 'time','time',tunits) |
---|
| 2323 | # Neighbourhghood variable |
---|
| 2324 | newvar = newnc.createVariable(variable + 'neigevol', 'f4', ('y','x'), \ |
---|
| 2325 | fill_value=fillValue) |
---|
| 2326 | newvar[:] = neighbourghood |
---|
| 2327 | |
---|
| 2328 | newnc.sync() |
---|
| 2329 | newnc.close() |
---|
| 2330 | print fname + ": Successfull generation of file '" + ofilen + "' !!" |
---|
| 2331 | |
---|
| 2332 | # Time ticks |
---|
| 2333 | timeposX, timelabelsX = drw.CFtimes_plot(dimxv, tunits, timekinds[0], timefmts[0]) |
---|
| 2334 | timeposY, timelabelsY = drw.CFtimes_plot(dimyv, tunits, timekinds[1], timefmts[1]) |
---|
| 2335 | |
---|
[232] | 2336 | timepos = [timeposX[0:len(timeposX)], timeposY[len(timeposY):0:-1]] |
---|
| 2337 | timelabels = [timelabelsX[0:len(timeposX)], timelabelsY[0:len(timeposY)]] |
---|
[192] | 2338 | |
---|
| 2339 | for i in range(2): |
---|
| 2340 | if shadxtrms[i][0:1] != 'S': |
---|
| 2341 | shadxtrms[i] = np.float(shadxtrms[i]) |
---|
| 2342 | |
---|
| 2343 | drw.plot_Neighbourghood_evol(neighbourghood, dimxv, dimyv, gvarname, timetits, \ |
---|
| 2344 | timepos, timelabels, cbar, Nneig, shadxtrms, vunits, gtitle, gkind, True) |
---|
| 2345 | |
---|
[545] | 2346 | def draw_points(filen, values): |
---|
| 2347 | """ Function to plot a series of points |
---|
[549] | 2348 | [values]= [ptasciifile]:[gtit]:[mapvalues]:[kindfigure]:[pointcolor]:[pointlabels]: |
---|
| 2349 | [locleg]:[figureko]:[figuren] |
---|
[545] | 2350 | [ptasciifile]:[file],[comchar],[collon],[collat],[lab] |
---|
| 2351 | [file]: column ASCII file with the location of the points |
---|
| 2352 | [comchar]: '|' list of characters for commentaries |
---|
| 2353 | [collon]: number of column with the longitude of the points |
---|
| 2354 | [collat]: number of column with the latitude of the points |
---|
| 2355 | [collab]: number of column with the labels of the points ('None', and will get |
---|
| 2356 | the values from the [pointlabels] variable |
---|
| 2357 | [gtit]: title of the figure ('|' for spaces) |
---|
| 2358 | [mapvalues]: drawing coastaline ([proj],[res]) or None |
---|
| 2359 | [proj]: projection |
---|
| 2360 | * 'cyl', cilindric |
---|
| 2361 | * 'lcc', lambert conformal |
---|
| 2362 | [res]: resolution: |
---|
| 2363 | * 'c', crude |
---|
| 2364 | * 'l', low |
---|
| 2365 | * 'i', intermediate |
---|
| 2366 | * 'h', high |
---|
| 2367 | * 'f', full |
---|
[549] | 2368 | [kindfigure]: kind of figure |
---|
| 2369 | 'legend': only points in the map with the legend with the names |
---|
| 2370 | 'labelled',[txtsize],[txtcol]: points with the names and size, color of text |
---|
[545] | 2371 | [pointcolor]: color for the points ('auto' for "red") |
---|
| 2372 | [pointlabels]: ',' of labels [only used if [collab]='None'] ('None' for no labels) |
---|
| 2373 | [locleg]: location of the legend (0, autmoatic) |
---|
| 2374 | 1: 'upper right', 2: 'upper left', 3: 'lower left', 4: 'lower right', |
---|
| 2375 | 5: 'right', 6: 'center left', 7: 'center right', 8: 'lower center', |
---|
| 2376 | 9: 'upper center', 10: 'center' |
---|
[549] | 2377 | [figureko]: kind of the output file (pdf, png, ...) |
---|
[545] | 2378 | [figuren]: name of the figure |
---|
| 2379 | [filen]= [ncfile],[lonvarn],[latvarn][,[varn],[dimvals],[vargn],[min],[max],[cbar],[varu]] |
---|
| 2380 | [ncfile]: netCDF to use to geolocalize the points |
---|
| 2381 | [lonvarn]: name of the variable with the longitudes |
---|
| 2382 | [latvarn]: name of the variable with the latitudes |
---|
| 2383 | [varn]: optional variable to add staff into the graph |
---|
| 2384 | [dimval]: '@' list of [dimn]|[dimval] to get the values for [varn] |
---|
| 2385 | [dimn]: name of the dimension |
---|
| 2386 | [dimval]: value of the dimension (no value all range) |
---|
| 2387 | [vargn]: name of the variable in the graph |
---|
| 2388 | [min]: minimum value for the extra variable |
---|
| 2389 | [max]: maximum value for the extra variable |
---|
| 2390 | [cbar]: color bar |
---|
| 2391 | [varu]: units of the variable |
---|
| 2392 | """ |
---|
| 2393 | fname = 'draw_points' |
---|
| 2394 | |
---|
| 2395 | if values == 'h': |
---|
| 2396 | print fname + '_____________________________________________________________' |
---|
| 2397 | print draw_points.__doc__ |
---|
| 2398 | quit() |
---|
| 2399 | |
---|
[549] | 2400 | expectargs = '[ptasciifile]:[gtit]:[mapvalues]:[kindfigure]:[pointcolor]:' + \ |
---|
| 2401 | '[pointlabels]:[locleg]:[figurek]:[figuren]' |
---|
[545] | 2402 | |
---|
[690] | 2403 | drw.check_arguments(fname,values,expectargs,':') |
---|
[545] | 2404 | |
---|
| 2405 | ptasciifile = values.split(':')[0] |
---|
| 2406 | gtit = values.split(':')[1] |
---|
| 2407 | mapvalues = values.split(':')[2] |
---|
[549] | 2408 | kindfigure = values.split(':')[3] |
---|
| 2409 | pointcolor = values.split(':')[4] |
---|
| 2410 | pointlabels = values.split(':')[5] |
---|
| 2411 | locleg = int(values.split(':')[6]) |
---|
| 2412 | figureko = values.split(':')[7] |
---|
| 2413 | figuren = values.split(':')[8] |
---|
[545] | 2414 | |
---|
| 2415 | # Getting station locations |
---|
| 2416 | ## |
---|
| 2417 | filev = ptasciifile.split(',')[0] |
---|
| 2418 | comchar = ptasciifile.split(',')[1].split('|') |
---|
| 2419 | collon = int(ptasciifile.split(',')[2]) |
---|
| 2420 | collat = int(ptasciifile.split(',')[3]) |
---|
| 2421 | collab = ptasciifile.split(',')[4] |
---|
| 2422 | |
---|
| 2423 | if not os.path.isfile(filev): |
---|
| 2424 | print errormsg |
---|
| 2425 | print ' ' + fname + ": file '" + filev + "' does not exist!!" |
---|
| 2426 | quit(-1) |
---|
| 2427 | |
---|
| 2428 | # Getting points position and labels |
---|
| 2429 | oascii = open(filev, 'r') |
---|
| 2430 | xptval = [] |
---|
| 2431 | yptval = [] |
---|
| 2432 | if collab != 'None': |
---|
| 2433 | ptlabels = [] |
---|
| 2434 | for line in oascii: |
---|
| 2435 | if not drw.searchInlist(comchar, line[0:1]): |
---|
| 2436 | linevals = drw.reduce_spaces(line) |
---|
| 2437 | xptval.append(np.float(linevals[collon].replace('\n',''))) |
---|
| 2438 | yptval.append(np.float(linevals[collat].replace('\n',''))) |
---|
| 2439 | ptlabels.append(linevals[int(collab)].replace('\n','')) |
---|
| 2440 | else: |
---|
| 2441 | ptlabels = None |
---|
| 2442 | for line in oascii: |
---|
| 2443 | if not drw.searchInlist(comchar, line[0:1]): |
---|
| 2444 | linevals = drw.reduce_spaces(line) |
---|
| 2445 | xptval.append(np.float(linevals[collon].replace('\n',''))) |
---|
| 2446 | yptval.append(np.float(linevals[collat].replace('\n',''))) |
---|
| 2447 | |
---|
| 2448 | oascii.close() |
---|
| 2449 | |
---|
| 2450 | if pointlabels != 'None' and collab == 'None': |
---|
| 2451 | ptlabels = pointlabels.split(',') |
---|
| 2452 | |
---|
| 2453 | # Getting localization of the points |
---|
| 2454 | ## |
---|
| 2455 | filev = filen.split(',') |
---|
| 2456 | Nvals = len(filev) |
---|
| 2457 | |
---|
| 2458 | ncfile = filev[0] |
---|
| 2459 | lonvarn = filev[1] |
---|
| 2460 | latvarn = filev[2] |
---|
| 2461 | varn = None |
---|
| 2462 | varextrav = None |
---|
| 2463 | if Nvals == 10: |
---|
| 2464 | varn = filev[3] |
---|
| 2465 | dimvals = filev[4] |
---|
| 2466 | varextrav = [filev[5], np.float(filev[6]), np.float(filev[7]), filev[8], \ |
---|
| 2467 | filev[9]] |
---|
| 2468 | |
---|
| 2469 | if not os.path.isfile(ncfile): |
---|
| 2470 | print errormsg |
---|
| 2471 | print ' ' + fname + ": file '" + ncfile + "' does not exist!!" |
---|
| 2472 | quit(-1) |
---|
| 2473 | |
---|
| 2474 | onc = NetCDFFile(ncfile, 'r') |
---|
| 2475 | |
---|
| 2476 | lonv, latv = drw.lonlat2D(onc.variables[lonvarn], onc.variables[latvarn]) |
---|
| 2477 | |
---|
| 2478 | if varn is not None: |
---|
| 2479 | objextra = onc.variables[varn] |
---|
| 2480 | vard = objextra.dimensions |
---|
| 2481 | dd = {} |
---|
| 2482 | for dn in dimvals.split('@'): |
---|
| 2483 | ddn = dn.split('|')[0] |
---|
| 2484 | ddv = dn.split('|')[1] |
---|
| 2485 | dd[ddn] = ddv |
---|
| 2486 | slicevar = [] |
---|
| 2487 | for dv in vard: |
---|
| 2488 | found= False |
---|
| 2489 | for dn in dd.keys(): |
---|
| 2490 | if dn == dv: |
---|
| 2491 | slicevar.append(int(dd[dn])) |
---|
| 2492 | found = True |
---|
| 2493 | break |
---|
| 2494 | if not found: |
---|
| 2495 | slicevar.append(slice(0,len(onc.dimensions[dv]))) |
---|
| 2496 | |
---|
| 2497 | varextra = np.squeeze(objextra[tuple(slicevar)]) |
---|
| 2498 | |
---|
[585] | 2499 | if mapvalues == 'None': |
---|
| 2500 | mapV = None |
---|
| 2501 | else: |
---|
| 2502 | mapV = mapvalues |
---|
| 2503 | |
---|
| 2504 | drw.plot_points(xptval, yptval, lonv, latv, varextra, varextrav, gtit, mapV, \ |
---|
[549] | 2505 | kindfigure, pointcolor, ptlabels, locleg, figureko, figuren) |
---|
[545] | 2506 | |
---|
| 2507 | onc.close() |
---|
| 2508 | |
---|
| 2509 | return |
---|
| 2510 | |
---|
[665] | 2511 | def draw_points_lonlat(filen, values): |
---|
| 2512 | """ Function to plot a series of lon/lat points |
---|
| 2513 | filen= name of the file |
---|
[668] | 2514 | values= [lonvarname]:[latvarname]:[gkind]:[gtit]:[ptcolor]:[pttype]:[ptsize]:[labels]:[locleg]:[figureK] |
---|
[665] | 2515 | [lonvarname]: name of the variable longitude |
---|
| 2516 | [latvarname]: name of the variable latitude |
---|
| 2517 | [gkind]: kind of graphical output |
---|
| 2518 | [gtit]: graphic title '!' for spaces |
---|
| 2519 | [ptcolor]: color of the points ('auto', for "red") |
---|
| 2520 | [pttype]: type of point |
---|
[668] | 2521 | [ptsize]: size of point |
---|
[665] | 2522 | [labels]: ',' list of labels to use |
---|
| 2523 | [locleg]: location of the legend (0, automatic) |
---|
| 2524 | 1: 'upper right', 2: 'upper left', 3: 'lower left', 4: 'lower right', |
---|
| 2525 | 5: 'right', 6: 'center left', 7: 'center right', 8: 'lower center', |
---|
| 2526 | 9: 'upper center', 10: 'center' |
---|
| 2527 | [figureK]= kind of figure |
---|
| 2528 | 'legend': only points in the map with the legend with the names |
---|
| 2529 | 'labelled',[txtsize],[txtcol]: points with the names and size, color of text |
---|
| 2530 | """ |
---|
| 2531 | fname = 'draw_points_lonlat' |
---|
| 2532 | |
---|
| 2533 | if values == 'h': |
---|
| 2534 | print fname + '_____________________________________________________________' |
---|
| 2535 | print draw_points_lonlat.__doc__ |
---|
| 2536 | quit() |
---|
| 2537 | |
---|
| 2538 | expectargs = '[lonvarname]:[latvarname]:[gkind]:[gtit]:[ptcolor]:[pttype]:' + \ |
---|
[668] | 2539 | '[ptsize]:[labels]:[locleg]:[figureK]' |
---|
[665] | 2540 | |
---|
[690] | 2541 | drw.check_arguments(fname,values,expectargs,':') |
---|
[665] | 2542 | |
---|
| 2543 | lonname = values.split(':')[0] |
---|
| 2544 | latname = values.split(':')[1] |
---|
| 2545 | kindfigure = values.split(':')[2] |
---|
| 2546 | gtit = values.split(':')[3].replace('!',' ') |
---|
| 2547 | pointcolor = values.split(':')[4] |
---|
| 2548 | pointtype = values.split(':')[5] |
---|
[668] | 2549 | pointsize = np.float(values.split(':')[6]) |
---|
| 2550 | labelsv = values.split(':')[7] |
---|
| 2551 | loclegend = values.split(':')[8] |
---|
| 2552 | figureK = values.split(':')[9] |
---|
[665] | 2553 | |
---|
| 2554 | fname = 'points_lonlat' |
---|
| 2555 | |
---|
| 2556 | onc = NetCDFFile(filen, 'r') |
---|
| 2557 | if not onc.variables.has_key(lonname): |
---|
[668] | 2558 | print errormsg |
---|
[665] | 2559 | print fname + ": file '" + filen + "' does not have longitudes '" + lonname +\ |
---|
| 2560 | "' !!" |
---|
| 2561 | quit(-1) |
---|
| 2562 | if not onc.variables.has_key(lonname): |
---|
[668] | 2563 | print errormsg |
---|
[665] | 2564 | print fname + ": file '" + filen + "' does not have longitudes '" + lonname +\ |
---|
| 2565 | "' !!" |
---|
| 2566 | quit(-1) |
---|
| 2567 | |
---|
| 2568 | olon = onc.variables[lonname] |
---|
| 2569 | olat = onc.variables[latname] |
---|
| 2570 | |
---|
[668] | 2571 | Ndimlon = len(olon.shape) |
---|
| 2572 | if Ndimlon == 1: |
---|
[688] | 2573 | dx = olon.shape[0] |
---|
| 2574 | dy = olat.shape[0] |
---|
| 2575 | if dx == dy: |
---|
| 2576 | lonvals = olon[:] |
---|
| 2577 | latvals = olat[:] |
---|
| 2578 | else: |
---|
| 2579 | lonvals0 = np.zeros((dy,dx), dtype=np.float) |
---|
| 2580 | latvals0 = np.zeros((dy,dx), dtype=np.float) |
---|
| 2581 | for iL in range(dy): |
---|
| 2582 | lonvals0[iL,:] = olon[:] |
---|
| 2583 | for il in range(dx): |
---|
| 2584 | latvals0[:,il] = olat[:] |
---|
| 2585 | lonvals = lonvals0.flatten() |
---|
| 2586 | latvals = latvals0.flatten() |
---|
| 2587 | |
---|
[668] | 2588 | elif Ndimlon == 2: |
---|
| 2589 | lonvals = olon[:].flatten() |
---|
| 2590 | latvals = olat[:].flatten() |
---|
| 2591 | elif Ndimlon == 3: |
---|
| 2592 | lonvals = olon[1,:,:].flatten() |
---|
| 2593 | latvals = olat[1,:,:].flatten() |
---|
| 2594 | # Playing for Anna |
---|
| 2595 | # lonvals = olon[:].flatten() |
---|
| 2596 | # latvals = olat[:].flatten() |
---|
| 2597 | elif Ndimlon == 4: |
---|
| 2598 | lonvals = olon[1,0,:,:].flatten() |
---|
| 2599 | latvals = olat[1,0,:,:].flatten() |
---|
| 2600 | else: |
---|
| 2601 | print errormsg |
---|
| 2602 | print ' ' + fname + ': longitude size:',len(olon),' not ready!!' |
---|
| 2603 | quit(-1) |
---|
| 2604 | |
---|
[665] | 2605 | if labelsv == 'None': |
---|
| 2606 | labels = None |
---|
| 2607 | else: |
---|
| 2608 | labels = labelsv.split(',') |
---|
| 2609 | |
---|
[668] | 2610 | drw.plot_list_points(lonvals, latvals, lonname, latname, gtit, figureK, pointcolor, pointtype, \ |
---|
| 2611 | pointsize, labels, loclegend, kindfigure, fname) |
---|
[665] | 2612 | |
---|
| 2613 | onc.close() |
---|
| 2614 | |
---|
| 2615 | return |
---|
| 2616 | |
---|
[192] | 2617 | def draw_timeSeries(filen, values, variables): |
---|
| 2618 | """ Function to draw a time-series |
---|
| 2619 | draw_timeSeries(filen, values, variable): |
---|
| 2620 | filen= name of the file |
---|
[678] | 2621 | values= [gvarname]:[timetit]:[tkind]:[timefmt]:[title]:[locleg]:[gkind]:[colorlines]:[pointtype]:[pointfreq] |
---|
[192] | 2622 | [gvarname]: name of the variable to appear in the graph |
---|
| 2623 | [timetit]: title of the time axis (assumed x-axis, '|' for spaces) |
---|
| 2624 | [tkind]: kind of time to appear in the graph (assumed x-axis) |
---|
| 2625 | 'Nval': according to a given number of values as 'Nval',[Nval] |
---|
| 2626 | 'exct': according to an exact time unit as 'exct',[tunit]; |
---|
| 2627 | tunit= [Nunits],[tu]; [tu]= 'c': centuries, 'y': year, 'm': month, |
---|
| 2628 | 'w': week, 'd': day, 'h': hour, 'i': minute, 's': second, |
---|
| 2629 | 'l': milisecond |
---|
| 2630 | [timefmt]: format of the time labels |
---|
| 2631 | [title]: title of the graphic ('|' for spaces) |
---|
[655] | 2632 | [locleg]: location of the legend (0, automatic) |
---|
[192] | 2633 | 1: 'upper right', 2: 'upper left', 3: 'lower left', 4: 'lower right', |
---|
| 2634 | 5: 'right', 6: 'center left', 7: 'center right', 8: 'lower center', |
---|
| 2635 | 9: 'upper center', 10: 'center' |
---|
| 2636 | [gkind]: kind of graphical output |
---|
[673] | 2637 | [colorlines]: ',' list of colors for the lines, None for automatic, single |
---|
| 2638 | value all the same |
---|
| 2639 | [pointtype]: ',' list of type of points for the lines, None for automatic, single |
---|
| 2640 | value all the same |
---|
[678] | 2641 | [pointfreq]: frequency of point plotting, 'all' for all time steps |
---|
[192] | 2642 | variables= [varname],[timename] names of variable and variable with times |
---|
| 2643 | draw_timeSeries('wrfout_d01_1979-12-01_00:00:00_bottom_top_B6-E6-I1_south_north_B3-E3-I1_west_east_B26-E26-I1.nc', 'dt_con:time|($[DD]^{[HH]}$):exct,12,h:$%d^{%H}$:time|evolution|at|-1|6|3|26:1:pdf', 'LDQCON,time') |
---|
| 2644 | """ |
---|
| 2645 | |
---|
| 2646 | fname = 'draw_timeSeries' |
---|
| 2647 | |
---|
| 2648 | if values == 'h': |
---|
| 2649 | print fname + '_____________________________________________________________' |
---|
| 2650 | print draw_timeSeries.__doc__ |
---|
| 2651 | quit() |
---|
| 2652 | |
---|
[690] | 2653 | expectargs = '[gvarname]:[timetit]:[tkind]:[timefmt]:[title]:' + \ |
---|
| 2654 | '[locleg]:[gkind]:[colorlines]:[pointtype]:[pointfreq]' |
---|
[192] | 2655 | |
---|
[690] | 2656 | drw.check_arguments(fname,values,expectargs,':') |
---|
[192] | 2657 | |
---|
| 2658 | gvarname = values.split(':')[0] |
---|
| 2659 | timetit = values.split(':')[1].replace('|',' ') |
---|
| 2660 | tkind = values.split(':')[2] |
---|
| 2661 | timefmt = values.split(':')[3] |
---|
| 2662 | title = values.split(':')[4].replace('|',' ') |
---|
| 2663 | locleg = int(values.split(':')[5]) |
---|
| 2664 | gkind = values.split(':')[6] |
---|
[673] | 2665 | colorlines = values.split(':')[7] |
---|
| 2666 | pointtype = values.split(':')[8] |
---|
[678] | 2667 | pointfreq0 = values.split(':')[9] |
---|
[192] | 2668 | |
---|
| 2669 | ncobj = NetCDFFile(filen, 'r') |
---|
| 2670 | |
---|
| 2671 | variable = variables.split(',')[0] |
---|
| 2672 | timevar = variables.split(',')[1] |
---|
| 2673 | |
---|
| 2674 | if not ncobj.variables.has_key(variable): |
---|
| 2675 | print errormsg |
---|
| 2676 | print ' ' + fname + ": file '" + filen + "' does not have variable '" + \ |
---|
| 2677 | variable + "' !!" |
---|
| 2678 | quit(-1) |
---|
| 2679 | |
---|
| 2680 | if not ncobj.variables.has_key(timevar): |
---|
| 2681 | print errormsg |
---|
| 2682 | print ' ' + fname + ": file '" + filen + "' does not have variable time '" \ |
---|
| 2683 | + timevar + "' !!" |
---|
| 2684 | quit(-1) |
---|
| 2685 | |
---|
| 2686 | varobj = ncobj.variables[variable] |
---|
| 2687 | timeobj = ncobj.variables[timevar] |
---|
| 2688 | |
---|
| 2689 | dimt = len(timeobj[:]) |
---|
| 2690 | varvals = np.zeros((2,dimt), dtype=np.float) |
---|
| 2691 | |
---|
| 2692 | gunits = varobj.getncattr('units') |
---|
| 2693 | tunits = timeobj.getncattr('units') |
---|
| 2694 | |
---|
| 2695 | varvals[0,:], valpot, newgunits, Spot = drw.pot_values(varobj[:].flatten(), gunits) |
---|
| 2696 | varvals[1,:] = timeobj[:] |
---|
| 2697 | |
---|
| 2698 | tseriesvals = [] |
---|
| 2699 | tseriesvals.append(varvals) |
---|
| 2700 | |
---|
[673] | 2701 | if colorlines == 'None': |
---|
| 2702 | collines = None |
---|
| 2703 | else: |
---|
| 2704 | collines = colorlines.split(',') |
---|
| 2705 | if pointtype == 'None': |
---|
| 2706 | pttype = None |
---|
| 2707 | else: |
---|
| 2708 | pttype = pointtype.split(',') |
---|
| 2709 | |
---|
[678] | 2710 | if pointfreq0 == 'all': |
---|
| 2711 | pointfreq = None |
---|
| 2712 | else: |
---|
| 2713 | pointfreq = int(pointfreq0) |
---|
| 2714 | |
---|
[192] | 2715 | drw.plot_TimeSeries(tseriesvals, Spot + drw.units_lunits(gunits), tunits, \ |
---|
[678] | 2716 | 'TimeSeries', gvarname, timetit, tkind, timefmt, title, \ |
---|
| 2717 | gvarname.replace('_','\_'), locleg, gkind, collines, pttype, pointfreq) |
---|
[192] | 2718 | |
---|
| 2719 | return |
---|
| 2720 | |
---|
[673] | 2721 | #draw_timeSeries('wrfout_d01_1979-12-01_00:00:00_bottom_top_B6-E6-I1_south_north_B3-E3-I1_west_east_B26-E26-I1.nc', 'dt_con:time|($[DD]^{[HH]}$):exct,12,h:$%d^{%H}$:time|evolution|at|-1|6|3|26:1:pdf:None:None', 'LDQCON,time') |
---|
[192] | 2722 | |
---|
| 2723 | def draw_trajectories(trjfilens, values, observations): |
---|
| 2724 | """ Function to draw different trajectories at the same time |
---|
| 2725 | draw_trajectories(trjfilens, values, observations): |
---|
| 2726 | trjfilens= [filen]@[Tint]@[map] ',' separated list of files with trajectories, |
---|
| 2727 | time intervals and reference maps (first one will be used to plot) |
---|
| 2728 | [filen]: name of the file to use (lines with '#', not readed) as: |
---|
| 2729 | [t-step] [x] [y] |
---|
| 2730 | [Tint]: interval of time as [Tbeg]@[Tend] or -1 for all the interval |
---|
| 2731 | [map]: [file]#[lonname]#[latname] |
---|
| 2732 | [file]; with the [lon],[lat] matrices |
---|
| 2733 | [lonname],[latname]; names of the longitudes and latitudes variables |
---|
| 2734 | values=[leglabels]|[lonlatlims]|[title]|[graphk]|[mapkind] |
---|
| 2735 | [leglabels]: ',' separated list of names for the legend |
---|
[422] | 2736 | [lonlatlims]: ',' list of limits of the map [lonmin, latmin, lonmax, latmax] or None |
---|
| 2737 | [title]: title of the plot ('!' for spaces) |
---|
[192] | 2738 | [graphk]: kind of the graphic |
---|
| 2739 | [mapkind]: drawing coastaline ([proj],[res]) or None |
---|
| 2740 | [proj]: projection |
---|
| 2741 | * 'cyl', cilindric |
---|
| 2742 | * 'lcc', lambert conformal |
---|
| 2743 | [res]: resolution: |
---|
| 2744 | * 'c', crude |
---|
| 2745 | * 'l', low |
---|
| 2746 | * 'i', intermediate |
---|
| 2747 | * 'h', high |
---|
| 2748 | * 'f', full |
---|
| 2749 | obsevations= [obsfile],[obsname],[Tint],[null] |
---|
| 2750 | [obsfile]: name fo the File with the observations as [t-step] [lat] [lon] |
---|
| 2751 | [obsname]: name of the observations in the graph |
---|
| 2752 | [Tint]: interval of time as [Tbeg]@[Tend] or -1 for all the interval |
---|
| 2753 | [null]: null value for the observed trajectory |
---|
| 2754 | """ |
---|
| 2755 | |
---|
| 2756 | fname = 'draw_trajectories' |
---|
| 2757 | |
---|
| 2758 | if values == 'h': |
---|
| 2759 | print fname + '_____________________________________________________________' |
---|
| 2760 | print draw_trajectories.__doc__ |
---|
| 2761 | quit() |
---|
| 2762 | |
---|
[422] | 2763 | expectargs = '[leglabels]|[lonlatlims]|[title]|[graphk]|[mapkind]' |
---|
| 2764 | |
---|
[690] | 2765 | drw.check_arguments(fname,values,expectargs,'|') |
---|
[422] | 2766 | |
---|
[192] | 2767 | trjfiles = trjfilens.split(',') |
---|
| 2768 | leglabels = values.split('|')[0] |
---|
| 2769 | lonlatlims = values.split('|')[1] |
---|
[422] | 2770 | title = values.split('|')[2].replace('!',' ') |
---|
[192] | 2771 | graphk = values.split('|')[3] |
---|
| 2772 | mapkind = values.split('|')[4] |
---|
| 2773 | |
---|
| 2774 | Nfiles = len(trjfiles) |
---|
| 2775 | |
---|
| 2776 | # Getting trajectotries |
---|
| 2777 | ## |
---|
| 2778 | |
---|
| 2779 | lontrjvalues = [] |
---|
| 2780 | lattrjvalues = [] |
---|
| 2781 | |
---|
| 2782 | print ' ' + fname |
---|
| 2783 | ifn = 0 |
---|
| 2784 | for ifile in trjfiles: |
---|
| 2785 | filen = ifile.split('@')[0] |
---|
| 2786 | Tint = ifile.split('@')[1] |
---|
| 2787 | |
---|
| 2788 | print ' trajectory:',filen |
---|
| 2789 | |
---|
| 2790 | if Tint != '-1': |
---|
| 2791 | Tbeg = Tint |
---|
| 2792 | Tend = ifile.split('@')[2] |
---|
| 2793 | mapv = ifile.split('@')[3] |
---|
| 2794 | else: |
---|
| 2795 | mapv = ifile.split('@')[2] |
---|
| 2796 | |
---|
| 2797 | if not os.path.isfile(filen): |
---|
| 2798 | print errormsg |
---|
| 2799 | print ' ' + fname + ": trajectory file '" + filen + "' does not exist !!" |
---|
| 2800 | quit(-1) |
---|
| 2801 | |
---|
| 2802 | # Charging longitude and latitude values |
---|
| 2803 | ## |
---|
| 2804 | lonvals, latvals = drw.lonlat_values(mapv.split('#')[0], mapv.split('#')[1], \ |
---|
| 2805 | mapv.split('#')[2]) |
---|
| 2806 | |
---|
| 2807 | if ifn == 0: mapref = mapv |
---|
| 2808 | ifn = ifn + 1 |
---|
| 2809 | |
---|
| 2810 | objfile = open(filen, 'r') |
---|
| 2811 | trjtimev = [] |
---|
| 2812 | trjxv = [] |
---|
| 2813 | trjyv = [] |
---|
| 2814 | |
---|
| 2815 | for line in objfile: |
---|
| 2816 | if line[0:1] != '#': |
---|
| 2817 | trjtimev.append(int(line.split(' ')[0])) |
---|
| 2818 | trjxv.append(int(line.split(' ')[1])) |
---|
| 2819 | trjyv.append(int(line.split(' ')[2])) |
---|
| 2820 | |
---|
| 2821 | objfile.close() |
---|
| 2822 | |
---|
| 2823 | if Tint != '-1': |
---|
| 2824 | lontrjvalues.append(lonvals[trjyv[Tint:Tend+1], trjxv[Tint:Tend+1]]) |
---|
| 2825 | lattrjvalues.append(latvals[trjyv[Tint:Tend+1], trjxv[Tint:Tend+1]]) |
---|
| 2826 | else: |
---|
| 2827 | lontrjvalues.append(lonvals[trjyv[:], trjxv[:]]) |
---|
| 2828 | lattrjvalues.append(latvals[trjyv[:], trjxv[:]]) |
---|
| 2829 | |
---|
| 2830 | # lonlatlimits |
---|
| 2831 | ## |
---|
| 2832 | |
---|
| 2833 | if lonlatlims == 'None': |
---|
| 2834 | lonlatlimsv = None |
---|
| 2835 | else: |
---|
| 2836 | lonlatlimsv = np.zeros((4), dtype=np.float) |
---|
| 2837 | lonlatlimsv[0] = np.float(lonlatlims.split(',')[0]) |
---|
| 2838 | lonlatlimsv[1] = np.float(lonlatlims.split(',')[1]) |
---|
| 2839 | lonlatlimsv[2] = np.float(lonlatlims.split(',')[2]) |
---|
| 2840 | lonlatlimsv[3] = np.float(lonlatlims.split(',')[3]) |
---|
| 2841 | |
---|
| 2842 | # lon/lat objects |
---|
| 2843 | ## |
---|
| 2844 | objnc = NetCDFFile(mapref.split('#')[0]) |
---|
| 2845 | lonobj = objnc.variables[mapref.split('#')[1]] |
---|
| 2846 | latobj = objnc.variables[mapref.split('#')[2]] |
---|
| 2847 | |
---|
| 2848 | # map |
---|
| 2849 | ## |
---|
| 2850 | if mapkind == 'None': |
---|
| 2851 | mapkindv = None |
---|
| 2852 | else: |
---|
| 2853 | mapkindv = mapkind |
---|
| 2854 | |
---|
| 2855 | if observations is None: |
---|
| 2856 | obsname = None |
---|
| 2857 | else: |
---|
| 2858 | obsfile = observations.split(',')[0] |
---|
| 2859 | obsname = observations.split(',')[1] |
---|
| 2860 | Tint = observations.split(',')[2] |
---|
| 2861 | null = np.float(observations.split(',')[3]) |
---|
| 2862 | print ' observational trajectory:',obsfile |
---|
| 2863 | |
---|
| 2864 | if not os.path.isfile(obsfile): |
---|
| 2865 | print errormsg |
---|
| 2866 | print ' ' + fname + ": observations trajectory file '" + obsfile + \ |
---|
| 2867 | "' does not exist !!" |
---|
| 2868 | quit(-1) |
---|
| 2869 | |
---|
| 2870 | objfile = open(obsfile, 'r') |
---|
| 2871 | obstrjtimev = [] |
---|
| 2872 | obstrjxv = [] |
---|
| 2873 | obstrjyv = [] |
---|
| 2874 | |
---|
| 2875 | for line in objfile: |
---|
| 2876 | if line[0:1] != '#': |
---|
| 2877 | lon = np.float(line.split(' ')[2]) |
---|
| 2878 | lat = np.float(line.split(' ')[1]) |
---|
| 2879 | if not lon == null and not lat == null: |
---|
| 2880 | obstrjtimev.append(int(line.split(' ')[0])) |
---|
| 2881 | obstrjxv.append(lon) |
---|
| 2882 | obstrjyv.append(lat) |
---|
| 2883 | else: |
---|
| 2884 | obstrjtimev.append(int(line.split(' ')[0])) |
---|
| 2885 | obstrjxv.append(None) |
---|
| 2886 | obstrjyv.append(None) |
---|
| 2887 | |
---|
| 2888 | objfile.close() |
---|
| 2889 | |
---|
| 2890 | if Tint != '-1': |
---|
| 2891 | Tint = int(observations.split(',')[2].split('@')[0]) |
---|
| 2892 | Tend = int(observations.split(',')[2].split('@')[1]) |
---|
| 2893 | lontrjvalues.append(obstrjxv[Tint:Tend+1]) |
---|
| 2894 | lattrjvalues.append(obstrjyv[Tint:Tend+1]) |
---|
| 2895 | else: |
---|
| 2896 | lontrjvalues.append(obstrjxv[:]) |
---|
| 2897 | lattrjvalues.append(obstrjyv[:]) |
---|
| 2898 | |
---|
| 2899 | drw.plot_Trajectories(lontrjvalues, lattrjvalues, leglabels.split(','), \ |
---|
| 2900 | lonobj, latobj, lonlatlimsv, title, graphk, mapkindv, obsname) |
---|
| 2901 | |
---|
| 2902 | objnc.close() |
---|
| 2903 | |
---|
| 2904 | return |
---|
| 2905 | |
---|
| 2906 | def draw_vals_trajectories(ncfile, values, variable): |
---|
| 2907 | """ Function to draw values from the outputs from 'compute_tevolboxtraj' |
---|
| 2908 | draw_vals_trajectories(ncfile, values, variable) |
---|
| 2909 | ncfile= [ncfile] ',' list of files to use |
---|
[440] | 2910 | values= [statisticskind]:[Tint]:[labels]@[locleg]:[gvarname]:[timetit]:[tkind]:[timefmt]:[title]:[gkind] |
---|
| 2911 | [statisticskind]=[statistics][kind] |
---|
| 2912 | [statistics]: which statistics to use, from: 'center', 'min', 'max', 'mean', |
---|
[192] | 2913 | 'mean2', 'stdev' |
---|
[440] | 2914 | [kind]: 'box', 'circle' statistics taking the values from a box or a circle |
---|
| 2915 | 'trj': value following the trajectory |
---|
[192] | 2916 | [Tint]: [Tbeg]@[Tend] or None, interval of time to plot or -1 for all the times |
---|
| 2917 | [labels]: ',' separated list of labels for the legend |
---|
[655] | 2918 | [locleg]: location of the legend (0, automatic) |
---|
[192] | 2919 | 1: 'upper right', 2: 'upper left', 3: 'lower left', 4: 'lower right', |
---|
| 2920 | 5: 'right', 6: 'center left', 7: 'center right', 8: 'lower center', |
---|
| 2921 | 9: 'upper center', 10: 'center' |
---|
| 2922 | [gvarname]: name of the variable to appear in the graph |
---|
| 2923 | [timetit]: title of the time axis (assumed x-axis, '|' for spaces) |
---|
| 2924 | [tkind]: kind of time to appear in the graph (assumed x-axis) |
---|
| 2925 | 'Nval': according to a given number of values as 'Nval',[Nval] |
---|
| 2926 | 'exct': according to an exact time unit as 'exct',[tunit]; |
---|
| 2927 | tunit= [Nunits],[tu]; [tu]= 'c': centuries, 'y': year, 'm': month, |
---|
| 2928 | 'w': week, 'd': day, 'h': hour, 'i': minute, 's': second, |
---|
| 2929 | 'l': milisecond |
---|
| 2930 | [timefmt]: format of the time labels |
---|
| 2931 | [title]: title of the graphic ('|' for spaces) |
---|
| 2932 | [gkind]: kind of graphical output |
---|
| 2933 | variable= variable to use |
---|
| 2934 | """ |
---|
| 2935 | |
---|
| 2936 | fname = 'draw_vals_trajectories' |
---|
| 2937 | |
---|
| 2938 | if values == 'h': |
---|
| 2939 | print fname + '_____________________________________________________________' |
---|
| 2940 | print draw_vals_trajectories.__doc__ |
---|
| 2941 | quit() |
---|
| 2942 | |
---|
| 2943 | sims = ncfile.split(',') |
---|
| 2944 | |
---|
| 2945 | if len(values.split(':')) != 9: |
---|
| 2946 | print errormsg |
---|
| 2947 | print ' ' + fname + ': wrong number of values!', len(values.split(':')), \ |
---|
| 2948 | 'given 9 needed!!' |
---|
| 2949 | print ' ',values.split(':') |
---|
| 2950 | quit(-1) |
---|
| 2951 | |
---|
[440] | 2952 | statisticskind = values.split(':')[0] |
---|
[192] | 2953 | Tint = values.split(':')[1] |
---|
| 2954 | labels = values.split(':')[2] |
---|
| 2955 | gvarname = values.split(':')[3] |
---|
| 2956 | timetit = values.split(':')[4].replace('|',' ') |
---|
| 2957 | tkind = values.split(':')[5] |
---|
| 2958 | timefmt = values.split(':')[6] |
---|
| 2959 | title = values.split(':')[7].replace('|',' ') |
---|
| 2960 | gkind = values.split(':')[8] |
---|
| 2961 | |
---|
| 2962 | leglabels = labels.split('@')[0].split(',') |
---|
| 2963 | locleg = int(labels.split('@')[1]) |
---|
| 2964 | |
---|
| 2965 | Nsims = len(sims) |
---|
| 2966 | |
---|
| 2967 | if Tint != '-1': |
---|
| 2968 | tini = np.float(Tint.split('@')[0]) |
---|
| 2969 | tend = np.float(Tint.split('@')[1]) |
---|
| 2970 | else: |
---|
| 2971 | tini = -1. |
---|
| 2972 | tend = -1. |
---|
| 2973 | |
---|
| 2974 | vartimetrjv = [] |
---|
| 2975 | |
---|
| 2976 | print ' ' + fname |
---|
| 2977 | for trjfile in sims: |
---|
| 2978 | print ' ' + trjfile + ' ...' |
---|
| 2979 | if not os.path.isfile(trjfile): |
---|
| 2980 | print errormsg |
---|
| 2981 | print ' ' + fname + ": trajectory file: '" + trjfile + \ |
---|
| 2982 | "' does not exist !!" |
---|
| 2983 | quit(-1) |
---|
| 2984 | |
---|
| 2985 | trjobj = NetCDFFile(trjfile, 'r') |
---|
| 2986 | otim = trjobj.variables['time'] |
---|
[440] | 2987 | if not trjobj.variables.has_key(statisticskind + '_' + variable): |
---|
[192] | 2988 | print errormsg |
---|
| 2989 | print ' ' + fname + ": file '" + trjfile + "' does not have variable '"+\ |
---|
[440] | 2990 | statisticskind + '_' + variable + "' !!" |
---|
[192] | 2991 | quit(-1) |
---|
[440] | 2992 | ovar = trjobj.variables[statisticskind + '_' + variable] |
---|
[192] | 2993 | dimt = otim.shape[0] |
---|
| 2994 | |
---|
| 2995 | if trjfile == sims[0]: |
---|
| 2996 | gunits = ovar.getncattr('units') |
---|
| 2997 | lname = ovar.getncattr('long_name') |
---|
| 2998 | tunits = otim.getncattr('units') |
---|
| 2999 | |
---|
| 3000 | if tini != -1: |
---|
| 3001 | tiniid = -1 |
---|
| 3002 | tendid = -1 |
---|
| 3003 | for itv in range(dimt): |
---|
| 3004 | if otim[itv] <= tini and otim[itv+1] >= tini: tiniid = itv |
---|
| 3005 | if otim[itv] <= tend and otim[itv+1] >= tend: tendid = itv |
---|
| 3006 | |
---|
| 3007 | if tiniid == -1 or tendid == -1: |
---|
| 3008 | print errormsg |
---|
| 3009 | print ' ' + main + ' time interval ', tini,',',tend,' not found: ', \ |
---|
| 3010 | tendid, ',', tiniid, ' !!' |
---|
| 3011 | print ' data interval [',otim[0], otim[dimt-1],']' |
---|
| 3012 | quit(-1) |
---|
| 3013 | dimt = tendid - tiniid + 1 |
---|
| 3014 | |
---|
| 3015 | else: |
---|
| 3016 | dimt = otim.shape[0] |
---|
| 3017 | |
---|
| 3018 | valsv = np.zeros((2,dimt), dtype=np.float) |
---|
[446] | 3019 | # Checking for time consistency |
---|
| 3020 | if otim.getncattr('units') != tunits: |
---|
| 3021 | print warnmsg |
---|
| 3022 | print ' ' + fname + ': different time units in the plot!!' |
---|
| 3023 | newtimes = drw.coincident_CFtimes(otim[:], tunits, otim.getncattr('units')) |
---|
| 3024 | else: |
---|
| 3025 | newtimes = otim[:] |
---|
[192] | 3026 | |
---|
| 3027 | if tini == -1: |
---|
[446] | 3028 | valsv[1,:] = newtimes[:] |
---|
[192] | 3029 | valsv[0,:] = ovar[:] |
---|
| 3030 | else: |
---|
[446] | 3031 | valsv[1,:] = newtimes[tiniid:tendid+1] |
---|
[192] | 3032 | valsv[0,:] = ovar[tiniid:tendid+1] |
---|
| 3033 | |
---|
| 3034 | vartimetrjv.append(valsv) |
---|
| 3035 | trjobj.close() |
---|
| 3036 | |
---|
| 3037 | drw.plot_TimeSeries(vartimetrjv, drw.units_lunits(gunits), tunits, \ |
---|
[440] | 3038 | 'val_trajectories_' + statisticskind, gvarname, timetit, tkind, timefmt, title,\ |
---|
[192] | 3039 | leglabels, locleg, gkind) |
---|
| 3040 | |
---|
| 3041 | def variable_values(values): |
---|
| 3042 | """ Function to give back values for a given variable |
---|
| 3043 | values= [varname] name of the variable |
---|
| 3044 | """ |
---|
| 3045 | |
---|
| 3046 | fname = 'variable_values' |
---|
| 3047 | |
---|
| 3048 | values = drw.variables_values(values) |
---|
| 3049 | |
---|
| 3050 | print fname,'values:',values |
---|
| 3051 | print fname,'variable_name:',values[0] |
---|
| 3052 | print fname,'standard_name:',values[1] |
---|
| 3053 | print fname,'min,max:',str(values[2]) + ',' + str(values[3]) |
---|
| 3054 | print fname,'long_name:',values[4] |
---|
| 3055 | print fname,'units:',values[5] |
---|
| 3056 | print fname,'shad_colors:',values[6] |
---|
| 3057 | print fname,'all_values:',drw.numVector_String(values,',') |
---|
| 3058 | |
---|
| 3059 | return |
---|
| 3060 | |
---|
[651] | 3061 | def draw_ptZvals(ncfile, values, variable): |
---|
| 3062 | """ Function to plot a given list of points and values |
---|
| 3063 | ncfile= netCDF file to use |
---|
| 3064 | values= [fvname]:[XYvar]:[pointype]:[pointsize]:[graphlimits]:[nxtype]: |
---|
| 3065 | [figuretitle]:[colorbar]:[mapvalue]:[kindfig] |
---|
| 3066 | fvname: name of the variable in the graph |
---|
| 3067 | XYvar: [lon],[lat] variable names |
---|
| 3068 | ptype: type of the point |
---|
| 3069 | ptsize: size of the point |
---|
| 3070 | graphlimits: minLON,minLAT,maxLON,maxLAT limits of the graph 'None' for the full size |
---|
| 3071 | nxtype: minimum and maximum type |
---|
| 3072 | 'auto': values taken from the extrems of the data |
---|
| 3073 | [min],[max]: given minimum and maximum values |
---|
| 3074 | figtitle: title of the figure |
---|
| 3075 | cbar: color bar |
---|
| 3076 | mapv: map characteristics: [proj],[res] |
---|
| 3077 | see full documentation: http://matplotlib.org/basemap/ |
---|
| 3078 | [proj]: projection |
---|
| 3079 | * 'cyl', cilindric |
---|
| 3080 | * 'lcc', lambert-conformal |
---|
| 3081 | [res]: resolution: |
---|
| 3082 | * 'c', crude |
---|
| 3083 | * 'l', low |
---|
| 3084 | * 'i', intermediate |
---|
| 3085 | * 'h', high |
---|
| 3086 | * 'f', full |
---|
| 3087 | kfig: kind of figure |
---|
| 3088 | variable= name of the variable to plot |
---|
| 3089 | """ |
---|
| 3090 | fname = 'draw_ptZvals' |
---|
| 3091 | import numpy.ma as ma |
---|
| 3092 | |
---|
| 3093 | if values == 'h': |
---|
| 3094 | print fname + '_____________________________________________________________' |
---|
| 3095 | print draw_ptZvals.__doc__ |
---|
| 3096 | quit() |
---|
| 3097 | |
---|
| 3098 | expectargs = '[fvname]:[XYvar]:[pointype]:[pointsize]:[graphlmits]:[nxtype]:' + \ |
---|
| 3099 | '[figuretit]:[colorbar]:[mapvalue]:[kindfig]' |
---|
| 3100 | |
---|
[690] | 3101 | drw.check_arguments(fname,values,expectargs,':') |
---|
[651] | 3102 | |
---|
| 3103 | fvname = values.split(':')[0] |
---|
| 3104 | XYvar = values.split(':')[1] |
---|
| 3105 | pointype = values.split(':')[2] |
---|
| 3106 | pointsize = int(values.split(':')[3]) |
---|
| 3107 | graphlimits = values.split(':')[4] |
---|
| 3108 | nxtype = values.split(':')[5] |
---|
| 3109 | figuretitle = values.split(':')[6].replace('!',' ') |
---|
| 3110 | colorbar = values.split(':')[7] |
---|
| 3111 | mapvalue = values.split(':')[8] |
---|
| 3112 | kindfig = values.split(':')[9] |
---|
| 3113 | |
---|
| 3114 | onc = NetCDFFile(ncfile, 'r') |
---|
| 3115 | |
---|
| 3116 | if not onc.variables.has_key(variable): |
---|
| 3117 | print errormsg |
---|
| 3118 | print ' ' + fname + ": file '" + ncfile + "' does not have variable '" + \ |
---|
| 3119 | variable + "' !!" |
---|
| 3120 | quit(-1) |
---|
| 3121 | |
---|
| 3122 | # points |
---|
| 3123 | lonvarn = XYvar.split(',')[0] |
---|
| 3124 | latvarn = XYvar.split(',')[1] |
---|
| 3125 | |
---|
| 3126 | if not onc.variables.has_key(lonvarn): |
---|
| 3127 | print errormsg |
---|
| 3128 | print ' ' + fname + ": file '" + ncfile + "' does not have longitude " + \ |
---|
| 3129 | "variable '" + lonvarn + "' !!" |
---|
| 3130 | quit(-1) |
---|
| 3131 | |
---|
| 3132 | if not onc.variables.has_key(latvarn): |
---|
| 3133 | print errormsg |
---|
| 3134 | print ' ' + fname + ": file '" + ncfile + "' does not have latitude " + \ |
---|
| 3135 | "variable '" + latvarn + "' !!" |
---|
| 3136 | quit(-1) |
---|
| 3137 | |
---|
| 3138 | olonvar = onc.variables[lonvarn] |
---|
| 3139 | olatvar = onc.variables[latvarn] |
---|
| 3140 | ovarvar = onc.variables[variable] |
---|
| 3141 | |
---|
| 3142 | Lpts = len(olonvar[:].flatten()) |
---|
| 3143 | |
---|
| 3144 | pointvalues = ma.masked_array(np.zeros((Lpts,3), dtype=np.float)) |
---|
[792] | 3145 | pointvalues[:,0] = olonvar[:].flatten() |
---|
| 3146 | pointvalues[:,1] = olatvar[:].flatten() |
---|
| 3147 | pointvalues[:,2] = ovarvar[:].flatten() |
---|
[651] | 3148 | |
---|
| 3149 | varattrs = ovarvar.ncattrs() |
---|
| 3150 | if drw.searchInlist(varattrs, 'units'): |
---|
| 3151 | fvunits = ovarvar.getncattr('units') |
---|
| 3152 | else: |
---|
| 3153 | fvunits = drw.variables_values(variable)[5] |
---|
| 3154 | |
---|
| 3155 | # map value |
---|
| 3156 | if mapvalue == 'None': mapvalue = None |
---|
| 3157 | |
---|
| 3158 | # Graph limits |
---|
| 3159 | if graphlimits != 'None': |
---|
| 3160 | graphlts = np.zeros((4), dtype=np.float) |
---|
| 3161 | for il in range(4): graphlts[il] = np.float(graphlimits.split(',')[il]) |
---|
| 3162 | pointvalues[:,0] = ma.masked_outside(pointvalues[:,0], graphlts[0], \ |
---|
| 3163 | graphlts[2]) |
---|
| 3164 | pointvalues[:,1] = ma.masked_outside(pointvalues[:,1], graphlts[3], \ |
---|
| 3165 | graphlts[2]) |
---|
| 3166 | |
---|
| 3167 | # for ip in range(Lpts): |
---|
| 3168 | # if pointvalues[ip,0] < graphlts[0] or pointvalues[ip,0] > graphlts[2] \ |
---|
| 3169 | # or pointvalues[ip,1] < graphlts[1] or pointvalues[ip,1] > graphlts[3]: |
---|
| 3170 | # print ip,pointvalues[ip,0:2], graphlts |
---|
| 3171 | # pointvalues[ip,2] = None |
---|
| 3172 | else: |
---|
| 3173 | graphlts = None |
---|
| 3174 | |
---|
| 3175 | drw.plot_ptZvals(fvname,fvunits,pointvalues,pointype,pointsize,graphlts, nxtype, \ |
---|
| 3176 | figuretitle,colorbar,mapvalue,kindfig) |
---|
| 3177 | |
---|
| 3178 | return |
---|
| 3179 | |
---|
| 3180 | #draw_ptZvals('OBSnetcdf.nc', 'pracc:lon,lat:o:80:2,42,7,47,:values!of!values:Blues:cyl,l:pdf', 'pr') |
---|
[698] | 3181 | |
---|
| 3182 | def draw_vectors(ncfile, values, varns): |
---|
| 3183 | """ Function to plot wind vectors |
---|
| 3184 | values= [dimname]|[vardimname]|[value]:[vecvals]:[windlabs]:[mapvalues]: |
---|
| 3185 | [gtit]:[kindfig]:[figuren] |
---|
| 3186 | 'X/Y/Z/T'|[dimname]|[vardimname]|[value]: ',', list for each basic dimension '|' separated of: |
---|
| 3187 | [dimname]: name of the dimension in the file |
---|
| 3188 | [vardimname]: name of the variable with the values for the dimension in the file |
---|
| 3189 | [value]: which value of the given dimension (-1, all; [ibeg]@[iend], i-range beginning, end) |
---|
| 3190 | No value takes all the range of the dimension |
---|
| 3191 | [vecvals]= [frequency],[color],[length] |
---|
| 3192 | [frequency]: [xfreq]@[yfreq] frequency of values allong each axis ('None', all grid points; |
---|
| 3193 | 'auto', computed automatically to have 20 vectors along each axis) |
---|
| 3194 | [color]: color of the vectors |
---|
| 3195 | 'singlecol'@[colorn]: all the vectors same color ('auto': for 'red') |
---|
| 3196 | 'wind'@[colorbar]: color of the vectors according to wind speed sqrt(u^2+v^2) and given [colorbar] |
---|
| 3197 | all vectors the same length |
---|
[704] | 3198 | '3rdvar'@[colorbar]@[varn]@[units]: color of the vectors according to a 3rd variable (to be added at -v) and given [colorbar] |
---|
[698] | 3199 | all vectors the same length |
---|
| 3200 | [length]: length of the wind vectors ('auto', for 9) |
---|
| 3201 | [windlabs]= [windname],[windunits] |
---|
| 3202 | [windname]: name of the wind variable in the graph |
---|
| 3203 | [windunits]: units of the wind variable in the graph ('None', for the value in the file) |
---|
| 3204 | [mapvalues]= map characteristics: [proj],[res] |
---|
| 3205 | see full documentation: http://matplotlib.org/basemap/ |
---|
| 3206 | [proj]: projection |
---|
| 3207 | * 'cyl', cilindric |
---|
| 3208 | * 'lcc', lambert conformal |
---|
| 3209 | [res]: resolution: |
---|
| 3210 | * 'c', crude |
---|
| 3211 | * 'l', low |
---|
| 3212 | * 'i', intermediate |
---|
| 3213 | * 'h', high |
---|
| 3214 | * 'f', full |
---|
| 3215 | gtit= title of the graph ('|', for spaces) |
---|
| 3216 | kindfig= kind of figure |
---|
| 3217 | figuren= name of the figure |
---|
| 3218 | ncfile= file to use |
---|
| 3219 | varns= [uwind],[ywind] ',' list of the name of the variables with the u-wind,y-wind component |
---|
| 3220 | """ |
---|
| 3221 | fname = 'draw_vectors' |
---|
| 3222 | |
---|
| 3223 | if values == 'h': |
---|
| 3224 | print fname + '_____________________________________________________________' |
---|
| 3225 | print draw_vectors.__doc__ |
---|
| 3226 | quit() |
---|
| 3227 | |
---|
| 3228 | expectargs = '[X/Y/Z/T]|[dimname]|[vardimname]|[value]:[vecvals]:[windlabs]:' + \ |
---|
| 3229 | '[mapvalues]:[gtit]:[kindfig]:[figuren]' |
---|
| 3230 | |
---|
| 3231 | drw.check_arguments(fname,values,expectargs,':') |
---|
| 3232 | |
---|
| 3233 | dimvals = values.split(':')[0] |
---|
| 3234 | vecvals = values.split(':')[1] |
---|
| 3235 | windlabels = values.split(':')[2] |
---|
| 3236 | mapvalues = values.split(':')[3] |
---|
| 3237 | gtit = values.split(':')[4] |
---|
| 3238 | kindfig = values.split(':')[5] |
---|
| 3239 | figuren = values.split(':')[6] |
---|
| 3240 | |
---|
| 3241 | of = NetCDFFile(ncfile,'r') |
---|
| 3242 | |
---|
| 3243 | dims = {} |
---|
| 3244 | for dimv in dimvals.split(','): |
---|
| 3245 | dns = dimv.split('|') |
---|
| 3246 | dims[dns[0]] = [dns[1], dns[2], dns[3]] |
---|
| 3247 | |
---|
| 3248 | varNs = [] |
---|
| 3249 | for dn in dims.keys(): |
---|
| 3250 | if dn == 'X': |
---|
| 3251 | varNs.append(dims[dn][1]) |
---|
| 3252 | dimx = len(of.dimensions[dims[dn][0]]) |
---|
| 3253 | elif dn == 'Y': |
---|
| 3254 | varNs.append(dims[dn][1]) |
---|
| 3255 | dimy = len(of.dimensions[dims[dn][0]]) |
---|
| 3256 | |
---|
| 3257 | ivar = 0 |
---|
| 3258 | for wvar in varns.split(','): |
---|
| 3259 | if not drw.searchInlist(of.variables.keys(), wvar): |
---|
| 3260 | print errormsg |
---|
| 3261 | print ' ' + fname + ": file does not have variable '" + wvar + "' !!" |
---|
| 3262 | quit(-1) |
---|
| 3263 | if ivar == 0: |
---|
| 3264 | varNs.append(wvar) |
---|
| 3265 | else: |
---|
| 3266 | varNs.append(wvar) |
---|
| 3267 | |
---|
| 3268 | ivar = 0 |
---|
| 3269 | for varN in varNs: |
---|
| 3270 | varslice = [] |
---|
| 3271 | |
---|
| 3272 | ovarN = of.variables[varN] |
---|
| 3273 | vard = ovarN.dimensions |
---|
| 3274 | for vdn in vard: |
---|
| 3275 | found = False |
---|
| 3276 | for dd in dims.keys(): |
---|
| 3277 | if dims[dd][0] == vdn: |
---|
| 3278 | if dims[dd][2].find('@') != -1: |
---|
| 3279 | rvals = dims[dd][2].split('@') |
---|
| 3280 | varslice.append(slice(int(rvals[0]), int(rvals[1]))) |
---|
| 3281 | elif dims[dd][2] == '-1': |
---|
| 3282 | varslice.append(slice(0,len(of.dimensions[dims[dd][0]]))) |
---|
| 3283 | else: |
---|
| 3284 | varslice.append(int(dims[dd][2])) |
---|
| 3285 | |
---|
| 3286 | found = True |
---|
| 3287 | break |
---|
| 3288 | if not found: |
---|
| 3289 | varslice.append(slice(0,len(of.dimensions[dims[dd][0]]))) |
---|
| 3290 | |
---|
| 3291 | if varN == dims['X'][1]: |
---|
| 3292 | lonvals0 = np.squeeze(ovarN[tuple(varslice)]) |
---|
| 3293 | elif varN == dims['Y'][1]: |
---|
| 3294 | latvals0 = np.squeeze(ovarN[tuple(varslice)]) |
---|
| 3295 | elif ivar == 2: |
---|
| 3296 | uwvals = np.squeeze(np.array(ovarN[tuple(varslice)])) |
---|
| 3297 | elif ivar == 3: |
---|
| 3298 | vwvals = np.squeeze(ovarN[tuple(varslice)]) |
---|
| 3299 | |
---|
| 3300 | ivar = ivar + 1 |
---|
| 3301 | |
---|
| 3302 | # print 'Final shapes:',lonvals0.shape,':',latvals0.shape,':',uwvals.shape,':', |
---|
| 3303 | # vwvals.shape |
---|
| 3304 | |
---|
| 3305 | if len(uwvals.shape) != 2 or len(vwvals.shape) != 2: |
---|
| 3306 | print errormsg |
---|
| 3307 | print ' ' + fname + ': wrong size of the wind fields! they must be ' + \ |
---|
| 3308 | '2-dimensional!' |
---|
| 3309 | print ' u-winds shape:',uwvals.shape,'dims:',of.variables[varNs[2]] |
---|
| 3310 | print ' v-winds shape:',vwvals.shape,'dims:',of.variables[varNs[3]] |
---|
| 3311 | print ' provide more values for their dimensions!!' |
---|
| 3312 | quit(-1) |
---|
| 3313 | |
---|
| 3314 | if len(lonvals0.shape) == 1: |
---|
| 3315 | lonvals, latvals = np.meshgrid(lonvals0, latvals0) |
---|
| 3316 | else: |
---|
| 3317 | lonvals = lonvals0 |
---|
| 3318 | latvals = latvals0 |
---|
| 3319 | |
---|
| 3320 | # Vector values |
---|
| 3321 | if vecvals.split(',')[0] == 'None': |
---|
| 3322 | freqv = None |
---|
| 3323 | else: |
---|
| 3324 | freqv = vecvals.split(',')[0] |
---|
| 3325 | colorvals = vecvals.split(',')[1] |
---|
| 3326 | coln = colorvals.split('@')[0] |
---|
| 3327 | colv = colorvals.split('@')[1] |
---|
| 3328 | if coln == 'singlecol': |
---|
| 3329 | colorv = colv |
---|
| 3330 | elif coln == 'wind': |
---|
| 3331 | colorv = np.sqrt(uwvals**2 + vwvals**2) |
---|
| 3332 | elif coln == '3rdvar': |
---|
| 3333 | if len(varn.split(',')) != 3: |
---|
| 3334 | print errormsg |
---|
| 3335 | print ' ' + fname + ": color of vectors should be according to '" + \ |
---|
| 3336 | coln + "' but a third varibale is not provided !!" |
---|
| 3337 | quit(-1) |
---|
| 3338 | ocolvec = of.variables[varNs[4]] |
---|
| 3339 | colorv = ocolvec[:] |
---|
| 3340 | stdvn, lonvn, unitsvn = drw.var_3desc(ocolvec) |
---|
| 3341 | colorvals = colorvals + '@' + stdvn + '@' + unitsvn |
---|
| 3342 | else: |
---|
| 3343 | print errormsg |
---|
| 3344 | print ' ' + fname + ": color type '" + coln + "' not ready !!" |
---|
| 3345 | quit(-1) |
---|
| 3346 | |
---|
| 3347 | lengthv = vecvals.split(',')[2] |
---|
| 3348 | |
---|
| 3349 | # Vector labels |
---|
| 3350 | windname = windlabels.split(',')[0] |
---|
| 3351 | windunits = windlabels.split(',')[1] |
---|
| 3352 | |
---|
| 3353 | drw.plot_vector(lonvals, latvals, uwvals, vwvals, freqv, colorvals, colorv, \ |
---|
| 3354 | lengthv, windname, windunits, mapvalues, gtit, kindfig, figuren) |
---|
| 3355 | |
---|
| 3356 | of.close() |
---|
| 3357 | |
---|
| 3358 | return |
---|
[704] | 3359 | |
---|
| 3360 | def draw_basins(ncfile, values, varns): |
---|
[785] | 3361 | """ Function to plot river basins with their discharge vector and basins id (from 'routing.nc') |
---|
[781] | 3362 | values= [lonlatbox]:[mapres]:[cbarname]:[xtrmbasin]:[mapdraw]:[veclength]:[freq]: |
---|
[782] | 3363 | [ifreq]:[plotcountry]:[basinidn]:[gtit]:[kindfig]:[figuren] |
---|
[781] | 3364 | [lonlatbox]= [lonSW],[lonNE],[latSW],[latNE] coordinates of the lon/lat box |
---|
[782] | 3365 | [mapres]= resolution of the mapping information |
---|
| 3366 | [cbarname]= colorbar name for the colors |
---|
| 3367 | [xtrmbasin]= [minbasin],[maxbasin] minimum and maximum basin numbers |
---|
| 3368 | [mapdraw]= whether to draw the map (and project the data) or not ('True/False') |
---|
| 3369 | [veclength]= length of the vectors of discharge at each grid cell |
---|
| 3370 | [freq]= frequency of values allong each axis (None, all grid points; |
---|
| 3371 | 'auto', computed automatically to have 20 vectors along each axis) |
---|
| 3372 | [plotcountry]= whether country lines should be plotted or not ('True/False') |
---|
| 3373 | [plotbasinid]= whether id of the basins should be plotted or not ('True/False') |
---|
| 3374 | [gtit]= title of the graph ('|', for spaces) |
---|
| 3375 | [kindfig]= kind of figure |
---|
| 3376 | [figuren]= name of the figure |
---|
[781] | 3377 | ncfile= file to use |
---|
| 3378 | """ |
---|
| 3379 | fname = 'draw_basins' |
---|
| 3380 | |
---|
| 3381 | if values == 'h': |
---|
| 3382 | print fname + '_____________________________________________________________' |
---|
| 3383 | print draw_vectors.__doc__ |
---|
| 3384 | quit() |
---|
| 3385 | |
---|
| 3386 | expectargs = '[lonlatbox]:[mapres]:[cbarname]:[xtrmbasin]:[mapdraw]:' + \ |
---|
[782] | 3387 | '[veclength]:[freq]:[plotcountry]:[basinidn]:[gtit]:[kindfig]:[figuren]' |
---|
[781] | 3388 | |
---|
| 3389 | drw.check_arguments(fname,values,expectargs,':') |
---|
| 3390 | |
---|
| 3391 | varn='basins' |
---|
| 3392 | lonname = 'nav_lon' |
---|
| 3393 | latname = 'nav_lat' |
---|
| 3394 | flowname = 'trip' |
---|
| 3395 | |
---|
| 3396 | lonlims =[] |
---|
| 3397 | latlims =[] |
---|
| 3398 | |
---|
| 3399 | lonlims.append(np.float(values.split(':')[0].split(',')[0])) |
---|
| 3400 | lonlims.append(np.float(values.split(':')[0].split(',')[1])) |
---|
| 3401 | latlims.append(np.float(values.split(':')[0].split(',')[2])) |
---|
| 3402 | latlims.append(np.float(values.split(':')[0].split(',')[3])) |
---|
| 3403 | map_res = values.split(':')[1] |
---|
| 3404 | cbarname = values.split(':')[2] |
---|
| 3405 | vtit = 'basins' |
---|
| 3406 | minbasin = np.int(values.split(':')[3].split(',')[0]) |
---|
| 3407 | maxbasin = np.int(values.split(':')[3].split(',')[1]) |
---|
[782] | 3408 | mapdraw = gen.Str_Bool(values.split(':')[4]) |
---|
[781] | 3409 | veclength = np.float(values.split(':')[5]) |
---|
| 3410 | freq0 = values.split(':')[6] |
---|
[782] | 3411 | plotcountry = gen.Str_Bool(values.split(':')[7]) |
---|
| 3412 | plotbasinid = gen.Str_Bool(values.split(':')[8]) |
---|
| 3413 | gtit = values.split(':')[9].replace('|',' ') |
---|
| 3414 | kindfig = values.split(':')[10] |
---|
| 3415 | figuren = values.split(':')[11] |
---|
[781] | 3416 | |
---|
| 3417 | if freq0 == 'None': freq = None |
---|
| 3418 | |
---|
| 3419 | ofile = NetCDFFile(ncfile, 'r') |
---|
| 3420 | |
---|
| 3421 | obasins = ofile.variables[varn] |
---|
| 3422 | olon = ofile.variables[lonname] |
---|
| 3423 | olat = ofile.variables[latname] |
---|
| 3424 | oflow = ofile.variables[flowname] |
---|
| 3425 | |
---|
| 3426 | lons = olon[:] |
---|
| 3427 | lats = olat[:] |
---|
| 3428 | |
---|
| 3429 | lon, lat = drw.lonlat2D(lons, lats) |
---|
| 3430 | |
---|
| 3431 | nlon = lonlims[0] |
---|
| 3432 | xlon = lonlims[1] |
---|
| 3433 | nlat = latlims[0] |
---|
| 3434 | xlat = latlims[1] |
---|
| 3435 | |
---|
| 3436 | imin, imax, jmin, jmax = gen.ijlonlat(lon, lat, nlon, xlon, nlat, xlat) |
---|
| 3437 | |
---|
| 3438 | drw.plot_basins(lon[jmin:jmax,imin:imax], lat[jmin:jmax,imin:imax], \ |
---|
| 3439 | oflow[jmin:jmax,imin:imax], freq, cbarname+'@basin@-', \ |
---|
| 3440 | obasins[jmin:jmax,imin:imax], veclength, minbasin, maxbasin, 'outflow', '-', \ |
---|
[782] | 3441 | 'cyl,'+map_res, plotcountry, plotbasinid, gtit, kindfig, figuren) |
---|
[781] | 3442 | |
---|
| 3443 | ofile.close() |
---|
| 3444 | |
---|
| 3445 | return |
---|
| 3446 | |
---|
| 3447 | def draw_basinsold(ncfile, values, varns): |
---|
| 3448 | """ Function to plot wind basins |
---|
[704] | 3449 | values= [dimname]|[vardimname]|[value]:[vecvals]:[windlabs]:[mapvalues]: |
---|
| 3450 | [gtit]:[kindfig]:[figuren] |
---|
| 3451 | 'X/Y/Z/T'|[dimname]|[vardimname]|[value]: ',', list for each basic dimension '|' separated of: |
---|
| 3452 | [dimname]: name of the dimension in the file |
---|
| 3453 | [vardimname]: name of the variable with the values for the dimension in the file |
---|
| 3454 | [value]: which value of the given dimension (-1, all; [ibeg]@[iend], i-range beginning, end) |
---|
| 3455 | No value takes all the range of the dimension |
---|
| 3456 | [vecvals]= [frequency],[color],[length] |
---|
| 3457 | [frequency]: [xfreq]@[yfreq] frequency of values allong each axis ('None', all grid points; |
---|
| 3458 | 'auto', computed automatically to have 20 vectors along each axis) |
---|
| 3459 | [color]: [colorbar]@[varn]@[units]: color of the vectors according to a 3rd variable (to be added at -v) and given [colorbar] |
---|
| 3460 | all vectors the same length |
---|
| 3461 | [length]: length of the wind vectors ('auto', for 9) |
---|
| 3462 | [windlabs]= [windname],[windunits] |
---|
| 3463 | [windname]: name of the wind variable in the graph |
---|
| 3464 | [windunits]: units of the wind variable in the graph ('None', for the value in the file) |
---|
| 3465 | [mapvalues]= map characteristics: [proj],[res] |
---|
| 3466 | see full documentation: http://matplotlib.org/basemap/ |
---|
| 3467 | [proj]: projection |
---|
| 3468 | * 'cyl', cilindric |
---|
| 3469 | * 'lcc', lambert conformal |
---|
| 3470 | [res]: resolution: |
---|
| 3471 | * 'c', crude |
---|
| 3472 | * 'l', low |
---|
| 3473 | * 'i', intermediate |
---|
| 3474 | * 'h', high |
---|
| 3475 | * 'f', full |
---|
| 3476 | gtit= title of the graph ('|', for spaces) |
---|
| 3477 | kindfig= kind of figure |
---|
| 3478 | figuren= name of the figure |
---|
| 3479 | ncfile= file to use |
---|
| 3480 | varns= [lon],[lat],[outflow],[basinID] ',' list of the name of the variables with the lon,lat, the outflow and the basin ID |
---|
| 3481 | """ |
---|
| 3482 | fname = 'draw_basins' |
---|
| 3483 | |
---|
| 3484 | if values == 'h': |
---|
| 3485 | print fname + '_____________________________________________________________' |
---|
| 3486 | print draw_vectors.__doc__ |
---|
| 3487 | quit() |
---|
| 3488 | |
---|
| 3489 | expectargs = '[X/Y/Z/T]|[dimname]|[vardimname]|[value]:[vecvals]:[windlabs]:' + \ |
---|
| 3490 | '[mapvalues]:[gtit]:[kindfig]:[figuren]' |
---|
| 3491 | |
---|
| 3492 | drw.check_arguments(fname,values,expectargs,':') |
---|
| 3493 | |
---|
| 3494 | dimvals = values.split(':')[0] |
---|
| 3495 | vecvals = values.split(':')[1] |
---|
| 3496 | windlabels = values.split(':')[2] |
---|
| 3497 | mapvalues = values.split(':')[3] |
---|
| 3498 | gtit = values.split(':')[4] |
---|
| 3499 | kindfig = values.split(':')[5] |
---|
| 3500 | figuren = values.split(':')[6] |
---|
| 3501 | |
---|
| 3502 | of = NetCDFFile(ncfile,'r') |
---|
| 3503 | |
---|
| 3504 | dims = {} |
---|
| 3505 | for dimv in dimvals.split(','): |
---|
| 3506 | dns = dimv.split('|') |
---|
| 3507 | dims[dns[0]] = [dns[1], dns[2], dns[3]] |
---|
| 3508 | |
---|
| 3509 | varNs = [] |
---|
| 3510 | for dn in dims.keys(): |
---|
| 3511 | if dn == 'X': |
---|
| 3512 | varNs.append(dims[dn][1]) |
---|
| 3513 | dimx = len(of.dimensions[dims[dn][0]]) |
---|
| 3514 | elif dn == 'Y': |
---|
| 3515 | varNs.append(dims[dn][1]) |
---|
| 3516 | dimy = len(of.dimensions[dims[dn][0]]) |
---|
| 3517 | |
---|
| 3518 | ivar = 0 |
---|
| 3519 | for wvar in varns.split(','): |
---|
| 3520 | if not drw.searchInlist(of.variables.keys(), wvar): |
---|
| 3521 | print errormsg |
---|
| 3522 | print ' ' + fname + ": file does not have variable '" + wvar + "' !!" |
---|
| 3523 | quit(-1) |
---|
| 3524 | if ivar == 0: |
---|
| 3525 | varNs.append(wvar) |
---|
| 3526 | else: |
---|
| 3527 | varNs.append(wvar) |
---|
| 3528 | |
---|
| 3529 | ivar = 0 |
---|
| 3530 | for varN in varNs: |
---|
| 3531 | varslice = [] |
---|
| 3532 | |
---|
| 3533 | ovarN = of.variables[varN] |
---|
| 3534 | vard = ovarN.dimensions |
---|
| 3535 | for vdn in vard: |
---|
| 3536 | found = False |
---|
| 3537 | for dd in dims.keys(): |
---|
| 3538 | if dims[dd][0] == vdn: |
---|
| 3539 | if dims[dd][2].find('@') != -1: |
---|
| 3540 | rvals = dims[dd][2].split('@') |
---|
| 3541 | varslice.append(slice(int(rvals[0]), int(rvals[1]))) |
---|
| 3542 | elif dims[dd][2] == '-1': |
---|
| 3543 | varslice.append(slice(0,len(of.dimensions[dims[dd][0]]))) |
---|
| 3544 | else: |
---|
| 3545 | varslice.append(int(dims[dd][2])) |
---|
| 3546 | |
---|
| 3547 | found = True |
---|
| 3548 | break |
---|
| 3549 | if not found: |
---|
| 3550 | varslice.append(slice(0,len(of.dimensions[dims[dd][0]]))) |
---|
| 3551 | |
---|
| 3552 | if varN == dims['X'][1]: |
---|
| 3553 | lonvals0 = np.squeeze(ovarN[tuple(varslice)]) |
---|
| 3554 | elif varN == dims['Y'][1]: |
---|
| 3555 | latvals0 = np.squeeze(ovarN[tuple(varslice)]) |
---|
| 3556 | |
---|
| 3557 | ivar = ivar + 1 |
---|
| 3558 | |
---|
| 3559 | if len(lonvals0.shape) == 1: |
---|
| 3560 | lonvals, latvals = np.meshgrid(lonvals0, latvals0) |
---|
| 3561 | else: |
---|
| 3562 | lonvals = lonvals0 |
---|
| 3563 | latvals = latvals0 |
---|
| 3564 | |
---|
| 3565 | # Vector values |
---|
| 3566 | if vecvals.split(',')[0] == 'None': |
---|
| 3567 | freqv = None |
---|
| 3568 | else: |
---|
| 3569 | freqv = vecvals.split(',')[0] |
---|
| 3570 | |
---|
| 3571 | colorvals = vecvals.split(',')[1] |
---|
| 3572 | if len(varn.split(',')) != 3: |
---|
| 3573 | print errormsg |
---|
[705] | 3574 | print ' ' + fname + ": color of vectors should be according to '" + \ |
---|
| 3575 | coln + "' but a third varibale is not provided !!" |
---|
| 3576 | quit(-1) |
---|
[704] | 3577 | |
---|
| 3578 | ocolvec = of.variables[varNs[3]] |
---|
| 3579 | colorv = ocolvec[:] |
---|
| 3580 | stdvn, lonvn, unitsvn = drw.var_3desc(ocolvec) |
---|
| 3581 | colorvals = colorvals + '@' + stdvn + '@' + unitsvn |
---|
| 3582 | |
---|
| 3583 | lengthv = vecvals.split(',')[2] |
---|
| 3584 | |
---|
| 3585 | # Vector labels |
---|
| 3586 | windname = windlabels.split(',')[0] |
---|
| 3587 | windunits = windlabels.split(',')[1] |
---|
| 3588 | |
---|
| 3589 | # Vector angles |
---|
| 3590 | oflow = ofile.variables[varNs[2]] |
---|
| 3591 | angle = (oflow[:] - 1)*np.pi/4 |
---|
| 3592 | xflow = np.where(oflow[:] < 9, np.float(lengthv)*np.sin(angle), 0.) |
---|
| 3593 | yflow = np.where(oflow[:] < 9, np.float(lengthv)*np.cos(angle), 0.) |
---|
| 3594 | |
---|
| 3595 | drw.plot_basins(lonvals, latvals, xflow, yflow, freqv, colorvals, colorv, \ |
---|
| 3596 | lengthv, windname, windunits, mapvalues, gtit, kindfig, figuren) |
---|
| 3597 | |
---|
| 3598 | of.close() |
---|
| 3599 | |
---|
| 3600 | return |
---|
[705] | 3601 | |
---|
| 3602 | def draw_river_desc(ncfile, values, riverns): |
---|
| 3603 | """ Function to plot rivers' description from ORCHIDEE's routing scheme |
---|
| 3604 | values= [dimname]|[vardimname]|[value]:[basinvals]:[upstreamvals]:[mapvalues]: |
---|
| 3605 | [gtit]:[kindfig]:[legloc]:[figuren] |
---|
| 3606 | 'X/Y'|[dimname]|[vardimname]|[value]: ',', list for each basic dimension '|' separated of: |
---|
| 3607 | [dimname]: name of the dimension in the file |
---|
| 3608 | [vardimname]: name of the variable with the values for the dimension in the file |
---|
| 3609 | [value]: which value of the given dimension (-1, all; [ibeg]@[iend], i-range beginning, end) |
---|
| 3610 | No value takes all the range of the dimension |
---|
| 3611 | [basinsvals]= [colorline] |
---|
| 3612 | [basincolor]: ',' list of colors of the line to use to mark the basins contours (single value also possible) |
---|
| 3613 | [upstreamvals]= [upstreamvarn],[colorbar] |
---|
| 3614 | [upstreamcolor]: colorbar to use to plot the basins upstream values |
---|
| 3615 | [mapvalues]= map characteristics: [proj],[res] |
---|
| 3616 | see full documentation: http://matplotlib.org/basemap/ |
---|
| 3617 | [proj]: projection |
---|
| 3618 | * 'cyl', cilindric |
---|
| 3619 | * 'lcc', lambert conformal |
---|
| 3620 | [res]: resolution: |
---|
| 3621 | * 'c', crude |
---|
| 3622 | * 'l', low |
---|
| 3623 | * 'i', intermediate |
---|
| 3624 | * 'h', high |
---|
| 3625 | * 'f', full |
---|
| 3626 | gtit= title of the graph ('|', for spaces) |
---|
| 3627 | kindfig= kind of figure |
---|
| 3628 | legloc= location of the legend (0, automatic) |
---|
| 3629 | 1: 'upper right', 2: 'upper left', 3: 'lower left', 4: 'lower right', |
---|
| 3630 | 5: 'right', 6: 'center left', 7: 'center right', 8: 'lower center', |
---|
| 3631 | 9: 'upper center', 10: 'center' kfig= kind of figure |
---|
| 3632 | figuren= name of the figure |
---|
| 3633 | ncfile= file to use |
---|
| 3634 | riverns= ',' list of the name of the rivers to plot |
---|
| 3635 | """ |
---|
| 3636 | import numpy.ma as ma |
---|
| 3637 | fname = 'draw_river_desc' |
---|
| 3638 | |
---|
| 3639 | if values == 'h': |
---|
| 3640 | print fname + '_____________________________________________________________' |
---|
| 3641 | print draw_river_desc.__doc__ |
---|
| 3642 | quit() |
---|
| 3643 | |
---|
| 3644 | expectargs = '[X/Y/Z/T]|[dimname]|[vardimname]|[value]:[basinvals]:' + \ |
---|
| 3645 | '[upstreamvals]:[mapvalues]:[gtit]:[kindfig]:[legloc]:[figuren]' |
---|
| 3646 | |
---|
| 3647 | drw.check_arguments(fname,values,expectargs,':') |
---|
| 3648 | |
---|
| 3649 | dimvals = values.split(':')[0] |
---|
| 3650 | basinvals = values.split(':')[1] |
---|
| 3651 | upstreamvals = values.split(':')[2] |
---|
| 3652 | mapvals = values.split(':')[3] |
---|
| 3653 | gtit = values.split(':')[4] |
---|
| 3654 | kindfig = values.split(':')[5] |
---|
| 3655 | legloc = int(values.split(':')[6]) |
---|
| 3656 | figuren = values.split(':')[7] |
---|
| 3657 | |
---|
| 3658 | basincol = basinvals |
---|
| 3659 | if basincol.find(',') != 1: |
---|
| 3660 | basincolor = basincol.split(',') |
---|
| 3661 | else: |
---|
| 3662 | basincolor = [basincol] |
---|
| 3663 | |
---|
| 3664 | upstreamcolor = upstreamvals |
---|
| 3665 | |
---|
| 3666 | of = NetCDFFile(ncfile,'r') |
---|
| 3667 | |
---|
| 3668 | dims = {} |
---|
| 3669 | for dimv in dimvals.split(','): |
---|
| 3670 | dns = dimv.split('|') |
---|
| 3671 | dims[dns[0]] = [dns[1], dns[2], dns[3]] |
---|
| 3672 | |
---|
| 3673 | varNs = [] |
---|
| 3674 | for dn in dims.keys(): |
---|
| 3675 | if dn == 'X': |
---|
| 3676 | varNs.append(dims[dn][1]) |
---|
| 3677 | dimx = len(of.dimensions[dims[dn][0]]) |
---|
| 3678 | elif dn == 'Y': |
---|
| 3679 | varNs.append(dims[dn][1]) |
---|
| 3680 | dimy = len(of.dimensions[dims[dn][0]]) |
---|
| 3681 | |
---|
| 3682 | if riverns.find(',') != -1: |
---|
| 3683 | riverns = riverns.split(',') |
---|
| 3684 | else: |
---|
| 3685 | riverns = [riverns] |
---|
| 3686 | |
---|
| 3687 | rivers = [] |
---|
| 3688 | riversubbasins = {} |
---|
| 3689 | riversupstream = {} |
---|
| 3690 | riversoutflow = {} |
---|
| 3691 | for rivern in riverns: |
---|
| 3692 | print rivern |
---|
| 3693 | |
---|
| 3694 | # subBasins |
---|
| 3695 | basinvar = rivern + '_coding' |
---|
| 3696 | if not drw.searchInlist(of.variables.keys(), basinvar): |
---|
| 3697 | print errormsg |
---|
| 3698 | print ' ' + fname + ": file does not have variable '" + basinvar + "' !!" |
---|
| 3699 | quit(-1) |
---|
| 3700 | rivers.append(rivern) |
---|
| 3701 | obasin = of.variables[basinvar] |
---|
| 3702 | riversubbasins[rivern] = obasin[:] |
---|
| 3703 | if rivern == riverns[0]: |
---|
| 3704 | finalmask = obasin[:].mask |
---|
| 3705 | else: |
---|
| 3706 | finalmask = finalmask * obasin[:].mask |
---|
| 3707 | |
---|
| 3708 | # upstream |
---|
| 3709 | upstreamvar = rivern + '_upstream' |
---|
| 3710 | if not drw.searchInlist(of.variables.keys(), upstreamvar): |
---|
| 3711 | print errormsg |
---|
| 3712 | print ' ' + fname + ": file does not have variable '" + upstreamvar + "' !!" |
---|
| 3713 | quit(-1) |
---|
| 3714 | oupstream = of.variables[upstreamvar] |
---|
| 3715 | riversupstream[rivern] = oupstream[:] |
---|
| 3716 | if rivern == riverns[0]: |
---|
| 3717 | uunits = oupstream.getncattr('units') |
---|
| 3718 | |
---|
| 3719 | # River metadata |
---|
| 3720 | fracvar = rivern + '_frac' |
---|
| 3721 | if not drw.searchInlist(of.variables.keys(), fracvar): |
---|
| 3722 | print errormsg |
---|
| 3723 | print ' ' + fname + ": file does not have variable '" + fracvar + "' !!" |
---|
| 3724 | quit(-1) |
---|
| 3725 | ofrac = of.variables[fracvar] |
---|
| 3726 | riversoutflow[rivern] = [ofrac.getncattr('Longitude_of_outflow_point'), \ |
---|
| 3727 | ofrac.getncattr('Latitude_of_outflow_point')] |
---|
| 3728 | |
---|
| 3729 | ivar = 0 |
---|
| 3730 | for varN in varNs: |
---|
| 3731 | varslice = [] |
---|
| 3732 | |
---|
| 3733 | ovarN = of.variables[varN] |
---|
| 3734 | vard = ovarN.dimensions |
---|
| 3735 | for vdn in vard: |
---|
| 3736 | found = False |
---|
| 3737 | for dd in dims.keys(): |
---|
| 3738 | if dims[dd][0] == vdn: |
---|
| 3739 | if dims[dd][2].find('@') != -1: |
---|
| 3740 | rvals = dims[dd][2].split('@') |
---|
| 3741 | varslice.append(slice(int(rvals[0]), int(rvals[1]))) |
---|
| 3742 | elif dims[dd][2] == '-1': |
---|
| 3743 | varslice.append(slice(0,len(of.dimensions[dims[dd][0]]))) |
---|
| 3744 | else: |
---|
| 3745 | varslice.append(int(dims[dd][2])) |
---|
| 3746 | |
---|
| 3747 | found = True |
---|
| 3748 | break |
---|
| 3749 | if not found: |
---|
| 3750 | varslice.append(slice(0,len(of.dimensions[dims[dd][0]]))) |
---|
| 3751 | |
---|
| 3752 | if varN == dims['X'][1]: |
---|
| 3753 | lonvals0 = np.squeeze(ovarN[tuple(varslice)]) |
---|
| 3754 | elif varN == dims['Y'][1]: |
---|
| 3755 | latvals0 = np.squeeze(ovarN[tuple(varslice)]) |
---|
| 3756 | |
---|
| 3757 | ivar = ivar + 1 |
---|
| 3758 | |
---|
| 3759 | if len(lonvals0.shape) == 1: |
---|
| 3760 | lonvals, latvals = np.meshgrid(lonvals0, latvals0) |
---|
| 3761 | else: |
---|
| 3762 | lonvals = lonvals0 |
---|
| 3763 | latvals = latvals0 |
---|
| 3764 | |
---|
| 3765 | # Masking only the lon,lat with rivers |
---|
| 3766 | malonvals = ma.masked_array(lonvals, mask=finalmask) |
---|
| 3767 | malatvals = ma.masked_array(latvals, mask=finalmask) |
---|
| 3768 | |
---|
| 3769 | if mapvals == 'None': |
---|
| 3770 | mapvalues = None |
---|
| 3771 | else: |
---|
| 3772 | mapvalues = mapvals |
---|
| 3773 | |
---|
| 3774 | drw.plot_river_desc(malonvals, malatvals, rivers, riversubbasins, riversupstream, riversoutflow, \ |
---|
| 3775 | basincolor, upstreamcolor, uunits, mapvalues, gtit, kindfig, legloc, figuren) |
---|
| 3776 | |
---|
| 3777 | of.close() |
---|
| 3778 | |
---|
[773] | 3779 | def draw_vertical_levels(ncfile, values, varn): |
---|
| 3780 | """ plotting vertical levels distribution |
---|
| 3781 | draw_topo_geogrid(ncfile, values, varn) |
---|
| 3782 | ncfile= file to use |
---|
[776] | 3783 | values= [zlogs]:[plogs]:[title]:[graphic_kind]:[legloc] |
---|
| 3784 | zlogs= zlog,dzlog |
---|
[773] | 3785 | zlog: to use logarithmic scale on the height axis ('true/false') |
---|
| 3786 | dzlog: to use logarithmic scale on the difference of height between levels axis ('true/false') |
---|
[776] | 3787 | plogs= plog,dplog |
---|
| 3788 | plog: to use logarithmic scale on the height axis ('true/false') |
---|
| 3789 | dplog: to use logarithmic scale on the difference of height between levels axis ('true/false') |
---|
[773] | 3790 | title: title of the graph ('!' for spaces) |
---|
| 3791 | graphic_kind: kind of figure (jpg, pdf, png) |
---|
| 3792 | legloc= location of the legend (0, automatic) |
---|
| 3793 | 1: 'upper right', 2: 'upper left', 3: 'lower left', 4: 'lower right', |
---|
| 3794 | 5: 'right', 6: 'center left', 7: 'center right', 8: 'lower center', |
---|
| 3795 | 9: 'upper center', 10: 'center' kfig= kind of figure |
---|
[776] | 3796 | varn= [varnheight],[varnpres] |
---|
| 3797 | varnheight: name of the variable with the height of the vertical levels |
---|
| 3798 | 'WRFz': for WRF z-levels (computed as (PH + PHB)/g, from a PHB(0,i,j) = 0) |
---|
| 3799 | varnpres: name of the variable with the pressure of the vertical levels ('None', for no pressure plot) |
---|
| 3800 | 'WRFp': for WRF p-levels (computed as P + PB, from a PHB(0,i,j) = 0) |
---|
[773] | 3801 | """ |
---|
| 3802 | fname = 'draw_vertical_levels' |
---|
| 3803 | |
---|
| 3804 | if values == 'h': |
---|
| 3805 | print fname + '_____________________________________________________________' |
---|
| 3806 | print draw_vertical_levels.__doc__ |
---|
| 3807 | quit() |
---|
| 3808 | |
---|
[776] | 3809 | expectargs = '[zlogs]:[plogs]:[title]:[graphic_kind]:[legloc]' |
---|
[773] | 3810 | |
---|
| 3811 | drw.check_arguments(fname,values,expectargs,':') |
---|
| 3812 | |
---|
[776] | 3813 | zlog = values.split(':')[0].split(',')[0] |
---|
| 3814 | dzlog = values.split(':')[0].split(',')[1] |
---|
| 3815 | plog = values.split(':')[1].split(',')[0] |
---|
| 3816 | dplog = values.split(':')[1].split(',')[1] |
---|
[773] | 3817 | title = values.split(':')[2].replace('!',' ') |
---|
| 3818 | kindfig = values.split(':')[3] |
---|
| 3819 | legloc = int(values.split(':')[4]) |
---|
| 3820 | |
---|
[776] | 3821 | if varn.find(',') == -1: |
---|
| 3822 | varnheight = varn |
---|
| 3823 | varnpres = None |
---|
| 3824 | pvals = None |
---|
| 3825 | print warnmsg |
---|
| 3826 | print ' ' + fname + ': assuming no pressure variable!!' |
---|
| 3827 | else: |
---|
| 3828 | varnheight = varn.split(',')[0] |
---|
| 3829 | varnpres = varn.split(',')[1] |
---|
| 3830 | if varnpres == 'None': |
---|
| 3831 | varnpres = None |
---|
| 3832 | pvals = None |
---|
| 3833 | |
---|
[773] | 3834 | if not os.path.isfile(ncfile): |
---|
| 3835 | print errormsg |
---|
| 3836 | print ' ' + fname + ': file "' + ncfile + '" does not exist !!' |
---|
| 3837 | quit(-1) |
---|
| 3838 | |
---|
| 3839 | objf = NetCDFFile(ncfile, 'r') |
---|
| 3840 | |
---|
[776] | 3841 | if varnheight == 'WRFz': |
---|
[773] | 3842 | if not gen.searchInlist(objf.variables,'PH'): |
---|
| 3843 | print errormsg |
---|
| 3844 | print ' ' + fname + ": WRF file '" + ncfile + "' does not have " + \ |
---|
| 3845 | "variable 'PH' !!" |
---|
| 3846 | quit(-1) |
---|
| 3847 | if not gen.searchInlist(objf.variables,'PHB'): |
---|
| 3848 | print errormsg |
---|
| 3849 | print ' ' + fname + ": WRF file '" + ncfile + "' does not have " + \ |
---|
| 3850 | "variable 'PHB' !!" |
---|
| 3851 | quit(-1) |
---|
| 3852 | |
---|
| 3853 | objph = objf.variables['PH'] |
---|
| 3854 | objphb = objf.variables['PHB'] |
---|
| 3855 | geop = objph[:] + objphb[:] |
---|
| 3856 | |
---|
| 3857 | ijz0 = gen.index_mat(geop[0,], 0.) |
---|
| 3858 | zvals = geop[0, :, ijz0[0], ijz0[1]] / 9.8 |
---|
| 3859 | else: |
---|
[776] | 3860 | if not gen.searchInlist(objf.variables, varnheight): |
---|
[773] | 3861 | print errormsg |
---|
[776] | 3862 | print ' ' + fname + ": file '" + ncfile + "' does not have height " + \ |
---|
| 3863 | " variable '" + varnheight + "' !!" |
---|
[773] | 3864 | quit(-1) |
---|
| 3865 | objvar = objf.variables[varn] |
---|
| 3866 | if len(objvar.shape) == 4: |
---|
| 3867 | print warnmsg |
---|
[776] | 3868 | print ' ' + fname + ": assuming that height variable '" + varnheight + \ |
---|
| 3869 | "' with shape: dt,dz,dy,dx. Tacking first time-step" |
---|
[773] | 3870 | |
---|
| 3871 | ijz0 = gen.index_mat(objvar[0,0,], 0.) |
---|
| 3872 | zvals = objvar[0, :, ijz0[0], ijz0[1]] |
---|
| 3873 | elif len(objvar.shape) == 3: |
---|
| 3874 | print warnmsg |
---|
[776] | 3875 | print ' ' + fname + ": assuming that height variable '" + varnheight + \ |
---|
| 3876 | "' with shape: dz,dy,dx" |
---|
[773] | 3877 | |
---|
| 3878 | ijz0 = gen.index_mat(objvar[0,], 0.) |
---|
| 3879 | zvals = objvar[:, ijz0[0], ijz0[1]] |
---|
| 3880 | |
---|
| 3881 | elif len(objvar.shape) == 2: |
---|
| 3882 | print warnmsg |
---|
[776] | 3883 | print ' ' + fname + ": assuming that height variable '" + varnheight + \ |
---|
| 3884 | "' with shape: dz,dyx" |
---|
[773] | 3885 | |
---|
| 3886 | ijz0 = gen.index_mat(objvar[0,], 0.) |
---|
| 3887 | zvals = objvar[:, ijz0[0]] |
---|
| 3888 | else: |
---|
| 3889 | zvals = objvar[:] |
---|
| 3890 | |
---|
[776] | 3891 | # Pressure |
---|
| 3892 | if varnpres is not None: |
---|
| 3893 | if varnpres == 'WRFp': |
---|
| 3894 | if not gen.searchInlist(objf.variables,'P'): |
---|
| 3895 | print errormsg |
---|
| 3896 | print ' ' + fname + ": WRF file '" + ncfile + "' does not have " + \ |
---|
| 3897 | "variable 'P' !!" |
---|
| 3898 | quit(-1) |
---|
| 3899 | if not gen.searchInlist(objf.variables,'PB'): |
---|
| 3900 | print errormsg |
---|
| 3901 | print ' ' + fname + ": WRF file '" + ncfile + "' does not have " + \ |
---|
| 3902 | "variable 'PB' !!" |
---|
| 3903 | quit(-1) |
---|
| 3904 | |
---|
| 3905 | objph = objf.variables['P'] |
---|
| 3906 | objphb = objf.variables['PB'] |
---|
| 3907 | pres = objph[:] + objphb[:] |
---|
| 3908 | |
---|
| 3909 | pvals = pres[0, :, ijz0[0], ijz0[1]] |
---|
| 3910 | else: |
---|
| 3911 | if not gen.searchInlist(objf.variables, varnpres): |
---|
| 3912 | print errormsg |
---|
| 3913 | print ' ' + fname + ": file '" + ncfile + "' does not have pressure " + \ |
---|
| 3914 | " variable '" + varnpres + "' !!" |
---|
| 3915 | quit(-1) |
---|
| 3916 | objvar = objf.variables[varnpres] |
---|
| 3917 | if len(objvar.shape) == 4: |
---|
| 3918 | print warnmsg |
---|
| 3919 | print ' ' + fname + ": assuming that pressure variable '" + varnpres + \ |
---|
| 3920 | "' with shape: dt,dz,dy,dx. Tacking first time-step" |
---|
| 3921 | |
---|
| 3922 | pvals = objvar[0, :, ijz0[0], ijz0[1]] |
---|
| 3923 | elif len(objvar.shape) == 3: |
---|
| 3924 | print warnmsg |
---|
| 3925 | print ' ' + fname + ": assuming that pressure variable '" + varnpres + \ |
---|
| 3926 | "' with shape: dz,dy,dx" |
---|
| 3927 | |
---|
| 3928 | pvals = objvar[:, ijz0[0], ijz0[1]] |
---|
| 3929 | |
---|
| 3930 | elif len(objvar.shape) == 2: |
---|
| 3931 | print warnmsg |
---|
| 3932 | print ' ' + fname + ": assuming that pressure variable '" + varnpres + \ |
---|
| 3933 | "' with shape: dz,dyx" |
---|
| 3934 | |
---|
| 3935 | pvals = objvar[:, ijz0[0]] |
---|
| 3936 | else: |
---|
| 3937 | pvals = objvar[:] |
---|
| 3938 | |
---|
| 3939 | # Logarithmic axes |
---|
[773] | 3940 | if zlog == 'true': |
---|
| 3941 | zlogv = True |
---|
| 3942 | elif zlog == 'false': |
---|
| 3943 | zlogv = False |
---|
| 3944 | else: |
---|
| 3945 | print errormsg |
---|
| 3946 | print ' ' + fname + ": wrong value for zlog: '" + zlog + "' !!" |
---|
| 3947 | print " must be either: 'true' or 'false'" |
---|
| 3948 | quit(-1) |
---|
[776] | 3949 | |
---|
[773] | 3950 | if dzlog == 'true': |
---|
| 3951 | dzlogv = True |
---|
| 3952 | elif dzlog == 'false': |
---|
| 3953 | dzlogv = False |
---|
| 3954 | else: |
---|
| 3955 | print errormsg |
---|
| 3956 | print ' ' + fname + ": wrong value for dzlog: '" + dzlog + "' !!" |
---|
| 3957 | print " must be either: 'true' or 'false'" |
---|
| 3958 | quit(-1) |
---|
| 3959 | |
---|
[776] | 3960 | if pvals is not None: |
---|
| 3961 | if plog == 'true': |
---|
| 3962 | plogv = True |
---|
| 3963 | elif plog == 'false': |
---|
| 3964 | plogv = False |
---|
| 3965 | else: |
---|
| 3966 | print errormsg |
---|
| 3967 | print ' ' + fname + ": wrong value for plog: '" + plog + "' !!" |
---|
| 3968 | print " must be either: 'true' or 'false'" |
---|
| 3969 | quit(-1) |
---|
| 3970 | if dplog == 'true': |
---|
| 3971 | dplogv = True |
---|
| 3972 | elif dplog == 'false': |
---|
| 3973 | dplogv = False |
---|
| 3974 | else: |
---|
| 3975 | print errormsg |
---|
| 3976 | print ' ' + fname + ": wrong value for dplog: '" + dplog + "' !!" |
---|
| 3977 | print " must be either: 'true' or 'false'" |
---|
| 3978 | quit(-1) |
---|
[773] | 3979 | |
---|
[776] | 3980 | drw.plot_vertical_lev(zvals, pvals, zlogv, dzlogv, plogv, dplogv, title, kindfig, legloc) |
---|
| 3981 | |
---|
[773] | 3982 | objf.close() |
---|
| 3983 | |
---|
| 3984 | return |
---|
| 3985 | |
---|
[651] | 3986 | #quit() |
---|
| 3987 | |
---|
[192] | 3988 | ####### ###### ##### #### ### ## # |
---|
| 3989 | |
---|
| 3990 | ngraphics = "'" + drw.numVector_String(namegraphics, "', '") + "'" |
---|
[705] | 3991 | |
---|
[192] | 3992 | ### Options |
---|
| 3993 | ##string_operation="operation to make: " + '\n' + " out, output values -S inidim1,[inidim2,...]:enddim1,[enddim2,...]" |
---|
| 3994 | string_operation="""operation to make: |
---|
| 3995 | draw_topo_geogrid, draws topography from a WPS geo_em.d[nn].nc: -S [minTopo],[maxTopo]:[SW_lon],[SW_lat],[NE_lon],[NE_lat]:[title]:[graphic_kind]:[projection],[res_coastline] |
---|
| 3996 | draw_2D_shad_cont, draws two 2D fields, first with shading second with contour lines: -v [varns],[varnc] -S [vnamefs],[vnamefc],[dimxvn],[dimyvn],[colorbar],[ckind],[clabfmt],[sminv]:[smaxv],[sminc]:[smaxv]:[Nlev],[figt],[kindfig],[reverse] |
---|
| 3997 | [ckind]: |
---|
| 3998 | 'cmap': as it gets from colorbar |
---|
| 3999 | 'fixc,[colname]': fixed color [colname], all stright lines |
---|
| 4000 | 'fixsignc,[colname]': fixed color [colname], >0 stright, <0 dashed line |
---|
| 4001 | """ |
---|
| 4002 | |
---|
| 4003 | #print string_operation |
---|
| 4004 | |
---|
| 4005 | parser = OptionParser() |
---|
| 4006 | parser.add_option("-f", "--netCDF_file", dest="ncfile", |
---|
| 4007 | help="file to use", metavar="FILE") |
---|
| 4008 | parser.add_option("-o", "--operation", type='choice', dest="operation", |
---|
| 4009 | choices=namegraphics, |
---|
| 4010 | help="operation to make: " + ngraphics, metavar="OPER") |
---|
| 4011 | parser.add_option("-S", "--valueS", dest="values", |
---|
| 4012 | help="[WHEN APPLICABLE] values to use according to the operation", metavar="VALUES") |
---|
| 4013 | parser.add_option("-v", "--variable", dest="varname", |
---|
| 4014 | help="[WHEN APPLICABLE] variable to check", metavar="VAR") |
---|
| 4015 | |
---|
| 4016 | (opts, args) = parser.parse_args() |
---|
| 4017 | |
---|
| 4018 | ####### ####### |
---|
| 4019 | ## MAIN |
---|
| 4020 | ####### |
---|
| 4021 | |
---|
| 4022 | # Not checking file operation |
---|
| 4023 | Notcheckingfile = ['draw_2D_shad_cont', 'draw_2D_shad_cont_time', \ |
---|
[195] | 4024 | 'draw_2D_shad_line', 'draw_2D_shad_line_time', 'draw_lines', 'draw_lines_time', \ |
---|
[545] | 4025 | 'draw_points', 'draw_topo_geogrid_boxes', 'draw_trajectories', \ |
---|
| 4026 | 'draw_vals_trajectories', 'variable_values'] |
---|
[192] | 4027 | |
---|
| 4028 | ####### ###### ##### #### ### ## # |
---|
| 4029 | errormsg='ERROR -- error -- ERROR -- error' |
---|
| 4030 | |
---|
| 4031 | varn=opts.varname |
---|
| 4032 | oper=opts.operation |
---|
| 4033 | |
---|
| 4034 | if opts.ncfile is not None and not os.path.isfile(opts.ncfile) and \ |
---|
| 4035 | not drw.searchInlist(Notcheckingfile, oper): |
---|
| 4036 | print errormsg |
---|
| 4037 | print ' ' + main + ': File ' + opts.ncfile + ' does not exist !!' |
---|
| 4038 | quit(-1) |
---|
| 4039 | |
---|
| 4040 | if oper == 'create_movie': |
---|
| 4041 | create_movie(opts.ncfile, opts.values, opts.varname) |
---|
| 4042 | elif oper == 'draw_2D_shad': |
---|
| 4043 | draw_2D_shad(opts.ncfile, opts.values, opts.varname) |
---|
| 4044 | elif oper == 'draw_2D_shad_time': |
---|
| 4045 | draw_2D_shad_time(opts.ncfile, opts.values, opts.varname) |
---|
| 4046 | elif oper == 'draw_2D_shad_cont': |
---|
| 4047 | draw_2D_shad_cont(opts.ncfile, opts.values, opts.varname) |
---|
| 4048 | elif oper == 'draw_2D_shad_cont_time': |
---|
| 4049 | draw_2D_shad_cont_time(opts.ncfile, opts.values, opts.varname) |
---|
| 4050 | elif oper == 'draw_2D_shad_line': |
---|
| 4051 | draw_2D_shad_line(opts.ncfile, opts.values, opts.varname) |
---|
| 4052 | elif oper == 'draw_2D_shad_line_time': |
---|
| 4053 | draw_2D_shad_line_time(opts.ncfile, opts.values, opts.varname) |
---|
[539] | 4054 | elif oper == 'draw_barbs': |
---|
| 4055 | draw_barbs(opts.ncfile, opts.values, opts.varname) |
---|
[704] | 4056 | elif oper == 'draw_basins': |
---|
| 4057 | draw_basins(opts.ncfile, opts.values, opts.varname) |
---|
[192] | 4058 | elif oper == 'draw_Neighbourghood_evol': |
---|
| 4059 | draw_Neighbourghood_evol(opts.ncfile, opts.values, opts.varname) |
---|
| 4060 | elif oper == 'draw_lines': |
---|
| 4061 | draw_lines(opts.ncfile, opts.values, opts.varname) |
---|
[194] | 4062 | elif oper == 'draw_lines_time': |
---|
| 4063 | draw_lines_time(opts.ncfile, opts.values, opts.varname) |
---|
[545] | 4064 | elif oper == 'draw_points': |
---|
| 4065 | draw_points(opts.ncfile, opts.values) |
---|
[665] | 4066 | elif oper == 'draw_points_lonlat': |
---|
| 4067 | draw_points_lonlat(opts.ncfile, opts.values) |
---|
[651] | 4068 | elif oper == 'draw_ptZvals': |
---|
| 4069 | draw_ptZvals(opts.ncfile, opts.values, opts.varname) |
---|
[705] | 4070 | elif oper == 'draw_river_desc': |
---|
| 4071 | draw_river_desc(opts.ncfile, opts.values, opts.varname) |
---|
[192] | 4072 | elif oper == 'draw_timeSeries': |
---|
| 4073 | draw_timeSeries(opts.ncfile, opts.values, opts.varname) |
---|
| 4074 | elif oper == 'draw_topo_geogrid': |
---|
| 4075 | draw_topo_geogrid(opts.ncfile, opts.values) |
---|
| 4076 | elif oper == 'draw_topo_geogrid_boxes': |
---|
| 4077 | draw_topo_geogrid_boxes(opts.ncfile, opts.values) |
---|
| 4078 | elif oper == 'draw_trajectories': |
---|
| 4079 | draw_trajectories(opts.ncfile, opts.values, opts.varname) |
---|
| 4080 | elif oper == 'draw_vals_trajectories': |
---|
| 4081 | draw_vals_trajectories(opts.ncfile, opts.values, opts.varname) |
---|
[698] | 4082 | elif oper == 'draw_vectors': |
---|
| 4083 | draw_vectors(opts.ncfile, opts.values, opts.varname) |
---|
[773] | 4084 | elif oper == 'draw_vertical_levels': |
---|
| 4085 | draw_vertical_levels(opts.ncfile, opts.values, opts.varname) |
---|
[192] | 4086 | elif oper == 'list_graphics': |
---|
| 4087 | # From: http://www.diveintopython.net/power_of_introspection/all_together.html |
---|
| 4088 | import drawing as myself |
---|
| 4089 | object = myself |
---|
| 4090 | for opern in namegraphics: |
---|
| 4091 | if opern != 'list_graphics': |
---|
| 4092 | print opern + '_______ ______ _____ ____ ___ __ _' |
---|
| 4093 | print getattr(object, opern).__doc__ |
---|
| 4094 | elif oper == 'variable_values': |
---|
| 4095 | variable_values(opts.values) |
---|
| 4096 | else: |
---|
| 4097 | print errormsg |
---|
| 4098 | print ' ' + main + ": the graphic '" + oper + "' is not ready !!" |
---|
| 4099 | print errormsg |
---|
| 4100 | quit() |
---|