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

improve creation of gridded data

parent 1a25b13d
Branches
Tags
1 merge request!11Creation of first beta release version
...@@ -7,16 +7,17 @@ import xarray as xr ...@@ -7,16 +7,17 @@ import xarray as xr
date_created = datetime.utcnow().strftime("%Y-%m-dT%H:%M:%SZ") date_created = datetime.utcnow().strftime("%Y-%m-dT%H:%M:%SZ")
DATA_VARIABLES = ["latitude", "longitude", "time"] COORDINATE_VARIABLES = ["latitude", "longitude", "time"]
COORDINATE_VARIABLES = ["mean", "std", "n"] DATA_VARIABLES = ["mean", "std", "n"]
DataVariables = Enum("DataVariables", DATA_VARIABLES) DataVariables = Enum("DataVariables", DATA_VARIABLES)
Coordinates = Enum("Coordinates", COORDINATE_VARIABLES)
Variables = Enum("Variables", [*DATA_VARIABLES, *COORDINATE_VARIABLES]) Variables = Enum("Variables", [*DATA_VARIABLES, *COORDINATE_VARIABLES])
@dataclass @dataclass
class Variable: class Variable:
name: str var: Variables
data: np.ndarray data: np.ndarray
attributes: dict[str, str] attributes: dict[str, str]
encoding: dict[str, str] encoding: dict[str, str]
...@@ -24,15 +25,19 @@ class Variable: ...@@ -24,15 +25,19 @@ class Variable:
@classmethod @classmethod
def from_data(cls, data, variable: Variables, **kwargs): def from_data(cls, data, variable: Variables, **kwargs):
metadata = Variable.get_metadata(variable) metadata = Variable.get_metadata(variable)
return cls(variable.name, data, **metadata, **kwargs) return cls(variable, data, **metadata, **kwargs)
@staticmethod @staticmethod
def get_metadata(variable: Variables): def get_metadata(variable: Variables):
return VARIABLE_METADATA[variable] return VARIABLE_METADATA[variable.name]
def as_data_array(self, dims): def as_data_array(self, dims):
return xr.DataArray(self.data, name=self.name, dims=dims, attrs=self.attributes) return xr.DataArray(self.data, name=self.name, dims=dims, attrs=self.attributes)
@property
def name(self):
return self.var.name
@property @property
def size(self): def size(self):
return self.data.size return self.data.size
...@@ -74,7 +79,7 @@ def get_variable_attributes(variable: Variables): ...@@ -74,7 +79,7 @@ def get_variable_attributes(variable: Variables):
VARIABLE_METADATA = { VARIABLE_METADATA = {
Variables.latitude: { Variables.latitude.name: {
"attributes": { "attributes": {
"standard_name": "latitude", "standard_name": "latitude",
"long_name": "latitude in degrees north", "long_name": "latitude in degrees north",
...@@ -86,7 +91,7 @@ VARIABLE_METADATA = { ...@@ -86,7 +91,7 @@ VARIABLE_METADATA = {
"_FillValue": -1000, "_FillValue": -1000,
}, },
}, },
Variables.longitude: { Variables.longitude.name: {
"attributes": { "attributes": {
"standard_name": "longitude", "standard_name": "longitude",
"long_name": "longitude in degrees east", "long_name": "longitude in degrees east",
...@@ -98,7 +103,7 @@ VARIABLE_METADATA = { ...@@ -98,7 +103,7 @@ VARIABLE_METADATA = {
"_FillValue": -1000, "_FillValue": -1000,
}, },
}, },
Variables.time: { Variables.time.name: {
"attributes": { "attributes": {
"units": "time since ...", "units": "time since ...",
"long_name": "time", "long_name": "time",
...@@ -107,11 +112,11 @@ VARIABLE_METADATA = { ...@@ -107,11 +112,11 @@ VARIABLE_METADATA = {
}, },
"encoding": {"dtype": "int32", "_FillValue": None}, "encoding": {"dtype": "int32", "_FillValue": None},
}, },
Variables.mean: { Variables.mean.name: {
"attributes": {}, "attributes": {},
"encoding": {"dtype": "float32", "_FillValue": np.nan, "zlib": False}, "encoding": {"dtype": "float32", "_FillValue": np.nan, "zlib": False},
}, },
Variables.std: { Variables.std.name: {
"attributes": {}, "attributes": {},
"encoding": { "encoding": {
"dtype": "float32", "dtype": "float32",
...@@ -119,7 +124,7 @@ VARIABLE_METADATA = { ...@@ -119,7 +124,7 @@ VARIABLE_METADATA = {
"zlib": False, "zlib": False,
}, },
}, },
Variables.n: { Variables.n.name: {
"attributes": {}, "attributes": {},
"encoding": { "encoding": {
"dtype": "int32", "dtype": "int32",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment