-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add attribute tracking functionality
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
from . import tracking # noqa | ||
from .accessor import CFAccessor # noqa | ||
from .helpers import bounds_to_vertices, vertices_to_bounds # noqa | ||
from .options import set_options # noqa | ||
from .tracking import track_cf_attributes # noqa | ||
from .utils import _get_version | ||
|
||
__version__ = _get_version() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# This module provides functions for adding CF attribtues | ||
# and tracking history, provenance using xarray's keep_attrs | ||
# functionality | ||
|
||
import copy | ||
import functools | ||
|
||
CELL_METHODS = { | ||
"sum": "sum", | ||
"max": "maximum", | ||
"min": "minimum", | ||
"median": "median", | ||
"mean": "mean", | ||
"std": "standard_deviation", | ||
"var": "variance", | ||
} | ||
|
||
|
||
def add_cell_methods(attrs, context): | ||
"""Add appropriate cell_methods attribute.""" | ||
assert len(attrs) == 1 | ||
cell_methods = attrs[0].get("cell_methods", "") | ||
# TODO: get dim_name from context | ||
return {"cell_methods": f"dim_name: {CELL_METHODS[context.func]} {cell_methods}"} | ||
|
||
|
||
def add_history(attrs, context): | ||
return {"history": None} | ||
|
||
|
||
def _tracker( | ||
attrs, | ||
context, | ||
strict: bool = False, | ||
cell_methods: bool = True, | ||
history: bool = True, | ||
): | ||
|
||
# can only handle single variable attrs for now | ||
assert len(attrs) == 1 | ||
attrs_out = copy.deepcopy(attrs[0]) | ||
|
||
if context.func in CELL_METHODS: | ||
attrs_out.update(add_cell_methods(attrs, context)) | ||
if history: | ||
attrs_out.update(add_history(attrs, context)) | ||
pass | ||
return attrs_out | ||
|
||
|
||
def track_cf_attributes( | ||
*, strict: bool = False, cell_methods: bool = True, history: bool = True | ||
): | ||
"""Top-level user-facing function. | ||
Parameters | ||
---------- | ||
strict: bool | ||
Controls if an error is raised when an appropriate attribute cannot | ||
be added because of lack of information. | ||
cell_methods: bool | ||
Add cell_methods attribute when possible | ||
history: bool | ||
Adds a history attribute like NCO and follows the NUG convention. | ||
""" | ||
return functools.partial( | ||
_tracker, strict=strict, cell_methods=cell_methods, history=history | ||
) |