Skip to content
Snippets Groups Projects
Commit ebd4443d authored by Simon Grasse's avatar Simon Grasse
Browse files

refactor dataset writing

parent b70136d6
Branches
Tags
1 merge request!11Creation of first beta release version
...@@ -14,7 +14,7 @@ from toargridding.toar_rest_client import ( ...@@ -14,7 +14,7 @@ from toargridding.toar_rest_client import (
STATION_LON, STATION_LON,
COORDS, COORDS,
) )
from toargridding.metadata import GLOBAL, LATITUDE, LONGITUDE from toargridding.metadata import GLOBAL, LATITUDE, LONGITUDE, get_variable_attributes
GridType = Enum("GridType", ["regular"]) GridType = Enum("GridType", ["regular"])
...@@ -116,18 +116,36 @@ class RegularGrid(GridDefinition): ...@@ -116,18 +116,36 @@ class RegularGrid(GridDefinition):
gridded_ds = self.get_empty_grid(time, metadata) gridded_ds = self.get_empty_grid(time, metadata)
for cell_statistic, grouped_timeseries in cell_statistics.items(): for cell_statistic, grouped_timeseries in cell_statistics.items():
values = np.empty((self.lat.size, self.lon.size, time.size)) gridded_ds = self.assign_statistic_to_grid(
index = self._as_xy_index[grouped_timeseries.index] gridded_ds, cell_statistic, grouped_timeseries, time
values[index.T[0], index.T[1]] = grouped_timeseries.values.reshape(
-1, time.size
) )
gridded_ds = gridded_ds.assign( # maybe better all in one ? (memory) return gridded_ds
{cell_statistic: (["latitude", "longitude", "time"], values)}
) def assign_statistic_to_grid(
self, gridded_ds: xr.Dataset, cell_statistic, grouped_timeseries, time
):
values = self.create_gridded_statistics(time, grouped_timeseries)
metadata = get_variable_attributes(cell_statistic)
print(cell_statistic)
gridded_ds = gridded_ds.assign( # maybe better all in one ? (memory)
{cell_statistic: (["latitude", "longitude", "time"], values)}
)
return gridded_ds return gridded_ds
def create_gridded_statistics(self, time, grouped_timeseries):
values = np.empty((self.lat.size, self.lon.size, time.size))
index = self._as_xy_index[grouped_timeseries.index]
values[index.T[0], index.T[1]] = grouped_timeseries.values.reshape(
-1, time.size
)
return values
def as_cell_index(self, coords): def as_cell_index(self, coords):
id_x = self.coord_to_index( id_x = self.coord_to_index(
coords[STATION_LAT], self.lat.min(), self.lat_resolution coords[STATION_LAT], self.lat.min(), self.lat_resolution
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment