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%0A%20%20%20%20%23%20A%20conda%20install%20can%20point%20these%20at%20an%20older%20PROJ%20database%20that%20breaks%20rasterio%3B%20let%20rasterio%20use%20its%20bundled%20copy.%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)%0A%0A%20%20%20%20import%20inspect%0A%20%20%20%20import%20time%0A%0A%20%20%20%20import%20dask%0A%20%20%20%20import%20dask.array%20as%20da%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20pystac_client%0A%20%20%20%20import%20stackstac%0A%20%20%20%20import%20xarray%20as%20xr%0A%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22stackstac%22%2C%20stackstac.__version__%2C%20%22%7C%20dask%22%2C%20dask.__version__%2C%20%22%7C%20xarray%22%2C%20xr.__version__%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Imported%20here%20because%20later%20cells%20use%20them%20too.%0A%20%20%20%20import%20json%0A%0A%20%20%20%20import%20geopandas%20as%20gpd%0A%20%20%20%20import%20pyproj%0A%20%20%20%20import%20rasterio%0A%20%20%20%20from%20rasterio.sample%20import%20sample_gen%0A%20%20%20%20from%20shapely.geometry%20import%20box%0A%20%20%20%20from%20tf_data_ml_utils.common.extract%20import%20build_gdal_env%2C%20cog_read_env%0A%20%20%20%20from%20tf_data_ml_utils.sentinel1_rtc.ingest%20import%20extract%20as%20s1_extract%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.core.constants%20import%20resolve_offset_mode%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.core.harmonisation%20import%20apply_baseline_correction%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.streaming.field_aggregation%20import%20add_indices_to_cube%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.streaming.mds_schema%20import%20(%0A%20%20%20%20%20%20%20%20INDEX_NAMES%2C%0A%20%20%20%20%20%20%20%20N_CHANNELS%2C%0A%20%20%20%20%20%20%20%20N_INPUT_FEATURES%2C%0A%20%20%20%20)%0A%0A%20%20%20%20from%20labkit.show%20import%20show%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%20Streaming%20Sentinel-2%20fields%3A%20describe%20300%20GB%2C%20download%20megabytes%2C%20keep%20kilobytes%0A%0A%20%20%20%20**What%20you'll%20learn**%0A%0A%20%20%20%201.%20How%20%60pystac-client%60%2C%20%60stackstac%60%2C%20Dask%20and%20xarray%20work%20together%20to%20describe%20a%20huge%20stack%20of%20satellite%20images%20*without%20reading%20it*%2C%20then%20fetch%20only%20the%20pixels%20you%20ask%20for.%0A%20%20%20%202.%20How%20our%20production%20extractor%20(%60tf_data_ml_utils.sentinel2_l2a.ingest.extract%60)%20uses%20exactly%20that%20pattern%2C%20run%20here%20on%20real%20Sentinel-2%20data%20over%20an%20Iowa%20field.%0A%20%20%20%203.%20What%20the%20science%20does%20to%20those%20pixels%20(harmonisation%2C%20the%20cloud%20screen%2C%20field%20statistics)%2C%20and%20which%20other%20designs%20we%20could%20have%20chosen.%0A%20%20%20%204.%20The%20steps%20around%20the%20bake%3A%20which%20Sentinel-2%20tile%20a%20field%20belongs%20to%2C%20how%20field%20predictions%20become%20crop%20area%20for%20counties%20and%20states%2C%20and%20how%20phenology%20is%20read%20from%20a%20field's%20NDVI%20curve.%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)%2C%20then%20from%20%60academy%2F%60%2C%20open%20it%20with%20%60uv%20run%20marimo%20edit%201-data%2F1.2-streaming-sentinel2-fields%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.%20The%20notebook%20reads%20public%20Sentinel-2%20data%20from%20AWS%20(Earth%20Search)%2C%20so%20it%20needs%20internet%20access.%20The%20whole%20run%20takes%20under%20a%20minute%20from%20a%20UK%20laptop.%20If%20Earth%20Search%20can't%20be%20reached%2C%20the%20first%20search%20(1.3)%20stops%20with%20a%20message%20saying%20so%3B%20there%20is%20no%20offline%20copy%20of%20the%20data.%20Sections%201.5%2C%203.5%20to%203.7%2C%204.6%20and%204.7%20use%20synthetic%20data%20and%20need%20no%20network.%20Offline%2C%20open%20it%20with%20%60marimo%20edit%60%3A%20the%20cells%20that%20read%20Earth%20Search%20fail%2C%20marimo%20skips%20only%20the%20cells%20that%20use%20their%20results%2C%20and%20these%20sections%20still%20run.%20Run%20as%20a%20script%2C%20it%20stops%20at%20the%20first%20failure.%0A%0A%20%20%20%20New%20words%20(scene%2C%20tile%2C%20STAC%20item%20and%20asset%2C%20DN%2C%20L2A%2C%20field%20bake)%20are%20defined%20at%20the%20top%20of%20%5Bscience.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.2-streaming-sentinel2-fields%2Fscience.md).%0A%0A%20%20%20%20Everything%20imported%20from%20%60tf_data_ml_utils%60%20is%20the%20production%20code%20at%20commit%20%60c0ba9dc%60%2C%20pinned%20in%20%60pyproject.toml%60.%20The%20links%20below%20point%20at%20the%20same%20commit.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.function%0Adef%20tasks(arr)%3A%0A%20%20%20%20%22%22%22Return%20the%20number%20of%20tasks%20in%20a%20Dask-backed%20DataArray's%20graph.%22%22%22%0A%20%20%20%20return%20len(arr.data.__dask_graph__())%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%20Part%201.%20The%20libraries%2C%20the%20way%20their%20documentation%20introduces%20them%0A%0A%20%20%20%20Four%20libraries%20do%20the%20streaming%2C%20and%20a%20fifth%20assigns%20fields%20to%20tiles.%20Each%20one%20does%20one%20job%3A%0A%0A%20%20%20%20%7C%20Library%20%7C%20Job%20%7C%20Official%20docs%20%7C%0A%20%20%20%20%7C---%7C---%7C---%7C%0A%20%20%20%20%7C%20%60pystac-client%60%20%7C%20Search%20a%20STAC%20catalogue%3A%20*which%20images%20exist%3F*%20%7C%20https%3A%2F%2Fpystac-client.readthedocs.io%2Fen%2Fv0.9.1%2F%20%7C%0A%20%20%20%20%7C%20%60stackstac%60%20%7C%20Turn%20a%20list%20of%20STAC%20items%20into%20one%20lazy%20%60xarray.DataArray%60%20%7C%20https%3A%2F%2Fstackstac.readthedocs.io%2Fen%2Fv0.5.1%2F%20%7C%0A%20%20%20%20%7C%20Dask%20%7C%20Represent%20a%20big%20array%20as%20many%20small%20chunks%2C%20each%20with%20a%20recipe%2C%20and%20run%20only%20the%20recipes%20you%20need%20%7C%20https%3A%2F%2Fdocs.dask.org%2Fen%2Fstable%2Fgraphs.html%20%7C%0A%20%20%20%20%7C%20xarray%20%7C%20Labelled%20arrays%2C%20and%20the%20indexing%20that%20picks%20out%20our%20pixels%20%7C%20https%3A%2F%2Fdocs.xarray.dev%2Fen%2Fv2025.11.0%2Fuser-guide%2Findexing.html%20%7C%0A%20%20%20%20%7C%20geopandas%20%7C%20Tables%20of%20shapes%2C%20and%20the%20spatial%20join%20that%20finds%20which%20polygon%20holds%20each%20point%20(1.5%2C%20used%20in%203.5)%20%7C%20https%3A%2F%2Fgeopandas.org%2Fen%2Fv1.2.0%2Fdocs%2Fuser_guide%2Fmergingdata.html%20%7C%0A%0A%20%20%20%20%23%23%23%201.1%20Dask%20arrays%3A%20chunks%20and%20a%20task%20graph%0A%0A%20%20%20%20This%20is%20the%20example%20from%20the%20Dask%20documentation%2C%20almost%20word%20for%20word.%20Creating%20a%2010%2C000%20%C3%97%2010%2C000%20array%20of%20ones%20allocates%20nothing.%20Dask%20records%20100%20chunks%20and%20a%20recipe%20for%20each%20one.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_1_dask_arrays_chunks()%3A%0A%20%20%20%20x%20%3D%20da.ones((10_000%2C%2010_000)%2C%20chunks%3D(1_000%2C%201_000))%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22shape%22%2C%0A%20%20%20%20%20%20%20%20x.shape%2C%0A%20%20%20%20%20%20%20%20%22%7C%20chunks%20per%20axis%22%2C%0A%20%20%20%20%20%20%20%20x.numblocks%2C%0A%20%20%20%20%20%20%20%20%22%7C%20bytes%20if%20loaded%22%2C%0A%20%20%20%20%20%20%20%20f%22%7Bx.nbytes%20%2F%201e6%3A.0f%7D%20MB%22%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22tasks%20to%20build%20it%3A%22%2C%20len(x.__dask_graph__()))%0A%0A%20%20%20%20_total%20%3D%20x.sum()%0A%20%20%20%20print(%22tasks%20to%20build%20it%20AND%20sum%20it%3A%22%2C%20len(_total.__dask_graph__()))%0A%20%20%20%20print(%22result%3A%22%2C%20_total.compute())%0A%20%20%20%20return%20(x%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%60x.sum()%60%20didn't%20add%20anything%20up.%20It%20added%20more%20tasks%20to%20the%20graph%3A%20one%20partial%20sum%20per%20chunk%2C%20then%20a%20tree%20that%20combines%20them.%20Only%20%60.compute()%60%20runs%20the%20graph.%0A%0A%20%20%20%20The%20same%20idea%2C%20*describe%20first%2C%20compute%20last*%2C%20is%20what%20lets%20us%20treat%20a%20season%20of%20Sentinel-2%20tiles%20as%20one%20array.%0A%0A%20%20%20%20Now%20take%20a%20single%20element.%20Dask%20works%20out%20that%20only%20**one**%20chunk%20is%20needed%20and%20**culls**%20the%20rest%20of%20the%20graph%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_1_dask_arrays_chunks_2(x)%3A%0A%20%20%20%20one%20%3D%20x%5B1234%2C%205678%5D%0A%20%20%20%20print(%22tasks%20needed%20for%20one%20element%3A%22%2C%20len(dask.optimize(one)%5B0%5D.__dask_graph__()))%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%23%201.2%20xarray%3A%20outer%20indexing%20versus%20pointwise%20(vectorised)%20indexing%0A%0A%20%20%20%20This%20is%20the%20most%20important%20single%20idea%20in%20the%20notebook.%20Here%20is%20the%20example%20from%20the%20xarray%20indexing%20guide%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_2_xarray_outer_indexing()%3A%0A%20%20%20%20grid%20%3D%20xr.DataArray(np.arange(16).reshape(4%2C%204)%2C%20dims%3D(%22y%22%2C%20%22x%22))%0A%0A%20%20%20%20outer%20%3D%20grid.isel(y%3D%5B0%2C%203%5D%2C%20x%3D%5B1%2C%202%5D)%0A%20%20%20%20print(%22outer%20%20%20%3A%22%2C%20dict(outer.sizes)%2C%20outer.values.tolist())%0A%0A%20%20%20%20pointwise%20%3D%20grid.isel(%0A%20%20%20%20%20%20%20%20y%3Dxr.DataArray(%5B0%2C%203%5D%2C%20dims%3D%22sample%22)%2C%20x%3Dxr.DataArray(%5B1%2C%202%5D%2C%20dims%3D%22sample%22)%0A%20%20%20%20)%0A%20%20%20%20print(%22pointwise%3A%22%2C%20dict(pointwise.sizes)%2C%20pointwise.values.tolist())%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%20Plain%20lists%20give%20**every%20combination**%20of%20rows%20and%20columns%3A%202%20%C3%97%202%20%3D%204%20values.%0A%0A%20%20%20%20DataArrays%20that%20share%20a%20new%20dimension%20(%60sample%60)%20are%20paired%20element%20by%20element%3A%20(0%2C%201)%20and%20(3%2C%202)%2C%20so%202%20values.%0A%0A%20%20%20%20With%20900%20field%20pixels%2C%20outer%20indexing%20would%20ask%20for%20900%20%C3%97%20900%20%3D%20810%2C000%20pixels%3B%20pointwise%20asks%20for%20900.%20Part%204%20shows%20this%20blow-up%20on%20real%20data.%0A%0A%20%20%20%20%23%23%23%201.3%20pystac-client%3A%20which%20images%20exist%3F%0A%0A%20%20%20%20Earth%20Search%20is%20a%20public%20STAC%20catalogue%20of%20Sentinel-2%20on%20AWS.%20This%20search%20follows%20the%20pystac-client%20documentation%3A%20one%20MGRS%20tile%20(a%20fixed%20109.8%20km%20square%20of%20the%20Sentinel-2%20grid)%2C%20a%20date%20range%20and%20a%20cloud%20filter.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_3_pystac_client_images()%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20catalog%20%3D%20pystac_client.Client.open(%22https%3A%2F%2Fearth-search.aws.element84.com%2Fv1%22)%0A%20%20%20%20%20%20%20%20search%20%3D%20catalog.search(%0A%20%20%20%20%20%20%20%20%20%20%20%20collections%3D%5B%22sentinel-2-c1-l2a%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20datetime%3D%222024-08-31%2F2024-08-31%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20query%3D%7B%22grid%3Acode%22%3A%20%7B%22eq%22%3A%20%22MGRS-15TVG%22%7D%7D%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20items%20%3D%20list(search.items())%0A%20%20%20%20except%20Exception%20as%20exc%3A%0A%20%20%20%20%20%20%20%20raise%20RuntimeError(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Could%20not%20reach%20Earth%20Search%20(https%3A%2F%2Fearth-search.aws.element84.com%2Fv1).%20Only%20the%20cells%20that%20use%20these%20scenes%20stop%3B%20in%20%60marimo%20edit%60%20the%20rest%20still%20runs%2C%20including%20the%20synthetic%20sections%201.5%2C%203.5%20to%203.7%2C%204.6%20and%204.7.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Check%20your%20internet%20connection%20(and%20VPN)%2C%20then%20re-run%20from%20this%20cell.%22%0A%20%20%20%20%20%20%20%20)%20from%20exc%0A%20%20%20%20if%20not%20items%3A%0A%20%20%20%20%20%20%20%20raise%20RuntimeError(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Earth%20Search%20found%20no%20sentinel-2-c1-l2a%20scene%20of%20tile%2015TVG%20on%2031%20August%202024.%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20item%20%3D%20items%5B0%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20len(items)%2C%0A%20%20%20%20%20%20%20%20%22item%3A%22%2C%0A%20%20%20%20%20%20%20%20item.id%2C%0A%20%20%20%20%20%20%20%20%22%7C%20CRS%3A%22%2C%0A%20%20%20%20%20%20%20%20item.properties%5B%22proj%3Acode%22%5D%2C%0A%20%20%20%20%20%20%20%20%22%7C%20cloud%20%25%3A%22%2C%0A%20%20%20%20%20%20%20%20item.properties%5B%22eo%3Acloud_cover%22%5D%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22assets%3A%22%2C%20sorted(item.assets)%5B%3A12%5D%2C%20%22...%22)%0A%20%20%20%20print(%22red%20band%20COG%3A%22%2C%20item.assets%5B%22red%22%5D.href)%0A%20%20%20%20return%20catalog%2C%20items%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%20asset%20is%20a%20**Cloud-Optimised%20GeoTIFF%20(COG)**%3A%20a%20GeoTIFF%20with%20its%20header%20at%20the%20front%20and%20its%20pixels%20in%20internal%20tiles.%20That%20layout%20lets%20a%20client%20read%20one%20window%20with%20a%20few%20HTTP%20range%20requests%2C%20without%20downloading%20the%20file.%0A%0A%20%20%20%20%23%23%23%201.4%20stackstac%3A%20a%20lazy%20stack%20of%20images%0A%0A%20%20%20%20This%20is%20%60stackstac.stack%60%20as%20its%20documentation%20introduces%20it.%20Pass%20items%20and%20assets%3B%20get%20back%20an%20%60xarray.DataArray%60%20backed%20by%20Dask.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_4_stackstac_lazy_stack(items)%3A%0A%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20tile%20%3D%20stackstac.stack(items%2C%20assets%3D%5B%22red%22%2C%20%22nir%22%2C%20%22scl%22%5D%2C%20epsg%3D32615%2C%20resolution%3D10)%0A%20%20%20%20print(f%22built%20in%20%7Btime.perf_counter()%20-%20_t0%3A.2f%7Ds%2C%20no%20pixels%20read%22)%0A%20%20%20%20print(%22dims%3A%22%2C%20dict(tile.sizes)%2C%20%22%7C%20dtype%3A%22%2C%20tile.dtype)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22bytes%20if%20loaded%3A%20%7Btile.nbytes%20%2F%201e9%3A.1f%7D%20GB%20%7C%20chunks%3A%20%7Btile.data.numblocks%7D%20%7C%20tasks%3A%20%7Btasks(tile)%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(%22graph%20layers%3A%22%2C%20%5Bname.rsplit(%22-%22%2C%201)%5B0%5D%20for%20name%20in%20tile.data.__dask_graph__().layers%5D)%0A%20%20%20%20tile%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%20graph%20has%20three%20layers%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20asset-table%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20one%20row%20per%20(scene%2C%20asset)%3A%20URL%2C%20bounds%2C%20CRS%0A%20%20%20%20%20%20%E2%94%94%E2%94%80%20asset_table_to_reader_and_window%20%20%20pairs%20each%20chunk%20with%20a%20rasterio%20reader%20and%20a%20pixel%20window%0A%20%20%20%20%20%20%20%20%20%20%20%E2%94%94%E2%94%80%20fetch_raster_window%20%20%20%20%20%20%20%20%20%20%20%20each%20task%20opens%20a%20COG%20and%20reads%20one%20window%20via%20HTTP%20range%20requests%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Note%20the%20**float64**%20dtype%3A%20%60stackstac%60%20defaults%20to%208%20bytes%20per%20value%2C%20even%20though%20the%20data%20are%2016-bit%20integers.%20We%20come%20back%20to%20this%20in%20Part%204.%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%23%201.5%20geopandas%3A%20which%20polygon%20holds%20each%20point%0A%0A%20%20%20%20Section%203.5%20asks%20which%20Sentinel-2%20tile%20holds%20each%20field.%20The%20tool%20is%20a%20**spatial%20join**%2C%20as%20the%20%5Bgeopandas%20user%20guide%5D(https%3A%2F%2Fgeopandas.org%2Fen%2Fv1.2.0%2Fdocs%2Fuser_guide%2Fmergingdata.html)%20introduces%20it%3A%20two%20tables%20are%20joined%20by%20where%20their%20shapes%20are%2C%20not%20by%20a%20shared%20key.%20With%20%60how%3D%22left%22%60%2C%20every%20point%20keeps%20a%20row%2C%20and%20gets%20one%20row%20per%20polygon%20that%20holds%20it.%20Production%20uses%20the%20predicate%20%60covered_by%60%20(%5B%60geopandas.sjoin%60%5D(https%3A%2F%2Fgeopandas.org%2Fen%2Fv1.2.0%2Fdocs%2Freference%2Fapi%2Fgeopandas.sjoin.html))%2C%20which%20also%20counts%20a%20point%20lying%20exactly%20on%20a%20polygon's%20edge.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_5_geopandas_polygon_holds()%3A%0A%20%20%20%20from%20shapely.geometry%20import%20Point%0A%0A%20%20%20%20squares%20%3D%20gpd.GeoDataFrame(%0A%20%20%20%20%20%20%20%20%7B%22square%22%3A%20%5B%22west%22%2C%20%22east%22%5D%7D%2C%20geometry%3D%5Bbox(0%2C%200%2C%202%2C%202)%2C%20box(1.5%2C%200%2C%203.5%2C%202)%5D%0A%20%20%20%20)%0A%20%20%20%20dots%20%3D%20gpd.GeoDataFrame(%0A%20%20%20%20%20%20%20%20%7B%22dot%22%3A%20%5B%22a%22%2C%20%22b%22%2C%20%22c%22%5D%7D%2C%20geometry%3D%5BPoint(0.5%2C%201)%2C%20Point(1.75%2C%201)%2C%20Point(5%2C%201)%5D%0A%20%20%20%20)%0A%20%20%20%20gpd.sjoin(dots%2C%20squares%2C%20how%3D%22left%22%2C%20predicate%3D%22covered_by%22)%5B%5B%22dot%22%2C%20%22square%22%5D%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%20Dot%20%60b%60%20lies%20where%20the%20two%20squares%20overlap%2C%20so%20it%20gets%20two%20rows.%20Dot%20%60c%60%20lies%20in%20neither%2C%20so%20its%20%60square%60%20is%20missing.%20Any%20code%20that%20assigns%20points%20to%20overlapping%20polygons%20must%20break%20the%20first%20kind%20of%20tie%20and%20decide%20what%20to%20do%20with%20the%20second%20kind%20of%20point.%20Section%203.5%20shows%20how%20production%20does%20both.%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%20Part%202.%20How%20our%20production%20code%20does%20it%0A%0A%20%20%20%20Production%20wraps%20the%20same%20three%20moves%20(stack%20lazily%2C%20index%20pointwise%2C%20compute)%20in%20%60extract_tile_samples%60.%20Read%20the%20real%20code%20first.%0A%0A%20%20%20%20%23%23%23%202.1%20The%20bounding%20box%20and%20the%20pixel%20lookup%0A%0A%20%20%20%20%5B%60common%2Fsamples.py%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fcommon%2Fsamples.py%23L14-L60)%3A%20%60compute_bounds%60%20pads%20the%20box%20around%20the%20sample%20points%3B%20%60xy_to_indices%60%20turns%20UTM%20metres%20into%20array%20row%20and%20column%20numbers.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_1_bounding_box_pixel()%3A%0A%20%20%20%20from%20tf_data_ml_utils.common.samples%20import%20compute_bounds%2C%20xy_to_indices%0A%0A%20%20%20%20show(compute_bounds)%0A%20%20%20%20show(xy_to_indices)%0A%20%20%20%20return%20compute_bounds%2C%20xy_to_indices%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%23%202.2%20Cheap%20HTTP%20reads%20and%20a%20memory%20brake%0A%0A%20%20%20%20%5B%60common%2Fextract.py%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fcommon%2Fextract.py%23L24-L109)%3A%20%60cog_read_env%60%20sets%20GDAL%20options%20for%20reading%20COGs%20over%20HTTP.%20Some%20change%20behaviour%20(5%20retries%2C%20a%2032%20kB%20first%20read%20that%20holds%20the%20header%2C%20a%20200%20MB%20HTTP%20cache).%20Others%20repeat%20GDAL's%20or%20stackstac's%20defaults%2C%20and%20one%2C%20%60VSI_CACHE%3DTRUE%60%2C%20is%20set%20to%20%60False%60%20in%20stackstac's%20read%20layer%2C%20as%20the%20last%20line%20of%20the%20next%20cell%20shows%20(whether%20that%20actually%20stops%20caching%20for%20a%20file%20GDAL%20has%20already%20opened%20is%20a%20separate%20question%3B%20%5Bcode.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.2-streaming-sentinel2-fields%2Fcode.md)%20Step%204).%20%5Bcode.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.2-streaming-sentinel2-fields%2Fcode.md)%2C%20Step%204%2C%20sorts%20them%20all.%20%60memory_brake%60%20pauses%20a%20batch%20while%20the%20machine%20is%20short%20of%20RAM.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_2_cheap_http_reads()%3A%0A%20%20%20%20from%20tf_data_ml_utils.common.extract%20import%20mem_available_gib%2C%20memory_brake%0A%0A%20%20%20%20show(cog_read_env)%0A%20%20%20%20show(build_gdal_env)%0A%20%20%20%20show(memory_brake)%0A%20%20%20%20print(%22MemAvailable%20on%20this%20machine%20(GiB)%3A%22%2C%20mem_available_gib())%0A%20%20%20%20print(%22VSI_CACHE%20while%20stackstac%20reads%20pixels%3A%22%2C%20build_gdal_env(%22S2%22).read.options%5B%22VSI_CACHE%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%60mem_available_gib%60%20reads%20%60%2Fproc%2Fmeminfo%60%2C%20which%20exists%20only%20on%20Linux.%20On%20a%20Mac%20it%20returns%20infinity%2C%20so%20**the%20brake%20never%20engages%20locally**.%20It%20protects%20the%20Linux%20machines%20production%20runs%20on.%0A%0A%20%20%20%20%23%23%23%202.3%20The%20extractor%20itself%0A%0A%20%20%20%20%5B%60sentinel2_l2a%2Fingest%2Fextract.py%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fingest%2Fextract.py%23L56-L163)%3A%20%60_extract_batch%60%20makes%20one%20stack%2C%20picks%20the%20sample%20pixels%20pointwise%2C%20computes%2C%20then%20harmonises%20and%20masks.%20%60extract_tile_samples%60%20(%5BL183-269%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fingest%2Fextract.py%23L183-L269))%20loops%20over%20batches%20of%20up%20to%2030%20scenes.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_3_extractor()%3A%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.ingest%20import%20extract%0A%0A%20%20%20%20show(extract._extract_batch)%0A%20%20%20%20return%20(extract%2C)%0A%0A%0A%40app.cell%0Adef%20s2_3_extractor_2(extract)%3A%0A%20%20%20%20show(extract.extract_tile_samples)%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%23%202.4%20Which%20data%20source%2C%20and%20which%20radiometric%20offset%3F%0A%0A%20%20%20%20Production%20searches%20with%20%60S2StacClient%60.%20The%20library's%20default%20source%20is%20Planetary%20Computer%20(%60DEFAULT_S2_SOURCE%20%3D%20%22mpc%22%60)%2C%20and%20each%20data-pipelines%20stage%20chooses%20its%20own%20source.%20This%20notebook%20uses%20%60source%3D%22aws%22%60%20(Earth%20Search)%2C%20which%20needs%20no%20URL%20signing%3B%20with%20it%20the%20client%20defaults%20to%20Earth%20Search's%20**Collection%201**%20(%60sentinel-2-c1-l2a%60).%20%60resolve_offset_mode%60%20then%20tells%20the%20harmonisation%20step%20how%20to%20turn%20integers%20into%20reflectance%20(%5Bcore%2Fconstants.py%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fcore%2Fconstants.py%23L47-L73)).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_4_data_source_radiometric(mo)%3A%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.ingest.mpc_client%20import%20S2StacClient%0A%0A%20%20%20%20show(resolve_offset_mode)%0A%20%20%20%20client%20%3D%20S2StacClient(source%3D%22aws%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22collection%3A%22%2C%0A%20%20%20%20%20%20%20%20client.collection%2C%0A%20%20%20%20%20%20%20%20%22%7C%20offset%20mode%3A%22%2C%0A%20%20%20%20%20%20%20%20resolve_offset_mode(client.source%2C%20client.collection)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20season%20%3D%20client.search_tile(%2215TVG%22%2C%20%222024-05-15%2F2024-09-15%22%2C%20max_cloud%3D60)%0A%20%20%20%20print(f%22%7Blen(season)%7D%20scenes%20found%20in%20%7Btime.perf_counter()%20-%20_t0%3A.1f%7Ds%22)%0A%20%20%20%20mo.output.append(%0A%20%20%20%20%20%20%20%20pd.DataFrame(%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%22date%22%3A%20%5Bi.datetime.date()%20for%20i%20in%20season%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22cloud%20%25%22%3A%20%5Bround(i.properties%5B%22eo%3Acloud_cover%22%5D)%20for%20i%20in%20season%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20).T%0A%20%20%20%20)%0A%20%20%20%20return%20client%2C%20season%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%23%202.5%20A%20field%20to%20sample%0A%0A%20%20%20%20Production%20samples%20the%20interior%20pixels%20of%20real%20fields.%20Here%20we%20use%20a%20300%20m%20%C3%97%20300%20m%20square%20of%20cropland%20in%20tile%2015TVG%20(UTM%20zone%2015N%2C%20northern%20Iowa)%3A%2030%20%C3%97%2030%20%3D%20900%20pixel%20centres%20at%2010%20m%20spacing%2C%20the%20same%20way%20production%20builds%20its%20points.%20(Section%203.3%20shows%20that%20this%20square%20straddles%20two%20fields.)%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_5_field_sample(compute_bounds)%3A%0A%20%20%20%20FIELD_X0%2C%20FIELD_Y0%20%3D%20428_400.0%2C%204_670_730.0%0A%20%20%20%20xs%2C%20ys%20%3D%20np.meshgrid(FIELD_X0%20%2B%205%20%2B%20np.arange(0%2C%20300%2C%2010)%2C%20FIELD_Y0%20-%205%20-%20np.arange(0%2C%20300%2C%2010))%0A%20%20%20%20field_points%20%3D%20np.column_stack(%5Bxs.ravel()%2C%20ys.ravel()%5D)%0A%20%20%20%20bounds%20%3D%20compute_bounds(field_points%2C%20pad_m%3D100.0)%0A%20%20%20%20print(%22points%3A%22%2C%20field_points.shape%2C%20%22%7C%20padded%20bounds%20(m)%3A%22%2C%20bounds)%0A%20%20%20%20return%20FIELD_X0%2C%20FIELD_Y0%2C%20bounds%2C%20field_points%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%23%202.6%20Step%20by%20step%2C%20measuring%20each%20stage%0A%0A%20%20%20%20We%20repeat%20what%20%60_extract_batch%60%20does%20one%20line%20at%20a%20time%2C%20with%20three%20scenes%20and%20three%20assets%2C%20so%20every%20stage%20can%20be%20measured.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_6_step_step_measuring(bounds%2C%20field_points%2C%20season%2C%20xy_to_indices)%3A%0A%20%20%20%20three%20%3D%20season%5B%3A%3A5%5D%5B%3A3%5D%0A%20%20%20%20assets%20%3D%20%5B%22red%22%2C%20%22nir%22%2C%20%22scl%22%5D%0A%20%20%20%20kw%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22assets%22%3A%20assets%2C%0A%20%20%20%20%20%20%20%20%22epsg%22%3A%2032615%2C%0A%20%20%20%20%20%20%20%20%22resolution%22%3A%2010%2C%0A%20%20%20%20%20%20%20%20%22chunksize%22%3A%20(1%2C%20len(assets)%2C%201024%2C%201024)%2C%0A%20%20%20%20%20%20%20%20%22rescale%22%3A%20False%2C%0A%20%20%20%20%20%20%20%20%22gdal_env%22%3A%20build_gdal_env(%22S2%22)%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20whole%20%3D%20stackstac.stack(three%2C%20**kw)%0A%20%20%20%20bounded%20%3D%20stackstac.stack(three%2C%20bounds%3Dbounds%2C%20**kw)%0A%20%20%20%20rows%2C%20cols%2C%20in_bounds%20%3D%20xy_to_indices(field_points%2C%20bounded.x.values%2C%20bounded.y.values%2C%2010)%0A%20%20%20%20picked%20%3D%20bounded.isel(y%3Dxr.DataArray(rows%2C%20dims%3D%22sample%22)%2C%20x%3Dxr.DataArray(cols%2C%20dims%3D%22sample%22))%0A%0A%20%20%20%20stages%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22whole%20tile%22%2C%20dict(whole.sizes)%2C%20whole.nbytes%2C%20tasks(whole))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22bounds%3D%20around%20the%20field%22%2C%20dict(bounded.sizes)%2C%20bounded.nbytes%2C%20tasks(bounded))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22after%20pointwise%20isel%22%2C%20dict(picked.sizes)%2C%20picked.nbytes%2C%20tasks(picked))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20columns%3D%5B%22stage%22%2C%20%22shape%22%2C%20%22bytes%20if%20loaded%22%2C%20%22dask%20tasks%22%5D%2C%0A%20%20%20%20)%0A%20%20%20%20stages%5B%22size%22%5D%20%3D%20stages%5B%22bytes%20if%20loaded%22%5D.map(%0A%20%20%20%20%20%20%20%20lambda%20b%3A%20f%22%7Bb%20%2F%201e9%3A.2f%7D%20GB%22%20if%20b%20%3E%201e9%20else%20f%22%7Bb%20%2F%201e3%3A.1f%7D%20kB%22%0A%20%20%20%20)%0A%20%20%20%20stages%0A%20%20%20%20return%20assets%2C%20bounded%2C%20cols%2C%20kw%2C%20picked%2C%20rows%2C%20three%2C%20whole%0A%0A%0A%40app.cell%0Adef%20s2_6_step_step_measuring_2(picked)%3A%0A%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20pixels%20%3D%20picked.compute()%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22compute%3A%20%7Btime.perf_counter()%20-%20_t0%3A.1f%7Ds%20on%20this%20machine%20%7C%20result%20%7Bdict(pixels.sizes)%7D%2C%20%7Bpixels.nbytes%20%2F%201e3%3A.1f%7D%20kB%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%20The%20description%20of%20the%20data%20shrinks%20by%20four%20to%20five%20orders%20of%20magnitude%20before%20a%20single%20byte%20is%20fetched.%20The%20compute%20time%20is%20dominated%20by%20HTTP%20latency%3A%20from%20a%20UK%20laptop%20to%20%60us-west-2%60%20it's%20a%20few%20seconds%2C%20and%20in-region%20it's%20far%20less.%20Treat%20the%20number%20as%20a%20laptop%20measurement%2C%20not%20a%20benchmark.%0A%0A%20%20%20%20%23%23%23%202.7%20The%20production%20call%20for%20a%20whole%20season%0A%0A%20%20%20%20Now%20the%20real%20thing%3A%20one%20call%20to%20%60extract_tile_samples%60%20with%20the%20default%2010%20bands%20plus%20SCL%2C%20every%20third%20scene%20of%20the%20season%2C%20and%20all%20900%20field%20pixels.%20This%20is%20the%20same%20call%20the%20field%20bake%20makes%20for%20each%20%60(state%2C%20mgrs_tile%2C%20year)%60%20(%5Bbake_fold.py%3A416-425%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fstreaming%2Fbake_fold.py%23L416-L425)).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_7_production_call_whole(%0A%20%20%20%20FIELD_X0%2C%0A%20%20%20%20FIELD_Y0%2C%0A%20%20%20%20bounds%2C%0A%20%20%20%20client%2C%0A%20%20%20%20extract%2C%0A%20%20%20%20field_points%2C%0A%20%20%20%20season%2C%0A)%3A%0A%20%20%20%20scenes%20%3D%20season%5B%3A%3A3%5D%0A%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20ds%20%3D%20extract.extract_tile_samples(%0A%20%20%20%20%20%20%20%20scenes%2C%20field_points%2C%20utm_epsg%3D32615%2C%20source%3Dclient.source%2C%20collection%3Dclient.collection%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22%7Blen(scenes)%7D%20scenes%20x%2011%20assets%20x%20900%20pixels%20in%20%7Btime.perf_counter()%20-%20_t0%3A.1f%7Ds%20(laptop%2C%20UK%20-%3E%20us-west-2)%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20dict(ds.sizes)%2C%0A%20%20%20%20%20%20%20%20%22%7C%20failed%20batches%3A%22%2C%0A%20%20%20%20%20%20%20%20ds.attrs%5B%22n_failed_batches%22%5D%2C%0A%20%20%20%20%20%20%20%20%22of%22%2C%0A%20%20%20%20%20%20%20%20ds.attrs%5B%22n_total_batches%22%5D%2C%0A%20%20%20%20)%0A%20%20%20%20%23%20stackstac%20drops%20any%20scene%20whose%20footprint%20doesn't%20overlap%20%60bounds%60.%20Which%20one%20went%3F%0A%0A%20%20%20%20to_lonlat%20%3D%20pyproj.Transformer.from_crs(32615%2C%204326%2C%20always_xy%3DTrue)%0A%20%20%20%20kept_ids%20%3D%20set(%0A%20%20%20%20%20%20%20%20stackstac.stack(%0A%20%20%20%20%20%20%20%20%20%20%20%20scenes%2C%20assets%3D%5B%22red%22%5D%2C%20epsg%3D32615%2C%20resolution%3D10%2C%20bounds%3Dbounds%0A%20%20%20%20%20%20%20%20).id.values.tolist()%0A%20%20%20%20)%0A%20%20%20%20print(f%22field%20longitude%3A%20%7Bto_lonlat.transform(FIELD_X0%2C%20FIELD_Y0)%5B0%5D%3A.3f%7D%22)%0A%20%20%20%20for%20_it%20in%20scenes%3A%0A%20%20%20%20%20%20%20%20if%20_it.id%20not%20in%20kept_ids%3A%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%22dropped%20by%20stackstac%3A%20%7B_it.id%7D%20%7C%20footprint%20longitude%20%7B_it.bbox%5B0%5D%3A.3f%7D%20to%20%7B_it.bbox%5B2%5D%3A.3f%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20ds%0A%20%20%20%20return%20ds%2C%20to_lonlat%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%20There%20are%20fewer%20time%20steps%20than%20scenes%2C%20and%20the%20end%20of%20the%20output%20shows%20why.%20%60stackstac%60%20drops%20any%20scene%20whose%20footprint%20doesn't%20overlap%20%60bounds%3D%60%20(its%20documentation%3A%20%22When%20%60bounds%60%20is%20given%2C%20any%20assets%20that%20don't%20overlap%20those%20bounds%20are%20dropped%22).%20Here%20that%20is%20a%20sliver%20at%20the%20edge%20of%20the%20satellite's%20swath%2C%20whose%20footprint%20stops%20east%20of%20the%20field.%0A%0A%20%20%20%20This%20has%20a%20side%20effect%20in%20production.%20With%20fewer%20time%20steps%20than%20items%2C%20%60_extract_batch%60%20can't%20pair%20platforms%20to%20scenes%20one%20to%20one%2C%20so%20it%20falls%20back%20to%20%60_platforms_for_dates%60%20(%5Bextract.py%3A166-180%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fingest%2Fextract.py%23L166-L180)).%20That%20function%20looks%20each%20time%20step%20up%20by%20a%20timestamp%20string%20with%20a%20timezone%20(%60%E2%80%A6%2B00%3A00%60)%2C%20but%20the%20stack's%20times%20have%20none%2C%20so%20nothing%20matches%3A%20%60platform%60%20is%20blank%20for%20**every**%20scene%20in%20the%20batch%2C%20as%20the%20output%20shows.%20%60dedup_time_platform%60%20then%20deduplicates%20on%20time%20alone.%20The%20field%20bake%20doesn't%20read%20%60platform%60%2C%20so%20the%20damage%20is%20limited%3B%20it%20is%20logged%20as%20a%20lead%20in%20the%20backlog.%0A%0A%20%20%20%20%23%23%20Part%203.%20The%20what%3A%20turning%20pixels%20into%20a%20field's%20time%20series%0A%0A%20%20%20%20%23%23%23%203.1%20Harmonisation%3A%20one%20pixel%2C%20two%20collections%0A%0A%20%20%20%20Sentinel-2%20Level-2A%20stores%20reflectance%20as%20integers.%20For%20products%20made%20with%20Processing%20Baseline%2004.00%20or%20later%2C%20%60reflectance%20%3D%20(DN%20-%201000)%20%2F%2010000%60%3B%20before%2C%20%60reflectance%20%3D%20DN%20%2F%2010000%60.%20Baseline%2004.00%20started%20on%2025%20January%202022%2C%20and%20ESA's%20Collection-1%20reprocessing%20applied%20it%20to%20older%20scenes%20too%2C%20so%20the%20offset%20follows%20the%20baseline%2C%20not%20the%20date.%20Distributors%20differ%20in%20whether%20they%20have%20already%20removed%20the%20offset%2C%20which%20is%20why%20%60resolve_offset_mode%60%20exists.%0A%0A%20%20%20%20Read%20the%20same%20pixel%20on%20the%20same%20day%20from%20Earth%20Search's%20two%20collections%2C%20with%20the%20metadata%20each%20item%20carries%20about%20the%20offset%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_1_harmonisation_pixel_two(%0A%20%20%20%20catalog%2C%0A%20%20%20%20compute_bounds%2C%0A%20%20%20%20field_points%2C%0A%20%20%20%20xy_to_indices%2C%0A)%3A%0A%20%20%20%20show(apply_baseline_correction)%0A%0A%20%20%20%20centre%20%3D%20field_points%5B%5B465%5D%5D%0A%20%20%20%20raw%20%3D%20%7B%7D%0A%20%20%20%20for%20_collection%20in%20(%22sentinel-2-c1-l2a%22%2C%20%22sentinel-2-l2a%22)%3A%0A%20%20%20%20%20%20%20%20_it%20%3D%20list(%0A%20%20%20%20%20%20%20%20%20%20%20%20catalog.search(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20collections%3D%5B_collection%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20datetime%3D%222024-08-31%2F2024-08-31%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20query%3D%7B%22grid%3Acode%22%3A%20%7B%22eq%22%3A%20%22MGRS-15TVG%22%7D%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20).items()%0A%20%20%20%20%20%20%20%20)%5B0%5D%0A%20%20%20%20%20%20%20%20red_band%20%3D%20_it.assets%5B%22red%22%5D.extra_fields%5B%22raster%3Abands%22%5D%5B0%5D%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B_collection%3A18s%7D%20s2%3Aprocessing_baseline%20%7B_it.properties%5B's2%3Aprocessing_baseline'%5D%7D%20%7C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22earthsearch%3Aboa_offset_applied%20%7B_it.properties.get('earthsearch%3Aboa_offset_applied')%7D%20%7C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22red%20raster%3Abands%20offset%20%7Bred_band.get('offset')%7D%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_s%20%3D%20stackstac.stack(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B_it%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20assets%3D%5B%22red%22%2C%20%22nir%22%2C%20%22scl%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20epsg%3D32615%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20resolution%3D10%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20bounds%3Dcompute_bounds(centre%2C%20pad_m%3D20.0)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20rescale%3DFalse%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_r%2C%20c%2C%20_%20%3D%20xy_to_indices(centre%2C%20_s.x.values%2C%20_s.y.values%2C%2010)%0A%20%20%20%20%20%20%20%20raw%5B_collection%5D%20%3D%20_s.isel(time%3D0%2C%20y%3Dint(_r%5B0%5D)%2C%20x%3Dint(c%5B0%5D)).values%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B''%3A18s%7D%20raw%20DN%20red%3D%7Braw%5B_collection%5D%5B0%5D%3A.0f%7D%20nir%3D%7Braw%5B_collection%5D%5B1%5D%3A.0f%7D%20%7C%20SCL%20class%20%7Braw%5B_collection%5D%5B2%5D%3A.0f%7D%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20(raw%2C)%0A%0A%0A%40app.cell%0Adef%20s3_1_harmonisation_pixel_two_2(raw)%3A%0A%20%20%20%20def%20ndvi_for(dn_red%2C%20dn_nir%2C%20mode)%3A%0A%20%20%20%20%20%20%20%20%22%22%22NDVI%20of%20one%20pixel%20after%20production%20harmonisation%20in%20the%20given%20offset%20mode.%22%22%22%0A%20%20%20%20%20%20%20%20dn%20%3D%20np.zeros((1%2C%201%2C%2010))%0A%20%20%20%20%20%20%20%20dn%5B0%2C%200%2C%202%5D%2C%20dn%5B0%2C%200%2C%206%5D%20%3D%20dn_red%2C%20dn_nir%0A%20%20%20%20%20%20%20%20refl%20%3D%20apply_baseline_correction(%0A%20%20%20%20%20%20%20%20%20%20%20%20dn%2C%20np.array(%5B%222024-08-31%22%5D%2C%20dtype%3D%22datetime64%5Bns%5D%22)%2C%20offset_mode%3Dmode%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20return%20refl%5B0%2C%200%2C%202%5D%2C%20refl%5B0%2C%200%2C%206%5D%2C%20add_indices_to_cube(refl)%5B0%2C%200%2C%2010%5D%0A%0A%20%20%20%20table%20%3D%20%5B%5D%0A%20%20%20%20for%20_collection%2C%20(red%2C%20nir%2C%20_scl)%20in%20raw.items()%3A%0A%20%20%20%20%20%20%20%20for%20mode%20in%20(%22never%22%2C%20%22always%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_r%2C%20_n%2C%20v%20%3D%20ndvi_for(red%2C%20nir%2C%20mode)%0A%20%20%20%20%20%20%20%20%20%20%20%20chosen%20%3D%20mode%20%3D%3D%20resolve_offset_mode(%22aws%22%2C%20_collection)%0A%20%20%20%20%20%20%20%20%20%20%20%20table.append(%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%20%20%20%20%20%20%20%20_collection%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%3C-%20production%22%20if%20chosen%20else%20%22%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20round(float(_r)%2C%204)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20round(float(_n)%2C%204)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20round(float(v)%2C%203)%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)%0A%20%20%20%20pd.DataFrame(table%2C%20columns%3D%5B%22collection%22%2C%20%22offset%20mode%22%2C%20%22%22%2C%20%22red%22%2C%20%22NIR%22%2C%20%22NDVI%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%20The%20two%20collections%20store%20**different%20integers%20for%20the%20same%20reflectance**%3A%20Collection%201%20carries%20the%20%2B1000%20offset.%20With%20the%20mode%20production%20picks%20for%20each%20collection%2C%20both%20give%20the%20same%20physical%20reflectance%20and%20NDVI.%0A%0A%20%20%20%20The%20wrong%20mode%20does%20two%20different%20kinds%20of%20damage%3A%0A%0A%20%20%20%20-%20On%20data%20whose%20offset%20has%20already%20been%20removed%2C%20subtracting%201000%20again%20makes%20red%20reflectance%20negative.%20NDVI%20can%20then%20go%20**above%201**%2C%20which%20is%20physically%20impossible%20and%20easy%20to%20catch.%20(The%20last%20row%20of%20the%20table%20shows%20it%3A%20NDVI%201.535.)%0A%20%20%20%20-%20On%20Collection%201%20data%2C%20*forgetting*%20to%20subtract%20it%20gives%20an%20NDVI%20that%20is%20**too%20low%20but%20still%20looks%20plausible**.%20Nothing%20flags%20this%20one%2C%20which%20makes%20it%20the%20more%20dangerous%20error.%0A%0A%20%20%20%20Now%20look%20at%20the%20metadata%20printed%20above.%20The%20%60sentinel-2-l2a%60%20item%20says%20%60earthsearch%3Aboa_offset_applied%60%20is%20%60True%60%2C%20yet%20its%20%60raster%3Abands%60%20still%20lists%20an%20offset%20of%20%E2%88%920.1%2C%20and%20its%20pixels%20side%20with%20the%20first.%20Trusting%20%60raster%3Abands%60%20here%2C%20as%20%60stackstac%60's%20default%20%60rescale%3DTrue%60%20would%2C%20subtracts%20the%20offset%20twice.%20Both%20items%20also%20report%20%60s2%3Aprocessing_baseline%60%20after%2004.00%2C%20yet%20only%20Collection%201%20needs%20the%20subtraction%3A%20the%20baseline%20says%20the%20offset%20*exists*%2C%20and%20the%20distributor's%20correction%20state%20says%20whether%20it%20has%20already%20been%20removed.%20You%20need%20both.%20%5Bscience.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.2-streaming-sentinel2-fields%2Fscience.md)%2C%20section%201%2C%20covers%20how%20to%20read%20this%20metadata.%0A%0A%20%20%20%20%23%23%23%203.2%20The%20cloud%20screen%20(SCL)%0A%0A%20%20%20%20Sen2Cor%20labels%20every%20pixel%20with%20a%20scene%20class.%20Production%20drops%20these%20codes%20(%5Bcore%2Fconstants.py%3A31%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fcore%2Fconstants.py%23L31)%2C%20applied%20by%20%5B%60compute_obs_valid%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fcore%2Fscl_mask.py%23L10-L17))%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_2_cloud_screen_scl(mo)%3A%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.core.constants%20import%20SCL_BAD_CODES%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.core.scl_mask%20import%20compute_obs_valid%0A%0A%20%20%20%20SCL_NAMES%20%3D%20%7B%0A%20%20%20%20%20%20%20%200%3A%20%22no%20data%22%2C%0A%20%20%20%20%20%20%20%201%3A%20%22saturated%20%2F%20defective%22%2C%0A%20%20%20%20%20%20%20%202%3A%20%22cast%20shadows%20(formerly%20dark%20area)%22%2C%0A%20%20%20%20%20%20%20%203%3A%20%22cloud%20shadow%22%2C%0A%20%20%20%20%20%20%20%204%3A%20%22vegetation%22%2C%0A%20%20%20%20%20%20%20%205%3A%20%22not%20vegetated%22%2C%0A%20%20%20%20%20%20%20%206%3A%20%22water%22%2C%0A%20%20%20%20%20%20%20%207%3A%20%22unclassified%22%2C%0A%20%20%20%20%20%20%20%208%3A%20%22cloud%2C%20medium%20probability%22%2C%0A%20%20%20%20%20%20%20%209%3A%20%22cloud%2C%20high%20probability%22%2C%0A%20%20%20%20%20%20%20%2010%3A%20%22thin%20cirrus%22%2C%0A%20%20%20%20%20%20%20%2011%3A%20%22snow%20%2F%20ice%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20show(compute_obs_valid)%0A%20%20%20%20mo.output.append(%0A%20%20%20%20%20%20%20%20pd.DataFrame(%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%22class%22%3A%20SCL_NAMES.values()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22kept%22%3A%20%5Bcode%20not%20in%20SCL_BAD_CODES%20for%20code%20in%20SCL_NAMES%5D%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%20%20%20%20index%3DSCL_NAMES.keys()%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20return%20(SCL_NAMES%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%20Class%205%20(bare%20soil)%20and%20class%206%20(water)%20are%20**kept%20on%20purpose**.%20A%20field%20before%20emergence%20is%20bare%20soil%2C%20and%20a%20flooded%20paddy%20is%20water.%20Dropping%20them%20would%20erase%20the%20start%20of%20every%20season.%0A%0A%20%20%20%20Class%202%20is%20kept%20too.%20ESA%20now%20calls%20it%20%22cast%20shadows%22%20(formerly%20dark%20area%20pixels)%3A%20shadows%20cast%20by%20terrain%2C%20a%20defensible%20keep%20on%20flat%20cropland%20but%20a%20choice%20nonetheless.%20SCL%20is%20also%20a%2020%20m%20layer%2C%20so%20on%20this%2010%20m%20grid%20the%20mask%20works%20in%202%20%C3%97%202%20blocks.%0A%0A%20%20%20%20Here%20is%20how%20the%20SCL%20classes%20are%20distributed%20in%20our%20field%2C%20scene%20by%20scene%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_2_cloud_screen_scl_2(SCL_NAMES%2C%20ds)%3A%0A%20%20%20%20scl_counts%20%3D%20(%0A%20%20%20%20%20%20%20%20pd.DataFrame(ds.scl.values.T%2C%20index%3Dpd.to_datetime(ds.time.values).date)%0A%20%20%20%20%20%20%20%20.apply(lambda%20row%3A%20row.value_counts()%2C%20axis%3D1)%0A%20%20%20%20%20%20%20%20.fillna(0)%0A%20%20%20%20%20%20%20%20.astype(int)%0A%20%20%20%20)%0A%20%20%20%20scl_counts.columns%20%3D%20%5Bf%22%7Bc%7D%20%7BSCL_NAMES.get(c%2C%20'%3F')%7D%22%20for%20c%20in%20scl_counts.columns%5D%0A%20%20%20%20scl_counts%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%23%203.3%20Field%20statistics%3A%2017%20channels%20%C3%97%205%20statistics%20%2B%204%20quality%20fractions%0A%0A%20%20%20%20%60compute_field_ts%60%20(%5Bfield_aggregation.py%3A78-119%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fstreaming%2Ffield_aggregation.py%23L78-L119))%20sets%20invalid%20pixels%20to%20NaN%2C%20adds%207%20spectral%20indices%20to%20the%2010%20bands%2C%20and%20summarises%20each%20scene%20with%20mean%2C%20standard%20deviation%20and%20the%2010th%2C%2050th%20and%2090th%20percentiles.%20It%20also%20records%20the%20valid%2C%20cloud%2C%20shadow%20and%20snow%20fractions.%0A%0A%20%20%20%20**A%20warning%20before%20you%20trust%20these%20numbers.**%20Production's%20%60xy_to_indices%60%20reads%20the%20wrong%20pixel%20for%20three%20in%20four%20of%20our%20900%20points%2C%20so%20these%20statistics%20come%20from%20a%20quarter%20of%20the%20square's%20pixels%2C%20each%20counted%20up%20to%20four%20times%20(finding%20F1%3B%20section%204.4b%20measures%20it).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_3_field_statistics_17(ds)%3A%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.streaming.field_aggregation%20import%20compute_field_ts%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.streaming.mds_schema%20import%20PER_SCENE_STATS%0A%0A%20%20%20%20show(compute_field_ts)%0A%20%20%20%20field_ts%20%3D%20compute_field_ts(ds.reflectance.values%2C%20ds.obs_valid.values%2C%20ds.scl.values)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22per-scene%20features%3A%22%2C%0A%20%20%20%20%20%20%20%20field_ts.shape%2C%0A%20%20%20%20%20%20%20%20%22%7C%20expected%22%2C%0A%20%20%20%20%20%20%20%20N_INPUT_FEATURES%2C%0A%20%20%20%20%20%20%20%20%22%3D%22%2C%0A%20%20%20%20%20%20%20%20N_CHANNELS%2C%0A%20%20%20%20%20%20%20%20%22channels%20x%22%2C%0A%20%20%20%20%20%20%20%20len(PER_SCENE_STATS)%2C%0A%20%20%20%20%20%20%20%20%22stats%20%2B%204%20fractions%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(field_ts%2C)%0A%0A%0A%40app.cell%0Adef%20s3_3_field_statistics_17_2(ds%2C%20field_ts)%3A%0A%20%20%20%20NDVI%20%3D%2010%20%2B%20list(INDEX_NAMES).index(%22NDVI%22)%0A%20%20%20%20dates%20%3D%20pd.to_datetime(ds.time.values)%0A%20%20%20%20summary%20%3D%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%22NDVI%20mean%22%3A%20field_ts%5B%3A%2C%200%20*%20N_CHANNELS%20%2B%20NDVI%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22NDVI%20p10%22%3A%20field_ts%5B%3A%2C%202%20*%20N_CHANNELS%20%2B%20NDVI%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22NDVI%20p50%22%3A%20field_ts%5B%3A%2C%203%20*%20N_CHANNELS%20%2B%20NDVI%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22NDVI%20p90%22%3A%20field_ts%5B%3A%2C%204%20*%20N_CHANNELS%20%2B%20NDVI%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22valid%22%3A%20field_ts%5B%3A%2C%20-4%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22cloud%22%3A%20field_ts%5B%3A%2C%20-3%5D%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20index%3Ddates.date%2C%0A%20%20%20%20).round(3)%0A%20%20%20%20summary%0A%20%20%20%20return%20dates%2C%20summary%0A%0A%0A%40app.cell%0Adef%20s3_3_field_statistics_17_3(dates%2C%20summary)%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(9%2C%204))%0A%20%20%20%20_ax.fill_between(%0A%20%20%20%20%20%20%20%20dates%2C%0A%20%20%20%20%20%20%20%20summary%5B%22NDVI%20p10%22%5D%2C%0A%20%20%20%20%20%20%20%20summary%5B%22NDVI%20p90%22%5D%2C%0A%20%20%20%20%20%20%20%20alpha%3D0.25%2C%0A%20%20%20%20%20%20%20%20label%3D%22p10%20to%20p90%20across%20the%20field%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.plot(dates%2C%20summary%5B%22NDVI%20mean%22%5D%2C%20marker%3D%22o%22%2C%20label%3D%22field%20mean%22)%0A%20%20%20%20flagged%20%3D%20summary%5B%22valid%22%5D%20%3C%201.0%0A%20%20%20%20_ax.scatter(%0A%20%20%20%20%20%20%20%20dates%5Bflagged%5D%2C%0A%20%20%20%20%20%20%20%20summary%5B%22NDVI%20mean%22%5D%5Bflagged%5D%2C%0A%20%20%20%20%20%20%20%20s%3D160%2C%0A%20%20%20%20%20%20%20%20facecolors%3D%22none%22%2C%0A%20%20%20%20%20%20%20%20edgecolors%3D%22red%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22scene%20with%20masked%20pixels%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.set_ylabel(%22NDVI%22)%0A%20%20%20%20_ax.set_ylim(0%2C%201)%0A%20%20%20%20_ax.set_title(%0A%20%20%20%20%20%20%20%20%22A%20300%20m%20square%20straddling%20two%20fields%20in%20tile%2015TVG%2C%20summer%202024%20(real%20Sentinel-2%20data)%22%0A%20%20%20%20)%0A%20%20%20%20_ax.legend(loc%3D%22lower%20right%22)%0A%20%20%20%20_fig.autofmt_xdate()%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell%0Adef%20s3_3_field_statistics_17_4(dates%2C%20ds%2C%20summary)%3A%0A%20%20%20%20%23%20The%20scene%20where%20the%20p10-p90%20band%20is%20widest%2C%20in%2050%20m%20strips%20from%20west%20to%20east.%0A%20%20%20%20widest%20%3D%20int(np.argmax(summary%5B%22NDVI%20p90%22%5D%20-%20summary%5B%22NDVI%20p10%22%5D))%0A%20%20%20%20red_px%2C%20nir_px%20%3D%20ds.reflectance.values%5B%3A%2C%20widest%2C%202%5D%2C%20ds.reflectance.values%5B%3A%2C%20widest%2C%206%5D%0A%20%20%20%20ndvi_px%20%3D%20((nir_px%20-%20red_px)%20%2F%20(nir_px%20%2B%20red_px)).reshape(%0A%20%20%20%20%20%20%20%2030%2C%2030%0A%20%20%20%20)%20%20%23%20rows%20north%20to%20south%2C%20columns%20west%20to%20east%0A%20%20%20%20strips%20%3D%20np.nanmean(ndvi_px.reshape(30%2C%206%2C%205)%2C%20axis%3D(0%2C%202))%0A%20%20%20%20print(dates%5Bwidest%5D.date()%2C%20%22%7C%20mean%20NDVI%20per%2050%20m%20strip%2C%20west%20to%20east%3A%22%2C%20strips.round(2))%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%20This%20is%20a%20crop%20season%20in%20one%20picture.%20NDVI%20is%20near-bare-soil%20in%20mid-May%2C%20the%20canopy%20greens%20up%20through%20June%2C%20and%20it%20holds%20at%20about%200.9%20through%20July%20and%20August.%0A%0A%20%20%20%20The%20band%20between%20p10%20and%20p90%20is%20widest%20on%2024%20June%2C%20and%20the%20cell%20above%20shows%20why%3A%20the%20square%20**straddles%20two%20fields**.%20On%20this%20run%20its%20western%20200%20m%20are%20still%20bare%20or%20sparse%20(NDVI%20about%200.3)%20while%20its%20eastern%20100%20m%20are%20already%20green%20(about%200.8)%2C%20with%20a%20sharp%2C%20straight%20boundary%20at%20x%20%E2%89%88%20428%2C600%20m.%20That's%20a%20field%20edge%20(perhaps%20corn%20beside%20later-planted%20soy)%2C%20not%20patchy%20emergence.%20It%20shows%20what%20mixing%20two%20crops%20at%20different%20stages%20does%20to%20the%20statistics%2C%20and%20it%20is%20why%20production%20aggregates%20per%20field%20polygon%2C%20not%20per%20box.%20Scenes%20circled%20in%20red%20had%20pixels%20screened%20out%2C%20and%20their%20statistics%20come%20from%20the%20remaining%20clear%20pixels%20only.%0A%0A%20%20%20%20%23%23%23%203.4%20Sentinel-1%3A%20why%20decibels%0A%0A%20%20%20%20Sentinel-1%20RTC%20delivers%20backscatter%20as%20**linear%20power**.%20%5B%60sentinel1_rtc%2Fingest%2Fextract.py%3A121-127%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel1_rtc%2Fingest%2Fextract.py%23L121-L127)%20converts%20it%20to%20decibels%20after%20the%20same%20stack%20%E2%86%92%20isel%20%E2%86%92%20compute%20pattern.%0A%0A%20%20%20%20Radar%20speckle%20is%20multiplicative%20noise%2C%20and%20the%20logarithm%20turns%20it%20additive.%20But%20averaging%20is%20not%20the%20same%20in%20the%20two%20spaces%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_4_sentinel_1_decibels()%3A%0A%20%20%20%20_src%20%3D%20inspect.getsource(s1_extract._extract_batch)%0A%20%20%20%20print(_src%5B_src.index(%22valid_lin%20%3D%22)%20%3A%20_src.index(%22ds%20%3D%20xr.Dataset%22)%5D)%0A%0A%20%20%20%20linear%20%3D%20np.array(%5B0.05%2C%200.002%5D)%0A%20%20%20%20print(%22dB%20of%20each%20pixel%3A%22%2C%20np.round(10%20*%20np.log10(linear)%2C%202))%0A%20%20%20%20print(f%22mean%20of%20the%20dB%20values%3A%20%7Bnp.mean(10%20*%20np.log10(linear))%3A.2f%7D%20dB%22)%0A%20%20%20%20print(f%22dB%20of%20the%20mean%20power%20%3A%20%7B10%20*%20np.log10(linear.mean())%3A.2f%7D%20dB%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%20Same%20two%20pixels%2C%20two%20%22averages%22%20that%20are%204%20dB%20apart.%20Speckle%20preserves%20the%20mean%20in%20linear%20power%2C%20so%20the%20standard%20practice%20is%20to%20average%20in%20linear%20power%20and%20then%20convert%20to%20dB%3B%20averaging%20in%20dB%20is%20biased%20low.%20Whichever%20space%20a%20field%20statistic%20uses%2C%20it%20has%20to%20stay%20the%20same%20between%20training%20and%20inference.%0A%0A%20%20%20%20The%20next%20cell%20measures%20the%20same%20gap%20on%20real%20Sentinel-1%20RTC%20data%20over%20our%20square%2C%20from%20Planetary%20Computer.%20Its%20URL%20signing%20is%20automatic%20and%20anonymous%2C%20so%20the%20cell%20needs%20no%20credentials%20and%20takes%20a%20few%20seconds.%20STAC%20searches%20take%20longitude%20and%20latitude%2C%20so%20the%20UTM%20%60bounds%60%20are%20converted%20first.%20Set%20%60RUN_S1%20%3D%20False%60%20to%20skip%20it.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_4_sentinel_1_decibels_2(bounds%2C%20field_points%2C%20to_lonlat)%3A%0A%20%20%20%20RUN_S1%20%3D%20True%0A%20%20%20%20if%20RUN_S1%3A%0A%20%20%20%20%20%20%20%20from%20tf_data_ml_utils.sentinel1_rtc.ingest.stac_client%20import%20S1StacClient%0A%0A%20%20%20%20%20%20%20%20%23%20STAC%20searches%20take%20longitude%20and%20latitude%3B%20%60bounds%60%20is%20in%20UTM%20metres.%0A%20%20%20%20%20%20%20%20lon0%2C%20lat0%20%3D%20to_lonlat.transform(bounds%5B0%5D%2C%20bounds%5B1%5D)%0A%20%20%20%20%20%20%20%20lon1%2C%20lat1%20%3D%20to_lonlat.transform(bounds%5B2%5D%2C%20bounds%5B3%5D)%0A%20%20%20%20%20%20%20%20s1_items%20%3D%20S1StacClient().search_bbox((lon0%2C%20lat0%2C%20lon1%2C%20lat1)%2C%20%222024-07-01%2F2024-07-31%22)%0A%20%20%20%20%20%20%20%20s1%20%3D%20s1_extract.extract_tile_samples_s1(s1_items%5B%3A4%5D%2C%20field_points%2C%20utm_epsg%3D32615)%0A%20%20%20%20%20%20%20%20vv_db%20%3D%20s1.backscatter.sel(band%3D%22vv%22).values%20%20%23%20(sample%2C%20time)%2C%20already%20in%20dB%0A%20%20%20%20%20%20%20%20print(dict(s1.sizes)%2C%20%22%7C%20dates%3A%22%2C%20%5Bstr(t)%5B%3A10%5D%20for%20t%20in%20s1.time.values%5D)%0A%20%20%20%20%20%20%20%20print(%22VV%20field%20mean%20of%20the%20dB%20values%3A%22%2C%20np.nanmean(vv_db%2C%20axis%3D0).round(2))%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22VV%20dB%20of%20the%20field-mean%20power%20%3A%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(10%20*%20np.log10(np.nanmean(10%20**%20(vv_db%20%2F%2010)%2C%20axis%3D0))).round(2)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Skipped%3A%20set%20RUN_S1%20%3D%20True%20to%20fetch%20real%20Sentinel-1%20RTC%20data%20from%20Planetary%20Computer.%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%20%23%23%23%203.5%20Which%20tile%20a%20field%20belongs%20to%0A%0A%20%20%20%20The%20field%20bake%20searches%20STAC%20**one%20tile%20at%20a%20time**%20(2.7).%20So%20every%20field%20must%20first%20be%20given%20the%20name%20of%20the%20Sentinel-2%20tile%20it%20falls%20in%2C%20and%20it%20must%20be%20a%20name%20Sentinel-2%20actually%20uses.%20Two%20production%20functions%20can%20give%20a%20name%3A%0A%0A%20%20%20%20-%20%60pixel_mgrs_tile%60%20(%5Bcore%2Fmgrs_assign.py%3A28-35%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fcore%2Fmgrs_assign.py%23L28-L35))%20returns%20the%20point's%20**MGRS%20designation**%3A%20the%20UTM%20zone%20(two%20digits)%2C%20the%20latitude%20band%20(a%20letter%20for%20each%208%C2%B0%20of%20latitude)%2C%20then%20two%20letters%20for%20the%20100%20km%20square.%0A%20%20%20%20-%20%60resolve_s2_tiles%60%20(%5Bcore%2Fs2_grid.py%3A133-222%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fcore%2Fs2_grid.py%23L133-L222))%20returns%20the%20tile%20whose%20**real%20footprint**%20contains%20the%20point.%20%60fetch_s2_tile_footprints%60%20(%5BL52-130%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fcore%2Fs2_grid.py%23L52-L130))%20asks%20STAC%20which%20tiles%20have%20scenes%20over%20the%20area%20and%20caches%20their%20footprints.%0A%0A%20%20%20%20The%20two%20disagree%20near%20an%20MGRS%20band%20line%20(32%C2%B0N%2C%2040%C2%B0N%20and%2048%C2%B0N%20in%20the%20US).%20The%20100%20km%20squares%20are%20laid%20out%20in%20UTM%20metres%2C%20so%20some%20of%20them%20straddle%20a%20line%20of%20latitude.%20Sentinel-2%20publishes%20such%20a%20square%20under%20one%20name%2C%20while%20MGRS%20gives%20the%20part%20below%20the%20line%20the%20southern%20band's%20letter.%20That%20name%20is%20a%20**phantom%20tile**%3A%20STAC%20has%20no%20scenes%20for%20it%20(%5Btf-data-ml-utils%20issue%20728%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fissues%2F728)).%20At%20the%20pinned%20commit%20both%20field-list%20builders%20call%20%60resolve_s2_tiles%60%3A%20the%20US%20one%20at%20%5Bsampling%2Fbuild_field_list.py%3A383-385%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fsampling%2Fbuild_field_list.py%23L383-L385)%2C%20France%20at%20%5Bsampling%2Frpg_field_list.py%3A309-316%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fsampling%2Frpg_field_list.py%23L309-L316).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_5_tile_field_belongs()%3A%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.core%20import%20mgrs_assign%2C%20s2_grid%0A%0A%20%20%20%20show(mgrs_assign.pixel_mgrs_tile)%0A%20%20%20%20show(s2_grid.resolve_s2_tiles)%0A%20%20%20%20return%20mgrs_assign%2C%20s2_grid%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%60pixel_mgrs_tile%60%20calls%20the%20%60mgrs%60%20library%2C%20which%20this%20notebook's%20environment%20doesn't%20install.%20The%20next%20cell%20supplies%20a%20stand-in%20for%20it%3A%20the%20standard%20MGRS%20lettering%20in%20a%20dozen%20lines.%20It%20is%20valid%20for%20these%20latitudes%20(it%20ignores%20the%20special%20zones%20around%20Norway%20and%20Svalbard)%2C%20and%20it%20is%20only%20installed%20if%20the%20real%20library%20is%20missing.%20It%20is%20checked%20against%20three%20known%20names%3A%2015TVG%20for%20the%20Iowa%20square%20of%202.5%2C%20and%20the%20two%20names%20of%20issue%20728's%20French%20example%2C%20a%20field%20at%2047.93%C2%B0N%2C%202.37%C2%B0E%20that%20MGRS%20calls%2031TDP%20although%20its%20Sentinel-2%20tile%20is%2031UDP.%20With%20it%20in%20place%2C%20%60pixel_mgrs_tile%60%20and%20the%20diagnostics%20inside%20%60resolve_s2_tiles%60%20run%20unchanged.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_5_tile_field_belongs_2(FIELD_X0%2C%20FIELD_Y0%2C%20mgrs_assign)%3A%0A%20%20%20%20import%20sys%0A%20%20%20%20import%20types%0A%0A%20%20%20%20_BANDS%20%3D%20%22CDEFGHJKLMNPQRSTUVWX%22%20%20%23%208%C2%B0%20latitude%20bands%20from%2080%C2%B0S%3B%20I%20and%20O%20are%20skipped%0A%20%20%20%20_COLUMNS%20%3D%20(%0A%20%20%20%20%20%20%20%20%22ABCDEFGH%22%2C%0A%20%20%20%20%20%20%20%20%22JKLMNPQR%22%2C%0A%20%20%20%20%20%20%20%20%22STUVWXYZ%22%2C%0A%20%20%20%20)%20%20%23%20100%20km%20column%20letters%20repeat%20every%20three%20zones%0A%20%20%20%20_ROWS%20%3D%20%22ABCDEFGHJKLMNPQRSTUV%22%20%20%23%20100%20km%20row%20letters%20repeat%20every%202%2C000%20km%0A%0A%20%20%20%20def%20mgrs_designation(lat%2C%20lon)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Return%20the%20five-character%20MGRS%20name%20of%20a%20point%3A%20zone%2C%20band%2C%20100%20km%20column%2C%20100%20km%20row.%22%22%22%0A%20%20%20%20%20%20%20%20zone%20%3D%20int((lon%20%2B%20180)%20%2F%2F%206)%20%2B%201%0A%20%20%20%20%20%20%20%20band%20%3D%20_BANDS%5Bint((lat%20%2B%2080)%20%2F%2F%208)%5D%0A%20%20%20%20%20%20%20%20easting%2C%20northing%20%3D%20pyproj.Transformer.from_crs(%0A%20%20%20%20%20%20%20%20%20%20%20%204326%2C%2032600%20%2B%20zone%2C%20always_xy%3DTrue%0A%20%20%20%20%20%20%20%20).transform(lon%2C%20lat)%0A%20%20%20%20%20%20%20%20column%20%3D%20_COLUMNS%5B(zone%20-%201)%20%25%203%5D%5Bint(easting%20%2F%2F%20100_000)%20-%201%5D%0A%20%20%20%20%20%20%20%20row%20%3D%20_ROWS%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(int(northing%20%2F%2F%20100_000)%20%2B%20(0%20if%20zone%20%25%202%20else%205))%20%25%2020%0A%20%20%20%20%20%20%20%20%5D%20%20%23%20even%20zones%20start%20their%20rows%20at%20F%0A%20%20%20%20%20%20%20%20return%20f%22%7Bzone%3A02d%7D%7Bband%7D%7Bcolumn%7D%7Brow%7D%22%0A%0A%20%20%20%20class%20_StandInMGRS%3A%0A%20%20%20%20%20%20%20%20def%20toMGRS(self%2C%20lat%2C%20lon%2C%20MGRSPrecision%3D0)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mgrs_designation(lat%2C%20lon)%0A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20import%20mgrs%20%20%23%20noqa%3A%20F401%20%20(use%20the%20real%20library%20when%20it%20is%20installed)%0A%20%20%20%20except%20ModuleNotFoundError%20as%20missing%3A%0A%20%20%20%20%20%20%20%20if%20missing.name%20!%3D%20%22mgrs%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20%20%23%20a%20dependency%20of%20mgrs%20is%20broken%3A%20surface%20it%20rather%20than%20hide%20it%0A%20%20%20%20%20%20%20%20sys.modules%5B%22mgrs%22%5D%20%3D%20types.SimpleNamespace(MGRS%3D_StandInMGRS)%0A%0A%20%20%20%20iowa_lon%2C%20iowa_lat%20%3D%20pyproj.Transformer.from_crs(32615%2C%204326%2C%20always_xy%3DTrue).transform(%0A%20%20%20%20%20%20%20%20FIELD_X0%2C%20FIELD_Y0%0A%20%20%20%20)%0A%20%20%20%20for%20lat%2C%20lon%2C%20known%20in%20%5B%0A%20%20%20%20%20%20%20%20(iowa_lat%2C%20iowa_lon%2C%20%2215TVG%22)%2C%0A%20%20%20%20%20%20%20%20(47.93%2C%202.37%2C%20%2231TDP%22)%2C%0A%20%20%20%20%20%20%20%20(48.20%2C%202.37%2C%20%2231UDP%22)%2C%0A%20%20%20%20%5D%3A%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Blat%3A5.2f%7D%C2%B0N%20%7Blon%3A6.2f%7D%C2%B0E%20-%3E%20%7Bmgrs_assign.pixel_mgrs_tile(lat%2C%20lon)%7D%20(known%3A%20%7Bknown%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%20Now%20a%20band%20line.%20The%20two%20footprints%20below%20are%20synthetic%20squares%2C%20but%20their%20corners%20are%20the%20real%20grids%20of%20two%20Sentinel-2%20tiles%20in%20southern%20Georgia%2C%2017SKR%20and%2017RKQ%20(UTM%20zone%2017N)%3A%20each%20starts%20at%20the%20top-left%20corner%20its%20STAC%20items%20give%20and%20is%2010%2C980%20pixels%20of%2010%20m%20on%20a%20side.%2017SKR%20straddles%2032%C2%B0N.%20The%20fields%20just%20south%20of%20that%20line%20are%20what%20issue%20728%20is%20about%3A%20their%20MGRS%20designation%20is%2017RKR%2C%20one%20of%20the%20Georgia%20phantom%20tiles%20the%20issue%20lists.%0A%0A%20%20%20%20Writing%20the%20footprints%20to%20a%20cache%20file%20lets%20the%20production%20%60resolve_s2_tiles%60%20run%20offline.%20%60fetch_s2_tile_footprints%60%20reads%20a%20cache%20instead%20of%20querying%20STAC%20when%20the%20cache's%20sidecar%20file%20says%20it%20covers%20the%20request%2C%20from%20the%20same%20source%20and%20probe%20dates.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_5_tile_field_belongs_3(s2_grid)%3A%0A%20%20%20%20import%20tempfile%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20TILE_TOP_LEFT%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%2217SKR%22%3A%20(199_980%2C%203_600_000)%2C%0A%20%20%20%20%20%20%20%20%2217RKQ%22%3A%20(199_980%2C%203_500_040)%2C%0A%20%20%20%20%7D%20%20%23%20UTM%2017N%20metres%2C%20from%20the%20tiles'%20STAC%20items%0A%20%20%20%20SIDE_M%20%3D%2010_980%20*%2010%0A%20%20%20%20footprints%20%3D%20gpd.GeoDataFrame(%0A%20%20%20%20%20%20%20%20%7B%22mgrs_tile%22%3A%20list(TILE_TOP_LEFT)%7D%2C%0A%20%20%20%20%20%20%20%20geometry%3Dgpd.GeoSeries(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Bbox(x0%2C%20y0%20-%20SIDE_M%2C%20x0%20%2B%20SIDE_M%2C%20y0)%20for%20x0%2C%20y0%20in%20TILE_TOP_LEFT.values()%5D%2C%20crs%3D32617%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.segmentize(%0A%20%20%20%20%20%20%20%20%20%20%20%201_000%0A%20%20%20%20%20%20%20%20)%20%20%23%20extra%20vertices%2C%20so%20the%20edges%20keep%20their%20shape%20in%20longitude%20and%20latitude%0A%20%20%20%20%20%20%20%20.to_crs(4326)%2C%0A%20%20%20%20)%0A%20%20%20%20cache%20%3D%20Path(tempfile.mkdtemp())%20%2F%20%22s2_footprints.geojson%22%0A%20%20%20%20footprints.to_file(cache%2C%20driver%3D%22GeoJSON%22)%0A%20%20%20%20Path(f%22%7Bcache%7D.meta.json%22).write_text(%0A%20%20%20%20%20%20%20%20json.dumps(%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%22bbox%22%3A%20%5B-86.0%2C%2029.5%2C%20-82.0%2C%2033.5%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22source%22%3A%20%22aws%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22datetime_range%22%3A%20s2_grid._PROBE_DAYS%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20print(footprints.bounds.round(3).set_index(footprints%5B%22mgrs_tile%22%5D))%0A%20%20%20%20to_utm17%20%3D%20pyproj.Transformer.from_crs(4326%2C%2032617%2C%20always_xy%3DTrue)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%2232%C2%B0N%20at%2083.6%C2%B0W%20lies%20%7Bto_utm17.transform(-83.6%2C%2032.0)%5B1%5D%20%2F%201000%3A.1f%7D%20km%20north%20in%20UTM%20zone%2017N%22%0A%20%20%20%20)%0A%20%20%20%20return%20(cache%2C)%0A%0A%0A%40app.cell%0Adef%20s3_5_tile_field_belongs_4(cache%2C%20mgrs_assign%2C%20s2_grid)%3A%0A%20%20%20%20points%20%3D%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%22where%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22north%20of%2032%C2%B0N%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22just%20south%20of%2032%C2%B0N%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22just%20south%20of%2032%C2%B0N%2C%20further%20east%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22in%20the%20overlap%2C%20near%20its%20south%20edge%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22in%20the%20overlap%2C%20near%20its%20north%20edge%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22deep%20inside%2017RKQ%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22west%20of%20both%20footprints%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22lat%22%3A%20%5B32.20%2C%2031.95%2C%2031.80%2C%2031.53%2C%2031.60%2C%2031.00%2C%2031.80%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22lon%22%3A%20%5B-83.60%2C%20-83.60%2C%20-83.30%2C%20-83.60%2C%20-83.60%2C%20-83.60%2C%20-84.50%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20resolved%2C%20report%20%3D%20s2_grid.resolve_s2_tiles(%0A%20%20%20%20%20%20%20%20points%5B%22lat%22%5D.to_numpy()%2C%20points%5B%22lon%22%5D.to_numpy()%2C%20source%3D%22aws%22%2C%20cache_path%3Dcache%0A%20%20%20%20)%0A%20%20%20%20points%5B%22MGRS%20designation%22%5D%20%3D%20%5B%0A%20%20%20%20%20%20%20%20mgrs_assign.pixel_mgrs_tile(la%2C%20lo)%0A%20%20%20%20%20%20%20%20for%20la%2C%20lo%20in%20zip(points%5B%22lat%22%5D%2C%20points%5B%22lon%22%5D%2C%20strict%3DTrue)%0A%20%20%20%20%5D%0A%20%20%20%20points%5B%22resolved%20S2%20tile%22%5D%20%3D%20resolved%0A%20%20%20%20print(json.dumps(report%2C%20indent%3D1))%0A%20%20%20%20points%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%20report%20separates%20the%20two%20reasons%20a%20designation%20and%20a%20resolved%20tile%20can%20differ%2C%20because%20only%20one%20is%20a%20bug%3A%0A%0A%20%20%20%20-%20**Phantom**%20(%60n_phantom_naive_tile%60)%3A%20the%20designation%20names%20a%20tile%20that%20has%20no%20footprint.%20The%20two%20points%20just%20south%20of%2032%C2%B0N%20are%20called%2017RKR%20and%20moved%20to%2017SKR.%20Before%20the%20fix%2C%20the%20bake%20would%20have%20searched%20STAC%20for%2017RKR%2C%20found%20no%20scenes%20and%20skipped%20them.%0A%20%20%20%20-%20**Overlap**%20(%60n_reassigned_within_overlap%60)%3A%20both%20names%20are%20real%2C%20and%20the%20point%20is%20moved%20to%20the%20tile%20whose%20centre%20is%20nearer.%20Neighbouring%20tiles%20overlap%20by%209.8%20km%2C%20and%20either%20tile%20sees%20the%20point%2C%20so%20this%20is%20harmless.%20The%20tie-break%20measures%20distance%20in%20EPSG%3A6933%2C%20an%20equal-area%20world%20projection%2C%20so%20a%20degree%20of%20longitude%20isn't%20treated%20as%20being%20as%20long%20as%20a%20degree%20of%20latitude.%0A%0A%20%20%20%20The%20point%20west%20of%20both%20footprints%20is%20**unresolved**%3A%20an%20empty%20name.%20The%20builders%20leave%20it%20out%2C%20and%20%60resolve_s2_tiles%60%20logs%20how%20many%20points%20fell%20outside%20every%20footprint.%20(Its%20designation%2C%2016RGA%2C%20is%20another%20of%20the%20issue's%20Georgia%20phantoms%3B%20its%20real%20tile%20simply%20isn't%20among%20our%20two%20footprints.)%0A%0A%20%20%20%20**What%20issue%20728%20measured.**%20Before%20the%20fix%2C%20the%20field%20lists%20used%20the%20MGRS%20designation.%20The%20bake's%20STAC%20search%20for%20a%20phantom%20tile%20returned%20no%20scenes%2C%20and%20the%20tile-year%20was%20marked%20%60skipped%60%20without%20an%20error.%20The%20issue's%20audit%20of%20the%20S3%20corpus%20counted%20the%20fields%20assigned%20to%20a%20tile%20but%20never%20baked%3A%20Georgia%20lost%203%2C897%20of%2019%2C158%20fields%20(20.3%20%25)%2C%20Louisiana%2011.6%20%25%2C%20Alabama%202.2%20%25%2C%20and%20Florida%2C%20a%20control%20lying%20entirely%20south%20of%2032%C2%B0N%2C%20none.%20At%20the%20pinned%20commit%20the%20bake%20also%20refuses%20to%20finish%20quietly%3A%20%60require_scenes%60%20(%5Bstreaming%2Fbake_fold.py%3A129-137%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fstreaming%2Fbake_fold.py%23L129-L137))%20raises%20after%20the%20manifest%20is%20written%20if%20any%20tile-year%20found%20no%20scene%20at%20any%20cloud%20cover.%20The%20issue%20is%20still%20open%3A%20the%20code%20is%20fixed%2C%20but%20whether%20the%20field%20lists%20built%20before%20the%20fix%20have%20been%20rebuilt%20and%20re-baked%20is%20not%20established.%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%23%203.6%20From%20field%20predictions%20to%20crop%20area%2C%20and%20roll-ups%0A%0A%20%20%20%20The%20crop%20classifier%20writes%20one%20row%20per%20field%3A%20a%20probability%20for%20each%20crop%20(%60prob_%3Ccrop%3E%60%2C%20summing%20to%201)%2C%20the%20most%20likely%20crop%20(%60pred_crop%60)%20and%20that%20crop's%20probability%20(%60confidence%60).%20%60aggregate_predictions_to_admin%60%20(%5Bparquet%2Fprediction_aggregation.py%3A71-178%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fparquet%2Fprediction_aggregation.py%23L71-L178))%20turns%20the%20rows%20into%20crop%20area%20for%20an%20administrative%20level%3A%20adm2%20(a%20county)%2C%20adm1%20(a%20state)%20or%20adm0%20(the%20nation).%20It%20computes%20two%20areas%20side%20by%20side%3A%0A%0A%20%20%20%20-%20**hard%3A**%20%60pred_area_ha%60%2C%20the%20area%20of%20the%20fields%20whose%20most%20likely%20crop%20is%20this%20one%3B%0A%20%20%20%20-%20**soft%3A**%20%60prob_area_ha%60%2C%20the%20sum%20of%20field%20area%20%C3%97%20the%20crop's%20probability%2C%20over%20every%20field.%0A%0A%20%20%20%20Each%20is%20divided%20by%20the%20total%20field%20area%20to%20give%20a%20share%20(%60pred_share%60%2C%20%60prob_share%60).%20%60confidence_wmean%60%20is%20the%20area-weighted%20mean%20confidence%20of%20the%20fields%20predicted%20as%20the%20crop.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_6_field_predictions_crop()%3A%0A%20%20%20%20from%20scipy.special%20import%20expit%2C%20logit%0A%20%20%20%20from%20scipy.stats%20import%20norm%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.parquet.admin_aggregation%20import%20load_admin_gazetteer%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.parquet.prediction_aggregation%20import%20(%0A%20%20%20%20%20%20%20%20aggregate_predictions_to_admin%2C%0A%20%20%20%20)%0A%0A%20%20%20%20show(aggregate_predictions_to_admin)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20aggregate_predictions_to_admin%2C%0A%20%20%20%20%20%20%20%20expit%2C%0A%20%20%20%20%20%20%20%20load_admin_gazetteer%2C%0A%20%20%20%20%20%20%20%20logit%2C%0A%20%20%20%20%20%20%20%20norm%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%20A%20synthetic%20test.%20Three%20Kansas%20counties%20have%203%2C000%2C%202%2C000%20and%201%2C000%20fields%2C%20of%20which%20a%20rare%20crop%20(call%20it%20sorghum)%20is%20about%204%20%25%2C%2010%20%25%20and%2025%20%25.%20Each%20field%20gets%20a%20classifier%20score%2C%20drawn%20from%20one%20bell%20curve%20for%20sorghum%20and%20from%20another%2C%20two%20standard%20deviations%20lower%2C%20for%20everything%20else.%20Its%20probability%20is%20the%20exact%20Bayes%20posterior%20for%20its%20county's%20prevalence%2C%20so%20the%20probabilities%20are%20**calibrated%20by%20construction**%3A%20among%20fields%20given%20p%20%3D%200.3%2C%20about%2030%20%25%20really%20are%20sorghum.%20The%20rows%20then%20go%20through%20the%20production%20aggregator%2C%20with%20a%20gazetteer%20(the%20list%20of%20admin%20identifiers%20it%20resolves%20keys%20against)%20built%20for%20these%20counties.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_6_field_predictions_crop_2(load_admin_gazetteer%2C%20norm)%3A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(42)%0A%20%20%20%20COUNTIES%20%3D%20%7B%20%20%23%20admin%20identifier%3A%20(number%20of%20fields%2C%20share%20of%20fields%20that%20are%20sorghum)%0A%20%20%20%20%20%20%20%20%22ADM0%3AUSA%2FADM1%3AKansas%2FADM2%3AFinney%22%3A%20(3000%2C%200.04)%2C%0A%20%20%20%20%20%20%20%20%22ADM0%3AUSA%2FADM1%3AKansas%2FADM2%3AGray%22%3A%20(2000%2C%200.10)%2C%0A%20%20%20%20%20%20%20%20%22ADM0%3AUSA%2FADM1%3AKansas%2FADM2%3AHaskell%22%3A%20(1000%2C%200.25)%2C%0A%20%20%20%20%7D%0A%20%20%20%20gazetteer%20%3D%20load_admin_gazetteer(%0A%20%20%20%20%20%20%20%20pd.DataFrame(%7B%22identifier%22%3A%20%5B*COUNTIES%2C%20%22ADM0%3AUSA%2FADM1%3AKansas%22%2C%20%22ADM0%3AUSA%22%5D%7D)%0A%20%20%20%20)%0A%20%20%20%20SEPARATION%20%3D%202.0%20%20%23%20distance%20between%20the%20two%20classes'%20mean%20scores%2C%20in%20standard%20deviations%0A%0A%20%20%20%20def%20calibrated_probability(score%2C%20prevalence)%3A%0A%20%20%20%20%20%20%20%20%22%22%22P(sorghum%20%7C%20score)%20by%20Bayes'%20rule%2C%20for%20a%20known%20prevalence.%22%22%22%0A%20%20%20%20%20%20%20%20s%2C%20o%20%3D%20norm.pdf(score%2C%20SEPARATION%2C%201)%2C%20norm.pdf(score%2C%200%2C%201)%0A%20%20%20%20%20%20%20%20return%20prevalence%20*%20s%20%2F%20(prevalence%20*%20s%20%2B%20(1%20-%20prevalence)%20*%20o)%0A%0A%20%20%20%20frames%20%3D%20%5B%5D%0A%20%20%20%20for%20county%2C%20(_n%2C%20prevalence)%20in%20COUNTIES.items()%3A%0A%20%20%20%20%20%20%20%20is_sorghum%20%3D%20_rng.random(_n)%20%3C%20prevalence%0A%20%20%20%20%20%20%20%20score%20%3D%20_rng.normal(np.where(is_sorghum%2C%20SEPARATION%2C%200.0)%2C%201.0)%0A%20%20%20%20%20%20%20%20area%20%3D%20_rng.lognormal(np.log(40)%2C%200.6%2C%20_n)%20%20%23%20field%20sizes%20around%2040%20ha%2C%20a%20few%20much%20larger%0A%20%20%20%20%20%20%20%20frames.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20pd.DataFrame(%0A%20%20%20%20%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%20%20%20%20%22county%22%3A%20county%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22area_ha%22%3A%20area%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22is_sorghum%22%3A%20is_sorghum%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22p%22%3A%20calibrated_probability(score%2C%20prevalence)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20fields%20%3D%20pd.concat(frames%2C%20ignore_index%3DTrue)%0A%0A%20%20%20%20def%20prediction_rows(p)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Build%20the%20prediction%20sink's%20columns.%20Production%20keys%20the%20county%20in%20%60county_fips%60%3B%20the%20gazetteer%20resolves%20it.%22%22%22%0A%20%20%20%20%20%20%20%20return%20pd.DataFrame(%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%22county_fips%22%3A%20fields%5B%22county%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22field_area_ha%22%3A%20fields%5B%22area_ha%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22target_year%22%3A%202025%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22prediction_doy%22%3A%20200%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22prob_sorghum%22%3A%20p%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22prob_other%22%3A%201%20-%20p%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22pred_crop%22%3A%20np.where(p%20%3E%3D%200.5%2C%20%22sorghum%22%2C%20%22other%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22confidence%22%3A%20np.maximum(p%2C%201%20-%20p)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20right%20%3D%20(fields%5B%22p%22%5D%20%3E%3D%200.5)%20%3D%3D%20fields%5B%22is_sorghum%22%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22fields%20classified%20correctly%3A%20%7Bright.mean()%3A.1%25%7D%3B%20always%20answering%20'other'%20would%20score%20%7B1%20-%20fields%5B'is_sorghum'%5D.mean()%3A.1%25%7D%22%0A%20%20%20%20)%0A%20%20%20%20%23%203.6's%20F16%20example%20draws%20its%20NDVI%20noise%20here%2C%20straight%20after%20the%20fields%2C%20so%20it%20is%20the%20same%0A%20%20%20%20%23%20whichever%20cells%20re-run.%0A%20%20%20%20ndvi_noise%20%3D%20_rng.normal(0%2C%200.05%2C%20len(fields))%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20SEPARATION%2C%0A%20%20%20%20%20%20%20%20calibrated_probability%2C%0A%20%20%20%20%20%20%20%20fields%2C%0A%20%20%20%20%20%20%20%20gazetteer%2C%0A%20%20%20%20%20%20%20%20ndvi_noise%2C%0A%20%20%20%20%20%20%20%20prediction_rows%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20s3_6_field_predictions_crop_3(%0A%20%20%20%20aggregate_predictions_to_admin%2C%0A%20%20%20%20expit%2C%0A%20%20%20%20fields%2C%0A%20%20%20%20gazetteer%2C%0A%20%20%20%20logit%2C%0A%20%20%20%20prediction_rows%2C%0A)%3A%0A%20%20%20%20def%20sorghum(agg%2C%20column)%3A%0A%20%20%20%20%20%20%20%20return%20agg%5Bagg%5B%22crop%22%5D%20%3D%3D%20%22sorghum%22%5D.set_index(%22admin_key%22)%5Bcolumn%5D%0A%0A%20%20%20%20overconfident%20%3D%20expit(%0A%20%20%20%20%20%20%20%203%20*%20logit(fields%5B%22p%22%5D)%0A%20%20%20%20)%20%20%23%20the%20same%20ranking%20and%20the%20same%20hard%20calls%2C%20pushed%20towards%200%20and%201%0A%20%20%20%20calibrated_adm2%20%3D%20aggregate_predictions_to_admin(%0A%20%20%20%20%20%20%20%20prediction_rows(fields%5B%22p%22%5D)%2C%20%22adm2%22%2C%20gazetteer%0A%20%20%20%20)%0A%20%20%20%20overconfident_adm2%20%3D%20aggregate_predictions_to_admin(%0A%20%20%20%20%20%20%20%20prediction_rows(overconfident)%2C%20%22adm2%22%2C%20gazetteer%0A%20%20%20%20)%0A%20%20%20%20area_table%20%3D%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%22truth%20(ha)%22%3A%20fields%5Bfields%5B%22is_sorghum%22%5D%5D.groupby(%22county%22)%5B%22area_ha%22%5D.sum()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22hard%20(ha)%22%3A%20sorghum(calibrated_adm2%2C%20%22pred_area_ha%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22soft%2C%20calibrated%20(ha)%22%3A%20sorghum(calibrated_adm2%2C%20%22prob_area_ha%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22soft%2C%20overconfident%20(ha)%22%3A%20sorghum(overconfident_adm2%2C%20%22prob_area_ha%22)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20).round(0)%0A%20%20%20%20area_table.index%20%3D%20area_table.index.str.rsplit(%22%3A%22%2C%20n%3D1).str%5B1%5D%0A%20%20%20%20area_table%0A%0A%0A%40app.cell%0Adef%20s3_6_field_predictions_crop_4(SEPARATION%2C%20calibrated_probability)%3A%0A%20%20%20%20def%20one_county(seed%2C%20n%3D3000%2C%20prevalence%3D0.04)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Return%20the%20true%2C%20hard%20and%20soft%20sorghum%20area%20(ha)%20of%20one%20simulated%20county.%22%22%22%0A%20%20%20%20%20%20%20%20r%20%3D%20np.random.default_rng(seed)%0A%20%20%20%20%20%20%20%20is_sorghum%20%3D%20r.random(n)%20%3C%20prevalence%0A%20%20%20%20%20%20%20%20p%20%3D%20calibrated_probability(r.normal(np.where(is_sorghum%2C%20SEPARATION%2C%200.0)%2C%201.0)%2C%20prevalence)%0A%20%20%20%20%20%20%20%20area%20%3D%20r.lognormal(np.log(40)%2C%200.6%2C%20n)%0A%20%20%20%20%20%20%20%20return%20area%5Bis_sorghum%5D.sum()%2C%20area%5Bp%20%3E%3D%200.5%5D.sum()%2C%20(area%20*%20p).sum()%0A%0A%20%20%20%20truth_total%2C%20hard_total%2C%20soft_total%20%3D%20np.array(%5Bone_county(seed)%20for%20seed%20in%20range(500)%5D).sum(%0A%20%20%20%20%20%20%20%20axis%3D0%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22500%20simulated%20counties%20at%204%20%25%2C%20added%20up%3A%20hard%20%2F%20truth%20%3D%20%7Bhard_total%20%2F%20truth_total%3A.3f%7D%3B%20soft%20%2F%20truth%20%3D%20%7Bsoft_total%20%2F%20truth_total%3A.3f%7D%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%20Read%20the%20area%20table%20from%20left%20to%20right.%0A%0A%20%20%20%20-%20**Hard%20counts%20are%20less%20than%20half%20the%20truth%20in%20the%20rare-crop%20county**%20(Finney%3A%202%2C156%20ha%20against%204%2C991%20ha)%20and%20closer%20where%20the%20crop%20is%20common%20(Haskell%3A%2011%2C485%20against%2013%2C118).%20A%20sorghum%20field%20whose%20score%20is%20ambiguous%20gets%20a%20probability%20below%200.5%2C%20because%20the%20prior%20says%20sorghum%20is%20rare.%20So%20misses%20outnumber%20false%20alarms.%20The%20hard%20area%20is%20the%20truth%20plus%20the%20false%20alarms'%20area%20minus%20the%20misses'%20area%2C%20and%20those%20two%20only%20cancel%20at%20one%20particular%20prevalence.%0A%20%20%20%20-%20**Soft%20sums%20are%20right%20when%20the%20probabilities%20are%20calibrated%20within%20each%20county%20and%20for%20fields%20of%20every%20size**%20(here%20field%20size%20is%20drawn%20independently%20of%20the%20crop%2C%20so%20calibration%20by%20count%20is%20enough)%3A%20if%20p%20is%20the%20true%20chance%20that%20a%20field%20is%20sorghum%2C%20then%20area%20%C3%97%20p%20is%2C%20on%20average%2C%20its%20sorghum%20area.%20The%20500-county%20repeat%20in%20the%20last%20cell%20shows%20it%3A%20added%20up%20over%20all%20500%2C%20the%20hard%20area%20is%200.388%20of%20the%20truth%20and%20the%20soft%20area%201.002.%20Any%20one%20county%20still%20misses%20by%20chance%3A%20Finney's%20calibrated%20soft%20area%20is%205%2C502%20ha%20against%204%2C991.%0A%20%20%20%20-%20**Soft%20sums%20are%20only%20as%20good%20as%20the%20calibration.**%20The%20overconfident%20column%20has%20the%20same%20ranking%20and%20the%20same%20hard%20calls%2C%20but%20probabilities%20pushed%20towards%200%20and%201%2C%20and%20it%20falls%20back%20towards%20the%20hard%20count%20(Finney%3A%202%2C451%20ha).%20Calibration%20also%20depends%20on%20prevalence%3A%20probabilities%20calibrated%20for%20one%20county's%20crop%20share%20are%20not%20calibrated%20for%20a%20county%20where%20the%20crop%20is%20rarer.%0A%0A%20%20%20%20The%20headline%20accuracy%20hides%20all%20of%20this.%20The%20classifier%20gets%2094.0%20%25%20of%20fields%20right%2C%20yet%20answering%20%22other%22%20for%20every%20field%20would%20score%2090.5%20%25.%0A%0A%20%20%20%20**Roll-ups.**%20Each%20level%20is%20computed%20straight%20from%20the%20field%20rows.%20For%20the%20state%20(adm1)%20and%20the%20nation%20(adm0)%2C%20%60canonical_admin_key%60%20maps%20every%20field's%20county%20to%20its%20ancestor%2C%20so%20all%20the%20state's%20fields%20form%20one%20group%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20fields%20(6%2C000%20rows)%20%E2%94%80%E2%94%80%E2%94%AC%E2%94%80%E2%94%80%20group%20by%20county%20%E2%94%80%E2%94%80%3E%20adm2%20rows%20(3)%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%E2%94%9C%E2%94%80%E2%94%80%20group%20by%20state%20%20%E2%94%80%E2%94%80%3E%20adm1%20row%20%20(1)%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%E2%94%94%E2%94%80%E2%94%80%20group%20by%20nation%20%E2%94%80%E2%94%80%3E%20adm0%20row%20%20(1)%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%20never%20adm2%20rows%20%E2%94%80%E2%94%80%3E%20adm1%20row%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_6_field_predictions_crop_5(%0A%20%20%20%20aggregate_predictions_to_admin%2C%0A%20%20%20%20fields%2C%0A%20%20%20%20gazetteer%2C%0A%20%20%20%20prediction_rows%2C%0A)%3A%0A%20%20%20%20levels%20%3D%20%7B%0A%20%20%20%20%20%20%20%20lvl%3A%20aggregate_predictions_to_admin(prediction_rows(fields%5B%22p%22%5D)%2C%20lvl%2C%20gazetteer)%0A%20%20%20%20%20%20%20%20for%20lvl%20in%20(%22adm2%22%2C%20%22adm1%22%2C%20%22adm0%22)%0A%20%20%20%20%7D%0A%20%20%20%20rollup%20%3D%20pd.concat(levels.values())%0A%20%20%20%20rollup%20%3D%20rollup%5Brollup%5B%22crop%22%5D%20%3D%3D%20%22sorghum%22%5D%5B%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22admin_key%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22adm_level%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22n_fields_total%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22total_area_ha%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22pred_area_ha%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22prob_area_ha%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22prob_share%22%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%5D%0A%20%20%20%20county_rows%20%3D%20rollup%5Brollup%5B%22adm_level%22%5D%20%3D%3D%20%22adm2%22%5D%0A%20%20%20%20state_row%20%3D%20rollup%5Brollup%5B%22adm_level%22%5D%20%3D%3D%20%22adm1%22%5D.iloc%5B0%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22soft%20area%3A%20sum%20of%20the%20counties%20%7Bcounty_rows%5B'prob_area_ha'%5D.sum()%3A%2C.1f%7D%20ha%20%7C%20the%20state%20%7Bstate_row%5B'prob_area_ha'%5D%3A%2C.1f%7D%20ha%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22share%3A%20plain%20mean%20of%20the%20counties%20%7Bcounty_rows%5B'prob_share'%5D.mean()%3A.3f%7D%20%7C%20the%20state%20%7Bstate_row%5B'prob_share'%5D%3A.3f%7D%22%0A%20%20%20%20)%0A%20%20%20%20rollup.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%20Areas%20add%20up%3A%20the%20state's%20soft%20area%2C%2027%2C594.5%20ha%2C%20is%20exactly%20the%20sum%20of%20its%20counties'.%20Shares%20don't%3A%20the%20state's%20share%2C%200.095%2C%20is%20the%20counties'%20shares%20**weighted%20by%20their%20field%20area**%2C%20not%20their%20plain%20mean%2C%200.132%2C%20because%20Finney's%203%2C000%20fields%20count%20for%20more%20than%20Haskell's%201%2C000.%0A%0A%20%20%20%20Percentiles%20don't%20add%20up%20at%20all.%20You%20can't%20build%20a%20state's%20median%20from%20its%20counties'%20medians%3B%20you%20need%20every%20field.%20The%20field-feature%20roll-up%2C%20%60aggregate_features_to_admin%60%20(%5Bparquet%2Fadmin_aggregation.py%3A159-252%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fparquet%2Fadmin_aggregation.py%23L159-L252))%2C%20therefore%20also%20works%20from%20field%20rows.%20For%20each%20group%2C%20%60_group_stats%60%20(%5BL121-156%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fparquet%2Fadmin_aggregation.py%23L121-L156))%20writes%20a%20mean%20and%20standard%20deviation%20**weighted%20by%20field%20area**%2C%20beside%20percentiles%20from%20%60np.nanpercentile%60%20that%20are%20**unweighted**%3A%20every%20field%20counts%20once%2C%20whatever%20its%20size.%20That%20mismatch%20in%20one%20row%20is%20finding%20F16%20(%5Btf-model-lab%20issue%2043%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F43)).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_6_field_predictions_crop_6()%3A%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.parquet.admin_aggregation%20import%20(%0A%20%20%20%20%20%20%20%20_group_stats%2C%0A%20%20%20%20%20%20%20%20aggregate_features_to_admin%2C%0A%20%20%20%20)%0A%0A%20%20%20%20show(_group_stats)%0A%20%20%20%20return%20(aggregate_features_to_admin%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%20To%20see%20F16%2C%20give%20the%20synthetic%20fields%20a%20peak%20NDVI%20that%20rises%20with%20field%20size%20(large%20farms%20here%20manage%20their%20crop%20better)%20and%20is%20higher%20in%20Haskell.%20The%20weighted%20median%20comes%20from%20NumPy's%20own%20weighted%20percentile%20(%60weights%3D%60%20needs%20%60method%3D%22inverted_cdf%22%60).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_6_field_predictions_crop_7(%0A%20%20%20%20aggregate_features_to_admin%2C%0A%20%20%20%20fields%2C%0A%20%20%20%20gazetteer%2C%0A%20%20%20%20ndvi_noise%2C%0A)%3A%0A%20%20%20%20features%20%3D%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%22field_id%22%3A%20np.arange(len(fields))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22year%22%3A%202025%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22label_name%22%3A%20%22corn%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22label_confidence%22%3A%201.0%2C%20%20%23%20without%20this%20column%20aggregate_features_to_admin%20raises%20(code.md)%0A%20%20%20%20%20%20%20%20%20%20%20%20%22county_key%22%3A%20fields%5B%22county%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22area_ha%22%3A%20fields%5B%22area_ha%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22ndvi_peak%22%3A%200.55%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%200.08%20*%20np.log(fields%5B%22area_ha%22%5D%20%2F%2040)%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20ndvi_noise%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20np.where(fields%5B%22county%22%5D.str.endswith(%22Haskell%22)%2C%200.15%2C%200.0)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20f16%20%3D%20pd.concat(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20aggregate_features_to_admin(features%2C%20None%2C%20lvl%2C%20gazetteer%2C%20per_crop%3DFalse)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20lvl%20in%20(%22adm2%22%2C%20%22adm1%22)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20).set_index(%22admin_key%22)%5B%5B%22n_fields%22%2C%20%22ndvi_peak_wmean%22%2C%20%22ndvi_peak_p50%22%5D%5D%0A%20%20%20%20f16%5B%22unweighted%20median%20(inverted_cdf)%22%5D%20%3D%20%5B%0A%20%20%20%20%20%20%20%20float(%0A%20%20%20%20%20%20%20%20%20%20%20%20np.percentile(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20features.loc%5Bfeatures%5B%22county_key%22%5D.str.startswith(key)%2C%20%22ndvi_peak%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2050%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20method%3D%22inverted_cdf%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20key%20in%20f16.index%0A%20%20%20%20%5D%0A%20%20%20%20f16%5B%22weighted%20median%20(inverted_cdf)%22%5D%20%3D%20%5B%0A%20%20%20%20%20%20%20%20float(%0A%20%20%20%20%20%20%20%20%20%20%20%20np.percentile(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20features.loc%5Bfeatures%5B%22county_key%22%5D.str.startswith(key)%2C%20%22ndvi_peak%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2050%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20weights%3Dfeatures.loc%5Bfeatures%5B%22county_key%22%5D.str.startswith(key)%2C%20%22area_ha%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20method%3D%22inverted_cdf%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20key%20in%20f16.index%0A%20%20%20%20%5D%0A%20%20%20%20print(f%22median%20of%20the%20three%20county%20medians%3A%20%7Bf16%5B'ndvi_peak_p50'%5D.iloc%5B%3A3%5D.median()%3A.4f%7D%22)%0A%20%20%20%20f16.round(4)%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%20Compare%20the%20two%20%60inverted_cdf%60%20columns%20first%3A%20they%20use%20the%20same%20percentile%20method%20and%20differ%20only%20in%20the%20field-area%20weights%2C%20so%20their%20gap%20is%20the%20effect%20of%20weighting%20alone.%20(Production's%20%60p50%60%20uses%20NumPy's%20default%20%60linear%60%20method%3B%20with%20this%20many%20fields%20it%20gives%20the%20same%20values%20as%20%60inverted_cdf%60%2C%20so%20the%20whole%20gap%20is%20the%20weighting.)%20In%20every%20county%20row%20the%20weighted%20median%20is%20within%200.003%20of%20the%20weighted%20mean%2C%20while%20the%20p50%20sits%200.024%20to%200.030%20below%20it.%20So%20the%20gap%20between%20%60wmean%60%20and%20%60p50%60%20here%20is%20not%20skew%20in%20the%20data%3A%20it%20is%20the%20two%20columns%20weighting%20the%20same%20fields%20differently.%20A%20reader%20comparing%20them%20would%20draw%20the%20wrong%20conclusion.%20The%20state%20row%20also%20shows%20why%20roll-ups%20must%20start%20from%20fields%3A%20its%20p50%2C%200.5688%2C%20is%20not%20the%20median%20of%20the%20three%20county%20medians%2C%200.5537.%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%23%203.7%20Phenology%20from%20a%20vegetation-index%20curve%0A%0A%20%20%20%20**Phenology**%20is%20the%20timing%20of%20a%20plant's%20life%3A%20when%20it%20greens%20up%2C%20when%20it%20peaks%2C%20when%20it%20senesces%20(turns%20yellow%20and%20dies%20back).%20The%20parquet%20layer%20reads%20it%20from%20each%20field-year's%20per-scene%20field%20mean%20(3.3)%2C%20for%20NDVI%2C%20EVI2%2C%20GCVI%20and%20NDRE.%20%60_phenology%60%20(%5Bparquet%2Ffeatures.py%3A24-60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fparquet%2Ffeatures.py%23L24-L60))%20sorts%20the%20valid%20samples%20by%20day%20of%20year%20(DOY)%20and%20returns%3A%0A%0A%20%20%20%20-%20the%20**peak**%20(largest%20value)%20and%20its%20DOY%2C%20and%20the%20**amplitude**%20(peak%20minus%20lowest%20value)%3B%0A%20%20%20%20-%20the%20plain%20**mean**%2C%20and%20the%20season%20**integral**%2C%20%60trapz(values%2C%20days)%20%2F%20span%60%3A%20the%20area%20under%20the%20curve%20divided%20by%20the%20span%20of%20observed%20days%2C%20a%20time-weighted%20mean%3B%0A%20%20%20%20-%20**green-up**%3A%20the%20first%20sample%2C%20up%20to%20the%20peak%2C%20at%20or%20above%20half%20the%20amplitude%20(lowest%20value%20%2B%200.5%20%C3%97%20amplitude)%3B%20**senescence**%3A%20the%20last%20sample%20after%20the%20peak%20at%20or%20above%20it.%0A%0A%20%20%20%20%60compute_features%60%20(%5BL63-96%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fparquet%2Ffeatures.py%23L63-L96))%20adds%20the%20mean%20in%20each%20fixed%20%60SEASON_WINDOWS%60%20window%20(%5Bparquet%2Fschema.py%3A52-57%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fparquet%2Fschema.py%23L52-L57))%20and%20counts%20the%20scenes%3A%20%60n_valid_scenes%60%2C%20and%20%60max_gap_days%60%2C%20the%20longest%20wait%20between%20two%20scenes%20with%20any%20valid%20pixel.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_7_phenology_vegetation_index()%3A%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.parquet.features%20import%20_phenology%2C%20compute_features%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.parquet.schema%20import%20CHANNELS%2C%20SEASON_WINDOWS%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.streaming.mds_schema%20import%20VALID_FRACTION_INDEX%0A%0A%20%20%20%20show(_phenology)%0A%20%20%20%20show(compute_features)%0A%20%20%20%20print(%22SEASON_WINDOWS%20%3D%22%2C%20SEASON_WINDOWS)%0A%20%20%20%20return%20CHANNELS%2C%20SEASON_WINDOWS%2C%20VALID_FRACTION_INDEX%2C%20compute_features%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%20A%20synthetic%20season.%20A%20**double-logistic**%20curve%20is%20the%20textbook%20shape%20of%20one%20crop%20season%20(Beck%20et%20al.%202006)%3A%20a%20low%20base%2C%20an%20S-shaped%20rise%2C%20a%20short%20peak%20and%20an%20S-shaped%20fall.%20We%20sample%20it%2C%20with%20a%20little%20noise%20(standard%20deviation%200.015)%2C%20three%20ways%3A%20every%205%20days%20with%20no%20cloud%20(Sentinel-2's%20revisit%20with%20two%20satellites)%3B%20every%2020%20days%3B%20and%20every%2020%20days%20with%20the%20two%20passes%20nearest%20the%20peak%20lost%20to%20cloud.%20Each%20series%20goes%20through%20the%20production%20%60compute_features%60%20as%20a%20field%20time%20series%20with%20only%20the%20NDVI%20mean%20and%20the%20valid%20fraction%20filled%20in.%20The%20%22truth%22%20row%20runs%20the%20same%20%60_phenology%60%20on%20the%20noiseless%20curve%2C%20day%20by%20day.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_7_phenology_vegetation_index_2(%0A%20%20%20%20CHANNELS%2C%0A%20%20%20%20VALID_FRACTION_INDEX%2C%0A%20%20%20%20compute_features%2C%0A)%3A%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.parquet.features%20import%20_phenology%0A%0A%20%20%20%20def%20double_logistic(doy%2C%20base%2C%20top%2C%20rise_doy%2C%20rise_days%2C%20fall_doy%2C%20fall_days)%3A%0A%20%20%20%20%20%20%20%20%22%22%22One%20season's%20NDVI%3A%20base%20%2B%20(top%20-%20base)%20x%20(an%20S-curve%20up%20-%20an%20S-curve%20down).%22%22%22%0A%20%20%20%20%20%20%20%20up%20%3D%201%20%2F%20(1%20%2B%20np.exp(-(doy%20-%20rise_doy)%20%2F%20rise_days))%0A%20%20%20%20%20%20%20%20down%20%3D%201%20%2F%20(1%20%2B%20np.exp(-(doy%20-%20fall_doy)%20%2F%20fall_days))%0A%20%20%20%20%20%20%20%20return%20base%20%2B%20(top%20-%20base)%20*%20(up%20-%20down)%0A%0A%20%20%20%20def%20field_record(doy%2C%20ndvi%2C%20clear)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Build%20a%20(scenes%2C%2089)%20field%20time%20series%20with%20only%20the%20NDVI%20mean%20and%20the%20valid%20fraction%20filled%20in.%22%22%22%0A%20%20%20%20%20%20%20%20ts%20%3D%20np.full((doy.size%2C%20N_INPUT_FEATURES)%2C%20np.nan%2C%20dtype%3Dnp.float32)%0A%20%20%20%20%20%20%20%20ts%5B%3A%2C%20CHANNELS.index(%22ndvi%22)%5D%20%3D%20np.where(clear%2C%20ndvi%2C%20np.nan)%0A%20%20%20%20%20%20%20%20ts%5B%3A%2C%20VALID_FRACTION_INDEX%5D%20%3D%20clear%0A%20%20%20%20%20%20%20%20return%20ts%0A%0A%20%20%20%20SEASON%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22base%22%3A%200.18%2C%0A%20%20%20%20%20%20%20%20%22top%22%3A%200.88%2C%0A%20%20%20%20%20%20%20%20%22rise_doy%22%3A%20160%2C%0A%20%20%20%20%20%20%20%20%22rise_days%22%3A%207%2C%0A%20%20%20%20%20%20%20%20%22fall_doy%22%3A%20215%2C%0A%20%20%20%20%20%20%20%20%22fall_days%22%3A%209%2C%0A%20%20%20%20%7D%0A%20%20%20%20every_day%20%3D%20np.arange(1%2C%20366)%0A%20%20%20%20noise%20%3D%20np.random.default_rng(7)%0A%20%20%20%20doy5%2C%20doy20%20%3D%20np.arange(3%2C%20366%2C%205)%2C%20np.arange(3%2C%20366%2C%2020)%0A%20%20%20%20ndvi5%20%3D%20double_logistic(doy5%2C%20**SEASON)%20%2B%20noise.normal(0%2C%200.015%2C%20doy5.size)%0A%20%20%20%20ndvi20%20%3D%20double_logistic(doy20%2C%20**SEASON)%20%2B%20noise.normal(0%2C%200.015%2C%20doy20.size)%0A%20%20%20%20series%20%3D%20%7B%20%20%23%20name%3A%20(day%20of%20year%2C%20NDVI%2C%20clear%3F)%0A%20%20%20%20%20%20%20%20%22every%205%20days%22%3A%20(doy5%2C%20ndvi5%2C%20np.ones(doy5.size%2C%20bool))%2C%0A%20%20%20%20%20%20%20%20%22every%2020%20days%22%3A%20(doy20%2C%20ndvi20%2C%20np.ones(doy20.size%2C%20bool))%2C%0A%20%20%20%20%20%20%20%20%22every%2020%20days%2C%202%20cloudy%22%3A%20(doy20%2C%20ndvi20%2C%20~np.isin(doy20%2C%20%5B163%2C%20183%5D))%2C%0A%20%20%20%20%7D%0A%20%20%20%20pheno%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22truth%20(daily%2C%20no%20noise)%22%3A%20_phenology(%0A%20%20%20%20%20%20%20%20%20%20%20%20every_day%2C%20double_logistic(every_day%2C%20**SEASON)%2C%20%22ndvi%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%7D%0A%20%20%20%20for%20_name%2C%20(_doy%2C%20_ndvi%2C%20_clear)%20in%20series.items()%3A%0A%20%20%20%20%20%20%20%20pheno%5B_name%5D%20%3D%20compute_features(%0A%20%20%20%20%20%20%20%20%20%20%20%20field_record(_doy%2C%20_ndvi%2C%20_clear)%2C%20_doy%2C%20_clear.astype(np.float32)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20PHENO_COLUMNS%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22ndvi_peak%22%2C%0A%20%20%20%20%20%20%20%20%22ndvi_peak_doy%22%2C%0A%20%20%20%20%20%20%20%20%22ndvi_greenup_doy%22%2C%0A%20%20%20%20%20%20%20%20%22ndvi_senescence_doy%22%2C%0A%20%20%20%20%20%20%20%20%22ndvi_integral%22%2C%0A%20%20%20%20%20%20%20%20%22n_valid_scenes%22%2C%0A%20%20%20%20%20%20%20%20%22max_gap_days%22%2C%0A%20%20%20%20%5D%0A%20%20%20%20pd.DataFrame(pheno).T.reindex(columns%3DPHENO_COLUMNS).round(3)%0A%20%20%20%20return%20SEASON%2C%20double_logistic%2C%20every_day%2C%20pheno%2C%20series%0A%0A%0A%40app.cell%0Adef%20s3_7_phenology_vegetation_index_3(%0A%20%20%20%20SEASON%2C%0A%20%20%20%20double_logistic%2C%0A%20%20%20%20every_day%2C%0A%20%20%20%20pheno%2C%0A%20%20%20%20series%2C%0A)%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(9%2C%204))%0A%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20every_day%2C%20double_logistic(every_day%2C%20**SEASON)%2C%20color%3D%22grey%22%2C%20label%3D%22the%20season%20(no%20noise)%22%0A%20%20%20%20)%0A%20%20%20%20_doy%2C%20_ndvi%2C%20_clear%20%3D%20series%5B%22every%2020%20days%2C%202%20cloudy%22%5D%0A%20%20%20%20_ax.scatter(_doy%5B_clear%5D%2C%20_ndvi%5B_clear%5D%2C%20zorder%3D3%2C%20label%3D%2220-day%20samples%20that%20were%20clear%22)%0A%20%20%20%20_ax.scatter(%0A%20%20%20%20%20%20%20%20_doy%5B~_clear%5D%2C%20_ndvi%5B~_clear%5D%2C%20marker%3D%22x%22%2C%20color%3D%22red%22%2C%20zorder%3D3%2C%20label%3D%22lost%20to%20cloud%22%0A%20%20%20%20)%0A%20%20%20%20for%20_name%2C%20style%20in%20((%22truth%20(daily%2C%20no%20noise)%22%2C%20%22-%22)%2C%20(%22every%2020%20days%2C%202%20cloudy%22%2C%20%22--%22))%3A%0A%20%20%20%20%20%20%20%20_ax.axvline(pheno%5B_name%5D%5B%22ndvi_greenup_doy%22%5D%2C%20linestyle%3Dstyle%2C%20color%3D%22green%22%2C%20alpha%3D0.6)%0A%20%20%20%20_ax.set_xlim(100%2C%20280)%0A%20%20%20%20_ax.set_xlabel(%22day%20of%20year%22)%0A%20%20%20%20_ax.set_ylabel(%22NDVI%22)%0A%20%20%20%20_ax.set_title(%0A%20%20%20%20%20%20%20%20%22Green-up%3A%20the%20true%20day%20(solid)%20and%20the%20day%20read%20from%20the%20cloudy%2020-day%20series%20(dashed)%22%0A%20%20%20%20)%0A%20%20%20%20_ax.legend(loc%3D%22upper%20left%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%20Three%20effects%2C%20one%20per%20column%3A%0A%0A%20%20%20%20-%20**The%20peak.**%20Sampling%20can%20only%20miss%20the%20top%20of%20the%20curve%2C%20which%20pulls%20a%20sampled%20peak%20down%3A%20losing%20one%20pass%20on%20the%20rise%20(day%20163)%20and%20one%20near%20the%20peak%20(day%20183)%20takes%20it%20from%200.837%20to%200.731.%20Noise%20pushes%20the%20other%20way.%20The%20largest%20of%20many%20noisy%20samples%20tends%20to%20sit%20above%20the%20curve%2C%20which%20is%20why%20both%20clear%20series%20read%20a%20peak%20a%20little%20above%20the%20truth%20(0.847%20and%200.841).%0A%20%20%20%20-%20**Green-up%20and%20senescence**%20are%20read%20off%20sample%20dates%2C%20not%20interpolated%20between%20them.%20With%20a%20fixed%20threshold%2C%20the%20first%20sample%20above%20it%20comes%20on%20or%20after%20the%20true%20crossing%2C%20so%20green-up%20is%20late%20by%20up%20to%20one%20gap%2C%20and%20senescence%20is%20early%20in%20the%20same%20way.%20(%60_phenology%60%20sets%20the%20threshold%20at%20half%20the%20*observed*%20amplitude%2C%20so%20if%20sampling%20misses%20the%20peak%20the%20threshold%20falls%20and%20green-up%20can%20come%20early%20instead%3B%20that%20bound%20assumes%20the%20threshold%20is%20unchanged.)%20In%20this%20run%20the%20clear%2020-day%20series%20reads%20green-up%20on%20day%20163%20against%20160%2C%20and%20senescence%20on%20day%20203%20against%20216.%20When%20cloud%20removes%20the%20samples%20around%20the%20rise%2C%20green-up%20moves%20to%20day%20203%2C%2043%20days%20late%3A%20most%20of%20the%2060-day%20gap.%0A%20%20%20%20-%20**%60max_gap_days%60%20is%20the%20warning.**%20It%20is%20the%20only%20column%20that%20tells%20a%20model%20%22the%20dates%20in%20this%20row%20are%20unreliable%22.%20A%20model%20trained%20on%20rows%20like%20these%20can%20learn%20to%20discount%20phenology%20where%20the%20gap%20is%20long%2C%20but%20only%20if%20%60max_gap_days%60%20sits%20beside%20the%20dates.%0A%0A%20%20%20%20The%20windows%20have%20their%20own%20problem%3A%20they%20are%20fixed%20calendar%20days%20of%20year.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_7_phenology_vegetation_index_4(SEASON_WINDOWS)%3A%0A%20%20%20%20windows%20%3D%20%5B%5D%0A%20%20%20%20for%20year%20in%20(2023%2C%202024)%3A%0A%20%20%20%20%20%20%20%20days%20%3D%20pd.date_range(f%22%7Byear%7D-01-01%22%2C%20f%22%7Byear%7D-12-31%22)%0A%20%20%20%20%20%20%20%20for%20_name%2C%20(lo%2C%20hi)%20in%20SEASON_WINDOWS.items()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20inside%20%3D%20days%5B(days.dayofyear%20%3E%3D%20lo)%20%26%20(days.dayofyear%20%3C%20hi)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20windows.append(%0A%20%20%20%20%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%20%20%20%20%22year%22%3A%20year%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22window%22%3A%20_name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22first%20day%22%3A%20inside%5B0%5D.date()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22last%20day%22%3A%20inside%5B-1%5D.date()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20pd.DataFrame(windows).pivot(index%3D%22window%22%2C%20columns%3D%22year%22).reindex(list(SEASON_WINDOWS))%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%20windows%20are%20not%20leap-aware.%20In%202024%20each%20window%20after%20February%20starts%20a%20day%20earlier%20in%20the%20calendar%2C%20so%2031%20March%20is%20%22winter%22%20in%202023%20and%20%22spring%22%20in%202024.%0A%0A%20%20%20%20They%20also%20split%20a%20season%20that%20crosses%20New%20Year.%20The%20bake%20searches%20one%20calendar%20year%20per%20record%20(January%20to%20December%20by%20default%2C%20%5Bstreaming%2Fbake_fold.py%3A387-390%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fstreaming%2Fbake_fold.py%23L387-L390))%2C%20and%20day%20of%20year%20counts%20from%201%20January%20(%5Bparquet%2Fconvert.py%3A37-44%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fparquet%2Fconvert.py%23L37-L44)).%20A%20winter%20wheat%20field%20sown%20in%20October%20and%20harvested%20the%20next%20July%20has%20its%20autumn%20growth%20in%20one%20record's%20%60autumn%60%20window%20and%20its%20spring%20growth%20in%20the%20next%20record's%20%60winter%60%20and%20%60spring%60.%20Its%20green-up%2C%20peak%20and%20senescence%20are%20read%20from%20a%20calendar-year%20curve%20that%20holds%20the%20end%20of%20one%20crop%20and%20the%20start%20of%20the%20next.%20Unit%20%5B2.1%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Ftree%2Fmain%2Facademy%2F2-time-and-signals%2F2.1-the-season-year-calendar)%20teaches%20season%20years%2C%20which%20give%20a%20season%20that%20crosses%20New%20Year%20a%20single%20label.%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%20Part%204.%20Paths%20not%20travelled%2C%20tried%20live%0A%0A%20%20%20%20%23%23%23%204.1%20Outer%20indexing%3A%20the%20blow-up%2C%20kept%20lazy%0A%0A%20%20%20%20Pass%20plain%20arrays%20instead%20of%20DataArrays%20with%20a%20shared%20%60sample%60%20dimension%2C%20and%20xarray%20takes%20every%20row%20%C3%97%20column%20combination.%20**Do%20not%20compute%20this**%3B%20just%20look%20at%20its%20size.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_1_outer_indexing_blow(bounded%2C%20cols%2C%20picked%2C%20rows)%3A%0A%20%20%20%20outer_pick%20%3D%20bounded.isel(y%3Drows%2C%20x%3Dcols)%0A%20%20%20%20print(%22pointwise%3A%22%2C%20dict(picked.sizes)%2C%20f%22%7Bpicked.nbytes%20%2F%201e3%3A.1f%7D%20kB%22)%0A%20%20%20%20print(%22outer%20%20%20%20%3A%22%2C%20dict(outer_pick.sizes)%2C%20f%22%7Bouter_pick.nbytes%20%2F%201e6%3A.1f%7D%20MB%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%23%204.2%20Chunk%20size%3A%20fewer%2C%20bigger%20reads%20or%20more%2C%20smaller%20ones%0A%0A%20%20%20%20Production%20uses%201024%20%C3%97%201024%20chunks.%20Smaller%20Dask%20chunks%20hold%20less%20in%20memory%20but%20mean%20more%20tasks.%20They%20don't%20download%20fewer%20bytes%3A%20GDAL%20always%20fetches%20and%20decompresses%20whole%20internal%20COG%20tiles%2C%20which%20the%20cell%20below%20prints.%20The%2020%20m%20tiles%20cover%20the%20same%20patch%20of%20ground%20as%20the%2010%20m%20ones.%20A%201024-pixel%20chunk%20*can*%20line%20up%20with%20one%20COG%20tile%20in%20every%20band%2C%20which%20avoids%20fetching%20a%20tile%20twice%2C%20but%20only%20when%20the%20output%20grid's%20origin%20also%20falls%20on%20the%20COG's%20tile%20edges.%20stackstac%20anchors%20chunks%20at%20the%20output%20grid's%20origin%2C%20which%20comes%20from%20%60bounds%3D%60%3B%20%60snap_bounds%3DTrue%60%20snaps%20that%20to%20pixels%2C%20not%20to%20tiles.%20A%20chunk%20that%20starts%2010%20columns%20into%20a%20tile%20straddles%20two%20tiles%2C%20and%20its%20neighbour%20fetches%20one%20of%20them%20again.%20Smaller%20chunks%20(256%C2%B2)%20fetch%20the%20same%20tile%20several%20times%20and%20lean%20on%20GDAL's%20caches.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_2_chunk_size_fewer(assets%2C%20three)%3A%0A%20%20%20%20with%20rasterio.Env(**cog_read_env(%22S2%22))%3A%0A%20%20%20%20%20%20%20%20for%20asset%20in%20(%22red%22%2C%20%22nir08%22%2C%20%22scl%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20with%20rasterio.open(three%5B0%5D.assets%5Basset%5D.href)%20as%20_src%3A%0A%20%20%20%20%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%20%20%20%20%20f%22%7Basset%3A6s%7D%3A%20COG%20internal%20tile%20%7B_src.block_shapes%5B0%5D%7D%20at%20%7B_src.res%5B0%5D%3A.0f%7D%20m%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20for%20size%20in%20(256%2C%20512%2C%201024%2C%202048)%3A%0A%20%20%20%20%20%20%20%20_s%20%3D%20stackstac.stack(%0A%20%20%20%20%20%20%20%20%20%20%20%20three%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20assets%3Dassets%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20epsg%3D32615%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20resolution%3D10%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20chunksize%3D(1%2C%20len(assets)%2C%20size%2C%20size)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20rescale%3DFalse%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22chunk%20%7Bsize%3A%3E4%7D%5E2%3A%20%7B_s.data.numblocks%5B2%5D%20*%20_s.data.numblocks%5B3%5D%3A%3E4%7D%20spatial%20chunks%20per%20scene%2C%20%7Btasks(_s)%3A%3E5%7D%20tasks%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Blen(assets)%20*%20size%20*%20size%20*%208%20%2F%201e6%3A6.1f%7D%20MB%20per%20chunk%20in%20memory%20(float64)%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%20%23%23%23%204.3%20The%20dtype%3A%20float64%20by%20default%2C%20uint16%20on%20request%0A%0A%20%20%20%20Sentinel-2%20integers%20fit%20in%2016%20bits.%20Asking%20%60stackstac%60%20for%20%60uint16%60%20makes%20every%20in-flight%20chunk%20four%20times%20smaller.%20Note%20the%20fill%20value%20must%20be%20%60np.uint16(0)%60%3A%20with%20numpy%202%2C%20%60stackstac%60%20rejects%20a%20plain%20%600%60.%0A%0A%20%20%20%20The%20catch%20is%20the%20fill%20value.%20Out-of-footprint%20pixels%20become%200%20instead%20of%20NaN.%20SCL%200%20already%20means%20%22no%20data%22%20and%20is%20screened%20out%2C%20but%20every%20downstream%20test%20for%20NaN%20would%20need%20checking%20first.%20That%20makes%20this%20**a%20behaviour%20change%2C%20not%20a%20refactor**.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_3_dtype_float64_default(kw%2C%20three%2C%20whole)%3A%0A%20%20%20%20%23%20A%20plain%200%20is%20a%20Python%20int%2C%20which%20numpy%202%20will%20not%20cast%20to%20uint16%20here%3B%20the%20fill%20must%20already%20be%20a%20uint16.%0A%20%20%20%20as_uint16%20%3D%20stackstac.stack(three%2C%20dtype%3D%22uint16%22%2C%20fill_value%3Dnp.uint16(0)%2C%20**kw)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22float64%3A%20%7Bwhole.nbytes%20%2F%201e9%3A.2f%7D%20GB%20%7C%20uint16%3A%20%7Bas_uint16.nbytes%20%2F%201e9%3A.2f%7D%20GB%20%7C%20one%2011-asset%201024%5E2%20chunk%3A%20%22%0A%20%20%20%20%20%20%20%20f%22%7B11%20*%201024%20*%201024%20*%208%20%2F%201e6%3A.0f%7D%20MB%20vs%20%7B11%20*%201024%20*%201024%20*%202%20%2F%201e6%3A.0f%7D%20MB%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%20%23%23%23%204.4%20No%20Dask%20at%20all%3A%20rasterio%20point%20sampling%0A%0A%20%20%20%20%5B%60rasterio.sample.sample_gen%60%5D(https%3A%2F%2Frasterio.readthedocs.io%2Fen%2Fstable%2Fapi%2Frasterio.sample.html)%20reads%20values%20at%20points%20straight%20from%20a%20COG%2C%20touching%20only%20the%20internal%20tiles%20that%20contain%20them.%20There's%20no%20graph%2C%20no%20chunks%20and%20no%20free%20parallelism%3A%20you%20write%20the%20loop%20over%20scenes%20and%20bands%20yourself.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_4_no_dask_all(bounds%2C%20field_points%2C%20three%2C%20xy_to_indices)%3A%0A%20%20%20%20%23%20Two%20bands%20nothing%20earlier%20in%20the%20notebook%20has%20read%2C%20so%20neither%20timing%20is%20flattered%20by%20GDAL's%20cache.%0A%20%20%20%20scene%20%3D%20three%5B1%5D%0A%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20with%20rasterio.Env(**cog_read_env(%22S2%22))%2C%20rasterio.open(scene.assets%5B%22nir08%22%5D.href)%20as%20_src%3A%0A%20%20%20%20%20%20%20%20print(%22COG%20internal%20tile%20size%3A%22%2C%20_src.block_shapes%5B0%5D%2C%20%22%7C%20CRS%3A%22%2C%20_src.crs)%0A%20%20%20%20%20%20%20%20_%20%3D%20np.array(%5Bv%5B0%5D%20for%20v%20in%20sample_gen(_src%2C%20field_points)%5D)%0A%20%20%20%20t_rasterio%20%3D%20time.perf_counter()%20-%20_t0%0A%0A%20%20%20%20one_band%20%3D%20stackstac.stack(%0A%20%20%20%20%20%20%20%20%5Bscene%5D%2C%0A%20%20%20%20%20%20%20%20assets%3D%5B%22swir16%22%5D%2C%0A%20%20%20%20%20%20%20%20epsg%3D32615%2C%0A%20%20%20%20%20%20%20%20resolution%3D10%2C%0A%20%20%20%20%20%20%20%20bounds%3Dbounds%2C%0A%20%20%20%20%20%20%20%20rescale%3DFalse%2C%0A%20%20%20%20%20%20%20%20gdal_env%3Dbuild_gdal_env(%22S2%22)%2C%0A%20%20%20%20)%0A%20%20%20%20r1%2C%20c1%2C%20_%20%3D%20xy_to_indices(field_points%2C%20one_band.x.values%2C%20one_band.y.values%2C%2010)%0A%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20_%20%3D%20one_band.isel(%0A%20%20%20%20%20%20%20%20time%3D0%2C%20band%3D0%2C%20y%3Dxr.DataArray(r1%2C%20dims%3D%22sample%22)%2C%20x%3Dxr.DataArray(c1%2C%20dims%3D%22sample%22)%0A%20%20%20%20).values%0A%20%20%20%20t_stackstac%20%3D%20time.perf_counter()%20-%20_t0%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22cold%20read%2C%20one%20scene%2C%20one%20band%2C%20900%20points%20(laptop)%3A%20rasterio%20sample_gen%20%7Bt_rasterio%3A.1f%7Ds%20%7C%20stackstac%20%7Bt_stackstac%3A.1f%7Ds%22%0A%20%20%20%20)%0A%20%20%20%20return%20(scene%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%20For%20one%20scene%20and%20one%20band%20the%20two%20are%20in%20the%20same%20range.%20The%20difference%20shows%20at%20scale.%20%60stackstac%60%20gives%20you%20a%20whole%20season%20%C3%97%2011%20bands%20as%20one%20array%2C%20with%20Dask%20fetching%20chunks%20in%20parallel.%20With%20%60sample_gen%60%20you%20would%20write%20and%20parallelise%20that%20loop%20yourself.%0A%0A%20%20%20%20%23%23%23%204.4b%20A%20live%20finding%3A%20do%20both%20approaches%20read%20the%20same%20pixel%3F%0A%0A%20%20%20%20They%20should%2C%20but%20they%20don't.%20%60rasterio.sample%60%20returns%20the%20value%20of%20the%20pixel%20that%20*contains*%20each%20point.%20Here%20we%20compare%20it%20with%20the%20production%20indices%20on%20the%20same%20band%2C%20for%20two%20sets%20of%20points%3A%20production's%20case%2C%20the%20field's%20exact%20pixel%20centres%2C%20and%20random%20points%20inside%20the%20field.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_4b_live_finding_both(%0A%20%20%20%20FIELD_X0%2C%0A%20%20%20%20FIELD_Y0%2C%0A%20%20%20%20compute_bounds%2C%0A%20%20%20%20field_points%2C%0A%20%20%20%20scene%2C%0A%20%20%20%20xy_to_indices%2C%0A)%3A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(0)%0A%20%20%20%20random_points%20%3D%20np.column_stack(%0A%20%20%20%20%20%20%20%20%5BFIELD_X0%20%2B%20_rng.uniform(0%2C%20300%2C%20900)%2C%20FIELD_Y0%20-%20_rng.uniform(0%2C%20300%2C%20900)%5D%0A%20%20%20%20)%0A%20%20%20%20red_stack%20%3D%20stackstac.stack(%0A%20%20%20%20%20%20%20%20%5Bscene%5D%2C%0A%20%20%20%20%20%20%20%20assets%3D%5B%22red%22%5D%2C%0A%20%20%20%20%20%20%20%20epsg%3D32615%2C%0A%20%20%20%20%20%20%20%20resolution%3D10%2C%0A%20%20%20%20%20%20%20%20bounds%3Dcompute_bounds(random_points%2C%20100.0)%2C%0A%20%20%20%20%20%20%20%20rescale%3DFalse%2C%0A%20%20%20%20)%0A%20%20%20%20with%20rasterio.Env(**cog_read_env(%22S2%22))%2C%20rasterio.open(scene.assets%5B%22red%22%5D.href)%20as%20_src%3A%0A%20%20%20%20%20%20%20%20truth%20%3D%20np.array(%5Bv%5B0%5D%20for%20v%20in%20sample_gen(_src%2C%20random_points)%5D)%0A%0A%20%20%20%20def%20red_at(stack%2C%20rows%2C%20cols)%3A%0A%20%20%20%20%20%20%20%20return%20stack.isel(%0A%20%20%20%20%20%20%20%20%20%20%20%20time%3D0%2C%20band%3D0%2C%20y%3Dxr.DataArray(rows%2C%20dims%3D%22sample%22)%2C%20x%3Dxr.DataArray(cols%2C%20dims%3D%22sample%22)%0A%20%20%20%20%20%20%20%20).values%0A%0A%20%20%20%20prod_r%2C%20prod_c%2C%20_%20%3D%20xy_to_indices(random_points%2C%20red_stack.x.values%2C%20red_stack.y.values%2C%2010)%0A%20%20%20%20floor_c%20%3D%20np.floor((random_points%5B%3A%2C%200%5D%20-%20red_stack.x.values%5B0%5D)%20%2F%2010).astype(int)%0A%20%20%20%20floor_r%20%3D%20np.floor((red_stack.y.values%5B0%5D%20-%20random_points%5B%3A%2C%201%5D)%20%2F%2010).astype(int)%0A%20%20%20%20centre_stack%20%3D%20stackstac.stack(%0A%20%20%20%20%20%20%20%20%5Bscene%5D%2C%0A%20%20%20%20%20%20%20%20assets%3D%5B%22red%22%5D%2C%0A%20%20%20%20%20%20%20%20epsg%3D32615%2C%0A%20%20%20%20%20%20%20%20resolution%3D10%2C%0A%20%20%20%20%20%20%20%20bounds%3Dcompute_bounds(random_points%2C%20100.0)%2C%0A%20%20%20%20%20%20%20%20rescale%3DFalse%2C%0A%20%20%20%20%20%20%20%20xy_coords%3D%22center%22%2C%0A%20%20%20%20)%0A%20%20%20%20cen_r%2C%20cen_c%2C%20_%20%3D%20xy_to_indices(random_points%2C%20centre_stack.x.values%2C%20centre_stack.y.values%2C%2010)%0A%0A%20%20%20%20%23%20Production's%20case%3A%20exact%20pixel%20centres%20(field_points)%2C%20the%20way%20enumerate_interior_pixels%20builds%20them.%0A%20%20%20%20fp_r%2C%20fp_c%2C%20_%20%3D%20xy_to_indices(field_points%2C%20red_stack.x.values%2C%20red_stack.y.values%2C%2010)%0A%20%20%20%20true_c%20%3D%20np.floor((field_points%5B%3A%2C%200%5D%20-%20red_stack.x.values%5B0%5D)%20%2F%2010).astype(int)%0A%20%20%20%20true_r%20%3D%20np.floor((red_stack.y.values%5B0%5D%20-%20field_points%5B%3A%2C%201%5D)%20%2F%2010).astype(int)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22pixel%20centres%3A%20production%20indices%20right%20for%20%7Bnp.mean((fp_r%20%3D%3D%20true_r)%20%26%20(fp_c%20%3D%3D%20true_c))%3A.0%25%7D%20of%20900%20points%3B%20%22%0A%20%20%20%20%20%20%20%20f%22%7Blen(np.unique(np.column_stack(%5Bfp_r%2C%20fp_c%5D)%2C%20axis%3D0))%7D%20distinct%20pixels%20read%2C%20%22%0A%20%20%20%20%20%20%20%20f%22of%20%7Blen(np.unique(np.column_stack(%5Btrue_r%2C%20true_c%5D)%2C%20axis%3D0))%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(%22random%20points%3A%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22stackstac%20labels%20pixels%20by%20their%3A%22%2C%0A%20%20%20%20%20%20%20%20inspect.signature(stackstac.stack).parameters%5B%22xy_coords%22%5D.default%2C%0A%20%20%20%20%20%20%20%20%22corner%22%2C%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22production%20xy_to_indices%20(round%20on%20top-left%20labels)%3A%20%7Bnp.mean(red_at(red_stack%2C%20prod_r%2C%20prod_c)%20%3D%3D%20truth)%3A.0%25%7D%20match%20rasterio%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22floor%20on%20top-left%20labels%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%3A%20%7Bnp.mean(red_at(red_stack%2C%20floor_r%2C%20floor_c)%20%3D%3D%20truth)%3A.0%25%7D%20match%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22production%20xy_to_indices%20on%20xy_coords%3D'center'%20%20%20%20%20%20%3A%20%7Bnp.mean(red_at(centre_stack%2C%20cen_r%2C%20cen_c)%20%3D%3D%20truth)%3A.0%25%7D%20match%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22pixel%20centres%2010.5%2C%2011.5%2C%2012.5%20px%20from%20the%20corner%20round%20to%3A%22%2C%0A%20%20%20%20%20%20%20%20np.round(%5B10.5%2C%2011.5%2C%2012.5%5D).astype(int)%2C%0A%20%20%20%20%20%20%20%20%22(banker's%20rounding)%22%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%20%60stackstac%60%20labels%20each%20pixel%20by%20its%20**top-left%20corner**%20(%60xy_coords%3D%22topleft%22%60%20is%20its%20default).%20%60xy_to_indices%60%20uses%20%60np.round((x%20-%20x0)%20%2F%20resolution)%60%2C%20which%20is%20correct%20only%20if%20the%20labels%20are%20pixel%20**centres**.%20On%20corner%20labels%2C%20a%20point%20in%20the%20right%20or%20lower%20half%20of%20its%20pixel%20is%20rounded%20to%20the%20next%20pixel%20along.%0A%0A%20%20%20%20Production's%20points%20are%20exact%20pixel%20centres.%20%60enumerate_interior_pixels%60%20(%5Bexpand_field_list.py%3A29-53%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fstreaming%2Fexpand_field_list.py%23L29-L53))%20builds%20each%20one%20at%20%60(floor(x%20%2F%2010)%20%2B%200.5)%20%C3%97%2010%60%2C%20and%20%60snap_bounds%3DTrue%60%20puts%20%60x0%60%20on%20a%20multiple%20of%2010.%20So%20%60(x%20-%20x0)%20%2F%2010%60%20is%20always%20a%20tie%2C%20%60k%20%2B%200.5%60%2C%20and%20numpy%20rounds%20ties%20to%20the%20even%20number.%20The%20error%20is%20deterministic%3A%20only%20points%20whose%20column%20and%20row%20are%20both%20even%20read%20the%20right%20pixel%2C%20odd%20columns%20and%20rows%20are%20never%20read%2C%20and%20each%20pixel%20that%20is%20read%20stands%20in%20for%20up%20to%20four.%20The%20first%20line%20of%20the%20output%20shows%20it%3A%2025%20%25%2C%20and%20256%20distinct%20pixels%20of%20900.%20Random%20points%20fare%20about%20the%20same%2C%20because%20each%20axis%20is%20wrong%20half%20the%20time%20(26%20%25%20here%3A%20a%20few%20wrong%20picks%20match%20by%20chance%2C%20when%20neighbours%20share%20a%20value).%0A%0A%20%20%20%20Each%20wrong%20pick%20is%20at%20most%20one%20pixel%20(10%20m)%20out%2C%20and%20the%20bias%20is%20systematic%2C%20towards%20the%20east%20and%20south.%20Inside%20a%20field%20that%20mostly%20lands%20on%20similar%20crop%3A%20re-extracting%20this%20season%20with%20%60floor%60%20(Exercise%206)%20moves%20the%20square's%20NDVI%20mean%20by%20at%20most%20about%200.01%20on%20any%20date.%20Near%20a%20field%20edge%2C%20or%20in%20a%20narrow%20field%2C%20it%20can%20reach%20the%20next%20field%20or%20a%20road%3B%20that%20is%20not%20yet%20measured.%0A%0A%20%20%20%20Two%20one-line%20fixes%20both%20give%20100%20%25%3A%20%60floor%60%20instead%20of%20%60round%60%2C%20or%20%60xy_coords%3D%22center%22%60%20in%20the%20stack%20call.%20**Either%20fix%20changes%20the%20extracted%20values%2C%20so%20it%20is%20a%20bug%20fix%2C%20not%20a%20refactor**%2C%20and%20a%20bake%20made%20before%20the%20fix%20would%20differ%20from%20one%20made%20after.%20This%20is%20finding%20F1%2C%20logged%20as%20%5Btf-model-lab%20issue%2028%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F28).%0A%0A%20%20%20%20%23%23%23%204.5%20odc-stac%0A%0A%20%20%20%20%5Bodc-stac%5D(https%3A%2F%2Fodc-stac.readthedocs.io%2Fen%2Flatest%2F)%20(%60odc.stac.load%60)%20builds%20a%20similar%20lazy%20array%20with%20its%20own%20loader.%20model-foundry's%20%60satellite_downscaling%2Ffetchers.py%60%20uses%20it%2C%20so%20the%20organisation%20runs%20both%20libraries.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_5_odc_stac()%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20import%20odc.stac%0A%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22odc-stac%20is%20installed%3A%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20odc.stac.__version__%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22-%20try%20odc.stac.load(three%2C%20bands%3Dassets%2C%20crs%3D'EPSG%3A32615'%2C%20resolution%3D10%2C%20bbox%3D...)%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20except%20ImportError%3A%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22odc-stac%20is%20not%20installed%20in%20this%20environment%3B%20%60uv%20add%20odc-stac%60%20to%20compare%20it%20with%20stackstac%20on%20the%20same%20items.%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%20%7C%20Approach%20%7C%20Strength%20%7C%20Weakness%20%7C%0A%20%20%20%20%7C---%7C---%7C---%7C%0A%20%20%20%20%7C%20%60stackstac%60%20%2B%20pointwise%20%60isel%60%20(production)%20%7C%20Memory%20scales%20with%20the%20pixels%20requested%3B%20parallel%20fetches%20for%20free%3B%20one%20code%20path%20for%20S1%20and%20S2%20%7C%20GDAL%20reads%20whole%20COG%20tiles%20(1024%C2%B2%20at%2010%20m)%3B%20float64%20by%20default%20%7C%0A%20%20%20%20%7C%20%60odc-stac%60%20%7C%20Similar%20laziness%3B%20richer%20resampling%20and%20grouping%20options%20%7C%20A%20second%20library%20to%20know%3B%20different%20chunking%20defaults%20%7C%0A%20%20%20%20%7C%20%60rasterio.sample%60%20%7C%20Minimal%3A%20reads%20only%20the%20COG%20tiles%20that%20hold%20points%20%7C%20You%20write%20the%20scene%20%C3%97%20band%20loop%20and%20the%20parallelism%20%7C%0A%20%20%20%20%7C%20%60dtype%3D%22uint16%22%60%20%7C%204%C3%97%20less%20memory%20per%20chunk%20%7C%20NaN%20becomes%200%3A%20a%20behaviour%20change%20%7C%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%23%204.6%20Area%20from%20a%20reference%20sample%2C%20not%20from%20the%20map%0A%0A%20%20%20%20Section%203.6%20trusted%20the%20classifier's%20probabilities.%20The%20standard%20alternative%20in%20remote%20sensing%20trusts%20neither%20the%20map%20nor%20its%20probabilities%20(Olofsson%20et%20al.%202014).%20Label%20a%20random%20sample%20of%20fields%20with%20their%20true%20crop%2C%20on%20the%20ground%20or%20from%20better%20imagery%2C%20and%20use%20the%20map%20only%20to%20**stratify**%20the%20sample%3A%20one%20stratum%20for%20the%20fields%20mapped%20as%20sorghum%2C%20one%20for%20the%20rest.%20In%20each%20stratum%20h%2C%20estimate%20the%20share%20of%20area%20that%20is%20really%20sorghum%20from%20the%20sample%2C%20then%20scale%20it%20by%20the%20stratum's%20total%20area%3A%0A%0A%20%20%20%20%C3%82%20%3D%20%CE%A3_h%20A_h%20%C3%97%20(%CE%A3_sample%20area%20%C3%97%20is_sorghum)%20%2F%20(%CE%A3_sample%20area)%0A%0A%20%20%20%20This%20is%20a%20ratio%20estimator%3A%20approximately%20unbiased%20whatever%20the%20classifier's%20calibration%2C%20and%20it%20comes%20with%20a%20standard%20error.%20%22Approximately%22%20matters%3A%20its%20denominator%20is%20itself%20random%2C%20so%20with%20a%20small%20sample%20it%20can%20be%20biased%20(one%201%20ha%20sorghum%20field%20and%20one%209%20ha%20other%20field%2C%20sampled%20one%20at%20a%20time%2C%20average%205%20ha%20against%20a%20truth%20of%201%20ha).%20That%20is%20sampling%20bias%2C%20separate%20from%20a%20classifier's%20miscalibration%2C%20and%20it%20shrinks%20as%20the%20sample%20grows.%20Its%20cost%20is%20the%20labelled%20sample.%20%5BUnit%200.3%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F0-foundations%2F0.3-evaluation-and-uncertainty%2Fscience.md%23assessing-a-map)%20covers%20assessing%20a%20map%2C%20including%20the%20stratified%20estimator%20by%20Olofsson%20et%20al.%20Here%20it%20is%20on%20Finney%2C%20with%20up%20to%2050%20labelled%20fields%20per%20stratum%2C%20repeated%20500%20times%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_6_area_reference_sample(fields)%3A%0A%20%20%20%20finney%20%3D%20fields%5Bfields%5B%22county%22%5D.str.endswith(%22Finney%22)%5D%0A%20%20%20%20mapped_sorghum%20%3D%20(finney%5B%22p%22%5D%20%3E%3D%200.5).to_numpy()%0A%20%20%20%20draws%20%3D%20np.random.default_rng(3)%0A%20%20%20%20estimates%20%3D%20%5B%5D%0A%20%20%20%20for%20_%20in%20range(500)%3A%0A%20%20%20%20%20%20%20%20_total%20%3D%200.0%0A%20%20%20%20%20%20%20%20for%20stratum%20in%20(mapped_sorghum%2C%20~mapped_sorghum)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20members%20%3D%20finney%5Bstratum%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20sample%20%3D%20members.sample(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20n%3Dmin(50%2C%20len(members))%2C%20random_state%3Dint(draws.integers(2**31))%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_total%20%2B%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20members%5B%22area_ha%22%5D.sum()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20*%20(sample%5B%22area_ha%22%5D%20*%20sample%5B%22is_sorghum%22%5D).sum()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%20sample%5B%22area_ha%22%5D.sum()%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20estimates.append(_total)%0A%20%20%20%20estimates%20%3D%20np.array(estimates)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22mapped-sorghum%20stratum%3A%20%7Bmapped_sorghum.sum()%7D%20fields%3B%20mapped-other%20stratum%3A%20%7B(~mapped_sorghum).sum()%7D%20fields%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22truth%20%7Bfinney.loc%5Bfinney%5B'is_sorghum'%5D%2C%20'area_ha'%5D.sum()%3A%2C.0f%7D%20ha%20%7C%20hard%20%7Bfinney.loc%5Bmapped_sorghum%2C%20'area_ha'%5D.sum()%3A%2C.0f%7D%20ha%22%0A%20%20%20%20%20%20%20%20f%22%20%7C%20soft%20%7B(finney%5B'area_ha'%5D%20*%20finney%5B'p'%5D).sum()%3A%2C.0f%7D%20ha%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22sample-based%3A%20mean%20%7Bestimates.mean()%3A%2C.0f%7D%20ha%2C%20standard%20deviation%20%7Bestimates.std()%3A%2C.0f%7D%20ha%20over%20500%20samples%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%20Averaged%20over%20500%20samples%2C%20the%20sample-based%20estimate%20lands%20close%20to%20the%20truth%20(4%2C943%20ha%20against%204%2C991)%20without%20using%20the%20probabilities%20at%20all%2C%20so%20miscalibration%20can't%20bias%20it.%20Any%20single%20sample%2C%20though%2C%20is%20noisy%3A%20across%20the%20500%20repeats%20the%20estimates%20have%20an%20empirical%20standard%20deviation%20of%203%2C497%20ha%20(the%20spread%20of%20the%20simulation%2C%20not%20a%20standard%20error%20estimated%20from%20one%20labelled%20sample)%2C%20because%20sorghum%20is%20rare%20in%20the%20big%20%22other%22%20stratum%2C%20and%2050%20fields%20find%20only%20a%20handful%20of%20it.%20Production's%20soft%20sum%20is%20far%20more%20precise%20*if*%20the%20probabilities%20are%20calibrated%3B%20a%20reference%20sample%20is%20how%20you%20would%20check%20that%20they%20are.%0A%0A%20%20%20%20%23%23%23%204.7%20Fit%20a%20curve%20before%20reading%20the%20dates%0A%0A%20%20%20%20Production%20reads%20phenology%20straight%20off%20the%20samples.%20TIMESAT%20(J%C3%B6nsson%20and%20Eklundh%202004)%20and%20much%20of%20the%20literature%20instead%20fit%20a%20smooth%20curve%2C%20often%20the%20double%20logistic%20itself%2C%20and%20read%20the%20dates%20from%20the%20fitted%20curve.%20That%20interpolates%20between%20samples%20and%20averages%20out%20noise%2C%20at%20the%20price%20of%20assuming%20the%20shape%20(a%20double%20crop%20or%20a%20replanted%20field%20breaks%20it)%20and%20of%20a%20fit%20that%20can%20fail.%20Here%20is%20%5B%60scipy.optimize.curve_fit%60%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Fgenerated%2Fscipy.optimize.curve_fit.html)%20on%20the%20three%20samplings%20from%203.7%2C%20with%20the%20dates%20read%20by%20the%20same%20%60_phenology%60%20from%20the%20fitted%20curve%2C%20day%20by%20day%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_7_fit_curve_before(double_logistic%2C%20every_day%2C%20pheno%2C%20series)%3A%0A%20%20%20%20from%20scipy.optimize%20import%20curve_fit%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.parquet.features%20import%20_phenology%0A%0A%20%20%20%20fitted%20%3D%20%7B%22truth%20(daily%2C%20no%20noise)%22%3A%20pheno%5B%22truth%20(daily%2C%20no%20noise)%22%5D%7D%0A%20%20%20%20for%20_name%2C%20(_doy%2C%20_ndvi%2C%20_clear)%20in%20series.items()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20params%2C%20_%20%3D%20curve_fit(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20double_logistic%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_doy%5B_clear%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_ndvi%5B_clear%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20p0%3D%5B0.2%2C%200.8%2C%20140%2C%2010%2C%20260%2C%2010%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20maxfev%3D20_000%2C%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%20fitted%5B_name%5D%20%3D%20_phenology(every_day%2C%20double_logistic(every_day%2C%20*params)%2C%20%22ndvi%22)%0A%20%20%20%20%20%20%20%20except%20RuntimeError%20as%20exc%3A%20%20%23%20curve_fit%20raises%20when%20it%20can't%20converge%0A%20%20%20%20%20%20%20%20%20%20%20%20fitted%5B_name%5D%20%3D%20%7B%22error%22%3A%20str(exc)%7D%0A%20%20%20%20fit_table%20%3D%20pd.DataFrame(fitted).T%0A%20%20%20%20shown%20%3D%20%5B%22ndvi_peak%22%2C%20%22ndvi_peak_doy%22%2C%20%22ndvi_greenup_doy%22%2C%20%22ndvi_senescence_doy%22%5D%0A%20%20%20%20if%20%22error%22%20in%20fit_table%3A%20%20%23%20a%20failed%20fit%20keeps%20its%20message%20instead%20of%20a%20row%20of%20blanks%0A%20%20%20%20%20%20%20%20shown.append(%22error%22)%0A%20%20%20%20fit_table.reindex(columns%3Dshown).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%20With%20clear%20samples%20the%20fit%20recovers%20the%20season%20almost%20exactly%2C%20even%20every%2020%20days.%20With%20the%20two%20passes%20at%20the%20top%20lost%20to%20cloud%2C%20it%20can't%3A%20no%20sample%20says%20how%20high%20the%20peak%20went%2C%20so%20the%20fitted%20curve%20is%20only%20one%20of%20many%20that%20pass%20through%20the%20remaining%20points.%20A%20curve%20narrows%20a%20gap%20but%20can't%20fill%20it%2C%20which%20is%20why%20%60max_gap_days%60%20matters%20whichever%20way%20the%20dates%20are%20read.%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%20Part%205.%20Exercises%0A%0A%20%20%20%20Each%20one%20changes%20a%20single%20thing.%20**Predict%20the%20result%20before%20you%20run%20it.**%0A%0A%20%20%20%201.%20In%20a%20new%20cell%20after%202.6%2C%20index%20%60bounded%60%20with%20%60rows%60%20and%20%60cols%60%20directly%20(%60bounded.isel(y%3Drows%2C%20x%3Dcols)%60)%2C%20without%20%60xr.DataArray(...%2C%20dims%3D%22sample%22)%60%2C%20and%20print%20its%20%60.sizes%60%20and%20%60.nbytes%60%20without%20computing%20it.%20(Changing%202.6%20itself%20would%20make%20marimo%20re-run%20the%20cell%20that%20computes%20%60picked%60.)%20What%20shape%20comes%20back%3F%20How%20many%20bytes%20would%20it%20be%20for%20a%20real%20bake%20with%205%2C000%20pixels%3F%20**Expected%3A**%20%60(3%2C%203%2C%20900%2C%20900)%60%2C%2058.3%20MB%2C%20as%20in%204.1.%20At%205%2C000%20points%20that%20is%205%2C000%C2%B2%20%C3%97%203%20%C3%97%203%20%C3%97%208%20B%20%E2%89%88%201.8%20GB%20for%20these%20three%20scenes%20and%20three%20assets%2C%20or%20%E2%89%88%2066%20GB%20for%2030%20scenes%20%C3%97%2011%20assets.%0A%20%20%20%202.%20In%202.7%2C%20use%20%60max_scenes_per_load%3D4%60.%20How%20many%20batches%20run%3F%20Does%20the%20result%20change%3F%20Does%20the%20time%3F%20**Expected%3A**%203%20batches%20and%20identical%20reflectances.%20Look%20at%20%60platform%60%20too%3A%20now%20only%20the%20batch%20that%20lost%20a%20scene%20to%20%60bounds%3D%60%20is%20blank.%0A%20%20%20%203.%20Switch%20the%202.7%20call%20to%20Earth%20Search's%20other%20collection%3A%20search%20with%20%60S2StacClient(source%3D%22aws%22%2C%20collection%3D%22sentinel-2-l2a%22)%60%2C%20then%20call%20%60extract_tile_samples%60%20with%20%60collection%3D%22sentinel-2-l2a%22%60%20and%20%60offset_mode%3D%22always%22%60.%20Which%20channels%20in%20the%20field%20time%20series%20move%2C%20and%20by%20how%20much%3F%20**Expected%3A**%20every%20band%20falls%20by%20exactly%200.1%20against%20the%20default%20mode%2C%20and%20NDVI%20goes%20above%201%20from%20July%20onwards.%0A%20%20%20%204.%20Add%20SCL%20class%207%20(unclassified)%20to%20the%20bad%20codes%3A%20%60compute_obs_valid(scl%2C%20bad_codes%3DSCL_BAD_CODES%20%2B%20%5B7%5D)%60.%20Which%20scenes%20change%3F%20**Expected%3A**%20none%20on%20this%20run%2C%20because%20class%207%20never%20occurs%20in%20the%20square%20(see%20the%203.2%20counts).%0A%20%20%20%205.%20Move%20the%20field%203%20km%20east%20(%60FIELD_X0%20%2B%3D%203000%60).%20Is%20it%20still%20cropland%3F%20How%20does%20the%20season%20curve%20change%3F%20**Check%3A**%20the%20SCL%20counts%20and%20the%20curve's%20shape%3B%20a%20summer%20crop%20rises%20from%20bare%20soil%20in%20May%20to%20a%20full%20canopy%20in%20July.%0A%20%20%20%206.%20Using%20what%20you%20found%20in%204.4b%2C%20re-extract%20the%20season%20with%20a%20%60floor%60%20indexer.%20Write%20%60floor_indexer%60%20with%20the%20same%20arguments%20and%20return%20values%20as%20%60xy_to_indices%60%2C%20but%20%60np.floor%60%20in%20place%20of%20%60np.round%60%3B%20then%2C%20in%20one%20new%20cell%2C%20set%20%60extract._xy_to_indices%20%3D%20floor_indexer%60%2C%20call%20%60extract_tile_samples%60%20as%202.7%20does%2C%20and%20restore%20the%20original%20in%20a%20%60finally%3A%60%20block%2C%20so%20the%20patch%20can't%20outlive%20the%20cell.%20Compare%20its%20result%20with%202.7's%20%60ds%60.%20How%20much%20does%20the%20field's%20mean%20NDVI%20move%20on%20each%20date%3F%20Would%20that%20change%20a%20crop%20classifier's%20decision%3F%20**Expected%3A**%20at%20most%20about%200.01%20on%20this%20square%2C%20largest%20on%2024%20June%2C%20when%20the%20field%20edge%20in%20the%20square%20is%20sharpest.%0A%20%20%20%207.%20In%203.5%2C%20add%20points%20at%2031.99%C2%B0N%20and%2032.01%C2%B0N%2C%20both%20at%2083.6%C2%B0W.%20Predict%20each%20one's%20MGRS%20designation%20and%20resolved%20tile%20before%20you%20run%20it.%20Then%20delete%2017SKR%20from%20%60TILE_TOP_LEFT%60%20and%20run%203.5%20again%3A%20what%20happens%20to%20the%20points%20the%20report%20called%20phantom%3F%20**Expected%3A**%2031.99%C2%B0N%20is%20designated%2017RKR%20and%20resolved%20to%2017SKR%3B%2032.01%C2%B0N%20is%2017SKR%20both%20ways.%20Without%2017SKR's%20footprint%2C%20the%20three%20points%20north%20of%20the%20overlap%20join%20the%20western%20point%20as%20unresolved%2C%20and%20so%20do%20your%20two%20added%20points%2C%20which%20lie%20north%20of%2017RKQ's%20top%20edge%20(about%2031.6%C2%B0N)%3A%206%20in%20all%2C%20and%20the%20report%20counts%20no%20phantoms%2C%20because%20it%20only%20checks%20the%20points%20it%20could%20resolve.%20A%20missing%20footprint%20hides%20the%20bug%20rather%20than%20revealing%20it.%0A%20%20%20%208.%20In%203.6%2C%20change%20Finney's%20sorghum%20share%20from%200.04%20to%200.5%2C%20then%20to%200.9.%20When%20is%20the%20hard%20area%20too%20low%20and%20when%20too%20high%3F%20Then%2C%20with%20Finney%20back%20at%200.04%2C%20compute%20the%20probabilities%20with%20one%20prevalence%20for%20the%20whole%20state%20(%60calibrated_probability(score%2C%200.095)%60%2C%20close%20to%20the%20state's%20share)%20instead%20of%20each%20county's%20own.%20Which%20counties'%20soft%20areas%20are%20now%20wrong%2C%20and%20which%20way%3F%20**Expected%3A**%20at%200.5%20the%20hard%20area%20is%20close%20(72%2C318%20ha%20against%2072%2C995)%3B%20at%200.9%20it%20is%20too%20high%20(134%2C007%20against%20129%2C178)%2C%20because%20with%20a%20common%20crop%20the%20false%20alarms%20outnumber%20the%20misses.%20With%20one%20prevalence%20for%20the%20state%2C%20Finney's%20soft%20area%20doubles%20(10%2C065%20ha%20against%204%2C991)%20and%20Haskell's%20falls%20to%208%2C567%20against%2013%2C118%3A%20probabilities%20calibrated%20for%20the%20average%20are%20wrong%20for%20every%20county%20that%20isn't%20average.%0A%20%20%20%209.%20In%203.7%2C%20move%20the%20two%20cloudy%20passes%20from%20%60%5B163%2C%20183%5D%60%20to%20%60%5B143%2C%20163%5D%60%2C%20then%20to%20%60%5B203%2C%20223%5D%60.%20Which%20phenology%20dates%20move%2C%20and%20does%20%60max_gap_days%60%20flag%20each%20case%3F%20**Expected%3A**%20with%20%60%5B143%2C%20163%5D%60%20green-up%20moves%20to%20day%20183%20(truth%20160)%20and%20the%20peak%20doesn't%20change%3B%20with%20%60%5B203%2C%20223%5D%60%20senescence%20moves%20to%20day%20183%20(truth%20216).%20%60max_gap_days%60%20is%2060%20in%20both%3A%20the%20same%20warning%2C%20whichever%20date%20the%20gap%20spoils.%0A%0A%20%20%20%20%23%23%20Sources%0A%0A%20%20%20%20Pinned%20to%20the%20locked%20versions%20where%20a%20versioned%20page%20exists%3B%20Dask%2C%20odc-stac%20and%20rasterio%20have%20none%20at%20the%20obvious%20paths.%0A%0A%20%20%20%20-%20pystac-client%200.9.1%3A%20https%3A%2F%2Fpystac-client.readthedocs.io%2Fen%2Fv0.9.1%2F%0A%20%20%20%20-%20stackstac%200.5.1%20%60stack%60%3A%20https%3A%2F%2Fstackstac.readthedocs.io%2Fen%2Fv0.5.1%2Fapi%2Fmain%2Fstackstac.stack.html%0A%20%20%20%20-%20Dask%20task%20graphs%3A%20https%3A%2F%2Fdocs.dask.org%2Fen%2Fstable%2Fgraphs.html%0A%20%20%20%20-%20xarray%202025.11.0%20vectorised%20indexing%3A%20https%3A%2F%2Fdocs.xarray.dev%2Fen%2Fv2025.11.0%2Fuser-guide%2Findexing.html%0A%20%20%20%20-%20GDAL%203.12%20configuration%20options%3A%20https%3A%2F%2Fgdal.org%2Fen%2Frelease-3.12%2Fuser%2Fconfigoptions.html%0A%20%20%20%20-%20GDAL%203.12%20virtual%20file%20systems%20(%60%2Fvsicurl%2F%60%2C%20%60%2Fvsis3%2F%60)%3A%20https%3A%2F%2Fgdal.org%2Fen%2Frelease-3.12%2Fuser%2Fvirtual_file_systems.html%0A%20%20%20%20-%20The%20COG%20format%3A%20https%3A%2F%2Fgdal.org%2Fen%2Frelease-3.12%2Fdrivers%2Fraster%2Fcog.html%20and%20https%3A%2F%2Fwww.cogeo.org%2F%0A%20%20%20%20-%20rasterio%20point%20sampling%3A%20https%3A%2F%2Frasterio.readthedocs.io%2Fen%2Fstable%2Fapi%2Frasterio.sample.html%0A%20%20%20%20-%20odc-stac%3A%20https%3A%2F%2Fodc-stac.readthedocs.io%2Fen%2Flatest%2F%0A%20%20%20%20-%20geopandas%201.2.0%20spatial%20joins%3A%20https%3A%2F%2Fgeopandas.org%2Fen%2Fv1.2.0%2Fdocs%2Fuser_guide%2Fmergingdata.html%20and%20%60sjoin%60%3A%20https%3A%2F%2Fgeopandas.org%2Fen%2Fv1.2.0%2Fdocs%2Freference%2Fapi%2Fgeopandas.sjoin.html%0A%20%20%20%20-%20NumPy%202.5%20%60nanpercentile%60%20(with%20%60weights%3D%60)%3A%20https%3A%2F%2Fnumpy.org%2Fdoc%2F2.5%2Freference%2Fgenerated%2Fnumpy.nanpercentile.html%20and%20%60trapezoid%60%3A%20https%3A%2F%2Fnumpy.org%2Fdoc%2F2.5%2Freference%2Fgenerated%2Fnumpy.trapezoid.html%0A%20%20%20%20-%20SciPy%20%60curve_fit%60%20(SciPy%20publishes%20no%20versioned%20page%20for%201.18.1)%3A%20https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Fgenerated%2Fscipy.optimize.curve_fit.html%0A%20%20%20%20-%20Phantom%20MGRS%20tiles%20and%20the%20audit%20of%20dropped%20fields%3A%20tf-data-ml-utils%20issue%20728%2C%20https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fissues%2F728%0A%20%20%20%20-%20Area-weighted%20means%20beside%20unweighted%20percentiles%20(finding%20F16)%3A%20tf-model-lab%20issue%2043%2C%20https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F43%0A%20%20%20%20-%20Olofsson%20et%20al.%20(2014)%2C%20%22Good%20practices%20for%20estimating%20area%20and%20assessing%20accuracy%20of%20land%20change%22%2C%20*Remote%20Sensing%20of%20Environment*%20148%2C%2042%E2%80%9357%3A%20https%3A%2F%2Fdoi.org%2F10.1016%2Fj.rse.2014.02.015%0A%20%20%20%20-%20Beck%20et%20al.%20(2006)%2C%20%22Improved%20monitoring%20of%20vegetation%20dynamics%20at%20very%20high%20latitudes%3A%20a%20new%20method%20using%20MODIS%20NDVI%22%2C%20*Remote%20Sensing%20of%20Environment*%20100(3)%2C%20321%E2%80%93334%3A%20https%3A%2F%2Fdoi.org%2F10.1016%2Fj.rse.2005.10.021%0A%20%20%20%20-%20J%C3%B6nsson%20and%20Eklundh%20(2004)%2C%20%22TIMESAT%2C%20a%20program%20for%20analyzing%20time-series%20of%20satellite%20sensor%20data%22%2C%20*Computers%20%26%20Geosciences*%2030(8)%2C%20833%E2%80%93845%3A%20https%3A%2F%2Fdoi.org%2F10.1016%2Fj.cageo.2004.05.006%0A%20%20%20%20-%20Sentinel-2%20processing%20and%20the%20SCL%20classes%20(SentiWiki)%3A%20https%3A%2F%2Fsentiwiki.copernicus.eu%2Fweb%2Fs2-processing%0A%20%20%20%20-%20The%20Baseline%2004.00%20offset%20and%20how%20to%20detect%20it%20per%20asset%20(Element%2084)%3A%20https%3A%2F%2Fgithub.com%2FElement84%2Fearth-search%2Fblob%2Fmain%2Fdocs%2Fcollections%2Fsentinel-2-l2a.md%0A%20%20%20%20-%20Planetary%20Computer%20Sentinel-2%20L2A%3A%20https%3A%2F%2Fplanetarycomputer.microsoft.com%2Fdataset%2Fsentinel-2-l2a%0A%20%20%20%20-%20Earth%20Search%20Sentinel-2%20COGs%20on%20AWS%3A%20https%3A%2F%2Fregistry.opendata.aws%2Fsentinel-2-l2a-cogs%2F%0A%20%20%20%20-%20Planetary%20Computer%20Sentinel-1%20RTC%3A%20https%3A%2F%2Fplanetarycomputer.microsoft.com%2Fdataset%2Fsentinel-1-rtc%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
377f46843da7ee98554456edd2e28b22