Skip to content

Commit e972dde

Browse files
committed
Update history attribute
1 parent e4c5014 commit e972dde

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

cf_xarray/tracking.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import copy
66
import functools
7+
from datetime import datetime
78

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

2627

2728
def add_history(attrs, context):
28-
return {"history": None}
29+
"""Adds a history attribute following the NetCDF User Guide convention."""
30+
31+
# https://www.unidata.ucar.edu/software/netcdf/documentation/4.7.4-pre/attribute_conventions.html
32+
# A global attribute for an audit trail. This is a character array with a line
33+
# for each invocation of a program that has modified the dataset. Well-behaved
34+
# generic netCDF applications should append a line containing:
35+
# date, time of day, user name, program name and command arguments.
36+
37+
# nco uses the ctime format
38+
now = datetime.now().ctime()
39+
history = attrs[0].get("history", [])
40+
new_history = (
41+
f"{now}:"
42+
f" {context.func}(args)\n"
43+
# TODO: should we record software versions?
44+
)
45+
return {"history": history + [new_history]}
2946

3047

3148
def _tracker(
@@ -63,6 +80,8 @@ def track_cf_attributes(
6380
history: bool
6481
Adds a history attribute like NCO and follows the NUG convention.
6582
"""
83+
84+
# TODO: check xarray version here.
6685
return functools.partial(
6786
_tracker, strict=strict, cell_methods=cell_methods, history=history
6887
)

0 commit comments

Comments
 (0)