- Timestamp:
- Apr 6, 2014, 8:19:31 AM (11 years ago)
- Location:
- trunk/UTIL/PYTHON/planetoplot_v2
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UTIL/PYTHON/planetoplot_v2/ppclass.py
r1215 r1219 871 871 # generic 1D/2D: load field and coord in plot object 872 872 plobj.f = obj.field # field to be plotted 873 plobj.x = obj.absc 874 plobj.y = obj.ordi 875 873 plobj.x = obj.absc # abscissa (or longitude) 874 plobj.y = obj.ordi # ordinate (or latitude) 875 # -- useless in 1D but not used anyway 876 876 # specific 1D plot stuff 877 877 if dp == 1: … … 1689 1689 self.index_x = yeah[0,:] 1690 1690 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]) 1692 1692 self.index_y = yeah[:,0] 1693 1693 self.index_x2d = self.index_x … … 1756 1756 elif self.dimplot == 1 and self.verbose: print "**** OK. 1D plot requested." 1757 1757 elif self.verbose: print "**** OK. 2D section requested." 1758 # well, now get field from netcdf file 1758 # well, now get field from netcdf file 1759 1759 # part below is necessary otherwise there is an index error below 1760 1760 if self.index_x.size == 1: self.index_x = self.index_x[0] … … 1762 1762 if self.index_z.size == 1: self.index_z = self.index_z[0] 1763 1763 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. 1768 1766 if self.dim == 1: 1769 1767 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 1771 1769 elif self.dim == 2: 1772 1770 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 1774 1772 elif self.dim == 3: 1775 1773 ## DEFAULT tyx (time-varying 2D field) 1776 1774 if self.kind3d == "tyx": 1777 1775 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 1779 1777 ## CASE tzy (e.g. time-varying zonal mean y-z field) 1780 1778 elif self.kind3d == "tzy": 1781 1779 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 1783 1781 else: 1784 1782 print "!! ERROR !! This kind of 3D field is not supported. Please send feedback." ; exit() … … 1786 1784 elif self.dim == 4: 1787 1785 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 1789 1787 else: 1790 1788 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] 1791 1793 # dirty hack (AS) ref1_dirty_hack 1792 1794 # waiting for more fundamental modifications. case when self.index_y is a bool array. … … 1897 1899 # prepare quadratic mean 1898 1900 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 1932 1933 # remove all dimensions with size 1 to prepare plot (and check the resulting dimension with dimplot) 1933 1934 self.field = np.squeeze(self.field) -
trunk/UTIL/PYTHON/planetoplot_v2/ppcompute.py
r1007 r1219 34 34 ## compute min 35 35 ## author AS + AC 36 def min 36 def min(field,axis=None): 37 37 if field is None: return None 38 38 if type(field).__name__=='MaskedArray': … … 45 45 ## compute max 46 46 ## author AS + AC 47 def max 47 def max(field,axis=None): 48 48 if field is None: return None 49 49 if type(field).__name__=='MaskedArray': … … 56 56 ## compute mean 57 57 ## author AS + AC 58 def mean 58 def mean(field,axis=None): 59 59 if field is None: return None 60 60 else: … … 77 77 ## compute sum 78 78 ## author AS + AC 79 def sum 79 def sum(field,axis=None): 80 80 if field is None: return None 81 81 else: … … 115 115 meanvalues = np.array(meanvalues) 116 116 return meanvalues 117 118 ## compute perturbation to mean 119 ## -- the whole dimension must exist! 120 ## author AS 121 def 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 117 133 118 134 ################
Note: See TracChangeset
for help on using the changeset viewer.