def smoothDepths(self): """ Smooth the data by running an interpolant over the model grid points """ print 'Smoothing the data...' if self.smoothmethod=='gaussian': print '\tUsing Gaussian filter...' Fsmooth = ufilter(self.xy, self.smoothrange) else: Fsmooth =interpXYZ(self.xy,self.xy,\ method=self.smoothmethod,NNear=self.smoothnear,vrange=self.smoothrange) self.grd.dv = Fsmooth(self.grd.dv)
def interp_depths(self): # Initialise the Interpolation class if not self.isDEM: print 'Building interpolant class...' self.F = interpXYZ(self.indata.XY,self.xy,method=self.interpmethod,NNear=self.NNear,\ p=self.p,varmodel=self.varmodel,nugget=self.nugget,sill=self.sill,vrange=self.vrange) # Interpolate the data print 'Interpolating data...' dv = self.F(self.indata.Zin)*self.scalefac else: print 'Interpolating DEM data...' dv = self.indata.interp(self.xy[:,0],self.xy[:,1])*self.scalefac if self.interpnodes: self.grd.dv = np.zeros_like(self.grd.xv) for nn in range(self.grd.Nc): self.grd.dv[nn] = np.max(dv[self.grd.cells[nn,0:self.grd.nfaces[nn] ] ]) #self.grd.dv[nn] = np.mean(dv[self.grd.cells[nn,0:self.grd.nfaces[nn] ] ]) else: self.grd.dv = dv
def extract_HC(modfile,lon,lat,z=None,conlist=None): """ Extract harmonic constituents from OTIS binary output and interpolate onto points in lon,lat set "z" to specifiy depth for transport to velocity conversion set "constituents" in conlist Returns: u_re, u_im, v_re, v_im, h_re, h_im, omega, conlist """ ### # Make sure the longitude is between 0 and 360 lon = np.mod(lon,360.0) ### # Read the filenames from the model file pathfile = os.path.split(modfile) path = pathfile[0] f = open(modfile,'r') hfile = path+'/' + f.readline().strip() uvfile = path+'/' + f.readline().strip() grdfile = path+'/' + f.readline().strip() f.close() ### # Read the grid file X,Y,depth, mask = read_OTPS_grd(grdfile) #X[X>180.0] = 180.0 - X[X>180.0] mask = mask == 1 # Create an interpolation object sz = lon.shape lon = lon.ravel() lat = lat.ravel() nx = lon.size F= interpXYZ(np.vstack((X[mask],Y[mask])).T,np.vstack((lon,lat)).T,method='idw',NNear=3,p=1.0) # Interpolate the model depths onto the points if z is none if z == None: z = F(depth[mask]) else: z = np.abs(z) # make sure they are positive ### # Check that the constituents are in the file conOTIS = get_OTPS_constits(hfile) if conlist == None: conlist = conOTIS for vv in conlist: if not vv in conOTIS: print 'Warning: constituent name: %s not present in OTIS file.'%vv conlist.remove(vv) ### # Now go through and read the data for each # Initialse the arrays ncon = len(conlist) u_re = np.zeros((ncon,nx)) u_im = np.zeros((ncon,nx)) v_re = np.zeros((ncon,nx)) v_im = np.zeros((ncon,nx)) h_re = np.zeros((ncon,nx)) h_im = np.zeros((ncon,nx)) omega = np.zeros((ncon,)) for ii, vv in enumerate(conlist): idx = otis_constits[vv]['index'] omega[ii] = otis_constits[vv]['omega'] print 'Interpolating consituent: %s...'%vv # Read and interpolate h X ,Y, tmp_h_re, tmp_h_im = read_OTPS_h(hfile,idx) h_re[ii,:] = F(tmp_h_re[mask]) h_im[ii,:] = F(tmp_h_im[mask]) # Read and interpolate u and v - Note the conversion from transport to velocity X ,Y, tmp_u_re, tmp_u_im, tmp_v_re, tmp_v_im = read_OTPS_UV(uvfile,idx) u_re[ii,:] = F(tmp_u_re[mask]) / z u_im[ii,:] = F(tmp_u_im[mask]) / z v_re[ii,:] = F(tmp_v_re[mask]) / z v_im[ii,:] = F(tmp_v_im[mask]) / z # Return the arrays in their original shape szout = (ncon,) + sz return u_re.reshape(szout), u_im.reshape(szout), v_re.reshape(szout), \ v_im.reshape(szout), h_re.reshape(szout), h_im.reshape(szout), omega, conlist
def extract_HC(modfile, lon, lat, z=None, conlist=None): """ Extract harmonic constituents from OTIS binary output and interpolate onto points in lon,lat set "z" to specifiy depth for transport to velocity conversion set "constituents" in conlist Returns: u_re, u_im, v_re, v_im, h_re, h_im, omega, conlist """ ### # Make sure the longitude is between 0 and 360 lon = np.mod(lon, 360.0) ### # Read the filenames from the model file pathfile = os.path.split(modfile) path = pathfile[0] f = open(modfile, 'r') hfile = path + '/' + f.readline().strip() uvfile = path + '/' + f.readline().strip() grdfile = path + '/' + f.readline().strip() f.close() ### # Read the grid file X, Y, depth, mask = read_OTPS_grd(grdfile) #X[X>180.0] = 180.0 - X[X>180.0] mask = mask == 1 # Create an interpolation object sz = lon.shape lon = lon.ravel() lat = lat.ravel() nx = lon.size F = interpXYZ(np.vstack((X[mask], Y[mask])).T, np.vstack((lon, lat)).T, method='idw', NNear=3, p=1.0) # Interpolate the model depths onto the points if z is none if z == None: z = F(depth[mask]) else: z = np.abs(z) # make sure they are positive ### # Check that the constituents are in the file conOTIS = get_OTPS_constits(hfile) if conlist == None: conlist = conOTIS for vv in conlist: if not vv in conOTIS: print('Warning: constituent name: %s not present in OTIS file.' % vv) conlist.remove(vv) ### # Now go through and read the data for each # Initialse the arrays ncon = len(conlist) u_re = np.zeros((ncon, nx)) u_im = np.zeros((ncon, nx)) v_re = np.zeros((ncon, nx)) v_im = np.zeros((ncon, nx)) h_re = np.zeros((ncon, nx)) h_im = np.zeros((ncon, nx)) omega = np.zeros((ncon, )) for ii, vv in enumerate(conlist): idx = otis_constits[vv]['index'] omega[ii] = otis_constits[vv]['omega'] print('Interpolating consituent: %s...' % vv) # Read and interpolate h X, Y, tmp_h_re, tmp_h_im = read_OTPS_h(hfile, idx) h_re[ii, :] = F(tmp_h_re[mask]) h_im[ii, :] = F(tmp_h_im[mask]) # Read and interpolate u and v - Note the conversion from transport to velocity X, Y, tmp_u_re, tmp_u_im, tmp_v_re, tmp_v_im = read_OTPS_UV( uvfile, idx) u_re[ii, :] = F(tmp_u_re[mask]) / z u_im[ii, :] = F(tmp_u_im[mask]) / z v_re[ii, :] = F(tmp_v_re[mask]) / z v_im[ii, :] = F(tmp_v_im[mask]) / z # Return the arrays in their original shape szout = (ncon, ) + sz return u_re.reshape(szout), u_im.reshape(szout), v_re.reshape(szout), \ v_im.reshape(szout), h_re.reshape(szout), h_im.reshape(szout), omega, conlist