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

eliminate string constants from processing

parent 54894844
Branches
Tags
1 merge request!11Creation of first beta release version
...@@ -14,7 +14,11 @@ from toargridding.toar_rest_client import ( ...@@ -14,7 +14,11 @@ from toargridding.toar_rest_client import (
STATION_LON, STATION_LON,
COORDS, COORDS,
) )
from toargridding.metadata import GLOBAL, LATITUDE, LONGITUDE, get_variable_attributes from toargridding.metadata import (
Variables,
get_variable_attributes,
get_global_attributes,
)
GridType = Enum("GridType", ["regular"]) GridType = Enum("GridType", ["regular"])
...@@ -108,13 +112,13 @@ class RegularGrid(GridDefinition): ...@@ -108,13 +112,13 @@ class RegularGrid(GridDefinition):
def get_cell_statistics(self, groups): def get_cell_statistics(self, groups):
stats = { stats = {
"mean": groups.mean(), Variables.mean.name: groups.mean(),
"std": groups.std(), Variables.std.name: groups.std(),
"n": groups.count(), Variables.n.name: groups.count(),
} }
idx = stats["n"] == 0 idx = stats[Variables.n.name] == 0
stats["mean"][idx] = self.fill_value stats[Variables.mean.name][idx] = self.fill_value
stats["std"][idx] = self.fill_value stats[Variables.std.name][idx] = self.fill_value
return stats return stats
...@@ -123,9 +127,9 @@ class RegularGrid(GridDefinition): ...@@ -123,9 +127,9 @@ class RegularGrid(GridDefinition):
time = pd.DatetimeIndex(random_df.columns) time = pd.DatetimeIndex(random_df.columns)
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, aggregated_timeseries in cell_statistics.items():
gridded_ds = self.assign_statistic_to_grid( gridded_ds = self.assign_statistic_to_grid(
gridded_ds, cell_statistic, grouped_timeseries, time gridded_ds, cell_statistic, aggregated_timeseries, time
) )
return gridded_ds return gridded_ds
...@@ -137,7 +141,16 @@ class RegularGrid(GridDefinition): ...@@ -137,7 +141,16 @@ class RegularGrid(GridDefinition):
metadata = get_variable_attributes(cell_statistic) metadata = get_variable_attributes(cell_statistic)
gridded_ds = gridded_ds.assign( # maybe better all in one ? (memory) gridded_ds = gridded_ds.assign( # maybe better all in one ? (memory)
{cell_statistic: (["latitude", "longitude", "time"], values)} {
cell_statistic: (
[
Variables.latitude.name,
Variables.longitude.name,
Variables.time.name,
],
values,
)
}
) )
return gridded_ds return gridded_ds
...@@ -172,9 +185,13 @@ class RegularGrid(GridDefinition): ...@@ -172,9 +185,13 @@ class RegularGrid(GridDefinition):
self, time: pd.DatetimeIndex, metadata: pd.DataFrame self, time: pd.DatetimeIndex, metadata: pd.DataFrame
) -> xr.Dataset: # TODO make CF-compliant => docs ) -> xr.Dataset: # TODO make CF-compliant => docs
coords = { coords = {
"longitude": self.lon, Variables.longitude.name: self.lon,
"latitude": self.lat, Variables.latitude.name: self.lat,
"time": time, Variables.time.name: time,
} }
return xr.Dataset(coords=coords, attrs=GLOBAL) ds = xr.Dataset(coords=coords, attrs=get_global_attributes(metadata))
ds.longitude.attrs = get_variable_attributes(Variables.longitude)
ds.latitude.attrs = get_variable_attributes(Variables.latitude)
return ds
from datetime import datetime from datetime import datetime
from enum import Enum
import numpy as np import numpy as np
date_created = datetime.utcnow().strftime("%Y-%m-dT%H:%M:%SZ") date_created = datetime.utcnow().strftime("%Y-%m-dT%H:%M:%SZ")
Variables = Enum("Variables", ["latitude", "longitude", "time", "mean", "std", "n"])
GLOBAL = { GLOBAL = {
"title": "foo", "title": "foo",
"date_created": date_created, "date_created": date_created,
...@@ -31,11 +34,11 @@ ENCODING = { ...@@ -31,11 +34,11 @@ ENCODING = {
"time": {"dtype": "int32", "_FillValue": None}, "time": {"dtype": "int32", "_FillValue": None},
"latitude": { "latitude": {
"dtype": "int32", "dtype": "int32",
"_FillValue": None, "_FillValue": -1000,
}, },
"longitude": { "longitude": {
"dtype": "int32", "dtype": "int32",
"_FillValue": None, "_FillValue": -1000,
}, },
"mean": { "mean": {
"dtype": "float32", "dtype": "float32",
...@@ -55,5 +58,13 @@ ENCODING = { ...@@ -55,5 +58,13 @@ ENCODING = {
} }
def get_variable_attributes(statistic): def get_global_attributes(metadata):
pass
def get_variable_attributes(variable: Variables):
pass
def get_variable_encoding(variable: Variables):
pass pass
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment