def extrap_at_mask(self,vname,time,quiet=1,**opts): if not quiet: print('Extrap at mask:\n loading') v=self.use(vname,SEARCHtime=time) x,y,h,m=self.grid.vars(vname) if self.hasz(vname): # 3d N=v.shape[0] for n in range(N): if not quiet: print(' extrap level %d of %d' % (n,N)) v[n,...]=calc.mask_extrap(x,y,np.ma.masked_where(m==0,v[n,...])) else: # 2d v=calc.mask_extrap(x,y,np.ma.masked_where(m==0,v)) return v
def extrap_at_mask(self, vname, time, quiet=1, **opts): if not quiet: print('Extrap at mask:\n loading') v = self.use(vname, SEARCHtime=time) x, y, h, m = self.grid.vars(vname) if self.hasz(vname): # 3d N = v.shape[0] for n in range(N): if not quiet: print(' extrap level %d of %d' % (n, N)) v[n, ...] = calc.mask_extrap( x, y, np.ma.masked_where(m == 0, v[n, ...])) else: # 2d v = calc.mask_extrap(x, y, np.ma.masked_where(m == 0, v)) return 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 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,time,loc='rr',i=False,j=False,k=False,extrapZeta=False): 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) if not np.ma.is_masked(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) zeta=rt.rho2uvp(zeta,hLoc) z=rt.s_levels(h,zeta,self.s_params,rw=vLoc) 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, 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 ww3gb_2TPAR(datafolder,date0,date1,romsgrid,nseg=(10,10),addxy=(0.001,0.001),dspr=20,path='',name='TPAR'): '''dspr, directional spreading; see https://www.myroms.org/forum/viewtopic.php?f=14&t=2335 ''' g=roms.Grid(romsgrid) # loading data: xlim=g.lon.min()-2,g.lon.max()+2 ylim=g.lat.min()-2,g.lat.max()+2 lon,lat,Time,Res=WW3Data(datafolder).data(date0,date1,xlim,ylim) # calc spec points: xy,ij=ww3_specpoints(romsgrid,nseg,addxy) # interpolate data at initial point of each segment: x=xy[:,0] y=xy[:,1] nt=len(Time) nv=len(Res) nx=xy.shape[0] TPAR=np.zeros((nt,nv,nx),'f') vnames='hs','tp','dp' # Significant_height_of_combined_wind_waves_and_swell # Primary_wave_mean_period # Primary_wave_direction for it in range(nt): if it%10==0: print 'time %03d of %03d : %s'%(it,nt,Time[it].isoformat(' ')) for iv in range(nv): vname=vnames[iv] #print ' -- %s'%vname v=Res[vname][it,...] # 1st extrap masked data: v=calc.mask_extrap(lon,lat,v) TPAR[it,iv,:]=calc.griddata(lon,lat,v,x,y) # create TPAR files: for i in range(nx): fname=name+'_seg%03d.txt'%i j=open(fname,'w') j.write('TPAR\n') for it in range(nt): # time as YYYYMMDD.HHMMSS tiso=Time[it].strftime('%Y%m%d.%H%M%S') j.write('%s %6.2f %6.2f %6.2f %6.2f\n'%(tiso,TPAR[it,0,i],TPAR[it,1,i],TPAR[it,2,i],dspr)) j.close() print 'created tpar file %s'%fname x1=xy[:,2] y1=xy[:,3] # add to swan INPUT: # BOUND SHAPESPEC JONSWAP PEAK DSPR DEGREES # BOUNDSPEC SEGMENT XY -54.3906 33.1171 -55.5271 35.8094 VARIABLE FILE 0 './forcings/TPAR2.txt' print '\n' print 'BOUND SHAPESPEC JONSWAP PEAK DSPR DEGREES' for i in range(nx): fname=os.path.join(path,name+'_seg%03d.txt'%i) print 'BOUNDSPEC SEGMENT XY %8.4f %8.4f %8.4f %8.4f VARIABLE FILE 0 \'%s\''%(x[i],y[i],x1[i],y1[i],fname) print '\n' i0=ij[:,0] j0=ij[:,1] i1=ij[:,2] j1=ij[:,3] for i in range(nx): fname=os.path.join(path,name+'_seg%03d.txt'%i) print 'BOUNDSPEC SEGMENT IJ %3d %3d %3d %3d VARIABLE FILE 0 \'%s\''%(i0[i],j0[i],i1[i],j1[i],fname) print '\n'
def ww3gb_2TPAR(datafolder, date0, date1, romsgrid, nseg=(10, 10), addxy=(0.001, 0.001), dspr=20, path='', name='TPAR'): '''dspr, directional spreading; see https://www.myroms.org/forum/viewtopic.php?f=14&t=2335 ''' g = roms.Grid(romsgrid) # loading data: xlim = g.lon.min() - 2, g.lon.max() + 2 ylim = g.lat.min() - 2, g.lat.max() + 2 lon, lat, Time, Res = WW3Data(datafolder).data(date0, date1, xlim, ylim) # calc spec points: xy, ij = ww3_specpoints(romsgrid, nseg, addxy) # interpolate data at initial point of each segment: x = xy[:, 0] y = xy[:, 1] nt = len(Time) nv = len(Res) nx = xy.shape[0] TPAR = np.zeros((nt, nv, nx), 'f') vnames = 'hs', 'tp', 'dp' # Significant_height_of_combined_wind_waves_and_swell # Primary_wave_mean_period # Primary_wave_direction for it in range(nt): if it % 10 == 0: print 'time %03d of %03d : %s' % (it, nt, Time[it].isoformat(' ')) for iv in range(nv): vname = vnames[iv] #print ' -- %s'%vname v = Res[vname][it, ...] # 1st extrap masked data: v = calc.mask_extrap(lon, lat, v) TPAR[it, iv, :] = calc.griddata(lon, lat, v, x, y) # create TPAR files: for i in range(nx): fname = name + '_seg%03d.txt' % i j = open(fname, 'w') j.write('TPAR\n') for it in range(nt): # time as YYYYMMDD.HHMMSS tiso = Time[it].strftime('%Y%m%d.%H%M%S') j.write( '%s %6.2f %6.2f %6.2f %6.2f\n' % (tiso, TPAR[it, 0, i], TPAR[it, 1, i], TPAR[it, 2, i], dspr)) j.close() print 'created tpar file %s' % fname x1 = xy[:, 2] y1 = xy[:, 3] # add to swan INPUT: # BOUND SHAPESPEC JONSWAP PEAK DSPR DEGREES # BOUNDSPEC SEGMENT XY -54.3906 33.1171 -55.5271 35.8094 VARIABLE FILE 0 './forcings/TPAR2.txt' print '\n' print 'BOUND SHAPESPEC JONSWAP PEAK DSPR DEGREES' for i in range(nx): fname = os.path.join(path, name + '_seg%03d.txt' % i) print 'BOUNDSPEC SEGMENT XY %8.4f %8.4f %8.4f %8.4f VARIABLE FILE 0 \'%s\'' % ( x[i], y[i], x1[i], y1[i], fname) print '\n' i0 = ij[:, 0] j0 = ij[:, 1] i1 = ij[:, 2] j1 = ij[:, 3] for i in range(nx): fname = os.path.join(path, name + '_seg%03d.txt' % i) print 'BOUNDSPEC SEGMENT IJ %3d %3d %3d %3d VARIABLE FILE 0 \'%s\'' % ( i0[i], j0[i], i1[i], j1[i], fname) print '\n'
def his2gnome(fname, his, grd=False, nomask=False, gshhsMask=True, xylim=False, dates=False, ij=(1, 1)): ''' Creates GNOME wind file Ex: his2gnome(out,his,grd,dates=dates,ij=(2,2)) if gshhsMask, the high res mask file mask_gshhs.npy will be created at 1st usage. Mask is based on high (h) resolution gshhs data which must be available (env variable GSHHS_MASK must be set). ''' if not grd: grd = his deta, dxi = ij dims = netcdf.fdim(his) xi, eta = dims['xi_rho'], dims['eta_rho'] xi0, eta0 = xi, eta nc0 = netcdf.ncopen(his) time = netcdf.nctime(nc0, 'ocean_time') # for roms agrif: #t=netcdf.use(nc0,'scrum_time') #time=netcdf.num2date(t,'seconds since %d-01-01' % year0) x0 = netcdf.use(grd, 'lon_rho') y0 = netcdf.use(grd, 'lat_rho') ang = netcdf.use(grd, 'angle') if not xylim is False: xlim = xylim[:2] ylim = xylim[2:] i1, i2, j1, j2 = calc.ij_limits(x0, y0, xlim, ylim) print(i1, i2, j1, j2) xi = i2 - i1 eta = j2 - j1 else: i1, i2 = 0, xi j1, j2 = 0, eta XI = '%d:%d:%d' % (i1, i2, dxi) ETA = '%d:%d:%d' % (j1, j2, deta) xi = len(range(i1, i2, dxi)) eta = len(range(j1, j2, deta)) # create file: create_uv(fname, xi, eta) nc = netcdf.ncopen(fname, 'a') for v0, v in ('lon_rho', 'lon'), ('lat_rho', 'lat'), ('mask_rho', 'mask'), ('h', 'depth'): print('filling %s with %s' % (v, v0)) nc.vars[v][:] = netcdf.use(grd, v0, xi_rho=XI, eta_rho=ETA) if nomask: print('NO MASK !!!') nc.vars['mask'][:] = 1 if gshhsMask: try: mask = np.load('mask_gshhs.npy') except: mask = 1 + 0 * netcdf.use(nc0, 'mask_rho', xi_rho=XI, eta_rho=ETA) mask = mask.astype('bool') x = netcdf.use(grd, 'lon_rho', xi_rho=XI, eta_rho=ETA) y = netcdf.use(grd, 'lat_rho', xi_rho=XI, eta_rho=ETA) from okean import gshhs axis = x.min(), x.max(), y.min(), y.max() g = gshhs.gshhs(axis, resolution='h', area_thresh=0., max_level=2, clip=True) for lon, lat, level in zip(g.lon, g.lat, g.level): if level == 1: # land print('mask ', lon.shape) i = calc.inpolygon(x, y, lon, lat) mask = mask & ~i mask.dump('mask_gshhs.npy') nc.vars['mask'][:] = mask x = x0[j1:j2:deta, i1:i2:dxi] y = y0[j1:j2:deta, i1:i2:dxi] ang = ang[j1:j2:deta, i1:i2:dxi] n = -1 for it in range(len(time)): if not dates is False: d0, d1 = dates if time[it] < d0 or time[it] >= d1: continue n += 1 U = np.zeros((eta0, xi0), 'f') V = np.zeros((eta0, xi0), 'f') nc.vars['time'][n] = netcdf.date2num(time[it], tunits) # for roms agrif: #u=netcdf.use(nc0,'u',time=it,s_rho=-1) #v=netcdf.use(nc0,'v',time=it,s_rho=-1) u = netcdf.use(nc0, 'u', ocean_time=it, s_rho=-1) v = netcdf.use(nc0, 'v', ocean_time=it, s_rho=-1) # mask extrap: print('mask extrap...') u = calc.mask_extrap(x0, y0, np.ma.masked_where(u == 0, u)) v = calc.mask_extrap(x0, y0, np.ma.masked_where(v == 0, v)) U[:, 1:-1] = 0.5 * (u[:, :-1] + u[:, 1:]) U[:, 0] = u[:, 0] U[:, -1] = u[:, -1] V[1:-1, :] = 0.5 * (v[:-1.:] + v[1:, :]) V[0, :] = v[0, :] V[-1, :] = v[-1, :] U = U[j1:j2, i1:i2] V = V[j1:j2, i1:i2] U = U[j1:j2:deta, i1:i2:dxi] V = V[j1:j2:deta, i1:i2:dxi] # rotate uv: print('rotating ...') U, V = calc.rot2d(U, V, -ang) print('filling uv', n, time[it]) nc.vars['u'][n, ...] = U nc.vars['v'][n, ...] = V nc.close() nc0.close()
def his2gnome(fname,his,grd=False,nomask=False,gshhsMask=True,xylim=False,dates=False,ij=(1,1)): ''' Creates GNOME wind file Ex: his2gnome(out,his,grd,dates=dates,ij=(2,2)) if gshhsMask, the high res mask file mask_gshhs.npy will be created at 1st usage. Mask is based on high (h) resolution gshhs data which must be available (env variable GSHHS_MASK must be set). ''' if not grd: grd=his deta,dxi=ij dims=netcdf.fdim(his) xi,eta=dims['xi_rho'],dims['eta_rho'] xi0,eta0=xi,eta nc0=netcdf.ncopen(his) time=netcdf.nctime(nc0,'ocean_time') # for roms agrif: #t=netcdf.use(nc0,'scrum_time') #time=netcdf.num2date(t,'seconds since %d-01-01' % year0) x0=netcdf.use(grd,'lon_rho') y0=netcdf.use(grd,'lat_rho') ang=netcdf.use(grd,'angle') if not xylim is False: xlim=xylim[:2] ylim=xylim[2:] i1,i2,j1,j2=calc.ij_limits(x0,y0,xlim,ylim) print i1,i2,j1,j2 xi=i2-i1 eta=j2-j1 else: i1,i2=0,xi j1,j2=0,eta XI ='%d:%d:%d' %(i1,i2,dxi) ETA ='%d:%d:%d' %(j1,j2,deta) xi=len(range(i1,i2,dxi)) eta=len(range(j1,j2,deta)) # create file: create_uv(fname,xi,eta) nc=netcdf.ncopen(fname,'a') for v0,v in ('lon_rho','lon'),('lat_rho','lat'),('mask_rho','mask'),('h','depth'): print 'filling %s with %s' % (v,v0) nc.vars[v][:]=netcdf.use(grd,v0,xi_rho=XI,eta_rho=ETA) if nomask: print 'NO MASK !!!' nc.vars['mask'][:]=1 if gshhsMask: try: mask=np.load('mask_gshhs.npy') except: mask=1+0*netcdf.use(nc0,'mask_rho',xi_rho=XI,eta_rho=ETA) mask=mask.astype('bool') x=netcdf.use(grd,'lon_rho',xi_rho=XI,eta_rho=ETA) y=netcdf.use(grd,'lat_rho',xi_rho=XI,eta_rho=ETA) from okean import gshhs axis=x.min(),x.max(),y.min(),y.max() g=gshhs.gshhs(axis, resolution='h',area_thresh=0., max_level=2,clip=True) for lon, lat, level in zip(g.lon, g.lat, g.level): if level == 1: # land print 'mask ',lon.shape i=calc.inpolygon(x,y,lon,lat) mask=mask & ~i mask.dump('mask_gshhs.npy') nc.vars['mask'][:]=mask x=x0[j1:j2:deta,i1:i2:dxi] y=y0[j1:j2:deta,i1:i2:dxi] ang=ang[j1:j2:deta,i1:i2:dxi] n=-1 for it in range(len(time)): if not dates is False: d0,d1=dates if time[it]<d0 or time[it]>=d1: continue n+=1 U=np.zeros((eta0,xi0),'f') V=np.zeros((eta0,xi0),'f') nc.vars['time'][n]=netcdf.date2num(time[it],tunits) # for roms agrif: #u=netcdf.use(nc0,'u',time=it,s_rho=-1) #v=netcdf.use(nc0,'v',time=it,s_rho=-1) u=netcdf.use(nc0,'u',ocean_time=it,s_rho=-1) v=netcdf.use(nc0,'v',ocean_time=it,s_rho=-1) # mask extrap: print 'mask extrap...' u=calc.mask_extrap(x0,y0,np.ma.masked_where(u==0,u)) v=calc.mask_extrap(x0,y0,np.ma.masked_where(v==0,v)) U[:,1:-1]=0.5*(u[:,:-1]+u[:,1:]) U[:,0]=u[:,0] U[:,-1]=u[:,-1] V[1:-1,:]=0.5*(v[:-1.:]+v[1:,:]) V[0,:]=v[0,:] V[-1,:]=v[-1,:] U=U[j1:j2,i1:i2] V=V[j1:j2,i1:i2] U=U[j1:j2:deta,i1:i2:dxi] V=V[j1:j2:deta,i1:i2:dxi] # rotate uv: print 'rotating ...' U,V=calc.rot2d(U,V,-ang) print 'filling uv', n, time[it] nc.vars['u'][n,...]=U nc.vars['v'][n,...]=V nc.close() nc0.close()