Debian Codegraph @

docs/source/test_example.rst

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 Quick Start
2 ************
3
4 This is a run-through example for how to use this package. We scan a set of netCDF4/HDF5 files,
5 and create a single ensemble, virtual dataset, which can be read in parallel from remote
6 using ``zarr``.
7
8 Single file JSONs
9 =================
10
11 This will create a ``.json`` file for each of the files defined in ``urllist``. In this case,
12 we simply keep the resultant reference sets in memory, but we could have written them into
13 JSON files. Writing to files is useful, so that we can access the individual datasets, or
14 redo the combine (which is the next step, below).
15
16 .. code-block:: python
17
18 import kerchunk.hdf
19 import fsspec
20
21 urls = ["s3://" + p for p in [
22 'noaa-nwm-retro-v2.0-pds/full_physics/2017/201704010000.CHRTOUT_DOMAIN1.comp',
23 'noaa-nwm-retro-v2.0-pds/full_physics/2017/201704010100.CHRTOUT_DOMAIN1.comp',
24 'noaa-nwm-retro-v2.0-pds/full_physics/2017/201704010200.CHRTOUT_DOMAIN1.comp',
25 'noaa-nwm-retro-v2.0-pds/full_physics/2017/201704010300.CHRTOUT_DOMAIN1.comp',
26 'noaa-nwm-retro-v2.0-pds/full_physics/2017/201704010400.CHRTOUT_DOMAIN1.comp',
27 'noaa-nwm-retro-v2.0-pds/full_physics/2017/201704010500.CHRTOUT_DOMAIN1.comp',
28 'noaa-nwm-retro-v2.0-pds/full_physics/2017/201704010600.CHRTOUT_DOMAIN1.comp',
29 'noaa-nwm-retro-v2.0-pds/full_physics/2017/201704010700.CHRTOUT_DOMAIN1.comp',
30 'noaa-nwm-retro-v2.0-pds/full_physics/2017/201704010800.CHRTOUT_DOMAIN1.comp',
31 'noaa-nwm-retro-v2.0-pds/full_physics/2017/201704010900.CHRTOUT_DOMAIN1.comp'
32 ]]
33 so = dict(
34 anon=True, default_fill_cache=False, default_cache_type='first'
35 )
36 singles = []
37 for u in urls:
38 with fsspec.open(u, **so) as inf:
39 h5chunks = kerchunk.hdf.SingleHdf5ToZarr(inf, u, inline_threshold=100)
40 singles.append(h5chunks.translate())
41
42
43 Multi-file JSONs
44 ================
45
46 This code uses the output generated above to create a single ensemble dataset, with
47 one set of references pointing to all of the chunks in the individual files.
48
49 .. code-block:: python
50
51 from kerchunk.combine import MultiZarrToZarr
52 mzz = MultiZarrToZarr(
53 singles,
54 remote_protocol="s3",
55 remote_options={'anon': True},
56 concat_dims=["time"]
57 )
58
59 out = mzz.translate()
60
61 Again, ``out`` could be written to a JSON file by providing arguments to
62 ``translate()``. Crucially, there is no restriction on where
63 this lives, it can be anywhere that fsspec can read from.
64
65 Using the output
66 ================
67
68 This is what a user of the generated dataset would do. This person does not need to have
69 ``kerchunk`` installed, or even ``h5py`` (the library we used to initially scan the files).
70
71 .. code-block:: python
72
73 import xarray as xr
74 ds = xr.open_dataset(
75 "reference://", engine="zarr",
76 backend_kwargs={
77 "storage_options": {
78 "fo": out,
79 "remote_protocol": "s3",
80 "remote_options": {"anon": True}
81 },
82 "consolidated": False
83 }
84 )
85 # do analysis...
86 ds.velocity.mean()
87
88 Since the invocation for xarray to read this data is a little involved, we recommend
89 declaring the data set in an ``intake`` catalog. Alternatively, you might split the command
90 into multiple lines by first constructing the filesystem or mapper (you will see this in some
91 examples).
92
93 Note that, if the combining was done previously and saved to a JSON file, then the path to
94 it should replace ``out``, above, along with a ``target_options`` for any additional
95 arguments fsspec might to access it
96
97 Example/Tutorial Notebook
98 =========================
99
100 A set of tutorials notebooks, presented at the Earth Science Information Partners 2022 Winter Meeting, can be found at the following link, along with links to run the code on free cloud-based notebook environments: https://github.com/lsterzinger/2022-esip-kerchunk-tutorial
101
102 .. raw:: html
103
104 <script data-goatcounter="https://kerchunk.goatcounter.com/count"
105 async src="//gc.zgo.at/count.js"></script>