import%20marimo%0A%0A__generated_with%20%3D%20%220.26.0%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20os%0A%20%20%20%20import%20sys%0A%0A%20%20%20%20for%20var%20in%20(%22PROJ_LIB%22%2C%20%22PROJ_DATA%22%2C%20%22GDAL_DATA%22)%3A%0A%20%20%20%20%20%20%20%20os.environ.pop(var%2C%20None)%20%20%23%20a%20conda%20PROJ%20install%20on%20the%20path%20breaks%20rasterio's%20bundled%20one%0A%0A%20%20%20%20import%20inspect%0A%20%20%20%20import%20json%0A%20%20%20%20import%20tempfile%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20dask%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numcodecs%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20xarray%20as%20xr%0A%20%20%20%20import%20zarr%0A%20%20%20%20from%20IPython.display%20import%20Markdown%2C%20display%0A%20%20%20%20from%20loguru%20import%20logger%0A%0A%20%20%20%20from%20labkit.show%20import%20TF_DATA_ML_UTILS%20as%20TFDMU_SHA%0A%20%20%20%20from%20labkit.show%20import%20show%20%20%23%20a%20function's%20real%20source%2C%20linked%20to%20the%20pinned%20commit%0A%0A%20%20%20%20logger.remove()%0A%20%20%20%20logger.add(sys.stderr%2C%20level%3D%22WARNING%22)%20%20%23%20production%20logs%20at%20DEBUG%3B%20keep%20the%20notebook%20readable%0A%0A%20%20%20%20WORK%20%3D%20Path(tempfile.mkdtemp(prefix%3D%22nb01_%22))%0A%0A%20%20%20%20def%20tree(path)%3A%0A%20%20%20%20%20%20%20%20%23%20List%20every%20file%20in%20a%20store%20with%20its%20size%20in%20bytes.%0A%20%20%20%20%20%20%20%20path%20%3D%20Path(path)%0A%20%20%20%20%20%20%20%20for%20f%20in%20sorted(p%20for%20p%20in%20path.rglob(%22*%22)%20if%20p.is_file())%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%7Bf.stat().st_size%3A%3E8%7D%20%20%7Bf.relative_to(path)%7D%22)%0A%0A%20%20%20%20def%20du(path)%3A%0A%20%20%20%20%20%20%20%20return%20sum(f.stat().st_size%20for%20f%20in%20Path(path).rglob(%22*%22)%20if%20f.is_file())%0A%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22zarr%22%2C%0A%20%20%20%20%20%20%20%20zarr.__version__%2C%0A%20%20%20%20%20%20%20%20%22%7C%20xarray%22%2C%0A%20%20%20%20%20%20%20%20xr.__version__%2C%0A%20%20%20%20%20%20%20%20%22%7C%20dask%22%2C%0A%20%20%20%20%20%20%20%20dask.__version__%2C%0A%20%20%20%20%20%20%20%20%22%7C%20numcodecs%22%2C%0A%20%20%20%20%20%20%20%20numcodecs.__version__%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22working%20folder%3A%22%2C%20WORK)%0A%0A%20%20%20%20%23%20Imported%20here%20because%20later%20cells%20use%20them%20too.%0A%20%20%20%20from%20tf_data_ml_utils.nasa_ingestion.core.gridding%20import%20reproject_tile_window%0A%20%20%20%20from%20tf_data_ml_utils.nasa_ingestion.instruments%20import%20MOD13A1%0A%20%20%20%20from%20tf_data_ml_utils.nasa_ingestion.stages.standardise%20import%20standardise%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Zarr%20and%20the%20MODIS%20NDVI%20lake%0A%0A%20%20%20%20**What%20you'll%20learn**%0A%0A%20%20%20%201.%20How%20Zarr%20stores%20an%20N-dimensional%20array%20as%20compressed%20chunks%2C%20and%20what%20%22Zarr%202%22%20versus%20%22Zarr%203%22%20actually%20means.%20Short%20version%3A%20it's%20two%20things%2C%20the%20library%20and%20the%20on-disk%20format.%0A%20%20%20%202.%20How%20our%20production%20code%20writes%2C%20appends%20and%20protects%20Zarr%20stores%20on%20S3%2C%20run%20here%20on%20a%20local%20store%20you%20can%20inspect.%0A%20%20%20%203.%20What%20we%20put%20in%20those%20stores%20for%20MODIS%20NDVI%2C%20and%20why%20every%20scientific%20decision%20on%20the%20way%20in%20is%20made%20once%2C%20at%20ingest.%20You'll%20also%20try%20the%20paths%20we%20didn't%20take.%0A%0A%20%20%20%20**How%20to%20run%3A**%20follow%20the%20set-up%20in%20%5Bacademy%2FREADME.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2FREADME.md%23setting-up)%3A%20%60uv%20sync%60%2C%20which%20needs%20network%20access%20the%20first%20time%2C%20to%20fetch%20the%20pinned%20packages.%20Then%2C%20from%20%60academy%2F%60%2C%20open%20it%20with%20%60uv%20run%20marimo%20edit%201-data%2F1.1-zarr-and-the-modis-ndvi-lake%2Fnotebook.py%60%3B%20marimo%20runs%20the%20cells%20in%20the%20order%20their%20results%20are%20needed%2C%20and%20re-runs%20the%20cells%20that%20depend%20on%20any%20cell%20you%20change.%20Once%20set%20up%2C%20no%20cell%20needs%20network%20access%20or%20AWS%20credentials.%20Every%20store%20is%20written%20to%20a%20temporary%20folder.%0A%0A%20%20%20%20The%20production%20code%20is%20imported%2C%20not%20copied.%20%60tf-data-ml-utils%60%20is%20pinned%20to%20%60c0ba9dc%60%20in%20%60pyproject.toml%60%2C%20and%20every%20%60show(...)%60%20cell%20prints%20the%20real%20source%20with%20a%20permalink%20to%20that%20commit.%20The%20library%20versions%20(zarr%203.1.5%2C%20xarray%202025.11.0%2C%20dask%202026.1.2%2C%20numcodecs%200.16.5)%20match%20production's%20lock%20file.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Part%201%3A%20the%20libraries%2C%20as%20their%20documentation%20introduces%20them%0A%0A%20%20%20%20%23%23%201.1%20Zarr%20in%20five%20lines%0A%0A%20%20%20%20This%20follows%20the%20%5BZarr%20quick%20start%5D(https%3A%2F%2Fzarr.readthedocs.io%2Fen%2Fv3.1.5%2Fquick-start%2F).%20A%20Zarr%20array%20is%20an%20N-dimensional%20array%20cut%20into%20**chunks**.%20Each%20chunk%20is%20compressed%20separately%20and%20stored%20as%20its%20own%20object%20(here%20a%20file%2C%20on%20S3%20an%20object)%2C%20and%20a%20small%20JSON%20file%20says%20how%20to%20reassemble%20them.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_1_zarr_five_lines()%3A%0A%20%20%20%20z%20%3D%20zarr.create_array(%0A%20%20%20%20%20%20%20%20store%3DWORK%20%2F%20%22hello.zarr%22%2C%0A%20%20%20%20%20%20%20%20shape%3D(4%2C%206)%2C%0A%20%20%20%20%20%20%20%20chunks%3D(2%2C%203)%2C%0A%20%20%20%20%20%20%20%20dtype%3D%22float32%22%2C%0A%20%20%20%20%20%20%20%20zarr_format%3D2%2C%0A%20%20%20%20%20%20%20%20overwrite%3DTrue%2C%0A%20%20%20%20)%0A%20%20%20%20z%5B%3A%5D%20%3D%20np.arange(24%2C%20dtype%3D%22float32%22).reshape(4%2C%206)%0A%20%20%20%20print(z%5B1%3A3%2C%202%3A5%5D)%0A%20%20%20%20print(z.info)%0A%20%20%20%20tree(WORK%20%2F%20%22hello.zarr%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Four%20chunks%20of%202%20%C3%97%203%20gives%20four%20chunk%20files%2C%20named%20by%20chunk%20position%3A%20%600.0%60%2C%20%600.1%60%2C%20%601.0%60%20and%20%601.1%60.%20Reading%20%60z%5B1%3A3%2C%202%3A5%5D%60%20touched%20all%20four%2C%20because%20the%20slice%20crosses%20both%20chunk%20boundaries.%0A%0A%20%20%20%20**This%20is%20the%20whole%20idea.**%20A%20reader%20fetches%20only%20the%20chunks%20that%20overlap%20its%20request%2C%20and%20can%20fetch%20them%20in%20parallel.%20On%20S3%20that%20means%20one%20GET%20per%20chunk%20and%20no%20need%20to%20download%20a%20whole%20file.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201.2%20The%20library%20version%20and%20the%20format%20version%20are%20different%20things%0A%0A%20%20%20%20%60zarr.__version__%60%20above%20is%203.1.5%3A%20that's%20the%20**library**.%20The%20%60zarr_format%3D2%60%20argument%20chose%20the%20**on-disk%20format**.%20Here%20is%20the%20same%20array%20written%20in%20both%20formats%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_2_library_version_format()%3A%0A%20%20%20%20for%20fmt%20in%20(2%2C%203)%3A%0A%20%20%20%20%20%20%20%20a%20%3D%20zarr.create_array(%0A%20%20%20%20%20%20%20%20%20%20%20%20store%3DWORK%20%2F%20f%22fmt%7Bfmt%7D.zarr%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20shape%3D(4%2C%206)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20chunks%3D(2%2C%203)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20dtype%3D%22float32%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20zarr_format%3Dfmt%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20overwrite%3DTrue%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20a%5B%3A%5D%20%3D%201.0%0A%20%20%20%20%20%20%20%20print(f%22---%20zarr_format%3D%7Bfmt%7D%20---%22)%0A%20%20%20%20%20%20%20%20tree(WORK%20%2F%20f%22fmt%7Bfmt%7D.zarr%22)%0A%0A%20%20%20%20print(%22%5Cnformat%202%20metadata%20(.zarray)%3A%22)%0A%20%20%20%20print((WORK%20%2F%20%22fmt2.zarr%22%20%2F%20%22.zarray%22).read_text())%0A%20%20%20%20print(%22format%203%20metadata%20(zarr.json)%3A%22)%0A%20%20%20%20print((WORK%20%2F%20%22fmt3.zarr%22%20%2F%20%22zarr.json%22).read_text())%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20differences%3A%0A%0A%20%20%20%20-%20**Metadata.**%20Format%202%20uses%20%60.zarray%60%2C%20%60.zgroup%60%20and%20%60.zattrs%60%2C%20and%20names%20one%20%60compressor%60.%20Format%203%20puts%20everything%20in%20one%20%60zarr.json%60%20and%20describes%20an%20ordered%20%60codecs%60%20pipeline.%0A%20%20%20%20-%20**Chunk%20keys.**%20Format%202%20uses%20%600.1%60%3B%20format%203%20uses%20%60c%2F0%2F1%60.%0A%0A%20%20%20%20Same%20library%2C%20two%20formats.%20So%20%22we%20use%20Zarr%202%22%20in%20our%20codebase%20means%20*we%20run%20zarr-python%203%20and%20write%20format%202*.%0A%0A%20%20%20%20**Look%20at%20the%20compressor%20too.**%20Zarr's%20own%20default%20for%20a%20format-2%20array%20is%20%60zstd%60%20at%20level%200%2C%20which%20zstd%20reads%20as%20%22use%20the%20default%22%2C%20level%203%20(section%204.2%20shows%20the%20two%20give%20identical%20bytes).%20In%201.3%20below%2C%20xarray's%20%60to_zarr%60%20writes%20%60blosc%60%20with%20%60lz4%60%20instead.%20Defaults%20depend%20on%20which%20layer%20you%20write%20through%2C%20and%20that%20is%20one%20reason%20our%20production%20code%20sets%20the%20encoding%20explicitly%20rather%20than%20trusting%20either.%0A%0A%20%20%20%20Specifications%3A%20%5Bformat%202%5D(https%3A%2F%2Fzarr-specs.readthedocs.io%2Fen%2Flatest%2Fv2%2Fv2.0.html)%20and%20%5Bformat%203%5D(https%3A%2F%2Fzarr-specs.readthedocs.io%2Fen%2Flatest%2Fv3%2Fcore%2Findex.html).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201.3%20xarray%20on%20top%20of%20Zarr%0A%0A%20%20%20%20We%20rarely%20touch%20%60zarr%60%20directly.%20xarray%20adds%20named%20dimensions%20and%20coordinates%2C%20and%20writes%20them%20as%20Zarr%20(%5Bxarray%20I%2FO%20guide%5D(https%3A%2F%2Fdocs.xarray.dev%2Fen%2Fv2025.11.0%2Fuser-guide%2Fio.html%23zarr)).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_3_xarray_top_zarr()%3A%0A%20%20%20%20xr_store%20%3D%20(%0A%20%20%20%20%20%20%20%20WORK%20%2F%20%22xr.zarr%22%0A%20%20%20%20)%20%20%23%20the%20cells%20that%20read%20this%20store%20take%20xr_store%2C%20so%20they%20re-run%20when%20it%20is%20rewritten%0A%20%20%20%20times%20%3D%20pd.date_range(%222024-01-01%22%2C%20periods%3D6%2C%20freq%3D%2216D%22)%0A%20%20%20%20ds%20%3D%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22ndvi%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22time%22%2C%20%22latitude%22%2C%20%22longitude%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20np.random.default_rng(0).uniform(0%2C%200.9%2C%20(6%2C%2040%2C%2050)).astype(%22float32%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20coords%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22time%22%3A%20times%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22latitude%22%3A%20np.linspace(39%2C%2038%2C%2040)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22longitude%22%3A%20np.linspace(-99.5%2C%20-98.5%2C%2050)%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20)%0A%20%20%20%20ds.to_zarr(xr_store%2C%20mode%3D%22w%22%2C%20zarr_format%3D2%2C%20encoding%3D%7B%22ndvi%22%3A%20%7B%22chunks%22%3A%20(3%2C%2040%2C%2050)%7D%7D)%0A%20%20%20%20tree(xr_store)%0A%20%20%20%20print(%22%5Cn.zattrs%20of%20ndvi%3A%22%2C%20(xr_store%20%2F%20%22ndvi%22%20%2F%20%22.zattrs%22).read_text())%0A%20%20%20%20return%20(xr_store%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Two%20things%20to%20notice%3A%0A%0A%20%20%20%20-%20**Coordinates%20are%20arrays%20too**%20(%60time%2F0%60%2C%20%60latitude%2F0%60).%0A%20%20%20%20-%20**%60_ARRAY_DIMENSIONS%60%20is%20how%20xarray%20remembers%20dimension%20names.**%20Format%202%20has%20no%20concept%20of%20dimension%20names%2C%20so%20xarray%20invented%20this%20attribute.%20Format%203%20has%20a%20native%20%60dimension_names%60%20field.%0A%0A%20%20%20%20There's%20also%20a%20%60.zmetadata%60%20file.%20That%20is%20**consolidated%20metadata**%3A%20every%20%60.zarray%60%2C%20%60.zattrs%60%20and%20%60.zgroup%60%20copied%20into%20one%20JSON%20file%2C%20so%20opening%20a%20store%20over%20S3%20costs%20one%20GET%20instead%20of%20one%20per%20array.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_3_xarray_top_zarr_2(xr_store)%3A%0A%20%20%20%20zmeta%20%3D%20json.loads((xr_store%20%2F%20%22.zmetadata%22).read_text())%0A%20%20%20%20print(%22keys%20inside%20.zmetadata%3A%22%2C%20sorted(zmeta%5B%22metadata%22%5D))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201.4%20Lazy%20by%20default%3A%20Dask%0A%0A%20%20%20%20%60open_zarr%60%20reads%20the%20metadata%2C%20plus%20the%20small%20coordinate%20arrays%20(%60time%60%2C%20%60latitude%60%2C%20%60longitude%60)%20that%20xarray%20loads%20to%20build%20its%20indexes.%20The%20data%20variables%20come%20back%20as%20a%20%5BDask%20array%5D(https%3A%2F%2Fdocs.dask.org%2Fen%2Fstable%2Farray.html)%2C%20a%20grid%20of%20chunk%20recipes%2C%20and%20none%20of%20the%20NDVI%20is%20read%20until%20an%20operation%20needs%20its%20values%3A%20%60.compute()%60%2C%20%60.values%60%20and%20%60.load()%60%20are%20examples.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_4_lazy_default_dask(xr_store)%3A%0A%20%20%20%20lazy%20%3D%20xr.open_zarr(xr_store)%0A%20%20%20%20print(type(lazy.ndvi.data)%2C%20%22chunks%3A%22%2C%20lazy.ndvi.data.chunks)%0A%20%20%20%20one_pixel%20%3D%20lazy.ndvi.isel(latitude%3D5%2C%20longitude%3D7)%0A%20%20%20%20graph%20%3D%20one_pixel.data.__dask_graph__()%0A%20%20%20%20print(%22tasks%20in%20the%20graph%20for%20one%20pixel's%20time%20series%3A%22%2C%20len(graph))%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22tasks%20per%20layer%3A%22%2C%0A%20%20%20%20%20%20%20%20%7Bname.rsplit(%22-%22%2C%201)%5B0%5D%3A%20len(layer)%20for%20name%2C%20layer%20in%20graph.layers.items()%7D%2C%0A%20%20%20%20)%0A%20%20%20%20print(one_pixel.compute().values.round(3))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Why%20five%20tasks%3F%20One%20task%20holds%20the%20opened%20Zarr%20array%20(%60original-%E2%80%A6%60).%20The%206%20time%20steps%20are%20stored%20as%20two%20chunks%20of%203%2C%20so%20two%20tasks%20load%20one%20chunk%20each%20(%60open_dataset-%E2%80%A6%60)%2C%20and%20two%20more%20pick%20the%20pixel%20out%20of%20each%20loaded%20chunk%20(%60getitem%60).%20Reading%20one%20pixel's%20time%20series%20still%20reads%20two%20whole%20chunks%3A%20the%20chunk%20shape%2C%20not%20the%20request%2C%20decides%20how%20much%20is%20fetched.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Part%202%3A%20how%20our%20production%20code%20does%20it%0A%0A%20%20%20%20Everything%20in%20this%20part%20runs%20%60tf_data_ml_utils.common.io%60%2C%20the%20module%20every%20ingester%20writes%20through.%0A%0A%20%20%20%20%23%23%202.1%20One%20format%2C%20chosen%20once%0A%0A%20%20%20%20The%20module%20pins%20the%20format%20in%20a%20single%20constant%20at%20%5Bcommon%2Fio.py%3A30%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fcommon%2Fio.py%23L30).%20It%20is%20defined%20again%20in%20two%20other%20modules%20of%20the%20same%20repo%2C%20and%20twice%20more%20in%20model-foundry.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_1_format_chosen_once()%3A%0A%20%20%20%20from%20tf_data_ml_utils.common%20import%20io%0A%0A%20%20%20%20print(%22io.ZARR_FORMAT%20%3D%22%2C%20io.ZARR_FORMAT)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22io.MAX_CHUNK_BYTES%20%3D%22%2C%0A%20%20%20%20%20%20%20%20io.MAX_CHUNK_BYTES%2C%0A%20%20%20%20%20%20%20%20f%22(%3D%202%20GiB%20-%201%20MiB%20%3D%20%7Bio.MAX_CHUNK_BYTES%20%2F%202**30%3A.4f%7D%20GiB)%22%2C%0A%20%20%20%20)%0A%20%20%20%20show(io.save_zarr)%0A%20%20%20%20return%20(io%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%60save_zarr%60%20does%20four%20things%3A%0A%0A%20%20%20%201.%20**Re-chunks%20and%20drops%20any%20inherited%20encoding**%20(%60.drop_encoding()%60).%20Section%202.4%20shows%20why.%0A%20%20%20%202.%20**Builds%20explicit%20chunk%20encoding**%2C%20so%20xarray%20never%20guesses.%0A%20%20%20%203.%20**Writes%20into%20a%20staging%20location**%2C%20never%20straight%20onto%20the%20live%20store.%0A%20%20%20%204.%20**Validates%20the%20staged%20copy%20and%20only%20then%20publishes%20it.**%0A%0A%20%20%20%20The%20staging%20is%20a%20context%20manager%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_1_format_chosen_once_2(io)%3A%0A%20%20%20%20show(io.staged_publish)%0A%20%20%20%20show(io._publish_staged_store)%0A%20%20%20%20show(io._publish_cloud_store)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Why%20stage%3F**%20S3%20has%20no%20atomic%20rename.%20A%20process%20killed%20half-way%20through%20writing%20%60store.zarr%60%20leaves%20a%20store%20that%20*exists*%20but%20is%20missing%20chunks.%20Writing%20to%20a%20temporary%20path%20and%20publishing%20only%20after%20validation%20means%20a%20failed%20write%2C%20or%20a%20store%20that%20fails%20validation%2C%20never%20reaches%20the%20live%20path.%0A%0A%20%20%20%20The%20publish%20itself%20is%20not%20atomic%2C%20though%2C%20in%20either%20world%3A%0A%0A%20%20%20%20-%20**Locally**%2C%20%60_publish_staged_store%60%20removes%20the%20old%20store%20before%20renaming%20the%20staged%20one%20into%20place%2C%20so%20a%20reader%20can%20briefly%20find%20no%20store%2C%20and%20if%20the%20rename%20fails%20the%20live%20path%20stays%20missing.%0A%20%20%20%20-%20**On%20S3**%2C%20where%20production%20runs%2C%20%60_publish_cloud_store%60%20(printed%20last%20above)%20copies%20the%20live%20store%20to%20a%20backup%2C%20**deletes**%20it%2C%20then%20copies%20every%20staged%20object%20in%2C%20with%20no%20%22metadata%20last%22%20ordering.%20If%20the%20copy%20fails%20with%20an%20error%20the%20code%20catches%2C%20it%20tries%20to%20restore%20the%20backup%3B%20if%20the%20process%20is%20killed%20after%20the%20delete%2C%20or%20the%20restore%20fails%2C%20the%20backup%20and%20staged%20store%20are%20left%20for%20manual%20recovery.%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20live%20path%3A%20%20%5Bold%20store%5D%20%E2%94%80%20backup%20%E2%94%80%20delete%20%E2%94%80%E2%94%80%E2%94%80%E2%94%80%20copy%20staged%20objects%20in%20%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%20%5Bnew%20store%5D%0A%20%20%20%20reader%3A%20%20%20%20%20%20sees%20old%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%20sees%20%20%20%20%20%20%E2%94%82%20sees%20a%20partial%20store%20%20%20%20%20%20%20%E2%94%82%20sees%20new%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%20nothing%20%20%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20So%20the%20guarantee%20is%20%22a%20failed%20or%20invalid%20write%20never%20replaces%20the%20live%20store%22%2C%20not%20%22no%20reader%20ever%20sees%20a%20half-written%20store%22.%20Readers%20should%20not%20run%20during%20a%20publish.%0A%0A%20%20%20%20%60%40contextmanager%60%20turns%20a%20generator%20into%20a%20%60with%60%20block.%20Code%20before%20%60yield%60%20runs%20on%20entry.%20If%20the%20block%20raises%2C%20the%20%60except%60%20branch%20removes%20the%20staging%20copy.%20If%20it%20succeeds%2C%20the%20publish%20runs.%0A%0A%20%20%20%20%23%23%202.2%20Keeping%20chunks%20under%20Blosc's%202%20GiB%20ceiling%0A%0A%20%20%20%20Blosc%2C%20the%20default%20compressor%2C%20can't%20compress%20a%20buffer%20larger%20than%20%60numcodecs.blosc.MAX_BUFFERSIZE%60%20(printed%20below)%2C%20just%20under%202%20GiB.%20%60cap_chunk_bytes%60%20halves%20chunk%20extents%20until%20every%20chunk%20fits.%20Here%20it%20is%20on%20a%20lazy%2C%20never-allocated%20dataset%20the%20size%20of%20a%20Sentinel-2%20tile%20time%20series%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_2_keeping_chunks_under(io)%3A%0A%20%20%20%20show(io.cap_chunk_bytes)%0A%20%20%20%20big%20%3D%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22refl%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22time%22%2C%20%22y%22%2C%20%22x%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dask.array.zeros((30%2C%2010980%2C%2010980)%2C%20dtype%3D%22float32%22%2C%20chunks%3D(30%2C%2010980%2C%2010980))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20print(f%22lazy%20dataset%3A%20%7Bbig.refl.nbytes%20%2F%201e9%3A.1f%7D%20GB%2C%20never%20allocated%22)%0A%20%20%20%20print(%22requested%20one%20chunk%20per%20dim%20-%3E%22%2C%20io.cap_chunk_bytes(big%2C%20%7B%22time%22%3A%20-1%2C%20%22y%22%3A%20-1%2C%20%22x%22%3A%20-1%7D))%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22Blosc%20limit%20%7Bnumcodecs.blosc.MAX_BUFFERSIZE%3A%2C%7D%20B%3B%20our%20cap%20MAX_CHUNK_BYTES%20%7Bio.MAX_CHUNK_BYTES%3A%2C%7D%20B%22%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Three%20steps%20of%2010980%20%C3%97%2010980%20float32%20is%20about%201.45%20GB%2C%20which%20fits.%20%22Fits%22%20means%20%22won't%20crash%20Blosc%22%2C%20not%20%22a%20good%20size%22%3A%20%60MAX_CHUNK_BYTES%60%20is%20a%20crash%20guard%2C%20not%20a%20design%20target.%20For%20reads%20from%20S3%2C%20chunks%20of%20tens%20to%20a%20few%20hundred%20MB%20are%20typical%2C%20because%20every%20read%20fetches%20and%20decompresses%20whole%20chunks%20(%5BDask's%20chunk-size%20advice%5D(https%3A%2F%2Fdocs.dask.org%2Fen%2Fstable%2Farray-best-practices.html%23select-a-good-chunk-size)%3B%20%5Bzarr's%20performance%20guide%5D(https%3A%2F%2Fzarr.readthedocs.io%2Fen%2Fv3.1.5%2Fuser-guide%2Fperformance%2F)).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202.3%20Appending%20safely%3A%20%60.zmetadata%60%20as%20a%20commit%20marker%0A%0A%20%20%20%20Appending%20a%20time%20step%20to%20a%20live%20store%20is%20the%20risky%20operation.%20Our%20%60append_zarr%60%20turns%20consolidated%20metadata%2C%20which%20was%20designed%20as%20a%20*speed*%20feature%2C%20into%20a%20**commit%20marker**%3A%0A%0A%20%20%20%201.%20Write%20the%20new%20data%20with%20%60consolidated%3DFalse%60.%20Each%20array's%20%60.zarray%60%20grows%2C%20but%20%60.zmetadata%60%20still%20describes%20the%20old%20length.%0A%20%20%20%202.%20Call%20%60zarr.consolidate_metadata%60%20only%20after%20every%20chunk%20has%20landed.%0A%0A%20%20%20%20Why%20is%20one%20write%20a%20valid%20commit%3F%20%60.zmetadata%60%20is%20a%20single%20object%2C%20and%20on%20S3%20a%20PUT%20of%20one%20object%20is%20all-or-nothing%2C%20with%20strong%20read-after-write%20consistency%20(%5BAWS%20S3%20consistency%20model%5D(https%3A%2F%2Fdocs.aws.amazon.com%2FAmazonS3%2Flatest%2Fuserguide%2FWelcome.html%23ConsistencyModel))%3B%20locally%2C%20zarr%20writes%20each%20file%20to%20a%20temporary%20name%20and%20renames%20it.%20A%20reader%20sees%20the%20old%20file%20or%20the%20new%20one%2C%20never%20half%20of%20each.%20zarr's%20own%20documentation%20names%20the%20property%20being%20used%3A%20%22Readers%20using%20consolidated%20metadata%20will%20regularly%20see%20a%20'past'%20version%20of%20the%20metadata%22%20(%5Bconsolidated%20metadata%5D(https%3A%2F%2Fzarr.readthedocs.io%2Fen%2Fv3.1.5%2Fuser-guide%2Fconsolidated_metadata%2F)).%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20time%20%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%96%BA%0A%20%20%20%20writer%3A%20%20%20%20%20%20%20%20write%20chunks%20%2B%20ndvi%2F.zarray%20%20%20%20%20%20%20%20%20%20consolidate%20(one%20PUT)%0A%20%20%20%20ndvi%2F.zarray%3A%20%20length%202%20%E2%94%80%E2%94%80%E2%96%BA%20length%203%20%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%0A%20%20%20%20.zmetadata%3A%20%20%20%20length%202%20%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%96%BA%20length%203%20%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%0A%20%20%20%20reader%20A%20(trusts%20.zmetadata)%3A%20%20%20%20sees%202%20%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%96%BA%20sees%203%0A%20%20%20%20reader%20B%20(consolidated%3DFalse)%3A%20%20%20sees%202%20%E2%94%80%E2%94%80%E2%96%BA%20sees%203%20(not%20yet%20committed)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20A%20reader%20that%20trusts%20%60.zmetadata%60%20therefore%20never%20sees%20the%20*length*%20of%20a%20half-finished%20append.%20That's%20the%20whole%20guarantee%3A%20%60mode%3D%22a%22%60%20also%20rewrites%20variables%20without%20a%20%60time%60%20dimension%20(coordinates%2C%20root%20attributes)%20before%20consolidation%2C%20so%20such%20a%20reader%20can%20still%20see%20new%20values%20there%2C%20and%20the%20rollback%20below%20only%20shrinks%20arrays%20along%20%60time%60.%20Note%20too%20that%20%60append_zarr%60%20validates%20the%20store%20*after*%20consolidating%2C%20so%20a%20store%20that%20fails%20validation%20keeps%20the%20new%20steps%3B%20nothing%20rolls%20them%20back.%20And%20the%20**first**%20thing%20the%20next%20%60append_zarr%60%20does%20is%20roll%20back%20anything%20uncommitted%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_3_appending_safely_zmetadata(io)%3A%0A%20%20%20%20show(io._rollback_uncommitted)%0A%20%20%20%20src%20%3D%20inspect.getsource(io.append_zarr)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22append_zarr%20starts%20by%20calling%20_rollback_uncommitted%3A%22%2C%0A%20%20%20%20%20%20%20%20%22_rollback_uncommitted(path%2C%20dim)%22%20in%20src%2C%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22%E2%80%A6and%20commits%20with%20consolidate_metadata%20after%20an%20unconsolidated%20write%3A%22%2C%0A%20%20%20%20%20%20%20%20%22consolidated%3DFalse)%5Cn%20%20%20%20zarr.consolidate_metadata%22%20in%20src%2C%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22%E2%80%A6and%20validates%20only%20after%20that%20commit%3A%22%2C%0A%20%20%20%20%20%20%20%20%22validate_zarr_integrity(%22%20in%20src%5Bsrc.index(%22zarr.consolidate_metadata%22)%20%3A%5D%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Now%20try%20it.%20We%20write%20a%20tiny%20store%20through%20production%20code%2C%20append%20one%20step%20properly%2C%20then%20**simulate%20a%20crash**%3A%20an%20append%20that%20writes%20its%20data%20but%20never%20consolidates.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_3_appending_safely_zmetadata_2(io)%3A%0A%20%20%20%20def%20step(date%2C%20value)%3A%0A%20%20%20%20%20%20%20%20return%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22ndvi%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22time%22%2C%20%22latitude%22%2C%20%22longitude%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20np.full((1%2C%204%2C%205)%2C%20value%2C%20dtype%3D%22float32%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20coords%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22time%22%3A%20%5Bpd.Timestamp(date)%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22latitude%22%3A%20np.linspace(39%2C%2038%2C%204)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22longitude%22%3A%20np.linspace(-99%2C%20-98%2C%205)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20store%20%3D%20WORK%20%2F%20%22commit_marker.zarr%22%0A%20%20%20%20io.save_zarr(%0A%20%20%20%20%20%20%20%20step(%222024-06-09%22%2C%200.6)%2C%0A%20%20%20%20%20%20%20%20store%2C%0A%20%20%20%20%20%20%20%20%7B%22time%22%3A%2023%2C%20%22latitude%22%3A%20-1%2C%20%22longitude%22%3A%20-1%7D%2C%0A%20%20%20%20%20%20%20%20append_dims%3Dfrozenset(%7B%22time%22%7D)%2C%0A%20%20%20%20%20%20%20%20require_daily_continuity%3DFalse%2C%0A%20%20%20%20)%0A%20%20%20%20io.append_zarr(%0A%20%20%20%20%20%20%20%20step(%222024-06-25%22%2C%200.7)%2C%0A%20%20%20%20%20%20%20%20store%2C%0A%20%20%20%20%20%20%20%20dim%3D%22time%22%2C%0A%20%20%20%20%20%20%20%20raise_on_gap%3DFalse%2C%0A%20%20%20%20%20%20%20%20require_daily_continuity%3DFalse%2C%0A%20%20%20%20)%0A%0A%20%20%20%20step(%222024-07-11%22%2C%200.8).to_zarr(%0A%20%20%20%20%20%20%20%20store%2C%20mode%3D%22a%22%2C%20append_dim%3D%22time%22%2C%20zarr_format%3D2%2C%20consolidated%3DFalse%0A%20%20%20%20)%20%20%23%20crash%20before%20consolidating%0A%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22reader%20trusting%20.zmetadata%20(tf-data-ml-utils%20load_zarr)%20sees%3A%22%2C%0A%20%20%20%20%20%20%20%20io.load_zarr(store).sizes%5B%22time%22%5D%2C%0A%20%20%20%20%20%20%20%20%22steps%22%2C%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22reader%20with%20consolidated%3DFalse%20(data-pipelines%20open_store)%20sees%3A%22%2C%0A%20%20%20%20%20%20%20%20xr.open_zarr(store%2C%20consolidated%3DFalse).sizes%5B%22time%22%5D%2C%0A%20%20%20%20%20%20%20%20%22steps%22%2C%0A%20%20%20%20)%0A%20%20%20%20io._rollback_uncommitted(store%2C%20%22time%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22after%20_rollback_uncommitted%2C%20consolidated%3DFalse%20sees%3A%22%2C%0A%20%20%20%20%20%20%20%20xr.open_zarr(store%2C%20consolidated%3DFalse).sizes%5B%22time%22%5D%2C%0A%20%20%20%20%20%20%20%20%22steps%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(store%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20That's%20the%20study%20question%20%5Bwho%20sees%20an%20append%20before%20it%20is%20committed%3F%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.1-zarr-and-the-modis-ndvi-lake%2Fscience.md)%2C%20answered%20by%20experiment.%0A%0A%20%20%20%20data-pipelines'%20shared%20reader%20always%20passes%20%60consolidated%3DFalse%60%20(%5Bcommon%2Fstore%2Fio.py%3A19-30%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftreefera-data-pipelines%2Fblob%2Fa332e4c41870deb7daf7425edeae7578ab933ed3%2Fsrc%2Ftreefera_pipelines%2Fcommon%2Fstore%2Fio.py%23L19-L30))%2C%20so%20it%20can%20see%20the%20uncommitted%20step.%20Whether%20that%20matters%20depends%20on%20whether%20anything%20reads%20a%20store%20while%20it%20is%20being%20appended%20to.%20That's%20a%20Dagster%20scheduling%20question%2C%20not%20a%20Zarr%20one.%0A%0A%20%20%20%20**One%20more%20thing%20on%20disk.**%20%60time%60%20is%20a%20coordinate%2C%20so%20%60build_zarr_encoding%60%20caps%20its%20chunk%20at%20the%20first%20write's%20length%2C%201%2C%20and%20each%20step%20becomes%20its%20own%20tiny%20object.%20List%20the%20folder%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_3_appending_safely_zmetadata_3(store)%3A%0A%20%20%20%20print(%22time%20chunks%20on%20disk%3A%22%2C%20sorted(p.name%20for%20p%20in%20(store%20%2F%20%22time%22).iterdir()))%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22time%20fill_value%20in%20.zarray%3A%22%2C%0A%20%20%20%20%20%20%20%20json.loads((store%20%2F%20%22time%22%20%2F%20%22.zarray%22).read_text())%5B%22fill_value%22%5D%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22time%20units%3A%22%2C%20json.loads((store%20%2F%20%22time%22%20%2F%20%22.zattrs%22).read_text())%5B%22units%22%5D)%0A%20%20%20%20print(%22raw%20stored%20time%20values%3A%22%2C%20zarr.open_array(store%20%2F%20%22time%22%2C%20mode%3D%22r%22)%5B%3A%5D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%60time%2F0%60%20is%20missing%2C%20yet%20reading%20it%20gives%200.%20zarr-python%203%20does%20not%20write%20a%20chunk%20whose%20values%20all%20equal%20the%20array's%20fill%20value%20(%5Bempty%20chunks%5D(https%3A%2F%2Fzarr.readthedocs.io%2Fen%2Fv3.1.5%2Fuser-guide%2Fperformance%2F%23empty-chunks)).%20The%20first%20step%20is%20stored%20as%200%20days%20since%20itself%2C%20the%20%60.zarray%60%20says%20%60fill_value%3A%20null%60%2C%20and%20zarr%20uses%200%20in%20its%20place%20for%20an%20integer%20array%2C%20so%20the%20chunk%20was%20skipped.%20Nothing%20is%20lost%20here%3B%20but%20the%20v2%20specification%20leaves%20a%20missing%20chunk%20with%20a%20%60null%60%20fill%20undefined%2C%20so%20another%20reader%20could%20disagree.%20**A%20missing%20chunk%20reads%20as%20the%20fill%20value**%3A%20unit%201.3%20meets%20the%20same%20rule%20in%20ARCO-ERA5%2C%20where%20a%20missing%20chunk%20reads%20as%20NaN.%0A%0A%20%20%20%20%23%23%202.4%20The%20trap%20the%20code%20comments%20warn%20about%0A%0A%20%20%20%20data-pipelines%20pins%20the%20format%20with%20the%20comment%20%60GUARD%3A%20pin%20zarr_format%3D2%3B%20the%20v3%20default%20rejects%20the%20carried-over%20numcodecs%20Blosc%20encoding.%60%20Here%20is%20that%20failure%20happening%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_4_trap_code_comments(xr_store)%3A%0A%20%20%20%20opened%20%3D%20xr.open_zarr(xr_store)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22encoding%20carried%20by%20the%20opened%20variable%3A%22%2C%0A%20%20%20%20%20%20%20%20opened.ndvi.encoding.get(%22compressors%22)%20or%20opened.ndvi.encoding.get(%22compressor%22)%2C%0A%20%20%20%20)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20opened.to_zarr(WORK%20%2F%20%22rewrite_v3.zarr%22%2C%20mode%3D%22w%22%2C%20zarr_format%3D3)%0A%20%20%20%20except%20Exception%20as%20exc%3A%0A%20%20%20%20%20%20%20%20print(f%22%5Cn%7Btype(exc).__name__%7D%3A%20%7Bexc%7D%22)%0A%0A%20%20%20%20opened.drop_encoding().to_zarr(WORK%20%2F%20%22rewrite_v3.zarr%22%2C%20mode%3D%22w%22%2C%20zarr_format%3D3)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22%5Cnafter%20.drop_encoding()%3A%20written%20as%20format%203%20-%3E%22%2C%0A%20%20%20%20%20%20%20%20sorted(p.name%20for%20p%20in%20(WORK%20%2F%20%22rewrite_v3.zarr%22).iterdir())%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20When%20xarray%20opens%20a%20store%2C%20it%20remembers%20each%20variable's%20compressor%20in%20%60.encoding%60.%20A%20format-2%20Blosc%20compressor%20is%20not%20a%20valid%20format-3%20codec%2C%20so%20a%20naive%20round%20trip%20fails.%20That's%20why%20%60save_zarr%60%20calls%20%60.drop_encoding()%60%2C%20and%20it's%20one%20concrete%20reason%20a%20v2%20%E2%86%92%20v3%20migration%20is%20real%20work%20rather%20than%20a%20flag%20change.%0A%0A%20%20%20%20%23%20Part%203%3A%20the%20science%2C%20normalising%20MODIS%20NDVI%20into%20the%20lake%0A%0A%20%20%20%20%23%23%203.1%20What%20we're%20storing%0A%0A%20%20%20%20**NDVI**%20%3D%20(NIR%20%E2%88%92%20Red)%20%2F%20(NIR%20%2B%20Red).%20Healthy%20canopy%20is%20around%200.8%2C%20bare%20soil%20around%200.1%E2%80%930.2%2C%20and%20water%20and%20snow%20are%20below%200.%0A%0A%20%20%20%20**MOD13A1**%20(Terra)%20is%20a%20**16-day%20composite%20at%20500%20m**.%20Each%20pixel%20is%20chosen%20by%20*constrained-view%20maximum%20value%20compositing*%20(CV-MVC)%3A%20of%20the%20two%20highest-NDVI%20candidates%2C%20keep%20the%20one%20viewed%20closer%20to%20nadir%20(straight%20down).%20Clouds%20lower%20NDVI%2C%20so%20preferring%20high%20values%20is%20a%20cheap%20cloud%20filter.%20Since%20Collection%206%20the%20candidates%20are%20two%208-day%20surface-reflectance%20composites%2C%20already%20cloud-screened%20upstream%2C%20so%20this%20is%20a%20tie-break%20between%20at%20most%20two%20observations.%20When%20fewer%20than%20two%20candidates%20have%20good%20quality%2C%20it%20falls%20back%20to%20plain%20maximum-value%20compositing%2C%20so%20not%20every%20pixel%20was%20chosen%20by%20view%20angle.%0A%0A%20%20%20%20It%20ships%20as%20int16%20counts%3A%20scale%20%C3%970.0001%2C%20fill%20%E2%88%923000%2C%20valid%20range%20%E2%88%922000%20to%2010000%20(%E2%88%920.2%20to%201.0).%20A%20separate%20**pixel-reliability**%20band%20ranks%20each%20pixel%200%20good%2C%201%20marginal%2C%202%20snow%20or%20ice%2C%203%20cloud.%0A%0A%20%20%20%20The%20lake's%20promise%3A%20*every%20NDVI%20store%20answers%20%22clear-sky%20greenness%20of%20this%200.005%C2%B0%20cell%20in%20this%20composite%22%20in%20the%20same%20units%2C%20on%20the%20same%20grid%20and%20calendar%2C%20with%20NaN%20meaning%20%22don't%20trust%20it%22.*%0A%0A%20%20%20%20%23%23%203.2%20The%20composite%20calendar%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_2_composite_calendar()%3A%0A%20%20%20%20from%20tf_data_ml_utils.nasa_ingestion.instruments%20import%20MYD13A1%0A%0A%20%20%20%20for%20_name%2C%20product%20in%20((%22Terra%20MOD13A1%22%2C%20MOD13A1)%2C%20(%22Aqua%20%20MYD13A1%22%2C%20MYD13A1))%3A%0A%20%20%20%20%20%20%20%20for%20year%20in%20(2023%2C%202024)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20steps%20%3D%20product.step_dates(pd.Timestamp(f%22%7Byear%7D-01-01%22)%2C%20pd.Timestamp(f%22%7Byear%7D-12-31%22))%0A%20%20%20%20%20%20%20%20%20%20%20%20next_first%20%3D%20product.step_dates(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pd.Timestamp(f%22%7Byear%20%2B%201%7D-01-01%22)%2C%20pd.Timestamp(f%22%7Byear%20%2B%201%7D-12-31%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20)%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20window%20%3D%20(pd.Timestamp(f%22%7Byear%7D-12-31%22)%20-%20steps%5B-1%5D).days%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B_name%7D%20%7Byear%7D%3A%20%7Blen(steps)%7D%20composites%2C%20first%20DOYs%20%7Blist(steps.dayofyear%5B%3A3%5D)%7D%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22last%20starts%20DOY%20%7Bsteps%5B-1%5D.dayofyear%7D%20(%7Bsteps%5B-1%5D%3A%25d%20%25b%7D)%3B%20search%20window%20capped%20at%20%7Bwindow%7D%20days%3B%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22time%20step%20to%20the%20next%20start%20%7B(next_first%20-%20steps%5B-1%5D).days%7D%20days%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20from%20tf_data_ml_utils.nasa_ingestion.instruments.base%20import%20composite_calendar_steps%0A%0A%20%20%20%20show(composite_calendar_steps)%0A%20%20%20%20from%20tf_data_ml_utils.nasa_ingestion.instruments.base%20import%20composite_search_end%0A%0A%20%20%20%20show(composite_search_end)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Each%20satellite%20has%2023%20composite%20starts%20a%20year%2C%20and%20the%20calendar%20restarts%20on%201%20January.%20Aqua%20is%20offset%20by%208%20days%2C%20so%20Terra%20and%20Aqua%20together%20give%20a%20new%20composite%20roughly%20every%208%20days.%0A%0A%20%20%20%20**The%20year%20end.**%20%60composite_search_end%60%20(printed%20above)%20caps%20every%20*search%20window*%20at%2031%20December%2C%20so%20Aqua's%20day-361%20search%20covers%20only%205%20or%206%20days.%20Is%20NASA's%20composite%20that%20short%3F%20No.%20NASA%20CMR's%20granule%20metadata%20for%20tile%20h10v05%20(one%20character%20is%20one%20day%20in%20the%20diagram)%3A%0A%0A%20%20%20%20%7C%20Granule%20%7C%20Starts%20%7C%20Ends%20%7C%0A%20%20%20%20%7C---%7C---%7C---%7C%0A%20%20%20%20%7C%20MOD13A1.A2023353%20%7C%202023-12-19%20%7C%202024-01-03%20%7C%0A%20%20%20%20%7C%20MOD13A1.A2024001%20%7C%202024-01-01%20%7C%202024-01-16%20%7C%0A%20%20%20%20%7C%20MYD13A1.A2023361%20%7C%202023-12-27%20%7C%202024-01-11%20%7C%0A%20%20%20%20%7C%20MYD13A1.A2024009%20%7C%202024-01-09%20%7C%202024-01-24%20%7C%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2031%20Dec%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%8A%0A%20%20%20%20Terra%20A2023353%20%20%7C%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2019%20Dec%20to%203%20Jan%0A%20%20%20%20Terra%20A2024001%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%7C%20%20%20%20%20%20%20%20%20%20%201%20Jan%20to%2016%20Jan%0A%20%20%20%20Aqua%20%20A2023361%20%20%20%20%20%20%20%20%20%20%7C%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2027%20Dec%20to%2011%20Jan%0A%20%20%20%20Aqua%20%20A2024009%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%7C%20%20%209%20Jan%20to%2024%20Jan%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%8A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20our%20search%20windows%20stop%20here%3B%20NASA's%20composites%20do%20not%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Every%20composite%20is%20a%20full%2016%20days%2C%20and%20CMR%20matches%20granules%20by%20overlap%2C%20so%20our%20short%20window%20still%20finds%20the%20whole%20granule.%20Consecutive%20composites%20of%20one%20satellite%20**overlap**%20at%20the%20year%20boundary%20(Terra's%20share%201%E2%80%933%20January%2C%20Aqua's%209%E2%80%9311%20January)%2C%20so%20a%201%20January%20search%20returns%20two%20granules%2C%20and%20%60_keep_requested_composite%60%20keeps%20only%20the%20one%20dated%201%20January.%20The%20%60time%60%20axis%20steps%2013%20or%2014%20days%20across%20the%20boundary%20(printed%20above)%2C%20although%20every%20composite%20still%20spans%2016%20days.%20The%20CMR%20query%20needs%20no%20login%3B%20it's%20in%20%5Bscience.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.1-zarr-and-the-modis-ndvi-lake%2Fscience.md)'s%20worked%20answer%2C%20so%20this%20notebook%20stays%20offline.%0A%0A%20%20%20%20The%20standardise%20stage%20loads%20and%20writes%2023%20time%20steps%20at%20a%20time%20(%60_TIME_CHUNK%60)%2C%20which%20for%20one%2016-day%20product%20happens%20to%20be%20a%20year's%20worth.%20It%20serves%20every%20nasa%20product%2C%20though%3A%20for%20daily%20SMAP%20a%20block%20is%2023%20days.%0A%0A%20%20%20%20%23%23%203.3%20One%20grid%20per%20region%0A%0A%20%20%20%20MODIS%20ships%20on%20a%20**sinusoidal**%20projection%20in%2010%C2%B0%20%C3%97%2010%C2%B0%20tiles.%20We%20reproject%20every%20tile%20onto%20a%20latitude%E2%80%93longitude%20grid%20at%200.005%C2%B0%2C%20anchored%20at%20the%20region's%20bounding-box%20corner%20(%60build_target_grid%60%2C%20printed%20next)%2C%20so%20every%20year%20and%20every%20MODIS%20product%20for%20that%20region%20line%20up%20cell%20for%20cell.%20Two%20regions%20line%20up%20only%20if%20their%20corners%20differ%20by%20a%20whole%20number%20of%200.005%C2%B0%20steps%20on%20both%20axes.%20Sharing%20a%20coordinate%20system%20isn't%20sharing%20a%20grid%2C%20either%3A%20ERA5%20sits%20on%20a%200.25%C2%B0%20grid%2C%2050%20times%20coarser%2C%20so%20a%20MODIS%E2%80%93ERA5%20join%20still%20needs%20an%20explicit%20regridding%20or%20aggregation%20step.%0A%0A%20%20%20%20The%20next%20cell%20also%20prints%20a%20cell's%20size%20in%20metres.%20North%E2%80%93south%2C%200.005%C2%B0%20is%20always%20wider%20than%20MODIS's%20native%20cell.%20East%E2%80%93west%20it%20shrinks%20with%20cos(latitude)%2C%20so%20poleward%20of%20the%20printed%20crossover%20our%20cells%20are%20*narrower*%20than%20MODIS's%20and%20nearest%20neighbour%20repeats%20native%20columns.%20Nearest%20neighbour%20never%20invents%20values%2C%20but%20it%20neither%20averages%20nor%20adds%20detail%3A%20it%20samples.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_3_grid_per_region()%3A%0A%20%20%20%20from%20affine%20import%20Affine%0A%20%20%20%20from%20rasterio.warp%20import%20transform_bounds%0A%20%20%20%20from%20tf_data_ml_utils.nasa_ingestion.core.gridding%20import%20build_target_grid%0A%20%20%20%20from%20tf_data_ml_utils.nasa_ingestion.instruments.modis_hdf4%20import%20(%0A%20%20%20%20%20%20%20%20_GRID_UPPER_LEFT_M%2C%0A%20%20%20%20%20%20%20%20_SINU_CRS%2C%0A%20%20%20%20%20%20%20%20_TILE_EDGE_M%2C%0A%20%20%20%20)%0A%0A%20%20%20%20show(build_target_grid)%0A%20%20%20%20show(reproject_tile_window)%0A%0A%20%20%20%20H%2C%20V%2C%20N%20%3D%2010%2C%205%2C%202400%20%20%23%20tile%20h10v05%20covers%20Kansas%3B%202400%20x%202400%20cells%20of%20~463%20m%0A%20%20%20%20left%20%3D%20_GRID_UPPER_LEFT_M%5B0%5D%20%2B%20H%20*%20_TILE_EDGE_M%0A%20%20%20%20top%20%3D%20_GRID_UPPER_LEFT_M%5B1%5D%20-%20V%20*%20_TILE_EDGE_M%0A%20%20%20%20TILE_TRANSFORM%20%3D%20Affine(_TILE_EDGE_M%20%2F%20N%2C%200%2C%20left%2C%200%2C%20-_TILE_EDGE_M%20%2F%20N%2C%20top)%0A%20%20%20%20TILE_BOUNDS%20%3D%20(left%2C%20top%20-%20_TILE_EDGE_M%2C%20left%20%2B%20_TILE_EDGE_M%2C%20top)%0A%20%20%20%20M_PER_DEG%20%3D%20(%0A%20%20%20%20%20%20%20%20_TILE_EDGE_M%20%2F%2010%0A%20%20%20%20)%20%20%23%20a%20sinusoidal%20tile%20is%2010%20degrees%20of%20arc%2C%20so%20this%20is%20metres%20per%20degree%0A%20%20%20%20native%20%3D%20_TILE_EDGE_M%20%2F%20N%0A%20%20%20%20print(f%22native%20cell%20size%3A%20%7Bnative%3A.1f%7D%20m%22)%0A%20%20%20%20for%20lat%20in%20(0%2C%2038.5%2C%2045%2C%2050)%3A%0A%20%20%20%20%20%20%20%20ew%20%3D%200.005%20*%20M_PER_DEG%20*%20np.cos(np.radians(lat))%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22lat%20%7Blat%3A%3E4%7D%3A%20a%200.005%20deg%20cell%20is%20%7Bew%3A.0f%7D%20m%20E-W%20(%7Bew%20%2F%20native%3A.2f%7Dx%20native)%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B0.005%20*%20M_PER_DEG%3A.0f%7D%20m%20N-S%20(%7B0.005%20*%20M_PER_DEG%20%2F%20native%3A.2f%7Dx%20native)%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20print(f%22E-W%20crossover%3A%20%7Bnp.degrees(np.arccos(native%20%2F%20(0.005%20*%20M_PER_DEG)))%3A.1f%7D%20deg%20latitude%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22h10v05%20in%20lon%2Flat%3A%22%2C%0A%20%20%20%20%20%20%20%20%5Bround(b%2C%201)%20for%20b%20in%20transform_bounds(_SINU_CRS%2C%20%22EPSG%3A4326%22%2C%20*TILE_BOUNDS)%5D%2C%0A%20%20%20%20)%0A%0A%20%20%20%20BBOX%20%3D%20(-99.5%2C%2038.0%2C%20-98.5%2C%2039.0)%20%20%23%20a%201%20x%201%20degree%20box%20of%20central%20Kansas%0A%20%20%20%20LATS%2C%20LONS%2C%20_%20%3D%20build_target_grid(BBOX%2C%200.005)%0A%20%20%20%20print(%22target%20grid%3A%22%2C%20len(LATS)%2C%20%22x%22%2C%20len(LONS)%2C%20%22%7C%20first%20centres%3A%22%2C%20LATS%5B%3A2%5D%2C%20LONS%5B%3A2%5D)%0A%20%20%20%20return%20BBOX%2C%20LATS%2C%20LONS%2C%20N%2C%20TILE_BOUNDS%2C%20TILE_TRANSFORM%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203.4%20A%20synthetic%20composite%20through%20the%20real%20chain%0A%0A%20%20%20%20We%20can't%20download%20real%20MOD13A1%20without%20an%20Earthdata%20login%2C%20so%20we%20**build%20a%20realistic%20synthetic%20tile**%20and%20push%20it%20through%20the%20production%20functions%2C%20in%20the%20same%20order%20the%20ingester%20calls%20them%3A%0A%0A%20%20%20%20-%20a%20crop%20field%20gradient%3B%0A%20%20%20%20-%20a%20cloud%20bank%2C%20rank%203%2C%20whose%20NDVI%20(0.68)%20still%20looks%20plausible%3B%0A%20%20%20%20-%20a%20snow%20patch%2C%20rank%202%3B%0A%20%20%20%20-%20a%20strip%20with%20no%20data%3A%20NDVI%20filled%20with%20%E2%88%923000%20and%20pixel%20reliability%20set%20to%20%E2%88%921%2C%20the%20MOD13%20fill%20rank%20(the%20User%20Guide%20gives%20the%20band's%20valid%20range%20as%200%E2%80%933%20and%20its%20fill%20value%20as%20%E2%88%921).%0A%0A%20%20%20%20All%20four%20sit%20inside%20the%20Kansas%20box%20we%20grid%2C%20and%20the%20next%20cell%20counts%20them%20after%20reprojection%2C%20so%20you%20can%20check%20they%20really%20appear%20in%20the%20panels%20below.%0A%0A%20%20%20%20The%20scaling%20step%20is%20copied%20from%20%60_mosaic_tile%60%2C%20shown%20next.%20That%20method%20needs%20real%20HDF%20files%2C%20so%20we%20run%20its%20two%20key%20lines%20ourselves.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_4_synthetic_composite_through()%3A%0A%20%20%20%20from%20tf_data_ml_utils.nasa_ingestion.instruments.modis_hdf4%20import%20ModisHdf4Product%0A%0A%20%20%20%20lines%2C%20start%20%3D%20inspect.getsourcelines(ModisHdf4Product._mosaic_tile)%0A%20%20%20%20i0%20%3D%20next(i%20for%20i%2C%20line%20in%20enumerate(lines)%20if%20%22fill_int%20%3D%20int(%22%20in%20line)%0A%20%20%20%20i1%20%3D%20next(i%20for%20i%2C%20line%20in%20enumerate(lines)%20if%20%22acc%5Bm%5D%20%3D%20scaled%5Bm%5D%22%20in%20line)%0A%20%20%20%20rel_path%20%3D%20%22tf_data_ml_utils%2Fnasa_ingestion%2Finstruments%2Fmodis_hdf4.py%22%0A%20%20%20%20url%20%3D%20f%22https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2F%7BTFDMU_SHA%7D%2F%7Brel_path%7D%23L%7Bstart%20%2B%20i0%7D-L%7Bstart%20%2B%20i1%7D%22%0A%20%20%20%20display(%0A%20%20%20%20%20%20%20%20Markdown(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%5B%60modis_hdf4.py%60%20lines%20%7Bstart%20%2B%20i0%7D%E2%80%93%7Bstart%20%2B%20i1%7D%5D(%7Burl%7D)%5Cn%60%60%60python%5Cn%7B''.join(lines%5Bi0%20%3A%20i1%20%2B%201%5D)%7D%60%60%60%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20s3_4_synthetic_composite_through_2(%0A%20%20%20%20BBOX%2C%0A%20%20%20%20LATS%2C%0A%20%20%20%20LONS%2C%0A%20%20%20%20N%2C%0A%20%20%20%20TILE_BOUNDS%2C%0A%20%20%20%20TILE_TRANSFORM%2C%0A%20%20%20%20mo%2C%0A)%3A%0A%20%20%20%20from%20tf_data_ml_utils.nasa_ingestion.instruments.modis_hdf4%20import%20_SINU_CRS%0A%0A%20%20%20%20def%20synthetic_composite(date%2C%20seed)%3A%0A%20%20%20%20%20%20%20%20%23%20Build%20one%20MOD13A1-like%20composite%20and%20grid%20it%20exactly%20as%20the%20ingester%20does.%0A%20%20%20%20%20%20%20%20rng%20%3D%20np.random.default_rng(seed)%0A%20%20%20%20%20%20%20%20yy%2C%20xx%20%3D%20np.mgrid%5B0%3AN%2C%200%3AN%5D%0A%20%20%20%20%20%20%20%20ndvi_dn%20%3D%20np.clip(%0A%20%20%20%20%20%20%20%20%20%20%20%200.35%20%2B%200.45%20*%20np.sin(xx%20%2F%20180.0)%20**%202%20%2B%20rng.normal(0%2C%200.03%2C%20(N%2C%20N))%2C%20-0.2%2C%201.0%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20ndvi_dn%20%3D%20(ndvi_dn%20*%2010_000).astype(np.int16)%0A%20%20%20%20%20%20%20%20%23%20The%20Kansas%20box%20covers%20tile%20rows%20~240-480%20and%20columns%20~380-830%2C%20so%20every%20feature%20sits%20there.%0A%20%20%20%20%20%20%20%20cloud%20%3D%20(yy%20-%20330)%20**%202%20%2B%20(xx%20-%20640)%20**%202%20%3C%2040**2%0A%20%20%20%20%20%20%20%20snow%20%3D%20(yy%20-%20430)%20**%202%20%2B%20(xx%20-%20480)%20**%202%20%3C%2020**2%0A%20%20%20%20%20%20%20%20ndvi_dn%5Bcloud%5D%20%3D%206800%20%20%23%20plausible-looking%20NDVI%20under%20cloud%2C%20whatever%20its%20rank%20says%0A%20%20%20%20%20%20%20%20strip%20%3D%20yy%20%3C%20262%20%20%23%20a%20strip%20with%20no%20data%0A%20%20%20%20%20%20%20%20ndvi_dn%5Bstrip%5D%20%3D%20-3000%0A%20%20%20%20%20%20%20%20rel%20%3D%20np.zeros((N%2C%20N)%2C%20np.int8)%0A%20%20%20%20%20%20%20%20rel%5Brng.random((N%2C%20N))%20%3C%200.05%5D%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%201%20%20%23%20scattered%20marginal%20pixels%20first%2C%20so%20they%20can't%20hide%20the%20patches%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20rel%5Bcloud%5D%20%3D%203%20%20%23%20cloud%20bank%0A%20%20%20%20%20%20%20%20rel%5Bsnow%5D%20%3D%202%20%20%23%20snow%0A%20%20%20%20%20%20%20%20rel%5Bstrip%5D%20%3D%20-1%20%20%23%20MOD13's%20fill%20rank%3B%20127%20is%20only%20our%20gridding%20sentinel%20for%20no%20source%20tile%0A%20%20%20%20%20%20%20%20ny%2C%20nx%20%3D%20len(LATS)%2C%20len(LONS)%0A%20%20%20%20%20%20%20%20out%20%3D%20%7B%7D%0A%20%20%20%20%20%20%20%20for%20raw%2C%20arr%2C%20fill%2C%20is_qc%20in%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22500m%2016%20days%20NDVI%22%2C%20ndvi_dn%2C%20-3000%2C%20False)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22500m%2016%20days%20pixel%20reliability%22%2C%20rel%2C%20127%2C%20True)%2C%0A%20%20%20%20%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20patch%2C%20(r0%2C%20r1%2C%20c0%2C%20c1)%20%3D%20reproject_tile_window(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arr%2C%20TILE_TRANSFORM%2C%20_SINU_CRS%2C%20TILE_BOUNDS%2C%20BBOX%2C%200.005%2C%20ny%2C%20nx%2C%20fill%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20values%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20patch%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20is_qc%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%20np.where(patch%20%3D%3D%20fill%2C%20np.nan%2C%20patch.astype(np.float32)%20*%200.0001)%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20out%5BMOD13A1.canonical_name(raw)%5D%20%3D%20((%22time%22%2C%20%22latitude%22%2C%20%22longitude%22)%2C%20values%5BNone%5D)%0A%20%20%20%20%20%20%20%20return%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%20%20%20%20out%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20coords%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22time%22%3A%20%5Bpd.Timestamp(date)%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22latitude%22%3A%20LATS.astype(%22float32%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22longitude%22%3A%20LONS.astype(%22float32%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20show(MOD13A1.apply_qa)%0A%20%20%20%20c1%20%3D%20MOD13A1.apply_qa(synthetic_composite(%222024-06-09%22%2C%201))%0A%20%20%20%20c2%20%3D%20MOD13A1.apply_qa(synthetic_composite(%222024-06-25%22%2C%202))%0A%0A%20%20%20%20ndvi_name%20%3D%20MOD13A1.canonical_name(%22500m%2016%20days%20NDVI%22)%0A%20%20%20%20rel_name%20%3D%20MOD13A1.canonical_name(%22500m%2016%20days%20pixel%20reliability%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22gridded%20cells%3A%20%7Bc1.sizes%5B'latitude'%5D%7D%20x%20%7Bc1.sizes%5B'longitude'%5D%7D%20%7C%20%22%0A%20%20%20%20%20%20%20%20f%22cloud%20(rank%203)%3A%20%7Bint((c1%5Brel_name%5D%20%3D%3D%203).sum())%7D%20%7C%20snow%20(rank%202)%3A%20%7Bint((c1%5Brel_name%5D%20%3D%3D%202).sum())%7D%20%7C%20%22%0A%20%20%20%20%20%20%20%20f%22marginal%20(rank%201)%3A%20%7Bint((c1%5Brel_name%5D%20%3D%3D%201).sum())%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22no-data%20strip%20(rank%20-1)%3A%20%7Bint((c1%5Brel_name%5D%20%3D%3D%20-1).sum())%7D%20%7C%20%22%0A%20%20%20%20%20%20%20%20f%22no%20source%20tile%20(sentinel%20127)%3A%20%7Bint((c1%5Brel_name%5D%20%3D%3D%20127).sum())%7D%20%7C%20%22%0A%20%20%20%20%20%20%20%20f%22NDVI%20NaN%2C%20all%20causes%3A%20%7Bint(c1%5Bndvi_name%5D.isnull().sum())%7D%22%0A%20%20%20%20)%0A%20%20%20%20mo.output.append(c1)%0A%20%20%20%20return%20c1%2C%20c2%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Write%20the%20two%20composites%20into%20a%20local%20**ingest%20store**%20through%20the%20production%20writer%2C%20the%20same%20%60common.io%60%20calls%20the%20nasa%20ingester%20makes%2C%20then%20**standardise**%20them.%20(In%20production%20the%20nasa%20stages%20call%20%60append_zarr%60%20through%20a%20retry%20wrapper%2C%20%60stages%2Fingest%2Fstore.append_zarr%60%2C%20whose%20docstring%20reads%20%22core%20rolls%20back%20an%20uncommitted%20append%2C%20so%20retry%22%3A%20the%20rollback%20in%202.3%20is%20what%20makes%20retrying%20safe.)%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_4_synthetic_composite_through_3(c1%2C%20c2%2C%20io%2C%20mo)%3A%0A%20%20%20%20ingest_store%20%3D%20WORK%20%2F%20%22lake%22%20%2F%20%22MOD13A1_ingest.zarr%22%0A%20%20%20%20ingest_store.parent.mkdir(exist_ok%3DTrue)%0A%20%20%20%20io.save_zarr(%0A%20%20%20%20%20%20%20%20c1%2C%0A%20%20%20%20%20%20%20%20ingest_store%2C%0A%20%20%20%20%20%20%20%20%7B%22time%22%3A%2023%2C%20%22latitude%22%3A%20-1%2C%20%22longitude%22%3A%20-1%7D%2C%0A%20%20%20%20%20%20%20%20append_dims%3Dfrozenset(%7B%22time%22%7D)%2C%0A%20%20%20%20%20%20%20%20require_daily_continuity%3DFalse%2C%0A%20%20%20%20)%0A%20%20%20%20io.append_zarr(c2%2C%20ingest_store%2C%20dim%3D%22time%22%2C%20raise_on_gap%3DFalse%2C%20require_daily_continuity%3DFalse)%0A%20%20%20%20print(%22ingest%20store%20variables%3A%22%2C%20list(io.load_zarr(ingest_store).data_vars))%0A%0A%20%20%20%20show(standardise)%0A%20%20%20%20show(MOD13A1.quality_mask)%0A%20%20%20%20std%20%3D%20standardise(io.load_zarr(ingest_store).load()%2C%20MOD13A1)%0A%20%20%20%20mo.output.append(std)%0A%20%20%20%20return%20ingest_store%2C%20std%0A%0A%0A%40app.cell%0Adef%20s3_4_synthetic_composite_through_4(ingest_store%2C%20io%2C%20std)%3A%0A%20%20%20%20from%20matplotlib.colors%20import%20BoundaryNorm%2C%20ListedColormap%0A%20%20%20%20from%20matplotlib.patches%20import%20Patch%0A%0A%20%20%20%20RANKS%20%3D%20%7B%0A%20%20%20%20%20%20%20%20-1%3A%20(%22no%20data%22%2C%20%22%23bdbdbd%22)%2C%0A%20%20%20%20%20%20%20%200%3A%20(%22good%22%2C%20%22%231a9850%22)%2C%0A%20%20%20%20%20%20%20%201%3A%20(%22marginal%22%2C%20%22%23a6d96a%22)%2C%0A%20%20%20%20%20%20%20%202%3A%20(%22snow%22%2C%20%22%234575b4%22)%2C%0A%20%20%20%20%20%20%20%203%3A%20(%22cloud%22%2C%20%22%23f46d43%22)%2C%0A%20%20%20%20%7D%0A%20%20%20%20rank_cmap%20%3D%20ListedColormap(%5Bcolour%20for%20_%2C%20colour%20in%20RANKS.values()%5D)%0A%20%20%20%20rank_norm%20%3D%20BoundaryNorm(np.arange(-1.5%2C%204.5)%2C%20rank_cmap.N)%0A%20%20%20%20first%20%3D%20io.load_zarr(ingest_store).isel(time%3D0)%0A%20%20%20%20fig%2C%20axes%20%3D%20plt.subplots(1%2C%203%2C%20figsize%3D(15%2C%204.2)%2C%20constrained_layout%3DTrue)%0A%20%20%20%20first%5BMOD13A1.canonical_name(%22500m%2016%20days%20NDVI%22)%5D.plot(ax%3Daxes%5B0%5D%2C%20vmin%3D0%2C%20vmax%3D1%2C%20cmap%3D%22YlGn%22)%0A%20%20%20%20axes%5B0%5D.set_title(%22ingest%20store%3A%20NDVI%20after%20apply_qa%22)%0A%20%20%20%20first%5BMOD13A1.canonical_name(%22500m%2016%20days%20pixel%20reliability%22)%5D.plot(%0A%20%20%20%20%20%20%20%20ax%3Daxes%5B1%5D%2C%20cmap%3Drank_cmap%2C%20norm%3Drank_norm%2C%20add_colorbar%3DFalse%0A%20%20%20%20)%0A%20%20%20%20axes%5B1%5D.legend(%0A%20%20%20%20%20%20%20%20handles%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20Patch(color%3Dcolour%2C%20label%3Df%22%7Brank%7D%20%7Bname%7D%22)%20for%20rank%2C%20(name%2C%20colour)%20in%20RANKS.items()%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20loc%3D%22lower%20right%22%2C%0A%20%20%20%20%20%20%20%20fontsize%3D8%2C%0A%20%20%20%20)%0A%20%20%20%20axes%5B1%5D.set_title(%22pixel%20reliability%20rank%22)%0A%20%20%20%20std.ndvi.isel(time%3D0).plot(ax%3Daxes%5B2%5D%2C%20vmin%3D0%2C%20vmax%3D1%2C%20cmap%3D%22YlGn%22)%0A%20%20%20%20axes%5B2%5D.set_title(%22standardised%20store%3A%20clear-sky%20NDVI%20only%22)%0A%20%20%20%20fig.suptitle(%22Cloud%20NDVI%200.68%20passes%20apply_qa%3B%20only%20the%20reliability%20rank%20removes%20it%22)%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Read%20the%20left%20and%20right%20panels%20together.**%20The%20cloud%20bank's%20NDVI%20of%200.68%20sails%20through%20%60apply_qa%60%2C%20because%20it's%20a%20physically%20possible%20value.%20Only%20the%20reliability%20band%20in%20the%20middle%20panel%20catches%20it.%20%60standardise%60%20removes%20it%2C%20along%20with%20the%20snow%20patch%2C%20and%20keeps%20the%20marginal%20pixels%20(rank%201).%20The%20no-data%20strip%20along%20the%20top%20was%20NaN%20from%20the%20moment%20it%20was%20gridded%2C%20and%20its%20reliability%20rank%20of%20%E2%88%921%20lies%20outside%20the%20kept%200%E2%80%931%2C%20so%20%60standardise%60%20would%20mask%20it%20even%20if%20its%20NDVI%20weren't%20already%20missing.%20The%20counts%20printed%20above%20the%20dataset%20confirm%20all%20three%20features%20landed%20inside%20the%20box.%20They%20also%20separate%20the%20strip%20(rank%20%E2%88%921)%20from%20cells%20no%20source%20tile%20covers%2C%20which%20get%20our%20gridding%20sentinel%20127%3B%20inside%20this%20box%20there%20are%20none.%20Every%20NaN%20here%20is%20the%20strip%3A%20the%20synthetic%20NDVI%20never%20leaves%20%E2%88%921%E2%80%A61%2C%20so%20%60apply_qa%60's%20range%20check%20removes%20nothing%20in%20this%20example.%0A%0A%20%20%20%20That's%20why%20the%20**ingest%20store%20keeps%20the%20QC%20band**%20(it's%20faithful)%2C%20and%20the%20**standardised%20store%20applies%20it%20and%20drops%20it**%20(it's%20opinionated).%20Changing%20our%20minds%20about%20marginal%20pixels%20means%20re-running%20%60standardise%60%2C%20not%20downloading%20again.%0A%0A%20%20%20%20%23%23%203.5%20Six%20pixels%2C%20every%20decision%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_5_six_pixels_every()%3A%0A%20%20%20%20raw%20%3D%20np.array(%5B7234%2C%201850%2C%20-3000%2C%2010500%2C%206100%2C%206800%5D%2C%20dtype%3Dnp.int16)%0A%20%20%20%20rel%20%3D%20np.array(%5B0%2C%201%2C%20-1%2C%200%2C%202%2C%203%5D%2C%20dtype%3Dnp.int8)%0A%20%20%20%20scaled%20%3D%20np.where(raw%20%3D%3D%20-3000%2C%20np.nan%2C%20raw.astype(np.float32)%20*%200.0001)%0A%20%20%20%20px%20%3D%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20MOD13A1.canonical_name(%22500m%2016%20days%20NDVI%22)%3A%20((%22time%22%2C%20%22x%22)%2C%20scaled%5BNone%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20MOD13A1.canonical_name(%22500m%2016%20days%20pixel%20reliability%22)%3A%20((%22time%22%2C%20%22x%22)%2C%20rel%5BNone%5D)%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20coords%3D%7B%22time%22%3A%20%5Bpd.Timestamp(%222024-06-25%22)%5D%7D%2C%0A%20%20%20%20)%0A%20%20%20%20after_qa%20%3D%20MOD13A1.apply_qa(px.copy())%0A%20%20%20%20after_std%20%3D%20standardise(after_qa%2C%20MOD13A1)%0A%20%20%20%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22raw%20int16%22%3A%20raw%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22reliability%22%3A%20rel%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22scaled%22%3A%20scaled.round(4)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22after%20apply_qa%22%3A%20after_qa%5BMOD13A1.canonical_name(%22500m%2016%20days%20NDVI%22)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20.values%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20.round(4)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22after%20standardise%22%3A%20after_std.ndvi.values%5B0%5D.round(4)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Part%204%3A%20paths%20not%20travelled%2C%20tried%20live%0A%0A%20%20%20%20%23%23%204.1%20Store%20int16%20with%20a%20CF%20scale%20factor%20instead%20of%20float32%3F%0A%0A%20%20%20%20Our%20lake%20stores%20float32%20(4%20bytes).%20The%20source%20was%20int16%20(2%20bytes).%20xarray%20can%20store%20int16%20and%20decode%20it%20on%20read%20using%20%5BCF%5D(https%3A%2F%2Fcfconventions.org%2FData%2Fcf-conventions%2Fcf-conventions-1.11%2Fcf-conventions.html%23packed-data)%20%60scale_factor%60%20and%20%60_FillValue%60%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_1_store_int16_cf(std)%3A%0A%20%20%20%20a_1%20%3D%20std%5B%5B%22ndvi%22%5D%5D.drop_encoding()%0A%20%20%20%20a_1.to_zarr(WORK%20%2F%20%22f32.zarr%22%2C%20mode%3D%22w%22%2C%20zarr_format%3D2)%0A%20%20%20%20a_1.to_zarr(%0A%20%20%20%20%20%20%20%20WORK%20%2F%20%22i16.zarr%22%2C%0A%20%20%20%20%20%20%20%20mode%3D%22w%22%2C%0A%20%20%20%20%20%20%20%20zarr_format%3D2%2C%0A%20%20%20%20%20%20%20%20encoding%3D%7B%22ndvi%22%3A%20%7B%22dtype%22%3A%20%22int16%22%2C%20%22scale_factor%22%3A%200.0001%2C%20%22_FillValue%22%3A%20-3000%7D%7D%2C%0A%20%20%20%20)%0A%20%20%20%20back%20%3D%20xr.open_zarr(WORK%20%2F%20%22i16.zarr%22).ndvi.values%0A%20%20%20%20print(f%22float32%20store%3A%20%7Bdu(WORK%20%2F%20'f32.zarr')%3A%3E7%7D%20bytes%22)%0A%20%20%20%20print(f%22int16%20%2B%20CF%3A%20%20%20%20%7Bdu(WORK%20%2F%20'i16.zarr')%3A%3E7%7D%20bytes%22)%0A%20%20%20%20print(%22max%20round-trip%20error%3A%22%2C%20float(np.nanmax(np.abs(back%20-%20a_1.ndvi.values))))%0A%20%20%20%20print(%22NaN%20pattern%20preserved%3A%22%2C%20bool((np.isnan(back)%20%3D%3D%20np.isnan(a_1.ndvi.values)).all()))%0A%20%20%20%20print(%22decoded%20dtype%3A%22%2C%20back.dtype%2C%20%22%7C%20our%20float32%20store%3A%22%2C%20a_1.ndvi.dtype)%0A%20%20%20%20return%20(a_1%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20It's%20smaller%20and%20loses%20nothing%20here%2C%20because%20the%20values%20started%20life%20as%20int16%2F10000.%20The%20cost%20is%20that%20**every%20reader%20must%20decode%20correctly**.%20xarray%20does%2C%20but%20a%20raw%20%60zarr%60%20or%20GDAL%20reader%20sees%20counts.%20Normalising%20to%20float32%20at%20ingest%20removes%20that%20trap%20for%20every%20consumer%3A%20it%20trades%20bytes%20for%20safety.%0A%0A%20%20%20%20Notice%20the%20decoded%20dtype%2C%20too%3A%20float64%2C%20because%20the%20scale%20factor%20was%20stored%20as%20a%20Python%20float.%20That's%20half%20the%20bytes%20on%20disk%20but%20*twice*%20our%20float32's%20memory%20on%20read%2C%20and%20writing%20%60scale_factor%60%20as%20%60np.float32(1e-4)%60%20doesn't%20help%3A%20xarray's%20Zarr%20backend%20stores%20attributes%20as%20plain%20JSON%20numbers%2C%20so%20a%20reader%20that%20wants%20float32%20must%20cast%20the%20decoded%20array%20itself.%0A%0A%20%20%20%20%23%23%204.2%20Which%20compressor%3F%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_2_compressor(a_1)%3A%0A%20%20%20%20from%20numcodecs%20import%20Blosc%2C%20Zstd%0A%0A%20%20%20%20for%20k%2C%20(_name%2C%20compressor)%20in%20enumerate(%0A%20%20%20%20%20%20%20%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Blosc%20lz4%2C%20shuffle%20(xarray%20default)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Blosc(cname%3D%22lz4%22%2C%20clevel%3D5%2C%20shuffle%3DBlosc.SHUFFLE)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22Blosc%20lz4%2C%20no%20shuffle%22%2C%20Blosc(cname%3D%22lz4%22%2C%20clevel%3D5%2C%20shuffle%3DBlosc.NOSHUFFLE))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22Blosc%20lz4%2C%20bitshuffle%22%2C%20Blosc(cname%3D%22lz4%22%2C%20clevel%3D5%2C%20shuffle%3DBlosc.BITSHUFFLE))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22Blosc%20zstd%2C%20shuffle%22%2C%20Blosc(cname%3D%22zstd%22%2C%20clevel%3D5%2C%20shuffle%3DBlosc.SHUFFLE))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22Zstd(level%3D0)%2C%20no%20shuffle%22%2C%20Zstd(level%3D0))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22Zstd(level%3D3)%2C%20no%20shuffle%22%2C%20Zstd(level%3D3))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22none%22%2C%20None)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20path%20%3D%20WORK%20%2F%20f%22comp_%7Bk%7D.zarr%22%0A%20%20%20%20%20%20%20%20a_1.to_zarr(path%2C%20mode%3D%22w%22%2C%20zarr_format%3D2%2C%20encoding%3D%7B%22ndvi%22%3A%20%7B%22compressor%22%3A%20compressor%7D%7D)%0A%20%20%20%20%20%20%20%20print(f%22%7B_name%3A36%7D%20%7Bdu(path%20%2F%20'ndvi')%3A%3E8%7D%20bytes%20(the%20ndvi%20array%20only)%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Each%20row%20changes%20one%20thing.%20Byte-shuffle%20reorders%20the%20bytes%20of%20each%20float%20so%20that%20bytes%20of%20the%20same%20rank%20sit%20together.%20It%20helps%20LZ4%20here%20(rows%201%20and%202)%2C%20and%20bitshuffle%20does%20no%20better%20than%20byte-shuffle.%20But%20plain%20Zstd%20without%20any%20shuffle%20beats%20every%20shuffled%20variant.%20These%20floats%20came%20from%20int16%20%2F%2010000%20plus%20noise%2C%20so%20their%20low%20bytes%20look%20random%3A%20shuffle%20helps%20when%20neighbouring%20values%20share%20their%20high%20bytes%2C%20and%20on%20noisy%20float32%20its%20benefit%20is%20modest.%0A%0A%20%20%20%20Two%20more%20things%20to%20read%20off%20the%20table.%20%60Zstd(level%3D0)%60%20and%20%60Zstd(level%3D3)%60%20give%20identical%20bytes%2C%20because%20zstd%20reads%20level%200%20as%20%22use%20the%20default%22%2C%20which%20is%203.%20And%20no%20codec%20matches%20quantising%3A%20the%20whole%20int16%20%2B%20CF%20store%20in%204.1%20is%20smaller%20than%20the%20%60ndvi%60%20array%20alone%20under%20any%20codec%20here.%20LZ4%20is%20designed%20for%20speed%20and%20Zstd%20for%20ratio%3B%20this%20table%20measures%20only%20size.%0A%0A%20%20%20%20%23%23%204.3%20Format%203%20with%20sharding%0A%0A%20%20%20%20Format%203's%20headline%20feature%20is%20**sharding**%3A%20many%20small%20chunks%20packed%20into%20one%20object%2C%20with%20an%20index%2C%20so%20readers%20can%20still%20fetch%20one%20chunk%20with%20a%20byte-range%20request.%20That%20means%20fewer%20S3%20objects%20and%20fewer%20LIST%20and%20PUT%20calls%20(%5Bsharding%20spec%5D(https%3A%2F%2Fzarr-specs.readthedocs.io%2Fen%2Flatest%2Fv3%2Fcodecs%2Fsharding-indexed%2Findex.html)).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_3_format_3_sharding(a_1)%3A%0A%20%20%20%20a_1.to_zarr(%0A%20%20%20%20%20%20%20%20WORK%20%2F%20%22v3_plain.zarr%22%2C%20mode%3D%22w%22%2C%20zarr_format%3D3%2C%20encoding%3D%7B%22ndvi%22%3A%20%7B%22chunks%22%3A%20(1%2C%2050%2C%2050)%7D%7D%0A%20%20%20%20)%0A%20%20%20%20a_1.to_zarr(%0A%20%20%20%20%20%20%20%20WORK%20%2F%20%22v3_sharded.zarr%22%2C%0A%20%20%20%20%20%20%20%20mode%3D%22w%22%2C%0A%20%20%20%20%20%20%20%20zarr_format%3D3%2C%0A%20%20%20%20%20%20%20%20encoding%3D%7B%22ndvi%22%3A%20%7B%22chunks%22%3A%20(1%2C%2050%2C%2050)%2C%20%22shards%22%3A%20(2%2C%20200%2C%20200)%7D%7D%2C%0A%20%20%20%20)%0A%20%20%20%20for%20_name%20in%20(%22v3_plain%22%2C%20%22v3_sharded%22)%3A%0A%20%20%20%20%20%20%20%20n%20%3D%20sum(%0A%20%20%20%20%20%20%20%20%20%20%20%201%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20f%20in%20(WORK%20%2F%20f%22%7B_name%7D.zarr%22%20%2F%20%22ndvi%22).rglob(%22*%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20f.is_file()%20and%20f.name%20!%3D%20%22zarr.json%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20print(f%22%7B_name%3A11%7D%20ndvi%20chunk%20objects%20on%20disk%3A%20%7Bn%7D%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22same%20values%3A%22%2C%0A%20%20%20%20%20%20%20%20bool(%0A%20%20%20%20%20%20%20%20%20%20%20%20np.allclose(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20xr.open_zarr(WORK%20%2F%20%22v3_plain.zarr%22).ndvi%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20xr.open_zarr(WORK%20%2F%20%22v3_sharded.zarr%22).ndvi%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20equal_nan%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Same%20data%2C%2032%20objects%20against%201.%20Notice%20the%20%60ZarrUserWarning%60%20printed%20above%3A%20zarr%20itself%20says%20consolidated%20metadata%20%22is%20currently%20not%20part%20in%20the%20Zarr%20format%203%20specification%22.%20It%20isn't%20part%20of%20the%20format%202%20specification%20either%3A%20in%20both%20formats%20it%20is%20a%20zarr-python%20convention%2C%20%60.zmetadata%60%20in%20v2%20and%20a%20key%20inside%20the%20root%20%60zarr.json%60%20in%20v3%20(a%20proposed%2C%20unratified%20extension).%20So%20the%20commit%20marker%20from%20Part%202%20would%20need%20redesigning%3A%20one%20real%20cost%20of%20migrating.%0A%0A%20%20%20%20The%20other%20cost%20is%20on%20the%20write%20side.%20A%20shard%20is%20one%20object%2C%20so%20appending%20one%2016-day%20step%20into%20a%20shard%20means%20rewriting%20the%20whole%20shard.%20Our%20pattern%20is%20append-one-step%2C%20so%20shard%20along%20%60time%60%20only%20as%20far%20as%20you%20are%20willing%20to%20rewrite.%0A%0A%20%20%20%20%23%23%204.4%20Why%20nearest%20neighbour%20for%20the%20reprojection%3F%0A%0A%20%20%20%20%60reproject_tile_window%60%20defaults%20to%20%60Resampling.nearest%60.%20Here's%20what%20bilinear%20would%20do%20to%20the%20reliability%20band%2C%20a%20**categorical**%20variable%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_4_nearest_neighbour_reprojection(%0A%20%20%20%20BBOX%2C%0A%20%20%20%20LATS%2C%0A%20%20%20%20LONS%2C%0A%20%20%20%20N%2C%0A%20%20%20%20TILE_BOUNDS%2C%0A%20%20%20%20TILE_TRANSFORM%2C%0A)%3A%0A%20%20%20%20from%20rasterio.warp%20import%20Resampling%0A%20%20%20%20from%20tf_data_ml_utils.nasa_ingestion.instruments.modis_hdf4%20import%20_SINU_CRS%0A%0A%20%20%20%20rng%20%3D%20np.random.default_rng(3)%0A%20%20%20%20rel_tile%20%3D%20rng.choice(np.array(%5B0%2C%201%2C%203%5D%2C%20dtype%3Dnp.int8)%2C%20size%3D(N%2C%20N)%2C%20p%3D%5B0.6%2C%200.2%2C%200.2%5D)%0A%20%20%20%20ny%2C%20nx%20%3D%20len(LATS)%2C%20len(LONS)%0A%20%20%20%20for%20method%20in%20(Resampling.nearest%2C%20Resampling.bilinear)%3A%0A%20%20%20%20%20%20%20%20patch%2C%20_%20%3D%20reproject_tile_window(%0A%20%20%20%20%20%20%20%20%20%20%20%20rel_tile.astype(np.float32)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20TILE_TRANSFORM%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20_SINU_CRS%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20TILE_BOUNDS%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20BBOX%2C%0A%20%20%20%20%20%20%20%20%20%20%20%200.005%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20ny%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20nx%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20127.0%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20resampling%3Dmethod%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20values%20%3D%20patch%5Bpatch%20!%3D%20127.0%5D%0A%20%20%20%20%20%20%20%20invented%20%3D%20~np.isin(values%2C%20%5B0%2C%201%2C%203%5D)%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bmethod.name%3A9%7D%20distinct%20values%3A%20%7Blen(np.unique(values))%3A%3E6%7D%20%20values%20that%20were%20never%20a%20real%20rank%3A%20%7Binvented.mean()%3A.1%25%7D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Bilinear%20interpolation%20invents%20ranks%20that%20never%20existed.%20A%20blend%20of%20%22good%22%20and%20%22cloud%22%20isn't%20%22marginal%22%2C%20it's%20nonsense.%20For%20NDVI%20itself%2C%20nearest%20neighbour%20also%20keeps%20each%20output%20value%20a%20**real%20observation**%20chosen%20by%20the%20compositing%2C%20rather%20than%20a%20blend%20of%20different%20days.%0A%0A%20%20%20%20%23%23%204.5%20Other%20containers%20we%20could%20have%20used%0A%0A%20%20%20%20%7C%20Alternative%20%7C%20Trade-off%20%7C%0A%20%20%20%20%7C---%7C---%7C%0A%20%20%20%20%7C%20NetCDF4%20%2F%20HDF5%2C%20one%20file%20per%20region%20%7C%20Familiar%2C%20but%20reading%20a%20small%20subset%20over%20S3%20needs%20byte-range%20tricks%2C%20and%20appending%20in%20place%20is%20unsafe%20%7C%0A%20%20%20%20%7C%20Cloud-Optimised%20GeoTIFF%20per%20composite%20%7C%20Great%20for%20one%20date%3B%20a%20time%20series%20becomes%20thousands%20of%20files%20plus%20a%20catalogue%20%7C%0A%20%20%20%20%7C%20Kerchunk%20%2F%20VirtualiZarr%20over%20the%20source%20HDFs%20%7C%20No%20copy%2C%20but%20it%20keeps%20the%20source's%20sinusoidal%20grid%2C%20int16%20counts%20and%20QC%3A%20exactly%20what%20ingest%20normalises%20away%20%7C%0A%20%20%20%20%7C%20%5BIcechunk%5D(https%3A%2F%2Ficechunk.io)%20%7C%20Real%20transactions%20on%20S3%2C%20replacing%20our%20staging%20and%20%60.zmetadata%60%20workarounds%2C%20at%20the%20cost%20of%20a%20new%20store%20format%20%7C%0A%0A%20%20%20%20%23%20Part%205%3A%20exercises%0A%0A%20%20%20%20Change%20one%20thing%2C%20predict%20the%20result%2C%20then%20run%20it.%0A%0A%20%20%20%201.%20In%201.1%2C%20change%20%60chunks%3D(2%2C%203)%60%20to%20%60(4%2C%206)%60.%20How%20many%20chunk%20files%20appear%3F%20Which%20reads%20would%20get%20slower%20over%20S3%3F%20*Check%3A*%20one%20chunk%20file%2C%20%600.0%60%3B%20every%20read%2C%20however%20small%2C%20now%20fetches%20the%20whole%20array.%0A%20%20%20%202.%20In%202.3%2C%20*don't*%20call%20%60_rollback_uncommitted%60.%20Call%20%60io.append_zarr(step(%222024-07-27%22%2C%200.9)%2C%20store%2C%20dim%3D%22time%22%2C%20raise_on_gap%3DFalse%2C%20require_daily_continuity%3DFalse)%60%20instead.%20How%20many%20steps%20does%20the%20store%20end%20with%2C%20and%20why%3F%20(Hint%3A%20read%20the%20first%20lines%20of%20%60append_zarr%60.)%20*Check%3A*%20three%20steps%2C%2006-09%2C%2006-25%20and%2007-27%3A%20%60append_zarr%60%20rolls%20back%20the%20uncommitted%2007-11%20first%2C%20then%20appends.%0A%20%20%20%203.%20In%203.4%2C%20change%20%60rel%5Bcloud%5D%20%3D%203%60%20to%20%60rel%5Bcloud%5D%20%3D%201%60%2C%20so%20the%20cloud%20bank%20keeps%20its%20NDVI%20of%200.68%20but%20is%20ranked%20marginal.%20What%20happens%20to%20the%20right-hand%20panel%3F%20Would%20you%20accept%20that%20for%20a%20crop-condition%20index%3F%20*Check%3A*%20the%20cloud%20bank%20reappears%20in%20the%20right-hand%20panel%20at%200.68%2C%20because%20rank%201%20is%20kept.%0A%20%20%20%204.%20Which%20single%20change%20saves%20the%20most%20bytes%3A%20the%20codec%2C%20the%20shuffle%2C%20or%20quantising%20to%20int16%20(4.1)%3F%20Predict%2C%20then%20compare%20the%204.1%20and%204.2%20outputs.%20*Check%3A*%20quantising%3B%20the%20whole%20int16%20store%20is%20smaller%20than%20the%20%60ndvi%60%20array%20alone%20under%20any%20codec%20in%204.2.%0A%20%20%20%205.%20In%204.3%2C%20set%20%60shards%60%20equal%20to%20%60chunks%60.%20What%20does%20that%20do%20to%20the%20object%20count%2C%20and%20why%20is%20that%20pointless%3F%20*Check%3A*%20still%2032%20objects%2C%20each%20now%20carrying%20its%20own%20shard%20index%2C%20so%20it%20is%20strictly%20worse%20than%20unsharded.%0A%20%20%20%206.%20MOD13's%2016-bit%20%60VI%20Quality%60%20band%20packs%20several%20fields%20into%20one%20integer%20(User%20Guide%20Table%205)%3A%20bits%200%E2%80%931%20MODLAND%20QA%2C%202%E2%80%935%20VI%20usefulness%2C%206%E2%80%937%20aerosol%20quantity%2C%208%20adjacent%20cloud%2C%2010%20mixed%20clouds%2C%2011%E2%80%9313%20land%2Fwater%2C%2014%20possible%20snow%20or%20ice%2C%2015%20possible%20shadow.%20Decode%20%60np.array(%5B2116%2C%2035977%5D%2C%20dtype%3Dnp.uint16)%60%20with%20%60np.right_shift%60%20and%20%60np.bitwise_and%60.%20Which%20pixel%20would%20you%20distrust%2C%20and%20why%3F%20*Check%3A*%202116%20gives%20MODLAND%200%2C%20usefulness%201%2C%20aerosol%201%2C%20land%2Fwater%201%20and%20every%20flag%200%3B%2035977%20gives%20MODLAND%201%2C%20usefulness%202%2C%20aerosol%202%2C%20mixed%20clouds%201%2C%20land%2Fwater%201%20and%20possible%20shadow%201.%0A%0A%20%20%20%20%23%20Sources%0A%0A%20%20%20%20-%20zarr-python%203.1.5%20quick%20start%3A%20https%3A%2F%2Fzarr.readthedocs.io%2Fen%2Fv3.1.5%2Fquick-start%2F%3B%20consolidated%20metadata%3A%20https%3A%2F%2Fzarr.readthedocs.io%2Fen%2Fv3.1.5%2Fuser-guide%2Fconsolidated_metadata%2F%3B%20performance%20and%20empty%20chunks%3A%20https%3A%2F%2Fzarr.readthedocs.io%2Fen%2Fv3.1.5%2Fuser-guide%2Fperformance%2F%0A%20%20%20%20-%20Zarr%20format%202%3A%20https%3A%2F%2Fzarr-specs.readthedocs.io%2Fen%2Flatest%2Fv2%2Fv2.0.html%3B%20format%203%3A%20https%3A%2F%2Fzarr-specs.readthedocs.io%2Fen%2Flatest%2Fv3%2Fcore%2Findex.html%3B%20sharding%3A%20https%3A%2F%2Fzarr-specs.readthedocs.io%2Fen%2Flatest%2Fv3%2Fcodecs%2Fsharding-indexed%2Findex.html%0A%20%20%20%20-%20xarray%202025.11.0%20I%2FO%3A%20https%3A%2F%2Fdocs.xarray.dev%2Fen%2Fv2025.11.0%2Fuser-guide%2Fio.html%23zarr%3B%20%60Dataset.to_zarr%60%3A%20https%3A%2F%2Fdocs.xarray.dev%2Fen%2Fv2025.11.0%2Fgenerated%2Fxarray.Dataset.to_zarr.html%3B%20%60open_zarr%60%3A%20https%3A%2F%2Fdocs.xarray.dev%2Fen%2Fv2025.11.0%2Fgenerated%2Fxarray.open_zarr.html%0A%20%20%20%20-%20Dask%20arrays%3A%20https%3A%2F%2Fdocs.dask.org%2Fen%2Fstable%2Farray.html%3B%20chunk%20sizes%3A%20https%3A%2F%2Fdocs.dask.org%2Fen%2Fstable%2Farray-best-practices.html%23select-a-good-chunk-size%0A%20%20%20%20-%20numcodecs%200.16.5%20Blosc%3A%20https%3A%2F%2Fnumcodecs.readthedocs.io%2Fen%2Fv0.16.5%2Fcompression%2Fblosc.html%0A%20%20%20%20-%20AWS%20S3%20consistency%20model%3A%20https%3A%2F%2Fdocs.aws.amazon.com%2FAmazonS3%2Flatest%2Fuserguide%2FWelcome.html%23ConsistencyModel%0A%20%20%20%20-%20rasterio%20reprojection%3A%20https%3A%2F%2Frasterio.readthedocs.io%2Fen%2Fstable%2Ftopics%2Freproject.html%0A%20%20%20%20-%20MOD13A1%20v061%3A%20https%3A%2F%2Flpdaac.usgs.gov%2Fproducts%2Fmod13a1v061%2F%2C%20MYD13A1%20v061%3A%20https%3A%2F%2Flpdaac.usgs.gov%2Fproducts%2Fmyd13a1v061%2F%2C%20the%20NASA%20CMR%20search%20API%3A%20https%3A%2F%2Fcmr.earthdata.nasa.gov%2Fsearch%2Fsite%2Fdocs%2Fsearch%2Fapi.html%2C%20and%20the%20MOD13%20User%20Guide%3A%20https%3A%2F%2Flpdaac.usgs.gov%2Fdocuments%2F621%2FMOD13_User_Guide_V61.pdf%0A%20%20%20%20-%20The%20MODIS%20land%20grid%3A%20https%3A%2F%2Fmodis-land.gsfc.nasa.gov%2FMODLAND_grid.html%0A%20%20%20%20-%20The%20companion%20reading%20in%20this%20folder%3A%20%5Bcode.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.1-zarr-and-the-modis-ndvi-lake%2Fcode.md)%20and%20%5Bscience.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.1-zarr-and-the-modis-ndvi-lake%2Fscience.md)%0A%20%20%20%20%22%22%22)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
9a38e7fdc4a6487affe4de4afa305f31