Changeset 724


Ignore:
Timestamp:
Jul 16, 2012, 11:15:30 PM (12 years ago)
Author:
aslmd
Message:

UTIL PYTHON. erase a duplicate in planetoplot about defining lat and lon. added the capability to guess dimension arrays if not present. this allows to plot virtually anything in any NETCDF file, in particular file with 4D or 3D variables included but no clear reference to lat lon vert time, just for instance x y z t. convenient for extracted fields.

Location:
trunk/UTIL/PYTHON
Files:
2 edited

Legend:

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

    r721 r724  
    645645            print "WARNING: domain plot artificially centered on lat,lon 0,0"
    646646    elif typefile in ['gcm','earthgcm','ecmwf']:
     647        #### n est ce pas nc.variables ?
    647648        if "longitude" in nc.dimensions:  dalon = "longitude"
    648649        elif "lon" in nc.dimensions:      dalon = "lon"
     650        else:                             dalon = "nothing"
    649651        if "latitude" in nc.dimensions:   dalat = "latitude"
    650652        elif "lat" in nc.dimensions:      dalat = "lat"
     653        else:                             dalat = "nothing"
    651654        [lon2d,lat2d] = getcoord2d(nc,nlat=dalat,nlon=dalon,is1d=True)
    652655    elif typefile in ['geo']:
     
    657660def getcoord2d (nc,nlat='XLAT',nlon='XLONG',is1d=False):
    658661    import numpy as np
    659     if is1d:
    660         lat = nc.variables[nlat][:]
    661         lon = nc.variables[nlon][:]
     662    if nlon == "nothing" or nlat == "nothing":
     663        print "NO LAT LON FIELDS. I AM TRYING MY BEST. I ASSUME GLOBAL FIELD."
     664        lon = np.linspace(-180.,180.,getdimfromvar(nc)[-1])
     665        lat = np.linspace(-90.,90.,getdimfromvar(nc)[-2])
    662666        [lon2d,lat2d] = np.meshgrid(lon,lat)
    663667    else:
    664         lat = nc.variables[nlat][0,:,:]
    665         lon = nc.variables[nlon][0,:,:]
    666         [lon2d,lat2d] = [lon,lat]
     668        if is1d:
     669            lat = nc.variables[nlat][:]
     670            lon = nc.variables[nlon][:]
     671            [lon2d,lat2d] = np.meshgrid(lon,lat)
     672        else:
     673            lat = nc.variables[nlat][0,:,:]
     674            lon = nc.variables[nlon][0,:,:]
     675            [lon2d,lat2d] = [lon,lat]
    667676    return lon2d,lat2d
     677
     678## Author: AS
     679def getdimfromvar (nc):
     680    varinfile = nc.variables.keys()
     681    dim = nc.variables[varinfile[-1]].shape ## usually the last variable is 4D or 3D
     682    return dim
    668683
    669684## FROM COOKBOOK http://www.scipy.org/Cookbook/SignalSmooth
  • trunk/UTIL/PYTHON/planetoplot.py

    r721 r724  
    8080                       zoomset,getcoorddef,getwinddef,whatkindfile,reducefield,bounds,getstralt,getfield,smooth,nolow,\
    8181                       getname,localtime,check_localtime,polarinterv,getsindex,define_axis,determineplot,readslices,bidimfind,getlschar,hole_bounds,\
    82                        windamplitude,slopeamplitude,deltat0t1,enrichment_factor
     82                       windamplitude,slopeamplitude,deltat0t1,enrichment_factor,getdimfromvar
    8383    from mymath import deg,max,min,mean,get_tsat,writeascii,fig2data,fig2img
    8484    import matplotlib as mpl
     
    184184          if slon is not None: sslon = slon 
    185185          if slat is not None: sslat = slat 
    186           ### LAT
    187           if "latitude" in nc.variables:  lat = nc.variables["latitude"][:]
    188           elif "lat" in nc.variables:     lat = nc.variables["lat"][:]
    189           ### LON
    190           if "longitude" in nc.variables: lon = nc.variables["longitude"][:]
    191           elif "lon" in nc.variables:     lon = nc.variables["lon"][:]
     186          ### LAT : done in getcoorddef
     187          lat = lat2d[0,:]
     188          ### LON : done in getcoorddef
     189          lon = lon2d[:,0]
    192190          ### ALT
    193191          if "altitude" in nc.variables:   vert = nc.variables["altitude"][:]
     
    195193          elif "lev" in nc.variables:      vert = nc.variables["lev"][:]
    196194          elif "presnivs" in nc.variables: vert = nc.variables["presnivs"][:]
    197           else:                            vert = [0.]
     195          else:
     196              print "No altitude found. Try to build a simple axis."
     197              dadim = getdimfromvar(nc)
     198              if   len(dadim) == 4:  print "-- 4D field. Assume z is dim 2." ; vert = np.arange(dadim[-3])
     199              elif len(dadim) == 3:  print "-- 3D field. Assume z is dim 1." ; vert = [0.]
     200              else:                  errormess("-- 2D or 1D field. Unclear. Stop.")
     201          #else:                            vert = [0.]
    198202          ### AIRE (to weight means with the area)
    199203          if "aire" in nc.variables:      area = nc.variables["aire"][:,:] 
     
    203207          elif "time" in nc.variables:          time = nc.variables["time"][:]
    204208          elif "time_counter" in nc.variables:  time = nc.variables["time_counter"][:]/86400. #### time counter cinverstion from s-> days
    205           else:                                 time = [0.] #errormess("no time axis found.")
     209          else:
     210              print "No time found. Try to build a simple axis. Assume t is dim 1."
     211              dadim = getdimfromvar(nc)
     212              if   len(dadim) == 4:  time = np.arange(dadim[-4])
     213              elif len(dadim) == 3:  time = np.arange(dadim[-3])
     214              else:                  errormess("-- I am sorry this is 2D or 1D field. I failed.")
     215          #else:                                time = [0.] #errormess("no time axis found.")
    206216          if axtime in ["ls"]:
    207217              print "converting to Ls ..."
Note: See TracChangeset for help on using the changeset viewer.