Changeset 698 in lmdz_wrf
- Timestamp:
- Mar 10, 2016, 6:55:00 PM (9 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/drawing.py
r690 r698 26 26 ## 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 27 27 ## e.g. # drawing.py -f carteveg5km.nc -o draw_points_lonlat -S 'longitude:latitude:pdf:points!veget|type:green:.:0.5:None:0:legend' 28 ## 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 28 29 29 30 main = 'drawing.py' … … 35 36 namegraphics = ['create_movie', 'draw_2D_shad', 'draw_2D_shad_time', \ 36 37 'draw_2D_shad_cont', 'draw_2D_shad_cont_time', 'draw_2D_shad_line', \ 37 'draw_2D_shad_line_time', 'draw_barbs', 'draw_points', 'draw_points_lonlat', \ 38 'draw_2D_shad_line_time', 'draw_barbs', \ 39 'draw_lines', 'draw_lines_time', 'draw_Neighbourghood_evol', \ 40 'draw_points', 'draw_points_lonlat', \ 38 41 'draw_ptZvals', 'draw_timeSeries', \ 39 42 'draw_topo_geogrid', \ 40 43 'draw_topo_geogrid_boxes', 'draw_trajectories', 'draw_vals_trajectories', \ 41 'draw_lines', 'draw_lines_time', 'draw_Neighbourghood_evol', 'list_graphics', \ 42 'variable_values'] 44 'draw_vectors', 'list_graphics', 'variable_values'] 43 45 44 46 def draw_2D_shad(ncfile, values, varn): … … 1295 1297 uwvals = np.squeeze(np.array(ovarN[tuple(varslice)])) 1296 1298 elif ivar == 3: 1297 vwvals = np.squeeze(ovarN[tuple(varslice)]) 1299 vwvals = np.squeeze(ovarN[tuple(varslice)]) 1298 1300 1299 1301 ivar = ivar + 1 … … 3147 3149 3148 3150 #draw_ptZvals('OBSnetcdf.nc', 'pracc:lon,lat:o:80:2,42,7,47,:values!of!values:Blues:cyl,l:pdf', 'pr') 3151 3152 def draw_vectors(ncfile, values, varns): 3153 """ Function to plot wind vectors 3154 values= [dimname]|[vardimname]|[value]:[vecvals]:[windlabs]:[mapvalues]: 3155 [gtit]:[kindfig]:[figuren] 3156 'X/Y/Z/T'|[dimname]|[vardimname]|[value]: ',', list for each basic dimension '|' separated of: 3157 [dimname]: name of the dimension in the file 3158 [vardimname]: name of the variable with the values for the dimension in the file 3159 [value]: which value of the given dimension (-1, all; [ibeg]@[iend], i-range beginning, end) 3160 No value takes all the range of the dimension 3161 [vecvals]= [frequency],[color],[length] 3162 [frequency]: [xfreq]@[yfreq] frequency of values allong each axis ('None', all grid points; 3163 'auto', computed automatically to have 20 vectors along each axis) 3164 [color]: color of the vectors 3165 'singlecol'@[colorn]: all the vectors same color ('auto': for 'red') 3166 'wind'@[colorbar]: color of the vectors according to wind speed sqrt(u^2+v^2) and given [colorbar] 3167 all vectors the same length 3168 '3rdvar'@[colorbar]: color of the vectors according to a 3rd variable (to be added at -v) and given [colorbar] 3169 all vectors the same length 3170 [length]: length of the wind vectors ('auto', for 9) 3171 [windlabs]= [windname],[windunits] 3172 [windname]: name of the wind variable in the graph 3173 [windunits]: units of the wind variable in the graph ('None', for the value in the file) 3174 [mapvalues]= map characteristics: [proj],[res] 3175 see full documentation: http://matplotlib.org/basemap/ 3176 [proj]: projection 3177 * 'cyl', cilindric 3178 * 'lcc', lambert conformal 3179 [res]: resolution: 3180 * 'c', crude 3181 * 'l', low 3182 * 'i', intermediate 3183 * 'h', high 3184 * 'f', full 3185 gtit= title of the graph ('|', for spaces) 3186 kindfig= kind of figure 3187 figuren= name of the figure 3188 ncfile= file to use 3189 varns= [uwind],[ywind] ',' list of the name of the variables with the u-wind,y-wind component 3190 """ 3191 fname = 'draw_vectors' 3192 3193 if values == 'h': 3194 print fname + '_____________________________________________________________' 3195 print draw_vectors.__doc__ 3196 quit() 3197 3198 expectargs = '[X/Y/Z/T]|[dimname]|[vardimname]|[value]:[vecvals]:[windlabs]:' + \ 3199 '[mapvalues]:[gtit]:[kindfig]:[figuren]' 3200 3201 drw.check_arguments(fname,values,expectargs,':') 3202 3203 dimvals = values.split(':')[0] 3204 vecvals = values.split(':')[1] 3205 windlabels = values.split(':')[2] 3206 mapvalues = values.split(':')[3] 3207 gtit = values.split(':')[4] 3208 kindfig = values.split(':')[5] 3209 figuren = values.split(':')[6] 3210 3211 of = NetCDFFile(ncfile,'r') 3212 3213 dims = {} 3214 for dimv in dimvals.split(','): 3215 dns = dimv.split('|') 3216 dims[dns[0]] = [dns[1], dns[2], dns[3]] 3217 3218 varNs = [] 3219 for dn in dims.keys(): 3220 if dn == 'X': 3221 varNs.append(dims[dn][1]) 3222 dimx = len(of.dimensions[dims[dn][0]]) 3223 elif dn == 'Y': 3224 varNs.append(dims[dn][1]) 3225 dimy = len(of.dimensions[dims[dn][0]]) 3226 3227 ivar = 0 3228 for wvar in varns.split(','): 3229 if not drw.searchInlist(of.variables.keys(), wvar): 3230 print errormsg 3231 print ' ' + fname + ": file does not have variable '" + wvar + "' !!" 3232 quit(-1) 3233 if ivar == 0: 3234 varNs.append(wvar) 3235 else: 3236 varNs.append(wvar) 3237 3238 ivar = 0 3239 for varN in varNs: 3240 varslice = [] 3241 3242 ovarN = of.variables[varN] 3243 vard = ovarN.dimensions 3244 for vdn in vard: 3245 found = False 3246 for dd in dims.keys(): 3247 if dims[dd][0] == vdn: 3248 if dims[dd][2].find('@') != -1: 3249 rvals = dims[dd][2].split('@') 3250 varslice.append(slice(int(rvals[0]), int(rvals[1]))) 3251 elif dims[dd][2] == '-1': 3252 varslice.append(slice(0,len(of.dimensions[dims[dd][0]]))) 3253 else: 3254 varslice.append(int(dims[dd][2])) 3255 3256 found = True 3257 break 3258 if not found: 3259 varslice.append(slice(0,len(of.dimensions[dims[dd][0]]))) 3260 3261 if varN == dims['X'][1]: 3262 lonvals0 = np.squeeze(ovarN[tuple(varslice)]) 3263 elif varN == dims['Y'][1]: 3264 latvals0 = np.squeeze(ovarN[tuple(varslice)]) 3265 elif ivar == 2: 3266 uwvals = np.squeeze(np.array(ovarN[tuple(varslice)])) 3267 elif ivar == 3: 3268 vwvals = np.squeeze(ovarN[tuple(varslice)]) 3269 3270 ivar = ivar + 1 3271 3272 # print 'Final shapes:',lonvals0.shape,':',latvals0.shape,':',uwvals.shape,':', 3273 # vwvals.shape 3274 3275 if len(uwvals.shape) != 2 or len(vwvals.shape) != 2: 3276 print errormsg 3277 print ' ' + fname + ': wrong size of the wind fields! they must be ' + \ 3278 '2-dimensional!' 3279 print ' u-winds shape:',uwvals.shape,'dims:',of.variables[varNs[2]] 3280 print ' v-winds shape:',vwvals.shape,'dims:',of.variables[varNs[3]] 3281 print ' provide more values for their dimensions!!' 3282 quit(-1) 3283 3284 if len(lonvals0.shape) == 1: 3285 lonvals, latvals = np.meshgrid(lonvals0, latvals0) 3286 else: 3287 lonvals = lonvals0 3288 latvals = latvals0 3289 3290 # Vector values 3291 if vecvals.split(',')[0] == 'None': 3292 freqv = None 3293 else: 3294 freqv = vecvals.split(',')[0] 3295 colorvals = vecvals.split(',')[1] 3296 coln = colorvals.split('@')[0] 3297 colv = colorvals.split('@')[1] 3298 if coln == 'singlecol': 3299 colorv = colv 3300 elif coln == 'wind': 3301 colorv = np.sqrt(uwvals**2 + vwvals**2) 3302 elif coln == '3rdvar': 3303 if len(varn.split(',')) != 3: 3304 print errormsg 3305 print ' ' + fname + ": color of vectors should be according to '" + \ 3306 coln + "' but a third varibale is not provided !!" 3307 quit(-1) 3308 ocolvec = of.variables[varNs[4]] 3309 colorv = ocolvec[:] 3310 stdvn, lonvn, unitsvn = drw.var_3desc(ocolvec) 3311 colorvals = colorvals + '@' + stdvn + '@' + unitsvn 3312 else: 3313 print errormsg 3314 print ' ' + fname + ": color type '" + coln + "' not ready !!" 3315 quit(-1) 3316 3317 lengthv = vecvals.split(',')[2] 3318 3319 # Vector labels 3320 windname = windlabels.split(',')[0] 3321 windunits = windlabels.split(',')[1] 3322 3323 drw.plot_vector(lonvals, latvals, uwvals, vwvals, freqv, colorvals, colorv, \ 3324 lengthv, windname, windunits, mapvalues, gtit, kindfig, figuren) 3325 3326 of.close() 3327 3328 return 3149 3329 #quit() 3150 3330 … … 3239 3419 elif oper == 'draw_vals_trajectories': 3240 3420 draw_vals_trajectories(opts.ncfile, opts.values, opts.varname) 3421 elif oper == 'draw_vectors': 3422 draw_vectors(opts.ncfile, opts.values, opts.varname) 3241 3423 elif oper == 'list_graphics': 3242 3424 # From: http://www.diveintopython.net/power_of_introspection/all_together.html -
trunk/tools/drawing_tools.py
r690 r698 44 44 # DegGradSec_deg: 45 45 # intT2dt: 46 # lonlat2D: Function to return lon, lat 2D matrices from any lon,lat matrix 46 47 # lonlat_values: 47 48 # date_CFtime: … … 1492 1493 elif u == 'deg K': lu='$K$' 1493 1494 elif u == 'degK': lu='$K$' 1495 elif u == 'g/g': lu='$gg^{-1}$' 1494 1496 elif u == 'hours': lu='$hour$' 1495 1497 elif u == 'J/kg': lu='$Jkg^{-1}$' … … 1536 1538 elif u == 'seconds': lu='$second$' 1537 1539 elif u == '%': lu='\%' 1540 elif u == '?': lu='-' 1538 1541 else: 1539 1542 print errormsg … … 5794 5797 def plot_barbs(xvals,yvals,uvals,vvals,vecfreq,veccolor,veclength,windn,wuts,mapv,graphtit,kfig,figname): 5795 5798 """ Function to plot wind barbs 5796 xvals= values for the 'x-axis' 5797 yvals= values for the 'y-axis' 5799 xvals= x position of the values 5800 yvals= y position of the values 5801 uvals= values for the x-wind 5802 uvals= values for the y-wind 5798 5803 vecfreq= [xfreq],[yfreq] frequency of values allong each axis (None, all grid points; 5799 5804 'auto', computed automatically to have 20 vectors along each axis) … … 6061 6066 6062 6067 return 6068 6069 def plot_vector(xvals,yvals,uvals,vvals,vecfreq,vecoln,veccolor,veclength,windn,wuts,\ 6070 mapv,graphtit,kfig,figname): 6071 """ Function to plot vectors 6072 xvals= values for the x-axis 6073 yvals= values for the y-axis 6074 uvals= values for the x-wind 6075 vvals= values for the y-wind 6076 vecfreq= [xfreq],[yfreq] frequency of values allong each axis (None, all grid points; 6077 'auto', computed automatically to have 20 vectors along each axis) 6078 veccoln= name for the color of the vectors 6079 'singlecol'@[colorn]: all the vectors same color ('auto': for 'red') 6080 'wind'@[colorbar]: color of the vectors according to wind speed sqrt(u^2+v^2) and given [colorbar] 6081 '3rdvar'@[colorbar]: color of the vectors according to a 3rd variable (to be added at -v) and given [colorbar] 6082 veccolor= color of the vectors 6083 veclength= length of the wind vectors: 6084 'singlecol': 'auto', for 9 6085 'wind' and '3rdvar': 'auto' length as wind speed, otherwise fix length 6086 windn= name of the wind variable in the graph 6087 wuts= units of the wind variable in the graph 6088 mapv= map characteristics: [proj],[res] 6089 see full documentation: http://matplotlib.org/basemap/ 6090 [proj]: projection 6091 * 'cyl', cilindric 6092 * 'lcc', lambert conformal 6093 [res]: resolution: 6094 * 'c', crude 6095 * 'l', low 6096 * 'i', intermediate 6097 * 'h', high 6098 * 'f', full 6099 graphtit= title of the graph ('|', for spaces) 6100 kfig= kind of figure 6101 figname= name of the figure 6102 """ 6103 fname = 'plot_vector' 6104 6105 dx=xvals.shape[1] 6106 dy=xvals.shape[0] 6107 6108 # Frequency of vectors 6109 if vecfreq is None: 6110 xfreq = 1 6111 yfreq = 1 6112 elif vecfreq == 'auto': 6113 xfreq = dx/20 6114 yfreq = dy/20 6115 else: 6116 xfreq=int(vecfreq.split('@')[0]) 6117 yfreq=int(vecfreq.split('@')[1]) 6118 6119 # Vector length 6120 if veclength == 'auto': 6121 vlength = 9 6122 else: 6123 vlength = veclength 6124 6125 # Colors 6126 VecN = vecoln.split('@')[0] 6127 if VecN == 'singlecol': 6128 if veccolor == 'auto': 6129 vcolor = "red" 6130 else: 6131 vcolor = veccolor 6132 elif VecN == 'wind' or VecN == '3rdvar': 6133 vcolor = vecoln.split('@')[1] 6134 6135 plt.rc('text', usetex=True) 6136 6137 if not mapv is None: 6138 lon00 = np.where(xvals[:] < 0., 360. + xvals[:], xvals[:]) 6139 lat00 = yvals[:] 6140 6141 map_proj=mapv.split(',')[0] 6142 map_res=mapv.split(',')[1] 6143 6144 nlon = np.min(xvals[::yfreq,::xfreq]) 6145 xlon = np.max(xvals[::yfreq,::xfreq]) 6146 nlat = np.min(yvals[::yfreq,::xfreq]) 6147 xlat = np.max(yvals[::yfreq,::xfreq]) 6148 6149 lon2 = xvals[dy/2,dx/2] 6150 lat2 = yvals[dy/2,dx/2] 6151 6152 print 'lon2:', lon2, 'lat2:', lat2, 'SW pt:', nlon, ',', nlat, 'NE pt:', \ 6153 xlon, ',', xlat 6154 6155 if map_proj == 'cyl': 6156 m = Basemap(projection=map_proj, llcrnrlon=nlon, llcrnrlat=nlat, \ 6157 urcrnrlon=xlon, urcrnrlat= xlat, resolution=map_res) 6158 elif map_proj == 'lcc': 6159 m = Basemap(projection=map_proj, lat_0=lat2, lon_0=lon2, llcrnrlon=nlon, \ 6160 llcrnrlat=nlat, urcrnrlon=xlon, urcrnrlat= xlat, resolution=map_res) 6161 else: 6162 print errormsg 6163 print ' ' + fname + ": projection '" + map_proj + "' not ready!!" 6164 print ' projections available: cyl, lcc' 6165 quit(-1) 6166 6167 m.drawcoastlines() 6168 6169 meridians = pretty_int(nlon,xlon,5) 6170 m.drawmeridians(meridians,labels=[True,False,False,True],color="black") 6171 6172 parallels = pretty_int(nlat,xlat,5) 6173 m.drawparallels(parallels,labels=[False,True,True,False],color="black") 6174 6175 plt.xlabel('W-E') 6176 plt.ylabel('S-N') 6177 6178 if VecN == 'singlecol': 6179 plt.quiver(xvals[::yfreq,::xfreq], yvals[::yfreq,::xfreq], \ 6180 uvals[::yfreq,::xfreq], vvals[::yfreq,::xfreq], color=vcolor, pivot='middle') 6181 plt.annotate(windn.replace('_','\_') +' (' + units_lunits(wuts) + ')', \ 6182 xy=(0.80,-0.15), xycoords='axes fraction', color=vcolor) 6183 else: 6184 if veclength != 'auto': 6185 wind = np.sqrt(uvals**2 + vvals**2) 6186 uvals = np.where(wind == 0., 0., uvals) 6187 vvals = np.where(wind == 0., 0., vvals) 6188 uvals = np.float(veclength)*uvals/wind 6189 vvals = np.float(veclength)*vvals/wind 6190 6191 plt.quiver(xvals[::yfreq,::xfreq], yvals[::yfreq,::xfreq], \ 6192 uvals[::yfreq,::xfreq], vvals[::yfreq,::xfreq], 6193 veccolor[::yfreq,::xfreq], cmap=plt.get_cmap(vcolor), pivot='middle') 6194 cbar = plt.colorbar() 6195 6196 if VecN == 'wind': 6197 cbar.set_label('$\sqrt{u^{2} + v^{2}}$ (' + units_lunits(wuts) + ')') 6198 else: 6199 vN = vecoln.split('@')[2] 6200 vU = vecoln.split('@')[3] 6201 cbar.set_label(vN + ' (' + units_lunits(vU) + ')') 6202 6203 plt.annotate(windn.replace('_','\_') +' (' + units_lunits(wuts) + ')', \ 6204 xy=(0.80,-0.15), xycoords='axes fraction', color='black') 6205 6206 plt.title(graphtit.replace('|',' ').replace('&','\&')) 6207 6208 output_kind(kfig, figname, True) 6209 6210 return 6211 6212 def var_3desc(ovar): 6213 """ Function to provide std_name, long_name and units from an object variable 6214 ovar= object variable 6215 """ 6216 fname = 'var_desc' 6217 varattrs = ovar.ncattrs() 6218 6219 if searchInlist(varattrs,'std_name'): 6220 stdn = ovar.getncattr('std_name') 6221 else: 6222 vvalues = variables_values(ovar._name) 6223 stdn = vvalues[1] 6224 6225 if searchInlist(varattrs,'long_name'): 6226 lonn = ovar.getncattr('long_name') 6227 else: 6228 lonn = vvalues[4].replace('|',' ') 6229 6230 if searchInlist(varattrs,'units'): 6231 un = ovar.getncattr('units') 6232 else: 6233 un = vvalues[5] 6234 6235 return stdn, lonn, un
Note: See TracChangeset
for help on using the changeset viewer.