Debian Codegraph @

docs/source/reference_aggregation.rst

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 Aggregation special cases
2 =============================
3
4 As we have already seen in this `page <https://fsspec.github.io/kerchunk/test_example.html#multi-file-jsons>`_,
5 that the main purpose of ``kerchunk`` it to generate references, to view whole archive
6 of files like GRIB2, NetCDF etc, allowing us for direct access to the data. In
7 this part of the documentation, we will see some other efficient ways of
8 combining references.
9
10 GRIB Aggregations
11 -----------------
12
13 This reference aggregation method of GRIB files, developed by `Camus Energy <https://www.camus.energy>`_,
14 and it functions if accompanying ``.idx`` files are present. It involves creating a reference index
15 for every GRIB message across the files that we want to aggregate.
16
17 **But this procedure has some certain restrictions:**
18
19 - GRIB files must paired with their ``.idx`` files
20 - The ``.idx`` file must be of *text* type.
21 - Only specialised for time-series data, where GRIB files
22 have *identical* structure.
23 - Each horizon(forecast time) must be indexed separately.
24
25
26 Utilizing this method can significantly reduce the time required to combine
27 references, cutting it down to a fraction of the previous duration. The original
28 idea was showcased in this `talk <https://discourse.pangeo.io/t/pangeo-showcase-optimizations-for-kerchunk-aggregation-and-zarr-i-o-at-scale-for-machine-learning/4074>`_.
29 It follows a three step approach.
30
31 **Three step approach:**
32
33 1. Extract and persist metadata directly from a few arbitrary grib
34 files for a given product such as HRRR SUBH, GEFS, GFS etc.
35 2. Use the metadata mapping to build an index table of every grib
36 message from the ``.idx`` files
37 3. Combine the index data with the metadata to build any FMRC
38 slice (Horizon, RunTime, ValidTime, BestAvailable)
39
40
41 *How is it faster*
42
43 The ``.idx`` file otherwise known as an *index* file contains the key
44 metadata of the messages in the GRIB files. These metadata include `index`, `offset`, `datetime`,
45 `variable` and `forecast time` for their respective messages. This metadata
46 will be used to index every GRIB message. By following this approach, we only have to
47 ``scan_grib`` a single GRIB file, not the whole archive.
48
49 Building the index of a time horizon, first requires a single one-to-one mapping of GRIB/Zarr
50 metadata to the attributes in the idx file. Only constraint is the mapping needs to be
51 made from a single GRIB file, belonging to the *same time horizon*. The indexing process
52 primarily involves the `pandas <https://pandas.pydata.org/>`_ library. To confirm this,
53 see this `notebook <https://gist.github.com/Anu-Ra-g/efa01ad1c274c1bd1c14ee01666caa77>`_.
54 After indexing a single time horizon, you can combine this index with indexes of
55 other time horizon and store it.
56
57 .. note::
58 The index in ``.idx`` file indexes the GRIB messages where as the ``k_index``
59 (kerchunk index), index the variables
60 in those messages.
61
62 The table mentioned below is a *k_index* made from a single GRIB file.
63
64 .. list-table:: k_index for a single GRIB file
65 :header-rows: 1
66 :widths: 5 10 15 10 20 15 10 20 20 30 10 10 10
67
68 * -
69 - varname
70 - typeOfLevel
71 - stepType
72 - name
73 - step
74 - level
75 - time
76 - valid_time
77 - uri
78 - offset
79 - length
80 - inline_value
81 * - 0
82 - gh
83 - isobaricInhPa
84 - instant
85 - Geopotential height
86 - 0 days 06:00:00
87 - 0.0
88 - 2017-01-01 06:00:00
89 - 2017-01-01 12:00:00
90 - s3://noaa-gefs-pds/gefs.20170101/06/gec00.t06z...
91 - 0
92 - 47493
93 - None
94 * - 1
95 - t
96 - isobaricInhPa
97 - instant
98 - Temperature
99 - 0 days 06:00:00
100 - 0.0
101 - 2017-01-01 06:00:00
102 - 2017-01-01 12:00:00
103 - s3://noaa-gefs-pds/gefs.20170101/06/gec00.t06z...
104 - 47493
105 - 19438
106 - None
107 * - 2
108 - r
109 - isobaricInhPa
110 - instant
111 - Relative humidity
112 - 0 days 06:00:00
113 - 0.0
114 - 2017-01-01 06:00:00
115 - 2017-01-01 12:00:00
116 - s3://noaa-gefs-pds/gefs.20170101/06/gec00.t06z...
117 - 66931
118 - 10835
119 - None
120 * - 3
121 - u
122 - isobaricInhPa
123 - instant
124 - U component of wind
125 - 0 days 06:00:00
126 - 0.0
127 - 2017-01-01 06:00:00
128 - 2017-01-01 12:00:00
129 - s3://noaa-gefs-pds/gefs.20170101/06/gec00.t06z...
130 - 77766
131 - 22625
132 - None
133 * - 4
134 - v
135 - isobaricInhPa
136 - instant
137 - V component of wind
138 - 0 days 06:00:00
139 - 0.0
140 - 2017-01-01 06:00:00
141 - 2017-01-01 12:00:00
142 - s3://noaa-gefs-pds/gefs.20170101/06/gec00.t06z...
143 - 100391
144 - 20488
145 - None
146
147
148 *What now*
149
150 After creating the k_index as per the desired duration, we will use the ``DataTree`` model
151 from the `xarray-datatree <https://xarray-datatree.readthedocs.io/en/latest/>`_ to view a
152 part(desired variables) or the whole of the aggregation, using the k_index. Below is a
153 tree model made from an aggregation of GRIB files produced from **GEFS** model hosted
154 in AWS S3 bucket.
155
156 .. code-block:: bash
157
158 DataTree('None', parent=None)
159 ├── DataTree('prmsl')
160 │ │ Dimensions: ()
161 │ │ Data variables:
162 │ │ *empty*
163 │ │ Attributes:
164 │ │ name: Pressure reduced to MSL
165 │ └── DataTree('instant')
166 │ │ Dimensions: ()
167 │ │ Data variables:
168 │ │ *empty*
169 │ │ Attributes:
170 │ │ stepType: instant
171 │ └── DataTree('meanSea')
172 │ Dimensions: (latitude: 181, longitude: 360, time: 1, step: 1,
173 │ model_horizons: 1, valid_times: 237)
174 │ Coordinates:
175 │ * latitude (latitude) float64 1kB 90.0 89.0 88.0 87.0 ... -88.0 -89.0 -90.0
176 │ * longitude (longitude) float64 3kB 0.0 1.0 2.0 3.0 ... 357.0 358.0 359.0
177 │ meanSea float64 8B ...
178 │ number (time, step) int64 8B ...
179 │ step (model_horizons, valid_times) timedelta64[ns] 2kB ...
180 │ time (model_horizons, valid_times) datetime64[ns] 2kB ...
181 │ valid_time (model_horizons, valid_times) datetime64[ns] 2kB ...
182 │ Dimensions without coordinates: model_horizons, valid_times
183 │ Data variables:
184 │ prmsl (model_horizons, valid_times, latitude, longitude) float64 124MB ...
185 │ Attributes:
186 │ typeOfLevel: meanSea
187 └── DataTree('ulwrf')
188 │ Dimensions: ()
189 │ Data variables:
190 │ *empty*
191 │ Attributes:
192 │ name: Upward long-wave radiation flux
193 └── DataTree('avg')
194 │ Dimensions: ()
195 │ Data variables:
196 │ *empty*
197 │ Attributes:
198 │ stepType: avg
199 └── DataTree('nominalTop')
200 Dimensions: (latitude: 181, longitude: 360, time: 1, step: 1,
201 model_horizons: 1, valid_times: 237)
202 Coordinates:
203 * latitude (latitude) float64 1kB 90.0 89.0 88.0 87.0 ... -88.0 -89.0 -90.0
204 * longitude (longitude) float64 3kB 0.0 1.0 2.0 3.0 ... 357.0 358.0 359.0
205 nominalTop float64 8B ...
206 number (time, step) int64 8B ...
207 step (model_horizons, valid_times) timedelta64[ns] 2kB ...
208 time (model_horizons, valid_times) datetime64[ns] 2kB ...
209 valid_time (model_horizons, valid_times) datetime64[ns] 2kB ...
210 Dimensions without coordinates: model_horizons, valid_times
211 Data variables:
212 ulwrf (model_horizons, valid_times, latitude, longitude) float64 124MB ...
213 Attributes:
214 typeOfLevel: nominalTop
215
216
217 .. tip::
218 For a full tutorial on this workflow, refer this `kerchunk cookbook <https://projectpythia.org/kerchunk-cookbook/README.html>`_
219 in `Project Pythia <https://projectpythia.org/>`_.
220
221 .. raw:: html
222
223 <script data-goatcounter="https://kerchunk.goatcounter.com/count"
224 async src="//gc.zgo.at/count.js"></script>