def slicell(self,varname,X,Y,time=0,plot=False,**opts): x,y,z,v,m=[[]]*5 data = opts.get('data',False) dist = opts.get('dist',False) extrap = opts.get('extrap',False) varOnly = opts.get('retv',False) maskLimit = opts.get('lmask',0.5) # points where interpolated mask are above # this value are considered as mask! # Most strict value is 0 X=np.array(X) Y=np.array(Y) if X.ndim>1: X=np.squeeze(X) if Y.ndim>1: Y=np.squeeze(X) if varname not in netcdf.varnames(self.name): print(':: variable %s not found' % varname) return x,y,z,v,m if time>=self.TIME: print('t = %d exceeds TIME dimension (%d)' % (time,self.TIME)) return x,y,z,v,m if data is False: v=self.use(varname,SEARCHtime=time) else: v=data x,y,h,m=self.grid.vars(ruvp=self.var_at(varname)) if v.ndim==3: V=calc.griddata(x,y,v,X,Y,extrap=extrap,mask2d=m==0, keepMaskVal=maskLimit) elif v.ndim==2: V=calc.griddata(x,y,np.ma.masked_where(m==0,v),X,Y,extrap=extrap, keepMaskVal=maskLimit) if varOnly: return V # Z: if v.ndim==3: Z=self.path_s_levels(time,X,Y,rw=varname[0]) else: Z=0.*X # X, Y, dist: if dist: Dist=calc.distance(X,Y) if v.ndim==3: Dist=np.tile(Dist,(v.shape[0],1)) else: if v.ndim==3: X=np.tile(X,(v.shape[0],1)) Y=np.tile(Y,(v.shape[0],1)) if dist: return Dist,Z,V else: return X,Y,Z,V
def slicej(self,varname,ind,time=0,**opts): coords=opts.get('coords',self._default_coords('slicej')).split(',') out=vis.Data() out.label='slicej' out.msg=self.check_slice(varname,t=time,j=ind) if out.msg: return out v=self.use(varname,SEARCHtime=time,eta_SEARCH=ind) # add mask if not masked: if not np.ma.isMA(v): ### m=self.grid.vars(ruvp=self.var_at(varname)[0],j=ind)[-1] m=self.grid.vars(ruvp=self.vloc(varname)[0],j=ind)[-1] if v.ndim==2: m=np.tile(m,(v.shape[0],1)) v=np.ma.masked_where(m==0,v) out.v=v out.info['v']['name']=varname out.info['v']['slice']='j=%d'%ind try: out.info['v']['units']=netcdf.vatt(self.name,varname,'units') except: pass # coords: if 'z' in coords and v.ndim==2: ###### out.z=self.s_levels(time=time,ruvpw=self.var_at(varname),j=ind) ### out.z=self.s_levels(time=time,loc=self.var_at(varname),j=ind) out.z=self.s_levels(time=time,loc=self.vloc(varname),j=ind) out.info['z']=dict(name='Depth',units='m') if any([i in coords for i in 'xyd']): ### x,y,h,m=self.grid.vars(ruvp=self.var_at(varname)[0],j=ind) x,y,h,m=self.grid.vars(ruvp=self.vloc(varname)[0],j=ind) if 'd' in coords: d=calc.distance(x,y) if d[-1]-d[0]>1e4: d=d/1000. dunits='km' else: dunits='m' if v.ndim==2: d=np.tile(d,(v.shape[0],1)) out.d=d out.info['d']=dict(name='Distance',units=dunits) if 'x' in coords: if v.ndim==2: x=np.tile(x,(v.shape[0],1)) out.x=x out.info['x']=dict(name='Longitude',units=r'$\^o$E') if 'y' in coords: if v.ndim==2: y=np.tile(y,(v.shape[0],1)) out.y=y out.info['y']=dict(name='Latitude',units=r'$\^o$N') ######## if 't' in coords and self.hast(varname): out.t=self.time[time] if 't' in coords and 't' in self.vaxes(varname): out.t=self.time[time] if v.ndim==2: out.extra=[vis.Data()] if 'd' in coords: out.extra[0].x=out.d[0] if 'x' in coords: out.extra[0].y=out.x[0] if 'y' in coords: out.extra[0].x=out.y[0] out.extra[0].v=-h out.extra[0].config['d1.plot']='fill_between' out.extra[0].config['d1.y0']=-h.max()-(h.max()-h.min())/20. out.extra[0].label='bottom' out.coordsReq=','.join(sorted(coords)) return out
def slicell(self,varname,X,Y,time=0,**opts): coords=opts.get('coords',self._default_coords('slicell')).split(',') data = opts.get('data',False) extrap = opts.get('extrap',False) maskLimit = opts.get('lmask',0.5) # points where interpolated mask are above # this value are considered as mask! # Most strict value is 0 out=vis.Data() out.label='slicell' out.msg=self.check_slice(varname,t=time) if out.msg: return out#None,aux X=np.asarray(X) Y=np.asarray(Y) if X.ndim>1: X=np.squeeze(X) if Y.ndim>1: Y=np.squeeze(X) ### x,y,h,m=self.grid.vars(ruvp=self.var_at(varname)[0]) x,y,h,m=self.grid.vars(ruvp=self.vloc(varname)[0]) if True: # extrat only portion of data needed: i0,i1,j0,j1=calc.ij_limits(x, y, (X.min(),X.max()),(Y.min(),Y.max()), margin=1) xi='%d:%d'%(i0,i1) eta='%d:%d'%(j0,j1) if data is False: V=self.use(varname,SEARCHtime=time,xi_SEARCH=xi,eta_SEARCH=eta) else: v=data[...,j0:j1,i0:i1] x=x[j0:j1,i0:i1] y=y[j0:j1,i0:i1] h=h[j0:j1,i0:i1] m=m[j0:j1,i0:i1] else: if data is False: V=self.use(varname,SEARCHtime=time) else: v=data if V.ndim==3: v=calc.griddata(x,y,V,X,Y,extrap=extrap,mask2d=m==0, keepMaskVal=maskLimit) elif V.ndim==2: v=calc.griddata(x,y,np.ma.masked_where(m==0,V),X,Y,extrap=extrap, keepMaskVal=maskLimit) out.v=v out.info['v']['name']=varname out.info['v']['slice']='path npts=%d'%X.size try: out.info['v']['units']=netcdf.vatt(self.nc,varname,'units') except: pass # coords: if 'z' in coords and V.ndim==3: inds=dict(xi=(i0,i1),eta=(j0,j1)) ######### out.z=self.path_s_levels(time,X,Y,rw=varname[0],inds=inds) ### out.z=self.path_s_levels(time,X,Y,rw=self.var_at(varname)[1],inds=inds) ### ####### out.z,zw=self.path_s_levels(time,X,Y,rw=False,inds=inds) ####### if self.vloc(varname)[1]=='w': out.z=zw out.z=self.path_s_levels(time,X,Y,rw=self.vloc(varname)[1],inds=inds) out.info['z']=dict(name='Depth',units='m') if 'd' in coords: d=calc.distance(X,Y) if d[-1]-d[0]>1e4: d=d/1000. dunits='km' else: dunits='m' if v.ndim==2: d=np.tile(d,(v.shape[0],1)) out.d=d out.info['d']=dict(name='Distance',units=dunits) if 'x' in coords: if v.ndim==2: X=np.tile(X,(v.shape[0],1)) out.x=X out.info['x']=dict(name='Longitude',units=r'$\^o$E') if 'y' in coords: if v.ndim==2: Y=np.tile(Y,(v.shape[0],1)) out.y=Y out.info['y']=dict(name='Latitude',units=r'$\^o$N') ####### if 't' in coords and self.hast(varname): out.t=self.time[time] if 't' in coords and 't' in self.vaxes(varname): out.t=self.time[time] if v.ndim==2: ################3 and not out.z is None: # zeta and bottom already calculated out.extra=[vis.Data()] if 'd' in coords: out.extra[0].x=out.d[0] if 'x' in coords: out.extra[0].y=out.x[0] if 'y' in coords: out.extra[0].x=out.y[0] #### #h=-zw[0] h = calc.griddata(x,y,h,X,Y,extrap=False) out.extra[0].v=-h # bottom out.extra[0].config['d1.plot']='fill_between' out.extra[0].config['d1.y0']=-h.max()-(h.max()-h.min())/20. out.extra[0].label='bottom' out.coordsReq=','.join(sorted(coords)) return out
def slicei(self, varname, ind, time=0, dist=1, plot=False, **opts): savename = False if "savename" in opts.keys(): savename = opts["savename"] d, x, y, z, v = [[]] * 5 if varname not in netcdf.varnames(self.name): print ":: variable %s not found" % varname if dist: return d, z, v else: return x, y, z, v isU = varname == "u" isV = varname == "v" if isU: XI = self.XI_U xi = "XI_U" elif isV: if hasattr(self, "XI_V"): XI = self.XI_V xi = "XI_V" else: # this happens in current roms-agrif XI = self.XI_RHO xi = "XI_RHO" else: XI = self.XI_RHO xi = "XI_RHO" if ind >= XI: print "j = %d exceeds %s dimension (%d)" % (ind, xi, XI) x, y, h, m = self.grid.vars(ruvp=self.var_at(varname), i=ind) karg = {"SEARCHtime": time, xi.lower(): ind} v = self.use(varname, **karg) if v.ndim == 2: z = self.s_levels(time=time, ruvpw=self.var_at(varname), i=ind) elif v.ndim == 1: z = [] N = v.shape[0] if dist: d = calc.distance(x, y) if plot: pylab.figure() if v.ndim == 2: pylab.pcolor(np.tile(d, (N, 1)), z, np.ma.masked_where(np.tile(m, (N, 1)) == 0, v), shading="flat") pylab.plot(d, -h) pylab.colorbar(shrink=0.7) elif v.ndim == 1: pylab.plot(d, v) if savename: pylab.savefig(savename, dpi=pylab.gcf().dpi) pylab.close(pylab.gcf()) if v.ndim == 2: return np.tile(d, (N, 1)), z, v # ,np.tile(m,(N,1))==0 elif v.ndim == 1: return d, z, v # ,m==0 else: if plot: pylab.figure() if v.ndim == 2: pylab.pcolor(np.tile(y, (N, 1)), z, np.ma.masked_where(np.tile(m, (N, 1)) == 0, v), shading="flat") pylab.colorbar(shrink=0.7) pylab.plot(y, -h) elif v.ndim == 1: pylab.plot(y, v) if savename: pylab.savefig(savename, dpi=pylab.gcf().dpi) pylab.close(pylab.gcf()) if v.ndim == 2: return np.tile(x, (N, 1)), np.tile(y, (N, 1)), z, v # ,np.tile(m,(N,1))==0 elif v.ndim == 1: return x, y, z, v # ,m==0
def slicej(self,varname,ind,time=0,dist=1,plot=False,**opts): savename=False if 'savename' in opts.keys(): savename=opts['savename'] d,x,y,z,v=[[]]*5 if varname not in netcdf.varnames(self.name): print(':: variable %s not found' % varname) if dist: return d,z,v else: return x,y,z,v isU=varname=='u' isV=varname=='v' if isU: if hasattr(self,'ETA_U'): ETA= self.ETA_U eta='ETA_U' else: # this happens in current roms-agrif ETA= self.ETA_RHO eta='ETA_RHO' elif isV: ETA= self.ETA_V eta='ETA_V' else: ETA= self.ETA_RHO eta='ETA_RHO' if ind >= ETA: print('j = %d exceeds %s dimension (%d)' % (ind,eta,ETA)) if dist: return d,z,v else: return x,y,z,v x,y,h,m=self.grid.vars(ruvp=self.var_at(varname),j=ind) karg={'SEARCHtime':time,eta.lower(): ind} v=self.use(varname,**karg) if v.ndim==2: z=self.s_levels(time=time,ruvpw=self.var_at(varname),j=ind) elif v.ndim==1: z=[] N=v.shape[0] if dist: d=calc.distance(x,y) if plot: pylab.figure() if v.ndim==2: pylab.pcolor(np.tile(d,(N,1)),z,np.ma.masked_where(np.tile(m,(N,1))==0,v),shading='flat') pylab.plot(d,-h) pylab.colorbar(shrink=.7) elif v.ndim==1: pylab.plot(d,v) if savename: pylab.savefig(savename,dpi=pylab.gcf().dpi) pylab.close(pylab.gcf()) if v.ndim==2: return np.tile(d,(N,1)),z,v elif v.ndim==1: return d,z,v #np.ma.masked_where(m==0,v) else: if plot: pylab.figure() if v.ndim==2: pylab.pcolor(tile(x,(N,1)),z,ma.masked_where(tile(m,(N,1))==0,v),shading='flat') pylab.colorbar(shrink=.7) pylab.plot(x,-h) elif v.ndim==1: pylab.plot(x,v) if savename: pylab.savefig(savename,dpi=pylab.gcf().dpi) pylab.close(pylab.gcf()) if v.ndim==2: m=np.tile(m,(N,1))==0 return np.tile(x,(N,1)),np.tile(y,(N,1)),z,v #np.ma.masked_where(m,v) elif v.ndim==1: return x,y,z,v #np.ma.masked_where(m==0,v)
def slicej(self, varname, ind, time=0, dist=1, plot=False, **opts): savename = False if 'savename' in opts.keys(): savename = opts['savename'] d, x, y, z, v = [[]] * 5 if varname not in netcdf.varnames(self.name): print(':: variable %s not found' % varname) if dist: return d, z, v else: return x, y, z, v isU = varname == 'u' isV = varname == 'v' if isU: if hasattr(self, 'ETA_U'): ETA = self.ETA_U eta = 'ETA_U' else: # this happens in current roms-agrif ETA = self.ETA_RHO eta = 'ETA_RHO' elif isV: ETA = self.ETA_V eta = 'ETA_V' else: ETA = self.ETA_RHO eta = 'ETA_RHO' if ind >= ETA: print('j = %d exceeds %s dimension (%d)' % (ind, eta, ETA)) if dist: return d, z, v else: return x, y, z, v x, y, h, m = self.grid.vars(ruvp=self.var_at(varname), j=ind) karg = {'SEARCHtime': time, eta.lower(): ind} v = self.use(varname, **karg) if v.ndim == 2: z = self.s_levels(time=time, ruvpw=self.var_at(varname), j=ind) elif v.ndim == 1: z = [] N = v.shape[0] if dist: d = calc.distance(x, y) if plot: pylab.figure() if v.ndim == 2: pylab.pcolor(np.tile(d, (N, 1)), z, np.ma.masked_where( np.tile(m, (N, 1)) == 0, v), shading='flat') pylab.plot(d, -h) pylab.colorbar(shrink=.7) elif v.ndim == 1: pylab.plot(d, v) if savename: pylab.savefig(savename, dpi=pylab.gcf().dpi) pylab.close(pylab.gcf()) if v.ndim == 2: return np.tile(d, (N, 1)), z, v elif v.ndim == 1: return d, z, v #np.ma.masked_where(m==0,v) else: if plot: pylab.figure() if v.ndim == 2: pylab.pcolor(tile(x, (N, 1)), z, ma.masked_where(tile(m, (N, 1)) == 0, v), shading='flat') pylab.colorbar(shrink=.7) pylab.plot(x, -h) elif v.ndim == 1: pylab.plot(x, v) if savename: pylab.savefig(savename, dpi=pylab.gcf().dpi) pylab.close(pylab.gcf()) if v.ndim == 2: m = np.tile(m, (N, 1)) == 0 return np.tile(x, (N, 1)), np.tile( y, (N, 1)), z, v #np.ma.masked_where(m,v) elif v.ndim == 1: return x, y, z, v #np.ma.masked_where(m==0,v)
def slicell(self, varname, X, Y, time=0, plot=False, **opts): x, y, z, v, m = [[]] * 5 data = opts.get('data', False) dist = opts.get('dist', False) extrap = opts.get('extrap', False) varOnly = opts.get('retv', False) maskLimit = opts.get('lmask', 0.5) # points where interpolated mask are above # this value are considered as mask! # Most strict value is 0 X = np.array(X) Y = np.array(Y) if X.ndim > 1: X = np.squeeze(X) if Y.ndim > 1: Y = np.squeeze(X) if varname not in netcdf.varnames(self.name): print(':: variable %s not found' % varname) return x, y, z, v, m if time >= self.TIME: print('t = %d exceeds TIME dimension (%d)' % (time, self.TIME)) return x, y, z, v, m if data is False: v = self.use(varname, SEARCHtime=time) else: v = data x, y, h, m = self.grid.vars(ruvp=self.var_at(varname)) if v.ndim == 3: V = calc.griddata(x, y, v, X, Y, extrap=extrap, mask2d=m == 0, keepMaskVal=maskLimit) elif v.ndim == 2: V = calc.griddata(x, y, np.ma.masked_where(m == 0, v), X, Y, extrap=extrap, keepMaskVal=maskLimit) if varOnly: return V # Z: if v.ndim == 3: Z = self.path_s_levels(time, X, Y, rw=varname[0]) else: Z = 0. * X # X, Y, dist: if dist: Dist = calc.distance(X, Y) if v.ndim == 3: Dist = np.tile(Dist, (v.shape[0], 1)) else: if v.ndim == 3: X = np.tile(X, (v.shape[0], 1)) Y = np.tile(Y, (v.shape[0], 1)) if dist: return Dist, Z, V else: return X, Y, Z, V
def slicell(self,varname,X,Y,time=0,**opts): coords=opts.get('coords',self._default_coords('slicell')).split(',') data = opts.get('data',False) extrap = opts.get('extrap',False) maskLimit = opts.get('lmask',0.5) # points where interpolated mask are above # this value are considered as mask! # Most strict value is 0 out=Data() out.msg=self.check_slice(varname,t=time) if out.msg: return out#None,aux X=np.asarray(X) Y=np.asarray(Y) if X.ndim>1: X=np.squeeze(X) if Y.ndim>1: Y=np.squeeze(X) x,y,h,m=self.grid.vars(ruvp=self.var_at(varname)[0]) if True: # extrat only portion of data needed: i0,i1,j0,j1=calc.ij_limits(x, y, (X.min(),X.max()),(Y.min(),Y.max()), margin=1) xi='%d:%d'%(i0,i1) eta='%d:%d'%(j0,j1) if data is False: V=self.use(varname,SEARCHtime=time,xi_SEARCH=xi,eta_SEARCH=eta) else: v=data[...,j0:j1,i0:i1] x=x[j0:j1,i0:i1] y=y[j0:j1,i0:i1] #h=h[j0:j1,i0:i1] # not used m=m[j0:j1,i0:i1] else: if data is False: V=self.use(varname,SEARCHtime=time) else: v=data if V.ndim==3: v=calc.griddata(x,y,V,X,Y,extrap=extrap,mask2d=m==0, keepMaskVal=maskLimit) elif V.ndim==2: v=calc.griddata(x,y,np.ma.masked_where(m==0,V),X,Y,extrap=extrap, keepMaskVal=maskLimit) out.v=v out.info['v']['name']=varname out.info['v']['slice']='path npts=%d'%X.size try: out.info['v']['units']=netcdf.vatt(self.nc,varname,'units') except: pass # coords: if 'z' in coords and V.ndim==3: inds=dict(xi=(i0,i1),eta=(j0,j1)) ######### out.z=self.path_s_levels(time,X,Y,rw=varname[0],inds=inds) out.z=self.path_s_levels(time,X,Y,rw=self.var_at(varname)[1],inds=inds) if 'd' in coords: d=calc.distance(X,Y) if v.ndim==2: d=np.tile(d,(v.shape[0],1)) out.d=d if 'x' in coords: if v.ndim==2: X=np.tile(X,(v.shape[0],1)) out.x=X if 'y' in coords: if v.ndim==2: Y=np.tile(Y,(v.shape[0],1)) out.y=Y if 't' in coords and self.hast(varname): out.t=self.time[time] out.coordsReq=','.join(sorted(coords)) return out
def data2roms(data,grd,sparams,**kargs): ''' Interpolates data to roms 3D grid. The dict data must contain the prognostic variables temp, salt, u, v (3d) and ssh (zeta, 2d), as well as lon, lat (2d), depth (1d) and time/date info: date (data date), date0 (reference date) and time (difference between date and date0). The input data can be provided by load_data. Parameters ---------- data : dict with prognostic variables grd : ROMS netcdf grid file sparams : s-coordinates parameters, theta_s,theta_b, hc and NLevels **kargs: ij : axis for vertical interpolations (*i,j) ij_ind : list of i or j index for vertical interpolation, all by default (ij_ind=False) horizAux : if True, the original data horizontally interpolated is returned and can be used for next data2roms call with this same karg quiet : output messages flag (false by default) ''' ij='j' ij_ind=False horizAux=False quiet=False if 'ij' in kargs.keys(): ij = kargs['ij'] if 'ij_ind' in kargs.keys(): ij_ind = kargs['ij_ind'] if 'horizAux' in kargs.keys(): horizAux = kargs['horizAux'] if 'quiet' in kargs.keys(): quiet = kargs['quiet'] if not quiet: print 'using grid %s' % grd g=roms.Grid(grd) xr,yr,hr,mr=g.vars('r') xu,yu,hu,mu=g.vars('u') xv,yv,hv,mv=g.vars('v') ny,nx=hr.shape nz=sparams[3] NX=data['NX'] NY=data['NY'] NZ=data['NZ'] if not quiet: print 'calc s levels...' sshr=calc.griddata(data['lon'],data['lat'],data['ssh'],xr,yr,extrap=True) Zr = g.s_levels(sparams,sshr,hr,'r') Zu = g.s_levels(sparams,sshr,hr,'u') Zv = g.s_levels(sparams,sshr,hr,'v') # interp horiz: retHorizAux=horizAux is True if horizAux in (True,False): TEMP = np.ma.masked_all((NZ,ny,nx),data['temp'].dtype) SALT = np.ma.masked_all((NZ,ny,nx),data['salt'].dtype) U = np.ma.masked_all((NZ,ny,nx),data['u'].dtype) V = np.ma.masked_all((NZ,ny,nx),data['v'].dtype) if not quiet: print 'horizontal interpolation:' for i in range(NZ): if not quiet and i%10==0: print ' lev %d of %d' % (i,NZ) #import pylab #pylab.figure() #pylab.pcolormesh(data['lon'],data['lat'],data['temp'][i,...]) try: TEMP[i,...] = calc.griddata(data['lon'],data['lat'],data['temp'][i,...],xr,yr,extrap=True) except: pass try: SALT[i,...] = calc.griddata(data['lon'],data['lat'],data['salt'][i,...],xr,yr,extrap=True) except: pass try: U[i,...] = calc.griddata(data['lon'],data['lat'],data['u'][i,...],xr,yr,extrap=True) except: pass try: V[i,...] = calc.griddata(data['lon'],data['lat'],data['v'][i,...],xr,yr,extrap=True) except: pass # rotate U,V: if not quiet: print 'rotating U,V to grid angle' angle=g.use('angle') # rad U,V=calc.rot2d(U,V,angle) U=rt.rho2uvp3d(U,'u') V=rt.rho2uvp3d(V,'v') horizAux={} horizAux['TEMP'] = TEMP horizAux['SALT'] = SALT horizAux['U'] = U horizAux['V'] = V else: TEMP = horizAux['TEMP'] SALT = horizAux['SALT'] U = horizAux['U'] V = horizAux['V'] # interp vert: nxu=nx-1 nyv=ny-1 #> ----------------------------------------------------------------- useInd=not ij_ind is False if ij_ind is False: if ij=='j': ij_ind=range(ny) elif ij=='i': ij_ind=range(nx) else: try: iter(ij_ind) except: ij_ind=[ij_ind] if ij=='j': ny=nyv=len(ij_ind) elif ij=='i': nx=nxu=len(ij_ind) # -----------------------------------------------------------------< Temp = np.zeros((nz,ny ,nx ),data['temp'].dtype) Salt = np.zeros((nz,ny ,nx ),data['salt'].dtype) Uvel = np.zeros((nz,ny ,nxu),data['u'].dtype) Vvel = np.zeros((nz,nyv,nx ),data['v'].dtype) jslice=lambda x,ind: x[:,ind,:] islice=lambda x,ind: x[:,:,ind] ZZr = np.tile(data['depth'],(nx,ny,1)).T ZZu = np.tile(data['depth'],(nxu,ny,1)).T ZZv = np.tile(data['depth'],(nx,nyv,1)).T if not useInd is False: #>------------------------------------------ if ij=='j': slice=jslice sshr=sshr[ij_ind,:] hr =hr[ij_ind,:] elif ij=='i': slice=islice sshr=sshr[:,ij_ind] hr =hr[:,ij_ind] Zr,Zu,Zv,TEMP,SALT,U,V=[slice(k,ij_ind) for k in [Zr,Zu,Zv,TEMP,SALT,U,V]] # -----------------------------------------------------------------< if useInd: # then store distances for a possible bry file dtype=Temp.dtype distr=np.zeros((nz,ny, nx ),dtype) distu=np.zeros((nz,ny, nxu),dtype) distv=np.zeros((nz,nyv,nx ),dtype) if not quiet: print 'vertical interpolation:' if ij=='j': for j in range(ny): if not quiet and (ny<10 or (ny>=10 and j%10==0)): print ' j=%3d of %3d' % (j,ny) ind=ij_ind[j] dr=np.tile(calc.distance(xr[ind,:],yr[ind,:]),(nz,1)) du=np.tile(calc.distance(xu[ind,:],yu[ind,:]),(nz,1)) Dr=np.tile(calc.distance(xr[ind,:],yr[ind,:]),(NZ,1)) Du=np.tile(calc.distance(xu[ind,:],yu[ind,:]),(NZ,1)) if useInd: distr[:,j,:]=dr; distu[:,j,:]=du; Temp[:,j,:] = calc.griddata(Dr,ZZr[:,j,:],TEMP[:,j,:],dr,Zr[:,j,:],extrap=True) Salt[:,j,:] = calc.griddata(Dr,ZZr[:,j,:],SALT[:,j,:],dr,Zr[:,j,:],extrap=True) if 0 and j%10==0: print Dr.shape, ZZr[:,j,:].shape import pylab as pl pl.figure(1) pl.clf() pl.pcolormesh(Dr,ZZr[:,j,:],SALT[:,j,:]) pl.colorbar() clim=pl.gci().get_clim() pl.figure(2) pl.clf() pl.pcolormesh(dr,Zr[:,j,:],Salt[:,j,:]) pl.clim(clim) pl.colorbar() raw_input() Uvel[:,j,:] = calc.griddata(Du,ZZu[:,j,:],U[:,j,:], du,Zu[:,j,:],extrap=True) if j<Vvel.shape[1]: dv=np.tile(calc.distance(xv[ind,:],yv[ind,:]),(nz,1)) Dv=np.tile(calc.distance(xv[ind,:],yv[ind,:]),(NZ,1)) Vvel[:,j,:] = calc.griddata(Dv,ZZv[:,j,:],V[:,j,:], dv,Zv[:,j,:],extrap=True) if useInd: distv[:,j,:]=dv if np.any(np.isnan(Temp[:,j,:])): print 'found nan in temp',j if np.any(np.isnan(Salt[:,j,:])): print 'found nan in salt',j if np.any(np.isnan(Uvel[:,j,:])): print 'found nan in u',j if j<Vvel.shape[1] and np.any(np.isnan(Vvel[:,j,:])): print 'found nan in v',j elif ij=='i': for i in range(nx): if not quiet and (nx<10 or (nx>=10 and i%10==0)): print ' i=%3d of %3d' % (i,nx) ind=ij_ind[i] dr=np.tile(calc.distance(xr[:,ind],yr[:,ind]),(nz,1)) dv=np.tile(calc.distance(xv[:,ind],yv[:,ind]),(nz,1)) Dr=np.tile(calc.distance(xr[:,ind],yr[:,ind]),(NZ,1)) Dv=np.tile(calc.distance(xv[:,ind],yv[:,ind]),(NZ,1)) if useInd: distr[:,:,i]=dr; distv[:,:,i]=dv; Temp[:,:,i] = calc.griddata(Dr,ZZr[:,:,i],TEMP[:,:,i],dr,Zr[:,:,i],extrap=True) Salt[:,:,i] = calc.griddata(Dr,ZZr[:,:,i],SALT[:,:,i],dr,Zr[:,:,i],extrap=True) Vvel[:,:,i] = calc.griddata(Dv,ZZv[:,:,i],V[:,:,i], dv,Zv[:,:,i],extrap=True) if i<Uvel.shape[2]: du=np.tile(calc.distance(xu[:,ind],yu[:,ind]),(nz,1)) Du=np.tile(calc.distance(xu[:,ind],yu[:,ind]),(NZ,1)) Uvel[:,:,i] = calc.griddata(Du,ZZu[:,:,i],U[:,:,i], du,Zu[:,:,i],extrap=True) if useInd: distu[:,:,i]=du # uv bar: if not quiet: print 'calc uvbar' if useInd is False: ubar,vbar=rt.uvbar(Uvel,Vvel,sshr,hr,sparams) else: #>------------------------------------------------------------ sshu=calc.griddata(data['lon'],data['lat'],data['ssh'],xu,yu,extrap=True) sshv=calc.griddata(data['lon'],data['lat'],data['ssh'],xv,yv,extrap=True) if ij=='j': sshu=sshu[ij_ind,:] sshv=sshv[ij_ind,:] hu =hu[ij_ind,:] hv =hv[ij_ind,:] elif ij=='i': sshu=sshu[:,ij_ind] sshv=sshv[:,ij_ind] hu =hu[:,ij_ind] hv =hv[:,ij_ind] ubar=rt.barotropic(Uvel,sshu,hu,sparams) vbar=rt.barotropic(Vvel,sshv,hv,sparams) # -----------------------------------------------------------------< Vars=cb.odict() Vars['temp'] = Temp Vars['salt'] = Salt Vars['u'] = Uvel Vars['v'] = Vvel Vars['zeta'] = sshr Vars['ubar'] = ubar Vars['vbar'] = vbar Vars['date'] = data['date'] if not useInd is False: #>------------------------------------------ Vars['depth'] = Zr Vars['depthu'] = Zu Vars['depthv'] = Zv Vars['dist'] = distr Vars['distu'] = distu Vars['distv'] = distv # -----------------------------------------------------------------< if retHorizAux: return Vars, horizAux else: return Vars
def data2roms(data, grd, sparams, **kargs): ''' Interpolates data to roms 3D grid. The dict data must contain the prognostic variables temp, salt, u, v (3d) and ssh (zeta, 2d), as well as lon, lat (2d), depth (1d) and time/date info: date (data date), date0 (reference date) and time (difference between date and date0). The input data can be provided by load_data. Parameters ---------- data : dict with prognostic variables grd : ROMS netcdf grid file sparams : s-coordinates parameters, theta_s,theta_b, hc and NLevels **kargs: ij : axis for vertical interpolations (*i,j) ij_ind : list of i or j index for vertical interpolation, all by default (ij_ind=False) horizAux : if True, the original data horizontally interpolated is returned and can be used for next data2roms call with this same karg quiet : output messages flag (false by default) ''' ij = 'j' ij_ind = False horizAux = False quiet = False if 'ij' in kargs.keys(): ij = kargs['ij'] if 'ij_ind' in kargs.keys(): ij_ind = kargs['ij_ind'] if 'horizAux' in kargs.keys(): horizAux = kargs['horizAux'] if 'quiet' in kargs.keys(): quiet = kargs['quiet'] if not quiet: print 'using grid %s' % grd g = roms.Grid(grd) xr, yr, hr, mr = g.vars('r') xu, yu, hu, mu = g.vars('u') xv, yv, hv, mv = g.vars('v') ny, nx = hr.shape nz = sparams[3] NX = data['NX'] NY = data['NY'] NZ = data['NZ'] if not quiet: print 'calc s levels...' sshr = calc.griddata(data['lon'], data['lat'], data['ssh'], xr, yr, extrap=True) Zr = g.s_levels(sparams, sshr, hr, 'r') Zu = g.s_levels(sparams, sshr, hr, 'u') Zv = g.s_levels(sparams, sshr, hr, 'v') # interp horiz: retHorizAux = horizAux is True if horizAux in (True, False): TEMP = np.ma.masked_all((NZ, ny, nx), data['temp'].dtype) SALT = np.ma.masked_all((NZ, ny, nx), data['salt'].dtype) U = np.ma.masked_all((NZ, ny, nx), data['u'].dtype) V = np.ma.masked_all((NZ, ny, nx), data['v'].dtype) if not quiet: print 'horizontal interpolation:' for i in range(NZ): if not quiet and i % 10 == 0: print ' lev %d of %d' % (i, NZ) #import pylab #pylab.figure() #pylab.pcolormesh(data['lon'],data['lat'],data['temp'][i,...]) try: TEMP[i, ...] = calc.griddata(data['lon'], data['lat'], data['temp'][i, ...], xr, yr, extrap=True) except: pass try: SALT[i, ...] = calc.griddata(data['lon'], data['lat'], data['salt'][i, ...], xr, yr, extrap=True) except: pass try: U[i, ...] = calc.griddata(data['lon'], data['lat'], data['u'][i, ...], xr, yr, extrap=True) except: pass try: V[i, ...] = calc.griddata(data['lon'], data['lat'], data['v'][i, ...], xr, yr, extrap=True) except: pass # rotate U,V: if not quiet: print 'rotating U,V to grid angle' angle = g.use('angle') # rad U, V = calc.rot2d(U, V, angle) U = rt.rho2uvp3d(U, 'u') V = rt.rho2uvp3d(V, 'v') horizAux = {} horizAux['TEMP'] = TEMP horizAux['SALT'] = SALT horizAux['U'] = U horizAux['V'] = V else: TEMP = horizAux['TEMP'] SALT = horizAux['SALT'] U = horizAux['U'] V = horizAux['V'] # interp vert: nxu = nx - 1 nyv = ny - 1 #> ----------------------------------------------------------------- useInd = not ij_ind is False if ij_ind is False: if ij == 'j': ij_ind = range(ny) elif ij == 'i': ij_ind = range(nx) else: try: iter(ij_ind) except: ij_ind = [ij_ind] if ij == 'j': ny = nyv = len(ij_ind) elif ij == 'i': nx = nxu = len(ij_ind) # -----------------------------------------------------------------< Temp = np.zeros((nz, ny, nx), data['temp'].dtype) Salt = np.zeros((nz, ny, nx), data['salt'].dtype) Uvel = np.zeros((nz, ny, nxu), data['u'].dtype) Vvel = np.zeros((nz, nyv, nx), data['v'].dtype) jslice = lambda x, ind: x[:, ind, :] islice = lambda x, ind: x[:, :, ind] ZZr = np.tile(data['depth'], (nx, ny, 1)).T ZZu = np.tile(data['depth'], (nxu, ny, 1)).T ZZv = np.tile(data['depth'], (nx, nyv, 1)).T if not useInd is False: #>------------------------------------------ if ij == 'j': slice = jslice sshr = sshr[ij_ind, :] hr = hr[ij_ind, :] elif ij == 'i': slice = islice sshr = sshr[:, ij_ind] hr = hr[:, ij_ind] Zr, Zu, Zv, TEMP, SALT, U, V = [ slice(k, ij_ind) for k in [Zr, Zu, Zv, TEMP, SALT, U, V] ] # -----------------------------------------------------------------< if useInd: # then store distances for a possible bry file dtype = Temp.dtype distr = np.zeros((nz, ny, nx), dtype) distu = np.zeros((nz, ny, nxu), dtype) distv = np.zeros((nz, nyv, nx), dtype) if not quiet: print 'vertical interpolation:' if ij == 'j': for j in range(ny): if not quiet and (ny < 10 or (ny >= 10 and j % 10 == 0)): print ' j=%3d of %3d' % (j, ny) ind = ij_ind[j] dr = np.tile(calc.distance(xr[ind, :], yr[ind, :]), (nz, 1)) du = np.tile(calc.distance(xu[ind, :], yu[ind, :]), (nz, 1)) Dr = np.tile(calc.distance(xr[ind, :], yr[ind, :]), (NZ, 1)) Du = np.tile(calc.distance(xu[ind, :], yu[ind, :]), (NZ, 1)) if useInd: distr[:, j, :] = dr distu[:, j, :] = du Temp[:, j, :] = calc.griddata(Dr, ZZr[:, j, :], TEMP[:, j, :], dr, Zr[:, j, :], extrap=True) Salt[:, j, :] = calc.griddata(Dr, ZZr[:, j, :], SALT[:, j, :], dr, Zr[:, j, :], extrap=True) if 0 and j % 10 == 0: print Dr.shape, ZZr[:, j, :].shape import pylab as pl pl.figure(1) pl.clf() pl.pcolormesh(Dr, ZZr[:, j, :], SALT[:, j, :]) pl.colorbar() clim = pl.gci().get_clim() pl.figure(2) pl.clf() pl.pcolormesh(dr, Zr[:, j, :], Salt[:, j, :]) pl.clim(clim) pl.colorbar() raw_input() Uvel[:, j, :] = calc.griddata(Du, ZZu[:, j, :], U[:, j, :], du, Zu[:, j, :], extrap=True) if j < Vvel.shape[1]: dv = np.tile(calc.distance(xv[ind, :], yv[ind, :]), (nz, 1)) Dv = np.tile(calc.distance(xv[ind, :], yv[ind, :]), (NZ, 1)) Vvel[:, j, :] = calc.griddata(Dv, ZZv[:, j, :], V[:, j, :], dv, Zv[:, j, :], extrap=True) if useInd: distv[:, j, :] = dv if np.any(np.isnan(Temp[:, j, :])): print 'found nan in temp', j if np.any(np.isnan(Salt[:, j, :])): print 'found nan in salt', j if np.any(np.isnan(Uvel[:, j, :])): print 'found nan in u', j if j < Vvel.shape[1] and np.any(np.isnan(Vvel[:, j, :])): print 'found nan in v', j elif ij == 'i': for i in range(nx): if not quiet and (nx < 10 or (nx >= 10 and i % 10 == 0)): print ' i=%3d of %3d' % (i, nx) ind = ij_ind[i] dr = np.tile(calc.distance(xr[:, ind], yr[:, ind]), (nz, 1)) dv = np.tile(calc.distance(xv[:, ind], yv[:, ind]), (nz, 1)) Dr = np.tile(calc.distance(xr[:, ind], yr[:, ind]), (NZ, 1)) Dv = np.tile(calc.distance(xv[:, ind], yv[:, ind]), (NZ, 1)) if useInd: distr[:, :, i] = dr distv[:, :, i] = dv Temp[:, :, i] = calc.griddata(Dr, ZZr[:, :, i], TEMP[:, :, i], dr, Zr[:, :, i], extrap=True) Salt[:, :, i] = calc.griddata(Dr, ZZr[:, :, i], SALT[:, :, i], dr, Zr[:, :, i], extrap=True) Vvel[:, :, i] = calc.griddata(Dv, ZZv[:, :, i], V[:, :, i], dv, Zv[:, :, i], extrap=True) if i < Uvel.shape[2]: du = np.tile(calc.distance(xu[:, ind], yu[:, ind]), (nz, 1)) Du = np.tile(calc.distance(xu[:, ind], yu[:, ind]), (NZ, 1)) Uvel[:, :, i] = calc.griddata(Du, ZZu[:, :, i], U[:, :, i], du, Zu[:, :, i], extrap=True) if useInd: distu[:, :, i] = du # uv bar: if not quiet: print 'calc uvbar' if useInd is False: ubar, vbar = rt.uvbar(Uvel, Vvel, sshr, hr, sparams) else: #>------------------------------------------------------------ sshu = calc.griddata(data['lon'], data['lat'], data['ssh'], xu, yu, extrap=True) sshv = calc.griddata(data['lon'], data['lat'], data['ssh'], xv, yv, extrap=True) if ij == 'j': sshu = sshu[ij_ind, :] sshv = sshv[ij_ind, :] hu = hu[ij_ind, :] hv = hv[ij_ind, :] elif ij == 'i': sshu = sshu[:, ij_ind] sshv = sshv[:, ij_ind] hu = hu[:, ij_ind] hv = hv[:, ij_ind] ubar = rt.barotropic(Uvel, sshu, hu, sparams) vbar = rt.barotropic(Vvel, sshv, hv, sparams) # -----------------------------------------------------------------< Vars = cb.odict() Vars['temp'] = Temp Vars['salt'] = Salt Vars['u'] = Uvel Vars['v'] = Vvel Vars['zeta'] = sshr Vars['ubar'] = ubar Vars['vbar'] = vbar Vars['date'] = data['date'] if not useInd is False: #>------------------------------------------ Vars['depth'] = Zr Vars['depthu'] = Zu Vars['depthv'] = Zv Vars['dist'] = distr Vars['distu'] = distu Vars['distv'] = distv # -----------------------------------------------------------------< if retHorizAux: return Vars, horizAux else: return Vars
def slicell(self,varname,X,Y,time=0,**opts): # x,y,z,v,m=[[]]*5 ######## plot= opts.get('plot',False) ######### ax = opts.get('ax',None) coords=opts.get('coords',self._default_coords('slicell')).split(',') data = opts.get('data',False) # dist = opts.get('dist',False) extrap = opts.get('extrap',False) # varOnly = opts.get('retv',False) maskLimit = opts.get('lmask',0.5) # points where interpolated mask are above # this value are considered as mask! # Most strict value is 0 out=Data() out.msg=self.check_slice(varname,t=time) if out.msg: return out#None,aux X=np.asarray(X) Y=np.asarray(Y) if X.ndim>1: X=np.squeeze(X) if Y.ndim>1: Y=np.squeeze(X) # if varname not in netcdf.varnames(self.name): # print ':: variable %s not found' % varname # return x,y,z,v,m # # if time>=self.TIME: # print 't = %d exceeds TIME dimension (%d)' % (time,self.TIME) # return x,y,z,v,m x,y,h,m=self.grid.vars(ruvp=self.var_at(varname)) if True: # extrat only portion of data needed: ## print 'start', x.shape,y.shape,X.shape i0,i1,j0,j1=calc.ij_limits(x, y, (X.min(),X.max()),(Y.min(),Y.max()), margin=1) ## print 'end' xi='%d:%d'%(i0,i1) eta='%d:%d'%(j0,j1) if data is False: V=self.use(varname,SEARCHtime=time,xi_SEARCH=xi,eta_SEARCH=eta) else: v=data[...,j0:j1,i0:i1] x=x[j0:j1,i0:i1] y=y[j0:j1,i0:i1] h=h[j0:j1,i0:i1] m=m[j0:j1,i0:i1] else: if data is False: V=self.use(varname,SEARCHtime=time) else: v=data if V.ndim==3: v=calc.griddata(x,y,V,X,Y,extrap=extrap,mask2d=m==0, keepMaskVal=maskLimit) elif V.ndim==2: v=calc.griddata(x,y,np.ma.masked_where(m==0,V),X,Y,extrap=extrap, keepMaskVal=maskLimit) # coords: if 'z' in coords and V.ndim==3: out.z=self.path_s_levels(time,X,Y,rw=varname[0]) if 'd' in coords: d=calc.distance(X,Y) if v.ndim==2: d=np.tile(d,(v.shape[0],1)) out.d=d if 'x' in coords: if v.ndim==2: X=np.tile(X,(v.shape[0],1)) out.x=X if 'y' in coords: if v.ndim==2: Y=np.tile(Y,(v.shape[0],1)) out.y=Y if 't' in coords: out.t=self.time[time] # # X, Y, dist: # if dist: # Dist=calc.distance(X,Y) # if v.ndim==3: # Dist=np.tile(Dist,(v.shape[0],1)) # else: # if v.ndim==3: # X=np.tile(X,(v.shape[0],1)) # Y=np.tile(Y,(v.shape[0],1)) # # if dist: # return Dist,Z,V # else: # return X,Y,Z,V # # if plot: # if not ax: ax=pl.gca() # if v.ndim==2: # if 'x' in coords: # p=ax.pcolormesh(out.x,out.z,v) ### ax.plot(aux.x[0],-h) # elif 'y' in coords: # p=ax.pcolormesh(out.y,out.z,v) ### ax.plot(aux.y[0],-h) # else: # p=ax.pcolormesh(out.d,out.z,v) #### ax.plot(aux.d[0],-h) ## # pl.colorbar(p,shrink=.7) ## elif v.ndim==1: # if 'x' in coords: # ax.plot(out.x,v) # elif 'y' in coords: # ax.plot(out.y,v) # else: # ax.plot(out.d,v) # out.v=v out.coordsReq=','.join(sorted(coords)) return out###v,aux
def slicej(self,varname,ind,time=0,**opts): coords=opts.get('coords',self._default_coords('slicej')).split(',') out=Data() out.msg=self.check_slice(varname,t=time,j=ind) if out.msg: return out v=self.use(varname,SEARCHtime=time,eta_SEARCH=ind) # add mask if not masked: if not np.ma.isMA(v): m=self.grid.vars(ruvp=self.var_at(varname),j=ind)[-1] if v.ndim==2: m=np.tile(m,(v.shape[0],1)) v=np.ma.masked_where(m==0,v) out.v=v out.info['v']['name']=varname out.info['v']['slice']='j=%d'%ind try: out.info['v']['units']=netcdf.vatt(self.name,varname,'units') except: pass # coords: if 'z' in coords and v.ndim==2: out.z=self.s_levels(time=time,ruvpw=self.var_at(varname),j=ind) out.info['z']=dict(name='Depth',units='m') if any([i in coords for i in 'xyd']): x,y,h,m=self.grid.vars(ruvp=self.var_at(varname),j=ind) if 'd' in coords: d=calc.distance(x,y) if d[-1]-d[0]>1e4: d=d/1000. dunits='km' else: dunits='m' if v.ndim==2: d=np.tile(d,(v.shape[0],1)) out.d=d out.info['d']=dict(name='Distance',units=dunits) if 'x' in coords: if v.ndim==2: x=np.tile(x,(v.shape[0],1)) out.x=x out.info['x']=dict(name='Longitude',units=r'$\^o$E') if 'y' in coords: if v.ndim==2: y=np.tile(y,(v.shape[0],1)) out.y=y out.info['y']=dict(name='Latitude',units=r'$\^o$N') if 't' in coords: out.t=self.time[time] if v.ndim==2: out.extra=[Data()] if 'd' in coords: out.extra[0].x=out.d[0] if 'x' in coords: out.extra[0].y=out.x[0] if 'y' in coords: out.extra[0].x=out.y[0] out.extra[0].v=-h out.extra[0].config['1d.plot']='fill_between' out.extra[0].config['1d.y0']=-h.max()-(h.max()-h.min())/20. out.extra[0].alias='bottom' out.coordsReq=','.join(sorted(coords)) return out