Repository: ecmwf/earthkit-data (python)

Issue:
Test test_grib_output_pl fails
### What happened?

This test fails with

 `gribapi.errors.ReadOnlyError: Value is read only` 

when trying to set `levtype=pl` on a GRIB message. This happens because since ecCodes 2.33.0 `levtype` is a readonly key.

### What are the steps to reproduce the bug?

See above

### Version

ecCodes >= 2.33.0

### Platform (OS and architecture)

all

### Relevant log output

_No response_

### Accompanying data

_No response_

### Organisation

ECMWF

Pull request:
Fix setting grib levtype test
fixes #271 

Patch:
diff --git a/earthkit/data/readers/grib/output.py b/earthkit/data/readers/grib/output.py
index 92ae513..11bcec7 100644
--- a/earthkit/data/readers/grib/output.py
+++ b/earthkit/data/readers/grib/output.py
@@ -197,6 +197,18 @@ class GribOutput:
             except ValueError:
                 metadata["shortName"] = param
 
+        # levtype is a readOnly key in ecCodes >= 2.33.0
+        levtype_remap = {
+            "pl": "isobaricInhPa",
+            "ml": "hybrid",
+            "pt": "theta",
+            "pv": "potentialVorticity",
+            "sfc": "surface",
+        }
+        if "levtype" in metadata:
+            v = metadata.pop("levtype")
+            metadata["typeOfLevel"] = levtype_remap[v]
+
     def handle_from_metadata(self, values, metadata, compulsory):
         from .codes import GribCodesHandle  # Lazy loading of eccodes
 


Test patch:
diff --git a/tests/grib/test_grib_output.py b/tests/grib/test_grib_output.py
index 1eba7fc..56b9617 100644
--- a/tests/grib/test_grib_output.py
+++ b/tests/grib/test_grib_output.py
@@ -138,14 +138,17 @@ def test_grib_output_mars_labeling():
     sys.version_info < (3, 10),
     reason="ignore_cleanup_errors requires Python 3.10 or later",
 )
-def test_grib_output_pl():
+@pytest.mark.parametrize("levtype", [{}, {"levtype": "pl"}])
+def test_grib_output_pl(levtype):
     data = np.random.random((40320,))
 
     with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp:
         path = os.path.join(tmp, "a.grib")
 
         f = earthkit.data.new_grib_output(path, date=20010101)
-        f.write(data, param="t", level=850)
+        _kwargs = dict(param="t", level=850)
+        _kwargs.update(levtype)
+        f.write(data, **_kwargs)
         f.close()
 
         ds = earthkit.data.from_source("file", path)