Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies:
- ecmwf-opendata>=0.1.2
- polytope-client>=0.7.4
- earthkit-meteo>=0.0.1
- covjsonkit>=0.2.0
- covjsonkit>=0.2.2
- earthkit-geo>=0.2.0
- earthkit-utils>=0.1.1
- tqdm>=4.63.0
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ optional-dependencies.all = [
]
optional-dependencies.cds = [ "cdsapi>=0.7.2" ]
optional-dependencies.ci = [ "numpy" ]
optional-dependencies.covjsonkit = [ "covjsonkit>=0.2" ]
optional-dependencies.covjsonkit = [ "covjsonkit>=0.2.2" ]
optional-dependencies.dev = [
"earthkit-data[all,docs,test]",
]
Expand Down
13 changes: 12 additions & 1 deletion src/earthkit/data/readers/covjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ def to_xarray(self, **kwargs):
return decoder.to_xarray()


class CovjsonReader(XarrayMixIn, Reader):
class GeojsonMixIn:
def to_geojson(self, **kwargs):
try:
from covjsonkit.api import Covjsonkit
except ImportError:
raise ImportError("covjson handling requires 'covjsonkit' to be installed")

decoder = Covjsonkit().decode(self._json())
return decoder.to_geojson()


class CovjsonReader(XarrayMixIn, GeojsonMixIn, Reader):
def __init__(self, source, path):
super().__init__(source, path)

Expand Down
2 changes: 1 addition & 1 deletion tests/data/time_series.covjson
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"type": "CoverageCollection", "domainType": "PointSeries", "coverages": [{"mars:metadata": {"class": "od", "Forecast date": "20240103T00", "domain": "g", "expver": "0001", "levtype": "sfc", "stream": "oper", "type": "fc", "number": 1}, "type": "Coverage", "domain": {"type": "Domain", "axes": {"x": {"values": [0.035149384216]}, "y": {"values": [0.0]}, "z": {"values": ["sfc"]}, "t": {"values": ["2024-01-03T00:00:00", "2024-01-03T01:00:00", "2024-01-03T02:00:00", "2024-01-03T03:00:00", "2024-01-03T04:00:00", "2024-01-03T05:00:00", "2024-01-03T06:00:00", "2024-01-03T07:00:00", "2024-01-03T08:00:00"]}}}, "ranges": {"2t": {"type": "NdArray", "dataType": "float", "shape": [9], "axisNames": ["t"], "values": [298.9648132324219, 298.6933288574219, 298.9999694824219, 298.5683288574219, 298.9960632324219, 298.4452819824219, 299.05596923828125, 299.21807861328125, 298.95050048828125]}}}], "parameters": {"2t": {"type": "Parameter", "description": "Temperature", "unit": {"symbol": "K"}, "observedProperty": {"id": "2t", "label": {"en": "Temperature"}}}}, "referencing": [{"coordinates": ["x", "y", "z"], "system": {"type": "GeographicCRS", "id": "http://www.opengis.net/def/crs/OGC/1.3/CRS84"}}]}
{"type": "CoverageCollection", "domainType": "PointSeries", "coverages": [{"mars:metadata": {"class": "od", "Forecast date": "20240103T00", "domain": "g", "expver": "0001", "levtype": "sfc", "stream": "oper", "type": "fc", "number": 1}, "type": "Coverage", "domain": {"type": "Domain", "axes": {"latitude": {"values": [0.035149384216]}, "longitude": {"values": [0.0]}, "levelist": {"values": ["sfc"]}, "t": {"values": ["2024-01-03T00:00:00", "2024-01-03T01:00:00", "2024-01-03T02:00:00", "2024-01-03T03:00:00", "2024-01-03T04:00:00", "2024-01-03T05:00:00", "2024-01-03T06:00:00", "2024-01-03T07:00:00", "2024-01-03T08:00:00"]}}}, "ranges": {"2t": {"type": "NdArray", "dataType": "float", "shape": [9], "axisNames": ["t"], "values": [298.9648132324219, 298.6933288574219, 298.9999694824219, 298.5683288574219, 298.9960632324219, 298.4452819824219, 299.05596923828125, 299.21807861328125, 298.95050048828125]}}}], "parameters": {"2t": {"type": "Parameter", "description": "Temperature", "unit": {"symbol": "K"}, "observedProperty": {"id": "2t", "label": {"en": "Temperature"}}}}, "referencing": [{"coordinates": ["latitude", "longitude", "levelist"], "system": {"type": "GeographicCRS", "id": "http://www.opengis.net/def/crs/OGC/1.3/CRS84"}}]}
2 changes: 1 addition & 1 deletion tests/environment-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies:
- polytope-client>=0.7.4
- earthkit-meteo>=0.0.1
- git+https://github.com/ecmwf/earthkit-data-demo-source
- covjsonkit>=0.2.0
- covjsonkit>=0.2.2
- earthkit-geo>=0.2.0
- earthkit-utils>=0.1.1
- tqdm>=4.63.0
Expand Down
8 changes: 8 additions & 0 deletions tests/readers/test_covjson_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def test_covjson_to_xarray():
assert len(a.data_vars) == 1


@pytest.mark.skipif(NO_COVJSONKIT, reason="no covjsonkit available")
def test_covjson_to_geojson():
ds = from_source("file", earthkit_test_data_file("time_series.covjson"))
assert ds
a = ds.to_geojson()
assert len(a["features"]) == 9


@pytest.mark.skipif(NO_COVJSONKIT, reason="no covjsonkit available")
def test_covjson_memory():
with open(earthkit_test_data_file("time_series.covjson"), "r") as f:
Expand Down
Loading