import%20marimo%0A%0A__generated_with%20%3D%20%220.26.0%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20os%0A%20%20%20%20import%20sys%0A%0A%20%20%20%20for%20var%20in%20(%22PROJ_LIB%22%2C%20%22PROJ_DATA%22%2C%20%22GDAL_DATA%22)%3A%0A%20%20%20%20%20%20%20%20os.environ.pop(var%2C%20None)%20%20%23%20a%20conda%20PROJ%20install%20on%20the%20path%20breaks%20rasterio's%20bundled%20one%0A%20%20%20%20os.environ.setdefault(%22GRPC_VERBOSITY%22%2C%20%22ERROR%22)%20%20%23%20gcsfs%20brings%20in%20gRPC%2C%20which%20logs%20at%20start-up%0A%0A%20%20%20%20import%20inspect%0A%20%20%20%20import%20logging%0A%20%20%20%20import%20socket%0A%20%20%20%20import%20tempfile%0A%20%20%20%20import%20warnings%0A%20%20%20%20from%20pathlib%20import%20Path%0A%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%20xarray%20as%20xr%0A%20%20%20%20import%20yaml%0A%20%20%20%20import%20zarr%0A%20%20%20%20from%20loguru%20import%20logger%0A%0A%20%20%20%20from%20labkit.show%20import%20show%20%20%23%20a%20function's%20real%20source%2C%20linked%20to%20the%20pinned%20commit%0A%0A%20%20%20%20logger.remove()%0A%20%20%20%20logger.add(%0A%20%20%20%20%20%20%20%20sys.stderr%2C%20level%3D%22WARNING%22%2C%20colorize%3DFalse%2C%20format%3D%22%7Blevel%7D%20%7Bname%7D%3A%7Bfunction%7D%20-%20%7Bmessage%7D%22%0A%20%20%20%20)%20%20%23%20era5_ingest%20logs%20at%20INFO%0A%20%20%20%20warnings.filterwarnings(%0A%20%20%20%20%20%20%20%20%22ignore%22%2C%20message%3D%22The%20'generic'%20unit%20for%20NumPy%20timedelta%22%0A%20%20%20%20)%20%20%23%20pandas%202.3%20on%20numpy%202.5%2C%20harmless%0A%20%20%20%20logging.basicConfig(level%3Dlogging.ERROR)%20%20%23%20ecmwf_ingest%20logs%20through%20the%20standard%20library%0A%0A%20%20%20%20WORK%20%3D%20Path(tempfile.mkdtemp(prefix%3D%22nb13_%22))%0A%0A%20%20%20%20%23%20era5_ingest.config%20refuses%20to%20import%20without%20a%20spatial%20registry%20(production's%20lives%20in%20treefera-data-pipelines).%0A%20%20%20%20%23%20These%20boxes%20are%20copied%20from%20data-pipelines%20bb0ca14%20spatial_registry.yaml%2C%20in%20its%20order%20%5Blon_min%2C%20lat_min%2C%20lon_max%2C%20lat_max%5D.%0A%20%20%20%20REGISTRY%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22kenya%22%3A%20%5B32.0%2C%20-6.0%2C%2044.0%2C%208.0%5D%2C%0A%20%20%20%20%20%20%20%20%22tanzania%22%3A%20%5B28.0%2C%20-14.0%2C%2042.0%2C%200.0%5D%2C%0A%20%20%20%20%20%20%20%20%22zambia%22%3A%20%5B20.0%2C%20-20.0%2C%2036.0%2C%20-6.0%5D%2C%0A%20%20%20%20%20%20%20%20%22australia%22%3A%20%5B112.0%2C%20-44.0%2C%20154.0%2C%20-10.0%5D%2C%0A%20%20%20%20%20%20%20%20%22canada%22%3A%20%5B-142.0%2C%2040.0%2C%20-52.0%2C%2084.0%5D%2C%0A%20%20%20%20%20%20%20%20%22europe%22%3A%20%5B-26.0%2C%2028.0%2C%2046.0%2C%2074.0%5D%2C%0A%20%20%20%20%20%20%20%20%22european_russia%22%3A%20%5B18.0%2C%2042.0%2C%2045.0%2C%2070.0%5D%2C%0A%20%20%20%20%20%20%20%20%22russia_north_caucasus%22%3A%20%5B36.0%2C%2044.0%2C%2044.0%2C%2054.0%5D%2C%0A%20%20%20%20%20%20%20%20%22russia%22%3A%20%5B18.0%2C%2040.0%2C%20179.75%2C%2082.0%5D%2C%0A%20%20%20%20%20%20%20%20%22france%22%3A%20%5B-6.0%2C%2040.0%2C%2010.0%2C%2052.0%5D%2C%0A%20%20%20%20%20%20%20%20%22ukraine%22%3A%20%5B22.0%2C%2044.0%2C%2036.0%2C%2054.0%5D%2C%0A%20%20%20%20%20%20%20%20%22ghana%22%3A%20%5B-4.0%2C%204.0%2C%202.0%2C%2012.0%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20registry_path%20%3D%20WORK%20%2F%20%22spatial_registry.yaml%22%0A%20%20%20%20registry_path.write_text(%0A%20%20%20%20%20%20%20%20yaml.safe_dump(%7B%22spatial_ids%22%3A%20%7Bk%3A%20%7B%22bbox%22%3A%20v%7D%20for%20k%2C%20v%20in%20REGISTRY.items()%7D%7D)%0A%20%20%20%20)%0A%20%20%20%20os.environ%5B%22WEATHER_EXTRA_SPATIAL_REGISTRY_PATHS%22%5D%20%3D%20str(registry_path)%0A%0A%20%20%20%20def%20online(host%3D%22storage.googleapis.com%22)%3A%0A%20%20%20%20%20%20%20%20%23%20%5Bnet%5D%20cells%20run%20only%20when%20the%20public%20bucket%20is%20reachable%20and%20MODEL_LEARNING_OFFLINE%20is%20unset.%0A%20%20%20%20%20%20%20%20if%20os.environ.get(%22MODEL_LEARNING_OFFLINE%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20False%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20socket.create_connection((host%2C%20443)%2C%20timeout%3D3).close()%0A%20%20%20%20%20%20%20%20except%20OSError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20False%0A%20%20%20%20%20%20%20%20return%20True%0A%0A%20%20%20%20NET%20%3D%20online()%0A%20%20%20%20ARCO%20%3D%20%22gcp-public-data-arco-era5%2Far%2Ffull_37-1h-0p25deg-chunk-1.zarr-v3%22%0A%20%20%20%20TODAY%20%3D%20pd.Timestamp.now().normalize()%0A%20%20%20%20print(%22zarr%22%2C%20zarr.__version__%2C%20%22%7C%20xarray%22%2C%20xr.__version__%2C%20%22%7C%20network%20for%20%5Bnet%5D%20cells%3A%22%2C%20NET)%0A%20%20%20%20print(%22today%3A%22%2C%20TODAY.date())%0A%0A%20%20%20%20%23%20Imported%20here%20because%20later%20cells%20use%20them%20too.%0A%20%20%20%20from%20tf_data_ml_utils.era5_ingest.ingest%20import%20splice_day%0A%20%20%20%20from%20tf_data_ml_utils.features.stages.stitch.stitch%20import%20reduce_ensemble%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%20Weather%20and%20climate%3A%20a%20time%20axis%20that%20lies%2C%20and%20a%20longitude%20that%20wraps%0A%0A%20%20%20%20**What%20you'll%20learn**%0A%0A%20%20%20%20(Sections%202.10%20to%202.13%20add%20forecasts%3A%20how%20cumulative%20rain%20becomes%20days%2C%20how%20forecasts%20join%20observations%2C%20CHIRPS%20pentads%20and%20vintages%2C%20and%20ensembles.)%0A%0A%20%20%20%201.%20How%20xarray%20selects%20by%20label%2C%20why%20a%20label%20slice%20silently%20returns%20nothing%20when%20the%20axis%20is%20out%20of%20order%2C%20and%20how%20Zarr%20turns%20a%20missing%20chunk%20into%20NaN%20rather%20than%20an%20error.%0A%20%20%20%202.%20How%20our%20ERA5%20ingester%20finds%20the%20newest%20real%20data%20in%20a%20store%20whose%20time%20axis%20runs%20to%202050%2C%20refuses%20incomplete%20days%2C%20fills%20holes%20and%20raises%20an%20alarm%2C%20all%20run%20here%20on%20a%20local%20store%20you%20can%20inspect.%20Then%20how%20the%20ECMWF%20ingester%20cuts%20France%20and%20Europe%20to%20**zero%20longitudes%20and%20reports%20success**%20(finding%20F2%2C%20%5Btf-model-lab%20issue%2029%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F29)).%0A%20%20%20%203.%20The%20science%20underneath%3A%20two%20longitude%20conventions%20and%20the%20box%20neither%20suits%2C%20why%20ERA5%20precipitation%20is%20missing%20for%20the%20first%20hours%20of%201940%2C%20and%20how%20ERA5T%20differs%20from%20final%20ERA5.%20You'll%20also%20try%20three%20fixes%20for%20F2.%0A%0A%20%20%20%20**How%20to%20run%3A**%20follow%20the%20set-up%20in%20%5Bacademy%2FREADME.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2FREADME.md%23setting-up)%3A%20%60uv%20sync%60.%20Then%2C%20from%20%60academy%2F%60%2C%20open%20it%20with%20%60uv%20run%20marimo%20edit%201-data%2F1.3-weather-grids%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.%20Everything%20runs%20offline%20on%20synthetic%20grids%20written%20to%20a%20temporary%20folder.%20Two%20cells%20marked%20**%5Bnet%5D**%20read%20a%20few%20megabytes%20from%20the%20public%20ARCO-ERA5%20bucket%3B%20without%20a%20network%20(or%20with%20%60MODEL_LEARNING_OFFLINE%3D1%60%20set)%20they%20print%20%22skipped%20(offline)%22%20and%20the%20rest%20still%20runs.%0A%0A%20%20%20%20The%20production%20code%20is%20imported%2C%20not%20copied.%20%60tf-data-ml-utils%60%20is%20pinned%20to%20%60c0ba9dc%60%20in%20%60pyproject.toml%60%20with%20its%20%60%5Bfeatures%2Cera5%5D%60%20extras%2C%20and%20every%20%60show(...)%60%20cell%20prints%20the%20real%20source%20with%20a%20permalink%20to%20that%20commit.%20Region%20boxes%20come%20from%20treefera-data-pipelines%20%60bb0ca14%60.%20zarr%203.1.5%20and%20xarray%202025.11.0%20match%20tf-data-ml-utils'%20lock%20file.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Part%201%3A%20the%20libraries%2C%20as%20their%20documentation%20introduces%20them%0A%0A%20%20%20%20%23%23%201.1%20xarray%20selects%20by%20label%2C%20and%20a%20backwards%20slice%20selects%20nothing%0A%0A%20%20%20%20xarray's%20%5B%60.sel%60%5D(https%3A%2F%2Fdocs.xarray.dev%2Fen%2Fv2025.11.0%2Fuser-guide%2Findexing.html)%20picks%20data%20by%20coordinate%20**label**%2C%20not%20by%20position%20(%60.isel%60%20is%20by%20position).%20Underneath%2C%20a%20slice%20of%20labels%20becomes%20a%20pair%20of%20positions%20through%20pandas'%20%5B%60Index.slice_locs%60%5D(https%3A%2F%2Fpandas.pydata.org%2Fpandas-docs%2Fversion%2F2.3.3%2Freference%2Fapi%2Fpandas.Index.slice_locs.html)%3A%20%22the%20first%20position%20whose%20label%20is%20at%20least%20*start*%22%20and%20%22the%20last%20position%20whose%20label%20is%20at%20most%20*stop*%22.%20On%20an%20ascending%20axis%2C%20a%20slice%20whose%20start%20is%20greater%20than%20its%20stop%20gives%20an%20end%20position%20before%20the%20start%20position%2C%20so%20the%20result%20is%20**empty%2C%20with%20no%20error**.%0A%0A%20%20%20%20Here%20is%20a%201%C2%B0%20global%20grid%20stored%20the%20way%20ECMWF's%20GRIB%20files%20usually%20are%3A%20longitudes%200%E2%80%A6359%2C%20latitudes%2090%20down%20to%20%E2%88%9290.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_1_xarray_selects_label()%3A%0A%20%20%20%20lon360%20%3D%20np.arange(0.0%2C%20360.0%2C%201.0)%0A%20%20%20%20lat%20%3D%20np.arange(90.0%2C%20-90.5%2C%20-1.0)%0A%20%20%20%20rng%20%3D%20np.random.default_rng(0)%0A%20%20%20%20g360%20%3D%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22t2m%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22latitude%22%2C%20%22longitude%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rng.normal(288%2C%2010%2C%20(lat.size%2C%20lon360.size)).astype(%22float32%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20coords%3D%7B%22latitude%22%3A%20lat%2C%20%22longitude%22%3A%20lon360%7D%2C%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22longitudes%22%2C%0A%20%20%20%20%20%20%20%20g360.longitude.values%5B%5B0%2C%201%2C%20-1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%22%7C%20latitudes%22%2C%0A%20%20%20%20%20%20%20%20g360.latitude.values%5B%5B0%2C%20-1%5D%5D%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22sel(longitude%3Dslice(10%2C%2020))%20%20-%3E%22%2C%20dict(g360.sel(longitude%3Dslice(10%2C%2020)).sizes))%0A%20%20%20%20print(%22sel(longitude%3Dslice(354%2C%2010))%20-%3E%22%2C%20dict(g360.sel(longitude%3Dslice(354%2C%2010)).sizes))%0A%20%20%20%20print(%22slice_locs(354%2C%2010)%20on%20the%20index%3A%22%2C%20g360.indexes%5B%22longitude%22%5D.slice_locs(354%2C%2010))%0A%20%20%20%20return%20g360%2C%20lat%2C%20lon360%2C%20rng%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%60slice_locs(354%2C%2010)%60%20returns%20a%20start%20position%20after%20the%20stop%20position%2C%20so%20the%20selection%20is%20empty.%20France%20runs%20from%206%C2%B0%20W%20to%2010%C2%B0%20E.%20On%20this%20grid%20that%20is%20354%E2%80%A6359%20**and**%200%E2%80%A610%2C%20two%20pieces%20at%20opposite%20ends%20of%20the%20axis.%20No%20single%20label%20slice%20can%20express%20it.%0A%0A%20%20%20%20%23%23%201.2%20A%20non-monotonic%20index%3A%20relabelled%20but%20not%20sorted%0A%0A%20%20%20%20The%20usual%20fix%20is%20to%20relabel%200%E2%80%A6360%20as%20%E2%88%92180%E2%80%A6180%20with%20%60((lon%20%2B%20180)%20%25%20360)%20%E2%88%92%20180%60.%20If%20you%20relabel%20**without%20sorting**%2C%20the%20axis%20keeps%20its%20old%20order%3A%200%2C%201%2C%20%E2%80%A6%2C%20179%2C%20%E2%88%92180%2C%20%E2%80%A6%2C%20%E2%88%921.%20An%20index%20is%20*monotonic*%20when%20its%20labels%20only%20ever%20increase%20(or%20only%20decrease)%3B%20this%20one%20jumps%20back%20at%20180.%20pandas%20checks%20this%20with%20%5B%60is_monotonic_increasing%60%5D(https%3A%2F%2Fpandas.pydata.org%2Fpandas-docs%2Fversion%2F2.3.3%2Freference%2Fapi%2Fpandas.Index.is_monotonic_increasing.html).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_2_non_monotonic_index(g360)%3A%0A%20%20%20%20relabelled%20%3D%20g360.assign_coords(longitude%3D((g360.longitude%20%2B%20180)%20%25%20360)%20-%20180)%0A%20%20%20%20idx%20%3D%20relabelled.indexes%5B%22longitude%22%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22order%3A%22%2C%0A%20%20%20%20%20%20%20%20idx%5B%5B0%2C%201%2C%20179%2C%20180%2C%20181%2C%20-1%5D%5D.tolist()%2C%0A%20%20%20%20%20%20%20%20%22%7C%20monotonic%20increasing%3A%22%2C%0A%20%20%20%20%20%20%20%20idx.is_monotonic_increasing%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22sel(longitude%3Dslice(-6%2C%2010))%20-%3E%22%2C%20dict(relabelled.sel(longitude%3Dslice(-6%2C%2010)).sizes))%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20relabelled.sel(longitude%3D-5.2%2C%20method%3D%22nearest%22)%0A%20%20%20%20except%20ValueError%20as%20exc%3A%0A%20%20%20%20%20%20%20%20print(%22sel(method%3D'nearest')%20raises%3A%22%2C%20exc)%0A%0A%20%20%20%20fixed%20%3D%20relabelled.sortby(%22longitude%22)%0A%20%20%20%20print(%22after%20sortby%3A%20slice(-6%2C%2010)%20-%3E%22%2C%20dict(fixed.sel(longitude%3Dslice(-6%2C%2010)).sizes))%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20nearest%20to%20-5.2%20-%3E%22%2C%0A%20%20%20%20%20%20%20%20float(fixed.sel(longitude%3D-5.2%2C%20method%3D%22nearest%22).longitude)%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(fixed%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%20The%20two%20failures%20behave%20differently.%20%60method%3D%22nearest%22%60%20**raises**%2C%20because%20pandas%20refuses%20to%20search%20an%20unsorted%20index.%20A%20plain%20label%20slice%20returns%20**zero%20longitudes%20silently**.%20Silent%20failures%20are%20the%20dangerous%20kind%2C%20and%20the%20rest%20of%20this%20notebook%20keeps%20meeting%20this%20one.%20After%20%5B%60sortby%60%5D(https%3A%2F%2Fdocs.xarray.dev%2Fen%2Fv2025.11.0%2Fgenerated%2Fxarray.Dataset.sortby.html)%2C%20France%20is%2017%20columns%20(%E2%88%926%2C%20%E2%88%925%2C%20%E2%80%A6%2C%2010).%0A%0A%20%20%20%20%23%23%201.3%20%60roll%60%20against%20%60sortby%60%0A%0A%20%20%20%20%5B%60Dataset.roll%60%5D(https%3A%2F%2Fdocs.xarray.dev%2Fen%2Fv2025.11.0%2Fgenerated%2Fxarray.Dataset.roll.html)%20shifts%20every%20value%20along%20a%20dimension%20by%20a%20fixed%20number%20of%20positions%2C%20wrapping%20the%20end%20round%20to%20the%20start.%20Rolling%20a%200%E2%80%A6359%20axis%20by%20180%20positions%20puts%20180%E2%80%A6359%20first%2C%20and%20relabelling%20then%20gives%20an%20ascending%20%E2%88%92180%E2%80%A6179%20axis%20without%20sorting%20anything.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_3_roll_sortby(fixed%2C%20g360)%3A%0A%20%20%20%20rolled%20%3D%20g360.roll(longitude%3D180%2C%20roll_coords%3DTrue)%0A%20%20%20%20rolled%20%3D%20rolled.assign_coords(longitude%3D((rolled.longitude%20%2B%20180)%20%25%20360)%20-%20180)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22rolled%20first%2Flast%3A%22%2C%0A%20%20%20%20%20%20%20%20rolled.longitude.values%5B%5B0%2C%20-1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%22%7C%20monotonic%3A%22%2C%0A%20%20%20%20%20%20%20%20rolled.indexes%5B%22longitude%22%5D.is_monotonic_increasing%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22identical%20to%20relabel%20%2B%20sortby%3A%22%2C%20rolled.identical(fixed))%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%20array%2C%20two%20routes.%20%60roll%60%20moves%20data%20by%20a%20fixed%20offset%20in%20one%20pass%2C%20while%20%60sortby%60%20sorts%20the%20labels%20(an%20*n*%20log%20*n*%20operation)%20and%20reorders%20to%20match.%20%60roll%60%20only%20works%20when%20you%20know%20the%20axis%20is%20a%20full%2C%20evenly%20spaced%20circle%3B%20%60sortby%60%20works%20on%20anything%2C%20which%20is%20why%20production%20code%20uses%20it.%0A%0A%20%20%20%20%23%23%201.4%20Zarr%3A%20a%20missing%20chunk%20is%20read%20as%20the%20fill%20value%0A%0A%20%20%20%20A%20Zarr%20array%20is%20a%20JSON%20header%20plus%20one%20stored%20object%20per%20**chunk**%20(unit%201.1%20covers%20this).%20The%20%5BZarr%20v2%20specification%5D(https%3A%2F%2Fzarr-specs.readthedocs.io%2Fen%2Flatest%2Fv2%2Fv2.0.html)%20says%20a%20chunk%20that%20has%20never%20been%20written%20%22MUST%20be%20treated%20as%20if%20it%20was%20uniformly%20filled%20with%20the%20value%20of%20the%20'fill_value'%20field%22.%20(The%20link%20says%20%60latest%60%2C%20which%20is%20safe%20for%20v2%20because%20the%20v2%20specification%20is%20frozen.)%20So%20a%20missing%20object%20is%20not%20an%20error%3A%20with%20%60fill_value%3DNaN%60%20it%20reads%20back%20as%20NaN.%0A%0A%20%20%20%20We%20write%20rows%200%E2%80%932%20of%20a%2010-row%20array%2C%20then%20write%20row%205%20as%20all%20NaN%2C%20and%20look%20at%20what%20is%20on%20disk.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_4_zarr_missing_chunk()%3A%0A%20%20%20%20arr%20%3D%20zarr.create_array(%0A%20%20%20%20%20%20%20%20store%3DWORK%20%2F%20%22fill.zarr%22%2C%0A%20%20%20%20%20%20%20%20shape%3D(10%2C%204)%2C%0A%20%20%20%20%20%20%20%20chunks%3D(1%2C%204)%2C%0A%20%20%20%20%20%20%20%20dtype%3D%22float32%22%2C%0A%20%20%20%20%20%20%20%20fill_value%3Dfloat(%22nan%22)%2C%0A%20%20%20%20%20%20%20%20zarr_format%3D2%2C%0A%20%20%20%20%20%20%20%20overwrite%3DTrue%2C%0A%20%20%20%20)%0A%20%20%20%20arr%5B0%3A3%5D%20%3D%201.0%0A%20%20%20%20arr%5B5%5D%20%3D%20float(%22nan%22)%0A%20%20%20%20print(%22objects%20on%20disk%3A%22%2C%20sorted(p.name%20for%20p%20in%20(WORK%20%2F%20%22fill.zarr%22).iterdir()))%0A%20%20%20%20print(%22row%209%20(never%20written)%3A%22%2C%20arr%5B9%5D)%0A%20%20%20%20print(%22row%205%20(written%20as%20NaN)%3A%22%2C%20arr%5B5%5D)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22write_empty_chunks%20in%20this%20zarr's%20runtime%20config%3A%22%2C%0A%20%20%20%20%20%20%20%20zarr.config.get(%22array.write_empty_chunks%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%20Three%20chunk%20files%2C%20%600.0%60%2C%20%601.0%60%20and%20%602.0%60.%20Row%209%20was%20never%20written%20and%20reads%20as%20NaN.%20Row%205%20*was*%20written%2C%20but%20every%20value%20equalled%20the%20fill%20value%2C%20so%20zarr%20skipped%20it%3A%20no%20%605.0%60%20appears.%20That%20is%20the%20%60write_empty_chunks%60%20setting%2C%20False%20in%20this%20zarr's%20runtime%20configuration.%20(The%20zarr%203.1.5%20%5Bperformance%20guide's%20%22Empty%20chunks%22%20section%5D(https%3A%2F%2Fzarr.readthedocs.io%2Fen%2Fv3.1.5%2Fuser-guide%2Fperformance%2F)%20calls%20%60True%60%20%22the%20default%22%3B%20the%20runtime%20value%20printed%20above%20is%20what%20this%20library%20actually%20does.%20When%20the%20documentation%20and%20the%20code%20disagree%2C%20the%20code%20wins.)%0A%0A%20%20%20%20So%20**%22never%20written%22%20and%20%22written%20as%20all%20NaN%22%20can't%20be%20told%20apart%20by%20design**%2C%20and%20both%20read%20as%20NaN.%20Keep%20that%20in%20mind%20for%20Part%202.%0A%0A%20%20%20%20%23%23%201.5%20A%20pre-allocated%20time%20axis%0A%0A%20%20%20%20ARCO-ERA5%20(Google's%20cloud%20copy%20of%20ERA5%2C%20Part%202)%20has%20a%20time%20axis%20with%20one%20slot%20for%20every%20hour%20from%201900%20to%202050%2C%20though%20data%20exists%20only%20from%201940%20to%20a%20few%20days%20ago.%20Because%20unwritten%20chunks%20cost%20nothing%2C%20the%20future%20is%20simply%20objects%20that%20don't%20exist%20yet.%0A%0A%20%20%20%20We%20build%20a%20small%20store%20the%20same%20way%3A%2060%20days%20of%20hourly%20%60total_precipitation%60%2C%20the%20last%2040%20in%20the%20future.%20Only%20past%20hours%20are%20written%2C%20and%20with%20two%20problems%20planted%20in%20the%20past%3A%0A%0A%20%20%20%20-%20a%2024-hour%20hole%20at%20today%20%E2%88%92%206%20days%3B%0A%20%20%20%20-%20data%20again%20for%20one%20day%20at%20today%20%E2%88%92%205%20days%2C%20then%20nothing.%0A%0A%20%20%20%20One%20chunk%20per%20hour%2C%20as%20in%20ARCO%2C%20and%20a%202%20%C3%97%202%20corner%20of%20the%20globe%20so%20it%20stays%20tiny.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_5_pre_allocated_time()%3A%0A%20%20%20%20time%20%3D%20pd.date_range(%0A%20%20%20%20%20%20%20%20TODAY%20-%20pd.Timedelta(days%3D20)%2C%20TODAY%20%2B%20pd.Timedelta(days%3D40)%2C%20freq%3D%22h%22%2C%20inclusive%3D%22left%22%0A%20%20%20%20)%0A%20%20%20%20day%20%3D%20pd.Timedelta(days%3D1)%0A%20%20%20%20have%20%3D%20(time%20%3C%20TODAY%20-%206%20*%20day)%20%7C%20((time%20%3E%3D%20TODAY%20-%205%20*%20day)%20%26%20(time%20%3C%20TODAY%20-%204%20*%20day))%0A%20%20%20%20tp%20%3D%20np.full((time.size%2C%202%2C%202)%2C%20np.nan%2C%20dtype%3D%22float32%22)%0A%20%20%20%20tp%5Bhave%5D%20%3D%201e-4%20%20%23%200.1%20mm%20per%20hour%2C%20in%20metres%20as%20ERA5%20stores%20it%0A%20%20%20%20store_ds%20%3D%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%7B%22total_precipitation%22%3A%20((%22time%22%2C%20%22latitude%22%2C%20%22longitude%22)%2C%20tp%2C%20%7B%22units%22%3A%20%22m%22%7D)%7D%2C%0A%20%20%20%20%20%20%20%20coords%3D%7B%22time%22%3A%20time%2C%20%22latitude%22%3A%20%5B90.0%2C%2089.75%5D%2C%20%22longitude%22%3A%20%5B0.0%2C%200.25%5D%7D%2C%0A%20%20%20%20)%0A%20%20%20%20store%20%3D%20WORK%20%2F%20%22arco_like.zarr%22%0A%20%20%20%20store_ds.chunk(%7B%22time%22%3A%201%7D).to_zarr(store%2C%20zarr_format%3D2%2C%20consolidated%3DTrue%2C%20mode%3D%22w%22)%0A%0A%20%20%20%20objects%20%3D%20%5Bp%20for%20p%20in%20(store%20%2F%20%22total_precipitation%22).iterdir()%20if%20not%20p.name.startswith(%22.%22)%5D%0A%20%20%20%20opened%20%3D%20xr.open_zarr(store)%0A%20%20%20%20print(%22time%20slots%3A%22%2C%20opened.sizes%5B%22time%22%5D%2C%20%22%7C%20chunk%20objects%20written%3A%22%2C%20len(objects))%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22last%20timestamp%20on%20the%20axis%3A%22%2C%0A%20%20%20%20%20%20%20%20pd.Timestamp(opened.time.values%5B-1%5D)%2C%0A%20%20%20%20%20%20%20%20%22-%3E%20in%20the%20future%3A%22%2C%0A%20%20%20%20%20%20%20%20opened.time.values%5B-1%5D%20%3E%20np.datetime64(TODAY)%2C%0A%20%20%20%20)%0A%20%20%20%20return%20day%2C%20store%2C%20store_ds%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%60time%5B-1%5D%60%20is%2040%20days%20in%20the%20future%2C%20and%20only%20a%20quarter%20of%20the%20slots%20have%20an%20object.%20%22What%20is%20the%20newest%20data%3F%22%20can't%20be%20answered%20from%20the%20axis.%20The%20ARCO%20README's%20own%20idiom%2C%20%60ds.sel(time%3Dslice(ds.attrs%5B'valid_time_start'%5D%2C%20ds.attrs%5B'valid_time_stop'%5D))%60%2C%20drops%20the%20pre-allocated%20slots%20by%20trusting%20attributes%20instead.%20Our%20ingester%20trusts%20the%20values.%20Part%202%20shows%20how.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Part%202%3A%20our%20production%20code%2C%20shown%20and%20run%0A%0A%20%20%20%20%23%23%202.1%20The%20probe%3A%20%60get_era5_availability%60%0A%0A%20%20%20%20The%20ingester's%20first%20question%20each%20day%20is%20%22how%20far%20does%20ERA5%20go%20right%20now%3F%22%20%60get_era5_availability%60%20answers%20it%20by%20reading%20data%2C%20not%20the%20axis.%20From%20today%20it%20walks%20**backwards%20one%20day%20at%20a%20time**%2C%20reads%20a%2010%20%C3%97%2010%20corner%20of%20hour%2000%20and%20returns%20the%20first%20hour%20with%20any%20real%20value.%20It%20walks%20back%20at%20most%2030%20days.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_1_probe_get_era5()%3A%0A%20%20%20%20from%20tf_data_ml_utils.era5_ingest.frontier%20import%20assert_not_stalled%2C%20get_era5_availability%0A%0A%20%20%20%20show(get_era5_availability)%0A%20%20%20%20return%20assert_not_stalled%2C%20get_era5_availability%0A%0A%0A%40app.cell%0Adef%20s2_1_probe_get_era5_2(get_era5_availability%2C%20store)%3A%0A%20%20%20%20availability%20%3D%20get_era5_availability(str(store))%0A%20%20%20%20print(%22availability%3A%22%2C%20availability%2C%20%22%3D%22%2C%20(TODAY%20-%20availability).days%2C%20%22days%20before%20today%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%20It%20returns%20today%20%E2%88%92%205%20days%2C%20the%20last%20day%20with%20data.%20Walking%20back%20from%20today%2C%20it%20never%20reaches%20today%20%E2%88%92%206%2C%20so%20**the%20probe%20jumps%20over%20the%20hole**.%20Catching%20the%20hole%20is%20the%20job%20of%20the%20next%20layer.%0A%0A%20%20%20%20On%20the%20real%20store%20each%20step%20reads%20one%20whole-globe%20object%20(a%20chunk%20is%20one%20hour%20of%20the%20whole%20globe)%2C%20about%201.7%20MB%20compressed%20for%20%60total_precipitation%60.%20Cell%202.9%20measures%20that%20live.%20Zarr%20v2%20can't%20read%20part%20of%20a%20chunk%2C%20so%20asking%20for%20100%20values%20still%20downloads%201.7%20MB.%20A%2030-day%20walk-back%20costs%20at%20most%20about%2050%20MB%3A%20cheap%20next%20to%20the%20ingest%2C%20but%20not%20%22small%22.%0A%0A%20%20%20%20%23%23%202.2%20The%20gate%3A%20%60keep_complete_periods%60%0A%0A%20%20%20%20The%20ingester%20turns%20hours%20into%20days%2C%20counts%20the%20valid%20hours%20in%20each%20day%2C%20and%20keeps%20only%20the%20**leading%20run**%20of%20complete%20days.%20Everything%20from%20the%20first%20short%20day%20onwards%20waits%20for%20the%20next%20run.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_2_gate_keep_complete()%3A%0A%20%20%20%20from%20tf_data_ml_utils.common.cube%20import%20count_valid_samples%2C%20resample_to_frequency%0A%20%20%20%20from%20tf_data_ml_utils.era5_ingest.ingest%20import%20(%0A%20%20%20%20%20%20%20%20expected_samples_per_period%2C%0A%20%20%20%20%20%20%20%20keep_complete_periods%2C%0A%20%20%20%20)%0A%0A%20%20%20%20show(keep_complete_periods)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20count_valid_samples%2C%0A%20%20%20%20%20%20%20%20expected_samples_per_period%2C%0A%20%20%20%20%20%20%20%20keep_complete_periods%2C%0A%20%20%20%20%20%20%20%20resample_to_frequency%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20s2_2_gate_keep_complete_2(%0A%20%20%20%20count_valid_samples%2C%0A%20%20%20%20day%2C%0A%20%20%20%20expected_samples_per_period%2C%0A%20%20%20%20keep_complete_periods%2C%0A%20%20%20%20resample_to_frequency%2C%0A%20%20%20%20store_ds%2C%0A)%3A%0A%20%20%20%20hourly%20%3D%20store_ds.sel(time%3Dslice(TODAY%20-%209%20*%20day%2C%20TODAY%20-%204%20*%20day%20-%20pd.Timedelta(hours%3D1)))%0A%20%20%20%20_daily%20%3D%20resample_to_frequency(hourly%2C%20%5B%22total_precipitation%22%5D%2C%20%22D%22)%0A%20%20%20%20coverage%20%3D%20count_valid_samples(hourly%2C%20%22D%22)%0A%20%20%20%20kept%20%3D%20keep_complete_periods(_daily%2C%20coverage%2C%20expected_samples_per_period(%22D%22))%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22valid%20hours%20per%20day%3A%22%2C%0A%20%20%20%20%20%20%20%20coverage%5B%22total_precipitation_n_valid%22%5D.isel(latitude%3D0%2C%20longitude%3D0).values.tolist()%2C%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22days%3A%22%2C%0A%20%20%20%20%20%20%20%20_daily.sizes%5B%22time%22%5D%2C%0A%20%20%20%20%20%20%20%20%22%7C%20kept%3A%22%2C%0A%20%20%20%20%20%20%20%20kept.sizes%5B%22time%22%5D%2C%0A%20%20%20%20%20%20%20%20%22%7C%20expected%20hours%20per%20day%3A%22%2C%0A%20%20%20%20%20%20%20%20expected_samples_per_period(%22D%22)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20short%20%3D%20hourly.copy(deep%3DTrue)%0A%20%20%20%20short%5B%22total_precipitation%22%5D%5B30%5D%20%3D%20np.nan%20%20%23%20one%20missing%20hour%20on%20the%20second%20day%0A%20%20%20%20kept_short%20%3D%20keep_complete_periods(%0A%20%20%20%20%20%20%20%20resample_to_frequency(short%2C%20%5B%22total_precipitation%22%5D%2C%20%22D%22)%2C%0A%20%20%20%20%20%20%20%20count_valid_samples(short%2C%20%22D%22)%2C%0A%20%20%20%20%20%20%20%2024%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22with%20a%2023-hour%20second%20day%2C%20kept%3A%22%2C%20kept_short.sizes%5B%22time%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%20Five%20days%20span%20the%20hole%3A%20three%20complete%2C%20the%20empty%20hole%20day%2C%20then%20one%20complete%20day%20after%20it.%20The%20gate%20keeps%203%20of%205%2C%20so%20the%20complete%20day%20*after*%20the%20hole%20is%20held%20back%20too%3A%20a%20store%20with%20a%20gap%20in%20it%20would%20be%20worse%20than%20a%20store%20that%20ends%20early.%20With%20one%20hour%20missing%20on%20the%20second%20day%2C%20it%20keeps%201.%20A%2023-hour%20day%20is%20never%20written%20as%20if%20it%20were%20complete.%20(The%20WARNING%20printed%20above%20is%20the%20gate's%20own%20log%20line.)%0A%0A%20%20%20%20In%20production%2C%20%60process_batch%60%20then%20tries%20to%20**fill**%20the%20short%20day%20from%20ARCO's%20raw%20per-day%20NetCDF%20files%20and%20calls%20itself%20again%20on%20the%20patched%20data%2C%20remembering%20each%20day%20it%20has%20tried%2C%20so%20it%20can't%20recurse%20for%20ever.%20Cell%202.5%20tests%20the%20step%20that%20makes%20the%20patch%20line%20up.%0A%0A%20%20%20%20%23%23%202.3%20The%20alarm%3A%20%60assert_not_stalled%60%0A%0A%20%20%20%20A%20hole%20that%20keeps%20being%20skipped%20makes%20the%20store's%20end%20fall%20further%20behind%20the%20frontier.%20The%20alarm%20raises%20once%20that%20lag%20passes%20%60MAX_LAG_DAYS%60.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_3_alarm_assert_not(assert_not_stalled)%3A%0A%20%20%20%20from%20tf_data_ml_utils.common.errors%20import%20StaleStoreError%0A%20%20%20%20from%20tf_data_ml_utils.era5_ingest%20import%20frontier%0A%0A%20%20%20%20show(assert_not_stalled)%0A%20%20%20%20print(%22MAX_LAG_DAYS%20%3D%22%2C%20frontier.MAX_LAG_DAYS)%0A%20%20%20%20for%20lag%20in%20(pd.Timedelta(days%3D3%2C%20hours%3D23)%2C%20pd.Timedelta(days%3D4))%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20assert_not_stalled(TODAY%20-%20lag%2C%20TODAY)%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22lag%20%7Blag%7D%3A%20passes%22)%0A%20%20%20%20%20%20%20%20except%20StaleStoreError%20as%20exc%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22lag%20%7Blag%7D%3A%20raises%20StaleStoreError%5Cn%20%20%7Bexc%7D%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%60.days%60%20keeps%20whole%20days%20only%2C%20so%203%20days%2023%20hours%20counts%20as%203%20and%20passes%3B%204%20days%20raises.%20The%20message%20carries%20the%20reasoning.%0A%0A%20%20%20%20%23%23%202.4%20The%20ERA5%20clip%3A%20relabel%2C%20then%20clip%20by%20mask%0A%0A%20%20%20%20%60clip_to_area%60%20calls%20%60norm_lon%60%20(relabel%20to%20%E2%88%92180%E2%80%A6180%2C%20**no%20sort**)%20and%20then%20%60xr_clip_bbox%60.%20We%20run%20those%20two%20functions%20on%20a%200.25%C2%B0%20grid%2C%20ERA5's%20distribution%20grid%2C%20with%20the%20registry's%20Ghana%20box%20(4%C2%B0%20W%20to%202%C2%B0%20E%2C%20crossing%200%C2%B0).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_4_era5_clip_relabel()%3A%0A%20%20%20%20from%20tf_data_ml_utils.common.grid%20import%20centres_within%2C%20make_consistent_lon_dim%0A%20%20%20%20from%20tf_data_ml_utils.era5_ingest.utils%20import%20find_indices_in_bounds%2C%20norm_lon%2C%20xr_clip_bbox%0A%0A%20%20%20%20show(norm_lon)%0A%20%20%20%20show(find_indices_in_bounds)%0A%20%20%20%20show(centres_within)%0A%20%20%20%20return%20centres_within%2C%20make_consistent_lon_dim%2C%20norm_lon%2C%20xr_clip_bbox%0A%0A%0A%40app.cell%0Adef%20s2_4_era5_clip_relabel_2(make_consistent_lon_dim%2C%20norm_lon%2C%20xr_clip_bbox)%3A%0A%20%20%20%20lon_q%20%3D%20np.arange(0.0%2C%20360.0%2C%200.25)%0A%20%20%20%20lat_q%20%3D%20np.arange(90.0%2C%20-90.01%2C%20-0.25)%0A%20%20%20%20era5_grid%20%3D%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%7B%22t2m%22%3A%20((%22latitude%22%2C%20%22longitude%22)%2C%20np.zeros((lat_q.size%2C%20lon_q.size)%2C%20%22float32%22))%7D%2C%0A%20%20%20%20%20%20%20%20coords%3D%7B%22latitude%22%3A%20lat_q%2C%20%22longitude%22%3A%20lon_q%7D%2C%0A%20%20%20%20)%0A%20%20%20%20ghana%20%3D%20tuple(REGISTRY%5B%22ghana%22%5D)%20%20%23%20%5Blon_min%2C%20lat_min%2C%20lon_max%2C%20lat_max%5D%0A%20%20%20%20clipped%20%3D%20xr_clip_bbox(%0A%20%20%20%20%20%20%20%20norm_lon(era5_grid)%2C%20bbox%3Dghana%2C%20lat_dim%3D%22latitude%22%2C%20lon_dim%3D%22longitude%22%2C%20debug%3DFalse%0A%20%20%20%20)%0A%20%20%20%20print(%22clipped%20sizes%3A%22%2C%20dict(clipped.sizes))%0A%20%20%20%20print(%22longitude%20order%3A%22%2C%20clipped.longitude.values%5B%5B0%2C%201%2C%208%2C%209%2C%2010%2C%20-1%5D%5D.tolist()%2C%20%22...%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22a%20reader's%20sel(longitude%3Dslice(-4%2C%202))%20-%3E%22%2C%20dict(clipped.sel(longitude%3Dslice(-4%2C%202)).sizes)%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22after%20sortby%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-%3E%22%2C%0A%20%20%20%20%20%20%20%20dict(clipped.sortby(%22longitude%22).sel(longitude%3Dslice(-4%2C%202)).sizes)%2C%0A%20%20%20%20)%0A%20%20%20%20std%20%3D%20make_consistent_lon_dim(clipped%2C%20lon_dim%3D%22longitude%22%2C%20convention%3D%22-180%3A180%22%2C%20sortby%3DTrue)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22make_consistent_lon_dim(sortby%3DTrue)%2C%20as%20standardise%20calls%20it%3A%20monotonic%20%3D%22%2C%0A%20%20%20%20%20%20%20%20std.indexes%5B%22longitude%22%5D.is_monotonic_increasing%2C%0A%20%20%20%20)%0A%20%20%20%20return%20clipped%2C%20ghana%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%20clip%20itself%20**copes**.%20%60xr_clip_bbox%60%20defaults%20to%20%60simple_slice%3DFalse%60%2C%20which%20builds%20a%20boolean%20mask%20with%20%60centres_within%60%20and%20selects%20by%20position%20with%20%60isel%60%3B%20a%20mask%20doesn't%20care%20about%20order.%20It%20returns%20all%2025%20longitudes%2C%20in%20the%20order%200%20%E2%80%A6%202%2C%20%E2%88%924%20%E2%80%A6%20%E2%88%920.25.%0A%0A%20%20%20%20The%20hazard%20is%20the%20**next%20reader**.%20A%20label%20slice%20on%20that%20stored%20axis%20returns%200%20longitudes%2C%20silently%2C%20exactly%20as%20in%201.2.%20The%20next%20production%20stage%2C%20%60standardise%60%2C%20calls%20%60make_consistent_lon_dim(...%2C%20sortby%3DTrue)%60%20(%5Bcommon%2Fstandardise.py%20L351%E2%80%93L353%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fcommon%2Fstandardise.py%23L351-L353))%2C%20so%20standardised%20stores%20are%20safe.%20Only%20code%20that%20opens%20the%20raw%20%60era5_ingest%60%20stores%20for%20boxes%20crossing%200%C2%B0%20(civ_ghana%2C%20ghana%2C%20europe%20and%20uk)%20with%20label%20slices%20is%20exposed.%0A%0A%20%20%20%20%23%23%202.5%20The%20raw-fill%20GUARD%3A%20making%20the%20patch%20line%20up%0A%0A%20%20%20%20When%20ARCO's%20Zarr%20lacks%20a%20day%2C%20%60load_raw_day%60%20fetches%20that%20day's%20raw%20NetCDF%20(or%20a%20CDS%20copy%2C%20which%20arrives%20on%20%E2%88%92180%E2%80%A6180)%20and%20%60splice_day%60%20patches%20it%20in.%20The%20patch%20must%20have%20the%20**same%20longitude%20order**%20as%20the%20clipped%20Zarr%2C%20or%20the%20values%20land%20in%20the%20wrong%20columns.%20A%20GUARD%20comment%20records%20the%20step%20that%20ensures%20it%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_5_raw_fill_guard()%3A%0A%20%20%20%20from%20tf_data_ml_utils.era5_ingest.ingest%20import%20load_raw_day%0A%0A%20%20%20%20src%20%3D%20inspect.getsource(load_raw_day).splitlines()%0A%20%20%20%20print(%22%5Cn%22.join(line%20for%20line%20in%20src%20if%20%22GUARD%22%20in%20line%20or%20%22%25%20360%22%20in%20line))%0A%20%20%20%20show(splice_day)%0A%0A%0A%40app.cell%0Adef%20s2_5_raw_fill_guard_2(clipped%2C%20day%2C%20ghana%2C%20norm_lon%2C%20xr_clip_bbox)%3A%0A%20%20%20%20fill_day%20%3D%20TODAY%20-%206%20*%20day%0A%20%20%20%20hours3%20%3D%20pd.date_range(fill_day%20-%20day%2C%20periods%3D72%2C%20freq%3D%22h%22)%0A%20%20%20%20zarr_side%20%3D%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22tp%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22time%22%2C%20%22latitude%22%2C%20%22longitude%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20np.zeros((72%2C%20clipped.sizes%5B%22latitude%22%5D%2C%2025)%2C%20%22float32%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22units%22%3A%20%22m%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20coords%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22time%22%3A%20hours3%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22latitude%22%3A%20clipped.latitude.values%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22longitude%22%3A%20clipped.longitude.values%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20)%0A%20%20%20%20%23%20A%20CDS-style%20day%20for%20the%20Ghana%20box%3A%20longitudes%20-4..2%20ascending%2C%20the%20-180..180%20convention.%0A%20%20%20%20cds_lon%20%3D%20np.arange(-4.0%2C%202.01%2C%200.25)%0A%20%20%20%20cds_day%20%3D%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22tp%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22time%22%2C%20%22latitude%22%2C%20%22longitude%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20np.ones((24%2C%20clipped.sizes%5B%22latitude%22%5D%2C%20cds_lon.size)%2C%20%22float32%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22units%22%3A%20%22m%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20coords%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22time%22%3A%20pd.date_range(fill_day%2C%20periods%3D24%2C%20freq%3D%22h%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22latitude%22%3A%20clipped.latitude.values%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22longitude%22%3A%20cds_lon%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20)%0A%0A%20%20%20%20def%20clip_like_zarr(ds)%3A%0A%20%20%20%20%20%20%20%20return%20xr_clip_bbox(%0A%20%20%20%20%20%20%20%20%20%20%20%20norm_lon(ds)%2C%20bbox%3Dghana%2C%20lat_dim%3D%22latitude%22%2C%20lon_dim%3D%22longitude%22%2C%20debug%3DFalse%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20guarded%20%3D%20clip_like_zarr(%0A%20%20%20%20%20%20%20%20cds_day.assign_coords(longitude%3Dcds_day.longitude%20%25%20360).sortby(%22longitude%22)%0A%20%20%20%20)%0A%20%20%20%20unguarded%20%3D%20clip_like_zarr(cds_day)%0A%20%20%20%20print(%22Zarr%20order%20%20%20%20%3A%22%2C%20zarr_side.longitude.values%5B%5B0%2C%208%2C%209%2C%20-1%5D%5D.tolist())%0A%20%20%20%20print(%22with%20GUARD%20%20%20%20%3A%22%2C%20guarded.longitude.values%5B%5B0%2C%208%2C%209%2C%20-1%5D%5D.tolist())%0A%20%20%20%20print(%22without%20GUARD%20%3A%22%2C%20unguarded.longitude.values%5B%5B0%2C%208%2C%209%2C%20-1%5D%5D.tolist())%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22splice_day%20with%20GUARD%20%20%20%20-%3E%22%2C%0A%20%20%20%20%20%20%20%20%22spliced%22%20if%20splice_day(zarr_side%2C%20guarded%2C%20fill_day)%20is%20not%20None%20else%20%22None%22%2C%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22splice_day%20without%20GUARD%20-%3E%22%2C%0A%20%20%20%20%20%20%20%20%22spliced%22%20if%20splice_day(zarr_side%2C%20unguarded%2C%20fill_day)%20is%20not%20None%20else%20%22None%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%20With%20the%20GUARD%20(%60%25%20360%60%20then%20%60sortby%60)%2C%20the%20patch%20has%20the%20Zarr's%20odd%20order%20and%20%60splice_day%60%20accepts%20it.%20Without%20it%2C%20%60splice_day%60's%20%60np.allclose%60%20check%20on%20the%20longitudes%20fails%2C%20it%20logs%20a%20warning%20and%20returns%20%60None%60%2C%20so%20the%20day%20stays%20unfilled%20rather%20than%20filled%20wrongly.%20Two%20pieces%20of%20code%20cooperate%20here%3A%20the%20GUARD%20makes%20the%20orders%20match%2C%20and%20the%20check%20refuses%20to%20splice%20if%20they%20don't.%0A%0A%20%20%20%20%23%23%202.6%20The%20ECMWF%20subset%3A%20F2%0A%0A%20%20%20%20The%20ECMWF%20ingester%20(%60ecmwf_ingest%60)%20subsets%20its%20forecasts%20differently%2C%20with%20a%20label%20slice%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_6_ecmwf_subset_f2()%3A%0A%20%20%20%20from%20tf_data_ml_utils.ecmwf_ingest.geo%20import%20lon_is_0_360%2C%20lon_to_360%2C%20to_lon_180%0A%20%20%20%20from%20tf_data_ml_utils.ecmwf_ingest.process.pipeline%20import%20subset_lon_lat%0A%0A%20%20%20%20show(subset_lon_lat)%0A%20%20%20%20show(lon_is_0_360)%0A%20%20%20%20show(lon_to_360)%0A%20%20%20%20show(to_lon_180)%0A%20%20%20%20return%20lon_is_0_360%2C%20lon_to_360%2C%20subset_lon_lat%2C%20to_lon_180%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%20Before%20running%20it%2C%20note%20that%20one%20path%20uses%20**three%20bounding-box%20orders**%3A%0A%0A%20%20%20%20%7C%20where%20%7C%20order%20%7C%0A%20%20%20%20%7C---%7C---%7C%0A%20%20%20%20%7C%20data-pipelines%20%60spatial_registry.yaml%60%20and%20ERA5's%20%60SpatialArea.bbox%60%20%7C%20%60%5Blon_min%2C%20lat_min%2C%20lon_max%2C%20lat_max%5D%60%20%7C%0A%20%20%20%20%7C%20%60ecmwf_ingest%60%20%60BBox%60%2C%20%60RegionBbox%60%2C%20the%20CLI's%20%60--bbox%60%20and%20%60subset_lon_lat%60%20%7C%20%60(lon_min%2C%20lon_max%2C%20lat_min%2C%20lat_max)%60%20(%5Bgeo.py%20L9%E2%80%93L16%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fecmwf_ingest%2Fgeo.py%23L9-L16)%2C%20%5Bcli.py%20L232%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fecmwf_ingest%2Fcli.py%23L232))%20%7C%0A%20%20%20%20%7C%20the%20CDS%20%60area%60%20request%20key%20%7C%20%60%5BNorth%2C%20West%2C%20South%2C%20East%5D%60%20(%5Bdownload_seas5.py%20L55%E2%80%93L57%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fecmwf_ingest%2Fingest%2Fdownload_seas5.py%23L55-L57)%2C%20%5Bera5%20ingest.py%20L437%E2%80%93L445%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fera5_ingest%2Fingest.py%23L437-L445))%20%7C%0A%0A%20%20%20%20data-pipelines'%20%60factory._process_argv%60%20does%20the%20registry-to-CLI%20transpose%20(%5Bfactory.py%20L328%E2%80%93L330%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftreefera-data-pipelines%2Fblob%2Fbb0ca14937ec7366c6ad2afa38147d344ee58b6f%2Fsrc%2Ftreefera_pipelines%2Fpipelines%2Fecmwf_forecast_ingest%2Ffactory.py%23L328-L330)).%20The%20loop%20below%20does%20the%20same%20for%20every%20region%20the%20%60seas%60%20product%20(SEAS5%2C%20ECMWF's%20seasonal%20forecast)%20builds%20(%5Bfactory.py%20L79%E2%80%93L97%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftreefera-data-pipelines%2Fblob%2Fbb0ca14937ec7366c6ad2afa38147d344ee58b6f%2Fsrc%2Ftreefera_pipelines%2Fpipelines%2Fecmwf_forecast_ingest%2Ffactory.py%23L79-L97))%2C%20on%20a%201%C2%B0%20grid%20stored%200%E2%80%A6360%20like%20%60g360%60.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_6_ecmwf_subset_f2_2(g360%2C%20lon_to_360%2C%20subset_lon_lat%2C%20to_lon_180)%3A%0A%20%20%20%20SEAS_REGIONS%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22kenya%22%2C%0A%20%20%20%20%20%20%20%20%22tanzania%22%2C%0A%20%20%20%20%20%20%20%20%22zambia%22%2C%0A%20%20%20%20%20%20%20%20%22australia%22%2C%0A%20%20%20%20%20%20%20%20%22canada%22%2C%0A%20%20%20%20%20%20%20%20%22europe%22%2C%0A%20%20%20%20%20%20%20%20%22european_russia%22%2C%0A%20%20%20%20%20%20%20%20%22russia_north_caucasus%22%2C%0A%20%20%20%20%20%20%20%20%22russia%22%2C%0A%20%20%20%20%20%20%20%20%22france%22%2C%0A%20%20%20%20%20%20%20%20%22ukraine%22%2C%0A%20%20%20%20%5D%0A%20%20%20%20print(f%22%7B'region'%3A22s%7D%20%7B'registry%20bbox'%3A28s%7D%20%7B'0..360%20grid'%3A%3E12s%7D%20%7B'to_lon_180%20first'%3A%3E17s%7D%22)%0A%20%20%20%20for%20_region%20in%20SEAS_REGIONS%3A%0A%20%20%20%20%20%20%20%20_lon_min%2C%20_lat_min%2C%20_lon_max%2C%20_lat_max%20%3D%20REGISTRY%5B_region%5D%20%20%23%20registry%20order%0A%20%20%20%20%20%20%20%20cli_bbox%20%3D%20(_lon_min%2C%20_lon_max%2C%20_lat_min%2C%20_lat_max)%20%20%23%20factory._process_argv's%20transpose%0A%20%20%20%20%20%20%20%20raw%20%3D%20subset_lon_lat(g360%2C%20*cli_bbox).sizes%5B%22longitude%22%5D%0A%20%20%20%20%20%20%20%20safe%20%3D%20subset_lon_lat(to_lon_180(g360)%2C%20*cli_bbox).sizes%5B%22longitude%22%5D%0A%20%20%20%20%20%20%20%20flag%20%3D%20%22%20%20%20%3C-%20empty%22%20if%20raw%20%3D%3D%200%20else%20%22%22%0A%20%20%20%20%20%20%20%20print(f%22%7B_region%3A22s%7D%20%7Bstr(REGISTRY%5B_region%5D)%3A28s%7D%20%7Braw%3A%3E12d%7D%20%7Bsafe%3A%3E17d%7D%7Bflag%7D%22)%0A%20%20%20%20print(%22%5Cnfrance%20on%200..360%3A%20lon%20slice%20%3D%22%2C%20slice(lon_to_360(-6.0)%2C%20lon_to_360(10.0)))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Two%20of%20the%20eleven%20%60seas%60%20regions%2C%20**europe%20and%20france**%2C%20cross%200%C2%B0%20and%20get%20**zero%20longitudes**%20on%200%E2%80%A6360%20data.%20The%20other%20nine%20sit%20on%20one%20side%20of%200%C2%B0%20and%20are%20unaffected.%20Putting%20the%20grid%20on%20%E2%88%92180%E2%80%A6180%20first%20with%20%60to_lon_180%60%20gives%20France%20its%2017%20columns%20and%20Europe%2073.%0A%0A%20%20%20%20Latitude%20has%20a%20twin%20of%20this%20trap.%20%60slice(lat_max%2C%20lat_min)%60%20assumes%20latitude%20runs%20north%20to%20south.%20GRIB%20%60regular_ll%60%20grids%20do%2C%20so%20this%20is%20latent%20today%2C%20but%20%60standardise%60%20sorts%20latitude%20ascending%2C%20and%20any%20reuse%20of%20%60subset_lon_lat%60%20on%20standardised%20data%20would%20lose%20every%20latitude%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_6_ecmwf_subset_f2_3(g360%2C%20subset_lon_lat%2C%20to_lon_180)%3A%0A%20%20%20%20ascending%20%3D%20to_lon_180(g360).sortby(%22latitude%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22france%20on%20an%20ascending-latitude%20grid%3A%22%2C%0A%20%20%20%20%20%20%20%20dict(subset_lon_lat(ascending%2C%20-6.0%2C%2010.0%2C%2040.0%2C%2052.0).sizes)%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%23%23%202.7%20F2%20end%20to%20end%3A%20the%20empty%20store%20is%20written%20and%20reported%20as%20success%0A%0A%20%20%20%20Does%20anything%20downstream%20notice%20zero%20longitudes%3F%20We%20run%20the%20**real**%20%60process_run%60%2C%20the%20production%20entry%20point%20that%20subsets%2C%20reconciles%20lead%20times%2C%20adds%20metadata%20and%20writes%20the%20Zarr%20store.%20Only%20one%20thing%20is%20replaced%3A%20the%20processor%20that%20would%20decode%20a%20GRIB%20file%20returns%20a%20synthetic%20SEAS5-shaped%20dataset%20on%200%E2%80%A6360%20instead%20(1%20initialisation%20time%2C%202%20ensemble%20members%2C%2031%20lead%20times%2C%201%C2%B0%20grid).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_7_f2_end_end()%3A%0A%20%20%20%20from%20types%20import%20SimpleNamespace%0A%20%20%20%20from%20unittest%20import%20mock%0A%0A%20%20%20%20from%20tf_data_ml_utils.ecmwf_ingest.geo%20import%20RegionBbox%0A%20%20%20%20from%20tf_data_ml_utils.ecmwf_ingest.process%20import%20runner%0A%20%20%20%20from%20tf_data_ml_utils.ecmwf_ingest.products%20import%20get_product%0A%20%20%20%20from%20upath%20import%20UPath%0A%0A%20%20%20%20show(runner.process_run)%0A%20%20%20%20return%20RegionBbox%2C%20SimpleNamespace%2C%20UPath%2C%20get_product%2C%20mock%2C%20runner%0A%0A%0A%40app.cell%0Adef%20s2_7_f2_end_end_2(%0A%20%20%20%20RegionBbox%2C%0A%20%20%20%20SimpleNamespace%2C%0A%20%20%20%20UPath%2C%0A%20%20%20%20get_product%2C%0A%20%20%20%20lat%2C%0A%20%20%20%20lon360%2C%0A%20%20%20%20mock%2C%0A%20%20%20%20rng%2C%0A%20%20%20%20runner%2C%0A)%3A%0A%20%20%20%20_lead%20%3D%20np.arange(6%2C%204417%2C%206%20*%2024)%20%20%23%20hours%0A%20%20%20%20seas_like%20%3D%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22t2m%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22init_time%22%2C%20%22member%22%2C%20%22leadtime%22%2C%20%22latitude%22%2C%20%22longitude%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rng.normal(288%2C%2010%2C%20(1%2C%202%2C%20_lead.size%2C%20lat.size%2C%20lon360.size)).astype(%22float32%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20coords%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22init_time%22%3A%20%5Bnp.datetime64(%222026-11-01T00%22%2C%20%22ns%22)%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22member%22%3A%20%5B0%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22leadtime%22%3A%20_lead%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22latitude%22%3A%20lat%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22longitude%22%3A%20lon360%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20)%0A%20%20%20%20out_dir%20%3D%20UPath(WORK%20%2F%20%22seas%22)%0A%20%20%20%20out_dir.mkdir(exist_ok%3DTrue)%0A%20%20%20%20fake_strategy%20%3D%20SimpleNamespace(processor%3Dlambda%20run%3A%20%5Bseas_like%5D)%0A%0A%20%20%20%20def%20run_seas(region)%3A%0A%20%20%20%20%20%20%20%20lon_min%2C%20lat_min%2C%20lon_max%2C%20lat_max%20%3D%20REGISTRY%5Bregion%5D%0A%20%20%20%20%20%20%20%20with%20mock.patch.object(runner%2C%20%22strategy_for%22%2C%20lambda%20product_id%3A%20fake_strategy)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20written%20%3D%20runner.process_run(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20get_product(%22seas%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%222026110100%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20out_dir%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22t2m%22%3A%20out_dir%20%2F%20f%22%7Bregion%7D.zarr%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20region%3DRegionBbox(lon_min%2C%20lon_max%2C%20lat_min%2C%20lat_max%2C%20name%3Dregion)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20reprocess%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20return%20written%5B%22t2m%22%5D%0A%0A%20%20%20%20for%20_region%20in%20(%22france%22%2C%20%22kenya%22)%3A%0A%20%20%20%20%20%20%20%20path%2C%20n_written%2C%20n_skipped%20%3D%20run_seas(_region)%0A%20%20%20%20%20%20%20%20sizes%20%3D%20dict(xr.open_zarr(str(path)).sizes)%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B_region%3A7s%7D%20process_run%20returned%20n_written%3D%7Bn_written%7D%2C%20no%20exception%20%7C%20store%20on%20disk%3A%20%7Bsizes%7D%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20(run_seas%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%20France's%20store%20is%20written%20with%20**%60longitude%3A%200%60**%2C%20and%20%60process_run%60%20returns%20one%20init%20time%20written.%20The%20runner's%20only%20checks%20are%20%22were%20any%20variables%20saved%3F%22%20and%20%22did%20any%20variable%20write%20zero%20init%20times%3F%22%20(%5Brunner.py%20L67%E2%80%93L75%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fecmwf_ingest%2Fprocess%2Frunner.py%23L67-L75)).%20Both%20pass%2C%20because%20one%20init%20time%20*was*%20written%3B%20it%20just%20has%20no%20columns.%20Kenya%2C%20for%20contrast%2C%20gets%2013%20longitudes.%0A%0A%20%20%20%20**This%20is%20the%20evidence%20behind%20the%20correction%20to%20F2.**%20The%20backlog%20first%20recorded%20that%20%22zarr%20refuses%20a%20zero-length%20chunk%2C%20and%20%60process_run%60%20raises%20'Zarr%20save%20failed'%22.%20Whether%20that%20happens%20depends%20on%20the%20zarr%20version.%20Run%20through%20%60process_run%60%20with%20this%20same%20synthetic%20dataset%3A%0A%0A%20%20%20%20%7C%20zarr%20%7C%20where%20it%20is%20used%20%7C%20France%20%60seas%60%20run%20%7C%0A%20%20%20%20%7C---%7C---%7C---%7C%0A%20%20%20%20%7C%203.1.5%20%7C%20tf-data-ml-utils'%20lock%2C%20and%20this%20notebook%20%7C%20store%20written%20with%20%60longitude%3A%200%60%2C%20reported%20as%20success%20%7C%0A%20%20%20%20%7C%203.2.1%20%7C%20**treefera-data-pipelines'%20lock%20on%20main%2C%20where%20the%20ingest%20actually%20runs**%20%7C%20store%20written%20with%20%60longitude%3A%200%60%2C%20reported%20as%20success%20%7C%0A%20%20%20%20%7C%203.4.2%20%7C%20a%20later%20release%2C%20in%20neither%20lock%20%7C%20raises%20%22Zarr%20save%20failed%22%20%7C%0A%0A%20%20%20%20So%20in%20production%20F2%20is%20**silent**.%20The%20europe%20and%20france%20%60seas%60%20chains%20don't%20run%20today%20only%20because%20a%20temporary%20pause%2C%20%60_SEAS_FANOUT_REGIONS_TEMP%60%20in%20data-pipelines'%20%60schedules.py%60%2C%20launches%20just%20kenya%2C%20tanzania%20and%20zambia.%20Its%20comment%20says%20to%20revert%20it%20once%20those%20three%20are%20verified.%20Reverting%20it%20as%20written%20would%20publish%20empty%20stores%20for%20europe%20and%20france%20without%20an%20error%2C%20if%20the%20decoded%20SEAS5%20input%20uses%200%E2%80%A6360%20longitudes%20(study%20question%202%20is%20still%20open%3B%20on%20%E2%88%92180%E2%80%A6180%20input%20France%20is%20cut%20correctly).%20The%20synthetic%20result%20above%20stands%20either%20way.%20One%20question%20stays%20open%3A%20does%20a%20real%20SEAS5%20GRIB%20from%20the%20CDS%20arrive%20on%200%E2%80%A6360%20after%20%60cfgrib%60%3F%20Opening%20one%20file%20settles%20it%20(see%20%60science.md%60).%0A%0A%20%20%20%20%23%23%202.8%20The%20guarded%20twin%20that%20never%20runs%0A%0A%20%20%20%20Someone%20saw%20this%20trap%20before.%20%60compute_crop_indices%60%2C%20written%20to%20crop%20GRIB%20files%20before%20decoding%2C%20refuses%20a%20box%20that%20crosses%20the%20grid's%20edge%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_8_guarded_twin_never(RegionBbox)%3A%0A%20%20%20%20from%20tf_data_ml_utils.ecmwf_ingest.process.crop%20import%20GridSpec%2C%20compute_crop_indices%0A%0A%20%20%20%20GLOBAL_1DEG%20%3D%20GridSpec(%0A%20%20%20%20%20%20%20%20ni%3D360%2C%20nj%3D181%2C%20lat_first%3D90.0%2C%20lat_last%3D-90.0%2C%20lon_first%3D0.0%2C%20lon_last%3D359.0%0A%20%20%20%20)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20compute_crop_indices(GLOBAL_1DEG%2C%20RegionBbox(-6.0%2C%2010.0%2C%2040.0%2C%2052.0%2C%20name%3D%22france%22))%0A%20%20%20%20except%20ValueError%20as%20exc%3A%0A%20%20%20%20%20%20%20%20print(%22ValueError%3A%22%2C%20exc)%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%20Its%20test%20says%20why%3A%20*%2210W..10E%20becomes%20350..10%2C%20which%20no%20single%20slice%20can%20express%3B%20silently%20inverting%20it%20would%20return%20an%20empty%20or%20reversed%20crop%20rather%20than%20the%20region%20asked%20for%22*%20(%5Btest_crop.py%20L39%E2%80%93L46%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftests%2Ftest_ecmwf_ingest%2Ftest_crop.py%23L39-L46)).%20But%20its%20only%20caller%2C%20%60crop_s2s_init%60%2C%20has%20no%20callers%20itself.%20The%20guarded%20path%20is%20dead%3B%20the%20live%20path%20has%20no%20guard.%0A%0A%20%20%20%20%23%23%202.9%20%5Bnet%5D%20The%20live%20ARCO-ERA5%20store%0A%0A%20%20%20%20This%20cell%20reads%20the%20real%20store's%20attributes%2C%20checks%20whether%20each%20hourly%20chunk%20object%20of%20%602m_temperature%60%20exists%20for%20the%20ten%20days%20up%20to%20%60valid_time_stop_era5t%60%20and%20the%20day%20after%20(264%20small%20requests)%2C%20and%20asks%20for%20the%20size%20of%20one%20object.%20It%20reads%20a%20few%20kilobytes.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_9_net_live_arco(day)%3A%0A%20%20%20%20import%20json%0A%20%20%20%20from%20concurrent.futures%20import%20ThreadPoolExecutor%0A%0A%20%20%20%20live%20%3D%20%7B%7D%0A%20%20%20%20if%20not%20NET%3A%0A%20%20%20%20%20%20%20%20print(%22skipped%20(offline)%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20import%20gcsfs%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20fs%20%3D%20gcsfs.GCSFileSystem(token%3D%22anon%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20attrs%20%3D%20json.loads(fs.cat(f%22%7BARCO%7D%2F.zattrs%22))%0A%20%20%20%20%20%20%20%20%20%20%20%20live%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20k%3A%20attrs%5Bk%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20k%20in%20(%22valid_time_start%22%2C%20%22valid_time_stop%22%2C%20%22valid_time_stop_era5t%22)%0A%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%20print(live)%0A%20%20%20%20%20%20%20%20%20%20%20%20za%20%3D%20json.loads(fs.cat(f%22%7BARCO%7D%2F2m_temperature%2F.zarray%22))%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%20%222m_temperature%20chunks%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20za%5B%22chunks%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%7C%20zarr_format%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20za%5B%22zarr_format%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%7C%20fill_value%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20za%5B%22fill_value%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20def%20hour_index(ts)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20int((pd.Timestamp(ts)%20-%20pd.Timestamp(%221900-01-01%22)).total_seconds()%20%2F%2F%203600)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20era5t_day%20%3D%20pd.Timestamp(live%5B%22valid_time_stop_era5t%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20start%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20era5t_day%20-%209%20*%20day%0A%20%20%20%20%20%20%20%20%20%20%20%20)%20%20%23%20ten%20days%20up%20to%20and%20including%20the%20attribute's%20day%2C%20plus%20the%20day%20after%0A%20%20%20%20%20%20%20%20%20%20%20%20keys%20%3D%20%5Bf%22%7BARCO%7D%2F2m_temperature%2F%7Bhour_index(start)%20%2B%20i%7D.0.0%22%20for%20i%20in%20range(11%20*%2024)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20with%20ThreadPoolExecutor(16)%20as%20pool%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20exists%20%3D%20list(pool.map(fs.exists%2C%20keys))%0A%20%20%20%20%20%20%20%20%20%20%20%20mask%20%3D%20%22%22.join(%22%23%22%20if%20e%20else%20%22.%22%20for%20e%20in%20exists)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20d%20in%20range(11)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20note%20%3D%20%22%20%20%3C-%20valid_time_stop_era5t%22%20if%20start%20%2B%20d%20*%20day%20%3D%3D%20era5t_day%20else%20%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%7B(start%20%2B%20d%20*%20day).date()%7D%20%20%7Bmask%5B24%20*%20d%20%3A%2024%20*%20d%20%2B%2024%5D%7D%7Bnote%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20size%20%3D%20fs.info(f%22%7BARCO%7D%2Ftotal_precipitation%2F%7Bhour_index('2026-01-01')%7D.0.0%22)%5B%22size%22%5D%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%22one%20whole-globe%20total_precipitation%20hour%2C%20compressed%3A%20%7Bsize%3A%2C%7D%20bytes%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22(decoded%3A%20%7B721%20*%201440%20*%204%3A%2C%7D%20bytes)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20except%20Exception%20as%20exc%3A%20%20%23%20a%20network%20problem%20should%20not%20stop%20the%20notebook%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22skipped%20(network%20error)%3A%22%2C%20type(exc).__name__%2C%20exc)%0A%20%20%20%20return%20(live%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%60%23%60%20is%20an%20hour%20whose%20object%20exists%20and%20%60.%60%20one%20that%20doesn't.%20If%20you%20ran%20this%20with%20a%20network%2C%20compare%20the%20mask%20with%20%60valid_time_stop_era5t%60%3A%20on%202026-11-18%20the%20attribute%20said%202026-11-12%2C%20yet%20the%20Zarr%20still%20lacked%20all%20of%204%20November%20and%20all%20of%209%20November%2C%20while%20ARCO's%20raw%20NetCDF%20files%20for%20both%20days%20existed.%20The%20attribute%20is%20not%20a%20clean%20edge%2C%20and%20holes%20recur.%20Our%20ingest%20heals%20them%20from%20the%20raw%20files%2C%20not%20by%20waiting%20for%20ARCO's%20Zarr.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202.10%20Forecast%20time%3A%20de-accumulating%20rain%20and%20choosing%20its%20day%0A%0A%20%20%20%20ECMWF%20delivers%20rain%20as%20a%20total%20**since%20initialisation**.%20Standardise%20turns%20it%20into%20per-step%20rain%20with%20%60deaccumulate_along_leadtime%60%2C%20then%20sums%20the%20steps%20of%20each%20calendar%20day%20with%20%60process_single_init_time%60%2C%20which%20files%20every%20step%20under%20the%20day%20its%20valid%20time%20(the%20step's%20*end*)%20falls%20on.%20Here%20is%20one%206-hourly%20forecast%2C%20initialised%20at%2000%20UTC%20on%201%20June%2C%20in%20which%202%20mm%20falls%20at%2008%3A00%20and%205%20mm%20at%2022%3A00%20the%20same%20day.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_10_forecast_time_de()%3A%0A%20%20%20%20from%20tf_data_ml_utils.common.standardise%20import%20(%0A%20%20%20%20%20%20%20%20deaccumulate_along_leadtime%2C%0A%20%20%20%20%20%20%20%20process_single_init_time%2C%0A%20%20%20%20%20%20%20%20resample_forecast_to_daily%2C%0A%20%20%20%20)%0A%0A%20%20%20%20show(deaccumulate_along_leadtime)%0A%20%20%20%20show(process_single_init_time)%0A%0A%20%20%20%20_lead%20%3D%20np.arange(0%2C%2049%2C%206)%20%20%23%20hours%20after%2000%20UTC%20on%201%20June%0A%20%20%20%20step_rain%20%3D%20np.zeros(_lead.size)%0A%20%20%20%20step_rain%5B_lead%20%3D%3D%2012%5D%20%3D%202.0%20%20%23%20rain%20in%20the%2006%3A00-12%3A00%20step%20(it%20fell%20at%2008%3A00)%0A%20%20%20%20step_rain%5B_lead%20%3D%3D%2024%5D%20%3D%205.0%20%20%23%20rain%20in%20the%2018%3A00-24%3A00%20step%20(it%20fell%20at%2022%3A00)%0A%0A%20%20%20%20def%20forecast(values)%3A%0A%20%20%20%20%20%20%20%20return%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22pr%22%3A%20((%22init_time%22%2C%20%22leadtime%22)%2C%20values%5BNone%2C%20%3A%5D)%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20coords%3D%7B%22init_time%22%3A%20%5Bnp.datetime64(%222024-06-01T00%22%2C%20%22ns%22)%5D%2C%20%22leadtime%22%3A%20_lead%7D%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20cumulative%20%3D%20forecast(%0A%20%20%20%20%20%20%20%20np.cumsum(step_rain)%0A%20%20%20%20)%20%20%23%20how%20ECMWF%20delivers%20tp%3A%20the%20total%20since%20initialisation%0A%20%20%20%20print(%22cumulative%20since%20init%20%3A%22%2C%20np.cumsum(step_rain).tolist())%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22after%20de-accumulation%20%3A%22%2C%0A%20%20%20%20%20%20%20%20deaccumulate_along_leadtime(cumulative%5B%22pr%22%5D%2C%20%22leadtime%22).values.ravel().tolist()%2C%0A%20%20%20%20)%0A%20%20%20%20for%20mode%2C%20ds%20in%20((%22cumulative%22%2C%20cumulative)%2C%20(%22incremental%22%2C%20forecast(step_rain)))%3A%0A%20%20%20%20%20%20%20%20_daily%20%3D%20resample_forecast_to_daily(ds%2C%20accumulation%3Dmode)%0A%20%20%20%20%20%20%20%20days%20%3D%20%5Bstr(v)%5B%3A10%5D%20for%20v%20in%20_daily%5B%22valid_time%22%5D.values.ravel()%5D%0A%20%20%20%20%20%20%20%20totals%20%3D%20%22%2C%20%22.join(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bd%7D%3A%20%7Bv%7D%22%20for%20d%2C%20v%20in%20zip(days%2C%20_daily%5B%22pr%22%5D.values.ravel()%2C%20strict%3DTrue)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20print(f%22daily%20rain%2C%20%7Bmode%3A%3E11%7D%20input%3A%20%7Btotals%7D%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%20Read%20the%20two%20daily%20lines%20together%3A%0A%0A%20%20%20%20-%20**Production's%20path%20(%60cumulative%60%2C%20which%20every%20ECMWF%20product%20uses)%20loses%20the%20initialisation%20day.**%20The%20first%20step%20has%20no%20earlier%20total%20to%20subtract%2C%20so%20de-accumulation%20leaves%20a%20NaN%20there%2C%20and%20the%20cumulative%20path%20sums%20days%20without%20skipping%20NaNs.%20The%202%20mm%20that%20fell%20at%2008%3A00%20on%201%20June%20has%20gone.%20Given%20the%20same%20rain%20as%20per-step%20increments%20(%60incremental%60)%2C%201%20June%20keeps%20it.%0A%20%20%20%20-%20**The%2022%3A00%20rain%20is%20counted%20on%202%20June.**%20Its%20step%20ends%20at%2000%3A00%20on%202%20June%2C%20and%20steps%20are%20filed%20by%20their%20end.%20So%20a%20forecast%20%22day%22%20runs%20from%2018%3A00%20to%2018%3A00%2C%20while%20ERA5's%20runs%20from%2023%3A00%20to%2023%3A00.%20This%20is%20logged%20as%20%5Bissue%2052%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F52).%20%5Bscience.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.3-weather-grids%2Fscience.md)%20(study%20question%203)%20asks%20what%20it%20does%20once%20forecasts%20are%20joined%20onto%20observations.%0A%0A%20%20%20%20%23%23%202.11%20Joining%20forecasts%20to%20observations%3A%20the%20stitch%0A%0A%20%20%20%20A%20season-to-date%20feature%20needs%20one%20unbroken%20daily%20record%3A%20observations%2C%20then%20forecasts.%20%60extend_time_series%60%20appends%20a%20product's%20days%20after%20the%20record%20ends%2C%20and%20refuses%20a%20gap.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_11_joining_forecasts_observations()%3A%0A%20%20%20%20from%20tf_data_ml_utils.features.stages.stitch.stitch%20import%20extend_time_series%0A%0A%20%20%20%20show(extend_time_series)%0A%0A%20%20%20%20def%20daily_record(start%2C%20n_days%2C%20value)%3A%0A%20%20%20%20%20%20%20%20time%20%3D%20pd.date_range(start%2C%20periods%3Dn_days%2C%20freq%3D%22D%22)%0A%20%20%20%20%20%20%20%20return%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22pr%22%3A%20((%22time%22%2C%20%22latitude%22%2C%20%22longitude%22)%2C%20np.full((n_days%2C%201%2C%201)%2C%20value))%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20coords%3D%7B%22time%22%3A%20time%2C%20%22latitude%22%3A%20%5B0.0%5D%2C%20%22longitude%22%3A%20%5B10.0%5D%7D%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20observed%20%3D%20daily_record(%222024-06-01%22%2C%205%2C%201.0)%20%20%23%201-5%20June%2C%201%20mm%20a%20day%0A%20%20%20%20joined%20%3D%20extend_time_series(%0A%20%20%20%20%20%20%20%20observed%2C%20daily_record(%222024-06-06%22%2C%203%2C%209.0)%0A%20%20%20%20)%20%20%23%20a%20forecast%20from%206%20June%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22joined%3A%22%2C%0A%20%20%20%20%20%20%20%20joined%5B%22pr%22%5D.values.ravel().tolist()%2C%0A%20%20%20%20%20%20%20%20%22%7C%20forecast%20starts%22%2C%0A%20%20%20%20%20%20%20%20joined.attrs%5B%22extension_start_time%22%5D%5B%3A10%5D%2C%0A%20%20%20%20)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20extend_time_series(observed%2C%20daily_record(%222024-06-08%22%2C%203%2C%209.0))%20%20%23%20a%20forecast%20from%208%20June%0A%20%20%20%20except%20ValueError%20as%20err%3A%0A%20%20%20%20%20%20%20%20print(%22refused%3A%22%2C%20str(err).split(%22.%20%22)%5B0%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%20stitch%20never%20invents%20a%20day%3A%20two%20missing%20days%20are%20an%20error%2C%20not%20an%20interpolation.%20Production's%20cascade%20(%60stitch_cascade%60)%20tries%20observations%2C%20a%20gap-filler%2C%20then%20ENS%2C%20S2S%20and%20SEAS5%2C%20and%20%60_hole_free_init_time%60%20(in%20%5Bcode.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.3-weather-grids%2Fcode.md))%20picks%20the%20newest%20forecast%20whose%20first%20day%20is%20no%20later%20than%20the%20day%20after%20the%20record%20ends.%20With%202.10%20in%20mind%2C%20that%20first%20day%20can%20be%20a%20forecast's%20initialisation%20day%2C%20the%20one%20with%20no%20rain%20total.%0A%0A%20%20%20%20%23%23%202.12%20CHIRPS%3A%20pentads%20and%20vintages%0A%0A%20%20%20%20CHIRPS%20publishes%20rainfall%20in%20pentads%2C%20six%20per%20month.%20The%20sixth%20is%20whatever%20is%20left%20of%20the%20month%2C%20so%20a%20pentad%20total%20is%20not%20five%20days%20of%20rain.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_12_chirps_pentads_vintages()%3A%0A%20%20%20%20from%20datetime%20import%20date%0A%0A%20%20%20%20from%20tf_data_ml_utils.chirps_ingest.ingest%20import%20plan%0A%20%20%20%20from%20tf_data_ml_utils.chirps_ingest.products%20import%20Product%2C%20pentad_length%0A%0A%20%20%20%20show(pentad_length)%0A%20%20%20%20for%20sixth%20in%20(%0A%20%20%20%20%20%20%20%20date(2023%2C%202%2C%2026)%2C%0A%20%20%20%20%20%20%20%20date(2024%2C%202%2C%2026)%2C%0A%20%20%20%20%20%20%20%20date(2024%2C%204%2C%2026)%2C%0A%20%20%20%20%20%20%20%20date(2024%2C%201%2C%2026)%2C%0A%20%20%20%20)%3A%20%20%23%20not%20%60day%60%3A%20Part%203%20uses%20that%20name%0A%20%20%20%20%20%20%20%20print(f%22sixth%20pentad%20of%20%7Bsixth%3A%25B%20%25Y%7D%3A%20%7Bpentad_length(sixth)%7D%20days%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22a%20pentad%20product's%20units%3A%22%2C%0A%20%20%20%20%20%20%20%20Product(%22pentad%22%2C%20None).units%2C%0A%20%20%20%20%20%20%20%20%22%7C%20a%20daily%20product's%3A%22%2C%0A%20%20%20%20%20%20%20%20Product(%22daily%22%2C%20%22rnl%22).units%2C%0A%20%20%20%20)%0A%20%20%20%20sixth_feb_2023%20%3D%2030.0%20%20%23%20mm%20in%20the%20sixth%20pentad%20of%20February%202023%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%2230%20mm%20in%20that%20pentad%20is%20%7Bsixth_feb_2023%20%2F%20pentad_length(date(2023%2C%202%2C%2026))%3A.1f%7D%20mm%20a%20day%2C%20not%20%7Bsixth_feb_2023%20%2F%205%3A.1f%7D%22%0A%20%20%20%20)%0A%0A%20%20%20%20show(plan)%0A%20%20%20%20held%20%3D%20%7B%0A%20%20%20%20%20%20%20%20date(2024%2C%206%2C%201)%3A%20%22final%22%2C%0A%20%20%20%20%20%20%20%20date(2024%2C%206%2C%206)%3A%20%22preliminary%22%2C%0A%20%20%20%20%7D%20%20%23%20what%20the%20store%20already%20holds%0A%20%20%20%20print(plan(%5Bdate(2024%2C%206%2C%201)%2C%20date(2024%2C%206%2C%206)%2C%20date(2024%2C%206%2C%2011)%5D%2C%20vintage%3D%22final%22%2C%20held%3Dheld))%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%60plan%60%20sorts%20a%20run's%20dates%3A%2011%20June%20is%20new%20(append)%2C%206%20June%20holds%20a%20preliminary%20value%20that%20the%20final%20one%20replaces%20(revise)%2C%20and%201%20June%20is%20already%20final%20(leave%20alone).%20A%20preliminary%20value%20never%20overwrites%20a%20final%20one.%0A%0A%20%20%20%20%23%23%202.13%20Ensembles%3A%20members%2C%20the%20control%20and%20hindcasts%0A%0A%20%20%20%20SEAS5%20runs%2051%20members.%20%60merge_pf_cf%60%20puts%20the%20control%20first%20as%20member%200%3B%20%60reduce_ensemble%60%20collapses%20the%20members%20to%20one%20trajectory%3B%20%60hindcast_years%60%20chooses%20the%2020%20past%20years%20a%20hindcast%20reruns.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_13_ensembles_members_control()%3A%0A%20%20%20%20from%20datetime%20import%20datetime%0A%0A%20%20%20%20from%20tf_data_ml_utils.ecmwf_ingest.hindcast%20import%20hindcast_years%0A%20%20%20%20from%20tf_data_ml_utils.ecmwf_ingest.process.pipeline%20import%20merge_pf_cf%0A%0A%20%20%20%20show(merge_pf_cf)%0A%20%20%20%20show(reduce_ensemble)%0A%20%20%20%20show(hindcast_years)%0A%20%20%20%20years%20%3D%20hindcast_years(datetime(2024%2C%202%2C%2029))%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22%7Blen(years)%7D%20hindcast%20dates%20for%20a%2029%20February%20start%2C%20first%20three%3A%22%2C%0A%20%20%20%20%20%20%20%20%5Bf%22%7Bd%3A%25Y-%25m-%25d%7D%22%20for%20d%20in%20years%5B%3A3%5D%5D%2C%0A%20%20%20%20)%0A%0A%20%20%20%20rng_1%20%3D%20np.random.default_rng(13)%0A%20%20%20%20members%20%3D%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22tasmax%22%3A%20((%22member%22%2C%20%22time%22)%2C%2031.5%20%2B%20rng_1.normal(0%2C%202.0%2C%20(51%2C%2030)))%0A%20%20%20%20%20%20%20%20%7D%2C%20%20%23%2051%20members%20x%2030%20days%2C%20around%2031.5%20C%0A%20%20%20%20%20%20%20%20coords%3D%7B%22member%22%3A%20np.arange(51)%2C%20%22time%22%3A%20pd.date_range(%222024-07-01%22%2C%20periods%3D30)%7D%2C%0A%20%20%20%20)%0A%20%20%20%20expected%20%3D%20float((members%5B%22tasmax%22%5D%20%3E%2033).mean(%22member%22).sum())%0A%20%20%20%20print(f%22expected%20hot%20days%20(%3E%2033%20C)%20from%20the%20members%3A%20%7Bexpected%3A.1f%7D%22)%0A%20%20%20%20for%20statistic%20in%20(%22mean%22%2C%20%22median%22%2C%20%22control%22)%3A%0A%20%20%20%20%20%20%20%20hot%20%3D%20int((reduce_ensemble(members%2C%20statistic)%5B%22tasmax%22%5D%20%3E%2033).sum())%0A%20%20%20%20%20%20%20%20print(f%22hot%20days%20of%20the%20collapsed%20ensemble%20(%7Bstatistic%3A%3E7%7D)%3A%20%7Bhot%7D%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%20The%20members%20expect%20about%20six%20hot%20days%20in%20the%20month%2C%20yet%20their%20mean%20is%20never%20above%2033%20%C2%B0C%3A%20averaging%20cancels%20the%20hot%20and%20cool%20members%20before%20the%20threshold%20sees%20them.%20The%20control%20is%20one%20member%2C%20so%20it%20can%20land%20anywhere.%20A%20threshold%20index%20has%20to%20be%20computed%20**per%20member**%20and%20then%20averaged%2C%20which%20is%20why%20%60features-stitch%60%20keeps%20the%20members%20rather%20than%20collapsing%20them%20early.%0A%0A%20%20%20%20A%20hindcast%20reruns%20today's%20model%20on%20the%20same%20date%20in%20each%20of%20the%20past%2020%20years%20(a%2029%20February%20start%20falls%20back%20to%20the%2028th%20in%20non-leap%20years).%20That%20is%20what%20a%20bias%20correction%20would%20be%20fitted%20on.%20We%20ingest%20SEAS5's%20hindcast%2C%20but%20nothing%20uses%20it%20yet%3A%20forecast%20z-scores%20are%20computed%20against%20ERA5's%20climatology%20and%20labelled%20%60bias_corrected%3A%20False%60%20(%5Bcode.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.3-weather-grids%2Fcode.md)).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Part%203%3A%20the%20science%0A%0A%20%20%20%20%23%23%203.1%20Two%20conventions%2C%20and%20the%20box%20neither%20suits%0A%0A%20%20%20%20Longitude%20has%20two%20common%20conventions.%20**%E2%88%92180%E2%80%A6180**%20puts%20Greenwich%20(0%C2%B0)%20in%20the%20middle%3B%20**0%E2%80%A6360**%20puts%20it%20at%20the%20edge%20and%20is%20the%20native%20order%20of%20most%20ECMWF%20GRIB%20grids.%20A%20box%20is%20easy%20to%20cut%20only%20in%20a%20convention%20where%20it%20is%20**contiguous**%2C%20one%20unbroken%20range.%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%200%E2%80%A6360%3A%20%20%20%20%200%20%E2%94%80%E2%94%80%20France%20E%20%E2%94%80%E2%94%80%2010%20%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%20180%20%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%20354%20%E2%94%80%E2%94%80%20France%20W%20%E2%94%80%E2%94%80%20360%0A%20%20%20%20%E2%88%92180%E2%80%A6180%3A%20%E2%88%92180%20%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%20%E2%88%926%20%E2%94%80%E2%94%80%20France%20%E2%94%80%E2%94%80%2010%20%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%C2%B7%20170%20%E2%94%80%E2%94%80%20Chukotka%20W%20%E2%94%80%E2%94%80%20180%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(Chukotka%20E%20wraps%20to%20%E2%88%92180)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20France%20crosses%200%C2%B0%2C%20so%20it%20is%20whole%20only%20in%20%E2%88%92180%E2%80%A6180.%20A%20box%20crossing%20180%C2%B0%20(eastern%20Russia's%20Chukotka%2C%20Fiji%2C%20the%20Aleutians)%20is%20the%20mirror%20case%3A%20whole%20only%20in%200%E2%80%A6360.%20**Neither%20convention%20is%20safe%20for%20every%20box**%3A%20pick%20the%20one%20in%20which%20your%20box%20is%20contiguous%2C%20or%20split%20the%20box.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_1_two_conventions_box(to_lon_180)%3A%0A%20%20%20%20boxes%20%3D%20%7B%22france%20(crosses%200%C2%B0)%22%3A%20(-6.0%2C%2010.0)%2C%20%22Chukotka-like%20(crosses%20180%C2%B0)%22%3A%20(170.0%2C%20-170.0)%7D%0A%20%20%20%20fig%2C%20axes%20%3D%20plt.subplots(2%2C%201%2C%20figsize%3D(10%2C%203.2)%2C%20constrained_layout%3DTrue)%0A%20%20%20%20for%20ax%2C%20(_name%2C%20lo%2C%20hi)%20in%20zip(axes%2C%20((%220%E2%80%A6360%22%2C%200%2C%20360)%2C%20(%22%E2%88%92180%E2%80%A6180%22%2C%20-180%2C%20180))%2C%20strict%3DTrue)%3A%0A%20%20%20%20%20%20%20%20ax.set_xlim(lo%2C%20hi)%0A%20%20%20%20%20%20%20%20ax.set_ylim(0%2C%201)%0A%20%20%20%20%20%20%20%20ax.set_yticks(%5B%5D)%0A%20%20%20%20%20%20%20%20ax.set_title(f%22%7B_name%7D%20axis%22%2C%20loc%3D%22left%22%2C%20fontsize%3D10)%0A%20%20%20%20%20%20%20%20for%20(label%2C%20(w%2C%20e))%2C%20colour%20in%20zip(boxes.items()%2C%20(%22tab%3Ablue%22%2C%20%22tab%3Aorange%22)%2C%20strict%3DTrue)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20lo%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20w%2C%20e%20%3D%20w%20%25%20360%2C%20e%20%25%20360%0A%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20w%2C%20e%20%3D%20((w%20%2B%20180)%20%25%20360)%20-%20180%2C%20((e%20%2B%20180)%20%25%20360)%20-%20180%0A%20%20%20%20%20%20%20%20%20%20%20%20pieces%20%3D%20%5B(w%2C%20e)%5D%20if%20w%20%3C%20e%20else%20%5B(w%2C%20hi)%2C%20(lo%2C%20e)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20a%2C%20b%20in%20pieces%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ax.axvspan(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20a%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20b%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%200.2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%200.8%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20color%3Dcolour%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20alpha%3D0.6%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20label%3Df%22%7Blabel%7D%3A%20%7Blen(pieces)%7D%20piece(s)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20handles%2C%20labels%20%3D%20ax.get_legend_handles_labels()%0A%20%20%20%20%20%20%20%20by_label%20%3D%20dict(%0A%20%20%20%20%20%20%20%20%20%20%20%20zip(labels%2C%20handles%2C%20strict%3DTrue)%0A%20%20%20%20%20%20%20%20)%20%20%23%20one%20legend%20entry%20per%20box%2C%20not%20per%20piece%0A%20%20%20%20%20%20%20%20ax.legend(by_label.values()%2C%20by_label.keys()%2C%20loc%3D%22upper%20center%22%2C%20fontsize%3D8%2C%20ncol%3D2)%0A%20%20%20%20plt.show()%0A%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22to_lon_180%20on%20%5B179%2C%20180%2C%20181%5D%3A%22%2C%0A%20%20%20%20%20%20%20%20to_lon_180(%0A%20%20%20%20%20%20%20%20%20%20%20%20xr.Dataset(coords%3D%7B%22longitude%22%3A%20%5B179.0%2C%20180.0%2C%20181.0%5D%7D)%0A%20%20%20%20%20%20%20%20).longitude.values.tolist()%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22registry%20russia%20box%3A%22%2C%20REGISTRY%5B%22russia%22%5D%2C%20%22%3C-%20ends%20at%20179.75%2C%20ERA5's%20last%200.25%C2%B0%20column%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%20Production%20already%20works%20round%20the%20180%C2%B0%20edge%3A%20the%20registry's%20%60russia%60%20box%20ends%20at%20179.75%20because%20%22180.0%20fails%20the%20incremental%20edge%20check%22%20(%5Bspatial_registry.yaml%20L21%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftreefera-data-pipelines%2Fblob%2Fbb0ca14937ec7366c6ad2afa38147d344ee58b6f%2Fsrc%2Ftreefera_pipelines%2Fspatial_registry.yaml%23L21)).%20And%20%60to_lon_180%60%20turns%20180%20into%20%E2%88%92180%2C%20so%20a%20column%20exactly%20on%20the%20antimeridian%20changes%20side.%0A%0A%20%20%20%20%23%23%203.2%20ERA5%20accumulations%2C%20and%20why%201%20January%201940%20starts%20at%2007%3A00%0A%0A%20%20%20%20ERA5%20is%20a%20**reanalysis**%3A%20one%20fixed%20version%20of%20ECMWF's%20forecast%20model%20(IFS%20cycle%2041r2)%20combined%20with%20past%20observations%20by%204D-Var%20data%20assimilation%2C%20rerun%20over%201940%20to%20the%20present%20(Hersbach%20et%20al.%2C%202020).%20Temperature%20is%20an%20*analysis*%3A%20the%20model%20state%20fitted%20to%20observations%20at%20that%20hour.%20Precipitation%20is%20not%20observed%20at%20all.%20It%20is%20**output%20of%20short%20forecasts**%2C%20started%20from%20the%2006%20and%2018%20UTC%20analyses%2C%20and%20the%20hourly%20value%20stamped%20*H*%20is%20the%20total%20over%20the%20hour%20ending%20at%20*H*%20(%5BERA5%20data%20documentation%2C%20%22Mean%20rates%2Ffluxes%20and%20accumulations%22%5D(https%3A%2F%2Fconfluence.ecmwf.int%2Fdisplay%2FCKB%2FERA5%253A%2Bdata%2Bdocumentation%23ERA5%3Adatadocumentation-Meanrates%2Ffluxesandaccumulations)).%20The%20CDS%20serves%20steps%201%20to%2012%20of%20each%20forecast%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20forecast%20from%2006%20UTC%20%20%E2%86%92%20stamps%2007%2C%2008%2C%20%E2%80%A6%2C%2018%0A%20%20%20%20forecast%20from%2018%20UTC%20%20%E2%86%92%20stamps%2019%2C%2020%2C%20%E2%80%A6%2C%2023%2C%2000%2C%20%E2%80%A6%2C%2006%20(next%20day)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20So%20the%20values%20stamped%2000%E2%80%9306%20on%201%20January%201940%20come%20from%20a%20forecast%20started%20at%2018%20UTC%20on%2031%20December%201939%2C%20which%20ERA5%20doesn't%20have.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_2_era5_accumulations_1(count_valid_samples%2C%20day)%3A%0A%20%20%20%20stamps%20%3D%20pd.date_range(%221940-01-01T00%22%2C%20%221940-01-02T00%22%2C%20freq%3D%22h%22)%0A%0A%20%20%20%20def%20source_forecast(stamp)%3A%0A%20%20%20%20%20%20%20%20%23%20The%20ERA5%20short%20forecast%20whose%20steps%201..12%20produce%20this%20hourly%20accumulation.%0A%20%20%20%20%20%20%20%20for%20start_hour%20in%20(18%2C%206)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20start%20%3D%20stamp.floor(%22D%22)%20%2B%20pd.Timedelta(hours%3Dstart_hour)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20start%20%3E%3D%20stamp%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20start%20-%3D%20day%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20pd.Timedelta(hours%3D1)%20%3C%3D%20stamp%20-%20start%20%3C%3D%20pd.Timedelta(hours%3D12)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20start%0A%20%20%20%20%20%20%20%20raise%20ValueError(stamp)%0A%0A%20%20%20%20era5_start%20%3D%20pd.Timestamp(%221940-01-01T00%22)%0A%20%20%20%20src_table%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%22stamp%22%3A%20stamps%2C%20%22forecast%20start%22%3A%20%5Bsource_forecast(s)%20for%20s%20in%20stamps%5D%7D%0A%20%20%20%20)%0A%20%20%20%20src_table%5B%22exists%20in%20ERA5%3F%22%5D%20%3D%20src_table%5B%22forecast%20start%22%5D%20%3E%3D%20era5_start%0A%20%20%20%20print(src_table.iloc%5B%5B0%2C%206%2C%207%2C%2018%2C%2019%2C%2024%5D%5D.to_string(index%3DFalse))%0A%0A%20%20%20%20tp_1940%20%3D%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22total_precipitation%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22time%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20np.where(src_table%5B%22exists%20in%20ERA5%3F%22%5D%2C%201e-4%2C%20np.nan).astype(%22float32%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20coords%3D%7B%22time%22%3A%20stamps%7D%2C%0A%20%20%20%20)%0A%20%20%20%20n_valid%20%3D%20count_valid_samples(tp_1940.isel(time%3Dslice(0%2C%2024))%2C%20%22D%22)%5B%0A%20%20%20%20%20%20%20%20%22total_precipitation_n_valid%22%0A%20%20%20%20%5D.values%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22%5Cnvalid%20hours%20on%201940-01-01%3A%22%2C%0A%20%20%20%20%20%20%20%20int(n_valid%5B0%5D)%2C%0A%20%20%20%20%20%20%20%20%22of%2024%20-%3E%20keep_complete_periods%20would%20hold%20the%20day%20back%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%20Stamps%2000%E2%80%9306%20have%20no%20source%20forecast%3B%20from%2007%20they%20come%20from%20the%2006%20UTC%20forecast%20of%201%20January.%20So%201%20January%201940%20has%2017%20of%2024%20valid%20hours%2C%20and%20an%20ingest%20starting%20there%20would%20keep%20**zero**%20days%20from%20its%20first%20batch%3A%20the%20gate%20holds%20day%20one%20back%2C%20and%20so%20every%20later%20day%20too%2C%20because%20it%20keeps%20only%20the%20*leading*%20run.%20The%20raw%20files%20have%20the%20same%20NaN%2C%20so%20the%20fill%20can't%20rescue%20it.%20This%20is%20latent%3A%20data-pipelines%20starts%20ERA5%20at%201980-01-01.%0A%0A%20%20%20%20**Which%20hours%20make%20a%20day%3F**%20Our%20%60resample_to_frequency%60%20sums%20stamps%2000%E2%80%A623%20of%20day%20*D*.%20Each%20stamp%20covers%20the%20hour%20ending%20at%20it%2C%20so%20that%20window%20is%20**23%3A00%20on%20*D*%20%E2%88%92%201%20to%2023%3A00%20on%20*D***.%20ECMWF's%20guidance%20for%20a%20daily%20total%20is%20the%20other%20natural%20choice%2C%20stamps%2001%20on%20*D*%20to%2000%20on%20*D*%20%2B%201%2C%20which%20covers%20midnight%20to%20midnight.%20A%20shower%20between%2023%3A00%20and%20midnight%20is%20the%20difference%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_2_era5_accumulations_1_2(resample_to_frequency)%3A%0A%20%20%20%20two_days%20%3D%20pd.date_range(%222024-06-01T00%22%2C%20%222024-06-03T00%22%2C%20freq%3D%22h%22)%0A%20%20%20%20rain%20%3D%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%7B%22total_precipitation%22%3A%20(%22time%22%2C%20np.zeros(two_days.size%2C%20%22float32%22))%7D%2C%0A%20%20%20%20%20%20%20%20coords%3D%7B%22time%22%3A%20two_days%7D%2C%0A%20%20%20%20)%0A%20%20%20%20rain%5B%22total_precipitation%22%5D.loc%5B%222024-06-02T00%22%5D%20%3D%20(%0A%20%20%20%20%20%20%20%200.005%20%20%23%205%20mm%20that%20fell%20between%2023%3A00%20on%201%20June%20and%20midnight%0A%20%20%20%20)%0A%0A%20%20%20%20ours%20%3D%20(%0A%20%20%20%20%20%20%20%20resample_to_frequency(rain%2C%20%5B%22total_precipitation%22%5D%2C%20%22D%22)%5B%22total_precipitation%22%5D.values%5B%3A2%5D%0A%20%20%20%20%20%20%20%20*%201000%0A%20%20%20%20)%0A%20%20%20%20ecmwf%20%3D%20(rain.shift(time%3D-1).resample(time%3D%22D%22).sum()%5B%22total_precipitation%22%5D.values%5B%3A2%5D)%20*%201000%0A%20%20%20%20print(%22daily%20totals%2C%20mm%2C%20for%201%20and%202%20June%3A%22)%0A%20%20%20%20print(%22%20%20ours%20%20(stamps%2000..23%2C%20i.e.%2023%3A00%20D-1%20to%2023%3A00%20D)%3A%22%2C%20ours.round(1).tolist())%0A%20%20%20%20print(%22%20%20ECMWF%20(stamps%2001..00%2C%20i.e.%2000%3A00%20D%20to%2024%3A00%20D)%20%20%3A%22%2C%20ecmwf.round(1).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%20The%20same%205%20mm%20lands%20on%202%20June%20in%20our%20store%20and%20on%201%20June%20under%20ECMWF's%20convention.%20Over%20the%20whole%20record%20no%20rain%20is%20gained%20or%20lost%2C%20but%20a%20single%20day%20can%20differ%20by%20a%20whole%20shower%2C%20and%20so%20can%20a%20season's%20total%20when%20that%20shower%20falls%20on%20its%20boundary%3A%20a%20season%20ending%20on%201%20June%20counts%20the%2023%3A00%20shower%20under%20one%20convention%20and%20not%20the%20other.%20It%20matters%20when%20a%20daily%20ERA5%20total%20is%20lined%20up%20against%20a%20forecast%20day%2C%20which%20uses%20yet%20another%20window.%0A%0A%20%20%20%20%23%23%23%20%5Bnet%2C%20optional%5D%20The%20real%201%20January%201940%0A%0A%20%20%20%20This%20reads%20one%2010%20%C3%97%2010%20window%20of%20%60total_precipitation%60%20at%20three%20hours%20straight%20from%20the%20Zarr%20array%2C%20without%20opening%20the%201.3-million-step%20time%20axis%3A%20three%20whole-globe%20chunk%20objects%2C%20about%205%20MB.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20net_optional_real()%3A%0A%20%20%20%20if%20not%20NET%3A%0A%20%20%20%20%20%20%20%20print(%22skipped%20(offline)%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20tp_live%20%3D%20zarr.open_array(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22gs%3A%2F%2F%7BARCO%7D%2Ftotal_precipitation%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22r%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20storage_options%3D%7B%22token%22%3A%20%22anon%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20zarr_format%3D2%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%20for%20stamp%20in%20(%221940-01-01T00%22%2C%20%221940-01-01T06%22%2C%20%221940-01-01T07%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20i%20%3D%20int((pd.Timestamp(stamp)%20-%20pd.Timestamp(%221900-01-01%22)).total_seconds()%20%2F%2F%203600)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20window%20%3D%20tp_live%5Bi%2C%20300%3A310%2C%200%3A10%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%7Bstamp%7D%3A%20%7Bint(np.isnan(window).sum())%7D%20of%20%7Bwindow.size%7D%20values%20NaN%22)%0A%20%20%20%20%20%20%20%20except%20Exception%20as%20exc%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22skipped%20(network%20error)%3A%22%2C%20type(exc).__name__%2C%20exc)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20When%20it%20runs%2C%2000%20and%2006%20are%20all%20NaN%20and%2007%20is%20all%20valid%2C%20as%20the%20forecast%20table%20predicts.%20The%20chunk%20objects%20for%20those%20hours%20**exist**%3A%20they%20are%20written%20full%20of%20fill%20values.%20Cell%201.4%20showed%20that%20a%20reader%20can't%20tell%20that%20from%20a%20missing%20object.%0A%0A%20%20%20%20%23%23%203.3%20ERA5T%20against%20final%20ERA5%2C%20and%20two%20different%20lags%0A%0A%20%20%20%20ECMWF%20publishes%20ERA5%20twice.%20**ERA5T**%20(%22T%22%20for%20timely)%20appears%20about%20**5%20days**%20behind%20real%20time.%20The%20**final**%20ERA5%20replaces%20it%20about%20**three%20months**%20later%2C%20and%20can%20differ%20from%20it.%20ARCO-ERA5%20is%20a%20copy%2C%20with%20its%20own%20lags%3A%20its%20README%20says%20ERA5T%20arrives%20%22approximately%201%20week%22%20behind%2C%20and%20final%20ERA5%20%22on%20a%20monthly%20cadence%20(on%20roughly%20the%209th)%20with%20a%203%20month%20delay%22.%20ARCO%20marks%20the%20two%20with%20attributes%2C%20not%20with%20ECMWF's%20%60expver%60%20key.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_3_era5t_final_era5(live)%3A%0A%20%20%20%20snapshot%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22valid_time_stop%22%3A%20%222026-06-30%22%2C%0A%20%20%20%20%20%20%20%20%22valid_time_stop_era5t%22%3A%20%222026-11-12%22%2C%0A%20%20%20%20%7D%20%20%23%20read%20live%20on%202026-11-18%0A%20%20%20%20attrs_used%20%3D%20live%20or%20snapshot%0A%20%20%20%20source%20%3D%20%22live%22%20if%20live%20else%20%22snapshot%20of%202026-11-18%22%0A%20%20%20%20final_end%20%3D%20pd.Timestamp(attrs_used%5B%22valid_time_stop%22%5D)%0A%20%20%20%20era5t_end%20%3D%20pd.Timestamp(attrs_used%5B%22valid_time_stop_era5t%22%5D)%0A%20%20%20%20print(f%22(%7Bsource%7D)%22)%0A%20%20%20%20print(f%22final%20ERA5%20ends%20%7Bfinal_end.date()%7D%20%20(%7B(TODAY%20-%20final_end).days%7D%20days%20before%20today)%22)%0A%20%20%20%20print(f%22ERA5T%20ends%20%20%20%20%20%20%7Bera5t_end.date()%7D%20%20(%7B(TODAY%20-%20era5t_end).days%7D%20days%20before%20today)%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22so%20the%20%7B(era5t_end%20-%20final_end).days%7D%20days%20between%20them%20are%20ERA5T%3A%20preliminary%2C%20and%20due%20to%20be%20overwritten%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%20Run%20on%202026-11-18%2C%20ARCO's%20final%20ERA5%20ended%20141%20days%20before%20today%2C%20about%20four%20and%20a%20half%20months%2C%20later%20than%20ECMWF's%20own%20three%3B%20ARCO's%20ERA5T%20ended%206%20days%20before.%20Everything%20after%20%60valid_time_stop%60%20is%20preliminary.%20A%20feature%20built%20today%20on%20those%20days%20may%20change%20when%20the%20final%20release%20overwrites%20them%2C%20so%20a%20model%20trained%20on%20final%20values%20and%20scored%20on%20ERA5T%20values%20sees%20slightly%20different%20inputs.%20Unit%201.4%20(planned)%20will%20meet%20the%20same%20idea%20as%20*vintages*%20of%20official%20statistics.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Part%204%3A%20paths%20not%20travelled%2C%20tried%20live%0A%0A%20%20%20%20%23%23%204.1%20Three%20fixes%20for%20F2%0A%0A%20%20%20%20-%20**Fix%20A%3A**%20convert%20every%20processor's%20output%20with%20%60to_lon_180%60%2C%20as%20two%20processors%20(%60process_deterministic%60%20and%20%60process_opendata_ensemble%60)%20already%20do.%0A%20%20%20%20-%20**Fix%20B%3A**%20make%20%60subset_lon_lat%60%20mask-based%2C%20with%20%60centres_within%60%2C%20as%20the%20ERA5%20clip%20is.%0A%20%20%20%20-%20**Fix%20C%3A**%20split%20a%20box%20that%20crosses%20the%20edge%20into%20two%20label%20slices%20and%20%60concat%60%20them.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_1_three_fixes_f2(%0A%20%20%20%20centres_within%2C%0A%20%20%20%20g360%2C%0A%20%20%20%20lon_is_0_360%2C%0A%20%20%20%20lon_to_360%2C%0A%20%20%20%20subset_lon_lat%2C%0A%20%20%20%20to_lon_180%2C%0A)%3A%0A%20%20%20%20def%20fix_a(ds%2C%20lon_min%2C%20lon_max%2C%20lat_min%2C%20lat_max)%3A%0A%20%20%20%20%20%20%20%20return%20subset_lon_lat(to_lon_180(ds)%2C%20lon_min%2C%20lon_max%2C%20lat_min%2C%20lat_max)%0A%0A%20%20%20%20def%20fix_b(ds%2C%20lon_min%2C%20lon_max%2C%20lat_min%2C%20lat_max)%3A%0A%20%20%20%20%20%20%20%20lon%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20(ds.longitude.values%20%2B%20180)%20%25%20360%0A%20%20%20%20%20%20%20%20)%20-%20180%20%20%23%20compare%20in%20-180..180%2C%20keep%20the%20stored%20labels%0A%20%20%20%20%20%20%20%20return%20ds.isel(%0A%20%20%20%20%20%20%20%20%20%20%20%20longitude%3Dnp.flatnonzero(centres_within(lon%2C%20(lon_min%2C%20lon_max)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20latitude%3Dnp.flatnonzero(centres_within(ds.latitude.values%2C%20(lat_min%2C%20lat_max)))%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20def%20fix_c(ds%2C%20lon_min%2C%20lon_max%2C%20lat_min%2C%20lat_max)%3A%0A%20%20%20%20%20%20%20%20if%20not%20lon_is_0_360(ds)%20or%20lon_to_360(lon_min)%20%3C%3D%20lon_to_360(lon_max)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20subset_lon_lat(ds%2C%20lon_min%2C%20lon_max%2C%20lat_min%2C%20lat_max)%0A%20%20%20%20%20%20%20%20west%20%3D%20ds.sel(longitude%3Dslice(lon_to_360(lon_min)%2C%20360)%2C%20latitude%3Dslice(lat_max%2C%20lat_min))%0A%20%20%20%20%20%20%20%20east%20%3D%20ds.sel(longitude%3Dslice(0%2C%20lon_max)%2C%20latitude%3Dslice(lat_max%2C%20lat_min))%0A%20%20%20%20%20%20%20%20return%20xr.concat(%5Bwest%2C%20east%5D%2C%20dim%3D%22longitude%22)%0A%0A%20%20%20%20for%20_region%20in%20(%22france%22%2C%20%22europe%22)%3A%0A%20%20%20%20%20%20%20%20_lon_min%2C%20_lat_min%2C%20_lon_max%2C%20_lat_max%20%3D%20REGISTRY%5B_region%5D%0A%20%20%20%20%20%20%20%20for%20_name%2C%20fix%20in%20((%22A%20to_lon_180%22%2C%20fix_a)%2C%20(%22B%20mask%22%2C%20fix_b)%2C%20(%22C%20split%22%2C%20fix_c))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20out%20%3D%20fix(g360%2C%20_lon_min%2C%20_lon_max%2C%20_lat_min%2C%20_lat_max)%0A%20%20%20%20%20%20%20%20%20%20%20%20lons%20%3D%20out.longitude.values%0A%20%20%20%20%20%20%20%20%20%20%20%20reader%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20out.sortby(%22longitude%22).sel(longitude%3Dslice(_lon_min%2C%20_lon_max)).sizes%5B%22longitude%22%5D%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%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B_region%3A7s%7D%20%7B_name%3A13s%7D%20%7Bdict(out.sizes)%7D%20%20stored%20lon%20%7Blons%5B0%5D%3Ag%7D%20%E2%80%A6%20%7Blons%5B-1%5D%3Ag%7D%20%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22ascending%3A%20%7Bstr(bool((np.diff(lons)%20%3E%200).all()))%3A5s%7D%20%20reader's%20sorted%20slice(%7B_lon_min%3Ag%7D%2C%20%7B_lon_max%3Ag%7D)%3A%20%7Breader%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20canada%20%3D%20(-142.0%2C%20-52.0%2C%2040.0%2C%2084.0)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22%5Cncanada%20stored%20today%3A%22%2C%0A%20%20%20%20%20%20%20%20subset_lon_lat(g360%2C%20*canada).longitude.values%5B%5B0%2C%20-1%5D%5D.tolist()%2C%0A%20%20%20%20%20%20%20%20%22%7C%20after%20fix%20A%3A%22%2C%0A%20%20%20%20%20%20%20%20fix_a(g360%2C%20*canada).longitude.values%5B%5B0%2C%20-1%5D%5D.tolist()%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%20All%20three%20recover%2017%20columns%20for%20France%20and%2073%20for%20Europe.%20Only%20**A**%20stores%20%E2%88%92180%E2%80%A6180%20labels.%20**B**%20keeps%20the%20stored%200%E2%80%A6360%20labels%20in%20position%20order%2C%200%20%E2%80%A6%2010%20then%20354%20%E2%80%A6%20359%3A%20sorted%2C%20but%20with%20France's%20west%20end%20after%20its%20east%20end%20and%20a%20344%C2%B0%20jump%20between%20them.%20**C**%20stores%20354%20%E2%80%A6%20359%20then%200%20%E2%80%A6%2010%2C%20unsorted.%20Now%20the%20next%20reader%3A%20one%20who%20sorts%20and%20slices%20with%20the%20registry%20box%20(%E2%88%926%2C%2010)%20gets%20all%2017%20columns%20from%20A%2C%20but%20only%2011%20from%20B%20and%20C%2C%20because%20the%20western%20columns%20are%20labelled%20354%20%E2%80%A6%20359%20and%20fall%20outside%20the%20slice.%20B%20and%20C%20fix%20the%20subset%20and%20move%20the%20silent%20failure%20one%20step%20downstream.%0A%0A%20%20%20%20Whatever%20the%20fix%2C%20think%20about%20stores%20that%20already%20exist.%20%60process_run%60%20writes%20each%20run%20to%20its%20own%20store%2C%20replacing%20whatever%20is%20there%20(%60save_run_store%60)%2C%20so%20a%20run%20store%20never%20mixes%20conventions.%20The%20append%20path%20(%60save_zarr_dedup%60)%20does%20check%3A%20%5B%60validate_coordinates_match%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fecmwf_ingest%2Fprocess%2Fzarr_io.py%23L285-L318)%20compares%20the%20new%20longitudes%20with%20the%20store's%20and%20refuses%20a%20mismatch%2C%20which%20is%20loud%20and%20therefore%20safe.%20Fix%20A%20also%20changes%20regions%20that%20work%20today%3A%20a%20box%20west%20of%200%C2%B0%20such%20as%20canada%20is%20stored%20as%20218%20%E2%80%A6%20308%20now%20and%20would%20be%20stored%20as%20%E2%88%92142%20%E2%80%A6%20%E2%88%9252%20after%20the%20fix.%20Anything%20that%20stacks%20old%20and%20new%20run%20stores%20would%20then%20meet%20two%20conventions%2C%20so%20fix%20A%20comes%20with%20a%20rebuild%20of%20any%20such%20runs%20already%20written.%0A%0A%20%20%20%20%23%23%204.2%20Trust%20the%20attribute%3F%0A%0A%20%20%20%20ARCO%20publishes%20%60valid_time_stop_era5t%60.%20Reading%20it%20is%20one%20small%20request%20instead%20of%20a%20walk-back%20of%20up%20to%2030%20whole-globe%20objects.%20Does%20it%20see%20the%20hole%3F%20We%20give%20our%20cell-1.5%20store%20the%20attribute%20ARCO%20would%20give%20it%2C%20the%20last%20day%20with%20data%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_2_trust_attribute(day%2C%20get_era5_availability%2C%20store)%3A%0A%20%20%20%20z%20%3D%20zarr.open_group(store%2C%20mode%3D%22r%2B%22%2C%20zarr_format%3D2)%0A%20%20%20%20z.attrs%5B%22valid_time_stop_era5t%22%5D%20%3D%20str((TODAY%20-%205%20*%20day).date())%0A%20%20%20%20zarr.consolidate_metadata(store)%0A%20%20%20%20attr_stop%20%3D%20pd.Timestamp(xr.open_zarr(store).attrs%5B%22valid_time_stop_era5t%22%5D)%0A%20%20%20%20print(%22attribute%20says%20%20%20%3A%22%2C%20attr_stop.date())%0A%20%20%20%20print(%22probe%20says%20%20%20%20%20%20%20%3A%22%2C%20get_era5_availability(str(store)).date())%0A%20%20%20%20print(%22hole%20at%20%20%20%20%20%20%20%20%20%20%3A%22%2C%20(TODAY%20-%206%20*%20day).date()%2C%20%22%3C-%20invisible%20to%20both%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%20The%20attribute%20and%20the%20probe%20agree%2C%20and%20neither%20sees%20the%20hole%3A%20both%20describe%20the%20**edge**%2C%20and%20the%20hole%20is%20behind%20it.%20Here%20the%20attribute%20and%20the%20probe%20agree%2C%20but%20that%20isn't%20guaranteed%3A%20on%202026-11-11%20the%20attribute%20said%202026-11-05%20while%20ARCO%20already%20held%20data%20for%20a%20later%20day%20(%5Bcode.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.3-weather-grids%2Fcode.md))%2C%20so%20the%20attribute%20can%20trail%20readable%20data.%20So%20it%20can't%20serve%20as%20a%20safe%20upper%20bound%20for%20the%20probe%3A%20starting%20the%20probe%20at%20the%20attribute%20could%20miss%20newer%20data.%20Reading%20the%20data%2C%20as%20production%20does%2C%20remains%20the%20check%20that%20can't%20be%20fooled%20by%20a%20stale%20attribute.%20The%20completeness%20gate%20stays%20the%20only%20thing%20that%20sees%20holes.%0A%0A%20%20%20%20%23%23%204.3%20The%20minimal%20fix%20for%20F2%3A%20a%20size%20guard%0A%0A%20%20%20%20Before%20choosing%20A%2C%20B%20or%20C%2C%20the%20smallest%20change%20that%20turns%20F2%20from%20silent%20into%20loud%20is%20a%20check%20that%20the%20subset%20isn't%20empty.%20We%20wrap%20the%20real%20%60subset_lon_lat%60%20and%20run%202.7%20again%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_3_minimal_fix_f2(mock%2C%20run_seas%2C%20runner%2C%20subset_lon_lat)%3A%0A%20%20%20%20def%20guarded_subset(ds%2C%20lon_min%2C%20lon_max%2C%20lat_min%2C%20lat_max)%3A%0A%20%20%20%20%20%20%20%20out%20%3D%20subset_lon_lat(ds%2C%20lon_min%2C%20lon_max%2C%20lat_min%2C%20lat_max)%0A%20%20%20%20%20%20%20%20empty%20%3D%20%5Bdim%20for%20dim%20in%20(%22longitude%22%2C%20%22latitude%22)%20if%20out.sizes%5Bdim%5D%20%3D%3D%200%5D%0A%20%20%20%20%20%20%20%20if%20empty%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22bbox%20(%7Blon_min%7D%2C%20%7Blon_max%7D%2C%20%7Blat_min%7D%2C%20%7Blat_max%7D)%20kept%20no%20%7B'%20or%20'.join(empty)%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20return%20out%0A%0A%20%20%20%20with%20mock.patch.object(runner%2C%20%22subset_lon_lat%22%2C%20guarded_subset)%3A%0A%20%20%20%20%20%20%20%20for%20_region%20in%20(%22france%22%2C%20%22kenya%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(_region%2C%20%22-%3E%20written%2C%20n_written%20%3D%22%2C%20run_seas(_region)%5B1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20except%20ValueError%20as%20exc%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(_region%2C%20%22-%3E%20raises%20ValueError%3A%22%2C%20exc)%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%20France%20now%20fails%20loudly%20before%20anything%20is%20written%3B%20Kenya%20is%20unchanged.%20In%20the%20real%20repository%20this%20is%20where%20you'd%20start%3A%20write%20the%20failing%20test%20(exercise%201)%2C%20add%20the%20guard%2C%20and%20only%20then%20decide%20how%20to%20make%20France%20work.%0A%0A%20%20%20%20%23%20Part%205%3A%20exercises%20(no%20AI)%0A%0A%20%20%20%20Change%20one%20thing%2C%20predict%20the%20result%2C%20then%20run%20it.%0A%0A%20%20%20%201.%20Write%20the%20failing%20test%20from%20%60science.md%60%20step%203%20against%20the%20real%20%60subset_lon_lat%60%3A%20a%201%C2%B0%20grid%20stored%200%E2%80%A6360%2C%20and%20an%20assertion%20that%20France%20gets%2017%20longitudes.%20Make%20it%20pass%20with%20the%20smallest%20change%2C%20then%20argue%20in%20two%20sentences%20why%20you%20chose%20that%20fix%20over%20the%20other%20two%20in%204.1.%0A%20%20%20%202.%20In%201.5%2C%20add%20a%20second%20hole%20at%20today%20%E2%88%92%203%20days%20(and%20data%20again%20at%20today%20%E2%88%92%202).%20Before%20running%202.1%E2%80%932.3%20again%2C%20predict%20what%20%60get_era5_availability%60%20returns%2C%20how%20many%20days%20%60keep_complete_periods%60%20keeps%2C%20and%20whether%20the%20stall%20check%20passes.%0A%20%20%20%203.%20Using%20the%20compressed%20size%20from%202.9%20(or%201%2C709%2C555%20bytes%2C%20measured%20on%202026-11-18)%2C%20work%20out%20how%20many%20bytes%20the%20probe%20downloads%20in%20the%20worst%20case%2C%20a%2030-day%20walk-back.%20Compare%20that%20with%20reading%20one%20%60.zattrs%60.%0A%20%20%20%204.%20Using%20%60REGISTRY%60%20and%20the%20real%20registry%20linked%20in%203.1%2C%20find%20any%20box%20that%20is%20contiguous%20in%20neither%20convention.%20Then%20find%20a%20box%20for%20which%20%60lon_is_0_360%60%20would%20misjudge%20a%20regional%200%E2%80%A6360%20subset.%20(Hint%3A%20a%20dataset%20already%20cut%20to%200%E2%80%A610%20has%20a%20maximum%20of%2010%2C%20which%20is%20not%20above%20180.)%0A%20%20%20%205.%20%60process_batch%60%20fills%20a%20short%20day%20and%20calls%20itself%20again.%20Explain%20why%20it%20can't%20recurse%20for%20ever%20when%20two%20hole%20days%20fall%20in%20one%20batch%2C%20using%20%60spliced%20%7C%20%7Bday%7D%60%20in%20%5Bcode.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.3-weather-grids%2Fcode.md).%0A%20%20%20%206.%20In%202.10%2C%20move%20the%205%20mm%20from%20the%2018%3A00%E2%80%9324%3A00%20step%20to%20the%2012%3A00%E2%80%9318%3A00%20step%20(%60_lead%20%3D%3D%2018%60).%20Predict%20which%20day%20gets%20it%20before%20you%20run%20it.%20**Check%3A**%201%20June%2C%20which%20is%20still%20NaN%20on%20the%20cumulative%20path%2C%20so%20the%20rain%20disappears%3B%20the%20incremental%20path%20shows%207%20mm%20on%201%20June.%0A%20%20%20%207.%20In%202.12%2C%20the%20sixth%20pentad%20of%20February%202024%20holds%2030%20mm.%20What%20daily%20rate%20is%20that%2C%20and%20what%20would%20dividing%20by%205%20give%3F%20**Check%3A**%207.5%20mm%20a%20day%20(4%20days)%2C%20against%206.0.%0A%20%20%20%208.%20In%202.13%2C%20centre%20the%20members%20on%2032.5%20%C2%B0C%20instead%20of%2031.5%20%C2%B0C.%20Do%20the%20mean%20and%20median%20now%20see%20hot%20days%3F%20How%20close%20do%20they%20get%20to%20the%20members'%20expected%20count%3F%20**Check%3A**%20run%20it%3B%20the%20members%20expect%20far%20more%20hot%20days%20than%20any%20single%20collapsed%20trajectory%20shows.%0A%0A%20%20%20%20%23%20Sources%0A%0A%20%20%20%20-%20ERA5%3A%20Hersbach%2C%20H.%20et%20al.%20(2020)%2C%20%22The%20ERA5%20global%20reanalysis%22%2C%20*QJRMS*%20146%2C%201999%E2%80%932049%2C%20doi%3A10.1002%2Fqj.3803%3B%20ECMWF%2C%20ERA5%20data%20documentation%3A%20https%3A%2F%2Fconfluence.ecmwf.int%2Fdisplay%2FCKB%2FERA5%253A%2Bdata%2Bdocumentation%0A%20%20%20%20-%20ARCO-ERA5%20README%2C%20pinned%3A%20https%3A%2F%2Fgithub.com%2Fgoogle-research%2Farco-era5%2Fblob%2F6a998c49e4c49824f5d5f2077f311c46419b46bf%2FREADME.md%0A%20%20%20%20-%20xarray%202025.11.0%20indexing%3A%20https%3A%2F%2Fdocs.xarray.dev%2Fen%2Fv2025.11.0%2Fuser-guide%2Findexing.html%3B%20%60Dataset.roll%60%3A%20https%3A%2F%2Fdocs.xarray.dev%2Fen%2Fv2025.11.0%2Fgenerated%2Fxarray.Dataset.roll.html%3B%20%60Dataset.sortby%60%3A%20https%3A%2F%2Fdocs.xarray.dev%2Fen%2Fv2025.11.0%2Fgenerated%2Fxarray.Dataset.sortby.html%0A%20%20%20%20-%20pandas%202.3.3%20%60Index.slice_locs%60%3A%20https%3A%2F%2Fpandas.pydata.org%2Fpandas-docs%2Fversion%2F2.3.3%2Freference%2Fapi%2Fpandas.Index.slice_locs.html%0A%20%20%20%20-%20Zarr%20v2%20specification%3A%20https%3A%2F%2Fzarr-specs.readthedocs.io%2Fen%2Flatest%2Fv2%2Fv2.0.html%3B%20zarr-python%203.1.5%20performance%20guide%3A%20https%3A%2F%2Fzarr.readthedocs.io%2Fen%2Fv3.1.5%2Fuser-guide%2Fperformance%2F%0A%20%20%20%20-%20SEAS5%3A%20Johnson%2C%20S.%20J.%20et%20al.%20(2019)%2C%20%22SEAS5%3A%20the%20new%20ECMWF%20seasonal%20forecast%20system%22%2C%20*GMD*%2012%2C%201087%E2%80%931117%2C%20doi%3A10.5194%2Fgmd-12-1087-2019%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
293c8c8e86ef4c1db5f79e6e6239bf89