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
......@@ -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()
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"] ='%x %X')
cmd = ["git", "-C", root_code, "rev-parse", "HEAD"]
ret =, capture_output=True, text=True)
if ret.returncode == 0:
commit = ret.stdout.strip()
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