Changeset 1219 for trunk


Ignore:
Timestamp:
Apr 6, 2014, 8:19:31 AM (11 years ago)
Author:
aslmd
Message:

PLANETOPLOT. improve flexibility of code for computations. added computation of perturbations and diff

Location:
trunk/UTIL/PYTHON/planetoplot_v2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/UTIL/PYTHON/planetoplot_v2/ppclass.py

    r1215 r1219  
    871871                    # generic 1D/2D: load field and coord in plot object
    872872                    plobj.f = obj.field    # field to be plotted
    873                     plobj.x = obj.absc      # abscissa (or longitude)
    874                     plobj.y = obj.ordi      # ordinate (or latitude)
    875                                                # -- useless in 1D but not used anyway
     873                    plobj.x = obj.absc     # abscissa (or longitude)
     874                    plobj.y = obj.ordi     # ordinate (or latitude)
     875                                           # -- useless in 1D but not used anyway
    876876                    # specific 1D plot stuff
    877877                    if dp == 1:
     
    16891689                self.index_x = yeah[0,:]
    16901690            if self.method_y == "comp":
    1691                 yeah = (self.field_y >= dalisty[indy][0]) * (self.field_y <= dalisty[indy][1])
     1691                yeah = (self.field_y >= dalisty[indy][0])*(self.field_y <= dalisty[indy][1])
    16921692                self.index_y = yeah[:,0]
    16931693            self.index_x2d = self.index_x
     
    17561756          elif self.dimplot == 1 and self.verbose:   print "**** OK. 1D plot requested."
    17571757          elif self.verbose:                         print "**** OK. 2D section requested."
    1758         # well, now get field from netcdf file
     1758        # well, now get field from netcdf file 
    17591759        # part below is necessary otherwise there is an index error below
    17601760        if self.index_x.size == 1: self.index_x = self.index_x[0]
     
    17621762        if self.index_z.size == 1: self.index_z = self.index_z[0]
    17631763        if self.index_t.size == 1: self.index_t = self.index_t[0]
    1764         # then retrieve what is requested by user
    1765         # each self.dim case corresponds to tests in the beginning of getdim.
    1766         time0 = timelib.time()
    1767         if self.verbose: print "**** OK. I am getting values from files. Please wait."
     1764        # set everything about dimensions and slices
     1765        # -- each self.dim case corresponds to tests in the beginning of getdim.
    17681766        if self.dim == 1: 
    17691767            nt = self.index_t.size ; nz = 1 ; ny = 1 ; nx = 1
    1770             self.field = self.f.variables[self.var][self.index_t]
     1768            tupledim = self.index_t
    17711769        elif self.dim == 2:
    17721770            nt = 1 ; nz = 1 ; ny = self.index_y.size ; nx = self.index_x.size
    1773             self.field = self.f.variables[self.var][self.index_y,self.index_x]
     1771            tupledim = self.index_y,self.index_x
    17741772        elif self.dim == 3:
    17751773            ## DEFAULT tyx (time-varying 2D field)
    17761774            if self.kind3d == "tyx":
    17771775               nt = self.index_t.size ; nz = 1 ; ny = self.index_y.size ; nx = self.index_x.size
    1778                self.field = self.f.variables[self.var][self.index_t,self.index_y,self.index_x]
     1776               tupledim = self.index_t,self.index_y,self.index_x
    17791777            ## CASE tzy (e.g. time-varying zonal mean y-z field)
    17801778            elif self.kind3d == "tzy":
    17811779               nt = self.index_t.size ; nz = self.index_z.size ; ny = self.index_y.size ; nx = 1
    1782                self.field = self.f.variables[self.var][self.index_t,self.index_z,self.index_y]
     1780               tupledim = self.index_t,self.index_z,self.index_y
    17831781            else:
    17841782               print "!! ERROR !! This kind of 3D field is not supported. Please send feedback." ; exit()
     
    17861784        elif self.dim == 4:
    17871785            nt = self.index_t.size ; nz = self.index_z.size ; ny = self.index_y.size ; nx = self.index_x.size
    1788             self.field = self.f.variables[self.var][self.index_t,self.index_z,self.index_y,self.index_x]
     1786            tupledim = self.index_t,self.index_z,self.index_y,self.index_x
    17891787        else:
    17901788            print "!! ERROR !! field would have more than four dimensions ?" ; exit()
     1789        # then retrieve what is requested by user!
     1790        time0 = timelib.time()
     1791        if self.verbose: print "**** OK. I am getting values from files. Please wait."     
     1792        self.field = self.f.variables[self.var][tupledim]   
    17911793        # dirty hack (AS) ref1_dirty_hack
    17921794        # waiting for more fundamental modifications. case when self.index_y is a bool array.
     
    18971899        # prepare quadratic mean
    18981900        if "qmean" in self.compute: self.field = self.field*self.field
    1899         # now ready to compute [TBD: we would like to have e.g. mean over x,y and min over t ??]
    1900         if self.method_t == "comp":
    1901             if self.verbose: print "**** OK. Computing over t axis."
    1902             if "mean" in self.compute: self.field = ppcompute.mean(self.field,axis=0)
    1903             elif self.compute == "min": self.field = ppcompute.min(self.field,axis=0)
    1904             elif self.compute == "max": self.field = ppcompute.max(self.field,axis=0)
    1905             else: print "!! ERROR !! operation not supported." ; exit()
    1906             nt = 1 ; self.field = np.reshape(self.field,(nt,nz,ny,nx))
    1907         if self.method_z == "comp":
    1908             if self.verbose: print "**** OK. Computing over z axis."
    1909             if "mean" in self.compute: self.field = ppcompute.mean(self.field,axis=1)
    1910             elif self.compute == "min": self.field = ppcompute.min(self.field,axis=1)
    1911             elif self.compute == "max": self.field = ppcompute.max(self.field,axis=1)
    1912             else: print "!! ERROR !! operation not supported." ; exit()
    1913             nz = 1 ; self.field = np.reshape(self.field,(nt,nz,ny,nx))
    1914         if self.method_y == "comp":
    1915             if self.verbose: print "**** OK. Computing over y axis."
    1916             if self.compute == "meanarea": self.field = ppcompute.sum(self.field,axis=2)
    1917             elif "mean" in self.compute: self.field = ppcompute.mean(self.field,axis=2)
    1918             elif self.compute == "min": self.field = ppcompute.min(self.field,axis=2)
    1919             elif self.compute == "max": self.field = ppcompute.max(self.field,axis=2)
    1920             else: print "!! ERROR !! operation not supported." ; exit()
    1921             ny = 1 ; self.field = np.reshape(self.field,(nt,nz,ny,nx))
    1922             if self.field_x.ndim == 2: self.field_x = self.field_x[0,:] # TBD: this is OK for regular grid but not for irregular
    1923         if self.method_x == "comp":
    1924             if self.verbose: print "**** OK. Computing over x axis."
    1925             if self.compute == "meanarea": self.field = ppcompute.sum(self.field,axis=3)
    1926             elif "mean" in self.compute: self.field = ppcompute.mean(self.field,axis=3)
    1927             elif self.compute == "min": self.field = ppcompute.min(self.field,axis=3)
    1928             elif self.compute == "max": self.field = ppcompute.max(self.field,axis=3)
    1929             else: print "!! ERROR !! operation not supported." ; exit()
    1930             nx = 1 ; self.field = np.reshape(self.field,(nt,nz,ny,nx))
    1931             if self.field_y.ndim == 2: self.field_y = self.field_y[:,0] # TBD: this is OK for regular grid but not for irregular
     1901        # prepare and execute (possibly sequential) means
     1902        roll = [self.method_t, self.method_z, self.method_y, self.method_x]
     1903        reshapedim = [nt,nz,ny,nx]
     1904        for nr in range(len(roll)):
     1905          rrr = roll[nr]
     1906          if "comp" in rrr:
     1907            # a. computing
     1908            if self.verbose: print "**** OK. Computing over axis number ",zeaxis
     1909            if self.compute == "meanarea": self.field = ppcompute.sum  (self.field,axis=nr)
     1910            elif "mean" in self.compute:   self.field = ppcompute.mean (self.field,axis=nr)
     1911            elif self.compute == "min":    self.field = ppcompute.min  (self.field,axis=nr)
     1912            elif self.compute == "max":    self.field = ppcompute.max  (self.field,axis=nr)
     1913            else:                          print "!! ERROR !! operation not supported." ; exit()
     1914            # b. reshaping
     1915            reshapedim[nr] = 1
     1916            self.field = np.reshape(self.field,reshapedim)
     1917            # c. particular cases for 2D coordinates
     1918            if nr == 2 and self.field_x.ndim == 2: self.field_x = self.field_x[0,:] # TBD: this is OK for regular grid but not for irregular
     1919            if nr == 3 and self.field_y.ndim == 2: self.field_y = self.field_y[:,0] # TBD: this is OK for regular grid but not for irregular
     1920        # computations for which only setting compute is needed
     1921        if   "_x" in self.compute: zeaxis = 3
     1922        elif "_y" in self.compute: zeaxis = 2
     1923        elif "_z" in self.compute: zeaxis = 1
     1924        elif "_t" in self.compute: zeaxis = 0
     1925        if   "pert" in self.compute:
     1926           self.field = ppcompute.perturbation(self.field,axis=zeaxis)
     1927        elif "diff" in self.compute:
     1928           dadiff = np.diff(self.field,axis=zeaxis)
     1929           # (diff ouput has one value less in impacted dimension. fix this.)
     1930           reshapedim[zeaxis] = reshapedim[zeaxis]-1
     1931           self.field[:,:,:,:] = np.nan
     1932           self.field[0:reshapedim[0],0:reshapedim[1],0:reshapedim[2],0:reshapedim[3]] = dadiff
    19321933        # remove all dimensions with size 1 to prepare plot (and check the resulting dimension with dimplot)
    19331934        self.field = np.squeeze(self.field)
  • trunk/UTIL/PYTHON/planetoplot_v2/ppcompute.py

    r1007 r1219  
    3434## compute min
    3535## author AS + AC
    36 def min (field,axis=None):
     36def min(field,axis=None):
    3737        if field is None: return None
    3838        if type(field).__name__=='MaskedArray':
     
    4545## compute max
    4646## author AS + AC
    47 def max (field,axis=None):
     47def max(field,axis=None):
    4848        if field is None: return None
    4949        if type(field).__name__=='MaskedArray':
     
    5656## compute mean
    5757## author AS + AC
    58 def mean (field,axis=None):
     58def mean(field,axis=None):
    5959        if field is None: return None
    6060        else:
     
    7777## compute sum
    7878## author AS + AC
    79 def sum (field,axis=None):
     79def sum(field,axis=None):
    8080        if field is None: return None
    8181        else:
     
    115115    meanvalues = np.array(meanvalues)
    116116    return meanvalues
     117
     118## compute perturbation to mean
     119## -- the whole dimension must exist!
     120## author AS
     121def perturbation(field,axis=None):
     122    # calculate mean (averaged dim is reduced)
     123    mm = mean(field,axis=axis)
     124    # include back the reduced dim
     125    if field.ndim == 4: mm = np.tile(mm,(field.shape[axis],1,1,1))
     126    elif field.ndim == 3: mm = np.tile(mm,(field.shape[axis],1,1))
     127    elif field.ndim == 2: mm = np.tile(mm,(field.shape[axis],1))
     128    # array has right shape but not in good order: fix this.
     129    mm = np.reshape(mm,field.shape)
     130    # compute perturbations
     131    field = field - mm
     132    return field
    117133
    118134################
Note: See TracChangeset for help on using the changeset viewer.