def load_vars(self,vars): var,nc=netcdf.var(self.name) varnames=var.keys() for i in vars: if not self.quiet: print(':: loading var ', i) if isinstance(i,dict): iname, iopts = i.items()[0] elif isinstance(i,basestring): iname=i iopts=i elif isinstance(i,tuple): iname=False iopts=i if not isinstance(iopts,tuple): iopts=(iopts,) found=0 if not hasattr(self,'var_as'): self.var_as={} for j in iopts: if not iname: iname=j if j in varnames: setattr(self,iname,var[j][:]) found=1 if not self.quiet: print(' - found %s as %s' % (iname,j)) # store original name and other info, like units try: units=var[j].atts['units'].value except: units='' self.var_as[iname]={'name':j,'units':units} break if not found and not self.quiet: print(':: %s not found in %s' % (str(iopts),self.name)) nc.close()
def load_vars(self,vars): var=netcdf.var(self.nc) for i in vars: if not self.quiet: print(':: loading var %s'%i) if isinstance(i,dict): iname, iopts = list(i.items())[0] elif cb.isstr(i): iname=i if iname.startswith('@'): iopts=iname[1:] else: iopts=iname if not isinstance(iopts,tuple): iopts=(iopts,) found=0 if not hasattr(self,'var_as'): self.var_as={} for j in iopts: if j in var: if iname.startswith('@'): # variable will be loaded only when needed! iname=iname[1:] # can't use self._get inside lambda function... j is always the last element! # so, instead of: # setattr(self.__class__, iname, property(fget=lambda self: self._get(j))) # a propVar variable is used, storing the riht j value: ob=propVar(j) setattr(self.__class__,iname,property(fget=ob.get))##lambda self: self.getx(vv))) else: setattr(self,iname,var[j][:]) found=1 if not self.quiet: print(' - found %s as %s'%(iname,j)) # store original name and other info, like units try: units=var[j].atts['units'].value except: units='' self.var_as[iname]={'name':j,'units':units} break if not found and not self.quiet: print(':: %s not found in %s'%(str(iopts),self.name))
def load_vars(self, vars): var, nc = netcdf.var(self.name) varnames = var.keys() for i in vars: if not self.quiet: print(':: loading var ', i) if isinstance(i, dict): iname, iopts = i.items()[0] elif isinstance(i, basestring): iname = i iopts = i elif isinstance(i, tuple): iname = False iopts = i if not isinstance(iopts, tuple): iopts = (iopts, ) found = 0 if not hasattr(self, 'var_as'): self.var_as = {} for j in iopts: if not iname: iname = j if j in varnames: setattr(self, iname, var[j][:]) found = 1 if not self.quiet: print(' - found %s as %s' % (iname, j)) # store original name and other info, like units try: units = var[j].atts['units'].value except: units = '' self.var_as[iname] = {'name': j, 'units': units} break if not found and not self.quiet: print(':: %s not found in %s' % (str(iopts), self.name)) nc.close()
def load_vars(self,vars): #self.quiet=0 var=netcdf.var(self.nc) varnames=var.keys() for i in vars: if not self.quiet: print ':: loading var ', i if isinstance(i,dict): iname, iopts = i.items()[0] elif isinstance(i,basestring): iname=i if iname.startswith('@'): iopts=iname[1:] else: iopts=iname if not isinstance(iopts,tuple): iopts=(iopts,) found=0 if not hasattr(self,'var_as'): self.var_as={} for j in iopts: if j in varnames: if iname.startswith('@'): iname=iname[1:] setattr(Common, iname, property(fget=lambda self: self.use(j))) # not working as expected! self.use is executed in the end, for last variable only! # check later... else: setattr(self,iname,var[j][:]) found=1 if not self.quiet: print ' - found %s as %s' % (iname,j) # store original name and other info, like units try: units=var[j].atts['units'].value except: units='' self.var_as[iname]={'name':j,'units':units} break if not found and not self.quiet: print ':: %s not found in %s' % (str(iopts),self.name)
def roms2swan_wind(frc,date0,date1,fname='swan_wind.dat',**kargs): tname='wind_time' uname='Uwind' vname='Vwind' grd=False # needed if wind is 1d dt=1 # hours path='' if 'tname' in kargs.keys(): tname=kargs['tname'] if 'uname' in kargs.keys(): uname=kargs['uname'] if 'vname' in kargs.keys(): vname=kargs['vname'] if 'grd' in kargs.keys(): grd =kargs['grd'] if 'dt' in kargs.keys(): dt =kargs['dt'] if 'path' in kargs.keys(): path =kargs['path'] print 'wind: loading time ...' time=netcdf.nctime(frc,tname) #time=np.load('tfile') #cond=(time>=date0)&(time<=date1) cond=(time>=date0)&(time<=date1+datetime.timedelta(days=1)) # add one day at the end, just to avoid the "repeating last" time=time[cond] d=np.diff(pl.date2num(time)) print 'current max and min dt = %6.2f %6.2f hrs = %6.2f %6.2f mins'%(d.max()*24, d.min()*24, d.max()*24*60, d.min()*24*60) # time=time[::dt] # d=np.diff(pl.date2num(time)) # print ' final max and min dt = %6.2f %6.2f hrs = %6.2f %6.2f mins'%(d.max()*24, d.min()*24, d.max()*24*60, d.min()*24*60) print 'wind: loading u ...' u,nc=netcdf.var(frc,uname) print 'wind: loading v ...' v,nc=netcdf.var(nc,uname) # u=u[cond,...][::dt,...] # v=v[cond,...][::dt,...] u=u[cond,...] v=v[cond,...] nc.close() if u.ndim==1: if not grd: print 'must provide grd karg!' return nt=u.size eta=netcdf.fdim(grd)['eta_rho'] xi=netcdf.fdim(grd)['xi_rho'] else: nt,eta,xi=u.shape # array may be too big, so do this later (for each it) # # u=np.tile(u[:,np.newaxis,np.newaxis],(1,eta,xi)) # v=np.tile(v[:,np.newaxis,np.newaxis],(1,eta,xi)) i=open(fname,'w') times=[] time0=time[0]-datetime.timedelta(hours=dt) ITs=[] for it in range(nt): time0=time0+datetime.timedelta(hours=dt) if time0>date1: break if time0>time[-1]: print 'Warning : repeating last ...', it times+=[time0] d=np.abs(time-time0) it=np.where(d==d.min())[0][0] ITs+=[it] if it%100==0: print 'saving u %s %s'%(fname,time[it].isoformat(' ')) if u[it,...].ndim==0: U=np.tile(u[it,...],(eta,xi)).flatten() else: U=u[it,...].flatten() [i.write('%8.4f\n'%uu) for uu in U] for it in ITs: if it%100==0: print 'saving v %s %s'%(fname,time[it].isoformat(' ')) if v[it,...].ndim==0: V=np.tile(v[it,...],(eta,xi)).flatten() else: V=v[it,...].flatten() [i.write('%8.4f\n'%vv) for vv in V] times=np.asarray(times) t0iso=times[0].strftime('%Y%m%d.%H%M%S') t1iso=times[-1].strftime('%Y%m%d.%H%M%S') dt=times[1]-times[0] dth=dt.days*24. + dt.seconds/60.**2 print ' -- created swan wind file %s\n'%fname # add to swan INPUT: print '\n' print 'INPGRID WIND CURVILINEAR 0 0 %d %d EXC 9.999000e+003 &'%(xi-1,eta-1) print ' NONSTATIONARY %s %.2f HR %s'%(t0iso,dth,t1iso) print 'READINP WIND 1 \'%s\' 4 0 FREE '%(os.path.join(path,fname)) print '\n'
def roms2swan_wind(frc, date0, date1, fname='swan_wind.dat', **kargs): tname = 'wind_time' uname = 'Uwind' vname = 'Vwind' grd = False # needed if wind is 1d dt = 1 # hours path = '' if 'tname' in kargs.keys(): tname = kargs['tname'] if 'uname' in kargs.keys(): uname = kargs['uname'] if 'vname' in kargs.keys(): vname = kargs['vname'] if 'grd' in kargs.keys(): grd = kargs['grd'] if 'dt' in kargs.keys(): dt = kargs['dt'] if 'path' in kargs.keys(): path = kargs['path'] print 'wind: loading time ...' time = netcdf.nctime(frc, tname) #time=np.load('tfile') #cond=(time>=date0)&(time<=date1) cond = (time >= date0) & ( time <= date1 + datetime.timedelta(days=1) ) # add one day at the end, just to avoid the "repeating last" time = time[cond] d = np.diff(pl.date2num(time)) print 'current max and min dt = %6.2f %6.2f hrs = %6.2f %6.2f mins' % ( d.max() * 24, d.min() * 24, d.max() * 24 * 60, d.min() * 24 * 60) # time=time[::dt] # d=np.diff(pl.date2num(time)) # print ' final max and min dt = %6.2f %6.2f hrs = %6.2f %6.2f mins'%(d.max()*24, d.min()*24, d.max()*24*60, d.min()*24*60) print 'wind: loading u ...' nc = netcdf.ncopen(frc) u = netcdf.var(nc, uname) print 'wind: loading v ...' v = netcdf.var(nc, uname) # u=u[cond,...][::dt,...] # v=v[cond,...][::dt,...] u = u[cond, ...] v = v[cond, ...] nc.close() if u.ndim == 1: if not grd: print 'must provide grd karg!' return nt = u.size eta = netcdf.fdim(grd)['eta_rho'] xi = netcdf.fdim(grd)['xi_rho'] else: nt, eta, xi = u.shape # array may be too big, so do this later (for each it) # # u=np.tile(u[:,np.newaxis,np.newaxis],(1,eta,xi)) # v=np.tile(v[:,np.newaxis,np.newaxis],(1,eta,xi)) i = open(fname, 'w') times = [] time0 = time[0] - datetime.timedelta(hours=dt) ITs = [] for it in range(nt): time0 = time0 + datetime.timedelta(hours=dt) if time0 > date1: break if time0 > time[-1]: print 'Warning : repeating last ...', it times += [time0] d = np.abs(time - time0) it = np.where(d == d.min())[0][0] ITs += [it] if it % 100 == 0: print 'saving u %s %s' % (fname, time[it].isoformat(' ')) if u[it, ...].ndim == 0: U = np.tile(u[it, ...], (eta, xi)).flatten() else: U = u[it, ...].flatten() [i.write('%8.4f\n' % uu) for uu in U] for it in ITs: if it % 100 == 0: print 'saving v %s %s' % (fname, time[it].isoformat(' ')) if v[it, ...].ndim == 0: V = np.tile(v[it, ...], (eta, xi)).flatten() else: V = v[it, ...].flatten() [i.write('%8.4f\n' % vv) for vv in V] times = np.asarray(times) t0iso = times[0].strftime('%Y%m%d.%H%M%S') t1iso = times[-1].strftime('%Y%m%d.%H%M%S') dt = times[1] - times[0] dth = dt.days * 24. + dt.seconds / 60.**2 print ' -- created swan wind file %s\n' % fname # add to swan INPUT: print '\n' print 'INPGRID WIND CURVILINEAR 0 0 %d %d EXC 9.999000e+003 &' % (xi - 1, eta - 1) print ' NONSTATIONARY %s %.2f HR %s' % (t0iso, dth, t1iso) print 'READINP WIND 1 \'%s\' 4 0 FREE ' % (os.path.join(path, fname)) print '\n'