Commit 34fa6de6 authored by Clément Haëck's avatar Clément Haëck
Browse files

Add function to add metadata to netcdf files

with date of creation, commit of creation, script and args used.
parent 6c2f95d8
......@@ -6,9 +6,12 @@ import itertools
import os
import re
import sys
import subprocess
from datetime import datetime
from os import path
from typing import List
from typing import Any, Dict,List
import xarray as xr
# Scripts repository
try:
......@@ -158,6 +161,42 @@ def get_args(args, add_args=None, description=''):
return dargs
def setup_meta_attrs(ds: xr.Dataset,
args: Dict = None,
**kwargs: Any) -> xr.Dataset:
"""Set some dataset attributes with information on how it was created.
Attributes are:
- "created_with": filename of the python script used (ie `argv[0]`).
- "created_with_args": a string representing the `args` dictionnary,
- "created_on": date of creation
- "created_on_commit": if found, the current/HEAD commit hash.
completed by `kwargs`.
"""
ds.attrs["created_with"] = sys.argv[0]
if args is None:
args = {}
args_clean = args.copy()
args_clean.update(kwargs)
keys_to_remove = ['fix']
[args_clean.pop(k, None) for k in keys_to_remove]
ds.attrs["created_with_args"] = str(args_clean)
ds.attrs["created_on"] = datetime.today().strftime('%x %X')
cmd = ["git", "-C", root_code, "rev-parse", "HEAD"]
ret = subprocess.run(cmd, capture_output=True, text=True)
if ret.returncode == 0:
commit = ret.stdout.strip()
else:
commit = "not found"
ds.attrs["created_on_commit"] = commit
return ds
def progressbar(it, prefix="", size=60, file=sys.stdout):
count = len(it)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment