From baa39d7d369058d53074ca9500e0583ef02c173f Mon Sep 17 00:00:00 2001 From: Stefan Brand Date: Fri, 29 May 2020 13:25:02 +0000 Subject: [PATCH] Fix issue with saving to zarr IntervalIndex is not allowed as coords. More info: https://github.com/pydata/xarray/issues/2847 --- prepare_mapchete.ipynb | 59 ++++++++++++------------------------------ 1 file changed, 16 insertions(+), 43 deletions(-) diff --git a/prepare_mapchete.ipynb b/prepare_mapchete.ipynb index 7611c8a..367e64e 100644 --- a/prepare_mapchete.ipynb +++ b/prepare_mapchete.ipynb @@ -138,27 +138,27 @@ "data": { "text/html": [ "
<xarray.Dataset>\n",
-       "Dimensions:          (timestamps_bins: 6, x: 256, y: 256)\n",
+       "Dimensions:  (time: 6, x: 256, y: 256)\n",
        "Coordinates:\n",
-       "  * timestamps_bins  (timestamps_bins) object (2019-06-02, 2019-06-15 23:59:59] ... (2019-08-16, 2019-08-29 23:59:59]\n",
+       "  * time     (time) datetime64[ns] 2019-06-08T23:59:59.500000 ... 2019-08-22T23:59:59.500000\n",
        "Dimensions without coordinates: x, y\n",
        "Data variables:\n",
-       "    B04              (timestamps_bins, x, y) float64 324.0 329.0 ... 361.5 282.5\n",
-       "    B03              (timestamps_bins, x, y) float64 582.2 604.2 ... 541.5 469.5\n",
-       "    B02              (timestamps_bins, x, y) float64 367.2 374.2 ... 324.5 289.0\n",
-       "    B08              (timestamps_bins, x, y) float64 4.623e+03 ... 2.704e+03
" + " B04 (time, x, y) float64 324.0 329.0 325.2 312.8 ... 412.5 361.5 282.5\n", + " B03 (time, x, y) float64 582.2 604.2 612.2 597.5 ... 583.5 541.5 469.5\n", + " B02 (time, x, y) float64 367.2 374.2 373.5 364.2 ... 359.0 324.5 289.0\n", + " B08 (time, x, y) float64 4.623e+03 4.802e+03 ... 2.838e+03 2.704e+03" ], "text/plain": [ "\n", - "Dimensions: (timestamps_bins: 6, x: 256, y: 256)\n", + "Dimensions: (time: 6, x: 256, y: 256)\n", "Coordinates:\n", - " * timestamps_bins (timestamps_bins) object (2019-06-02, 2019-06-15 23:59:59] ... (2019-08-16, 2019-08-29 23:59:59]\n", + " * time (time) datetime64[ns] 2019-06-08T23:59:59.500000 ... 2019-08-22T23:59:59.500000\n", "Dimensions without coordinates: x, y\n", "Data variables:\n", - " B04 (timestamps_bins, x, y) float64 324.0 329.0 ... 361.5 282.5\n", - " B03 (timestamps_bins, x, y) float64 582.2 604.2 ... 541.5 469.5\n", - " B02 (timestamps_bins, x, y) float64 367.2 374.2 ... 324.5 289.0\n", - " B08 (timestamps_bins, x, y) float64 4.623e+03 ... 2.704e+03" + " B04 (time, x, y) float64 324.0 329.0 325.2 312.8 ... 412.5 361.5 282.5\n", + " B03 (time, x, y) float64 582.2 604.2 612.2 597.5 ... 583.5 541.5 469.5\n", + " B02 (time, x, y) float64 367.2 374.2 373.5 364.2 ... 359.0 324.5 289.0\n", + " B08 (time, x, y) float64 4.623e+03 4.802e+03 ... 2.838e+03 2.704e+03" ] }, "execution_count": 4, @@ -169,6 +169,8 @@ "source": [ "# Calculate values in time intervals\n", "avg_cube = cube.groupby_bins('timestamps',bins=int_idx).mean('timestamps')\n", + "avg_cube = avg_cube.rename({'timestamps_bins': 'time'}) # xcube Dataset spec\n", + "avg_cube.coords['time'] = int_idx.mid # zarr cannot have IntervalIndex as coords\n", "\n", "# # Fake data\n", "# from numpy.random import random_sample\n", @@ -186,37 +188,8 @@ "outputs": [ { "data": { - "text/html": [ - "
<xarray.Dataset>\n",
-       "Dimensions:  (time: 6, x: 256, y: 256)\n",
-       "Coordinates:\n",
-       "  * time     (time) object (2019-06-02, 2019-06-15 23:59:59] ... (2019-08-16, 2019-08-29 23:59:59]\n",
-       "Dimensions without coordinates: x, y\n",
-       "Data variables:\n",
-       "    B04      (time, x, y) uint16 324 329 325 312 303 316 ... 415 416 412 361 282\n",
-       "    B03      (time, x, y) uint16 582 604 612 597 574 577 ... 591 585 583 541 469\n",
-       "    B02      (time, x, y) uint16 367 374 373 364 354 358 ... 366 368 359 324 289\n",
-       "    B08      (time, x, y) uint16 4623 4802 4878 4738 ... 2821 2904 2838 2704\n",
-       "    NDVI     (time, x, y) uint16 61055 61145 61251 61289 ... 57209 57953 59155\n",
-       "    GNDVI    (time, x, y) uint16 58026 58032 58049 58019 ... 54405 54866 55670\n",
-       "    BNDVI    (time, x, y) uint16 60526 60611 60688 60671 ... 58147 58631 59027\n",
-       "    NDWI     (time, x, y) uint16 7308 7302 7285 7315 ... 11220 10929 10468 9664
" - ], "text/plain": [ - "\n", - "Dimensions: (time: 6, x: 256, y: 256)\n", - "Coordinates:\n", - " * time (time) object (2019-06-02, 2019-06-15 23:59:59] ... (2019-08-16, 2019-08-29 23:59:59]\n", - "Dimensions without coordinates: x, y\n", - "Data variables:\n", - " B04 (time, x, y) uint16 324 329 325 312 303 316 ... 415 416 412 361 282\n", - " B03 (time, x, y) uint16 582 604 612 597 574 577 ... 591 585 583 541 469\n", - " B02 (time, x, y) uint16 367 374 373 364 354 358 ... 366 368 359 324 289\n", - " B08 (time, x, y) uint16 4623 4802 4878 4738 ... 2821 2904 2838 2704\n", - " NDVI (time, x, y) uint16 61055 61145 61251 61289 ... 57209 57953 59155\n", - " GNDVI (time, x, y) uint16 58026 58032 58049 58019 ... 54405 54866 55670\n", - " BNDVI (time, x, y) uint16 60526 60611 60688 60671 ... 58147 58631 59027\n", - " NDWI (time, x, y) uint16 7308 7302 7285 7315 ... 11220 10929 10468 9664" + "" ] }, "execution_count": 5, @@ -249,7 +222,7 @@ "if \"B03\" in avg_cube and \"B05\" in avg_cube and \"B08\" in avg_cube:\n", " avg_cube['CVI'] = (avg_cube.B08 * avg_cube.B05 / avg_cube.B03**2).astype(np.float32)\n", "\n", - "avg_cube.rename({'timestamps_bins': 'time'})" + "avg_cube.to_zarr('test.zarr')" ] } ],