def Calculate(lat, t0, tref, cons, typ=0): const = t_getconsts(np.array([])) Const = [con.decode('UTF-8') for con in const[0]['name']] consindex = [Const.index(con.ljust(4)) for con in cons] # Earth equilibrium at the beginning of the run [v, u, _] = t_vuf('nodal', np.array(t0), consindex, lat) tear = 360. * (v + u) # Nodel factor at the middl of the run [_, _, f] = t_vuf('nodal', np.array(tref), consindex, lat) tnf = f tfreq = (2 * np.pi) * const[0]['freq'][consindex] / 3600. talpha = const[0]['name'][consindex] if typ == 1: tpspec = np.abs(const[0]['doodsonspecies'][consindex]) tpamp = t_equilib(const[0]['freq'][consindex], const[0]['doodsonamp'][consindex], const[0]['doodsonspecies'][consindex], lat) return talpha, tpspec, tpamp, tfreq, tear, f else: return talpha, tfreq, tear, f
def extract_HC(consfile): """ Extract harmonic constituents 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 """ ### # Read the filenames from the model file pathfile = os.path.split(consfile) path = pathfile[0] f = Dataset(consfile, 'r') ds = Dataset(consfile) X = f.variables['lon_u'][:] Y = f.variables['lat_u'][:] rloni, rlati = np.meshgrid(X, Y) ### # Check that the constituents are in the file conList = [] conIDX = [] for ncon in range(0, len(f.variables['con'])): x = '' conList.append(''.join( [x + n.decode('UTF-8') for n in f.variables['con'][ncon].data])) conIDX.append(ncon) const = t_getconsts(np.array([])) Const = [con.decode('UTF-8') for con in const[0]['name']] consindex = [Const.index(con.ljust(4).upper()) for con in conList] tfreq = (2 * np.pi) * const[0]['freq'][consindex] / 3600. var = {} # interpolating to ROMS grid requires special care with phases!! # this is subjected to parallelization via muitiprocessing.Process - do it! # there is a sandbox in roms repo with a starting exercise Vars = ['u', 'v'] for var0 in Vars: var[var0] = np.ones(shape=(len(tfreq), 4, rloni.shape[0] * rloni.shape[1])) * -1e-8 N = -1 for con, ncon in zip(const[0]['name'][consindex], conIDX): N = N + 1 con = con.decode('UTF-8') print("extracting %s for %s" % (var0, con)) Uim = f.variables[var0 + "Im"][ncon] Ure = f.variables[var0 + "Re"][ncon] h = Ure + Uim * 1j amp = np.abs(h) pha = np.arctan2(-np.imag(h), np.real(h)) var[var0][N, 0, :] = amp.flatten() var[var0][N, 2, :] = pha.flatten() #*180./np.pi return var, tfreq, consindex, rloni.flatten(), rlati.flatten()
def get_uv_cons(self, btypes): self.ifcons = {} for b in range(0, len(btypes)): Lat = [] Lon = [] self.ifcons[b] = {} if (btypes[b]['ifltype']['value'] == 3) or (btypes[b]['ifltype']['value'] == 5): file = btypes[b]['ifltype']['filename'] var = btypes[b]['ifltype']['vars'] for node_i in self.nnode[b]: Lat.append(self.hgrid.latitude[int(node_i)]) Lon.append(self.hgrid.longitude[int(node_i)]) HC, tfreq, constidx = extract_HC(file, var, Lon, Lat, logger=self.logger) const = t_getconsts(np.array([])) for i, n in enumerate(constidx): self.ifcons[b][const[0]['name'][n].decode('UTF-8')] = {} for k in var: k = k.split('_')[0] self.ifcons[b][const[0]['name'][n].decode('UTF-8')][ k + '_amp'] = HC[k][i, 0, :] self.ifcons[b][const[0]['name'][n].decode('UTF-8')][ k + '_pha'] = np.mod(HC[k][i, 2, :], 360)
def Calculate(lat, t0, cons): const = t_getconsts(np.array([])) Const= [con.decode('UTF-8') for con in const[0]['name']] consindex = [Const.index(con.ljust(4)) for con in cons] # V: astronomical phase, U: nodal phase modulation, F: nodal amplitude correction v,u,f = t_vuf('nodal', np.array(t0), consindex, lat) tear = 360.*(v+u) tfreq = (2*np.pi)*const[0]['freq'][consindex]/3600. talpha = const[0]['name'][consindex] return talpha, tfreq, tear, f
def Calculate(mean_lat, Tstart, Cons): const = t_getconsts(np.array([])) itemindex = [] for k in range(0, len(Cons)): itemindex.append(np.where(const[0]['name'] == Cons[k].ljust(4))[0][0]) # Earth equilibrium at the beginning of the run [v, u, f] = t_vuf('nodal', Tstart, itemindex, mean_lat) tear = 360. * (v + u) # Nodel factor at the middl of the run [v, u, f] = t_vuf('nodal', Tstart, itemindex, mean_lat) tnf = f tfreq = 2. * np.pi * const[0]['freq'][itemindex] / 3600. talpha = const[0]['name'][itemindex] return talpha, tfreq, tear, tnf
def extract_HC(consfile, Vars, lim, conlist=None, min_depth=1): """ Extract harmonic constituents 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 """ ### # Read the filenames from the model file pathfile = os.path.split(consfile) path = pathfile[0] f = Dataset(consfile, 'r') ### # Read the grid file X = f.variables['Easting'][:] Y = f.variables['Northing'][:] if 'Depth' in f.variables: Z = f.variables['Depth'][:] else: Z = f.variables['dep'][:] X, Y = transform_proj(X, Y, in_espg=2193, out_espg=WGS84) gd = (X >= lim[0]) & (X <= lim[1]) & (Y >= lim[2]) & (Y <= lim[3]) & ( Z >= min_depth) X = X[gd] Y = Y[gd] Z = Z[gd] ### # Check that the constituents are in the file conList = [] conIDX = [] if conlist is not None: for ncon in range(0, len(f.variables['cons'])): if ''.join([ y.decode('UTF-8') for y in f.variables['cons'][ncon] if y != b' ' ]) in conlist: x = '' conList.append(''.join([ x + n.decode('UTF-8') for n in f.variables['cons'][ncon] ])) conIDX.append(ncon) else: for ncon in range(0, len(f.variables['cons'])): x = '' conList.append(''.join([ x + n.decode('UTF-8') for n in f.variables['cons'][ncon].data ])) conIDX.append(ncon) const = t_getconsts(np.array([])) Const = [con.decode('UTF-8') for con in const[0]['name']] consindex = [Const.index(con.ljust(4)) for con in conList] tfreq = (2 * np.pi) * const[0]['freq'][consindex] / 3600. if 'lev' in f.variables: levs = f.variables['lev'][:] Ilev = (levs == 0).nonzero()[0] if len(Ilev) == 0: print('Lev %f not found' % lev) Ilev = 0 else: Ilev = Ilev[0] else: Ilev = 0 var = {} # interpolating to ROMS grid requires special care with phases!! # this is subjected to parallelization via muitiprocessing.Process - do it! # there is a sandbox in roms repo with a starting exercise for var0 in Vars: var[var0] = np.ones(shape=(len(tfreq), 4, len(X))) * -1e-8 N = -1 for con, ncon in zip(const[0]['name'][consindex], conIDX): N = N + 1 con = con.decode('UTF-8') print("extracting %s for %s" % (var0, con)) amp = f.variables[var0 + "_amp"][ncon] pha = f.variables[var0 + "_pha"][ncon] if len(amp.shape) > 1: amp = amp[Ilev, gd] pha = pha[Ilev, gd] else: amp = amp[gd] pha = pha[gd] var[var0][N, 0, :] = amp var[var0][N, 2, :] = pha #*180./np.pi return var, tfreq, consindex, X, Y, Z
def create_filetide_var(filetide, t0, vari, cons, X, Y, depth, nv, do_variance, lev): FillValue = -99999 ncons = len(cons) nconstr = 4 ncout = netCDF4.Dataset(filetide, 'w') # dimensions dcons = ncout.createDimension('cons', ncons) dconstr = ncout.createDimension('constr', nconstr) dnode = ncout.createDimension('nSCHISM_hgrid_node', len(depth)) dnface = ncout.createDimension('nMaxSCHISM_hgrid_face_nodes', 4) dface = ncout.createDimension('nSCHISM_hgrid_face', nv.shape[0]) if lev is not None: dlev = ncout.createDimension('lev', len(lev)) # write variable vcons = ncout.createVariable('cons', 'S1', dimensions=('cons', 'constr')) vcons.long_name = 'Tide constituents' vcons.standard_name = 'tide constituents' vX = ncout.createVariable('Easting', np.float64, ('nSCHISM_hgrid_node', )) vY = ncout.createVariable('Northing', np.float64, ('nSCHISM_hgrid_node', )) vD = ncout.createVariable('Depth', np.float64, ('nSCHISM_hgrid_node', )) vN = ncout.createVariable('ele', np.float64, ( 'nSCHISM_hgrid_face', 'nMaxSCHISM_hgrid_face_nodes', )) vfreq = ncout.createVariable('freq', np.float64, ('cons', )) vfreq.long_name = 'freq' if lev is not None: vlev = ncout.createVariable('lev', np.float64, ('lev', )) all_mean = {} all_exp = {} all_amp = {} all_pha = {} for nvar in vari: if nvar is 'u' or nvar is 'v': all_amp[nvar] = ncout.createVariable( nvar + '_amp', np.float64, ('cons', 'lev', 'nSCHISM_hgrid_node')) all_amp[nvar].fill_value = FillValue all_pha[nvar] = ncout.createVariable(nvar + '_pha', np.float64, ( 'cons', 'lev', 'nSCHISM_hgrid_node', )) all_pha[nvar].fill_value = FillValue all_pha[nvar].units = 'radians' if do_variance is True: all_mean[nvar] = ncout.createVariable(nvar + '_mean', np.float64, ( 'lev', 'nSCHISM_hgrid_node', )) all_mean[nvar].fill_value = FillValue all_exp[nvar] = ncout.createVariable(nvar + '_var', np.float64, ( 'lev', 'nSCHISM_hgrid_node', )) all_exp[nvar].fill_value = FillValue else: all_amp[nvar] = ncout.createVariable(nvar + '_amp', np.float64, ( 'cons', 'nSCHISM_hgrid_node', )) all_amp[nvar].fill_value = FillValue all_pha[nvar] = ncout.createVariable(nvar + '_pha', np.float64, ( 'cons', 'nSCHISM_hgrid_node', )) all_pha[nvar].fill_value = FillValue all_pha[nvar].units = 'radians' if do_variance is True: all_mean[nvar] = ncout.createVariable(nvar + '_mean', np.float64, ('nSCHISM_hgrid_node', )) all_mean[nvar].fill_value = FillValue all_exp[nvar] = ncout.createVariable(nvar + '_var', np.float64, ('nSCHISM_hgrid_node', )) all_exp[nvar].fill_value = FillValue for j in range(0, ncons): for k in range(0, len(cons[j])): vcons[j, k] = cons[j][k] # write V0, freq const, sat, cshallow = t_getconsts(np.array([t0])) freq = np.zeros(shape=(len(cons), 1)) i = 0 for ic in const['name']: if ic in cons: IDX = np.nonzero(ic == cons)[0][0] freq[IDX] = 2 * np.pi * 24 * const['freq'][i] i = i + 1 vfreq[:] = freq # for nvar in vari: # all_amp[nvar][:]=np.zeros([len(depth),ncons]) # all_pha[nvar][:]=np.zeros([len(depth),ncons]) vX[:] = X vY[:] = Y vD[:] = depth vN[:] = nv if lev is not None: vlev[:] = np.abs(lev) return ncout, all_amp, all_pha, all_mean, all_exp
def extract_HC(modfile, Vars, lon, lat, conlist=None, logger=None): """ Extract harmonic constituents 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 """ ### lon = np.asarray(lon) lat = np.asarray(lat) # 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 = Dataset(modfile, 'r') ### # Read the grid file X = np.mod(f.variables['lon'][:], 360) Y = f.variables['lat'][:] if len(X.shape) == 1: X, Y = np.meshgrid(X, Y) if 'dep' in f.variables: depth = f.variables['dep'][:] else: depth = np.zeros((X.shape)) ### # Check that the constituents are in the file conList = [] conIDX = [] if conlist is not None: for ncon in range(0, len(f.variables['cons'])): if f.variables['cons'][ncon][:] in conlist: x = '' conList.append(''.join( [x + n for n in f.variables['cons'][ncon]])) conIDX.append(ncon) else: for ncon in range(0, len(f.variables['cons'])): x = '' try: conList.append(''.join([ x + n.decode('UTF-8') for n in f.variables['cons'][ncon].data ])) except: conList.append(''.join( [x + n for n in f.variables['cons'][ncon]])) conIDX.append(ncon) const = t_getconsts(np.array([])) Const = [con.decode('UTF-8') for con in const[0]['name']] consindex = [Const.index(con.ljust(4).upper()) for con in conList] tfreq = (2 * np.pi) * const[0]['freq'][consindex] / 3600. ### # Now go through and read the data for each maskr = [] Vars = list(set([x.replace('_amp', '').replace('_pha', '') for x in Vars])) var = {} # interpolating to ROMS grid requires special care with phases!! # this is subjected to parallelization via muitiprocessing.Process - do it! # there is a sandbox in roms repo with a starting exercise for var0 in Vars: var[var0] = np.ones(shape=(len(tfreq), 4, len(lon))) * -1e-8 N = -1 for con, ncon in zip(const[0]['name'][consindex], conIDX): N = N + 1 con = con.decode('UTF-8') logger.info("Interpolating %s for %s" % (var0, con)) var[var0][N, 0, :] = extrapolate_amp( X, Y, lon, lat, maskr, f.variables[var0 + "_amp"][ncon, :, :]) var[var0][N, 2, :] = extrapolate_pha( X, Y, lon, lat, maskr, f.variables[var0 + "_pha"][ncon, :, :]) * 180. / np.pi return var, tfreq, consindex