Changeset 754


Ignore:
Timestamp:
Aug 2, 2012, 12:55:45 PM (12 years ago)
Author:
acolaitis
Message:

Python. Simplified planetoplot.py: all specific cases are now under the hood, in myplot. (see select_case). The user can add new specific cases to select_case routine without planetoplot.py getting dirtier.

Location:
trunk/UTIL/PYTHON
Files:
2 edited

Legend:

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

    r753 r754  
    174174    if "slopex" in varinfile: zu=getfield(nc,'slopex')
    175175    elif "SLOPEX" in varinfile: zu=getfield(nc,'SLOPEX')
     176    else: errormess("you need slopex or SLOPEX in your file.")
    176177    if "slopey" in varinfile: zv=getfield(nc,'slopey')
    177178    elif "SLOPEY" in varinfile: zv=getfield(nc,'SLOPEY')
     179    else: errormess("you need slopey or SLOPEY in your file.")
    178180    znt,zny,znx = np.array(zu).shape
    179181    zuint = np.zeros([znt,zny,znx])
     
    192194    if "tsurf" in varinfile: zu=getfield(nc,'tsurf')
    193195    elif "TSURF" in varinfile: zu=getfield(nc,'TSURF')
     196    else: errormess("You need tsurf or TSURF in your file")
    194197    if "tk" in varinfile: zv=getfield(nc,'tk')
    195198    elif "TK" in varinfile: zv=getfield(nc,'TK')
     199    else: errormess("You need tk or TK in your file. (might need to use API. try to add -i 4 -l XXX)")
    196200    znt,zny,znx = np.array(zu).shape
    197201    zuint = np.zeros([znt,zny,znx])
     
    14171421    ## scale regle la reference du vecteur. factor regle toutes les longueurs (dont la reference). l'AUGMENTER pour raccourcir les vecteurs.
    14181422    return
     1423## Author : AC
     1424## Handles calls to specific computations (e.g. wind norm, enrichment factor...)
     1425def select_case(zvarname=None,znc=None,ztypefile=None,mode=None,ztsat=None,ylon=None,ylat=None,yalt=None,ytime=None):
     1426      from mymath import get_tsat
     1427 
     1428      ## Specific variables are described here:
     1429      # for the mesoscale:
     1430      specificname_meso = ['UV','uv','uvmet','slopexy','SLOPEXY','deltat','DELTAT']
     1431      # for the gcm:
     1432      specificname_gcm = ['enfact']
     1433
     1434      ## Check for variable in file:
     1435      if mode == 'check':     
     1436          varname = zvarname
     1437          varinfile=znc.variables.keys()
     1438          logical_novarname = zvarname not in znc.variables
     1439          logical_nospecificname_meso = not ((ztypefile in ['meso']) and (zvarname in specificname_meso))
     1440          logical_nospecificname_gcm = not ((ztypefile in ['gcm']) and (zvarname in specificname_gcm))
     1441          if ( logical_novarname and logical_nospecificname_meso and logical_nospecificname_gcm ):
     1442              if len(varinfile) == 1:   varname = varinfile[0]
     1443              else:                     varname = False
     1444          ## Return the variable name:
     1445          return varname
     1446
     1447      ## Get the corresponding variable:
     1448      if mode == 'getvar':
     1449          ### ----------- 1. saturation temperature
     1450          if zvarname in ["temp","t","T_nadir_nit","T_nadir_day","temp_day","temp_night"] and ztsat:
     1451              tt=getfield(znc,zvarname) ; print "computing Tsat-T, I ASSUME Z-AXIS IS PRESSURE"
     1452              if type(tt).__name__=='MaskedArray':  tt.set_fill_value([np.NaN]) ; tinput=tt.filled()
     1453              else:                                 tinput=tt
     1454              all_var=get_tsat(yalt,tinput,zlon=ylon,zlat=ylat,zalt=yalt,ztime=ytime)
     1455          ### ----------- 2. wind amplitude
     1456          elif ((zvarname in ['UV','uv','uvmet']) and (ztypefile in ['meso']) and (zvarname not in znc.variables)):
     1457              all_var=windamplitude(znc)
     1458          elif ((zvarname in ['slopexy','SLOPEXY']) and (ztypefile in ['meso']) and (zvarname not in znc.variables)):
     1459              all_var=slopeamplitude(znc)
     1460          ### ------------ 3. Near surface instability
     1461          elif ((zvarname in ['DELTAT','deltat']) and (ztypefile in ['meso']) and (zvarname not in znc.variables)):
     1462              all_var=deltat0t1(znc)
     1463          ### ------------ 4. Enrichment factor
     1464          elif ((ztypefile in ['gcm']) and (zvarname in ['enfact'])):
     1465              all_var=enrichment_factor(znc,ylon,ylat,ytime)
     1466          else:
     1467          ### -----------  999. Normal case
     1468              all_var = getfield(znc,zvarname)
     1469          return all_var
     1470       
  • trunk/UTIL/PYTHON/planetoplot.py

    r753 r754  
    8484                       zoomset,getcoorddef,getwinddef,whatkindfile,reducefield,bounds,getstralt,getfield,smooth,nolow,\
    8585                       getname,localtime,check_localtime,polarinterv,getsindex,define_axis,determineplot,readslices,bidimfind,getlschar,hole_bounds,\
    86                        windamplitude,slopeamplitude,deltat0t1,enrichment_factor,getdimfromvar
    87     from mymath import deg,max,min,mean,get_tsat,writeascii,fig2data,fig2img
     86                       getdimfromvar,select_case
     87    from mymath import deg,max,min,mean,writeascii,fig2data,fig2img
    8888    import matplotlib as mpl
    8989    from matplotlib.pyplot import contour,contourf,hist, text,subplot, figure, rcParams, savefig, colorbar, \
     
    163163    ### we get the names of variables to be read. in case only one available, we choose this one.
    164164    ### (we take out of the test specific names e.g. UV is not in the file but used to ask a wind speed computation)
    165       varname=var[vvv]
    166       logical_novarname = varname not in nc.variables
    167       specificname_meso = ['UV','uv','uvmet','slopexy','SLOPEXY','deltat','DELTAT']
    168       logical_nospecificname_meso = not ((typefile in ['meso']) and (varname in specificname_meso))
    169       specificname_gcm = ['enfact']
    170       logical_nospecificname_gcm = not ((typefile in ['gcm']) and (varname in specificname_gcm))
    171       if ( logical_novarname and logical_nospecificname_meso and logical_nospecificname_gcm ):
    172           if len(varinfile) == 1:   varname = varinfile[0]
    173           else:                     varname = False
     165      varname = select_case(zvarname=var[vvv],znc=nc,ztypefile=typefile,mode='check')
    174166    ### we get the names of wind variables to be read (if any)
    175167      if winds:                                                   
     
    314306      all_namefile[k] = namefile
    315307      all_time[k] = time
    316       if var2:
    317          all_var2[k] = getfield(nc,var2)
    318          ### v--- too SPECIFIC (see below)
    319          if ((var2 in ['slopexy','SLOPEXY']) and (typefile in ['meso']) and (var2 not in nc.variables)): all_var2[k] = slopeamplitude(nc)
     308      if var2:  all_var2[k] = select_case(zvarname=var2,znc=nc,ztypefile=typefile,mode='getvar',ztsat=tsat,ylon=lon,ylat=lat,yalt=vert,ytime=all_time[k])
    320309      if winds: all_windu[k] = getfield(nc,uchar) ; all_windv[k] = getfield(nc,vchar)
    321310    ### we fill the arrays of fields to be plotted at the current step considered
    322       ### ----------- SPECIFIC CASES. NOT HAPPY WITH THIS. note : we could probably call those via a "toolbox" in myplot.
    323       ### ----------- 1. saturation temperature
    324       if varname in ["temp","t","T_nadir_nit","T_nadir_day","temp_day","temp_night"] and tsat:
    325           tt=getfield(nc,varname) ; print "computing Tsat-T, I ASSUME Z-AXIS IS PRESSURE"
    326           if type(tt).__name__=='MaskedArray':  tt.set_fill_value([np.NaN]) ; tinput=tt.filled()
    327           else:                                 tinput=tt
    328           all_var[k]=get_tsat(vert,tinput,zlon=lon,zlat=lat,zalt=vert,ztime=time)
    329       ### ----------- 2. wind amplitude
    330       elif ((varname in ['UV','uv','uvmet']) and (typefile in ['meso']) and (varname not in nc.variables)):
    331           all_var[k]=windamplitude(nc)
    332       elif ((varname in ['slopexy','SLOPEXY']) and (typefile in ['meso']) and (varname not in nc.variables)):
    333           all_var[k]=slopeamplitude(nc)
    334       ### ------------ 3. Near surface instability
    335       elif ((varname in ['DELTAT','deltat']) and (typefile in ['meso']) and (varname not in nc.variables)):
    336           all_var[k]=deltat0t1(nc)
    337       ### ------------ 4. Enrichment factor
    338       elif ((typefile in ['gcm']) and (varname in ['enfact'])):
    339           all_var[k]=enrichment_factor(nc,lon,lat,time)
    340       else:
    341       ### ideally only this line should be here
    342           all_var[k] = getfield(nc,varname)
     311      all_var[k] = select_case(zvarname=all_varname[k],znc=nc,ztypefile=typefile,mode='getvar',ztsat=tsat,ylon=lon,ylat=lat,yalt=vert,ytime=all_time[k])
    343312    ### we inform the user about the loop then increment the loop. this is the last line of "for namefile in namefiles"
    344313      print "**** GOT SUBDATA:",k," NAMEFILE:",namefile," VAR:",varname, var2 ; k += 1 ; firstfile = False
     
    355324                if fileref is not None:   
    356325                   ncref = Dataset(fileref)
    357                    if ((all_varname[k-1] in ['UV','uv','uvmet']) and (typefile in ['meso']) and (all_varname[k-1] not in ncref.variables)):
    358                       all_var[k] = windamplitude(ncref)
    359                    elif ((all_varname[k-1] in ['slopexy','SLOPEXY']) and (typefile in ['meso']) and (all_varname[k-1] not in ncref.variables)):
    360                       all_var[k] = slopeamplitude(ncref)
    361                    elif ((all_varname[k-1] in ['DELTAT','deltat']) and (typefile in ['meso']) and (all_varname[k-1] not in ncref.variables)):
    362                       all_var[k]=deltat0t1(ncref)
    363                    else:
    364                       all_var[k] = getfield(ncref,all_varname[k-1])
     326                   all_var[k] = select_case(zvarname=all_varname[k-1],znc=ncref,ztypefile=typefile,mode='getvar',ztsat=tsat,ylon=lon,ylat=lat,yalt=vert,ytime=all_time[k],zvert=vert)
    365327                   all_varname[k] = all_varname[k-1] ; all_time[k] = all_time[k-1] ; all_namefile[k] = all_namefile[k-1] ; all_var2[k] = all_var2[k-1]
    366328                   if winds: all_windu[k] = getfield(ncref,uchar) ; all_windv[k] = getfield(ncref,vchar)
Note: See TracChangeset for help on using the changeset viewer.