Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion modelskill/obs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""
from __future__ import annotations

from typing import Literal, Optional
from typing import Literal, Optional, Any, Union
import warnings
import pandas as pd
import xarray as xr
Expand All @@ -21,6 +21,9 @@
_parse_track_input,
)

# NetCDF attributes can only be str, int, float https://unidata.github.io/netcdf4-python/#attributes-in-a-netcdf-file
Serializable = Union[str, int, float]


def observation(
data: DataInputType,
Expand Down Expand Up @@ -104,6 +107,15 @@ def __init__(
super().__init__(data=data)
self.data.attrs["weight"] = weight

@property
def attrs(self) -> dict[str, Any]:
"""Attributes of the observation"""
return self.data.attrs

@attrs.setter
def attrs(self, value: dict[str, Serializable]) -> None:
self.data.attrs = value

@property
def weight(self) -> float:
"""Weighting factor for skill scores"""
Expand Down
14 changes: 12 additions & 2 deletions tests/observation/test_point_obs.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,22 @@ def test_attrs(klagshamn_filename):
o1 = ms.PointObservation(
klagshamn_filename, item=0, attrs={"a1": "v1"}, name="Klagshamn"
)
assert o1.data.attrs["a1"] == "v1"
assert o1.attrs["a1"] == "v1"

o1.attrs["a2"] = "v2"
assert o1.attrs["a2"] == "v2"

o2 = ms.PointObservation(
klagshamn_filename, item=0, attrs={"version": 42}, name="Klagshamn"
)
assert o2.data.attrs["version"] == 42
assert o2.attrs["version"] == 42

o2.attrs["version"] = 43
assert o2.attrs["version"] == 43

# remove all attributes and add a new one
o2.attrs = {"version": 44}
assert o2.attrs["version"] == 44


def test_attrs_non_serializable(klagshamn_filename):
Expand Down