# In[ ]: import os.path from scipy.io import loadmat mat = os.path.join('..', 't_tide_v1.3beta', 't_constituents.mat') con_info = loadmat(mat, squeeze_me=True) con_info = con_info['const'] # I am ignore shallow water and sat constants! # In[ ]: from utide import _ut_constants_fname from utide.utilities import loadmatbunch con_info = loadmatbunch(_ut_constants_fname)['const'] # In[ ]: # Find the indices of the tidal constituents. k = 0 ind_nc, ind_ttide = [], [] const_name = [e.strip() for e in con_info['name'].tolist()] for name in consts: try: if name == 'STEADY': indx = const_name.index('Z0')
print('Size of boundary: ', len(bnd)) seg_types = [0] * len(bnd) adcirc.order_boundary(bnd, seg_types) adcirc.update( os.path.join(data_files_dir, 'vdatum', 'vdatum_fl_sab_adcirc54.nc')) names = [] const = adcirc.Dataset.variables['tidenames'][:] for name in const: names.append(''.join(name.tolist()).strip()) from utide import _ut_constants_fname from utide.utilities import loadmatbunch con_info = loadmatbunch(_ut_constants_fname)['const'] k = 0 ind_nc, ind_ttide = [], [] const_name = [e.strip() for e in con_info['name'].tolist()] consts = ['STEADY', 'M2', 'S2', 'N2', 'K1', 'O1', 'P1', 'M4', 'M6'] for name in consts: try: if name == 'STEADY': indx = const_name.index('Z0') else: indx = const_name.index(name) k += 1 ind_ttide.append(indx)
def get_tidal_vectors(self, layer, time, bbox, vector_scale=None, vector_step=None): vector_scale = vector_scale or 1 vector_step = vector_step or 1 with netCDF4.Dataset(self.topology_file) as nc: data_obj = nc.variables[layer.access_name] data_location = getattr(data_obj, 'location', 'node') mesh_name = data_obj.mesh ug = UGrid.from_nc_dataset(nc, mesh_name=mesh_name) coords = np.empty(0) if data_location == 'node': coords = ug.nodes elif data_location == 'face': coords = ug.face_coordinates elif data_location == 'edge': coords = ug.edge_coordinates lon = coords[:, 0] lat = coords[:, 1] padding_factor = calc_safety_factor(vector_scale) padding = calc_lon_lat_padding(lon, lat, padding_factor) * vector_step spatial_idx = data_handler.ugrid_lat_lon_subset_idx( lon, lat, bbox=bbox, padding=padding) tnames = nc.get_variables_by_attributes( standard_name='tide_constituent')[0] tfreqs = nc.get_variables_by_attributes( standard_name='tide_frequency')[0] from utide import _ut_constants_fname from utide.utilities import loadmatbunch con_info = loadmatbunch(_ut_constants_fname)['const'] # Get names from the utide constant file utide_const_names = [e.strip() for e in con_info['name'].tolist()] # netCDF4-python is returning ugly arrays of bytes... names = [] for n in tnames[:]: z = ''.join([x.decode('utf-8') for x in n.tolist() if x]).strip() names.append(z) if 'STEADY' in names: names[names.index('STEADY')] = 'Z0' extract_names = list( set(utide_const_names).intersection(set(names))) ntides = data_obj.shape[data_obj.dimensions.index('ntides')] extract_mask = np.zeros(shape=(ntides, ), dtype=bool) for n in extract_names: extract_mask[names.index(n)] = True if not spatial_idx.any() or not extract_mask.any(): e = np.ma.empty(0) return e, e, e, e ua = nc.variables['u'][extract_mask, spatial_idx] va = nc.variables['v'][extract_mask, spatial_idx] up = nc.variables['u_phase'][extract_mask, spatial_idx] vp = nc.variables['v_phase'][extract_mask, spatial_idx] freqs = tfreqs[extract_mask] omega = freqs * 3600 # Convert from radians/s to radians/hour. from utide.harmonics import FUV from matplotlib.dates import date2num v, u, f = FUV( t=np.array([date2num(time) + 366.1667]), tref=np.array([0]), lind=np.array( [utide_const_names.index(x) for x in extract_names]), lat= 55, # Reference latitude for 3rd order satellites (degrees) (55 is fine always) ngflgs=[0, 0, 0, 0]) # [NodsatLint NodsatNone GwchLint GwchNone] s = calendar.timegm(time.timetuple()) / 60 / 60. v, u, f = map(np.squeeze, (v, u, f)) v = v * 2 * np.pi # Convert phase in radians. u = u * 2 * np.pi # Convert phase in radians. U = (f * ua.T * np.cos(v + s * omega + u - up.T * np.pi / 180)).sum(axis=1) V = (f * va.T * np.cos(v + s * omega + u - vp.T * np.pi / 180)).sum(axis=1) return U, V, lon[spatial_idx], lat[spatial_idx]
def get_tidal_vectors(self, layer, time, bbox, vector_scale=None, vector_step=None): vector_scale = vector_scale or 1 vector_step = vector_step or 1 with netCDF4.Dataset(self.topology_file) as nc: data_obj = nc.variables[layer.access_name] data_location = getattr(data_obj, 'location', 'node') mesh_name = data_obj.mesh ug = UGrid.from_nc_dataset(nc, mesh_name=mesh_name) coords = np.empty(0) if data_location == 'node': coords = ug.nodes elif data_location == 'face': coords = ug.face_coordinates elif data_location == 'edge': coords = ug.edge_coordinates lon = coords[:, 0] lat = coords[:, 1] padding_factor = calc_safety_factor(vector_scale) padding = calc_lon_lat_padding(lon, lat, padding_factor) * vector_step spatial_idx = data_handler.ugrid_lat_lon_subset_idx(lon, lat, bbox=bbox, padding=padding) tnames = nc.get_variables_by_attributes(standard_name='tide_constituent')[0] tfreqs = nc.get_variables_by_attributes(standard_name='tide_frequency')[0] from utide import _ut_constants_fname from utide.utilities import loadmatbunch con_info = loadmatbunch(_ut_constants_fname)['const'] # Get names from the utide constant file utide_const_names = [ e.strip() for e in con_info['name'].tolist() ] # netCDF4-python is returning ugly arrays of bytes... names = [] for n in tnames[:]: z = ''.join([ x.decode('utf-8') for x in n.tolist() if x ]).strip() names.append(z) if 'STEADY' in names: names[names.index('STEADY')] = 'Z0' extract_names = list(set(utide_const_names).intersection(set(names))) ntides = data_obj.shape[data_obj.dimensions.index('ntides')] extract_mask = np.zeros(shape=(ntides,), dtype=bool) for n in extract_names: extract_mask[names.index(n)] = True if not spatial_idx.any() or not extract_mask.any(): e = np.ma.empty(0) return e, e, e, e ua = nc.variables['u'][extract_mask, spatial_idx] va = nc.variables['v'][extract_mask, spatial_idx] up = nc.variables['u_phase'][extract_mask, spatial_idx] vp = nc.variables['v_phase'][extract_mask, spatial_idx] freqs = tfreqs[extract_mask] omega = freqs * 3600 # Convert from radians/s to radians/hour. from utide.harmonics import FUV from matplotlib.dates import date2num v, u, f = FUV(t=np.array([date2num(time) + 366.1667]), tref=np.array([0]), lind=np.array([ utide_const_names.index(x) for x in extract_names ]), lat=55, # Reference latitude for 3rd order satellites (degrees) (55 is fine always) ngflgs=[0, 0, 0, 0]) # [NodsatLint NodsatNone GwchLint GwchNone] s = calendar.timegm(time.timetuple()) / 60 / 60. v, u, f = map(np.squeeze, (v, u, f)) v = v * 2 * np.pi # Convert phase in radians. u = u * 2 * np.pi # Convert phase in radians. U = (f * ua.T * np.cos(v + s * omega + u - up.T * np.pi / 180)).sum(axis=1) V = (f * va.T * np.cos(v + s * omega + u - vp.T * np.pi / 180)).sum(axis=1) return U, V, lon[spatial_idx], lat[spatial_idx]