class SGridAttributes(object): """ Class containing methods to help with getting the attributes for either SGrid. """ def __init__(self, nc, topology_dim, topology_variable): self.nc = nc self.ncd = NetCDFDataset(self.nc) self.topology_dim = topology_dim self.topology_variable = topology_variable self.topology_var = self.nc.variables[self.topology_variable] def get_dimensions(self): ds_dims = self.nc.dimensions grid_dims = [(ds_dim, len(ds_dims[ds_dim])) for ds_dim in ds_dims] return grid_dims def get_topology_var(self): grid_topology_var = find_grid_topology_var(self.nc) return grid_topology_var def get_attr_dimension(self, attr_name): try: attr_dim = getattr(self.topology_var, attr_name) except AttributeError: attr_dim = None attr_padding = None else: attr_dim_padding = parse_padding(attr_dim, self.topology_variable) attr_padding = attr_dim_padding return attr_dim, attr_padding def get_attr_coordinates(self, attr_name): try: attr_coordinates_raw = getattr(self.topology_var, attr_name) except AttributeError: location_name = attr_name.split('_')[0] attr_coordinates = self.ncd.find_coordinates_by_location( location_name, self.topology_dim) # noqa else: attr_coordinates_val = attr_coordinates_raw.split(' ') attr_coordinates = tuple(attr_coordinates_val) return attr_coordinates def get_node_coordinates(self): node_dims = self.topology_var.node_dimensions node_dimensions = node_dims try: node_coordinates = self.topology_var.node_coordinates except AttributeError: grid_cell_node_vars = self.ncd.find_node_coordinates( node_dimensions) # noqa node_coordinates = grid_cell_node_vars else: node_coordinate_val = node_coordinates.split(' ') node_coordinates = tuple(node_coordinate_val) return node_dimensions, node_coordinates def get_variable_attributes(self, sgrid): dataset_variables = [] grid_variables = [] nc_variables = self.nc.variables for nc_variable in nc_variables: nc_var = nc_variables[nc_variable] sgrid_var = SGridVariable.create_variable(nc_var, sgrid) setattr(sgrid, sgrid_var.variable, sgrid_var) dataset_variables.append(nc_var.name) if hasattr(nc_var, 'grid'): grid_variables.append(nc_var.name) sgrid.variables = dataset_variables sgrid.grid_variables = grid_variables def get_angles(self): angles = self.nc.variables.get('angle') if not angles: # FIXME: Get rid of pair_arrays. center_lon, center_lat = self.get_cell_center_lat_lon() cell_centers = pair_arrays(center_lon, center_lat) centers_start = cell_centers[..., :-1, :] centers_end = cell_centers[..., 1:, :] angles = calculate_angle_from_true_east(centers_start, centers_end) return angles def get_cell_center_lat_lon(self): try: grid_cell_center_lon_var, grid_cell_center_lat_var = self.get_attr_coordinates( 'face_coordinates') # noqa except TypeError: center_lat, center_lon = None, None else: center_lat = self.nc[grid_cell_center_lat_var] center_lon = self.nc[grid_cell_center_lon_var] return center_lon, center_lat def get_cell_node_lat_lon(self): try: node_lon_var, node_lat_var = self.get_node_coordinates()[1] except TypeError: node_lon, node_lat = None, None else: node_lat = self.nc[node_lat_var] node_lon = self.nc[node_lon_var] return node_lon, node_lat def get_cell_edge1_lat_lon(self): try: edge1_lon_var, edge1_lat_var = self.get_attr_coordinates( 'edge1_coordinates') except: edge1_lon, edge1_lat = None, None else: edge1_lon = self.nc[edge1_lon_var] edge1_lat = self.nc[edge1_lat_var] return edge1_lon, edge1_lat def get_cell_edge2_lat_lon(self): try: edge2_lon_var, edge2_lat_var = self.get_attr_coordinates( 'edge2_coordinates') except TypeError: edge2_lon, edge2_lat = None, None else: edge2_lon = self.nc[edge2_lon_var] edge2_lat = self.nc[edge2_lat_var] return edge2_lon, edge2_lat
def __init__(self, nc, topology_dim, topology_variable): self.nc = nc self.ncd = NetCDFDataset(self.nc) self.topology_dim = topology_dim self.topology_variable = topology_variable self.topology_var = self.nc.variables[self.topology_variable]
def test_find_variable_by_attr(wrf_sgrid): nc_ds = NetCDFDataset(wrf_sgrid) result = nc_ds.find_variables_by_attr(cf_role='grid_topology', topology_dimension=2) expected = ['grid'] assert result == expected
def test_find_variable_by_nonexistant_attr(wrf_sgrid): nc_ds = NetCDFDataset(wrf_sgrid) result = nc_ds.find_variables_by_attr(bird='tufted titmouse') assert result == []
def test_sgrid_compliant_check(roms_sgrid): nc_ds = NetCDFDataset(roms_sgrid) result = nc_ds.sgrid_compliant_file() assert result
def test_node_coordinates(wrf_sgrid): nc_ds = NetCDFDataset(wrf_sgrid) node_coordinates = nc_ds.find_node_coordinates( 'west_east_stag south_north_stag') # noqa assert node_coordinates is None
def test_find_variables_by_standard_name_none(roms_sgrid): nc_ds = NetCDFDataset(roms_sgrid) result = nc_ds.find_variables_by_attr(standard_name='some standard_name') assert result == []
def test_find_variables_by_standard_name(roms_sgrid): nc_ds = NetCDFDataset(roms_sgrid) result = nc_ds.find_variables_by_attr(standard_name='time') expected = ['time'] assert result == expected
def test_find_edge_coordinates_by_location(roms_sgrid): nc_ds = NetCDFDataset(roms_sgrid) result = nc_ds.find_coordinates_by_location('edge1', 2) expected = ('lon_u', 'lat_u') assert result == expected
def test_find_face_coordinates_by_location(roms_sgrid): nc_ds = NetCDFDataset(roms_sgrid) result = nc_ds.find_coordinates_by_location('face', 2) expected = ('lon_rho', 'lat_rho') assert result == expected
def test_finding_node_variables(roms_sgrid): nc_ds = NetCDFDataset(roms_sgrid) result = nc_ds.find_node_coordinates('xi_psi eta_psi') expected = ('lon_psi', 'lat_psi') assert result == expected