diff --git a/src/toargridding/grids.py b/src/toargridding/grids.py index afc47ed16d1b371f3b629506eb52840642991854..1bd176ffe3692f546d3c2e2bd5f943673d60eefd 100644 --- a/src/toargridding/grids.py +++ b/src/toargridding/grids.py @@ -105,12 +105,8 @@ class RegularGrid(GridDefinition): super().__init__() # TODO make sure only sensible resolutions - self.lat = Coordinate.from_resolution( - Coordinates.latitude, lat_resolution, min=-90, max=90, wraps=False - ) - self.lon = Coordinate.from_resolution( - Coordinates.longitude, lon_resolution, min=-180, max=180, wraps=True - ) + self.lat = Coordinate.from_resolution(Coordinates.latitude, lat_resolution, min=-90, max=90, wraps=False) + self.lon = Coordinate.from_resolution(Coordinates.longitude, lon_resolution, min=-180, max=180, wraps=True) spatial_shape = (self.lon.size, self.lat.size) spatial_size = self.lon.size * self.lat.size self.dims = [ @@ -119,9 +115,7 @@ class RegularGrid(GridDefinition): Coordinates.longitude.name, ] - self._as_xy_index = np.dstack( - np.meshgrid(range(self.lat.size), range(self.lon.size)) - ).reshape(-1, 2) + self._as_xy_index = np.dstack(np.meshgrid(range(self.lat.size), range(self.lon.size))).reshape(-1, 2) self._as_i_index = np.arange(spatial_size).reshape(spatial_shape).T @property @@ -141,17 +135,13 @@ class RegularGrid(GridDefinition): results of the request, including data, station coordinates and metadata of request """ - data_grouped_by_cell = self.group_data_by_cell( - data.stations_data, data.stations_coords - ) + data_grouped_by_cell = self.group_data_by_cell(data.stations_data, data.stations_coords) cell_statistics = self.get_cell_statistics(data_grouped_by_cell) dataset = self.create_dataset(cell_statistics, data.metadata) return dataset - def group_data_by_cell( - self, data: pd.DataFrame, coords: pd.DataFrame - ) -> DataFrameGroupBy: + def group_data_by_cell(self, data: pd.DataFrame, coords: pd.DataFrame) -> DataFrameGroupBy: """grouping of stations into cells This function converts the lat/lon coordinates of the stations into cell indices and groups stations belonging to one cell. @@ -167,9 +157,7 @@ class RegularGrid(GridDefinition): cell_indices = self.as_cell_index(coords) # will convert cell_indices to float as some nans ar present - data_with_indices = data.join( - cell_indices.to_frame(GridDefinition.cell_index_name), how="outer" - ) + data_with_indices = data.join(cell_indices.to_frame(GridDefinition.cell_index_name), how="outer") return data_with_indices.groupby(GridDefinition.cell_index_name) @@ -192,9 +180,7 @@ class RegularGrid(GridDefinition): return stats - def create_dataset( - self, cell_statistics: dict[str, pd.DataFrame], metadata: Metadata - ) -> xr.Dataset: + def create_dataset(self, cell_statistics: dict[str, pd.DataFrame], metadata: Metadata) -> xr.Dataset: """creation of data set and filling with results from the gridding Parameters: @@ -216,9 +202,7 @@ class RegularGrid(GridDefinition): gridded_ds = self.get_empty_grid(time, metadata) for variable, aggregated_data in cell_statistics.items(): - data_array_dict = self.get_data_array_dict( - time, aggregated_data, variable, metadata - ) + data_array_dict = self.get_data_array_dict(time, aggregated_data, variable, metadata) gridded_ds = gridded_ds.assign(data_array_dict) return gridded_ds @@ -248,9 +232,7 @@ class RegularGrid(GridDefinition): gridded_variable = Variable.from_data(gridded_data, variable, metadata) return {variable.name: gridded_variable.as_data_array(self.dims)} - def create_gridded_data( - self, time: Coordinate, grouped_timeseries: pd.DataFrame - ) -> np.array: + def create_gridded_data(self, time: Coordinate, grouped_timeseries: pd.DataFrame) -> np.array: """converts the available cell data to a full lat/lon-temporal data cube. Parameters: @@ -268,9 +250,7 @@ class RegularGrid(GridDefinition): values[...] = self.fill_value index = self._as_xy_index[grouped_timeseries.index.astype(int)] - values[:, index.T[0], index.T[1]] = grouped_timeseries.values.reshape( - -1, time.size - ).T + values[:, index.T[0], index.T[1]] = grouped_timeseries.values.reshape(-1, time.size).T return values @@ -278,17 +258,13 @@ class RegularGrid(GridDefinition): """converts coordinates of stations into x and y indices of the regular grid""" id_x = self.coord_to_index(coords[self.lat.name], self.lat.min, self.lat.step) - id_y = self.coord_to_index( - coords[self.lon.name], self.lon.min, self.lon.step, len(self.lon.data) - ) + id_y = self.coord_to_index(coords[self.lon.name], self.lon.min, self.lon.step, len(self.lon.data)) id_i = self._as_i_index[id_x, id_y] return pd.Series(id_i, index=id_x.index) - def coord_to_index( - self, coord: pd.Series, x0_axis: float, d_axis: float, maxBin4Wrap: int = None - ) -> np.array: + def coord_to_index(self, coord: pd.Series, x0_axis: float, d_axis: float, maxBin4Wrap: int = None) -> np.array: """converts a coordinate into a bin index on one axis Parameters: @@ -308,9 +284,7 @@ class RegularGrid(GridDefinition): ids[ids < 0] += maxBin4Wrap return ids - def get_empty_grid( - self, time: Variable, metadata: Metadata - ) -> xr.Dataset: # TODO make CF-compliant => docs + def get_empty_grid(self, time: Variable, metadata: Metadata) -> xr.Dataset: # TODO make CF-compliant => docs """creation of an empty dataset without data Sets up a dataset with its three axis: time, longitude and latitude.