Skip to content

Add a datetime_to_365day method #6

@delgadom

Description

@delgadom

Every climate transformation I do requires changing the time (datetime64[ns]) dimension into a YYYYDDD 365-day calendar time (int64) dimension. This seems like a good candidate for admission into the toolbox.

Proposed API

The function should accept as inputs:

  • a dataset indexed by some time dimension and any other dimensions, with an arbitrary number of data variables, which may or may not be indexed by the time dimension.
  • the name of the time dimension, which should be a datetime-like object, and which may have leap-years and may be less than or greater than a full year
  • an inplace argument (default False)

The function should return:

  • a dataset indexed by some integer 365-day YYYYDDD time dimension with the same dimension name. * If inplace=True, the data should be modified in-place, and the function should return None

Suggested Code

def datetime_to_365_day(ds, dim='time', inplace=False):

    if inplace:
        result = ds
    else:
        result = ds.copy()

    result = result.loc[{
        'time': ~((result['time.month'] == 2) & (result['time.day'] == 29))}]

    # this needs to be changed!
    # currently, this only works for datasets with 1 year or less of data. If there is more
    # than one year of daily data, this method will fail.
    # note that the tricky part here is we want March 1 to become YYYY060 regardless
    # of whether it is a leap year.
    # result.coords[dim] = result[str(dim)+'.year']*1000 + np.arange(1, len(result[dim])+1)
    raise NotImplementedError

    if not inplace:
        return result

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions