pydata / pydata/xarray

Record processing steps into history attribute with context manager

Open
#4,914 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-metadata
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

I often want to record an entry into history of my netcdf file/ xarray. While one can always add it manually, i.e.

ds.attrs["history"] = ds.attrs["history"] + "\n" + "message"

I was wondering if there's a better way... In a first attempt I tried using a context manager for this. Not sure if there are other approaches? Would that be something useful for xarray core? What are other people using for this?

Demo:

import datetime
import xarray as xr

class XrHistory():

    def __init__(self, array, message, timestamp=True):
        self._array = array
        self._message = message
        self._timestamp = timestamp

    def __enter__(self):
        if 'history' not in self._array.attrs:
            self._array.attrs['history'] = ""
        
        if self._message != self._array.attrs['history'].split('\n')[-1]:
            ts = f"{datetime.datetime.now().strftime('%a %b %d %H:%M:%S %Y')}: " if self._timestamp else ""
            
            self._array.attrs['history'] += f"\n{ts}{self._message}"
            self._message = None
        return self._array
    
    def __exit__(self, exc_type,exc_value, exc_traceback):
        pass

# ds is any xarray dataset...

with XrHistory(ds, "normalise data") as ds:
    ds["array_one"] = (ds.array_one - ds.array_one.mean(dim='time')) / ds.array_one.std(dim='time')

with XrHistory(ds, "subset data") as ds:
    ds = ds.sel(x=slice(10, 20), y=slice(10,20))

# ...

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the issue's context-manager demo and the xarray Dataset attrs/history behavior it describes. No source file or test is named, and the requested API and alternatives remain open questions; done would require an agreed design, implementation, and tests for recording history entries.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.