Changeset 359 for trunk/UTIL


Ignore:
Timestamp:
Nov 8, 2011, 2:38:15 PM (13 years ago)
Author:
acolaitis
Message:

PYTHON.
Added resolution and output filename choices.

--res X to choose X instead of 200 for png outputs
-O or --output to choose filename of the output

Added possibility to do operations between 2 files.
The plot consists of:

  • a main plot, given by the regular options corresponding to the choice of -var and -f.
  • a reference plot, given by the option --fref, in a similar way as the first plot (same var, same min/max, etc...)
  • a plot on which the operation between file 1 and ref file is done.

--operation to choose the operation. For now, there is + and -. It is really simple to add more.
--mope --Mope to choose min and maximum of the colorbar for the operation. Without specifying it, it is computed.
--titleref to change the title of the reference plot. To use in combination with --title for the title of the first plot.

Future work:

  • Add possibility to compare multiple files to the same reference file.
  • Add mesoscale support. (adding the options in meso.py is straightforward but some modif might have to be done in planetoplot.)


Location:
trunk/UTIL/PYTHON
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/UTIL/PYTHON/gcm.py

    r357 r359  
    4747    #parser.add_option('-V', action='store', dest='comb',        type="float",   default=None,  help='a defined combination of variables')
    4848
     49    parser.add_option('-O','--output',  action='store',dest='output',    type="string",  default=None,  help='Output file name')
     50    parser.add_option('--res',          action='store',dest='res',       type="float",   default=200.,  help='Resolution for png outputs. --save png must be specified. (def=200.)')
     51    parser.add_option('--operation',    action='store',dest='operation', type="string",  default=None,  help='Basic operation to perform between input file and a specified reference file. Available: addition "+", soustraction "-". Operation is done as: file1 +or- fileref. The reference file must be specified using -fref option')
     52    parser.add_option('--fref',         action='store',dest='fref',     type="string",  default=None,  help='Reference namefile for the --operation option.')
     53    parser.add_option('--mope',         action='store',dest='vminope', type="float",   default=0.,  help='bounding minimum value for inter-file operation')
     54    parser.add_option('--Mope',         action='store',dest='vmaxope', type="float",   default=0.,  help='bounding maximum value for inter-file operation')
     55    parser.add_option('--titleref',     action='store',dest='titref',  type="string",  default="fill",  help='title for the reference file. Default: title of figure (1)')
     56
    4957    ############# T.N. changes
    5058    #parser.add_option('-o','--operation',action='store',dest='operation',type="string",   default=None ,help='matrix of operations between files (for now see code, sorry)')
     
    5361    parser.add_option('--vert',         action='append',dest='svert',type="string",   default=None, help='slices along vertical axis. One value, or two values separated by comas for averaging') # quelles coordonnees ?
    5462    parser.add_option('--time',         action='append',dest='stime',type="string",   default=None, help='slices along time. One value, or two values separated by comas for averaging') # quelles coordonnees ?
    55 
    5663
    5764    (opt,args) = parser.parse_args()
     
    7683    #    vmintab = adjust_length (opt.vmin, zelen) 
    7784    #    vmaxtab = adjust_length (opt.vmax, zelen)
     85
     86
     87    ################################
     88    ### General check
     89 
     90    if opt.fref is not None:
     91       if opt.operation is None:
     92          print "you must specify an operation when using a reference file"
     93          exit()
     94    if opt.operation is not None:
     95       if opt.fref is None:
     96          print "you must specifiy a reference file when using inter-file operations"
     97          exit()
     98
     99    ################################
     100
    78101
    79102    print "namefile, length", opt.namefile, len(opt.namefile)
     
    158181                anomaly=opt.anomaly,var2=opt.var2,ndiv=opt.ndiv,first=opt.first,\
    159182                mult=opt.mult,zetitle=opt.zetitle,\
    160                 slon=zeslon,slat=zeslat,svert=zesvert,stime=zestime)
     183                slon=zeslon,slat=zeslat,svert=zesvert,stime=zestime,\
     184                outputname=opt.output,resolution=opt.res,\
     185                ope=opt.operation,fileref=opt.fref,minop=opt.vminope,maxop=opt.vmaxope,titleref=opt.titref)
    161186        print 'Done: '+name
    162187        system("rm -f to_be_erased")
  • trunk/UTIL/PYTHON/meso.py

    r351 r359  
    5454    parser.add_option('--time',         action='append',dest='stime',type="string",   default=None, help='slices along time. One value, or two values separated by comas for averaging') # quelles coordonnees ?
    5555
     56    ############# A.C. changes
     57    parser.add_option('-O','--output',  action='store' ,dest='output',type="string",   default=None, help='Output file name')
     58    parser.add_option('--res',  action='store' ,dest='res',type="float",   default=200., help='Resolution for png outputs. --save png must be specified. (def=200.)')
    5659
    5760    (opt,args) = parser.parse_args()
     
    152155                anomaly=opt.anomaly,var2=opt.var2,ndiv=opt.ndiv,first=opt.first,\
    153156                mult=opt.mult,zetitle=opt.zetitle,\
    154                 slon=zeslon,slat=zeslat,svert=zesvert,stime=zestime)
     157                slon=zeslon,slat=zeslat,svert=zesvert,stime=zestime,\
     158                outputname=opt.output,resolution=opt.res)
    155159        print 'Done: '+name
    156160        system("rm -f to_be_erased")
  • trunk/UTIL/PYTHON/myplot.py

    r350 r359  
    138138        rcParams['font.size'] = int( rcParams['font.size'] * 3. / 4. )
    139139    elif numplot == 3:
    140         subv = 3
    141         subh = 1
     140        subv = 2
     141        subh = 2
    142142        fig.subplots_adjust(wspace = 0.5)
    143143        rcParams['font.size'] = int( rcParams['font.size'] * 1. / 2. )
  • trunk/UTIL/PYTHON/planetoplot.py

    r357 r359  
    55### A. Spiga     -- LMD -- 06~09/2011 -- General building and mapping capabilities
    66### 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
    78
    89def planetoplot (namefiles,\
     
    3637           slat=None,\
    3738           svert=None,\
    38            stime=None):
     39           stime=None,\
     40           outputname=None,\
     41           resolution=200,\
     42           ope=None,\
     43           fileref=None,\
     44           minop=0.,\
     45           maxop=0.,\
     46           titleref="fill"):
     47
    3948
    4049    ####################################################################################################################
     
    5362    import numpy as np
    5463    from numpy.core.defchararray import find
    55        
     64
    5665    ################################
    5766    ### Which plot needs to be done?
     
    6271    if numplot == None:  numplot = zelen*nslices
    6372    print "len(namefiles), nslices, numplot: ", zelen, nslices, numplot
    64     all_var   = [[]]*zelen
    65     all_var2  = [[]]*zelen
    66     all_title = [[]]*zelen
     73    if fileref is not None:
     74        all_var  = [[]]*(zelen+2)
     75    else:
     76        all_var   = [[]]*zelen
     77        all_var2  = [[]]*zelen
     78        all_title = [[]]*zelen
    6779   
    6880    #########################
     
    149161      #### End of for namefile in namefiles
    150162
     163    ##################################
     164    ### Load reference file if there is one
     165    if fileref is not None:
     166       ncref  = Dataset(fileref)
     167       ### ... VAR
     168       if var not in nc.variables: var = False
     169       if var:
     170             all_var[k] = getfield(ncref,var)
     171             if ope is "-":
     172                all_var[k+1]= all_var[k-1] - all_var[k]
     173             elif ope is "+":
     174                all_var[k+1]= all_var[k-1] + all_var[k]
     175             elif ope is not None:
     176                print "non-implemented operation. Available op. are + and -."
     177             numplot = numplot+2
    151178
    152179    ##################################
     
    210237           indexvert = getsindex(svert,((nplot-1)//(nlon*nlat))%nvert,vert) 
    211238           indextime = getsindex(stime,((nplot-1)//(nlon*nlat*nvert))%ntime,time) 
    212        index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%len(namefiles)
     239
     240       if fileref is not None:
     241           index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%(len(namefiles)+2)
     242       else:
     243           index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%len(namefiles)
     244
    213245       print nlon, nlat, nvert, ntime 
    214246       print slon, slat, svert, stime       
     
    244276           what_I_plot, error = reducefield(all_var[index_f], d4=indextime, d1=indexlon, d2=indexlat , d3=indexvert )
    245277           what_I_plot = what_I_plot*mult
    246            
     278
    247279           if not error:
    248280               fvar = var
     
    259291                   what_I_plot, x, y = define_axis(lon,lat,vert,time,indexlon,indexlat,indexvert,\
    260292                         indextime,what_I_plot, len(all_var[index_f].shape),vertmode)
    261                zevmin, zevmax = calculate_bounds(what_I_plot,vmin=vmin,vmax=vmax)
     293               if (fileref is not None) and (index_f is numplot-1):
     294                  zevmin, zevmax = calculate_bounds(what_I_plot,vmin=minop,vmax=maxop)
     295               else:
     296                  zevmin, zevmax = calculate_bounds(what_I_plot,vmin=vmin,vmax=vmax)
    262297               if colorb in ["def","nobar"]:   palette = get_cmap(name=defcolorb(fvar.upper()))
    263298               else:                           palette = get_cmap(name=colorb)
     
    274309                     pcolor( x, y, what_I_plot, cmap = palette, vmin=zevmin, vmax=zevmax )
    275310                 if colorb != 'nobar' and var != 'HGT' :       
    276                          colorbar(fraction=0.05,pad=0.03,format=fmtvar(fvar.upper()),\
     311                     if (fileref is not None) and (index_f is numplot-1):
     312                        colorbar(fraction=0.05,pad=0.03,format="%.2f",\
    277313                                           ticks=np.linspace(zevmin,zevmax,min([ndiv+1,20])),\
    278314                                           extend='neither',spacing='proportional')
     315                     else:
     316                        colorbar(fraction=0.05,pad=0.03,format=fmtvar(fvar.upper()),\
     317                                           ticks=np.linspace(zevmin,zevmax,min([ndiv+1,20])),\
     318                                           extend='neither',spacing='proportional')
     319
    279320                                           # both min max neither
    280321               ##### 1D field
     
    316357            else:        plottitle = plottitle + "_LT"+str(ltst)
    317358       else:
    318             plottitle = basename+' '+namefiles[index_f]
     359            if fileref is not None:
     360                if index_f is numplot-1:
     361                   plottitle = basename+' '+"fig(1) "+ope+" fig(2)"
     362                elif index_f is numplot-2:
     363                   plottitle = basename+' '+fileref
     364                else:
     365                   plottitle = basename+' '+namefiles[index_f]
     366            else:
     367                plottitle = basename+' '+namefiles[index_f]
    319368       if mult != 1:           plottitle = str(mult) + "*" + plottitle
    320        if zetitle != "fill":   plottitle = zetitle
     369       if zetitle != "fill":   
     370          plottitle = zetitle
     371          if titleref is "fill":
     372             titleref=zetitle
     373          if fileref is not None:
     374             if index_f is numplot-2:
     375                plottitle = titleref
     376             if index_f is numplot-1:
     377                plottitle = "fig(1) "+ope+" fig(2)"
    321378#       if indexlon is not None:
    322379#         plottitle = plottitle + " lon: " + str(min(lon[indexlon])) +" "+ str(max(lon[indexlon]))
     
    342399    ##########################################################################
    343400    ### Save the figure in a file in the data folder or an user-defined folder
    344     if typefile in ['meso','mesoapi']:   prefix = getprefix(nc)
    345     elif typefile in ['gcm']:            prefix = 'LMD_GCM_'
    346     else:                                prefix = ''
     401    if outputname is None:
     402       if typefile in ['meso','mesoapi']:   prefix = getprefix(nc)
     403       elif typefile in ['gcm']:            prefix = 'LMD_GCM_'
     404       else:                                prefix = ''
    347405    ###
    348     zeplot = prefix + basename
    349     if addchar:         zeplot = zeplot + addchar
    350     if numplot <= 0:    zeplot = zeplot + "_LT"+str(abs(numplot))
     406       zeplot = prefix + basename
     407       if addchar:         zeplot = zeplot + addchar
     408       if numplot <= 0:    zeplot = zeplot + "_LT"+str(abs(numplot))
    351409    ###
    352     if not target:      zeplot = namefile[0:find(namefile,'wrfout')] + zeplot
    353     else:               zeplot = target + "/" + zeplot 
     410       if not target:      zeplot = namefile[0:find(namefile,'wrfout')] + zeplot
     411       else:               zeplot = target + "/" + zeplot 
    354412    ###
     413    else:
     414       zeplot=outputname
     415
    355416    if found_lct:     
    356417        pad_inches_value = 0.35
     
    358419        if save == 'png':
    359420            if display: makeplotres(zeplot,res=100.,pad_inches_value=pad_inches_value) #,erase=True)  ## a miniature
    360             makeplotres(zeplot,res=200.,pad_inches_value=pad_inches_value,disp=False)
     421            makeplotres(zeplot,res=resolution,pad_inches_value=pad_inches_value,disp=False)
    361422        elif save in ['eps','svg','pdf']:
    362423            makeplotres(zeplot,         pad_inches_value=pad_inches_value,disp=False,ext=save)
Note: See TracChangeset for help on using the changeset viewer.