Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
Fix issue with saving to zarr
Browse files Browse the repository at this point in the history
IntervalIndex is not allowed as coords. More info:
pydata/xarray#2847
  • Loading branch information
StefanBrand committed May 29, 2020
1 parent 54d3a36 commit 29d9e8f
Showing 1 changed file with 16 additions and 43 deletions.
59 changes: 16 additions & 43 deletions prepare_mapchete.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -138,27 +138,27 @@
"data": {
"text/html": [
"<pre>&lt;xarray.Dataset&gt;\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</pre>"
" 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</pre>"
],
"text/plain": [
"<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"
]
},
"execution_count": 4,
Expand All @@ -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",
Expand All @@ -186,37 +188,8 @@
"outputs": [
{
"data": {
"text/html": [
"<pre>&lt;xarray.Dataset&gt;\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</pre>"
],
"text/plain": [
"<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"
"<xarray.backends.zarr.ZarrStore at 0x7f8d1b6d1f50>"
]
},
"execution_count": 5,
Expand Down Expand Up @@ -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')"
]
}
],
Expand Down

0 comments on commit 29d9e8f

Please sign in to comment.