0

I have a datacube opened with Xarray, which has several variables and a time vector ("mid_date", dimension 18206) in format datetime64.

The variables are 18206 x 334 x 333. The issue is that the time vector is not sorted at all, and I would like to sort it in ascending order (oldest to most recent). And at the same time, reorganize my variables' arrays.

Then, I would like to select part of a variable (for example: "vy"), between 2 dates (so I can do calculations on only a part of my data). I can sort the date vector but can't apply that sorting on the other variables. How could I do that ?

Here is the information of the dataset:

<xarray.Dataset>
Dimensions:                    (mid_date: 18206, y: 334, x: 333)
Coordinates:
  * mid_date                   (mid_date) datetime64[ns] 2011-10-01T00:00:00....
  * x                          (x) float64 4.868e+05 4.871e+05 ... 5.665e+05
  * y                          (y) float64 6.696e+06 6.696e+06 ... 6.616e+06
Data variables: (12/43)
    UTM_Projection             object ...
    acquisition_img1           (mid_date) datetime64[ns] ...
    acquisition_img2           (mid_date) datetime64[ns] ...
    autoRIFT_software_version  (mid_date) float64 ...
    chip_size_height           (mid_date, y, x) float32 ...
    chip_size_width            (mid_date, y, x) float32 ...
                        ...
    vy                         (mid_date, y, x) float32 ...
    vy_error                   (mid_date) float32 ...
    vy_stable_shift            (mid_date) float64 ...
    vyp                        (mid_date, y, x) float64 ...
    vyp_error                  (mid_date) float64 ...
    vyp_stable_shift           (mid_date) float64 ...
Attributes:
    GDAL_AREA_OR_POINT:         Area
    author:                     ITS_LIVE, a NASA MEaSUREs project (its-live.j...
    datacube_software_version:  1.0
    date_created:               30-01-2021 20:49:16
    date_updated:               30-01-2021 20:49:16
    institution:                NASA Jet Propulsion Laboratory (JPL), Califor...
    projection:                 32607
    title:                      ITS_LIVE datacube of image_pair velocities

I tried:

test = datacube.vy.sel(mid_date=slice("2010-01","2013-01"))

or

test = datacube.vy.sel(mid_date=slice(datetime.datetime("2010-01-01"),datetime.datetime("2013-01-01")))

and

test = datacube.vy.sel(mid_date=slice(np.datetime64(2010, 1, 1, 1, 1),np.datetime64(2013, 1, 1, 1, 1)))

But nothing works.

Nihilum
  • 549
  • 3
  • 11

1 Answers1

1

I see two possible solutions:

1/ selection based on the explicit list of dates you want

import numpy as np
from datetime import datetime

da = datacube.vy

filter_dates = filter(lambda d: (d >= np.datetime64(datetime(2010, 1, 1))) & (d < np.datetime64(datetime(2013, 1, 1))),
                      da.mid_date.values)

the_dates_you_want = list(filter_dates)

da[da.mid_date.isin(the_dates_you_want)]

2/ reindexing before selection

new_time = sorted(da.mid_date.values)
da = da.reindex(mid_date = new_time)
da.sel(mid_date = slice(datetime(2010, 1, 1), datetime(2013, 1, 1)))
Thrasy
  • 536
  • 3
  • 9