Skip to content

Commit

Permalink
Update history attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Aug 3, 2021
1 parent e4c5014 commit e972dde
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion cf_xarray/tracking.py
Expand Up @@ -4,6 +4,7 @@

import copy
import functools
from datetime import datetime

CELL_METHODS = {
"sum": "sum",
Expand All @@ -25,7 +26,23 @@ def add_cell_methods(attrs, context):


def add_history(attrs, context):
return {"history": None}
"""Adds a history attribute following the NetCDF User Guide convention."""

# https://www.unidata.ucar.edu/software/netcdf/documentation/4.7.4-pre/attribute_conventions.html
# A global attribute for an audit trail. This is a character array with a line
# for each invocation of a program that has modified the dataset. Well-behaved
# generic netCDF applications should append a line containing:
# date, time of day, user name, program name and command arguments.

# nco uses the ctime format
now = datetime.now().ctime()
history = attrs[0].get("history", [])
new_history = (
f"{now}:"
f" {context.func}(args)\n"
# TODO: should we record software versions?
)
return {"history": history + [new_history]}


def _tracker(
Expand Down Expand Up @@ -63,6 +80,8 @@ def track_cf_attributes(
history: bool
Adds a history attribute like NCO and follows the NUG convention.
"""

# TODO: check xarray version here.
return functools.partial(
_tracker, strict=strict, cell_methods=cell_methods, history=history
)

0 comments on commit e972dde

Please sign in to comment.