Changeset 647


Ignore:
Timestamp:
May 3, 2012, 6:46:55 PM (13 years ago)
Author:
tnavarro
Message:

Corrected bug in reducefield for masked arrays with grid area. Possibility to see stream function for a lat/vert slice with --stream option. Output of both data and axis with save txt option in 1D. Small bug corrected: title is the file name for multiple plots with multiple plots. Cleanup in myplot.py

Location:
trunk/UTIL/PYTHON
Files:
5 edited

Legend:

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

    r525 r647  
    5858           else:
    5959              return np.array(field).sum(axis=axis)
     60             
     61def getmask (field):
     62        import numpy as np
     63        if field is None: return None
     64        if type(field).__name__=='MaskedArray':
     65              return np.ma.getmask(field)
     66        else:
     67              return np.isnan(field)
     68       
    6069
    6170def deg ():
     
    6675    myfile = open(filename, 'w')
    6776    for line in mydata:
    68         myfile.write(str(line) + '\n')
     77        zeline = str(line)
     78        zeline = zeline.replace('[','')
     79        zeline = zeline.replace(']','')
     80        myfile.write(zeline + '\n')
    6981    myfile.close()
    7082    return
  • trunk/UTIL/PYTHON/myplot.py

    r638 r647  
    5555## Author: AS, AC, JL
    5656def whatkindfile (nc):
     57    typefile = 'gcm' # default
    5758    if 'controle' in nc.variables:             typefile = 'gcm'
    5859    elif 'phisinit' in nc.variables:           typefile = 'gcm'
     
    6263    elif hasattr(nc,'institution'):
    6364      if "European Centre" in getattr(nc,'institution'):  typefile = 'ecmwf'
    64     else:                                      typefile = 'gcm' # for lslin-ed files from gcm
    6565    return typefile
    6666
     
    165165    ### ... note, anomaly is only computed over d1 and d2 for the moment
    166166    import numpy as np
    167     from mymath import max,mean,min,sum
     167    from mymath import max,mean,min,sum,getmask
    168168    csmooth = 12 ## a fair amount of grid points (too high results in high computation time)
    169169    if redope is not None:
     
    194194        elif max(d1) >= shape[1]: error = True
    195195        elif d1 is not None and d2 is not None:
    196           totalarea=sum(mesharea[d2,:],axis=0); totalarea=sum(totalarea[d1],axis=0)
    197           output = output*mesharea; output = sum(output[d2,:],axis=0); output = sum(output[d1],axis=0)/totalarea
     196          totalarea = np.ma.masked_where(getmask(output),mesharea)
     197          totalarea = mean(totalarea[d2,:],axis=0);totalarea = mean(totalarea[d1])
     198          output = output*mesharea; output = mean(output[d2,:],axis=0); output = mean(output[d1])/totalarea
    198199        elif d1 is not None:         output = mean(input[:,d1],axis=1)
    199         elif d2 is not None:         totalarea=sum(mesharea[d2,:],axis=0); output = output*mesharea; output = sum(output[d2,:],axis=0)/totalarea
     200        elif d2 is not None:
     201          totalarea = np.ma.masked_where(getmask(output),mesharea)
     202          totalarea = mean(totalarea[d2,:],axis=0)
     203          output = output*mesharea; output = mean(output[d2,:],axis=0)/totalarea
    200204    elif dimension == 3:
    201205        if mesharea is None: mesharea=np.ones(shape[[1,2]])
     
    203207        elif max(d2) >= shape[1]: error = True
    204208        elif max(d1) >= shape[2]: error = True
    205         elif d4 is not None and d2 is not None and d1 is not None: 
    206           totalarea=sum(mesharea[d2,:],axis=0); totalarea=sum(totalarea[d1],axis=0)
     209        elif d4 is not None and d2 is not None and d1 is not None:
    207210          output = mean(input[d4,:,:],axis=0)
    208           output = output*mesharea; output = sum(output[d2,:],axis=0); output = sum(output[d1],axis=0)/totalarea
     211          totalarea = np.ma.masked_where(getmask(output),mesharea)
     212          totalarea = mean(totalarea[d2,:],axis=0);totalarea = mean(totalarea[d1])
     213          output = output*mesharea; output = mean(output[d2,:],axis=0); output = mean(output[d1])/totalarea
    209214        elif d4 is not None and d2 is not None:
    210215          output = mean(input[d4,:,:],axis=0)
    211           totalarea=sum(mesharea[d2,:],axis=0); output = output*mesharea; output = sum(output[d2,:],axis=0)/totalarea
     216          totalarea = np.ma.masked_where(getmask(output),mesharea)
     217          totalarea = mean(totalarea[d2,:],axis=0)
     218          output = output*mesharea; output = mean(output[d2,:],axis=0)/totalarea
    212219        elif d4 is not None and d1 is not None:    output = mean(input[d4,:,:],axis=0); output=mean(output[:,d1],axis=1)
    213220        elif d2 is not None and d1 is not None:
    214           totalarea=sum(mesharea[d2,:],axis=0); totalarea=sum(totalarea[d1],axis=0)
    215           output = output*mesharea; output = sum(output[:,d2,:],axis=1); output = sum(output[:,d1],axis=1)/totalarea
     221          totalarea = np.tile(mesharea,(output.shape[0],1,1))
     222          totalarea = np.ma.masked_where(getmask(output),totalarea)
     223          totalarea = mean(totalarea[:,d2,:],axis=1);totalarea = mean(totalarea[:,d1],axis=1)
     224          output = output*mesharea; output = mean(output[:,d2,:],axis=1); output = mean(output[:,d1],axis=1)/totalarea
    216225        elif d1 is not None:   output = mean(input[:,:,d1],axis=2)
    217         elif d2 is not None:   totalarea=sum(mesharea[d2,:],axis=0); output = output*mesharea; output = sum(output[:,d2,:],axis=1)/totalarea
     226        elif d2 is not None:   
     227          totalarea = np.tile(mesharea,(output.shape[0],1,1))
     228          totalarea = np.ma.masked_where(getmask(output),totalarea)
     229          totalarea = mean(totalarea[:,d2,:],axis=1)
     230          output = output*mesharea; output = mean(output[:,d2,:],axis=1)/totalarea
    218231        elif d4 is not None:   output = mean(input[d4,:,:],axis=0)
    219232    elif dimension == 4:
    220         if mesharea is None: mesharea=np.ones(shape[[2,3]])
     233        if mesharea is None: mesharea=np.ones(shape[[2,3]]) # mesharea=np.random.random_sample(shape[[2,3]])*5. + 2. # pour tester
    221234        if   max(d4) >= shape[0]: error = True
    222235        elif max(d3) >= shape[1]: error = True
     
    227240             output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt,indice=d3)
    228241             if anomaly: output = 100. * ((output / smooth(output,csmooth)) - 1.)
    229              totalarea=sum(mesharea[d2,:],axis=0); totalarea=sum(totalarea[d1],axis=0)
    230              output = output*mesharea
    231              output = sum(output[d2,:],axis=0)
    232              output = sum(output[d1],axis=0)/totalarea
     242             totalarea = np.ma.masked_where(np.isnan(output),mesharea)
     243             totalarea = mean(totalarea[d2,:],axis=0); totalarea = mean(totalarea[d1])
     244             output = output*mesharea; output = mean(output[d2,:],axis=0); output = mean(output[d1])/totalarea
    233245        elif d4 is not None and d3 is not None and d2 is not None:
    234246             output = mean(input[d4,:,:,:],axis=0)
    235247             output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt,indice=d3)
    236248             if anomaly: output = 100. * ((output / smooth(output,csmooth)) - 1.)
    237              totalarea=sum(mesharea[d2,:],axis=0)
    238              output = output*mesharea; output = sum(output[d2,:],axis=0)/totalarea
     249             totalarea = np.ma.masked_where(np.isnan(output),mesharea)
     250             totalarea = mean(totalarea[d2,:],axis=0)
     251             output = output*mesharea; output = mean(output[d2,:],axis=0)/totalarea
    239252        elif d4 is not None and d3 is not None and d1 is not None:
    240253             output = mean(input[d4,:,:,:],axis=0)
     
    246259             if anomaly:
    247260                 for k in range(output.shape[0]):  output[k,:,:] = 100. * ((output[k,:,:] / smooth(output[k,:,:],csmooth)) - 1.)
    248              totalarea=sum(mesharea[d2,:],axis=0); totalarea=sum(totalarea[d1],axis=0)
    249              output = output*mesharea; output = sum(output[:,d2,:],axis=1); output = sum(output[:,d1],axis=1)/totalarea
     261             totalarea = np.tile(mesharea,(output.shape[0],1,1))
     262             totalarea = np.ma.masked_where(getmask(output),mesharea)
     263             totalarea = mean(totalarea[:,d2,:],axis=1); totalarea = mean(totalarea[:,d1],axis=1)
     264             output = output*mesharea; output = mean(output[:,d2,:],axis=1); output = mean(output[:,d1],axis=1)/totalarea
    250265             #noperturb = smooth1d(output,window_len=7)
    251266             #lenlen = len(output) ; output = output[1:lenlen-7] ; yeye = noperturb[4:lenlen-4]
    252267             #plot(output) ; plot(yeye) ; show() ; plot(output-yeye) ; show()
    253         elif d3 is not None and d2 is not None and d1 is not None: 
    254              output = reduce_zaxis(input[:,d3,:,:],ax=1,yint=yint,vert=alt,indice=d3) 
     268        elif d3 is not None and d2 is not None and d1 is not None:
     269             output = reduce_zaxis(input[:,d3,:,:],ax=1,yint=yint,vert=alt,indice=d3)
    255270             if anomaly:
    256271                 for k in range(output.shape[0]):  output[k,:,:] = 100. * ((output[k,:,:] / smooth(output[k,:,:],csmooth)) - 1.)
    257              totalarea=sum(mesharea[d2,:],axis=0); totalarea=sum(totalarea[d1],axis=0)
    258              output = output*mesharea; output = sum(output[:,d2,:],axis=1); output = sum(output[:,d1],axis=1)/totalarea
     272             totalarea = np.tile(mesharea,(output.shape[0],1,1))
     273             totalarea = np.ma.masked_where(getmask(output),totalarea)
     274             totalarea = mean(totalarea[:,d2,:],axis=1); totalarea = mean(totalarea[:,d1],axis=1)
     275             output = output*mesharea; output = mean(output[:,d2,:],axis=1); output = mean(output[:,d1],axis=1)/totalarea
    259276        elif d4 is not None and d3 is not None: 
    260277             output = mean(input[d4,:,:,:],axis=0)
     
    262279             if anomaly: output = 100. * ((output / smooth(output,csmooth)) - 1.)
    263280        elif d4 is not None and d2 is not None: 
    264              output = mean(input[d4,:,:,:],axis=0)
    265              totalarea=sum(mesharea[d2,:],axis=0); output = output*mesharea; output = sum(output[:,d2,:],axis=1)/totalarea
     281             output = mean(input[d4,:,:,:],axis=0)
     282             totalarea = np.tile(mesharea,(output.shape[0],1,1))
     283             totalarea = np.ma.masked_where(getmask(output),mesharea)
     284             totalarea = mean(totalarea[:,d2,:],axis=1)
     285             output = output*mesharea; output = mean(output[:,d2,:],axis=1)/totalarea
    266286        elif d4 is not None and d1 is not None: 
    267287             output = mean(input[d4,:,:,:],axis=0)
    268288             output = mean(output[:,:,d1],axis=2)
    269         elif d3 is not None and d2 is not None: 
     289        elif d3 is not None and d2 is not None:
    270290             output = reduce_zaxis(input[:,d3,:,:],ax=1,yint=yint,vert=alt,indice=d3)
    271              totalarea=sum(mesharea[d2,:],axis=0); output = output*mesharea; output = sum(output[:,d2,:],axis=1)/totalarea
     291             totalarea = np.tile(mesharea,(output.shape[0],1,1))
     292             totalarea = np.ma.masked_where(getmask(output),mesharea)
     293             totalarea = mean(totalarea[:,d2,:],axis=1)
     294             output = output*mesharea; output = mean(output[:,d2,:],axis=1)/totalarea
    272295        elif d3 is not None and d1 is not None: 
    273296             output = reduce_zaxis(input[:,d3,:,:],ax=1,yint=yint,vert=alt,indice=d3)
    274297             output = mean(output[:,:,d1],axis=2)
    275         elif d2 is not None and d1 is not None: 
    276              totalarea=sum(mesharea[d2,:],axis=0); totalarea=sum(totalarea[d1],axis=0)
    277              output = output*mesharea; output = sum(output[:,:,d2,:],axis=2); output = sum(output[:,:,d1],axis=2)/totalarea
     298        elif d2 is not None and d1 is not None:
     299             totalarea = np.tile(mesharea,(output.shape[0],output.shape[1],1,1))
     300             totalarea = np.ma.masked_where(getmask(output),totalarea)
     301             totalarea = mean(totalarea[:,:,d2,:],axis=2); totalarea = mean(totalarea[:,:,d1],axis=1)
     302             output = output*mesharea; output = mean(output[:,:,d2,:],axis=2); output = mean(output[:,:,d1],axis=1)/totalarea
    278303        elif d1 is not None:        output = mean(input[:,:,:,d1],axis=3)
    279         elif d2 is not None:
    280              totalarea=sum(mesharea[d2,:],axis=0); output = output*mesharea; output = sum(output[:,:,d2,:],axis=2)/totalarea
     304        elif d2 is not None:
     305             totalarea = np.tile(mesharea,(output.shape[0],output.shape[1],1,output.shape[3]))
     306             totalarea = np.ma.masked_where(getmask(output),totalarea)
     307             totalarea = mean(totalarea[:,:,d2,:],axis=2)
     308             output = output*mesharea; output = mean(output[:,:,d2,:],axis=2)/totalarea
    281309        elif d3 is not None:        output = reduce_zaxis(input[:,d3,:,:],ax=1,yint=yint,vert=alt,indice=d3)
    282310        elif d4 is not None:        output = mean(input[d4,:,:,:],axis=0)
     
    739767    #zecolor = 'r'
    740768    #zelinewidth = 1
    741     #step = 5.625
    742     #steplon = 5.625
    743     #zelatmax = 89.9
     769    #step = 180./48.
     770    #steplon = 360./64.
     771    #zelatmax = 90. - step/3
    744772    if char not in ["moll"]:
    745773        m.drawmeridians(np.r_[-180.:180.:steplon], labels=[0,0,0,1], color=zecolor, linewidth=zelinewidth, fontsize=fontsizemer, latmax=zelatmax)
     
    932960             "CO2":          "YlOrBr_r",\
    933961             #### T.N.
    934              "MTOT":         "Jet",\
     962             "MTOT":         "spectral",\
    935963             "H2O_ICE_S":    "RdBu",\
    936964             "VMR_H2OICE":   "PuBu",\
     
    10561084  return names
    10571085
    1058 ## Author: TN [old]
    1059 def getopmatrix (kind,n):
    1060   import numpy as np
    1061   # compute matrix of operations between files
    1062   # the matrix is 'number of files'-square
    1063   # 1: difference (row minus column), 2: add
    1064   # not 0 in diag : just plot
    1065   if n == 1:
    1066     opm = np.eye(1)
    1067   elif kind == 'basic':
    1068     opm = np.eye(n)
    1069   elif kind == 'difference1': # show differences with 1st file
    1070     opm = np.zeros((n,n))
    1071     opm[0,:] = 1
    1072     opm[0,0] = 0
    1073   elif kind == 'difference2': # show differences with 1st file AND show 1st file
    1074     opm = np.zeros((n,n))
    1075     opm[0,:] = 1
    1076   else:
    1077     opm = np.eye(n)
    1078   return opm
    1079  
    1080 ## Author: TN [old]
    1081 def checkcoherence (nfiles,nlat,nlon,ntime):
    1082   if (nfiles > 1):
    1083      if (nlat > 1):
    1084         errormess("what you asked is not possible !")
    1085   return 1
    10861086
    10871087## Author: TN
  • trunk/UTIL/PYTHON/myscript.py

    r637 r647  
    7474    ### SPECIAL
    7575    parser.add_option('--tsat',         action='store_true',dest='tsat',               default=False,help='convert temperature field T in Tsat-T using pressure')
     76    parser.add_option('--stream',       action='store_true',dest='stream',             default=False,help='plot streamlines from streamfunction.e for vert/lat slices.')
    7677
    7778    return parser
  • trunk/UTIL/PYTHON/planetoplot.py

    r642 r647  
    6666           lbls=None,\
    6767           lstyle=None,\
    68            cross=None):
     68           cross=None,\
     69           streamflag=False):
    6970
    7071    ####################################################################################################################
     
    9192    #from singlet import singlet
    9293    from itertools import cycle
     94    import os
    9395
    9496#########################
     
    414416           if mult != 2718.:  what_I_plot = what_I_plot*mult
    415417           else:              what_I_plot = np.log10(what_I_plot) ; print "log plot"
     418                     
    416419       if var2:      ### what is contoured.
    417420           what_I_plot_contour, error = reducefield( all_var2[index_f], d4=indextime, d1=indexlon, d2=indexlat , d3=indexvert, \
     
    523526                           if indexvert is not None: lbl = lbl + " iz" + str(indexvert[0])
    524527                           if indextime is not None: lbl = lbl + " it" + str(indextime[0])
    525                            if lbl == "": lbl = namefiles[index_f]
     528                           if lbl == "": lbl = all_namefile[index_f]
    526529
    527530                        if mrate is not None: x = y  ## because swapaxes...
     
    539542
    540543                    elif which == "regular":
     544                   
     545                        # plot stream lines if there is a stream file and a vert/lat slice. Might not work with movies ??
     546                        if streamflag and sslat is None and svert is None:
     547                             streamfile = all_namefile[index_f].replace('.nc','_stream.nc')
     548                             teststream = os.path.exists(streamfile)
     549                             if teststream:
     550                                print 'INFO: Using stream file',streamfile, 'for stream lines'
     551                                ncstream = Dataset(streamfile)
     552                                psi = getfield(ncstream,'psi')
     553                                psi = psi[0,:,:,0] # time and longitude are dummy dimensions
     554                                if psi.shape[1] != len(x) or psi.shape[0] != len(y):
     555                                    errormess('stream file does not fit! Dimensions: '+str(psi.shape)+' '+str(x.shape)+' '+str(y.shape))
     556                                zelevels = np.arange(-1.e10,1.e10,1.e9)
     557                                zemin = np.min(abs(zelevels))
     558                                zemax = np.max(abs(zelevels))
     559                                zewidth  =  (abs(zelevels)-zemin)*(5.- 0.5)/(zemax - zemin) + 0.5 # linewidth ranges from 5 to 0.5
     560                                cs = contour( x,y,psi, zelevels, colors='k', linewidths = zewidth)
     561                                clabel(cs, inline=True, fontsize = 4.*rcParams['font.size']/5., fmt="%1.1e")
     562                             else:
     563                                print 'WARNING: STREAM FILE',streamfile, 'DOES NOT EXIST !'
     564                             
    541565                        if hole:         what_I_plot_frame = hole_bounds(what_I_plot_frame,zevmin,zevmax)
    542566                        else:            what_I_plot_frame = bounds(what_I_plot_frame,zevmin,zevmax)
     
    554578                            if mapmode == 1:       m.pcolor( x, y, what_I_plot_frame, cmap = palette, vmin=zevmin, vmax=zevmax, alpha=trans)
    555579                            elif mapmode == 0:     pcolor( x, y, what_I_plot_frame, cmap = palette, vmin=zevmin, vmax=zevmax, alpha=trans)
     580                       
    556581
    557582                        if colorb not in ['nobar','onebar']:       
     
    591616                            zebins = np.linspace(zevmin,zevmax,num=30)
    592617                            hist(toplot,bins=zebins,histtype='step',linewidth=2,color='k',normed=True)
    593                             zestd = np.std(toplot)
    594                             zemean = np.mean(toplot)
     618                            zestd = np.std(toplot);zemean = np.mean(toplot)
    595619                            zebins = np.linspace(zevmin,zevmax,num=300)
    596620                            zegauss = (1./(zestd * np.sqrt(2 * np.pi)) * np.exp( - (zebins - zemean)**2 / (2 * zestd**2) ) )
     
    675699                if index_f is numplot-1:     plottitle = basename+' '+"fig(1) "+ope+" fig(2)"
    676700                elif index_f is numplot-2:   plottitle = basename+' '+fileref
    677                 else:                        plottitle = basename+' '+namefiles[0]#index_f]
    678             else:                            plottitle = basename+' '+namefiles[0]#index_f]
     701                else:                        plottitle = basename+' '+all_namefile[index_f]
     702            else:                            plottitle = basename+' '+all_namefile[index_f]
    679703       if mult != 1:                         plottitle = '{:.0e}'.format(mult) + "*" + plottitle
    680704       if zetitle[0] != "fill":                 
  • trunk/UTIL/PYTHON/pp.py

    r612 r647  
    172172                mrate=opt.rate,mquality=opt.quality,trans=opt.trans,zarea=opt.area,axtime=opt.axtime,\
    173173                redope=opt.redope,seevar=opt.seevar,xlab=opt.xlab,ylab=opt.ylab,lbls=separatenames(opt.labels),\
    174                 lstyle=separatenames(opt.linestyle),cross=readslices(opt.mark))
     174                lstyle=separatenames(opt.linestyle),cross=readslices(opt.mark),streamflag=opt.stream)
    175175        print 'DONE: '+name
    176176        system("rm -f to_be_erased")
Note: See TracChangeset for help on using the changeset viewer.