diff --git a/cf_xarray/tracking.py b/cf_xarray/tracking.py index 8d11e08a..e559774a 100644 --- a/cf_xarray/tracking.py +++ b/cf_xarray/tracking.py @@ -4,6 +4,7 @@ import copy import functools +from datetime import datetime CELL_METHODS = { "sum": "sum", @@ -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( @@ -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 )