File: conftest.py

package info (click to toggle)
fdb 5.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 78,856 kB
  • sloc: cpp: 36,785; sh: 4,380; python: 1,334; makefile: 32; ansic: 8
file content (370 lines) | stat: -rw-r--r-- 12,074 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# (C) Copyright 2025- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.

"""Top-level pytest configuration."""

import itertools
import pathlib
import shutil

import eccodes as ec
from numpy import repeat
import pyfdb
import pytest
import yaml


def create_fdb(root: pathlib.Path, schema_src: pathlib.Path) -> pathlib.Path:
    """Create a new FDB under 'root' using the provided schema.

    This helper function is supposed to be used to build specific test FDB setups.

    Args:
        root (pathlib.Path): Directory that will hold the resulting ``.fdb``.
        schema_src (pathlib.Path): Path to a text file containing the schema definition.

    Returns:
        pathlib.Path: Absolute path to the created ``fdb_config.yaml`` file.

    Asserts:
        - ``root`` needs to already exist.
        - ``schema`` file exists
    """
    assert root.is_dir()
    assert schema_src.is_file()
    schema_path = root / "schema"
    shutil.copy(schema_src, schema_path)

    db_store_path = root / "db_store"
    db_store_path.mkdir()
    fdb_config = dict(
        type="local",
        engine="toc",
        schema=str(schema_path),
        spaces=[
            dict(
                handler="Default",
                roots=[
                    {"path": str(db_store_path)},
                ],
            )
        ],
    )
    fdb_config_str = yaml.dump(fdb_config)
    fdb_config_path = root / "fdb_config.yaml"
    fdb_config_path.write_text(fdb_config_str)
    return fdb_config_path


def populate_fdb(config: pathlib.Path, messages: list[pathlib.Path]):
    """Import 'messages' into fdb pointed to by 'config'

    This helper function is supposed to be used to build specific test FDB setups.

    Args:
        config (pathlib.Path): FDB database to use.
        messages (list[pathlib.Path]): List of files with messages to ingest.

    Asserts:
        - FDB config file needs to exist
        - All message files need to exist

    """
    assert config.is_file()
    fdb = pyfdb.FDB(config.read_text())
    for message in messages:
        assert message.is_file()
        fdb.archive(message.read_bytes())
    fdb.flush()


@pytest.fixture(scope="session")
def data_path() -> pathlib.Path:
    """
    Provides path to test data
    """
    path = pathlib.Path(__file__).resolve().parent / "data"
    assert path.exists()
    return path


@pytest.fixture(scope="session")
def session_tmp(tmp_path_factory) -> pathlib.Path:
    return tmp_path_factory.mktemp("session_data")


@pytest.fixture(scope="session")
def build_example_sfc_pl_grib_messages(data_path, session_tmp) -> pathlib.Path:
    """
    Build messages that span the data required to match mars requests in
    'data/anemoi-recipes/example.yaml'
    """
    tmp = session_tmp / "build_example_sfc_pl_messages"
    tmp.mkdir()
    template_grib = data_path / "template.grib"
    assert template_grib.is_file()
    template_grib_fd = open(template_grib, "rb")
    gid = ec.codes_grib_new_from_file(template_grib_fd)
    template_grib_fd.close()
    count_data_points = int(ec.codes_get(gid, "numberOfDataPoints"))
    count_values = int(ec.codes_get(gid, "numberOfValues"))
    count_missing = int(ec.codes_get(gid, "numberOfMissing"))

    # This only supports messages without missing datapoints
    assert count_data_points == count_values
    assert count_missing == 0

    # Set common keys / data "pattern"
    ec.codes_set_string(gid, "type", "an")
    ec.codes_set_string(gid, "class", "ea")
    ec.codes_set_string(gid, "expver", "0001")
    ec.codes_set_string(gid, "stream", "oper")

    dates = [20200101, 20200102]
    times = [0, 300, 600, 900, 1200, 1500, 1800, 2100]
    # 10u/10v
    parameters_sfc = [165, 166]

    # u/v
    parameters_pl = [131, 132]
    levels = [50, 100]

    messages = tmp / "test_data.grib"
    with open(messages, "wb") as out:
        ec.codes_set_string(gid, "levtype", "sfc")
        for value, (date, time, parameter) in enumerate(
            itertools.product(dates, times, parameters_sfc)
        ):
            ec.codes_set(gid, "date", date)
            ec.codes_set(gid, "time", time)
            ec.codes_set(gid, "paramId", parameter)

            ec.codes_set_values(gid, repeat(value, count_values))
            ec.codes_write(gid, out)

        offset = value

        ec.codes_set_string(gid, "levtype", "pl")
        for value, (date, time, level, parameter) in enumerate(
            itertools.product(dates, times, levels, parameters_pl)
        ):
            ec.codes_set(gid, "date", date)
            ec.codes_set(gid, "time", time)
            ec.codes_set(gid, "paramId", parameter)
            ec.codes_set(gid, "level", level)
            ec.codes_set_values(gid, repeat(offset + value, count_values))
            ec.codes_write(gid, out)

    ec.codes_release(gid)
    return messages


@pytest.fixture(scope="session")
def build_grib_messages(data_path, session_tmp) -> pathlib.Path:
    template_grib = data_path / "template.grib"
    assert template_grib.is_file()
    template_grib_fd = open(template_grib, "rb")
    gid = ec.codes_grib_new_from_file(template_grib_fd)
    template_grib_fd.close()
    count_data_points = int(ec.codes_get(gid, "numberOfDataPoints"))
    count_values = int(ec.codes_get(gid, "numberOfValues"))
    count_missing = int(ec.codes_get(gid, "numberOfMissing"))

    # This only supports messages without missing datapoints
    assert count_data_points == count_values
    assert count_missing == 0

    # Set common keys / data "pattern"
    ec.codes_set_string(gid, "type", "an")
    ec.codes_set_string(gid, "class", "ea")
    ec.codes_set_string(gid, "expver", "0001")
    ec.codes_set_string(gid, "stream", "oper")
    ec.codes_set_string(gid, "levtype", "sfc")
    ec.codes_set_values(gid, list(range(0, count_values)))

    dates = [20200101, 20200102, 20200103, 20200104]
    times = [0, 300, 600, 900, 1200, 1500, 1800, 2100]
    parameters = [167, 131, 132]

    messages = session_tmp / "test_data.grib"
    with open(messages, "wb") as out:
        for date, time, parameter in itertools.product(dates, times, parameters):
            ec.codes_set(gid, "date", date)
            ec.codes_set(gid, "time", time)
            ec.codes_set(gid, "paramId", parameter)
            ec.codes_write(gid, out)

    ec.codes_release(gid)
    return messages


@pytest.fixture(scope="session", autouse=False)
def read_only_fdb_setup_for_sfc_pl_example(
    data_path, session_tmp, build_example_sfc_pl_grib_messages
) -> pathlib.Path:
    """
    Creates a FDB setup in this tests temp directory.
    Test FDB currently reads all grib files in `tests/data`
    This setup can be shared between tests as we will only read
    data from this FDB
    """
    fdb_root = session_tmp / "sfc-pl-example-fdb"
    fdb_root.mkdir()
    schema_path_src = data_path / "schema"
    cfg = create_fdb(fdb_root, schema_path_src)
    populate_fdb(cfg, [build_example_sfc_pl_grib_messages])
    return cfg


@pytest.fixture(scope="session", autouse=False)
def read_only_fdb_setup(data_path, session_tmp, build_grib_messages) -> pathlib.Path:
    """
    Creates a FDB setup in this tests temp directory.
    Test FDB currently reads all grib files in `tests/data`
    This setup can be shared between tests as we will only read
    data from this FDB
    """
    schema_path_src = data_path / "schema"
    assert schema_path_src.is_file()
    schema_path = session_tmp / "schema"
    shutil.copy(schema_path_src, schema_path)

    db_store_path = session_tmp / "db_store"
    db_store_path.mkdir()
    fdb_config = dict(
        type="local",
        engine="toc",
        schema=str(schema_path),
        spaces=[
            dict(
                handler="Default",
                roots=[
                    {"path": str(db_store_path)},
                ],
            )
        ],
    )
    fdb_config_str = yaml.dump(fdb_config)
    fdb_config_path = session_tmp / "fdb_config.yaml"
    fdb_config_path.write_text(fdb_config_str)
    fdb = pyfdb.FDB(fdb_config_str)
    fdb.archive(build_grib_messages.read_bytes())
    fdb.flush()
    return fdb_config_path


@pytest.fixture(scope="session", autouse=False)
def read_only_fdb_pattern_setup(
    data_path, session_tmp, build_pattern_grib_messages
) -> pathlib.Path:
    """
    Creates a FDB setup in this tests temp directory.
    Test FDB currently reads all grib files in `tests/data`
    This setup can be shared between tests as we will only read
    data from this FDB
    """
    schema_path_src = data_path / "schema"
    assert schema_path_src.is_file()
    schema_path = session_tmp / "schema"
    shutil.copy(schema_path_src, schema_path)

    db_store_path = session_tmp / "db_store_pattern"
    db_store_path.mkdir()
    fdb_config = dict(
        type="local",
        engine="toc",
        schema=str(schema_path),
        spaces=[
            dict(
                handler="Default",
                roots=[
                    {"path": str(db_store_path)},
                ],
            )
        ],
    )
    fdb_config_str = yaml.dump(fdb_config)
    fdb_config_path = session_tmp / "fdb_config.yaml"
    fdb_config_path.write_text(fdb_config_str)
    fdb = pyfdb.FDB(fdb_config_str)
    fdb.archive(build_pattern_grib_messages.read_bytes())
    fdb.flush()
    return fdb_config_path


@pytest.fixture(scope="session")
def build_pattern_grib_messages(data_path, session_tmp) -> pathlib.Path:
    """
    Build messages which have a certain pattern to control the correct assembly of
    zarr files
    """
    tmp = session_tmp / "build_pattern_grib_messages"
    tmp.mkdir()
    template_grib = data_path / "template.grib"
    assert template_grib.is_file()
    template_grib_fd = open(template_grib, "rb")

    gid = ec.codes_grib_new_from_file(template_grib_fd)
    template_grib_fd.close()

    count_data_points = int(ec.codes_get(gid, "numberOfDataPoints"))
    count_values = int(ec.codes_get(gid, "numberOfValues"))
    count_missing = int(ec.codes_get(gid, "numberOfMissing"))

    # This only supports messages without missing data points
    assert count_data_points == count_values
    assert count_missing == 0

    # Set common keys / data "pattern"
    ec.codes_set_string(gid, "type", "an")
    ec.codes_set_string(gid, "class", "ea")
    ec.codes_set_string(gid, "expver", "0001")
    ec.codes_set_string(gid, "stream", "oper")

    dates = [20200101, 20200102, 20200103]
    times = [0000, 600, 1200, 1800]

    # 10u/10v
    parameters_sfc = [165, 166, 167]

    # u/v
    parameters_pl = [131, 132, 133]
    levels = [50, 100, 150]

    messages = tmp / "test_data_pattern.grib"
    with open(messages, "wb") as out:
        ec.codes_set_string(gid, "levtype", "sfc")

        total_values = 0

        for value, (date, time, parameter) in enumerate(
            itertools.product(dates, times, parameters_sfc)
        ):
            ec.codes_set(gid, "date", date)
            ec.codes_set(gid, "time", time)
            ec.codes_set(gid, "paramId", parameter)
            ec.codes_set_values(gid, list(itertools.repeat(value, count_values)))
            ec.codes_write(gid, out)

            total_values += 1

        ec.codes_set_string(gid, "levtype", "pl")
        for value, (date, time, parameter, level) in enumerate(
            itertools.product(dates, times, parameters_pl, levels)
        ):
            ec.codes_set(gid, "date", date)
            ec.codes_set(gid, "time", time)
            ec.codes_set(gid, "paramId", parameter)
            ec.codes_set(gid, "level", level)
            ec.codes_set_values(
                gid, list(itertools.repeat(total_values + value, count_values))
            )
            ec.codes_write(gid, out)

    ec.codes_release(gid)
    return messages