Changeset 896 for trunk/UTIL


Ignore:
Timestamp:
Mar 10, 2013, 10:44:32 PM (12 years ago)
Author:
aslmd
Message:

UTIL PYTHON. cosmetic changes. improved treatment of ticks in 1D plots.

Location:
trunk/UTIL/PYTHON
Files:
2 edited

Legend:

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

    r886 r896  
    9898    ## this allows to get much faster and use much less memory esp. with large datasets
    9999    print "   Opening variable",var," with", dimension, "dimensions ..."
     100    print indextime, indexlon, indexlat, indexvert
    100101    if dimension == 2:   
    101102        field = nc.variables[var][indextime,indexlon]
     
    16101611    idx=idxsub[0]+np.min(idxs) ; idy=idysub[0]+np.min(idys)
    16111612    return np.int(idx),np.int(idy)
     1613
     1614# Author : A.S.
     1615# This is temporary. An object formulation would solve this kind of inelegant approach.
     1616def alltransfer(zeto,zefrom,all_varname,all_time,all_vert,all_lat,all_lon,all_namefile,all_var2,all_colorb,all_var):
     1617    all_varname[zeto]   = all_varname[zefrom]
     1618    all_time[zeto]      = all_time[zefrom]
     1619    all_vert[zeto]      = all_vert[zefrom]
     1620    all_lat[zeto]       = all_lat[zefrom]
     1621    all_lon[zeto]       = all_lon[zefrom]
     1622    all_namefile[zeto]  = all_namefile[zefrom]
     1623    all_var2[zeto]      = all_var2[zefrom]
     1624    all_colorb[zeto]    = all_colorb[zefrom]
     1625    all_var[zeto]       = all_var[zefrom]
  • trunk/UTIL/PYTHON/planetoplot.py

    r893 r896  
    118118        print "********* SPEED MODE. EXPERIMENTAL. ********"
    119119        print "********************************************"
    120 ### we ensure namefiles and var are arrays
     120### we ensure namefiles and var are arrays and we get their size
    121121    if not isinstance(namefiles, np.ndarray): namefiles = [namefiles]
    122122    if not isinstance(var, np.ndarray):       var = [var]
     123    zelenfile = len(namefiles) ; zelenvar = len(var)
     124### we check colorbars options (including trycol i.e. the user wants to try a set of colorbars)
     125    if trycol: clb = ["Greys","Blues","YlOrRd","jet","spectral","hot","RdBu","RdYlBu","Paired"] ; zetitle = clb ; var = [var[0]]*9 ; zelenvar = len(var)
     126    if clb is None:            clb = ["def"]*zelenvar
     127    elif len(clb) < zelenvar:  clb = [clb[0]]*zelenvar ; print "WARNING: less color than vars! setting all to 1st value."
    123128### we initialize a few variables
    124129    initime=-1 ; sslon = None ; sslat = None
     
    127132    if slon is not None: sslon = np.zeros([1,2])
    128133    if slat is not None: sslat = np.zeros([1,2])
    129     if clb is None:            clb = ["def"]*len(var)
    130     elif len(clb) < len(var):  clb = [clb[0]]*len(var) ; print "WARNING: less color than vars! setting all to 1st value."
    131134    if redope is not None and winds: winds=False ; print "WARNING: no winds with redope. setting winds to False."
    132 ### we set option trycol i.e. the user wants to try a set of colorbars
    133     if trycol: clb = ["Greys","Blues","YlOrRd","jet","spectral","hot","RdBu","RdYlBu","Paired"] ; zetitle = clb ; var = [var[0]]*9
    134135### we avoid specific cases not yet implemented
    135     if mrate is not None and len(var) > 1: myplot.errormess("multivar not allowed in movies. should be fixed soon!")
     136    if mrate is not None and zelenvar > 1: myplot.errormess("multivar not allowed in movies. should be fixed soon!")
    136137### we prepare number of plot fields [zelen] and number of plot instances [numplot] according to user choices
    137138### --> we support multifile and multivar plots : files + vars separated by commas are on the same figure
    138139    nlon, nlat, nvert, ntime, mapmode, nslices = myplot.determineplot(slon, slat, svert, stime, redope)
    139     zelen = len(namefiles)*len(var)
     140    zelen = zelenfile*zelenvar
    140141    if (nslices > 1 and monster): myplot.errormess("multislice + monster not supported yet. to be done soon")
    141142### we have a special mode obtained by -p noproj in which lat/lon plots are just flat 2D plots
     
    143144### we correct number of plot fields for possible operation (substract, etc...)
    144145    if ope is not None:
    145         if fileref is not None:       zelen = 3*len(var)*len(namefiles)
     146        if fileref is not None:       zelen = 3*zelenvar*zelenfile
    146147        elif "var" in ope:            zelen = zelen + 1
    147148    numplot = zelen*nslices
    148     print "********** FILES, SLICES, VARS, TOTAL PLOTS: ", len(namefiles), nslices, len(var), numplot
     149    print "********** FILES, SLICES, VARS, TOTAL PLOTS: ", zelenfile, nslices, zelenvar, numplot
    149150    print "********** MAPMODE: ", mapmode
    150151### we define the arrays for plot fields -- which will be placed within multiplot loops
     
    160161#############################
    161162   
    162     for nnn in range(len(namefiles)):
    163      for vvv in range(len(var)):
     163    for nnn in range(zelenfile):
     164     for vvv in range(zelenvar):
    164165
    165166    ### we load each NETCDF objects in namefiles
     
    362363        ## 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
    363364        if ope is not None:
    364             if fileref is not None:      index_f = (k//(nlon*nlat*nvert*ntime))%(3*len(namefiles))  ## OK only 1 var,  see test in the beginning
    365             elif "var" in ope:           index_f = (k//(nlon*nlat*nvert*ntime))%(len(var)+1)        ## OK only 1 file, see test in the beginning
     365            if fileref is not None:      index_f = (k//(nlon*nlat*nvert*ntime))%(3*zelenfile)  ## OK only 1 var,  see test in the beginning
     366            elif "var" in ope:           index_f = (k//(nlon*nlat*nvert*ntime))%(zelenvar+1)        ## OK only 1 file, see test in the beginning
    366367            elif "cat" in ope:           index_f = 0
    367368        elif not (k == 0):
    368            #if len(namefiles) > 1 and len(var) == 1 and which == "unidim": pass ## TROUVER UN MOYEN POUR unidim
    369            if len(namefiles) > 1 and len(var) == 1: pass
    370            else: yeah = len(namefiles)*len(var) ; index_f = (k//(nlon*nlat*nvert*ntime))%yeah
    371         else: yeah = len(namefiles)*len(var) ; index_f = (k//(nlon*nlat*nvert*ntime))%yeah
     369           #if zelenfile > 1 and zelenvar == 1 and which == "unidim": pass ## TROUVER UN MOYEN POUR unidim
     370           if zelenfile > 1 and zelenvar == 1: pass
     371           else: yeah = zelenfile*zelenvar ; index_f = (k//(nlon*nlat*nvert*ntime))%yeah
     372        else: yeah = zelenfile*zelenvar ; index_f = (k//(nlon*nlat*nvert*ntime))%yeah
    372373
    373374        ilon  = myplot.getsindex(sslon,k%nlon,lon)
     
    424425    if ope is not None and "var" in ope:
    425426         print "********** OPERATION: ",ope
    426          if len(namefiles) > 1: myplot.errormess("for this operation... please set only one file !")
    427          if len(var) > 2:       myplot.errormess("not sure this works for more than 2 vars... please check.")
     427         if zelenfile > 1: myplot.errormess("for this operation... please set only one file !")
     428         if zelenvar > 2:       myplot.errormess("not sure this works for more than 2 vars... please check.")
    428429         if   "div_var" in ope: all_var[k] = all_var[k-2] / all_var[k-1] ; insert = '_div_'
    429430         elif "mul_var" in ope: all_var[k] = all_var[k-2] * all_var[k-1] ; insert = '_mul_'
     
    442443             all_varname[0] = all_varname[k-2] + insert + all_varname[k-1]
    443444
    444 
    445     ##################################
    446     ### Operation on files (II) without _var
    447     # we re-iterate on the plots to set operation subplots to make it compatible with multifile (using the same ref file)
    448     # (k+1)%3==0 is the index of operation plots
    449     # (k+2)%3==0 is the index of reference plots
    450     # (k+3)%3==0 is the index of first plots
    451     opefirstpass=True
    452445    if ope is not None and "var" not in ope:
    453446       print "********** OPERATION: ",ope
     447       if zelenvar > 1: myplot.errormess("for this operation... please set only one var !")
     448       if fileref is None and ope not in ['cat']: myplot.errormess("fileref is missing!")
     449       ################################## this is terribly complex for the main program file. got to do something about this
     450       ### Operation on files (II) without _var
     451       # we re-iterate on the plots to set operation subplots to make it compatible with multifile (using the same ref file)
     452       # (k+1)%3==0 is the index of operation plots
     453       # (k+2)%3==0 is the index of reference plots
     454       # (k+3)%3==0 is the index of first plots
     455       opefirstpass=True
    454456       for k in np.arange(zelen):
    455                if len(var) > 1: myplot.errormess("for this operation... please set only one var !")
    456457               if ope in ["-","+","-%","-_only","+_only","-%_only","-_histo"]:
    457                   if fileref is None: myplot.errormess("fileref is missing!")
    458                   else:ncref = Dataset(fileref)
     458                  ncref = netCDF4.Dataset(fileref)
    459459
    460460                  if opefirstpass: ## first plots
    461                      for ll in np.arange(len(namefiles)):
     461                     for ll in np.arange(zelenfile):
    462462                        print "SETTING FIRST PLOT"
    463                         all_varname[3*ll] = all_varname[ll] ; all_time[3*ll] = all_time[ll] ; all_vert[3*ll] = all_vert[ll] ; all_lat[3*ll] = all_lat[ll] ; all_lon[3*ll] = all_lon[ll] ; all_namefile[3*ll] = all_namefile[ll] ; all_var2[3*ll] = all_var2[ll] ; all_colorb[3*ll] = all_colorb[ll] ; all_var[3*ll] = all_var[ll]
     463                        myplot.alltransfer(3*ll,ll,all_varname,all_time,all_vert,all_lat,all_lon,all_namefile,all_var2,all_colorb,all_var)
    464464                        if plot_y[ll] is not None: plot_y[3*ll] = plot_y[ll] ; plot_x[3*ll] = plot_x[ll]
    465465                        else: plot_y[3*ll] = None ; plot_x[3*ll] = None
     
    468468                  if (k+2)%3==0: ## reference plots
    469469                        print "SETTING REFERENCE PLOT"
    470                         all_varname[k] = all_varname[k-1] ; all_time[k] = all_time[k-1] ; all_vert[k] = all_vert[k-1] ; all_lat[k] = all_lat[k-1] ; all_lon[k] = all_lon[k-1] ; all_namefile[k] = all_namefile[k-1] ; all_var2[k] = all_var2[k-1] ; all_colorb[k] = all_colorb[k-1]
     470                        myplot.alltransfer(k,k-1,all_varname,all_time,all_vert,all_lat,all_lon,all_namefile,all_var2,all_colorb,all_var)
     471                        ### all_var is actually overwritten here
    471472                        all_var[k], plot_x[k], plot_y[k] = myplot.select_getfield(zvarname=all_varname[k-1],znc=ncref,ztypefile=typefile,mode='getvar',ztsat=tsat,ylon=all_lon[k],ylat=all_lat[k],yalt=all_vert[k],ytime=all_time[k],analysis=analysis)
    472473                        if winds: all_windu[k] = myplot.getfield(ncref,uchar) ; all_windv[k] = myplot.getfield(ncref,vchar)
     
    499500                  tabtime = all_time[0];tab = all_var[0];k = 1
    500501                  if var2: tab2 = all_var2[0]
    501                   while k != len(namefiles) and len(all_time[k]) != 0:
     502                  while k != zelenfile and len(all_time[k]) != 0:
    502503                      if var2: tab2 = np.append(tab2,all_var2[k],axis=0)
    503504                      tabtime = np.append(tabtime,all_time[k]) ; tab = np.append(tab,all_var[k],axis=0) ; k += 1
     
    514515    fig = mpl.pyplot.figure()
    515516    subv,subh = myplot.definesubplot( numplot, fig, ipreferline = (mapmode == 1) )
    516     if ope in ['-','-%','-_histo'] and len(namefiles) ==1 : subv,subh = 2,2
    517     elif ope in ['-','-%'] and len(namefiles)>1 : subv, subh = len(namefiles), 3
     517    if ope in ['-','-%','-_histo'] and zelenfile ==1 : subv,subh = 2,2
     518    elif ope in ['-','-%'] and zelenfile>1 : subv, subh = zelenfile, 3
    518519 
    519520    #################################
     
    532533       ## 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
    533534       if ope is not None:
    534            if fileref is not None:      index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%(3*len(namefiles))  ## OK only 1 var,  see test in the beginning
    535            elif "var" in ope:           index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%(len(var)+1)        ## OK only 1 file, see test in the beginning
     535           if fileref is not None:      index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%(3*zelenfile)  ## OK only 1 var,  see test in the beginning
     536           elif "var" in ope:           index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%(zelenvar+1)        ## OK only 1 file, see test in the beginning
    536537           elif "cat" in ope:           index_f = 0
    537538       elif not firstpass:
    538           if len(namefiles) > 1 and len(var) == 1 and which == "unidim": pass
    539           else: yeah = len(namefiles)*len(var) ; index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%yeah
    540        else: yeah = len(namefiles)*len(var) ; index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%yeah
     539          if zelenfile > 1 and zelenvar == 1 and which == "unidim": pass
     540          else: yeah = zelenfile*zelenvar ; index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%yeah
     541       else: yeah = zelenfile*zelenvar ; index_f = ((nplot-1)//(nlon*nlat*nvert*ntime))%yeah
    541542       time = all_time[index_f] ; vert = all_vert[index_f] ; lat = all_lat[index_f] ; lon = all_lon[index_f]
    542543       indexlon  = myplot.getsindex(sslon,(nplot-1)%nlon,lon)
     
    669670                    which = "unidim"
    670671                    if what_I_plot.shape[-1] == 1:      print "VALUE VALUE VALUE VALUE ::: ", what_I_plot[0] ; save = 'donothing'
    671 ##                 if which == "unidim" and len(namefiles) > 1: numplot = 1 # this case is similar to several vars from one file
     672##                 if which == "unidim" and zelenfile > 1: numplot = 1 # this case is similar to several vars from one file
    672673                 ##### IMOV LOOP #### IMOV LOOP
    673674                 while imov <= iend:
     
    780781                             if teststream:
    781782                                print 'INFO: Using stream file',streamfile, 'for stream lines'
    782                                 ncstream = Dataset(streamfile)
     783                                ncstream = netCDF4.Dataset(streamfile)
    783784                                psi = myplot.getfield(ncstream,'psi')
    784785                                psi = psi[0,:,:,0] # time and longitude are dummy dimensions
     
    871872                           
    872873                    elif which == "contour":
    873                         mpl.pyplot.rcParams['contour.negative_linestyle'] = 'solid' # no dashed line for negative values
     874                        #mpl.pyplot.rcParams['contour.negative_linestyle'] = 'solid' # no dashed line for negative values
    874875                        zevminc, zevmaxc = myplot.calculate_bounds(what_I_plot_frame, vmin=min(what_I_plot_frame), vmax=max(what_I_plot_frame))
    875876                        zelevels = np.linspace(zevminc,zevmaxc,ticks/2) #20)
     
    880881                        elif var2 == 'wstar':    zelevels = np.arange(0,10,1.0)
    881882                        elif var2 == 'zmax_th':  zelevels = np.arange(0,10,2.0) ; what_I_plot_frame = what_I_plot_frame / 1000.
    882                         elif var2 in ['u']:      zelevels = np.arange(-400.,400.,10.)
     883                        elif var2 in ['u']:      zelevels = np.arange(-400.,400.,5.)
    883884                        ###
    884885                        if mapmode == 0:   
     
    901902                           if zxmin is not None: mpl.pyplot.xlim(xmin=zxmin)
    902903                           if zxmax is not None: mpl.pyplot.xlim(xmax=zxmax)
     904                           if zxmin is not None and zxmax is not None:
     905                               ax = mpl.pyplot.gca()
     906                               ax.get_xaxis().set_ticks(np.linspace(xaxis[0],xaxis[1],ticks/2+1))
    903907                           if zymin is not None: mpl.pyplot.ylim(ymin=zymin)
    904908                           if zymax is not None: mpl.pyplot.ylim(ymax=zymax)
     
    976980       nplot += 1
    977981
    978        if len(namefiles) > 1 and len(var) == 1 and which == "unidim": index_f=index_f+1
     982       if zelenfile > 1 and zelenvar == 1 and which == "unidim": index_f=index_f+1
    979983       firstpass=False
    980984
Note: See TracChangeset for help on using the changeset viewer.