def __init__(self,stat,components,components_names,time_domain,time_domain_names,id=None): # Set the id if id is None: self.id="" else: self.id=id # Check components if not isinstance(components,(list,tuple)): raise StatisticError,"components argument must be a list" else: if isinstance(components_names[0],str): self.components={} for i in range(len(components)): self.components[components[i]]=components_names[i] elif isinstance(components_names,dict): self.components=components else: raise StatisticError,"components_names argument must be a list" # Check time_domain if not isinstance(time_domain,(list,tuple)): raise StatisticError,"time_domain argument must be a list" else: if isinstance(time_domain_names[0],str): self.time_domain={} for i in range(len(time_domain)): self.time_domain[time_domain[i]]=time_domain_names[i] elif isinstance(time_domain_names,dict): self.time_domain=time_domain else: raise StatisticError,"time_domain_names argument must be a list" # check stat array if isinstance (stat,numpy.ndarray ) or numpy.ma.isMA(stat): s=stat.shape if len(s)!=2: raise StatisticError,"stat argument must be 2D" nc=len(components) nt=len(time_domain) if nc!=s[0]: raise StatisticError,"You claim "+str(nc)+" components but your stat shows:"+str(s[0]) if nt!=s[1]: raise StatisticError,"You claim "+str(nt)+" time_domain but your stat shows:"+str(s[1]) self.stat=cdms2.createVariable(stat,copy=0,id=str(self.id)) autobounds=cdms2.getAutoBounds() cdms2.setAutoBounds('off') compaxis=cdms2.createAxis(components) compaxis.id='component' timaxis=cdms2.createAxis(time_domain) timaxis.id='time_domain' self.stat.setAxis(0,compaxis) self.stat.setAxis(1,timaxis) self.stat.components=repr(self.components) self.stat.time_domain=repr(self.time_domain) cdms2.setAutoBounds(autobounds) else: raise StatisticError,"stat argument must be A numpy array a MA or a MV2"
def write(self,file,mode='w'): autobounds=cdms2.getAutoBounds() cdms2.setAutoBounds('off') if isinstance(file,str): f=cdms2.open(file,mode) elif isinstance(file,cdms2.dataset.CdmsFile): f=file else: raise StatisticError,"write arguments expects string or cdms2 file" f.write(self.stat) if not isinstance(file,cdms2.dataset.CdmsFile): f.close() cdms2.setAutoBounds(autobounds)
def create_metrics(ref, test, ref_regrid, test_regrid, diff): """Creates the mean, max, min, rmse, corr in a dictionary""" orig_bounds = cdms2.getAutoBounds() cdms2.setAutoBounds(1) lev = ref.getLevel() if lev is not None: lev.setBounds(None) lev = test.getLevel() if lev is not None: lev.setBounds(None) lev = test_regrid.getLevel() if lev is not None: lev.setBounds(None) lev = ref_regrid.getLevel() if lev is not None: lev.setBounds(None) lev = diff.getLevel() if lev is not None: lev.setBounds(None) cdms2.setAutoBounds(orig_bounds) metrics_dict = {} metrics_dict['ref'] = { 'min': min_cdms(ref), 'max': max_cdms(ref), #'mean': numpy.nan #mean(ref, axis='yz') 'mean': mean(ref, axis='yz') } metrics_dict['test'] = { 'min': min_cdms(test), 'max': max_cdms(test), #'mean': numpy.nan #mean(test, axis='yz') 'mean': mean(test, axis='yz') } metrics_dict['diff'] = { 'min': min_cdms(diff), 'max': max_cdms(diff), #'mean': numpy.nan #mean(diff, axis='yz') 'mean': mean(diff, axis='yz') } metrics_dict['misc'] = { 'rmse': rmse(test_regrid, ref_regrid, axis='yz'), 'corr': corr(test_regrid, ref_regrid, axis='yz') } return metrics_dict
def getAxisList(self): values = [] axes = [] for a in self.json_struct: values.append(set()) self.get_axes_values_recursive( 0, len(self.json_struct) - 1, self.data, values) autoBounds = cdms2.getAutoBounds() cdms2.setAutoBounds("off") for i, nm in enumerate(self.json_struct): axes.append(cdms2.createAxis(sorted(list(values[i])), id=nm)) self.axes = axes cdms2.setAutoBounds(autoBounds) return self.axes
def create_metrics(ref, test, ref_regrid, test_regrid, diff): """ Creates the mean, max, min, rmse, corr in a dictionary. """ orig_bounds = cdms2.getAutoBounds() cdms2.setAutoBounds(1) lev = ref.getLevel() if lev is not None: lev.setBounds(None) lev = test.getLevel() if lev is not None: lev.setBounds(None) lev = test_regrid.getLevel() if lev is not None: lev.setBounds(None) lev = ref_regrid.getLevel() if lev is not None: lev.setBounds(None) lev = diff.getLevel() if lev is not None: lev.setBounds(None) cdms2.setAutoBounds(orig_bounds) metrics_dict = {} metrics_dict["ref"] = { "min": min_cdms(ref), "max": max_cdms(ref), "mean": mean(ref, axis="xz"), } metrics_dict["test"] = { "min": min_cdms(test), "max": max_cdms(test), "mean": mean(test, axis="xz"), } metrics_dict["diff"] = { "min": min_cdms(diff), "max": max_cdms(diff), "mean": mean(diff, axis="xz"), } metrics_dict["misc"] = { "rmse": rmse(test_regrid, ref_regrid, axis="xz"), "corr": corr(test_regrid, ref_regrid, axis="xz"), } return metrics_dict
def linearInterpolation(A,I,levels=[100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000], status=None): """ Linear interpolation to interpolate a field from some levels to another set of levels Value below "surface" are masked Input A : array to interpolate I : interpolation field (usually Pressure or depth) from TOP (level 0) to BOTTOM (last level), i.e P value going up with each level levels : levels to interplate to (same units as I), default levels are:[100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000] I and levels must have same units Output array on new levels (levels) Examples: A=interpolate(A,I,levels=[100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000]) """ try: nlev=len(levels) # Number of pressure levels except: nlev=1 # if only one level len(levels) would breaks levels=[levels,] order=A.getOrder() A=A(order='z...') I=I(order='z...') sh=list(I.shape) nsigma=sh[0] #number of sigma levels sh[0]=nlev t=MV2.zeros(sh,typecode=MV2.float32) sh2=I[0].shape prev=-1 for ilev in range(nlev): # loop through pressure levels if status is not None: prev=genutil.statusbar(ilev,nlev-1.,prev) lev=levels[ilev] # get value for the level Iabv=MV2.ones(sh2,MV2.float) Aabv=-1*Iabv # Array on sigma level Above Abel=-1*Iabv # Array on sigma level Below Ibel=-1*Iabv # Pressure on sigma level Below Iabv=-1*Iabv # Pressure on sigma level Above Ieq=MV2.masked_equal(Iabv,-1) # Area where Pressure == levels for i in range(1,nsigma): # loop from second sigma level to last one a = MV2.greater_equal(I[i], lev) # Where is the pressure greater than lev b = MV2.less_equal(I[i-1],lev) # Where is the pressure less than lev # Now looks if the pressure level is in between the 2 sigma levels # If yes, sets Iabv, Ibel and Aabv, Abel a=MV2.logical_and(a,b) Iabv=MV2.where(a,I[i],Iabv) # Pressure on sigma level Above Aabv=MV2.where(a,A[i],Aabv) # Array on sigma level Above Ibel=MV2.where(a,I[i-1],Ibel) # Pressure on sigma level Below Abel=MV2.where(a,A[i-1],Abel) # Array on sigma level Below Ieq= MV2.where(MV2.equal(I[i],lev),A[i],Ieq) val=MV2.masked_where(MV2.equal(Ibel,-1.),numpy.ones(Ibel.shape)*lev) # set to missing value if no data below lev if there is tl=(val-Ibel)/(Iabv-Ibel)*(Aabv-Abel)+Abel # Interpolation if ((Ieq.mask is None) or (Ieq.mask is MV22.nomask)): tl=Ieq else: tl=MV2.where(1-Ieq.mask,Ieq,tl) t[ilev]=tl.astype(MV2.float32) ax=A.getAxisList() autobnds=cdms2.getAutoBounds() cdms2.setAutoBounds('off') lvl=cdms2.createAxis(MV2.array(levels).filled()) cdms2.setAutoBounds(autobnds) try: lvl.units=I.units except: pass lvl.id='plev' try: t.units=I.units except: pass ax[0]=lvl t.setAxisList(ax) t.id=A.id for att in A.listattributes(): setattr(t,att,getattr(A,att)) return t(order=order)
def logLinearInterpolation(A,P,levels=[100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000],status=None): """ Log-linear interpolation to convert a field from sigma levels to pressure levels Value below surface are masked Input A : array on sigma levels P : pressure field from TOP (level 0) to BOTTOM (last level) levels : pressure levels to interplate to (same units as P), default levels are:[100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000] P and levels must have same units Output array on pressure levels (levels) Examples: A=logLinearInterpolation(A,P),levels=[100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000]) """ try: nlev=len(levels) # Number of pressure levels except: nlev=1 # if only one level len(levels) would breaks levels=[levels,] order=A.getOrder() A=A(order='z...') P=P(order='z...') sh=list(P.shape) nsigma=sh[0] #number of sigma levels sh[0]=nlev t=MV2.zeros(sh,typecode=MV2.float32) sh2=P[0].shape prev=-1 for ilev in range(nlev): # loop through pressure levels if status is not None: prev=genutil.statusbar(ilev,nlev-1.,prev) lev=levels[ilev] # get value for the level Pabv=MV2.ones(sh2,MV2.float) Aabv=-1*Pabv # Array on sigma level Above Abel=-1*Pabv # Array on sigma level Below Pbel=-1*Pabv # Pressure on sigma level Below Pabv=-1*Pabv # Pressure on sigma level Above Peq=MV2.masked_equal(Pabv,-1) # Area where Pressure == levels for i in range(1,nsigma): # loop from second sigma level to last one a=MV2.greater_equal(P[i], lev) # Where is the pressure greater than lev b= MV2.less_equal(P[i-1],lev) # Where is the pressure less than lev # Now looks if the pressure level is in between the 2 sigma levels # If yes, sets Pabv, Pbel and Aabv, Abel a=MV2.logical_and(a,b) Pabv=MV2.where(a,P[i],Pabv) # Pressure on sigma level Above Aabv=MV2.where(a,A[i],Aabv) # Array on sigma level Above Pbel=MV2.where(a,P[i-1],Pbel) # Pressure on sigma level Below Abel=MV2.where(a,A[i-1],Abel) # Array on sigma level Below Peq= MV2.where(MV2.equal(P[i],lev),A[i],Peq) val=MV2.masked_where(MV2.equal(Pbel,-1),numpy.ones(Pbel.shape)*lev) # set to missing value if no data below lev if there is tl=MV2.log(val/Pbel)/MV2.log(Pabv/Pbel)*(Aabv-Abel)+Abel # Interpolation if ((Peq.mask is None) or (Peq.mask is MV2.nomask)): tl=Peq else: tl=MV2.where(1-Peq.mask,Peq,tl) t[ilev]=tl.astype(MV2.float32) ax=A.getAxisList() autobnds=cdms2.getAutoBounds() cdms2.setAutoBounds('off') lvl=cdms2.createAxis(MV2.array(levels).filled()) cdms2.setAutoBounds(autobnds) try: lvl.units=P.units except: pass lvl.id='plev' try: t.units=P.units except: pass ax[0]=lvl t.setAxisList(ax) t.id=A.id for att in A.listattributes(): setattr(t,att,getattr(A,att)) return t(order=order)
variables[41:45] ): #84 = tas, #41 = pr, 113 = tos 17 = evs (need to test mrro 36) varb = variables[variables.index(i)].split('/')[5] print Vcount, varb if varb in MissingVariables or varb in ['tos' ]: ## NOTE THE TOS!!!! #### print 'Skipping variable b/c no matching varb in AMON table' continue #%% INPUT JSON FILE CREATION WITH LOOP #create subdirectory path for variable pathin = pathin1 + varb #check if autobounds is on, off, or on grid mode print cdm.getAutoBounds() # creates sub direc in users save path to store the large amount of cmor_input json files that will be created subdir = 'input_json_files' if not os.path.exists(savepath + subdir): os.makedirs(savepath + subdir) # get the aliases for the data that is to be re-formatted lst = os.listdir(pathin) lst.sort() # Sort alphabetically print 'Starting' #%% print ' ' print 'CREATING USER INPUT JSON'
def linearInterpolation(A, I, levels=[ 100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000 ], status=None): """ Linear interpolation to interpolate a field from some levels to another set of levels Value below "surface" are masked Input A : array to interpolate I : interpolation field (usually Pressure or depth) from TOP (level 0) to BOTTOM (last level), i.e P value going up with each level levels : levels to interplate to (same units as I), default levels are:[100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000] I and levels must have same units Output array on new levels (levels) Examples: A=interpolate(A,I,levels=[100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000]) """ try: nlev = len(levels) # Number of pressure levels except: nlev = 1 # if only one level len(levels) would breaks levels = [ levels, ] order = A.getOrder() A = A(order='z...') I = I(order='z...') sh = list(I.shape) nsigma = sh[0] #number of sigma levels sh[0] = nlev t = MV2.zeros(sh, typecode=MV2.float32) sh2 = I[0].shape prev = -1 for ilev in range(nlev): # loop through pressure levels if status is not None: prev = genutil.statusbar(ilev, nlev - 1., prev) lev = levels[ilev] # get value for the level Iabv = MV2.ones(sh2, MV2.float) Aabv = -1 * Iabv # Array on sigma level Above Abel = -1 * Iabv # Array on sigma level Below Ibel = -1 * Iabv # Pressure on sigma level Below Iabv = -1 * Iabv # Pressure on sigma level Above Ieq = MV2.masked_equal(Iabv, -1) # Area where Pressure == levels for i in range(1, nsigma): # loop from second sigma level to last one a = MV2.greater_equal( I[i], lev) # Where is the pressure greater than lev b = MV2.less_equal(I[i - 1], lev) # Where is the pressure less than lev # Now looks if the pressure level is in between the 2 sigma levels # If yes, sets Iabv, Ibel and Aabv, Abel a = MV2.logical_and(a, b) Iabv = MV2.where(a, I[i], Iabv) # Pressure on sigma level Above Aabv = MV2.where(a, A[i], Aabv) # Array on sigma level Above Ibel = MV2.where(a, I[i - 1], Ibel) # Pressure on sigma level Below Abel = MV2.where(a, A[i - 1], Abel) # Array on sigma level Below Ieq = MV2.where(MV2.equal(I[i], lev), A[i], Ieq) val = MV2.masked_where( MV2.equal(Ibel, -1.), numpy.ones(Ibel.shape) * lev) # set to missing value if no data below lev if there is tl = (val - Ibel) / (Iabv - Ibel) * (Aabv - Abel) + Abel # Interpolation if ((Ieq.mask is None) or (Ieq.mask is MV22.nomask)): tl = Ieq else: tl = MV2.where(1 - Ieq.mask, Ieq, tl) t[ilev] = tl.astype(MV2.float32) ax = A.getAxisList() autobnds = cdms2.getAutoBounds() cdms2.setAutoBounds('off') lvl = cdms2.createAxis(MV2.array(levels).filled()) cdms2.setAutoBounds(autobnds) try: lvl.units = I.units except: pass lvl.id = 'plev' try: t.units = I.units except: pass ax[0] = lvl t.setAxisList(ax) t.id = A.id for att in A.listattributes(): setattr(t, att, getattr(A, att)) return t(order=order)
def logLinearInterpolation(A, P, levels=[ 100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000 ], status=None): """ Log-linear interpolation to convert a field from sigma levels to pressure levels Value below surface are masked Input A : array on sigma levels P : pressure field from TOP (level 0) to BOTTOM (last level) levels : pressure levels to interplate to (same units as P), default levels are:[100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000] P and levels must have same units Output array on pressure levels (levels) Examples: A=logLinearInterpolation(A,P),levels=[100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000]) """ try: nlev = len(levels) # Number of pressure levels except: nlev = 1 # if only one level len(levels) would breaks levels = [ levels, ] order = A.getOrder() A = A(order='z...') P = P(order='z...') sh = list(P.shape) nsigma = sh[0] #number of sigma levels sh[0] = nlev t = MV2.zeros(sh, typecode=MV2.float32) sh2 = P[0].shape prev = -1 for ilev in range(nlev): # loop through pressure levels if status is not None: prev = genutil.statusbar(ilev, nlev - 1., prev) lev = levels[ilev] # get value for the level Pabv = MV2.ones(sh2, MV2.float) Aabv = -1 * Pabv # Array on sigma level Above Abel = -1 * Pabv # Array on sigma level Below Pbel = -1 * Pabv # Pressure on sigma level Below Pabv = -1 * Pabv # Pressure on sigma level Above Peq = MV2.masked_equal(Pabv, -1) # Area where Pressure == levels for i in range(1, nsigma): # loop from second sigma level to last one a = MV2.greater_equal( P[i], lev) # Where is the pressure greater than lev b = MV2.less_equal(P[i - 1], lev) # Where is the pressure less than lev # Now looks if the pressure level is in between the 2 sigma levels # If yes, sets Pabv, Pbel and Aabv, Abel a = MV2.logical_and(a, b) Pabv = MV2.where(a, P[i], Pabv) # Pressure on sigma level Above Aabv = MV2.where(a, A[i], Aabv) # Array on sigma level Above Pbel = MV2.where(a, P[i - 1], Pbel) # Pressure on sigma level Below Abel = MV2.where(a, A[i - 1], Abel) # Array on sigma level Below Peq = MV2.where(MV2.equal(P[i], lev), A[i], Peq) val = MV2.masked_where( MV2.equal(Pbel, -1), numpy.ones(Pbel.shape) * lev) # set to missing value if no data below lev if there is tl = MV2.log(val / Pbel) / MV2.log( Pabv / Pbel) * (Aabv - Abel) + Abel # Interpolation if ((Peq.mask is None) or (Peq.mask is MV2.nomask)): tl = Peq else: tl = MV2.where(1 - Peq.mask, Peq, tl) t[ilev] = tl.astype(MV2.float32) ax = A.getAxisList() autobnds = cdms2.getAutoBounds() cdms2.setAutoBounds('off') lvl = cdms2.createAxis(MV2.array(levels).filled()) cdms2.setAutoBounds(autobnds) try: lvl.units = P.units except: pass lvl.id = 'plev' try: t.units = P.units except: pass ax[0] = lvl t.setAxisList(ax) t.id = A.id for att in A.listattributes(): setattr(t, att, getattr(A, att)) return t(order=order)
def plot(self,data,data2,template = None, bg=0, x=None): if x is None: x = self.x if template is None: template = self.template elif isinstance(template,str): template = x.gettemplate(template) elif not vcs.istemplate(template): raise "Error did not know what to do with template: %s" % template if not isinstance(data,cdms2.tvariable.TransientVariable): mode= cdms2.getAutoBounds() cdms2.setAutoBounds("on") data = MV2.array(data) data.getAxis(-1).getBounds() cdms2.setAutoBounds(mode) while data.rank()>1: data = data[0] while data2.rank()>1: data2 = data2[0] # ok now we have a good x and a good data npts1 = len(data) npts2 = len(data2) # create the primitive fill = x.createfillarea() line = x.createline() fill.viewport = [template.data.x1,template.data.x2,template.data.y1,template.data.y2] line.viewport = [template.data.x1,template.data.x2,template.data.y1,template.data.y2] ax = data.getAxis(0)[:] ax2 = data.getAxis(0)[:] xmn,xmx = vcs.minmax(ax,ax2) ymn,ymx = vcs.minmax(data,data2) xmn,xmx,ymn,ymx = self.prep_plot(xmn,xmx,ymn,ymx) fill.worldcoordinate=[xmn,xmx,ymn,ymx] line.worldcoordinate=[xmn,xmx,ymn,ymx] fill.style = [self.fillareastyle,] fill.color = [self.fillareacolor,] fill.index = [self.fillareaindex,] line.type = [self.line,] line.width = [self.linewidth,] line.color = [self.linecolor,] xs = [] ys = [] xs = numpy.concatenate((ax[:],ax2[::-1])).tolist() ys = numpy.concatenate((data[:],data2[::-1])).tolist() xs.append(xs[0]) ys.append(ys[0]) fill.x = xs fill.y = ys line.x = xs line.y = ys displays = [] displays.append(x.plot(fill,bg=bg)) displays.append(x.plot(line,bg=bg)) x.worldcoordinate = fill.worldcoordinate dsp = template.plot(data,self,bg=bg) for d in dsp: displays.append(d) self.restore() return displays
def linearInterpolation(A, Idx, levels=[ 100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000 ], status=None, axis='z'): """ Linear interpolation to interpolate a field from some levels to another set of levels Values below "surface" are masked. :param A: array to interpolate :type A: :param I: interpolation field (usually Pressure or depth) from TOP (level 0) to BOTTOM (last level) i.e P value going up with each level. :type I: :param levels: levels to interpolate to (same units as I). Default levels:[100000, 92500, 85000, 70000, 60000, 50000, 40000, 30000, 25000, 20000, 15000, 10000, 7000, 5000, 3000, 2000, 1000] :type levels: :param axis: Axis over which to do the linear interpolation. Can provide either an int representing axis index, or the axis name. Default: 'z'. :type axis: str or int .. note:: I and levels must have same units :returns: array on new levels (levels) :Examples: .. doctest:: vertical_linearInterpolation >>> A=interpolate(A,I) # interpolates A over default levels """ try: nlev = len(levels) # Number of pressure levels except BaseException: nlev = 1 # if only one level len(levels) would breaks levels = [ levels, ] order = A.getOrder() A = A(order='%s...' % axis) Idx = Idx(order='%s...' % axis) sh = list(Idx.shape) nsigma = sh[0] # number of sigma levels sh[0] = nlev t = MV2.zeros(sh, typecode=MV2.float32) sh2 = Idx[0].shape prev = -1 for ilev in range(nlev): # loop through pressure levels if status is not None: prev = genutil.statusbar(ilev, nlev - 1., prev) lev = levels[ilev] # get value for the level Iabv = MV2.ones(sh2, MV2.float) Aabv = -1 * Iabv # Array on sigma level Above Abel = -1 * Iabv # Array on sigma level Below Ibel = -1 * Iabv # Pressure on sigma level Below Iabv = -1 * Iabv # Pressure on sigma level Above Ieq = MV2.masked_equal(Iabv, -1) # Area where Pressure == levels for i in range(1, nsigma): # loop from second sigma level to last one a = MV2.greater_equal( Idx[i], lev) # Where is the pressure greater than lev b = MV2.less_equal(Idx[i - 1], lev) # Where is the pressure less than lev # Now looks if the pressure level is in between the 2 sigma levels # If yes, sets Iabv, Ibel and Aabv, Abel a = MV2.logical_and(a, b) Iabv = MV2.where(a, Idx[i], Iabv) # Pressure on sigma level Above Aabv = MV2.where(a, A[i], Aabv) # Array on sigma level Above Ibel = MV2.where(a, Idx[i - 1], Ibel) # Pressure on sigma level Below Abel = MV2.where(a, A[i - 1], Abel) # Array on sigma level Below Ieq = MV2.where(MV2.equal(Idx[i], lev), A[i], Ieq) val = MV2.masked_where(MV2.equal(Ibel, -1.), numpy.ones(Ibel.shape) * lev) # set to missing value if no data below lev if # there is tl = (val - Ibel) / (Iabv - Ibel) * \ (Aabv - Abel) + Abel # Interpolation if ((Ieq.mask is None) or (Ieq.mask is MV2.nomask)): tl = Ieq else: tl = MV2.where(1 - Ieq.mask, Ieq, tl) t[ilev] = tl.astype(MV2.float32) ax = A.getAxisList() autobnds = cdms2.getAutoBounds() cdms2.setAutoBounds('off') lvl = cdms2.createAxis(MV2.array(levels).filled()) cdms2.setAutoBounds(autobnds) try: lvl.units = Idx.units except BaseException: pass lvl.id = 'plev' try: t.units = Idx.units except BaseException: pass ax[0] = lvl t.setAxisList(ax) t.id = A.id for att in A.listattributes(): setattr(t, att, getattr(A, att)) return t(order=order)
def plot(self,data,template = None, bg=0, x=None): if x is None: x = self.x if template is None: template = self.template elif isinstance(template,str): template = x.gettemplate(template) elif not vcs.istemplate(template): raise "Error did not know what to do with template: %s" % template if not isinstance(data,cdms2.tvariable.TransientVariable): mode= cdms2.getAutoBounds() cdms2.setAutoBounds("on") data = MV2.array(data) data.getAxis(-1).getBounds() cdms2.setAutoBounds(mode) while data.rank()>1: data = data[0] # ok now we have a good x and a good data nbars = len(data) # create the primitive fill = x.createfillarea() line = x.createline() fill.viewport = [template.data.x1,template.data.x2,template.data.y1,template.data.y2] line.viewport = [template.data.x1,template.data.x2,template.data.y1,template.data.y2] axb = data.getAxis(0).getBounds() xmn,xmx = vcs.minmax(axb) ymn,ymx = vcs.minmax(data) xmn,xmx,ymn,ymx = self.prep_plot(xmn,xmx,ymn,ymx) fill.worldcoordinate=[xmn,xmx,ymn,ymx] line.worldcoordinate=[xmn,xmx,ymn,ymx] styles =[] cols = [] indices = [] lt = [] lw =[] lc = [] xs = [] ys = [] for i in range(nbars): if i < len(self.fillareastyles): styles.append(self.fillareastyles[i]) else: styles.append(self.fillareastyles[-1]) if i < len(self.fillareacolors): cols.append(self.fillareacolors[i]) else: cols.append(self.fillareacolors[-1]) if i < len(self.fillareaindices): indices.append(self.fillareaindices[i]) else: indices.append(self.fillareaindices[-1]) if i < len(self.line): lt.append( self.line[i]) else: lt.append(self.line[-1]) if i < len(self.linewidth): lw.append( self.linewidth[i]) else: lw.append(self.linewidth[-1]) if i < len(self.line): lc.append( self.linecolors[i]) else: lc.append(self.linecolors[-1]) xs.append( [axb[i][0],axb[i][1],axb[i][1],axb[i][0],axb[i][0]]) ys.append( [0,0,data[i],data[i],0]) fill.style = styles fill.x = xs fill.y = ys fill.style fill.index = indices fill.color = cols line.x = xs line.y = ys line.type = lt line.width = lw line.color = lc displays = [] displays.append(x.plot(fill,bg=bg)) displays.append(x.plot(line,bg=bg)) x.worldcoordinate = fill.worldcoordinate dsp = template.plot(data,self,bg=bg) for d in dsp: displays.append(d) self.restore() return displays
def get(self,components=None,time_domain=None): # Check the components if not components is None: if isinstance(components,(int,float,numpy.floating)): components=[components] if isinstance(components,(list,tuple)): k=self.components.keys() for c in components: if not c in k: raise StatisticError,"component:"+str(c)+"is not defined" else: raise StatisticError,"components must be a number or list (or None)" # Check the time components if not time_domain is None: if isinstance(time_domain,(int,float,numpy.floating)): time_domain=[time_domain] if isinstance(time_domain,(list,tuple)): k=self.time_domain.keys() for c in time_domain: if not c in k: raise StatisticError,"component:"+str(c)+"is not defined" else: raise StatisticError,"time_domain must be a number or list (or None)" # Now gets only the wanted components if components is None and time_domain is None: return self.stat() # Now prepare the output array s=self.stat.shape if components is None: nc=s[0] else: nc=len(components) if time_domain is None: nt=s[1] else: nt=len(time_domain) out=MV2.zeros((nc,nt),typecode=numpy.float32) out.id=self.stat.id autobounds=cdms2.getAutoBounds() cdms2.setAutoBounds('on') if components is None:# Retrieve all the comp components_dic=self.components time_domain_dic={} for it in range(nt): time_domain_dic[time_domain[it]]=self.time_domain[time_domain[it]] out[:,it]=self.stat(time_domain=(time_domain[it],time_domain[it],'cc'),squeeze=1) elif time_domain is None:# Retrieve all the time time_domain_dic=self.time_domain for ic in range(nc): components_dic[components[ic]]=self.components[components[ic]] out[ic]=self.stat(component=components[ic],squeeze=1) else: # Retrieve specific comp and time_domain for ic in range(nc): components_dic[components[ic]]=self.components[components[ic]] for it in range(nt): if ic==0 : time_domain_dic[time_domain[it]]=self.time_domain[time_domain[it]] out[ic,it]=self.stat(component=components[ic],time_domain=time_domain[it],squeeze=1) cdms2.setAutoBounds('off') # Create the Axis if components is None: compaxis=self.stat.getAxis(0) else: compaxis=cdms2.createAxis(components) compaxis.id='component' if time_domain is None: timaxis=self.stat.getAxis(1) else: timaxis=cdms2.createAxis(time_domain) timaxis.id='time_domain' # Set the Axes out.setAxis(0,compaxis) out.setAxis(1,timaxis) out.components=repr(components_dic) out.time_domain=repr(time_domain_dic) cdms2.setAutoBounds(autobounds) return out(squeeze=1)
def plot(self, data, data2, template=None, bg=0, x=None): if x is None: x = self.x if template is None: template = self.template elif isinstance(template, str): template = x.gettemplate(template) elif not vcs.istemplate(template): raise "Error did not know what to do with template: %s" % template if not isinstance(data, cdms2.tvariable.TransientVariable): mode = cdms2.getAutoBounds() cdms2.setAutoBounds("on") data = MV2.array(data) data.getAxis(-1).getBounds() cdms2.setAutoBounds(mode) while data.ndim > 1: data = data[0] while data2.ndim > 1: data2 = data2[0] # ok now we have a good x and a good data # create the primitive fill = x.createfillarea() line = x.createline() fill.viewport = [ template.data.x1, template.data.x2, template.data.y1, template.data.y2 ] line.viewport = [ template.data.x1, template.data.x2, template.data.y1, template.data.y2 ] ax = data.getAxis(0)[:] ax2 = data.getAxis(0)[:] xmn, xmx = vcs.minmax(ax, ax2) ymn, ymx = vcs.minmax(data, data2) xmn, xmx, ymn, ymx = self.prep_plot(xmn, xmx, ymn, ymx) fill.worldcoordinate = [xmn, xmx, ymn, ymx] line.worldcoordinate = [xmn, xmx, ymn, ymx] fill.style = [ self.fillareastyle, ] fill.color = [ self.fillareacolor, ] fill.index = [ self.fillareaindex, ] line.type = [ self.linetype, ] line.width = [ self.linewidth, ] line.color = [ self.linecolor, ] xs = [] ys = [] xs = numpy.concatenate((ax[:], ax2[::-1])).tolist() ys = numpy.concatenate((data[:], data2[::-1])).tolist() xs.append(xs[0]) ys.append(ys[0]) fill.x = xs fill.y = ys line.x = xs line.y = ys displays = [] displays.append(x.plot(fill, bg=bg)) displays.append(x.plot(line, bg=bg)) x.worldcoordinate = fill.worldcoordinate dsp = template.plot(x, data, self, bg=bg) for d in dsp: displays.append(d) self.restore() return displays
def __call__(self, merge=[], **kargs): """ Returns the array of values""" # First clean up kargs if "merge" in kargs: merge = kargs["merge"] del(kargs["merge"]) order = None axes_ids = self.getAxisIds() if "order" in kargs: # If it's an actual axis assume that it's what user wants # Otherwise it's an out order keyword if "order" not in axes_ids: order = kargs["order"] del(kargs["order"]) ab = cdms2.getAutoBounds() cdms2.setAutoBounds("off") axes = self.getAxisList() if merge != []: if isinstance(merge[0], str): merge = [merge, ] if merge != []: for merger in merge: for merge_axis_id in merger: if merge_axis_id not in axes_ids: raise RuntimeError( "You requested to merge axis is '{}' which is not valid. Axes: {}".format( merge_axis_id, axes_ids)) sh = [] ids = [] used_ids = [] for a in axes: # Regular axis not a merged one sh.append(len(a)) # store length to construct array shape ids.append(a.id) # store ids used_ids.append(a.id) # first let's see which vars are actually asked for # for now assume all keys means restriction on dims if not isinstance(merge, (list, tuple)): raise RuntimeError( "merge keyword must be a list of dimensions to merge together") if len(merge) > 0 and not isinstance(merge[0], (list, tuple)): merge = [merge, ] for axis_id in kargs: if axis_id not in ids: raise ValueError("Invalid axis '%s'" % axis_id) index = ids.index(axis_id) value = kargs[axis_id] if isinstance(value, basestring): value = [value] if not isinstance(value, (list, tuple, slice)): raise TypeError( "Invalid subsetting type for axis '%s', axes can only be subsetted by string,list or slice" % axis_id) if isinstance(value, slice): axes[index] = axes[index].subAxis( value.start, value.stop, value.step) sh[index] = len(axes[index]) else: # ok it's a list for v in value: if v not in axes[index][:]: raise ValueError( "Unkwown value '%s' for axis '%s'" % (v, axis_id)) axis = cdms2.createAxis(value, id=axes[index].id) axes[index] = axis sh[index] = len(axis) array = numpy.ma.ones(sh, dtype=numpy.float) # Now let's fill this array self.get_array_values_from_dict_recursive(array, [], [], [], axes) # Ok at this point we need to take care of merged axes # First let's create the merged axes axes_to_group = [] for merger in merge: merged_axes = [] for axid in merger: for ax in axes: if ax.id == axid: merged_axes.append(ax) axes_to_group.append(merged_axes) new_axes = [groupAxes(grp_axes) for grp_axes in axes_to_group] sh2 = list(sh) for merger in merge: for merger in merge: # loop through all possible merging merged_indices = [] for id in merger: merged_indices.append(axes_ids.index(id)) for indx in merged_indices: sh2[indx] = 1 smallest = min(merged_indices) for indx in merged_indices: sh2[smallest] *= sh[indx] myorder = [] for index in range(len(sh)): if index in myorder: continue for merger in merge: merger = [axes_ids.index(x) for x in merger] if index in merger and index not in myorder: for indx in merger: myorder.append(indx) if index not in myorder: # ok did not find this one anywhere myorder.append(index) outData = numpy.transpose(array, myorder) outData = numpy.reshape(outData, sh2) yank = [] for merger in merge: merger = [axes_ids.index(x) for x in merger] mn = min(merger) merger.remove(mn) yank += merger yank = sorted(yank, reverse=True) for yk in yank: extract = (slice(0, None),) * yk extract += (0,) outData = outData[extract] # Ok now let's apply the newaxes sub = 0 outData = MV2.array(outData) merged_axis_done = [] for index in range(len(array.shape)): foundInMerge = False for imerge, merger in enumerate(merge): merger = [axes_ids.index(x) for x in merger] if index in merger: foundInMerge = True if imerge not in merged_axis_done: merged_axis_done.append(imerge) setMergedAxis = imerge else: setMergedAxis = -1 if not foundInMerge: outData.setAxis(index - sub, axes[index]) else: if setMergedAxis == -1: sub += 1 else: outData.setAxis(index - sub, new_axes[setMergedAxis]) outData = MV2.masked_greater(outData, 9.98e20) outData.id = "pmp" if order is not None: myorder = "".join(["({})".format(nm) for nm in order]) outData = outData(order=myorder) # Merge needs cleaning for extra dims crated if merge != []: for i in range(outData.ndim): outData = scrap(outData, axis=i) outData = MV2.masked_greater(outData, 9.9e19) cdms2.setAutoBounds(ab) return outData
def __call__(self, merge=[], **kargs): """ Returns the array of values""" # First clean up kargs if "merge" in kargs: merge = kargs["merge"] del (kargs["merge"]) order = None axes_ids = self.getAxisIds() if "order" in kargs: # If it's an actual axis assume that it's what user wants # Otherwise it's an out order keyword if "order" not in axes_ids: order = kargs["order"] del (kargs["order"]) ab = cdms2.getAutoBounds() cdms2.setAutoBounds("off") axes = self.getAxisList() if merge != []: if isinstance(merge[0], str): merge = [ merge, ] if merge != []: for merger in merge: for merge_axis_id in merger: if merge_axis_id not in axes_ids: raise RuntimeError( "You requested to merge axis is '{}' which is not valid. Axes: {}" .format(merge_axis_id, axes_ids)) sh = [] ids = [] used_ids = [] for a in axes: # Regular axis not a merged one sh.append(len(a)) # store length to construct array shape ids.append(a.id) # store ids used_ids.append(a.id) # first let's see which vars are actually asked for # for now assume all keys means restriction on dims if not isinstance(merge, (list, tuple)): raise RuntimeError( "merge keyword must be a list of dimensions to merge together") if len(merge) > 0 and not isinstance(merge[0], (list, tuple)): merge = [ merge, ] for axis_id in kargs: if axis_id not in ids: raise ValueError("Invalid axis '%s'" % axis_id) index = ids.index(axis_id) value = kargs[axis_id] if isinstance(value, basestring): value = [value] if not isinstance(value, (list, tuple, slice)): raise TypeError( "Invalid subsetting type for axis '%s', axes can only be subsetted by string,list or slice" % axis_id) if isinstance(value, slice): axes[index] = axes[index].subAxis(value.start, value.stop, value.step) sh[index] = len(axes[index]) else: # ok it's a list for v in value: if v not in axes[index][:]: raise ValueError("Unkwown value '%s' for axis '%s'" % (v, axis_id)) axis = cdms2.createAxis(value, id=axes[index].id) axes[index] = axis sh[index] = len(axis) array = numpy.ma.ones(sh, dtype=numpy.float) # Now let's fill this array self.get_array_values_from_dict_recursive(array, [], [], [], axes) # Ok at this point we need to take care of merged axes # First let's create the merged axes axes_to_group = [] for merger in merge: merged_axes = [] for axid in merger: for ax in axes: if ax.id == axid: merged_axes.append(ax) axes_to_group.append(merged_axes) new_axes = [groupAxes(grp_axes) for grp_axes in axes_to_group] sh2 = list(sh) for merger in merge: for merger in merge: # loop through all possible merging merged_indices = [] for id in merger: merged_indices.append(axes_ids.index(id)) for indx in merged_indices: sh2[indx] = 1 smallest = min(merged_indices) for indx in merged_indices: sh2[smallest] *= sh[indx] myorder = [] for index in range(len(sh)): if index in myorder: continue for merger in merge: merger = [axes_ids.index(x) for x in merger] if index in merger and index not in myorder: for indx in merger: myorder.append(indx) if index not in myorder: # ok did not find this one anywhere myorder.append(index) outData = numpy.transpose(array, myorder) outData = numpy.reshape(outData, sh2) yank = [] for merger in merge: merger = [axes_ids.index(x) for x in merger] mn = min(merger) merger.remove(mn) yank += merger yank = sorted(yank, reverse=True) for yk in yank: extract = (slice(0, None), ) * yk extract += (0, ) outData = outData[extract] # Ok now let's apply the newaxes sub = 0 outData = MV2.array(outData) merged_axis_done = [] for index in range(len(array.shape)): foundInMerge = False for imerge, merger in enumerate(merge): merger = [axes_ids.index(x) for x in merger] if index in merger: foundInMerge = True if imerge not in merged_axis_done: merged_axis_done.append(imerge) setMergedAxis = imerge else: setMergedAxis = -1 if not foundInMerge: outData.setAxis(index - sub, axes[index]) else: if setMergedAxis == -1: sub += 1 else: outData.setAxis(index - sub, new_axes[setMergedAxis]) outData = MV2.masked_greater(outData, 9.98e20) outData.id = "pmp" if order is not None: myorder = "".join(["({})".format(nm) for nm in order]) outData = outData(order=myorder) # Merge needs cleaning for extra dims crated if merge != []: for i in range(outData.ndim): outData = scrap(outData, axis=i) outData = MV2.masked_greater(outData, 9.9e19) cdms2.setAutoBounds(ab) return outData
def plot(self, data, template=None, bg=0, x=None): if x is None: x = self.x if template is None: template = self.template elif isinstance(template, str): template = x.gettemplate(template) elif not vcs.istemplate(template): raise "Error did not know what to do with template: %s" % template if not isinstance(data, cdms2.tvariable.TransientVariable): mode = cdms2.getAutoBounds() cdms2.setAutoBounds("on") data = MV2.array(data) data.getAxis(-1).getBounds() cdms2.setAutoBounds(mode) while data.rank() > 1: data = data[0] # ok now we have a good x and a good data nbars = len(data) # create the primitive fill = x.createfillarea() line = x.createline() fill.viewport = [ template.data.x1, template.data.x2, template.data.y1, template.data.y2 ] line.viewport = [ template.data.x1, template.data.x2, template.data.y1, template.data.y2 ] axb = data.getAxis(0).getBounds() xmn, xmx = vcs.minmax(axb) ymn, ymx = vcs.minmax(data) xmn, xmx, ymn, ymx = self.prep_plot(xmn, xmx, ymn, ymx) fill.worldcoordinate = [xmn, xmx, ymn, ymx] line.worldcoordinate = [xmn, xmx, ymn, ymx] styles = [] cols = [] indices = [] lt = [] lw = [] lc = [] xs = [] ys = [] for i in range(nbars): if i < len(self.fillareastyles): styles.append(self.fillareastyles[i]) else: styles.append(self.fillareastyles[-1]) if i < len(self.fillareacolors): cols.append(self.fillareacolors[i]) else: cols.append(self.fillareacolors[-1]) if i < len(self.fillareaindices): indices.append(self.fillareaindices[i]) else: indices.append(self.fillareaindices[-1]) if i < len(self.line): lt.append(self.line[i]) else: lt.append(self.line[-1]) if i < len(self.linewidth): lw.append(self.linewidth[i]) else: lw.append(self.linewidth[-1]) if i < len(self.line): lc.append(self.linecolors[i]) else: lc.append(self.linecolors[-1]) xs.append([axb[i][0], axb[i][1], axb[i][1], axb[i][0], axb[i][0]]) ys.append([0, 0, data[i], data[i], 0]) fill.style = styles fill.x = xs fill.y = ys fill.style fill.index = indices fill.color = cols line.x = xs line.y = ys line.type = lt line.width = lw line.color = lc displays = [] displays.append(x.plot(fill, bg=bg)) displays.append(x.plot(line, bg=bg)) x.worldcoordinate = fill.worldcoordinate dsp = template.plot(data, self, bg=bg) for d in dsp: displays.append(d) self.restore() return displays