source: trunk/UTIL/PYTHON/pp.py @ 763

Last change on this file since 763 was 763, checked in by acolaitis, 12 years ago

###################################################
# PYTHON / PLANETPLOT #
###################################################

# ------------------- XY plots ------------------ #

# Added a new category of plot to unidim, contour, etc... called "xy"
# - xy plots are plots that do not use time,vert,lat or lon as axis
# - variables to be plotted are stored in plot_x and plot_y, which is done in
# select_getfield. (there is no "what_I_plot" var for "wy" plots)
# - plot_x and plot_y are also subject to reduce field. A None value indicates
# the plot is not 'xy'
# - "xy" plots are used for a specific subset of plots : histograms, fourier
# transforms, hodographs
# - added option --analysis to perform certain kinds of analysis on the data
# (corresponding to xy plots).
# - One selects the fields he wants to plot (e.g. -v UV --lon 0 --lat 20) and
# chooses a kind of analysis :

--analysis fft # in the particular case given above, this mode corresponds

# to the mean of the ampitude spectrum of the vertical spatial
# fast fourier transform taken at each time index
# note that for now, fft are always done along the vertical
# axis. This could be made more flexible.
# not that the minimum wavelength you can plot depends on the
# vertical step of your simulation. THIS STEP HAS TO BE
# CONSTANT, hence you MUST use API or ZRECAST with constant
# spacing.

--analysis histo # histogram on the flattened data. If the user asks for --lon 0,20,

# the average is done before computing the histogram (contrary to the fft).
# However, if given a 2D array (with only --lon and --lat on a
# 4D field for example), the data is flattened before computation
# so that the result is still a 1D histogram.

--analysis histodensity # histogram with a kernel density estimate to a

# gaussian distribution, also giving the mean, variance,
# skewness and kurtosis. Other distributions are available in
# the scipy.stats package and could be implemented.

# - added variables "-v hodograph" and "-v hodograph_2". First one is a
# regular hodograph with "u" and "v" as axis, with labels of the local times
# (use --axtime lt). This is a "xy" plot (you must specify a vertical level
# as well, usually --vert 0 with an interpolation at 5m (-i 4 -l 0.005)).
# Second one is the variation with local time (for exemple) of the wind
# rotation (arctan(v/u)). This is not a "xy" plot but "unidim".
# For --ope plots in "xy" cases, only the operation plot is displayed.

# ------------------- Operations ------------------ #

# - For operations --ope -, the histogram (fourth plot) has been removed. To get it
# back, call --ope -_histo.
# - _only has been added to "+","-","-%" operations (eg "-_only")
# - For operations "-","+","-%","-_only","+_only","-%_only","-_histo", it is now
# possible to call multiple files (sill one variable) and compare each of them
# with the unique given reference file. Ex: -f file1,file2 --fref file3 --ope - will give 6 plots:

file1 file3 file1-file3
file2 file3 file2-file3

# In the case of "xy" plots, the multiple operation plots are regrouped on a
# single plot (using multiple lines). the title of this plot is not 'fig(2)-fig(1)'
# (default) but the argument of the --title command. Labels have to be given
# as following : --labels "dummy","dummy","file1-file3 label","dummy","dummy","file2-file3 label" (dummy can be anything. this is to be improved)
# To be able to run on multiple files and easily introduce the correct number and order of plots, these operations have been moved outside of the main loop
# on namefiles and variables. Operation of the type "add_var" or "cat" have
# been left inside the loop and are unchanged.

# ------------------- Localtime ------------------ #

# Changed the way localtime is computed. Reasons:
# - it was assuming one timestep per hour in computations mixing indices and
# actual times
# - to determine the starting date, it was using the name of the file (for Meso), instead of using the netcdf
# attribute (START_DATE)
# - it was using the computed mean longitude of the domain, which is not correct for
# hemispheric domain. => better to use the netcdf attribute CEN_LON
# - it was using this mean longitude even for profile plots at given
# longitude => better to get the local time at this given lon, especially
# important for large domains
# => new localtime() is in myplot (old one is commented). Interv is obsolete (but not removed yet). Case "Geo" has not been looked at.
# new localtime in myplot correctly account for starting date of the file in
# all cases
# accounts for local longitude of the plot
# accounts for files that do not have per hour outputs, but per timestep.
# specific cases can be added in myplot in localtime()

# ------------------ Misc ------------------ #

# - added option --xlog to get x logarithmic axis (--ylog already existed)
# - added the possibility to use 2 files of different gridding (-f
# file1,file2) although of the same type (meso for exemple). For that purpose,
# lon, lat, alt and vert arrays are now indexed with 'index_f', as for
# all_var. => all_lon, all_lat, all_lat, all_vert
# - added function teta_to_tk in myplot so that a call to pp.py on standard
# LMD mesoscale file with "-v tk" can be done without the need to call API.
# The temperature is computed from T and PTOT, knowing P0, T0 and R_CP.
# - bug correction in determineplot() that was causing wrong plot number and
# slices number when not calling averaged lon, lat, vert or times. (which is
# often !)
# - added new options to redope: --redope edge_x1, edge_x2, edge_y1, edge_y2
# which plots the boundaries of the domain. This is different compared to
# asking for a fixed longitude, because the domain boundary might not be at constant
# longitude (hemispheric domain for exemple). x1 is the western boundary, x2
# the eastern, y1 the southern and y2 the northern. x1 and x2 reduce the
# dimension along --lon, y1 and y2 reduce the dimension along --lat.
# - added control in windamplitude() to determine whether winds are staggered or
# unstaggered. This is usefull when dealing with non LMD_MMM files.
# - corrected a bug in reduce_field where the mean was computed on the wrong
# axis !!! (pretty serious bug)

# Exemple of plots you can do with these new options can be found in
$YOUR_SVN/trunk/UTIL/PYTHON/Intercomparison/Plots_MasterScript/bam.sh

# ------------------ API ------------------ #

# - changed maximum of levels from 299 to 1000 in API (interpolation on 1000 levels is
# usefull to get larger bandwidth in fourier transform)
# the following concerns users of MRAMS files.
# - API has not been modified for MRAMS files. Instead, a python script
# (ic.py) is run on MRAMS .ctl and .dat files, which automatically format those files
# to be API and pp.py compatible.
# - ic.py is in $YOUR_SVN/trunk/UTIL/PYTHON/Intercomparison/File_conversion

###################################################
# INTERCOMPARISON TOOLS: #
###################################################

#ic.py in
$YOUR_SVN/trunk/UTIL/PYTHON/Intercomparison/File_conversion
#CDO installer with import_binary in
$YOUR_SVN/trunk/UTIL/PYTHON/Intercomparison/CDO
#Plotting scripts in
$YOUR_SVN/trunk/UTIL/PYTHON/Intercomparison/Plots_MasterScript
#1D sensibility tool in
$YOUR_SVN/trunk/UTIL/PYTHON/Intercomparison/1D_sensibility_tool

# See README in each of these folders for details.

  • Property svn:executable set to *
File size: 9.7 KB
RevLine 
[349]1#!/usr/bin/env python
2
[392]3### A. Spiga + T. Navarro + A. Colaitis
[349]4
5###########################################################################################
6###########################################################################################
7### What is below relate to running the file as a command line executable (very convenient)
8if __name__ == "__main__":
9    import sys
10    from optparse import OptionParser    ### to be replaced by argparse
11    from api_wrapper import api_onelevel
[464]12    from gcm_transformations import call_zrecast
[349]13    from netCDF4 import Dataset
[392]14    from myplot import getlschar, separatenames, readslices, adjust_length, whatkindfile, errormess
[349]15    from os import system
16    from planetoplot import planetoplot
[377]17    from myscript import getparseroptions
[478]18    import glob
[349]19    import numpy as np
20
[478]21
[349]22    #############################
23    ### Get options and variables
[392]24    parser = OptionParser() ; getparseroptions(parser) ; (opt,args) = parser.parse_args()
[721]25    if opt.file is None:                                errormess("I want to eat one file at least ! Use pp.py -f name_of_my_file. Or type pp.py -h")
[392]26    if opt.var is None and opt.anomaly is True:         errormess("Cannot ask to compute anomaly if no variable is set")
27    if opt.fref is not None and opt.operat is None:     errormess("you must specify an operation when using a reference file")
28    if opt.operat in ["+","-"] and opt.fref is None:    errormess("you must specifiy a reference file when using inter-file operations")
29    if opt.fref is not None and opt.operat is not None and opt.itp is not None: interpref=True
30    else:   interpref=False
[451]31    if opt.rate is not None:      opt.save = "avi"
32    elif opt.save == "avi":       opt.rate = 8   ## this is a default value for -S avi
33    if opt.save == "html":        opt.rate = -1  ## this is convenient because everything is done in planetoplot with mrate
[349]34
[392]35    #############################
36    ### Get infos about slices
37    zeslat  = readslices(opt.slat) ; zeslon  = readslices(opt.slon) ; zesvert = readslices(opt.svert) ; zestime = readslices(opt.stime)
38    reffile = opt.fref
39    zexaxis = [opt.xmin,opt.xmax] ; zeyaxis=[opt.ymin,opt.ymax]
[349]40
[392]41    #############################
[478]42    ### Catch multiple files
[479]43    if "*" in opt.file[0] or "?" in opt.file[0]: 
44        yeah = glob.glob(opt.file[0]) ; yeah.sort()
45        opt.file[0] = yeah[0] 
[489]46        for file in yeah[1:]: opt.file[0] = opt.file[0] + "," + file
[478]47
48    #############################
[392]49    ### 1. LOOP ON FILE LISTS TO BE PUT IN DIFFERENT FIGURES
50    for i in range(len(opt.file)):
[349]51
[392]52      zefiles = separatenames(opt.file[i])
[380]53
[426]54      typefile = whatkindfile(Dataset(zefiles[0])) ; stralt = None
[562]55      if typefile in ["meso"]:         
[392]56          [lschar,zehour,zehourin] = getlschar ( zefiles[0] )
[760]57          if opt.var is None:  opt.var = ["HGT"] #; opt.clb = "nobar" # otherwise breaks the automatic one-var file capability
[429]58      elif typefile in ["geo"]:
[763]59          lschar=""
[426]60          if opt.var is None:  opt.var = ["HGT_M"] ; opt.clb = "nobar"
[763]61      else:                                     
62          lschar="" 
[721]63          if opt.var is None: 
64             opt.var = ["phisinit"] ; opt.clb = "nobar"
65             ### temporaire... en attendant mieux.
66             if opt.back == "titan": opt.var = ["phis"] ; opt.clb = "nobar"
[380]67
[392]68      if opt.vmin is not None : zevmin  = opt.vmin[min(i,len(opt.vmin)-1)]
69      else:                     zevmin = None
70      if opt.vmax is not None : zevmax  = opt.vmax[min(i,len(opt.vmax)-1)]
71      else:                     zevmax = None
72      #print "vmin, zevmin", opt.vmin, zevmin ; print "vmax, zevmax", opt.vmax, zevmax
[380]73
[392]74      #############################
75      ### 2. LOOP ON VAR LISTS TO BE PUT IN DIFFERENT FIGURES
76      for j in range(len(opt.var)):
[380]77
[392]78        zevars = separatenames(opt.var[j])
[380]79
[377]80        inputnvert = separatenames(opt.lvl)
[351]81        if np.array(inputnvert).size == 1:
82            zelevel = float(inputnvert[0])
83            ze_interp_levels = [-9999.]
[610]84        elif np.array(inputnvert).size > 2:
[489]85            zelevel = -99.
[610]86            start = float(inputnvert[0])
87            stop = float(inputnvert[1])
88            if np.array(inputnvert).size == 2:  numsample = 20
89            else:                               numsample = float(inputnvert[2])
90            if stop > start:   
91               ## altitude coordinates
92               ze_interp_levels = np.linspace(start,stop,numsample)
93            else:
94               ## pressure coordinates
95               ze_interp_levels = np.logspace(np.log10(start),np.log10(stop),numsample)
[351]96
[392]97        #########################################################
[377]98        if opt.itp is not None:
[562]99         if opt.itp > 0:
[392]100          #####
101          ##### MESOSCALE : written by AS
102          #####
[562]103          if typefile in ["meso"]:
[377]104            if zelevel == 0. and opt.itp == 4:  zelevel = 0.010
[349]105            ### winds or no winds
106            if opt.winds            :  zefields = 'uvmet'
[612]107            elif opt.var[j] in ['deltat','DELTAT'] : zefields = 'tk,TSURF'
[763]108            elif opt.var[j] in ['uv','UV','hodograph','hodograph_2'] : zefields = 'U,V'
[349]109            else                    :  zefields = ''
110            ### var or no var
[392]111            if zefields == ''       :  zefields = opt.var[j] 
112            else                    :  zefields = zefields + "," + opt.var[j]
113            if opt.var2 is not None :  zefields = zefields + "," + opt.var2 
114            ### call fortran routines
115            for fff in range(len(zefiles)):
116                newname = api_onelevel (  path_to_input   = '', \
117                                               input_name      = zefiles[fff], \
118                                               fields          = zefields, \
119                                               interp_method   = opt.itp, \
120                                               interp_level    = ze_interp_levels, \
121                                               onelevel        = zelevel, \
122                                               nocall          = opt.nocall )
123                if fff == 0: zetab = newname
124                else:        zetab = np.append(zetab,newname)
[569]125            if interpref:
126                reffile = api_onelevel (  path_to_input   = '', \
127                                               input_name      = opt.fref, \
128                                               fields          = zefields, \
129                                               interp_method   = opt.itp, \
130                                               interp_level    = ze_interp_levels, \
131                                               onelevel        = zelevel, \
132                                               nocall          = opt.nocall )
[392]133            zefiles = zetab #; print zefiles
[349]134            zelevel = 0 ## so that zelevel could play again the role of nvert
[392]135          #####
136          ##### GCM : written by AC
137          #####
138          elif typefile == "gcm":
[475]139            inputvar = zevars
140            if opt.var2 is not None : inputvar = np.append(inputvar,opt.var2)
[392]141            interpolated_files=""
142            interpolated_files=call_zrecast(interp_mode=opt.itp,\
143                    input_name=zefiles,\
[475]144                    fields=inputvar,\
[489]145                    limites = ze_interp_levels,\
[464]146                    predefined=opt.intas)
[349]147
[392]148            zefiles=interpolated_files
149            if interpref:
150               interpolated_ref=""
151               interpolated_ref=call_zrecast(interp_mode=opt.itp,\
[380]152                    input_name=[opt.fref],\
[409]153                    fields=zevars,\
[464]154                    predefined=opt.intas)
[380]155
[392]156               reffile=interpolated_ref[0]
157          else:
[429]158            print "type not supported"
[392]159            exit()
[377]160
[392]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=zevars,\
[760]165                clb=separatenames(opt.clb),winds=opt.winds,\
[763]166                addchar=lschar,vmin=zevmin,vmax=zevmax,\
[349]167                tile=opt.tile,zoom=opt.zoom,display=opt.display,\
[424]168                hole=opt.hole,save=opt.save,\
169                anomaly=opt.anomaly,var2=opt.var2,ndiv=opt.ndiv,\
[578]170                mult=opt.mult,zetitle=separatenames(opt.zetitle),\
[359]171                slon=zeslon,slat=zeslat,svert=zesvert,stime=zestime,\
[377]172                outputname=opt.out,resolution=opt.res,\
[380]173                ope=opt.operat,fileref=reffile,minop=opt.vminope,maxop=opt.vmaxope,titleref=opt.titref,\
[763]174                invert_y=opt.inverty,xaxis=zexaxis,yaxis=zeyaxis,ylog=opt.logy,xlog=opt.logx,yintegral=opt.column,\
[453]175                blat=opt.blat,blon=opt.blon,tsat=opt.tsat,flagnolow=opt.nolow,\
[483]176                mrate=opt.rate,mquality=opt.quality,trans=opt.trans,zarea=opt.area,axtime=opt.axtime,\
[612]177                redope=opt.redope,seevar=opt.seevar,xlab=opt.xlab,ylab=opt.ylab,lbls=separatenames(opt.labels),\
[760]178                lstyle=separatenames(opt.linestyle),cross=readslices(opt.mark),facwind=opt.facwind,\
[763]179                trycol=opt.trycol,streamflag=opt.stream,analysis=opt.analysis)
[392]180        print 'DONE: '+name
[349]181        system("rm -f to_be_erased")
182 
183    #########################################################
184    ### Generate a .sh file with the used command saved in it
[392]185    command = "" 
[349]186    for arg in sys.argv: command = command + arg + ' '
[424]187    #if typefile not in ["meso","mesoapi"]: name = 'pycommand'
[451]188    if opt.save == "gui":    name = 'pycommand'
189    elif opt.save == "avi":  system("mv -f movie*.avi "+name+".avi")
190    elif opt.save == "html": system("cat $PYTHONPATH/header.html > anim.html ; cat zepics >> anim.html ; cat $PYTHONPATH/body.html >> anim.html ; rm -rf zepics "+name+" ; mkdir "+name+" ; mv anim.html image*png "+name) 
[349]191    f = open(name+'.sh', 'w')
192    f.write(command)
[392]193
[468]194    #print "********** OPTIONS: ", opt
[392]195    print "********************************************************** END"
Note: See TracBrowser for help on using the repository browser.