def vars(self, ruvp="r", i=False, j=False): ruvp = ruvp[0] isU = ruvp == "u" isV = ruvp == "v" isP = ruvp == "p" if isU: x = self.lonu y = self.latu h = rt.rho2uvp(self.h, "u") m = self.masku elif isV: x = self.lonv y = self.latv h = rt.rho2uvp(self.h, "v") m = self.maskv elif isP: x = self.lonp y = self.latp h = rt.rho2uvp(self.h, "p") m = self.maskp else: x = self.lon y = self.lat h = self.h m = self.mask if j is False: j = slice(None) if i is False: i = slice(None) return x[j, i], y[j, i], h[j, i], m[j, i]
def s_levels(self,time,loc='rr',i=False,j=False,k=False,extrapZeta=False): ## ruvpw=ruvpw[0] try: hLoc,vLoc=loc except: hLoc,vLoc=loc,'r' h=self.grid.h zeta=self.use('zeta',SEARCHtime=time) if extrapZeta: if not calc.ismarray(zeta): zeta=np.ma.masked_where(self.grid.mask==0,zeta) zeta=calc.mask_extrap(self.grid.lon,self.grid.lat,zeta) h=rt.rho2uvp(h,hLoc) ##########ruvpw) zeta=rt.rho2uvp(zeta,hLoc) ###########ruvpw) z=rt.s_levels(h,zeta,self.s_params,rw=vLoc) ##########ruvpw) if k is False: k=slice(None) if j is False: j=slice(None) if i is False: i=slice(None) return z[k,j,i]
def s_levels(self, sparams, zeta=0, h=False, ruvpw='r', i=False, j=False, k=False): ruvpw = ruvpw[0] isW = ruvpw == 'w' if h is False: h = self.h try: zeta.shape == h.shape except: zeta = np.tile(zeta, h.shape).astype(h.dtype) if h.ndim == 2: h = rt.rho2uvp(h, ruvpw) zeta = rt.rho2uvp(zeta, ruvpw) zr, zw = rt.s_levels(h, zeta, sparams) if isW: z = zw else: z = zr if k is False: k = slice(None) if j is False: j = slice(None) if i is False: i = slice(None) return np.squeeze(z[k, j, i])
def s_levels(self, time, ruvpw='r', i=False, j=False, k=False, extrapZeta=False): ruvpw = ruvpw[0] h = self.grid.h zeta = self.use('zeta', SEARCHtime=time) if extrapZeta: if not calc.ismarray(zeta): zeta = np.ma.masked_where(self.grid.mask == 0, zeta) zeta = calc.mask_extrap(self.grid.lon, self.grid.lat, zeta) h = rt.rho2uvp(h, ruvpw) zeta = rt.rho2uvp(zeta, ruvpw) z = rt.s_levels(h, zeta, self.s_params, rw=ruvpw) if k is False: k = slice(None) if j is False: j = slice(None) if i is False: i = slice(None) return z[k, j, i]
def s_levels(self,sparams,zeta=0,h=False,loc='rr',i=False,j=False,k=False): try: hLoc,vLoc=loc except: hLoc,vLoc=loc,'r' #### ruvpw=ruvpw[0] #### isW=ruvpw=='w' if h is False: h=self.h try: zeta.shape==h.shape except: zeta=np.tile(zeta,h.shape).astype(h.dtype) if h.ndim==2: h=rt.rho2uvp(h,hLoc) zeta=rt.rho2uvp(zeta,hLoc) z=rt.s_levels(h,zeta,sparams,rw=vLoc) ### zr,zw=rt.s_levels(h,zeta,sparams) ### if isW:z=zw ### if vLoc=='w': z=zw ### else: z=zr if k is False: k=slice(None) if j is False: j=slice(None) if i is False: i=slice(None) return np.squeeze(z[k,j,i])
def vars(self, ruvp='r', i=False, j=False): ruvp = ruvp[0] isU = ruvp == 'u' isV = ruvp == 'v' isP = ruvp == 'p' if isU: x = self.lonu y = self.latu h = rt.rho2uvp(self.h, 'u') m = self.masku elif isV: x = self.lonv y = self.latv h = rt.rho2uvp(self.h, 'v') m = self.maskv elif isP: x = self.lonp y = self.latp h = rt.rho2uvp(self.h, 'p') m = self.maskp else: x = self.lon y = self.lat h = self.h m = self.mask if j is False: j = slice(None) if i is False: i = slice(None) return x[j, i], y[j, i], h[j, i], m[j, i]
def vars(self,ruvp='r',i=False,j=False): ruvp=ruvp[0] isU=ruvp=='u' isV=ruvp=='v' isP=ruvp=='p' if isU: x = self.lonu y = self.latu h = rt.rho2uvp(self.h,'u') m = self.masku elif isV: x = self.lonv y = self.latv h = rt.rho2uvp(self.h,'v') m = self.maskv elif isP: x = self.lonp y = self.latp h = rt.rho2uvp(self.h,'p') m = self.maskp else: x = self.lon y = self.lat h = self.h m = self.mask if j is False: j=slice(None) if i is False: i=slice(None) return x[j,i],y[j,i],h[j,i],m[j,i]
def s_levels(self,sparams,zeta=0,h=False,ruvpw='r',i=False,j=False,k=False): ruvpw=ruvpw[0] isW=ruvpw=='w' if h is False: h=self.h try: zeta.shape==h.shape except: zeta=np.tile(zeta,h.shape).astype(h.dtype) if h.ndim==2: h=rt.rho2uvp(h,ruvpw) zeta=rt.rho2uvp(zeta,ruvpw) zr,zw=rt.s_levels(h,zeta,sparams) if isW:z=zw else: z=zr if k is False: k=slice(None) if j is False: j=slice(None) if i is False: i=slice(None) return np.squeeze(z[k,j,i])
def sliceuv(self,ind,time=0,**opts):#plot=False,**opts): ### plot= opts.pop('plot',False) ### opts['plot']=False ### ax = opts.get('ax',None) coords=opts.get('coords',self._default_coords('sliceuv')).split(',') #### out=Data() #### # savename=opts.pop('savename',False) # retMsg=opts.pop('msg',False) if ind=='bar': slc,uname,vname,ind = self.slicek, 'ubar','vbar', 9999 elif ind in ('s','surf','surface'): slc,uname,vname,ind = self.slicek, 'u','v', -1 elif ind>=0: slc,uname,vname,ind = self.slicek, 'u','v', ind elif ind <0: isK=False slc,uname,vname,ind = self.slicez, 'u','v', ind ## if isK: # xu,yu,zu,u,msg1=self.slicek(uname,ind,time,msg=True,**opts) # xv,yv,zv,v,msg2=self.slicek(vname,ind,time,msg=True,**opts) ## u,auxu=slc(uname,ind,time,**opts) ## v,auxv=slc(vname,ind,time,**opts) outu=slc(uname,ind,time,**opts) outv=slc(vname,ind,time,**opts) if outu.msg: return outu##None,None,auxu elif outv.msg: return outv##None,None,auxv # at psi: u,v=outu.v,outv.v u=( u[1:,:]+ u[:-1,:])/2. v=( v[:,1:]+ v[:,:-1])/2. # rotate uv: ang=rt.rho2uvp(self.grid.angle,'p') u,v=calc.rot2d(u,v,-ang) ## else: ## xu,yu,zu,u,msg1=self.slicez(uname,ind,time,msg=True,**opts) ## xv,yv,zv,v,msg2=self.slicez(vname,ind,time,msg=True,**opts) ## ## if msg1: msg=msg1+' and '+msg2 ## else: msg=msg2 out=outu out.v=u,v out.info['v']['name']=uname+vname if 'z' in coords: out.z=(out.z[1:,:]+out.z[:-1,:])/2. if 'x' in coords: out.x=(out.x[1:,:]+out.x[:-1,:])/2. if 'y' in coords: out.y=(out.y[1:,:]+out.y[:-1,:])/2. out.coordsReq=','.join(sorted(coords)) return out###u,v,aux
def slicez(self, varname, ind, time=0, plot=False, **opts): x, y, z, v = [[]] * 4 savename = False retMsg = False surf_nans = True if "savename" in opts.keys(): savename = opts["savename"] if "msg" in opts.keys(): retMsg = opts["msg"] if "surf_nans" in opts.keys(): surf_nans = opts["surf_nans"] if varname not in netcdf.varnames(self.name): msg = ":: variable %s not found" % varname if retMsg: return x, y, z, v, msg else: print msg return x, y, z, v if self.hast(varname) and time >= self.TIME: msg = "t = %d exceeds TIME dimension (%d)" % (time, self.TIME) if retMsg: return x, y, z, v, msg else: print msg return x, y, z, v v = self.use(varname, SEARCHtime=time) x, y, h, m = self.grid.vars(ruvp=self.var_at(varname)) zeta = self.use("zeta", SEARCHtime=time) zeta = rt.rho2uvp(zeta, varname) if len(v.shape) == 2: # no vertical comp if retMsg: return x, y, ind + np.zeros(v.shape), np.ma.masked_where(m == 0, v), "" else: return x, y, ind + np.zeros(v.shape), np.ma.masked_where(m == 0, v) v, mask = rt.slicez(v, m, h, zeta, self.s_params, ind, surf_nans) v = np.ma.masked_where(mask, v) if plot: p = self.grid.plot(bathy=None, **opts) xm, ym = p(x, y) pch = pylab.pcolor(xm, ym, v, shading="flat") pylab.colorbar(pch, shrink=0.7) pylab.axis([p.xmin, p.xmax, p.ymin, p.ymax]) if savename: pylab.savefig(savename, dpi=pylab.gcf().dpi) pylab.close(pylab.gcf()) if retMsg: return x, y, ind + np.zeros(v.shape), v, "" else: return x, y, ind + np.zeros(v.shape), v
def sliceuv(self, ind, time=0, plot=False, **opts): savename = False if savename in opts.keys(): savename = opts['savename'] if ind == 'bar': isK, uname, vname, ind = True, 'ubar', 'vbar', 9999 elif ind in ('s', 'surf', 'surface'): isK, uname, vname, ind = True, 'u', 'v', -1 elif ind >= 0: isK, uname, vname, ind = True, 'u', 'v', ind elif ind < 0: isK = False isK, uname, vname, ind = False, 'u', 'v', ind if isK: xu, yu, zu, u = self.slicek(uname, ind, time) xv, yv, zv, v = self.slicek(vname, ind, time) else: xu, yu, zu, u = self.slicez(uname, ind, time) xv, yv, zv, v = self.slicez(vname, ind, time) z = (zu[1:, :] + zu[:-1, :]) / 2. u = (u[1:, :] + u[:-1, :]) / 2. v = (v[:, 1:] + v[:, :-1]) / 2. x = self.grid.lonp y = self.grid.latp # rotate uv: ang = rt.rho2uvp(self.grid.angle, 'p') u, v = calc.rot2d(u, v, -ang) if plot: p = self.grid.plot(bathy=None) xm, ym = p(x, y) mm = 0 * m mm[::3, ::3] = 1 mm = mm * m == 1 s = np.sqrt(u**2 + v**2) q = pylab.quiver(xm[mm], ym[mm], u[mm], v[mm], s[mm]) pylab.colorbar(shrink=.7) pylab.axis([p.xmin, p.xmax, p.ymin, p.ymax]) qk = pylab.quiverkey(q, .05, .01, 0.2, '0.2 m/s', labelpos='E', coordinates='axes') if savename: pylab.savefig(savename, dpi=pylab.gcf().dpi) pylab.close(pylab.gcf()) return x, y, z, u, v
def slicez(self, varname, ind, time=0, plot=False, **opts): x, y, z, v = [[]] * 4 savename = False retMsg = False surf_nans = True if 'savename' in opts.keys(): savename = opts['savename'] if 'msg' in opts.keys(): retMsg = opts['msg'] if 'surf_nans' in opts.keys(): surf_nans = opts['surf_nans'] if varname not in netcdf.varnames(self.name): msg = ':: variable %s not found' % varname if retMsg: return x, y, z, v, msg else: print(msg) return x, y, z, v if self.hast(varname) and time >= self.TIME: msg = 't = %d exceeds TIME dimension (%d)' % (time, self.TIME) if retMsg: return x, y, z, v, msg else: print(msg) return x, y, z, v v = self.use(varname, SEARCHtime=time) x, y, h, m = self.grid.vars(ruvp=self.var_at(varname)) zeta = self.use('zeta', SEARCHtime=time) zeta = rt.rho2uvp(zeta, varname) if len(v.shape) == 2: # no vertical comp if retMsg: return x, y, ind + np.zeros(v.shape), np.ma.masked_where( m == 0, v), '' else: return x, y, ind + np.zeros(v.shape), np.ma.masked_where( m == 0, v) v, mask = rt.slicez(v, m, h, zeta, self.s_params, ind, surf_nans) v = np.ma.masked_where(mask, v) if plot: p = self.grid.plot(bathy=None, **opts) xm, ym = p(x, y) pch = pylab.pcolor(xm, ym, v, shading='flat') pylab.colorbar(pch, shrink=.7) pylab.axis([p.xmin, p.xmax, p.ymin, p.ymax]) if savename: pylab.savefig(savename, dpi=pylab.gcf().dpi) pylab.close(pylab.gcf()) if retMsg: return x, y, ind + np.zeros(v.shape), v, '' else: return x, y, ind + np.zeros(v.shape), v
def load_uvp(self): if not hasattr(self, "lonu"): self.lonu = rt.rho2uvp(self.lon, "u") if not hasattr(self, "lonv"): self.lonv = rt.rho2uvp(self.lon, "v") if not hasattr(self, "lonp"): self.lonp = rt.rho2uvp(self.lon, "p") if not hasattr(self, "latu"): self.latu = rt.rho2uvp(self.lat, "u") if not hasattr(self, "latv"): self.latv = rt.rho2uvp(self.lat, "v") if not hasattr(self, "latp"): self.latp = rt.rho2uvp(self.lat, "p") mu, mv, mp = rt.uvp_mask(self.mask) if not hasattr(self, "masku"): self.masku = mu if not hasattr(self, "maskv"): self.maskv = mv if not hasattr(self, "maskp"): self.maskp = mp if not hasattr(self, "XI_U"): self.XI_U = self.XI_RHO - 1 if not hasattr(self, "XI_V"): self.XI_V = self.XI_RHO if not hasattr(self, "ETA_U"): self.ETA_U = self.ETA_RHO if not hasattr(self, "ETA_V"): self.ETA_V = self.ETA_RHO - 1
def sliceuv(self,ind,time=0,plot=False,**opts): savename=False if savename in opts.keys(): savename=opts['savename'] if ind=='bar': isK,uname,vname,ind = True, 'ubar','vbar', 9999 elif ind in ('s','surf','surface'): isK,uname,vname,ind = True, 'u','v', -1 elif ind>=0: isK,uname,vname,ind = True, 'u','v', ind elif ind <0: isK=False isK,uname,vname,ind = False, 'u','v', ind if isK: xu,yu,zu,u=self.slicek(uname,ind,time) xv,yv,zv,v=self.slicek(vname,ind,time) else: xu,yu,zu,u=self.slicez(uname,ind,time) xv,yv,zv,v=self.slicez(vname,ind,time) z=(zu[1:,:]+zu[:-1,:])/2. u=( u[1:,:]+ u[:-1,:])/2. v=( v[:,1:]+ v[:,:-1])/2. x=self.grid.lonp y=self.grid.latp # rotate uv: ang=rt.rho2uvp(self.grid.angle,'p') u,v=calc.rot2d(u,v,-ang) if plot: p=self.grid.plot(bathy=None) xm, ym = p(x,y) mm=0*m mm[::3,::3]=1 mm=mm*m==1 s=np.sqrt(u**2+v**2) q=pylab.quiver(xm[mm],ym[mm],u[mm],v[mm],s[mm]) pylab.colorbar(shrink=.7) pylab.axis([p.xmin, p.xmax, p.ymin, p.ymax]) qk = pylab.quiverkey(q, .05, .01, 0.2, '0.2 m/s',labelpos='E', coordinates='axes') if savename: pylab.savefig(savename,dpi=pylab.gcf().dpi) pylab.close(pylab.gcf()) return x,y,z,u,v
def sliceuv(self, ind, time=0, plot=False, **opts): savename = False if savename in opts.keys(): savename = opts["savename"] if ind == "bar": isK, uname, vname, ind = True, "ubar", "vbar", 9999 elif ind in ("s", "surf", "surface"): isK, uname, vname, ind = True, "u", "v", -1 elif ind >= 0: isK, uname, vname, ind = True, "u", "v", ind elif ind < 0: isK = False isK, uname, vname, ind = False, "u", "v", ind if isK: xu, yu, zu, u = self.slicek(uname, ind, time) xv, yv, zv, v = self.slicek(vname, ind, time) else: xu, yu, zu, u = self.slicez(uname, ind, time) xv, yv, zv, v = self.slicez(vname, ind, time) z = (zu[1:, :] + zu[:-1, :]) / 2.0 u = (u[1:, :] + u[:-1, :]) / 2.0 v = (v[:, 1:] + v[:, :-1]) / 2.0 x = self.grid.lonp y = self.grid.latp # rotate uv: ang = rt.rho2uvp(self.grid.angle, "p") u, v = calc.rot2d(u, v, -ang) if plot: p = self.grid.plot(bathy=None) xm, ym = p(x, y) mm = 0 * m mm[::3, ::3] = 1 mm = mm * m == 1 s = np.sqrt(u ** 2 + v ** 2) q = pylab.quiver(xm[mm], ym[mm], u[mm], v[mm], s[mm]) pylab.colorbar(shrink=0.7) pylab.axis([p.xmin, p.xmax, p.ymin, p.ymax]) qk = pylab.quiverkey(q, 0.05, 0.01, 0.2, "0.2 m/s", labelpos="E", coordinates="axes") if savename: pylab.savefig(savename, dpi=pylab.gcf().dpi) pylab.close(pylab.gcf()) return x, y, z, u, v
def s_levels(self, time, ruvpw="r", i=False, j=False, k=False, extrapZeta=False): ruvpw = ruvpw[0] h = self.grid.h zeta = self.use("zeta", SEARCHtime=time) if extrapZeta: if not calc.ismarray(zeta): zeta = np.ma.masked_where(self.grid.mask == 0, zeta) zeta = calc.mask_extrap(self.grid.lon, self.grid.lat, zeta) h = rt.rho2uvp(h, ruvpw) zeta = rt.rho2uvp(zeta, ruvpw) z = rt.s_levels(h, zeta, self.s_params, rw=ruvpw) if k is False: k = slice(None) if j is False: j = slice(None) if i is False: i = slice(None) return z[k, j, i]
def load_uvp(self): if not hasattr(self,'lonu'): self.lonu=rt.rho2uvp(self.lon,'u') if not hasattr(self,'lonv'): self.lonv=rt.rho2uvp(self.lon,'v') if not hasattr(self,'lonp'): self.lonp=rt.rho2uvp(self.lon,'p') if not hasattr(self,'latu'): self.latu=rt.rho2uvp(self.lat,'u') if not hasattr(self,'latv'): self.latv=rt.rho2uvp(self.lat,'v') if not hasattr(self,'latp'): self.latp=rt.rho2uvp(self.lat,'p') mu,mv,mp=rt.uvp_mask(self.mask) if not hasattr(self,'masku'): self.masku=mu if not hasattr(self,'maskv'): self.maskv=mv if not hasattr(self,'maskp'): self.maskp=mp if not hasattr(self,'XI_U'): self.XI_U=self.XI_RHO-1 if not hasattr(self,'XI_V'): self.XI_V=self.XI_RHO if not hasattr(self,'ETA_U'): self.ETA_U=self.ETA_RHO if not hasattr(self,'ETA_V'): self.ETA_V=self.ETA_RHO-1
def load_uvp(self): if not hasattr(self, 'lonu'): self.lonu = rt.rho2uvp(self.lon, 'u') if not hasattr(self, 'lonv'): self.lonv = rt.rho2uvp(self.lon, 'v') if not hasattr(self, 'lonp'): self.lonp = rt.rho2uvp(self.lon, 'p') if not hasattr(self, 'latu'): self.latu = rt.rho2uvp(self.lat, 'u') if not hasattr(self, 'latv'): self.latv = rt.rho2uvp(self.lat, 'v') if not hasattr(self, 'latp'): self.latp = rt.rho2uvp(self.lat, 'p') mu, mv, mp = rt.uvp_mask(self.mask) if not hasattr(self, 'masku'): self.masku = mu if not hasattr(self, 'maskv'): self.maskv = mv if not hasattr(self, 'maskp'): self.maskp = mp if not hasattr(self, 'XI_U'): self.XI_U = self.XI_RHO - 1 if not hasattr(self, 'XI_V'): self.XI_V = self.XI_RHO if not hasattr(self, 'ETA_U'): self.ETA_U = self.ETA_RHO if not hasattr(self, 'ETA_V'): self.ETA_V = self.ETA_RHO - 1
def slicek(self,varname,ind,time=0,plot=False,**opts): x,y,z,v=[[]]*4 RetVarOnly=False savename=False retMsg=False if ind in ('s','surf','surface'): ind=-1 if 'retvar' in opts.keys(): RetVarOnly = opts['retvar'] if 'savename' in opts.keys(): savename = opts['savename'] if 'msg' in opts.keys(): retMsg = opts['msg'] if varname not in netcdf.varnames(self.name): msg=':: variable %s not found' % varname if retMsg: return x,y,z,v,msg else: print(msg) return x,y,z,v isW=varname=='w' hasZ=self.hasz(varname) if hasZ: if isW and ind >= self.S_W: msg='k = %d exceeds S_W dimension (%d)' % (ind,self.S_W) if retMsg: return x,y,z,v,msg else: print(msg) return x,y,z,v elif ind >= self.S_RHO: msg='k = %d exceeds S_RHO dimension (%d)' % (ind,self.S_RHO) if retMsg: return x,y,z,v,msg else: print(msg) return x,y,z,v if self.hast(varname) and time>=self.TIME: msg='t = %d exceeds TIME dimension (%d)' % (time,self.TIME) if retMsg: return x,y,z,v,msg else: print(msg) return x,y,z,v if isW: v=self.use(varname,SEARCHtime=time,s_w=ind) else: v=self.use(varname,SEARCHtime=time,s_rho=ind) x,y,h,m=self.grid.vars(ruvp=self.var_at(varname)) if hasZ: z=self.s_levels(time,k=ind,ruvpw=self.var_at(varname)) else: z=self.use('zeta',SEARCHtime=time) z=rt.rho2uvp(z,varname) if not np.ma.isMA(v): # roms agrif ... v=np.ma.masked_where(m==0,v) if plot: p=self.grid.plot(bathy=None,**opts) xm, ym = p(x,y) pch=pylab.pcolor(xm,ym,v,shading='flat') pylab.colorbar(pch,shrink=.7) pylab.axis([p.xmin, p.xmax, p.ymin, p.ymax]) if savename: pylab.savefig(savename,dpi=pylab.gcf().dpi) pylab.close(pylab.gcf()) if RetVarOnly: return v else: if retMsg: return x,y,z,v,'' else: return x,y,z,v
def slicez(self,varname,ind,time=0,**opts): if not self.hasz(varname): return self.slicek(varname,ind,time,**opts) surf_nans=opts.get('surf_nans',True) spline=opts.get('spline',True) coords=opts.get('coords',self._default_coords('slicez')).split(',') out=Data() out.msg=self.check_slice(varname,t=time) if out.msg: return out##None,au v=self.use(varname,SEARCHtime=time) x,y,h,m=self.grid.vars(ruvp=self.var_at(varname)) zeta=self.use('zeta',SEARCHtime=time) zeta=rt.rho2uvp(zeta,varname) v,mask=rt.slicez(v,m,h,zeta,self.s_params,ind,surf_nans,spline) v=np.ma.masked_where(mask,v) out.v=v out.info['v']['name']=varname out.info['v']['slice']='z=%d'%ind try: out.info['v']['units']=netcdf.vatt(self.nc,varname,'units') except: pass # coords: if 'x' in coords: if self.grid.is_spherical: out.x=x out.info['x']=dict(name='Longitude',units=r'$\^o$E') else: out.x=x/1000. out.info['x']=dict(name='Distance',units='km') if 'y' in coords: if self.grid.is_spherical: out.y=y out.info['y']=dict(name='Latitude',units=r'$\^o$N') else: out.y=y/1000. out.info['y']=dict(name='Distance',units='km') if 'z' in coords: out.z=ind+np.zeros(v.shape) out.info['z']=dict(name='Depth',units='m') if 't' in coords: out.t=self.time[time] if v.ndim==2: out.extra=[Data()] if 'x' in coords: out.extra[0].x=out.x if 'y' in coords: out.extra[0].y=out.y out.extra[0].v=h if h.max()>1000: cvals=200.,1000. elif h.max()>200: cvals=50.,100.,200. else: cvals=3 out.extra[0].config['field.plot']='contour' out.extra[0].config['field.cvals']=cvals out.extra[0].config['field.linecolors']='k' out.extra[0].alias='bathy' out.coordsReq=','.join(sorted(coords)) return out
def slicez(self,varname,ind,time=0,**opts): if not self.hasz(varname): return self.slicek(varname,ind,time,**opts) surf_nans=opts.get('surf_nans',True) spline=opts.get('spline',True) coords=opts.get('coords',self._default_coords('slicez')).split(',') out=Data() out.msg=self.check_slice(varname,t=time) if out.msg: return out##None,au v=self.use(varname,SEARCHtime=time) x,y,h,m=self.grid.vars(ruvp=self.var_at(varname)[0]) zeta=self.use('zeta',SEARCHtime=time) zeta=rt.rho2uvp(zeta,varname) out.v=rt.slicez(v,m,h,zeta,self.s_params,ind,surf_nans,spline) out.info['v']['name']=varname if calc.isarray(ind): out.info['v']['slice']='z= array %.2f to %.2f'%(ind.min(),ind.max()) else: out.info['v']['slice']='z=%d'%ind try: out.info['v']['units']=netcdf.vatt(self.nc,varname,'units') except: pass # coords: if 'x' in coords: if self.grid.is_spherical: out.x=x out.info['x']=dict(name='Longitude',units=r'$\^o$E') else: out.x=x/1000. out.info['x']=dict(name='Distance',units='km') if 'y' in coords: if self.grid.is_spherical: out.y=y out.info['y']=dict(name='Latitude',units=r'$\^o$N') else: out.y=y/1000. out.info['y']=dict(name='Distance',units='km') if 'z' in coords: out.z=ind+np.zeros(out.v.shape) out.info['z']=dict(name='Depth',units='m') if 't' in coords and self.hast(varname): out.t=self.time[time] if v.ndim==2: out.extra=[Data()] if 'x' in coords: out.extra[0].x=out.x if 'y' in coords: out.extra[0].y=out.y out.extra[0].v=h if h.max()>1000: cvals=200.,1000. elif h.max()>200: cvals=50.,100.,200. else: cvals=3 out.extra[0].config['field.plot']='contour' out.extra[0].config['field.cvals']=cvals out.extra[0].config['field.linecolors']='k' out.extra[0].alias='bathy' out.coordsReq=','.join(sorted(coords)) return out
def slicek(self, varname, ind, time=0, plot=False, **opts): x, y, z, v = [[]] * 4 RetVarOnly = False savename = False retMsg = False if ind in ('s', 'surf', 'surface'): ind = -1 if 'retvar' in opts.keys(): RetVarOnly = opts['retvar'] if 'savename' in opts.keys(): savename = opts['savename'] if 'msg' in opts.keys(): retMsg = opts['msg'] if varname not in netcdf.varnames(self.name): msg = ':: variable %s not found' % varname if retMsg: return x, y, z, v, msg else: print(msg) return x, y, z, v isW = varname == 'w' hasZ = self.hasz(varname) if hasZ: if isW and ind >= self.S_W: msg = 'k = %d exceeds S_W dimension (%d)' % (ind, self.S_W) if retMsg: return x, y, z, v, msg else: print(msg) return x, y, z, v elif ind >= self.S_RHO: msg = 'k = %d exceeds S_RHO dimension (%d)' % (ind, self.S_RHO) if retMsg: return x, y, z, v, msg else: print(msg) return x, y, z, v if self.hast(varname) and time >= self.TIME: msg = 't = %d exceeds TIME dimension (%d)' % (time, self.TIME) if retMsg: return x, y, z, v, msg else: print(msg) return x, y, z, v if isW: v = self.use(varname, SEARCHtime=time, s_w=ind) else: v = self.use(varname, SEARCHtime=time, s_rho=ind) x, y, h, m = self.grid.vars(ruvp=self.var_at(varname)) if hasZ: z = self.s_levels(time, k=ind, ruvpw=self.var_at(varname)) else: z = self.use('zeta', SEARCHtime=time) z = rt.rho2uvp(z, varname) if not np.ma.isMA(v): # roms agrif ... v = np.ma.masked_where(m == 0, v) if plot: p = self.grid.plot(bathy=None, **opts) xm, ym = p(x, y) pch = pylab.pcolor(xm, ym, v, shading='flat') pylab.colorbar(pch, shrink=.7) pylab.axis([p.xmin, p.xmax, p.ymin, p.ymax]) if savename: pylab.savefig(savename, dpi=pylab.gcf().dpi) pylab.close(pylab.gcf()) if RetVarOnly: return v else: if retMsg: return x, y, z, v, '' else: return x, y, z, v