4
4
5
5
import copy
6
6
import functools
7
+ from datetime import datetime
7
8
8
9
CELL_METHODS = {
9
10
"sum" : "sum" ,
@@ -25,7 +26,23 @@ def add_cell_methods(attrs, context):
25
26
26
27
27
28
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 ]}
29
46
30
47
31
48
def _tracker (
@@ -63,6 +80,8 @@ def track_cf_attributes(
63
80
history: bool
64
81
Adds a history attribute like NCO and follows the NUG convention.
65
82
"""
83
+
84
+ # TODO: check xarray version here.
66
85
return functools .partial (
67
86
_tracker , strict = strict , cell_methods = cell_methods , history = history
68
87
)
0 commit comments