Ignore:
Timestamp:
Aug 19, 2012, 12:19:59 AM (12 years ago)
Author:
aslmd
Message:

UTIL PYTHON : a more definitive version of what could make a new and simple MCD web interface. added a lot of capabilities: variable setting, beginner mode, wind vector, fixed or not local time, etc etc etc. some modifications were made to the python MCD interface too (this makes the basis for the web interface).

File:
1 edited

Legend:

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

    r723 r761  
    6060    def getextvarlab(self,num):
    6161        whichfield = { \
     62        91: "Pressure (Pa)", \
     63        92: "Density (kg/m3)", \
     64        93: "Temperature (K)", \
     65        94: "W-E wind component (m/s)", \
     66        95: "S-N wind component (m/s)", \
    6267        1: "Radial distance from planet center (m)",\
    6368        2: "Altitude above areoid (Mars geoid) (m)",\
     
    111116        50: "Air viscosity estimation (N s m-2)"
    112117        }
    113         if num not in whichfield: errormess("Incorrect subscript in extvar.")
     118        if num not in whichfield: myplot.errormess("Incorrect subscript in extvar.")
    114119        return whichfield[num]
     120
     121    def convertlab(self,num):       
     122        ## a conversion from text inquiries to extvar numbers. to be completed.
     123        if num == "p": num = 91
     124        elif num == "rho": num = 92
     125        elif num == "t": num = 93
     126        elif num == "u": num = 94
     127        elif num == "v": num = 95
     128        elif num == "tsurf": num = 15
     129        elif num == "topo": num = 4
     130        elif num == "h": num = 13
     131        elif num == "ps": num = 19
     132        elif num == "tau": num = 36
     133        elif num == "mtot": num = 40
     134        elif num == "icetot": num = 42
     135        elif num == "ps_ddv": num = 22
     136        elif num == "h2ovap": num = 41
     137        elif num == "h2oice": num = 43
     138        elif num == "cp": num = 8
     139        elif num == "rho_ddv": num = 10
     140        elif num == "tsurfmx": num = 16
     141        elif num == "tsurfmn": num = 17
     142        elif num == "lwdown": num = 31
     143        elif num == "swdown": num = 32
     144        elif num == "lwup": num = 33
     145        elif num == "swup": num = 34
     146        elif num == "o3": num = 44
     147        elif num == "o": num = 46
     148        elif num == "co": num = 48
     149        elif num == "visc": num = 50
     150        elif num == "co2ice": num = 35
     151        elif not isinstance(num, np.int): myplot.errormess("field reference not found.")
     152        return num
    115153
    116154###################
     
    126164             self.datekey,self.xdate,self.loct,self.dset,self.dust, \
    127165             self.perturkey,self.seedin,self.gwlength,self.extvarkey )
     166        ## we use the end of extvar (unused) to store meanvar. this is convenient for getextvar(lab)
     167        self.extvar[90] = self.pres ; self.extvar[91] = self.dens
     168        self.extvar[92] = self.temp ; self.extvar[93] = self.zonwind ; self.extvar[94] = self.merwind
    128169
    129170    def printset(self):
     
    139180    def printcoord(self):
    140181    # print requested space-time coordinates
    141         print "----------------------------------------------------------------"
    142182        print "LAT",self.lat,"LON",self.lon,"LOCT",self.loct,"XDATE",self.xdate
    143         print "----------------------------------------------------------------"
    144183
    145184    def printmeanvar(self):
     
    153192    def printextvar(self,num):
    154193    # print extra MCD variables
    155         print self.getextvarlab(num) + " ---> " + str(self.extvar[num-1])
     194        num = self.convertlab(num)
     195        print self.getextvarlab(num) + " ..... " + str(self.extvar[num-1])
    156196
    157197    def printallextvar(self):
    158198    # print all extra MCD variables   
    159199        for i in range(50): self.printextvar(i+1)
     200
     201    def htmlprinttabextvar(self,tabtodo):
     202        print "Results from the Mars Climate Database"
     203        print "<ul>"
     204        for i in range(len(tabtodo)): print "<li>" ; self.printextvar(tabtodo[i]) ; print "</li>"
     205        print "</ul>"
     206        print "<hr>"
     207        print "SETTINGS<br />"
     208        self.printcoord()
     209        self.printset()
    160210
    161211    def printmcd(self):
     
    163213        self.update()
    164214        self.printcoord()
     215        print "-------------------------------------------"
    165216        self.printmeanvar()
    166217
     
    187238    def definefield(self,choice):
    188239    ### for analysis or plot purposes, set field and field label from user-defined choice
    189     ### --- choice can be a MCD number for extvar
    190       if isinstance(choice, np.int):    field = self.getextvar(choice); fieldlab = self.getextvarlab(choice)
    191       else:
    192        if choice == "t":        field = self.temptab ; fieldlab="Temperature (K)"
    193        elif choice == "p":      field = self.prestab ; fieldlab="Pressure (Pa)"
    194        elif choice == "rho":    field = self.denstab ; fieldlab="Density (kg/m3)"
    195        elif choice == "u":      field = self.zonwindtab ; fieldlab="W-E wind component (m/s)"
    196        elif choice == "v":      field = self.merwindtab ; fieldlab="S-N wind component (m/s)"
    197        elif choice == "tsurf":  field = self.getextvar(15); fieldlab="Surface temperature (K)"
    198        elif choice == "topo":   field = self.getextvar(4) ; fieldlab="Topography (m)"
    199        elif choice == "h":      field = self.getextvar(13); fieldlab = "Scale height (m)"
    200        elif choice == "ps":     field = self.getextvar(19); fieldlab = "Surface pressure (Pa)"
    201        elif choice == "olr":    field = self.getextvar(33); fieldlab = "Outgoing longwave radiation (W/m2)"
    202        elif choice == "tau":    field = self.getextvar(36); fieldlab = "Dust optical depth"
    203        elif choice == "mtot":   field = self.getextvar(40); fieldlab = "Water vapor column (kg/m2)"
    204        elif choice == "icetot": field = self.getextvar(42); fieldlab = "Water ice column (kg/m2)"
    205        elif choice == "ps_ddv": field = self.getextvar(22); fieldlab = "Surface pressure RMS day to day variations (Pa)"
    206        else:                    errormess("field reference not found.")
     240      choice = self.convertlab(choice)
     241      field = self.getextvar(choice); fieldlab = self.getextvarlab(choice)
    207242      return field,fieldlab
    208243
     
    214249    ## fill in subscript i in output arrays
    215250    ## (arrays must have been correctly defined through prepare)
    216       if self.prestab is None:  errormess("arrays must be prepared first through self.prepare")
     251      if self.prestab is None:  myplot.errormess("arrays must be prepared first through self.prepare")
    217252      self.prestab[i] = self.pres ; self.denstab[i] = self.dens ; self.temptab[i] = self.temp
    218253      self.zonwindtab[i] = self.zonwind ; self.merwindtab[i] = self.merwind
     
    238273      for i in range(nd): self.lat = self.xcoord[i] ; self.update() ; self.put1d(i)
    239274
    240     def profile(self,nd=20,start=0.,end=100000.,tabperso=None):
     275    def profile(self,nd=20,start=0.,end=120000.,tabperso=None):
    241276    ### retrieve an altitude slice (profile)
    242277      self.xlabel = "Altitude (m)"
     
    253288      for i in range(nd): self.xdate = self.xcoord[i] ; self.update() ; self.put1d(i)
    254289
    255     def latlon(self,ndx=37,startx=-180.,endx=180.,ndy=19,starty=-90.,endy=90.):
    256     ### retrieve a latitude/longitude slice
    257       self.xlabel = "East longitude (degrees)" ; self.ylabel = "North latitude (degrees)"
    258       self.prepare(ndx=ndx,ndy=ndy)
    259       self.xcoord = np.linspace(startx,endx,ndx) ; self.ycoord = np.linspace(starty,endy,ndy)
    260       for i in range(ndx):
    261        for j in range(ndy):
    262          self.lon = self.xcoord[i] ; self.lat = self.ycoord[j] ; self.update() ; self.put2d(i,j)
    263 
    264290    def makeplot1d(self,choice,vertplot=0):
    265291    ### one 1D plot is created for the user-defined variable in choice.
     
    268294      else:              ordo = self.xcoord ; absc = field ; absclab = fieldlab ; ordolab = self.xlabel
    269295      mpl.plot(absc,ordo,'-bo') ; mpl.ylabel(ordolab) ; mpl.xlabel(absclab) #; mpl.xticks(query.xcoord)
     296      mpl.figtext(0.5, 0.01, "Mars Climate Database (c) LMD/OU/IAA/ESA/CNES", ha='center')
    270297
    271298    def plot1d(self,tabtodo,vertplot=0):
     
    280307###################
    281308
     309    def latlon(self,ndx=37,startx=-180.,endx=180.,ndy=19,starty=-90.,endy=90.,fixedlt=False):
     310    ### retrieve a latitude/longitude slice
     311    ### default is: local time is not fixed. user-defined local time is at longitude 0.
     312      self.xlabel = "East longitude (degrees)" ; self.ylabel = "North latitude (degrees)"
     313      self.prepare(ndx=ndx,ndy=ndy)
     314      self.xcoord = np.linspace(startx,endx,ndx) ; self.ycoord = np.linspace(starty,endy,ndy)
     315      if not fixedlt: umst = self.loct
     316      for i in range(ndx):
     317       for j in range(ndy):
     318         self.lon = self.xcoord[i] ; self.lat = self.ycoord[j]
     319         if not fixedlt: self.loct = (umst + self.lon/15.) % 24
     320         self.update() ; self.put2d(i,j)
     321      if not fixedlt: self.loct = umst
     322
    282323    def put2d(self,i,j):
    283324    ## fill in subscript i,j in output arrays
    284325    ## (arrays must have been correctly defined through prepare)
    285       if self.prestab is None:  errormess("arrays must be prepared first through self.prepare")
     326      if self.prestab is None:  myplot.errormess("arrays must be prepared first through self.prepare")
    286327      self.prestab[i,j] = self.pres ; self.denstab[i,j] = self.dens ; self.temptab[i,j] = self.temp
    287328      self.zonwindtab[i,j] = self.zonwind ; self.merwindtab[i,j] = self.merwind
     
    289330      self.extvartab[i,j,1:100] = self.extvar[0:99] ## note: var numbering according to MCD manual is kept
    290331
    291     def makemap2d(self,choice,incwind=False):
     332    def makemap2d(self,choice,incwind=False,fixedlt=False):
    292333    ### one 2D map is created for the user-defined variable in choice.
    293       self.latlon() ## a map is implicitely a lat-lon plot. otherwise it is a plot (cf. makeplot2d)
     334      self.latlon(fixedlt=fixedlt) ## a map is implicitely a lat-lon plot. otherwise it is a plot (cf. makeplot2d)
    294335      (field, fieldlab) = self.definefield(choice)
    295336      if incwind:
     
    299340      else:
    300341          myplot.maplatlon(self.xcoord,self.ycoord,field,title=fieldlab,proj="moll")
    301 
    302     def map2d(self,tabtodo,incwind=False):
     342      mpl.figtext(0.5, 0.0, "Mars Climate Database (c) LMD/OU/IAA/ESA/CNES", ha='center')
     343
     344    def map2d(self,tabtodo,incwind=False,fixedlt=False):
    303345    ### complete 2D figure with possible multiplots
    304346      if isinstance(tabtodo,np.str): tabtodo=[tabtodo] ## so that asking one element without [] is possible.
    305347      if isinstance(tabtodo,np.int): tabtodo=[tabtodo] ## so that asking one element without [] is possible.
    306348      fig = mpl.figure() ; subv,subh = myplot.definesubplot( len(tabtodo) , fig )
    307       for i in range(len(tabtodo)): mpl.subplot(subv,subh,i+1) ; self.makemap2d(tabtodo[i],incwind=incwind)
    308 
    309     ### TODO: makeplot2d, plot2d, passer plot settings, vecteurs, plot loct pas fixe
    310 
     349      for i in range(len(tabtodo)): mpl.subplot(subv,subh,i+1) ; self.makemap2d(tabtodo[i],incwind=incwind,fixedlt=fixedlt)
     350
     351    ### TODO: makeplot2d, plot2d, passer plot settings
     352
Note: See TracChangeset for help on using the changeset viewer.