Changeset 392
- Timestamp:
- Nov 17, 2011, 2:17:48 AM (13 years ago)
- Location:
- trunk/UTIL/PYTHON
- Files:
-
- 1 deleted
- 4 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/UTIL/PYTHON/README.PP
r391 r392 1 ************************************** 2 ************************************** 3 ************************************** 4 PLANETOPLOT TUTORIAL EXAMPLES 5 ************************************** 6 Authors : AC + AS 7 ************************************** 8 DON'T FORGET YOUR BEST FRIEND IS 9 pp.py -h [or] pp.py --help 10 ************************************** 11 ************************************** 12 ************************************** 1 13 2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~3 14 4 PYTHON COMMAND LINE PLOTS EXAMPLES 15 ************************************* 16 MAPMODE 1 17 MAPPING MODE 18 SIMPLE EXAMPLES on a SAMPLE GCM FILE 19 ************************************* 5 20 6 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 21 Goal: The simplest, most minimal example. Mapping near-surface wind vectors over topography. 22 pp.py -f diagfired.nc 7 23 24 Goal: I would like finer contours. 25 pp.py -f diagfired.nc --div 30 26 27 Goal: I would like more vectors [i.e. lower the stride]. 28 pp.py -f diagfired.nc --div 30 -s 1 29 30 Goal: I want to get rid of those damn vectors. 31 pp.py -f diagfired.nc -x 32 33 Goal: I want to map a given field (surface temperature). 34 pp.py -f diagfired.nc -x -v tsurf 35 36 Goal: I want to map two fields next to one another (topography and tauice). 37 pp.py -f diagfired.nc -x -v phisinit,tauice 38 39 Goal: I want to map two fields, tauice shaded, topography contoured, same plot. 40 pp.py -f diagfired.nc -x -v tauice -w phisinit 41 42 Goal: I want to map a field but projected on the sphere. 43 pp.py -f diagfired.nc -x -v tauice -p ortho 44 45 Goal: I want to redefine the minimum and maximum values shown. 46 pp.py -f diagfired.nc -x -v tauice -m 0.2 -M 0.9 47 48 Goal: I want to insert holes wherever values are lower than 0.2 and higher than 0.9 49 pp.py -f diagfired.nc -x -v tauice -m 0.2 -M 0.9 -H 50 51 Goal: I want to fill holes with an background image of Mars [you have to be connected to Internet] 52 pp.py -f diagfired.nc -x -v tauice -m 0.2 -M 0.9 -H -b vishires 53 54 Goal: I want the same map, but projected on the sphere 55 pp.py -f diagfired.nc -x -v tauice -m 0.2 -M 0.9 -H -b vishires -p ortho 56 57 Goal: I want the same map, but projected with north polar stereographic view 58 pp.py -f diagfired.nc -x -v tauice -m 0.2 -M 0.9 -H -b vishires -p npstere 59 60 Goal: I want to save this in PNG format 61 pp.py -f diagfired.nc -x -v tauice -m 0.2 -M 0.9 -H -b vishires -p ortho -S png 62 63 Goal: I want to plot results from two simulation files next to one another 64 pp.py -f diagfired.nc,diagfired.nc -x -v tsurf 8 65 9 66 *********************************************************************************** -
trunk/UTIL/PYTHON/api_wrapper.py
r351 r392 37 37 onelevel = -99999. 38 38 39 #print input_name, output_name39 print input_name, output_name 40 40 41 41 if nocall: pass -
trunk/UTIL/PYTHON/myplot.py
r391 r392 43 43 elif 'HGT_M' in nc.variables: typefile = 'geo' 44 44 #else: errormess("whatkindfile: typefile not supported.") 45 else: typefile = 'gcm' # for lslin-ed files from gcm45 else: typefile = 'gcm' # for lslin-ed files from gcm 46 46 return typefile 47 47 … … 50 50 ## this allows to get much faster (than simply referring to nc.variables[var]) 51 51 dimension = len(nc.variables[var].dimensions) 52 print " Opening variable with", dimension, "dimensions ..."52 #print " Opening variable with", dimension, "dimensions ..." 53 53 if dimension == 2: field = nc.variables[var][:,:] 54 54 elif dimension == 3: field = nc.variables[var][:,:,:] … … 65 65 shape = np.array(input).shape 66 66 #print 'd1,d2,d3,d4: ',d1,d2,d3,d4 67 print ' dim,shape: ',dimension,shape67 print 'IN REDUCEFIELD dim,shape: ',dimension,shape 68 68 output = input 69 69 error = False … … 98 98 elif max(d1) >= shape[3]: error = True 99 99 elif d4 is not None and d3 is not None and d2 is not None and d1 is not None: 100 output = mean(input[d4,:,:,:],axis=0); output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt[d3]); output = mean(output[d2,:],axis=0); output = mean(output[d1],axis=0) 100 output = mean(input[d4,:,:,:],axis=0) 101 output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt,indice=d3) 102 output = mean(output[d2,:],axis=0) 103 output = mean(output[d1],axis=0) 101 104 elif d4 is not None and d3 is not None and d2 is not None: 102 output = mean(input[d4,:,:,:],axis=0); output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt[d3]); output = mean(output[d2,:],axis=0) 105 output = mean(input[d4,:,:,:],axis=0) 106 output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt,indice=d3) 107 output = mean(output[d2,:],axis=0) 103 108 elif d4 is not None and d3 is not None and d1 is not None: 104 output = mean(input[d4,:,:,:],axis=0); output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt[d3]); output = mean(output[:,d1],axis=1) 109 output = mean(input[d4,:,:,:],axis=0) 110 output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt,indice=d3) 111 output = mean(output[:,d1],axis=1) 105 112 elif d4 is not None and d2 is not None and d1 is not None: 106 output = mean(input[d4,:,:,:],axis=0); output = mean(output[:,d2,:],axis=1); output = mean(output[:,d1],axis=1) 113 output = mean(input[d4,:,:,:],axis=0) 114 output = mean(output[:,d2,:],axis=1) 115 output = mean(output[:,d1],axis=1) 107 116 elif d3 is not None and d2 is not None and d1 is not None: 108 output = reduce_zaxis(input[:,d3,:,:],ax=1,yint=yint,vert=alt[d3]); output = mean(output[:,d2,:],axis=1); output = mean(output[:,d1],axis=1) 109 elif d4 is not None and d3 is not None: output = mean(input[d4,:,:,:],axis=0); output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt[d3]) 110 elif d4 is not None and d2 is not None: output = mean(input[d4,:,:,:],axis=0); output = mean(output[:,d2,:],axis=1) 111 elif d4 is not None and d1 is not None: output = mean(input[d4,:,:,:],axis=0); output = mean(output[:,:,d1],axis=2) 112 elif d3 is not None and d2 is not None: output = reduce_zaxis(input[:,d3,:,:],ax=1,yint=yint,vert=alt[d3]); output = mean(output[:,d2,:],axis=1) 113 elif d3 is not None and d1 is not None: output = reduce_zaxis(input[:,d3,:,:],ax=1,yint=yint,vert=alt[d3]); output = mean(output[:,:,d1],axis=0) 114 elif d2 is not None and d1 is not None: output = mean(input[:,:,d2,:],axis=2); output = mean(output[:,:,d1],axis=2) 115 elif d1 is not None: output = mean(input[:,:,:,d1],axis=3) 116 elif d2 is not None: output = mean(input[:,:,d2,:],axis=2) 117 elif d3 is not None: output = reduce_zaxis(input[:,d3,:,:],ax=0,yint=yint,vert=alt[d3]) 118 elif d4 is not None: output = mean(input[d4,:,:,:],axis=0) 117 output = reduce_zaxis(input[:,d3,:,:],ax=1,yint=yint,vert=alt,indice=d3) 118 output = mean(output[:,d2,:],axis=1) 119 output = mean(output[:,d1],axis=1) 120 elif d4 is not None and d3 is not None: 121 output = mean(input[d4,:,:,:],axis=0) 122 output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt,indice=d3) 123 elif d4 is not None and d2 is not None: 124 output = mean(input[d4,:,:,:],axis=0) 125 output = mean(output[:,d2,:],axis=1) 126 elif d4 is not None and d1 is not None: 127 output = mean(input[d4,:,:,:],axis=0) 128 output = mean(output[:,:,d1],axis=2) 129 elif d3 is not None and d2 is not None: 130 output = reduce_zaxis(input[:,d3,:,:],ax=1,yint=yint,vert=alt,indice=d3) 131 output = mean(output[:,d2,:],axis=1) 132 elif d3 is not None and d1 is not None: 133 output = reduce_zaxis(input[:,d3,:,:],ax=1,yint=yint,vert=alt,indice=d3) 134 output = mean(output[:,:,d1],axis=0) 135 elif d2 is not None and d1 is not None: 136 output = mean(input[:,:,d2,:],axis=2) 137 output = mean(output[:,:,d1],axis=2) 138 elif d1 is not None: output = mean(input[:,:,:,d1],axis=3) 139 elif d2 is not None: output = mean(input[:,:,d2,:],axis=2) 140 elif d3 is not None: output = reduce_zaxis(input[:,d3,:,:],ax=0,yint=yint,vert=alt,indice=d3) 141 elif d4 is not None: output = mean(input[d4,:,:,:],axis=0) 119 142 dimension = np.array(output).ndim 120 143 shape = np.array(output).shape 121 print ' dim,shape: ',dimension,shape144 print 'OUT REDUCEFIELD dim,shape: ',dimension,shape 122 145 return output, error 123 146 124 ## Author: AC 125 126 def reduce_zaxis (input,ax=None,yint=False,vert=None): 147 ## Author: AC + AS 148 def reduce_zaxis (input,ax=None,yint=False,vert=None,indice=None): 127 149 from mymath import max,mean 128 150 from scipy import integrate 129 if yint :151 if yint and vert is not None and indice is not None: 130 152 if type(input).__name__=='MaskedArray': 131 153 input.set_fill_value([np.NaN]) 132 output = integrate.trapz(input.filled(),x=vert ,axis=ax)154 output = integrate.trapz(input.filled(),x=vert[indice],axis=ax) 133 155 else: 134 output = integrate.trapz(input.filled(),x=vert ,axis=ax)156 output = integrate.trapz(input.filled(),x=vert[indice],axis=ax) 135 157 else: 136 158 output = mean(input,axis=ax) … … 257 279 if cen_lat > 10.: 258 280 proj="npstere" 259 print "NP stereographic polar domain"281 #print "NP stereographic polar domain" 260 282 else: 261 283 proj="spstere" 262 print "SP stereographic polar domain"284 #print "SP stereographic polar domain" 263 285 elif map_proj == 1: 264 print "lambert projection domain"286 #print "lambert projection domain" 265 287 proj="lcc" 266 288 elif map_proj == 3: 267 print "mercator projection"289 #print "mercator projection" 268 290 proj="merc" 269 291 else: … … 460 482 elif wlat[1] >= 0.: blat = wlat[0] 461 483 elif wlat[0] <= 0.: blat = wlat[1] 462 print "blat ", blat484 #print "blat ", blat 463 485 h = 50. ## en km 464 486 radius = 3397200. … … 484 506 # step = np.min([5.,step]) 485 507 # steplon = step 486 print step487 508 m.drawmeridians(np.r_[-180.:180.:steplon], labels=[0,0,0,1], color='grey', fontsize=fontsizemer) 488 509 m.drawparallels(np.r_[-90.:90.:step], labels=[1,0,0,0], color='grey', fontsize=fontsizemer) … … 534 555 ### 535 556 if zevmin < 0. and min(fieldcalc) > 0.: zevmin = 0. 536 print " field ", min(fieldcalc), max(fieldcalc)537 print " bounds", zevmin, zevmax557 print "BOUNDS field ", min(fieldcalc), max(fieldcalc) 558 print "BOUNDS adopted ", zevmin, zevmax 538 559 return zevmin, zevmax 539 560 … … 545 566 if zevmin < 0: what_I_plot[ what_I_plot < zevmin*(1. - 1.e-7) ] = zevmin*(1. - 1.e-7) 546 567 else: what_I_plot[ what_I_plot < zevmin*(1. + 1.e-7) ] = zevmin*(1. + 1.e-7) 547 print " new min", min(what_I_plot)568 print "NEW MIN ", min(what_I_plot) 548 569 what_I_plot[ what_I_plot > 9e+35 ] = -9e+35 549 570 what_I_plot[ what_I_plot > zevmax ] = zevmax 550 print "new max ", max(what_I_plot) 551 571 print "NEW MAX ", max(what_I_plot) 552 572 return what_I_plot 553 573 … … 556 576 from mymath import max,min 557 577 lim = 0.15*0.5*(abs(max(what_I_plot))+abs(min(what_I_plot))) 558 print " on vire en dessous de", lim578 print "NO PLOT BELOW VALUE ", lim 559 579 what_I_plot [ abs(what_I_plot) < lim ] = 1.e40 560 580 return what_I_plot … … 566 586 [wlon,wlat] = [ [wlon[0]+zoom*dlon/100.,wlon[1]-zoom*dlon/100.],\ 567 587 [wlat[0]+zoom*dlat/100.,wlat[1]-zoom*dlat/100.] ] 568 print " zoom%",zoom,wlon,wlat588 print "ZOOM %",zoom,wlon,wlat 569 589 return wlon,wlat 570 590 … … 642 662 #W --> spectral ou jet 643 663 #spectral BrBG RdBu_r 644 print "predefined colorbars"664 #print "predefined colorbars" 645 665 if whichone not in whichcolorb: 646 666 whichone = "def" … … 813 833 if axis[imin] == -180 and axis[imax] == 180: 814 834 zeindex = zeindex[0:len(zeindex)-1] 815 print " whole longitude averaging asked, so last point is not taken into account."835 print "INFO: whole longitude averaging asked, so last point is not taken into account." 816 836 return zeindex 817 837 … … 828 848 shape = what_I_plot.shape 829 849 if indextime is None: 830 print " axisis time"850 print "AXIS is time" 831 851 x = time 832 852 count = count+1 833 853 if indexlon is None: 834 print " axisis lon"854 print "AXIS is lon" 835 855 if count == 0: x = lon 836 856 else: y = lon 837 857 count = count+1 838 858 if indexlat is None: 839 print " axisis lat"859 print "AXIS is lat" 840 860 if count == 0: x = lat 841 861 else: y = lat 842 862 count = count+1 843 863 if indexvert is None and dim0 is 4: 844 print " axisis vert"864 print "AXIS is vert" 845 865 if vertmode == 0: # vertical axis is as is (GCM grid) 846 866 if count == 0: x=range(len(vert)) … … 853 873 x = array(x) 854 874 y = array(y) 855 print " what_I_plot.shape", what_I_plot.shape856 print " x.shape, y.shape", x.shape, y.shape875 print "CHECK: what_I_plot.shape", what_I_plot.shape 876 print "CHECK: x.shape, y.shape", x.shape, y.shape 857 877 if len(shape) == 1: 858 878 print shape[0] … … 864 884 if shape[1] == len(y) and shape[0] == len(x) and shape[0] != shape[1]: 865 885 what_I_plot = swapaxes(what_I_plot,0,1) 866 print " swapaxes", what_I_plot.shape, shape886 print "INFO: swapaxes", what_I_plot.shape, shape 867 887 #x0 = x 868 888 #x = y … … 896 916 if slon is None and slat is None: 897 917 mapmode = 1 # in this case we plot a map, with the given projection 898 #elif proj is not None:899 # print "WARNING: you specified a", proj,\900 # "projection but asked for slices", slon,"in longitude and",slat,"in latitude"901 print "mapmode: ", mapmode902 918 903 919 return nlon, nlat, nvert, ntime, mapmode, nslices -
trunk/UTIL/PYTHON/myscript.py
r388 r392 34 34 parser.add_option('-s', '--stride', action='store',dest='ste', type="int", default=3, help='stride vectors [3]') 35 35 parser.add_option('-x', '--no-vect',action='store_false',dest='winds', default=True, help='no wind vectors') 36 parser.add_option('-n', '--num', action='store',dest='num', type="int", default=None, help='plot number (<0: plot LT -*numplot*) [ 2]')36 parser.add_option('-n', '--num', action='store',dest='num', type="int", default=None, help='plot number (<0: plot LT -*numplot*) [1]') 37 37 parser.add_option('-z', '--zoom', action='store',dest='zoom', type="float", default=None, help='zoom factor in %') 38 38 parser.add_option('-e', '--itstep', action='store',dest='it', type="int", default=None, help='stride time [4]') … … 44 44 parser.add_option('--lon', action='append',dest='slon', type="string", default=None, help='slices along lon. 2 comma-separated values: averaging') 45 45 parser.add_option('--vert', action='append',dest='svert', type="string", default=None, help='slices along vert. 2 comma-separated values: averaging') 46 parser.add_option('--column', action='store_true',dest='column', 46 parser.add_option('--column', action='store_true',dest='column', default=False,help='changes the function of --vert z1,z2 from MEAN to INTEGRATE along z') 47 47 parser.add_option('--time', action='append',dest='stime', type="string", default=None, help='slices along time. 2 comma-separated values: averaging') 48 48 parser.add_option('--xmax', action='store',dest='xmax', type="float", default=None, help='max value for x-axis in contour-plots [max(xaxis)]') -
trunk/UTIL/PYTHON/planetoplot.py
r391 r392 5 5 ### A. Spiga -- LMD -- 06~09/2011 -- General building and mapping capabilities 6 6 ### T. Navarro -- LMD -- 10~11/2011 -- Improved use for GCM and added sections + 1Dplot capabilities 7 ### A. Colaitis -- LMD -- -- Mostly minor improvements and inter-plot operation capabilities + zrecast interpolation for gcm 7 ### A. Colaitis -- LMD -- 11/2011 -- Mostly minor improvements and inter-plot operation capabilities + zrecast interpolation for gcm 8 ### A. Spiga -- LMD -- 11/2011 -- Extended multivar subplot capabilities + cosmetic changes + general cleaning and tests 8 9 9 10 def planetoplot (namefiles,\ … … 63 64 fmtvar,definecolorvec,defcolorb,getprefix,putpoints,calculate_bounds,errormess,definesubplot,\ 64 65 zoomset,getcoorddef,getwinddef,whatkindfile,reducefield,bounds,getstralt,getfield,smooth,nolow,\ 65 getname,localtime,polarinterv,getsindex,define_axis,determineplot 66 getname,localtime,polarinterv,getsindex,define_axis,determineplot,readslices 66 67 from mymath import deg,max,min,mean,get_tsat 67 68 import matplotlib as mpl 68 from matplotlib.pyplot import contour,contourf, subplot, figure, rcParams, savefig, colorbar, pcolor, show, plot, clabel 69 from matplotlib.pyplot import contour,contourf, subplot, figure, rcParams, savefig, colorbar, pcolor, show, plot, clabel, title 69 70 from matplotlib.cm import get_cmap 70 71 import numpy as np … … 72 73 73 74 ################################ 75 ### Preliminary stuff 76 ################################ 77 print "********************************************" 78 print "********** WELCOME TO PLANETOPLOT **********" 79 print "********************************************" 80 if not isinstance(namefiles, np.ndarray): namefiles = [namefiles] 81 if not isinstance(var, np.ndarray): var = [var] 82 if ope is not None and len(var) > 1: errormess("you are using an operation... please set only one var !") 83 84 ################################ 74 85 ### Which plot needs to be done? 86 ################################ 75 87 nlon, nlat, nvert, ntime, mapmode, nslices = determineplot(slon, slat, svert, stime) 76 if mapmode == 0: winds=False 77 if not isinstance(namefiles, np.ndarray): namefiles = [namefiles] 78 zelen = len(namefiles) 79 if numplot == None: numplot = zelen*nslices 80 print "len(namefiles), nslices, numplot: ", zelen, nslices, numplot 81 if fileref is not None: 82 all_var = [[]]*(zelen+2) 83 else: 84 all_var = [[]]*zelen 85 all_var2 = [[]]*zelen 86 all_title = [[]]*zelen 87 88 ######################### 89 ### Loop over the files initially separated by comas to be plotted on the same figure 88 if mapmode == 0: winds=False 89 elif mapmode == 1: 90 if svert is None: svert = readslices(str(level)) ; nvert=1 91 zelen = len(namefiles)*len(var) 92 if numplot is None: numplot = zelen*nslices 93 print "********** FILES, SLICES, VARS, TOTAL PLOTS: ", len(namefiles), nslices, len(var), numplot 94 print "********** MAPMODE: ", mapmode 95 if fileref is not None: all_var = [[]]*(zelen+2) ; all_varname = [[]]*(zelen+2) 96 else: all_var = [[]]*zelen ; all_var2 = [[]]*zelen ; all_title = [[]]*zelen ; all_varname = [[]]*zelen 97 98 ################################################################################################# 99 ### Loop over the files + vars initially separated by commas to be plotted on the same figure ### 100 ################################################################################################# 90 101 k = 0 91 102 firstfile = True 92 for namefile in namefiles: 93 print namefile 103 for nnn in range(len(namefiles)): 104 for vvv in range(len(var)): 105 106 print "********** LOOP..... THIS IS SUBPLOT NUMBER.....",k 107 94 108 ###################### 95 109 ### Load NETCDF object 110 namefile = namefiles[nnn] ; print "********** THE NAMEFILE IS....", namefile 96 111 nc = Dataset(namefile) 97 112 … … 100 115 ### ... TYPEFILE 101 116 typefile = whatkindfile(nc) 102 if firstfile: 103 typefile0 = typefile 104 elif typefile != typefile0: 105 errormess("Not the same kind of files !", [typefile0, typefile]) 117 if firstfile: typefile0 = typefile 118 elif typefile != typefile0: errormess("Not the same kind of files !", [typefile0, typefile]) 106 119 ### ... VAR 107 varname=var 108 if var not in nc.variables: var = False 120 varname=var[vvv] 121 print "********** THE VAR IS....",varname, var2 122 if varname not in nc.variables: varname = False 109 123 ### ... WINDS 110 124 if winds: 111 125 [uchar,vchar,metwind] = getwinddef(nc) 112 126 if uchar == 'not found': winds = False 113 if not var and not winds: errormess("please set at least winds or var",printvar=nc.variables)127 if not varname and not winds: errormess("please set at least winds or var",printvar=nc.variables) 114 128 ### ... COORDINATES, could be moved below 115 129 [lon2d,lat2d] = getcoorddef(nc) 116 130 ### ... PROJECTION 117 if proj == None: proj = getproj(nc) 131 if proj == None: proj = getproj(nc) 118 132 119 133 ########################################################## … … 131 145 elif typefile in ['meso','mesoapi']: 132 146 if mapmode == 0: 133 if var in ['PHTOT','W']: vertdim='BOTTOM-TOP_PATCH_END_STAG'134 else: vertdim='BOTTOM-TOP_PATCH_END_UNSTAG'135 if var in ['V']: latdim='SOUTH-NORTH_PATCH_END_STAG'136 else: latdim='SOUTH-NORTH_PATCH_END_UNSTAG'137 if var in ['U']: londim='WEST-EAST_PATCH_END_STAG'138 else: londim='WEST-EAST_PATCH_END_UNSTAG'147 if varname in ['PHTOT','W']: vertdim='BOTTOM-TOP_PATCH_END_STAG' 148 else: vertdim='BOTTOM-TOP_PATCH_END_UNSTAG' 149 if varname in ['V']: latdim='SOUTH-NORTH_PATCH_END_STAG' 150 else: latdim='SOUTH-NORTH_PATCH_END_UNSTAG' 151 if varname in ['U']: londim='WEST-EAST_PATCH_END_STAG' 152 else: londim='WEST-EAST_PATCH_END_UNSTAG' 139 153 lon = np.arange(0,getattr(nc,londim),1) 140 154 lat = np.arange(0,getattr(nc,latdim),1) 141 if vertmode is None: vertmode=0142 if vertmode == 0: vert = np.arange(0,getattr(nc,vertdim),1)143 else: vert = nc.variables["vert"][:]155 if vertmode is None: vertmode=0 156 if vertmode == 0: vert = np.arange(0,getattr(nc,vertdim),1) 157 else: vert = nc.variables["vert"][:] 144 158 time = np.arange(0,len(nc.variables["Times"]),1) 159 else: 160 lon=None ; lat=None ; vert=None ; time=None 145 161 #if firstfile: 146 162 # lat0 = lat … … 160 176 else: [wlon,wlat] = simplinterv(lon2d,lat2d) 161 177 if zoom: [wlon,wlat] = zoomset(wlon,wlat,zoom) 162 163 ######################################### 164 ### Name for title and graphics save file 165 basename = getname(var=var,winds=winds,anomaly=anomaly) 178 ### ... NAME FOR TITLE and FILE (have to do something for multiple vars...) 179 basename = getname(var=varname,winds=winds,anomaly=anomaly) 166 180 basename = basename + getstralt(nc,level) ## can be moved elsewhere for a more generic routine 167 181 168 print "var, var2: ", var, var2 169 if varname in ["temp","t","T_nadir_nit","T_nadir_day"] and var and tsat: 170 print "computing Tsat-T, I ASSUME Z-AXIS IS PRESSURE" 171 tt=getfield(nc,var) 172 if type(tt).__name__=='MaskedArray': 173 tt.set_fill_value([np.NaN]) 174 tinput=tt.filled() 175 else: 176 tinput=tt 182 ##### SPECIFIC 183 if varname in ["temp","t","T_nadir_nit","T_nadir_day"] and tsat: 184 tt=getfield(nc,varname) ; print "computing Tsat-T, I ASSUME Z-AXIS IS PRESSURE" 185 if type(tt).__name__=='MaskedArray': tt.set_fill_value([np.NaN]) ; tinput=tt.filled() 186 else: tinput=tt 177 187 all_var[k]=get_tsat(vert,tinput,zlon=lon,zlat=lat,zalt=vert,ztime=time) 178 188 else: 179 if var: all_var[k] = getfield(nc,var) 189 ##### GENERAL STUFF HERE 190 all_var[k] = getfield(nc,varname) 191 all_varname[k] = varname 180 192 if var2: all_var2[k] = getfield(nc,var2) 181 193 182 print "k", k 183 print "all_var[k].shape", all_var[k].shape 194 print "********** all_var[k].shape", all_var[k].shape 184 195 k += 1 185 196 firstfile = False … … 188 199 ################################## 189 200 ### Operation on files 201 ### ... for the moment only OK when 1 var only 190 202 if ope is not None: 191 if var not in nc.variables: var = False192 if var:193 203 print ope 194 204 if ope in ["-","+"]: 195 if fileref is not None: all_var[k] = getfield(Dataset(fileref), var)205 if fileref is not None: all_var[k] = getfield(Dataset(fileref),all_varname[k-1]) ; all_varname[k] = all_varname[k-1] 196 206 else: errormess("fileref is missing!") 197 207 if ope == "-": all_var[k+1]= all_var[k-1] - all_var[k] 198 208 elif ope == "+": all_var[k+1]= all_var[k-1] + all_var[k] 209 all_varname[k+1] = all_varname[k] 199 210 numplot = numplot+2 200 211 elif ope in ["cat"]: … … 219 230 elif numplot <= 0: itstep = 1 220 231 221 #for nplot in range(numplot):232 print "********************************************" 222 233 while error is False: 223 print "nplot", nplot 224 print error 234 print "********** nplot", nplot, "itime",itime,"error",error 225 235 226 236 ### Which local time ? … … 249 259 250 260 #################################################################### 251 if typefile in ['meso','mesoapi'] and mapmode == 1: 252 indextime = itime 253 indexlon = None 254 indexlat = None 255 indexvert = level ## ou svert ??? 256 nlon = 1 257 nlat = 1 258 nvert = 1 259 ntime = 1 261 ## get all indexes to be taken into account for this subplot and then reduce field 262 ## We plot 1) all lon slices 2) all lat slices 3) all vert slices 4) all time slices and then go to the next slice 263 indexlon = getsindex(slon,(nplot-1)%nlon,lon) 264 indexlat = getsindex(slat,((nplot-1)//nlon)%nlat,lat) 265 indexvert = getsindex(svert,((nplot-1)//(nlon*nlat))%nvert,vert) 266 if mapmode == 1 and stime is None: 267 indextime = itime 268 if len(var) != 1 or len(namefiles) != 1: indextime = first 260 269 else: 261 ## get all indexes to be taken into account for this subplot and then reduce field262 ## We plot 1) all lon slices 2) all lat slices 3) all vert slices 4) all time slices and then go to the next slice263 indexlon = getsindex(slon,(nplot-1)%nlon,lon)264 indexlat = getsindex(slat,((nplot-1)//nlon)%nlat,lat)265 indexvert = getsindex(svert,((nplot-1)//(nlon*nlat))%nvert,vert)266 270 indextime = getsindex(stime,((nplot-1)//(nlon*nlat*nvert))%ntime,time) 267 271 268 272 if fileref is not None: 269 index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%(len(namefiles)+2) 273 index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%(len(namefiles)+2) ## OK only 1 var, see test in the beginning 270 274 else: 271 index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%len(namefiles) 272 273 print nlon, nlat, nvert, ntime 274 print slon, slat, svert, stime 275 print index_f 276 print "index lon,lat,vert,time", max(indexlon), max(indexlat), max(indexvert), max(indextime) 275 yeah = len(namefiles)*len(var) 276 index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%yeah 277 #print nlon, nlat, nvert, ntime 278 #print slon, slat, svert, stime 279 #print index_f 280 #print "index lon,lat,vert,time", max(indexlon), max(indexlat), max(indexvert), max(indextime) 281 #if varname != all_varname[index_f]: indextime = first 277 282 #################################################################### 283 284 ticks = ndiv + 1 278 285 279 286 #### Contour plot … … 285 292 if typefile in ['mesoapi','meso']: what_I_plot = dumpbdy(what_I_plot,6) 286 293 zevmin, zevmax = calculate_bounds(what_I_plot) 287 zelevels = np.linspace(zevmin,zevmax, num=ndiv+1) #20)294 zelevels = np.linspace(zevmin,zevmax,ticks) #20) 288 295 if var2 == 'HGT': zelevels = np.arange(-10000.,30000.,2000.) 289 296 if mapmode == 0: … … 293 300 if len(what_I_plot.shape) is 2: 294 301 cs = contour(x,y,what_I_plot, zelevels, colors='k', linewidths = 1 ) #0.33 colors='w' )# , alpha=0.5) 295 if typefile in ['gcm']: clabel(cs,fmt = '%1.2e')302 #if typefile in ['gcm']: clabel(cs,fmt = '%1.2e') 296 303 ### If we plot a 1-D field 297 304 elif len(what_I_plot.shape) is 1: … … 301 308 302 309 #### Shaded plot 303 if var: 310 varname = all_varname[index_f] 311 if varname: 304 312 what_I_plot, error = reducefield(all_var[index_f], d4=indextime, d1=indexlon, d2=indexlat , d3=indexvert , yint=yintegral, alt=vert) 305 313 what_I_plot = what_I_plot*mult 306 314 if not error: 307 fvar = var 315 fvar = varname 308 316 ### 309 317 if anomaly: … … 329 337 if not hole: what_I_plot = bounds(what_I_plot,zevmin,zevmax) 330 338 #zelevels = np.linspace(zevmin*(1. + 1.e-7),zevmax*(1. - 1.e-7)) #,num=20) 331 zelevels = np.linspace(zevmin,zevmax,num= 2.*ndiv+1)339 zelevels = np.linspace(zevmin,zevmax,num=ticks) 332 340 #contourf(what_I_plot, zelevels, cmap = palette ) 333 341 … … 336 344 elif mapmode == 0: 337 345 contourf( x, y, what_I_plot, zelevels, cmap = palette) 346 338 347 zxmin, zxmax = xaxis 339 348 zymin, zymax = yaxis 340 if zxmin is not None: 341 mpl.pyplot.xlim(xmin=zxmin) 342 if zxmax is not None: 343 mpl.pyplot.xlim(xmax=zxmax) 344 if zymin is not None: 345 mpl.pyplot.ylim(ymin=zymin) 346 if zymax is not None: 347 mpl.pyplot.ylim(ymax=zymax) 348 349 if invert_y: 350 lima,limb = mpl.pyplot.ylim() 351 mpl.pyplot.ylim(limb,lima) 352 if ylog: 353 mpl.pyplot.semilogy() 349 if zxmin is not None: mpl.pyplot.xlim(xmin=zxmin) 350 if zxmax is not None: mpl.pyplot.xlim(xmax=zxmax) 351 if zymin is not None: mpl.pyplot.ylim(ymin=zymin) 352 if zymax is not None: mpl.pyplot.ylim(ymax=zymax) 353 if invert_y: lima,limb = mpl.pyplot.ylim() ; mpl.pyplot.ylim(limb,lima) 354 if ylog: mpl.pyplot.semilogy() 354 355 355 356 else: 356 357 if hole: what_I_plot = nolow(what_I_plot) 357 358 pcolor( x, y, what_I_plot, cmap = palette, vmin=zevmin, vmax=zevmax ) 358 if colorb != 'nobar' and var != 'HGT' :359 if colorb != 'nobar' and varname != 'HGT' : 359 360 if (fileref is not None) and (index_f is numplot-1): 360 361 colorbar(fraction=0.05,pad=0.03,format="%.2f",\ 361 ticks=np.linspace(zevmin,zevmax, min([ndiv+1,20])),\362 ticks=np.linspace(zevmin,zevmax,num=min([ticks/2+1,20])),\ 362 363 extend='neither',spacing='proportional') 363 364 else: 364 365 colorbar(fraction=0.05,pad=0.03,format=fmtvar(fvar.upper()),\ 365 ticks=np.linspace(zevmin,zevmax, min([ndiv+1,10])),\366 ticks=np.linspace(zevmin,zevmax,num=min([ticks/2+1,20])),\ 366 367 extend='neither',spacing='proportional') 367 368 … … 391 392 key = False 392 393 if metwind: [vecx,vecy] = m.rotate_vector(vecx, vecy, lon2d, lat2d) 393 if var == False: colorvec = definecolorvec(back)394 if varname == False: colorvec = definecolorvec(back) 394 395 else: colorvec = definecolorvec(colorb) 395 396 vectorfield(vecx, vecy,\ … … 400 401 401 402 ### Next subplot 403 basename = getname(var=varname,winds=winds,anomaly=anomaly) 402 404 if typefile in ['mesoapi','meso']: 403 405 plottitle = basename … … 411 413 plottitle = basename+' '+fileref 412 414 else: 413 plottitle = basename+' '+namefiles[ index_f]415 plottitle = basename+' '+namefiles[0]#index_f] 414 416 else: 415 plottitle = basename+' '+namefiles[ index_f]417 plottitle = basename+' '+namefiles[0]#index_f] 416 418 if mult != 1: plottitle = str(mult) + "*" + plottitle 417 419 if zetitle != "fill": … … 432 434 # if indextime is not None: 433 435 # plottitle = plottitle + " time: " + str(min(time[indextime])) +" "+ str(max(time[indextime])) 434 ptitle( plottitle )436 title( plottitle ) 435 437 itime += itstep 436 438 if nplot >= numplot: … … 464 466 if found_lct: 465 467 pad_inches_value = 0.35 466 print " save", save468 print "********** SAVE ", save 467 469 if save == 'png': 468 470 if display: makeplotres(zeplot,res=100.,pad_inches_value=pad_inches_value) #,erase=True) ## a miniature … … 473 475 show() 474 476 else: 475 print " save mode not supported. using gui instead."477 print "INFO: save mode not supported. using gui instead." 476 478 show() 477 else: print " Local time not found"479 else: print "!!! Local time not found" 478 480 479 481 ############### -
trunk/UTIL/PYTHON/pp.py
r391 r392 1 1 #!/usr/bin/env python 2 2 3 ### A. Spiga 3 ### A. Spiga + T. Navarro + A. Colaitis 4 4 5 5 ########################################################################################### … … 10 10 from optparse import OptionParser ### to be replaced by argparse 11 11 from api_wrapper import api_onelevel 12 from zrecast_wrapper import call_zrecast 12 13 from netCDF4 import Dataset 13 from myplot import getlschar, separatenames, readslices, adjust_length 14 from myplot import getlschar, separatenames, readslices, adjust_length, whatkindfile, errormess 14 15 from os import system 15 16 from planetoplot import planetoplot … … 19 20 ############################# 20 21 ### Get options and variables 21 parser = OptionParser() 22 getparseroptions(parser) 23 (opt,args) = parser.parse_args() 22 parser = OptionParser() ; getparseroptions(parser) ; (opt,args) = parser.parse_args() 23 if opt.file is None: errormess("I want to eat one file at least ! Use winds.py -f name_of_my_file. Or type winds.py -h") 24 if opt.var is None and opt.anomaly is True: errormess("Cannot ask to compute anomaly if no variable is set") 25 if opt.fref is not None and opt.operat is None: errormess("you must specify an operation when using a reference file") 26 if opt.operat in ["+","-"] and opt.fref is None: errormess("you must specifiy a reference file when using inter-file operations") 27 if opt.fref is not None and opt.operat is not None and opt.itp is not None: interpref=True 28 else: interpref=False 24 29 25 if opt.file is None: 26 print "I want to eat one file at least ! Use winds.py -f name_of_my_file. Or type winds.py -h" 27 exit() 28 if opt.var is None and opt.anomaly is True: 29 print "Cannot ask to compute anomaly if no variable is set" 30 exit() 31 print "Options:", opt 30 ############################# 31 ### Get infos about slices 32 zeslat = readslices(opt.slat) ; zeslon = readslices(opt.slon) ; zesvert = readslices(opt.svert) ; zestime = readslices(opt.stime) 33 reffile = opt.fref 34 zexaxis = [opt.xmin,opt.xmax] ; zeyaxis=[opt.ymin,opt.ymax] 32 35 33 listvar = '' 34 if opt.var is None: 35 zerange = [-999999] 36 else: 37 zelen = len(opt.var) 38 zerange = range(zelen) 39 #if zelen == 1: listvar = opt.var[0] + ',' 40 #else : 41 for jjj in zerange: listvar += opt.var[jjj] + ',' 42 listvar = listvar[0:len(listvar)-1] 43 vmintab = adjust_length (opt.vmin, zelen) 44 vmaxtab = adjust_length (opt.vmax, zelen) 45 46 47 ################################ 48 ### General check 49 50 if opt.fref is not None: 51 if opt.operat is None: 52 print "you must specify an operation when using a reference file" 53 exit() 54 if opt.operat in ["+","-"]: 55 if opt.fref is None: 56 print "you must specifiy a reference file when using inter-file operations" 57 exit() 58 59 interpref=False 60 if opt.fref is not None: 61 if opt.operat is not None: 62 if opt.itp is not None: 63 interpref=True 64 65 ################################ 66 67 68 print "file, length", opt.file, len(opt.file) 69 70 zeslat = readslices(opt.slat) 71 zeslon = readslices(opt.slon) 72 zesvert = readslices(opt.svert) 73 zestime = readslices(opt.stime) 74 print "slat,zeslat", opt.slat, zeslat 75 print "slon,zeslon", opt.slon, zeslon 76 print "svert,zesvert", opt.svert, zesvert 77 print "stime,zestime", opt.stime, zestime 78 36 ############################# 37 ### 1. LOOP ON FILE LISTS TO BE PUT IN DIFFERENT FIGURES 79 38 for i in range(len(opt.file)): 80 39 81 zefiles = separatenames(opt.file[i]) 82 print "zefiles", zefiles 40 zefiles = separatenames(opt.file[i]) 83 41 84 #zefile = opt.file[i] 85 #print zefile 86 zefile = zefiles[0] 87 88 #zelevel = opt.lvl 89 stralt = None 90 [lschar,zehour,zehourin] = getlschar ( zefile ) ## getlschar from wrfout (or simply return "" if another file) 91 42 typefile = whatkindfile(Dataset(zefiles[0])) 43 stralt = None 44 if typefile in ["meso","mesoapi"]: 45 [lschar,zehour,zehourin] = getlschar ( zefiles[0] ) 46 if opt.var is None: opt.var = ["HGT"] 47 else: 48 [lschar,zehour,zehourin] = ["",0,0] 49 if opt.var is None: opt.var = ["phisinit"] 50 51 if opt.vmin is not None : zevmin = opt.vmin[min(i,len(opt.vmin)-1)] 52 else: zevmin = None 53 if opt.vmax is not None : zevmax = opt.vmax[min(i,len(opt.vmax)-1)] 54 else: zevmax = None 55 #print "vmin, zevmin", opt.vmin, zevmin ; print "vmax, zevmax", opt.vmax, zevmax 56 57 ############################# 58 ### 2. LOOP ON VAR LISTS TO BE PUT IN DIFFERENT FIGURES 59 for j in range(len(opt.var)): 60 61 zevars = separatenames(opt.var[j]) 62 92 63 inputnvert = separatenames(opt.lvl) 93 64 if np.array(inputnvert).size == 1: … … 97 68 zelevel = -99. 98 69 ze_interp_levels = np.linspace(float(inputnvert[0]),float(inputnvert[1]),float(inputnvert[2])) 99 print 'level: ', zelevel100 print 'interp_levels: ',ze_interp_levels101 70 102 ##################################################### 103 ### Call Fortran routines for vertical interpolations 71 ######################################################### 72 ### Call Fortran routines for vertical interpolations ### 73 ######################################################### 104 74 if opt.itp is not None: 75 ##### 76 ##### MESOSCALE : written by AS 77 ##### 78 if typefile == "meso": 105 79 if zelevel == 0. and opt.itp == 4: zelevel = 0.010 106 80 ### winds or no winds … … 108 82 else : zefields = '' 109 83 ### var or no var 110 #if opt.var is None : pass 111 if zefields == '' : zefields = listvar 112 else : zefields = zefields + "," + listvar 113 if opt.var2 is not None : zefields = zefields + "," + opt.var2 114 print zefields 115 zefile = api_onelevel ( path_to_input = '', \ 116 input_name = zefile, \ 117 fields = zefields, \ 118 interp_method = opt.itp, \ 119 interp_level = ze_interp_levels, \ 120 onelevel = zelevel, \ 121 nocall = opt.nocall ) 122 print zefile 84 if zefields == '' : zefields = opt.var[j] 85 else : zefields = zefields + "," + opt.var[j] 86 if opt.var2 is not None : zefields = zefields + "," + opt.var2 87 ### call fortran routines 88 for fff in range(len(zefiles)): 89 newname = api_onelevel ( path_to_input = '', \ 90 input_name = zefiles[fff], \ 91 fields = zefields, \ 92 interp_method = opt.itp, \ 93 interp_level = ze_interp_levels, \ 94 onelevel = zelevel, \ 95 nocall = opt.nocall ) 96 if fff == 0: zetab = newname 97 else: zetab = np.append(zetab,newname) 98 zefiles = zetab #; print zefiles 123 99 zelevel = 0 ## so that zelevel could play again the role of nvert 100 ##### 101 ##### GCM : written by AC 102 ##### 103 elif typefile == "gcm": 104 interpolated_files="" 105 interpolated_files=call_zrecast(interp_mode=opt.itp,\ 106 input_name=zefiles,\ 107 fields=zevars) 124 108 125 if opt.var is None: zerange = [-999999] 126 else: zerange = range(zelen) 127 128 129 130 131 132 133 134 135 if interpref: 136 interpolated_ref="" 137 interpolated_ref=call_zrecast(interp_mode=opt.itp,\ 109 zefiles=interpolated_files 110 if interpref: 111 interpolated_ref="" 112 interpolated_ref=call_zrecast(interp_mode=opt.itp,\ 138 113 input_name=[opt.fref],\ 139 114 fields=zevars) 140 115 141 reffile=interpolated_ref[0] 142 else: 143 reffile=opt.fref 144 # Divers #################################################### 145 146 zexaxis=[opt.xmin,opt.xmax] 147 zeyaxis=[opt.ymin,opt.ymax] 116 reffile=interpolated_ref[0] 117 else: 118 print "not supported" 119 exit() 148 120 149 for jjj in zerange: 150 if jjj == -999999: 151 argvar = None 152 zevmin = None 153 zevmax = None 154 else: 155 argvar = opt.var[jjj] 156 if vmintab[jjj] != -999999: zevmin = vmintab[jjj] 157 else: zevmin = None 158 if vmaxtab[jjj] != -999999: zevmax = vmaxtab[jjj] 159 else: zevmax = None 160 161 ############# 162 ### Main call 163 name = planetoplot (zefiles,level=int(zelevel),vertmode=opt.itp,\ 164 proj=opt.proj,back=opt.back,target=opt.tgt,stride=opt.ste,var=argvar,\ 121 ############# 122 ### Main call 123 name = planetoplot (zefiles,level=int(zelevel),vertmode=opt.itp,\ 124 proj=opt.proj,back=opt.back,target=opt.tgt,stride=opt.ste,var=zevars,\ 165 125 numplot=opt.num,colorb=opt.clb,winds=opt.winds,\ 166 126 addchar=lschar,interv=[zehour,zehourin],vmin=zevmin,vmax=zevmax,\ … … 173 133 ope=opt.operat,fileref=reffile,minop=opt.vminope,maxop=opt.vmaxope,titleref=opt.titref,\ 174 134 invert_y=opt.inverty,xaxis=zexaxis,yaxis=zeyaxis,ylog=opt.logy,yintegral=opt.column,\ 175 blat=opt.blat,tsat=opt.ts tat)176 print 'D one: '+name135 blat=opt.blat,tsat=opt.tsat) 136 print 'DONE: '+name 177 137 system("rm -f to_be_erased") 178 138 179 139 ######################################################### 180 140 ### Generate a .sh file with the used command saved in it 181 command = "" 141 command = "" 182 142 for arg in sys.argv: command = command + arg + ' ' 143 if typefile not in ["meso","mesoapi"]: name = 'pycommand' 183 144 f = open(name+'.sh', 'w') 184 145 f.write(command) 146 147 print "********** OPTIONS: ", opt 148 print "********************************************************** END"
Note: See TracChangeset
for help on using the changeset viewer.