|
| 1 | +import pandas as pd |
| 2 | +import pytest |
| 3 | +from helper_functions import find_nearest_latlon |
| 4 | + |
| 5 | +from polytope_feature.engine.hullslicer import HullSlicer |
| 6 | +from polytope_feature.polytope import Polytope, Request |
| 7 | +from polytope_feature.shapes import Box, Select |
| 8 | + |
| 9 | + |
| 10 | +class TestWaveData: |
| 11 | + def setup_method(self, method): |
| 12 | + self.options = { |
| 13 | + "axis_config": [ |
| 14 | + {"axis_name": "number", "transformations": [{"name": "type_change", "type": "int"}]}, |
| 15 | + {"axis_name": "step", "transformations": [{"name": "type_change", "type": "int"}]}, |
| 16 | + { |
| 17 | + "axis_name": "date", |
| 18 | + "transformations": [{"name": "merge", "other_axis": "time", "linkers": ["T", "00"]}], |
| 19 | + }, |
| 20 | + { |
| 21 | + "axis_name": "values", |
| 22 | + "transformations": [ |
| 23 | + {"name": "mapper", "type": "octahedral", "resolution": 1280, "axes": ["latitude", "longitude"]} |
| 24 | + ], |
| 25 | + }, |
| 26 | + {"axis_name": "longitude", "transformations": [{"name": "cyclic", "range": [0, 360]}]}, |
| 27 | + {"axis_name": "latitude", "transformations": [{"name": "reverse", "is_reverse": True}]}, |
| 28 | + ], |
| 29 | + "pre_path": {"class": "od", "expver": "0001", "type": "fc", "stream": "wave", "date": "20250201"}, |
| 30 | + "compressed_axes_config": [ |
| 31 | + "longitude", |
| 32 | + "latitude", |
| 33 | + "levtype", |
| 34 | + "step", |
| 35 | + "date", |
| 36 | + "domain", |
| 37 | + "expver", |
| 38 | + "param", |
| 39 | + "class", |
| 40 | + "stream", |
| 41 | + "type", |
| 42 | + "number", |
| 43 | + ], |
| 44 | + } |
| 45 | + self.slicer = HullSlicer() |
| 46 | + |
| 47 | + @pytest.mark.fdb |
| 48 | + def test_healpix_grid(self): |
| 49 | + import pygribjump as gj |
| 50 | + |
| 51 | + self.fdb_datacube = gj.GribJump() |
| 52 | + self.API = Polytope( |
| 53 | + datacube=self.fdb_datacube, |
| 54 | + engine=self.slicer, |
| 55 | + options=self.options, |
| 56 | + ) |
| 57 | + |
| 58 | + request = Request( |
| 59 | + Select("step", [1]), |
| 60 | + Select("date", [pd.Timestamp("20250201T000000")]), |
| 61 | + Select("domain", ["g"]), |
| 62 | + Select("expver", ["0001"]), |
| 63 | + Select("param", ["140251"]), |
| 64 | + Select("class", ["od"]), |
| 65 | + Select("stream", ["wave"]), |
| 66 | + Select("type", ["fc"]), |
| 67 | + Select("direction", ["1"]), |
| 68 | + Select("frequency", ["1"]), |
| 69 | + Box(["latitude", "longitude"], [1, 1], [2, 2]), |
| 70 | + Select("levtype", ["sfc"]), |
| 71 | + ) |
| 72 | + result = self.API.retrieve(request) |
| 73 | + result.pprint() |
| 74 | + assert len(result.leaves) == 14 |
| 75 | + assert result.leaves[0].result[1].size == 1 |
| 76 | + assert result.leaves[1].result[1].size == 1 |
| 77 | + |
| 78 | + lats = [] |
| 79 | + lons = [] |
| 80 | + eccodes_lats = [] |
| 81 | + tol = 1e-8 |
| 82 | + leaves = result.leaves |
| 83 | + for i, leaf in enumerate(leaves): |
| 84 | + cubepath = leaf.flatten() |
| 85 | + lat = cubepath["latitude"][0] |
| 86 | + new_lons = cubepath["longitude"] |
| 87 | + for j, lon in enumerate(new_lons): |
| 88 | + lats.append(lat) |
| 89 | + lons.append(lon) |
| 90 | + nearest_points = find_nearest_latlon("./tests/data/wave_spectra.grib", lat, lon) |
| 91 | + eccodes_lat = nearest_points[0][0]["lat"] |
| 92 | + eccodes_lon = nearest_points[0][0]["lon"] |
| 93 | + assert eccodes_lat - tol <= lat |
| 94 | + assert lat <= eccodes_lat + tol |
| 95 | + assert eccodes_lon - tol <= lon |
| 96 | + assert lon <= eccodes_lon + tol |
| 97 | + tol = 1e-2 |
| 98 | + eccodes_lats.append(lat) |
| 99 | + assert len(eccodes_lats) == 14 |
0 commit comments