home / github / issues

Menu
  • GraphQL API
  • Search all tables

issues: 809332917

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
809332917 MDU6SXNzdWU4MDkzMzI5MTc= 4914 Record processing steps into history attribute with context manager 13906519 open 0     4 2021-02-16T13:55:05Z 2022-05-23T13:28:48Z   NONE      

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

python 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:

```python 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))

...

```

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/4914/reactions",
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
    13221727 issue

Links from other tables

  • 1 row from issues_id in issues_labels
  • 4 rows from issue in issue_comments
Powered by Datasette · Queries took 154.991ms · About: xarray-datasette