Changeset 448 for trunk/UTIL


Ignore:
Timestamp:
Dec 2, 2011, 3:57:27 PM (13 years ago)
Author:
aslmd
Message:

GRAPHICS: coded a more rational way for contours and shaded plots. that is, no more need for call_contour and no more (or not too much) duplicates between contour part and shaded part. working well with static or animated plots.

Location:
trunk/UTIL/PYTHON
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/UTIL/PYTHON/README.PP

    r436 r448  
    6868
    6969Goal: I want to plot results for two different times in the file next to one another
    70 pp.py -f diagfi.nc -v tsurf --time 4 -- time 7
     70pp.py -f diagfi.nc -v tsurf --time 4 --time 7
    7171
    7272Goal: I want to plot averaged results in the file from one time to another time
  • trunk/UTIL/PYTHON/myplot.py

    r447 r448  
    158158        elif d3 is not None and d1 is not None: 
    159159             output = reduce_zaxis(input[:,d3,:,:],ax=1,yint=yint,vert=alt,indice=d3)
    160              output = mean(output[:,:,d1],axis=0)
     160             output = mean(output[:,:,d1],axis=2)
    161161        elif d2 is not None and d1 is not None: 
    162162             output = mean(input[:,:,d2,:],axis=2)
     
    10361036    return nlon, nlat, nvert, ntime, mapmode, nslices
    10371037
    1038 # Author: AC
    1039 # Reduce complexity of main script by moving the contour part here. Also allow to call it from elsewhere
    1040 # which can be usefull
    1041 
    1042 def call_contour(what_I_plot,error,x,y,m,lon,lat,vert,time,vertmode,ze_var2,indextime,indexlon,indexlat,indexvert,yintegral,mapmode,typefile,var2,ticks):
    1043       from matplotlib.pyplot import contour, plot, clabel
    1044       import numpy as np
    1045       #what_I_plot = what_I_plot*mult
    1046       if not error:
    1047          if mapmode == 1:
    1048              if typefile in ['mesoapi','meso']:    what_I_plot = dumpbdy(what_I_plot,6)
    1049          zevmin, zevmax = calculate_bounds(what_I_plot)
    1050          zelevels = np.linspace(zevmin,zevmax,ticks) #20)
    1051          if var2 == 'HGT':  zelevels = np.arange(-10000.,30000.,2000.)
    1052          if mapmode == 0:
    1053              #if typefile in ['mesoideal']:    what_I_plot = dumpbdy(what_I_plot,0,stag='W')
    1054              itime=indextime
    1055              if len(what_I_plot.shape) is 3: itime=[0]
    1056              what_I_plot, x, y = define_axis(lon,lat,vert,time,indexlon,indexlat,indexvert,\
    1057                    itime,what_I_plot, len(ze_var2.shape),vertmode)
    1058          ### If we plot a 2-D field
    1059          if len(what_I_plot.shape) is 2:
    1060              #zelevels=[1.]
    1061              if mapmode == 0:cs = contour(x,y,what_I_plot, zelevels, colors='k', linewidths = 1 ) #0.33 colors='w' )# , alpha=0.5)
    1062              elif mapmode == 1:cs = m.contour(x,y,what_I_plot, zelevels, colors='k', linewidths = 1 ) #0.33 colors='w' )# , alpha=0.5)
    1063              #clabel(cs,zelevels,inline=3,fmt='%1.1f',fontsize=7)
    1064          ### If we plot a 1-D field
    1065          elif len(what_I_plot.shape) is 1:
    1066              plot(what_I_plot,x)
    1067       else:
    1068          errormess("There is an error in reducing field !")
    1069       return error
    1070 
     1038## Author: AC
     1039## Reduce complexity of main script by moving the contour part here. Also allow to call it from elsewhere
     1040## which can be usefull
     1041#
     1042#def call_contour(what_I_plot,error,x,y,m,lon,lat,vert,time,vertmode,ze_var2,indextime,indexlon,indexlat,indexvert,yintegral,mapmode,typefile,var2,ticks):
     1043#      from matplotlib.pyplot import contour, plot, clabel
     1044#      import numpy as np
     1045#      #what_I_plot = what_I_plot*mult
     1046#      if not error:
     1047#         if mapmode == 1:
     1048#             if typefile in ['mesoapi','meso']:    what_I_plot = dumpbdy(what_I_plot,6)
     1049#         zevmin, zevmax = calculate_bounds(what_I_plot)
     1050#         zelevels = np.linspace(zevmin,zevmax,ticks) #20)
     1051#         if var2 == 'HGT':  zelevels = np.arange(-10000.,30000.,2000.)
     1052#         if mapmode == 0:
     1053#             #if typefile in ['mesoideal']:    what_I_plot = dumpbdy(what_I_plot,0,stag='W')
     1054#             itime=indextime
     1055#             if len(what_I_plot.shape) is 3: itime=[0]
     1056#             what_I_plot, x, y = define_axis(lon,lat,vert,time,indexlon,indexlat,indexvert,\
     1057#                   itime,what_I_plot, len(ze_var2.shape),vertmode)
     1058#         ### If we plot a 2-D field
     1059#         if len(what_I_plot.shape) is 2:
     1060#             #zelevels=[1.]
     1061#             if mapmode == 0:cs = contour(x,y,what_I_plot, zelevels, colors='k', linewidths = 1 ) #0.33 colors='w' )# , alpha=0.5)
     1062#             elif mapmode == 1:cs = m.contour(x,y,what_I_plot, zelevels, colors='k', linewidths = 1 ) #0.33 colors='w' )# , alpha=0.5)
     1063#             #clabel(cs,zelevels,inline=3,fmt='%1.1f',fontsize=7)
     1064#         ### If we plot a 1-D field
     1065#         elif len(what_I_plot.shape) is 1:
     1066#             plot(what_I_plot,x)
     1067#      else:
     1068#         errormess("There is an error in reducing field !")
     1069#      return error
     1070
  • trunk/UTIL/PYTHON/planetoplot.py

    r447 r448  
    66### T. Navarro   -- LMD -- 10~11/2011 -- Improved use for GCM and added sections + 1Dplot capabilities
    77### 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### A. Spiga     -- LMD -- 11~12/2011 -- Extended multivar subplot capabilities + cosmetic changes + general cleaning and tests
    99### A. Colaitis  -- LMD --    12/2011 -- Added movie capability [mencoder must be installed]
    1010
     
    6565                       fmtvar,definecolorvec,defcolorb,getprefix,putpoints,calculate_bounds,errormess,definesubplot,\
    6666                       zoomset,getcoorddef,getwinddef,whatkindfile,reducefield,bounds,getstralt,getfield,smooth,nolow,\
    67                        getname,localtime,polarinterv,getsindex,define_axis,determineplot,readslices,bidimfind,getlschar,hole_bounds,\
    68                        call_contour
     67                       getname,localtime,polarinterv,getsindex,define_axis,determineplot,readslices,bidimfind,getlschar,hole_bounds
    6968    from mymath import deg,max,min,mean,get_tsat,writeascii,fig2data,fig2img
    7069    import matplotlib as mpl
     
    243242    #################################
    244243    ### Time loop for plotting device
    245     nplot = 1 ; error = False
     244    nplot = 1 ; error = False 
    246245    print "********************************************"
    247246    while error is False:
     
    253252
    254253       ### Map projection                   
    255        if mapmode == 1:
    256            m = define_proj(proj,wlon,wlat,back=back,blat=blat)
    257            x, y = m(lon2d, lat2d)
    258        elif mapmode ==0:
    259            m = None ; x = None ; y = None
     254       if mapmode == 1:     m = define_proj(proj,wlon,wlat,back=back,blat=blat) ; x, y = m(lon2d, lat2d)
     255       elif mapmode ==0:    m = None ; x = None ; y = None
    260256
    261257       ####################################################################
     
    282278       print "********** index lon, lat, vert, time ",indexlon,indexlat,indexvert,indextime
    283279       ####################################################################
    284 
    285        ticks = ndiv + 1
    286 
    287        #### Contour plot. TBD: var2 could be just another instance of all_var
    288        if var2:  what_I_plot_contour, error = reducefield( all_var2[index_f], d4=indextime, d1=indexlon, d2=indexlat , d3=indexvert, \
    289                                                            yint=yintegral, alt=vert )
    290 
    291        #### Shaded plot
     280       error = False
    292281       varname = all_varname[index_f]
    293        if varname:
     282       if varname:   ### what is shaded.
    294283           what_I_plot, error = reducefield( all_var[index_f], d4=indextime, d1=indexlon, d2=indexlat, d3=indexvert, \
    295284                                             yint=yintegral, alt=vert, anomaly=anomaly )
    296285           what_I_plot = what_I_plot*mult
    297 #           if typefile in ['mesoideal']:    what_I_plot = dumpbdy(what_I_plot,0,stag='W')
    298            if not error:
     286       if var2:      ### what is contoured
     287           what_I_plot_contour, error = reducefield( all_var2[index_f], d4=indextime, d1=indexlon, d2=indexlat , d3=indexvert, \
     288                                             yint=yintegral, alt=vert )
     289       ####################################################################
     290
     291       if not error:
     292               ticks = ndiv + 1
    299293               fvar = varname
    300294               if anomaly: fvar = 'anomaly'
     
    317311               elif (fileref is not None) and (index_f is numplot-1):  palette = get_cmap(name="RdBu_r")
    318312               else:                                                   palette = get_cmap(name=colorb)
     313
    319314               ##### simple 2D field and movies of 2D fields
    320315               if len(what_I_plot.shape) >= 2:
    321316                 if (len(what_I_plot.shape) is 3 and mrate is None):  errormess("3D field. Use --rate RATE for movie or specify --time TIME. Exit.")
    322                  istart=0
    323317                 if mrate is not None: iend=len(time)-1
    324                  else:                 iend=istart
    325                  imov=istart
     318                 else:                 iend=0
     319                 imov = 0
     320                 if var2:  which = "contour" ## have to start with contours rather than shading
     321                 else:     which = "regular"
    326322                 while imov <= iend:
    327                     what_I_plot_frame = what_I_plot
    328                     if mrate is not None:     
    329                         what_I_plot_frame = what_I_plot[imov,:,:] ; print "-> frame ",imov+1
    330                         if mapmode == 1:
     323                    print "-> frame ",imov+1, which
     324                    if which == "regular":   
     325                        if mrate is None:                                   what_I_plot_frame = what_I_plot
     326                        else:                                               what_I_plot_frame = what_I_plot[imov,:,:]
     327                    elif which == "contour": 
     328                        if mrate is None or what_I_plot_contour.ndim < 3:   what_I_plot_frame = what_I_plot_contour
     329                        else:                                               what_I_plot_frame = what_I_plot_contour[imov,:,:]
     330                    if mrate is not None:     
     331                        if mapmode == 1:
    331332                            m = define_proj(proj,wlon,wlat,back=back,blat=blat)  ## this is dirty, defined above but out of imov loop
    332333                            x, y = m(lon2d, lat2d)                               ## this is dirty, defined above but out of imov loop
     
    334335                    if typefile in ['mesoideal']:    what_I_plot_frame = dumpbdy(what_I_plot_frame,0,stag='W',condition=dumped_vert_stag)
    335336
    336                     if hole:         what_I_plot_frame = hole_bounds(what_I_plot_frame,zevmin,zevmax)
    337                     else:            what_I_plot_frame = bounds(what_I_plot_frame,zevmin,zevmax)
    338                     if flagnolow:    what_I_plot_frame = nolow(what_I_plot_frame)
    339 
    340337                    if imov >= 0:
    341                        # Renew axis directives for movie frames which are not the first one.
    342                        zxmin, zxmax = xaxis ; zymin, zymax = yaxis
    343                        if zxmin is not None: mpl.pyplot.xlim(xmin=zxmin)
    344                        if zxmax is not None: mpl.pyplot.xlim(xmax=zxmax)
    345                        if zymin is not None: mpl.pyplot.ylim(ymin=zymin)
    346                        if zymax is not None: mpl.pyplot.ylim(ymax=zymax)
    347                        if invert_y:     lima,limb = mpl.pyplot.ylim() ; mpl.pyplot.ylim(limb,lima)
    348                        if ylog:         mpl.pyplot.semilogy()
    349                     # Renew contours for movie frames which are not the first one
    350                     if var2:
    351                          what_I_plot_contour_frame = what_I_plot_contour
    352                          if mrate is not None: what_I_plot_contour_frame = what_I_plot_contour[imov,:,:]
    353                          call_contour( what_I_plot_contour_frame,error,x,y,m,lon,lat,vert,time,vertmode,\
    354                                        all_var2[index_f],imov,indexlon,indexlat,indexvert,yintegral,mapmode,typefile, var2, ticks )
    355                     if not tile:
    356                         #zelevels = np.linspace(zevmin*(1. + 1.e-7),zevmax*(1. - 1.e-7)) #,num=20)
    357                         zelevels = np.linspace(zevmin,zevmax,num=ticks)
    358                         if imov is 0:          print np.array(x).shape, np.array(y).shape, np.array(what_I_plot_frame).shape
    359                         if mapmode == 1:       m.contourf( x, y, what_I_plot_frame, zelevels, cmap = palette)
    360                         elif mapmode == 0:     contourf( x, y, what_I_plot_frame, zelevels, cmap = palette)
    361                     else:
    362                         if mapmode == 1:       m.pcolor( x, y, what_I_plot_frame, cmap = palette, vmin=zevmin, vmax=zevmax )
    363                         elif mapmode == 0:     pcolor( x, y, what_I_plot_frame, cmap = palette, vmin=zevmin, vmax=zevmax )
    364                     if colorb != 'nobar':       
    365                         if (fileref is not None) and (index_f is numplot-1):   daformat = "%.3f"
    366                         else:                                                  daformat = fmtvar(fvar.upper())
    367                         colorbar( fraction=0.05,pad=0.03,format=daformat,\
    368                                   ticks=np.linspace(zevmin,zevmax,num=min([ticks/2+1,20])),extend='neither',spacing='proportional' )
    369                     ### movie stuff
    370                     figframe=mpl.pyplot.gcf()
    371                     if mrate is not None:
    372                        if mquality: figframe.set_dpi(600.)
    373                        else: figframe.set_dpi(200.)
    374                     mframe=fig2img(figframe)
    375                     if ((mrate is not None) and (imov is 0)):
    376                         moviename='test'
    377                         W,H = figframe.canvas.get_width_height()
    378                         video = VideoSink((H,W), moviename, rate=mrate, byteorder="rgba")
    379                     if mrate is not None:
    380                         video.run(mframe)
    381                         mpl.pyplot.close()
    382                     ### movie stuff
    383                     imov=imov+1
     338                        # Renew axis directives for movie frames which are not the first one.
     339                        zxmin, zxmax = xaxis ; zymin, zymax = yaxis
     340                        if zxmin is not None: mpl.pyplot.xlim(xmin=zxmin)
     341                        if zxmax is not None: mpl.pyplot.xlim(xmax=zxmax)
     342                        if zymin is not None: mpl.pyplot.ylim(ymin=zymin)
     343                        if zymax is not None: mpl.pyplot.ylim(ymax=zymax)
     344                        if invert_y:     lima,limb = mpl.pyplot.ylim() ; mpl.pyplot.ylim(limb,lima)
     345                        if ylog:         mpl.pyplot.semilogy()
     346                   
     347                    if which == "regular":
     348                        if hole:         what_I_plot_frame = hole_bounds(what_I_plot_frame,zevmin,zevmax)
     349                        else:            what_I_plot_frame = bounds(what_I_plot_frame,zevmin,zevmax)
     350                        if flagnolow:    what_I_plot_frame = nolow(what_I_plot_frame)
     351                        if not tile:
     352                            #zelevels = np.linspace(zevmin*(1. + 1.e-7),zevmax*(1. - 1.e-7)) #,num=20)
     353                            zelevels = np.linspace(zevmin,zevmax,num=ticks)
     354                            if mapmode == 1:       m.contourf( x, y, what_I_plot_frame, zelevels, cmap = palette)
     355                            elif mapmode == 0:     contourf( x, y, what_I_plot_frame, zelevels, cmap = palette)
     356                        else:
     357                            if mapmode == 1:       m.pcolor( x, y, what_I_plot_frame, cmap = palette, vmin=zevmin, vmax=zevmax )
     358                            elif mapmode == 0:     pcolor( x, y, what_I_plot_frame, cmap = palette, vmin=zevmin, vmax=zevmax )
     359                        if colorb != 'nobar':       
     360                            if (fileref is not None) and (index_f is numplot-1):   daformat = "%.3f"
     361                            else:                                                  daformat = fmtvar(fvar.upper())
     362                            colorbar( fraction=0.05,pad=0.03,format=daformat,\
     363                                      ticks=np.linspace(zevmin,zevmax,num=min([ticks/2+1,20])),extend='neither',spacing='proportional' )
     364                    elif which == "contour":
     365                        zevminc, zevmaxc = calculate_bounds(what_I_plot_frame)
     366                        zelevels = np.linspace(zevminc,zevmaxc,ticks/2) #20)
     367                        if var2 == 'HGT': zelevels = np.arange(-10000.,30000.,2000.)
     368                        if mapmode == 0:   
     369                            what_I_plot_frame, x, y = define_axis( lon,lat,vert,time,indexlon,indexlat,indexvert,\
     370                                                              itime,what_I_plot_frame, len(all_var2[index_f].shape),vertmode )
     371                            cs = contour( x,y,what_I_plot_frame, zelevels, colors='k', linewidths = 1 ) #0.33 colors='w' )# , alpha=0.5)
     372                        elif mapmode == 1:  cs = m.contour( x,y,what_I_plot_frame, zelevels, colors='k', linewidths = 1 ) #0.33 colors='w' )# , alpha=0.5)
     373
     374
     375                    if which == "regular":
     376                        if mrate is not None:
     377                           figframe=mpl.pyplot.gcf()
     378                           if mquality:   figframe.set_dpi(600.)
     379                           else:          figframe.set_dpi(200.)
     380                           mframe=fig2img(figframe)
     381                           if ((mrate is not None) and (imov is 0)):
     382                              moviename='test' ;W,H = figframe.canvas.get_width_height()
     383                              video = VideoSink((H,W), moviename, rate=mrate, byteorder="rgba")
     384                           video.run(mframe) ; mpl.pyplot.close()
     385                        if var2:  which = "contour"
     386                        imov = imov+1
     387                    elif which == "contour":
     388                        which = "regular"
     389
    384390                 if mrate is not None: video.close()
     391
    385392               ##### 1D field
    386393               elif len(what_I_plot.shape) is 1:
     
    392399                 print "WARNING!!! ",len(what_I_plot.shape),"-D PLOT NOT SUPPORTED !!! dimensions: ",what_I_plot.shape
    393400                 errormess("Are you sure you did not forget to prescribe a dimension ?")
    394            else:
     401       else:
    395402               errormess("There is an error in reducing field !")
    396403
  • trunk/UTIL/PYTHON/videosink.py

    r445 r448  
    1414                    '-nosound',
    1515                    '-ovc', 'x264',
     16                    '-msglevel', 'all=-1'
    1617#                    '-ovc', 'lavc',
    1718                    )
Note: See TracChangeset for help on using the changeset viewer.