Repository: euro-cordex/py-cordex

Issue:
rounding errors
For some coordinates, we get some weired rounding error, e.g.,:

![grafik](https://user-images.githubusercontent.com/5659125/175062964-cc350ec9-8785-46b4-9ffb-7ed9110a2313.png)


Discussion:

Patch:
diff --git a/.github/workflows/fair-software.yaml b/.github/workflows/fair-software.yaml
index d1fa0a4..c9e50b2 100644
--- a/.github/workflows/fair-software.yaml
+++ b/.github/workflows/fair-software.yaml
@@ -10,6 +10,6 @@ jobs:
       - uses: fair-software/howfairis-github-action@0.2.1
         name: Measure compliance with fair-software.eu recommendations
         env:
-          PYCHARM_HOSTED: "Trick colorama into displaying colored output" 
+          PYCHARM_HOSTED: "Trick colorama into displaying colored output"
         with:
           MY_REPO_URL: "https://github.com/${{ github.repository }}"
diff --git a/README.rst b/README.rst
index 4770768..52dc57a 100644
--- a/README.rst
+++ b/README.rst
@@ -27,7 +27,7 @@ py-cordex: create cordex grids and meta data
 
 .. image:: https://zenodo.org/badge/304687410.svg
    :target: https://zenodo.org/badge/latestdoi/304687410
-   
+
 .. image:: https://img.shields.io/badge/fair--software.eu-%E2%97%8F%20%20%E2%97%8F%20%20%E2%97%8F%20%20%E2%97%8F%20%20%E2%97%8B-yellow
    :target: https://fair-software.eu
 
diff --git a/cordex/core/config.py b/cordex/core/config.py
new file mode 100644
index 0000000..4ff6c1d
--- /dev/null
+++ b/cordex/core/config.py
@@ -0,0 +1,2 @@
+# number of digits for rounding coordinates due to issue https://github.com/euro-cordex/py-cordex/issues/60
+nround = 14
diff --git a/cordex/core/domain.py b/cordex/core/domain.py
index 9109d90..bbb9b42 100644
--- a/cordex/core/domain.py
+++ b/cordex/core/domain.py
@@ -25,6 +25,7 @@ import xarray as xr
 
 from ..tables import domains
 from . import cf, utils
+from .config import nround
 
 
 def domain_names(table_name=None):
@@ -377,8 +378,12 @@ def _grid_mapping(pollon, pollat, mapping_name=None):
 
 def _init_grid(nlon, nlat, dlon, dlat, ll_lon, ll_lat):
     """create coordinate arrays from lower left longitude and latitude"""
-    rlon = np.array([ll_lon + i * dlon for i in range(0, nlon)], dtype=np.float64)
-    rlat = np.array([ll_lat + i * dlat for i in range(0, nlat)], dtype=np.float64)
+    rlon = np.array(
+        [round(ll_lon + i * dlon, nround) for i in range(0, nlon)], dtype=np.float64
+    )
+    rlat = np.array(
+        [round(ll_lat + i * dlat, nround) for i in range(0, nlat)], dtype=np.float64
+    )
     return rlon, rlat
 
 
diff --git a/docs/whats_new.rst b/docs/whats_new.rst
index bbe7711..9ef6d04 100644
--- a/docs/whats_new.rst
+++ b/docs/whats_new.rst
@@ -15,7 +15,13 @@ v0.4.1 (Unreleased)
 Internal Changes
 ~~~~~~~~~~~~~~~~
 
--  Added pre-commit hooks for Jupyter notebooks (including reformatting), added ``.zenodo.json`` (:pull:`59`).
+- Added pre-commit hooks for Jupyter notebooks (including reformatting), added ``.zenodo.json`` (:pull:`59`).
+
+Bug Fixes
+~~~~~~~~~
+
+- Fixed rounding errors for grid coordinates (:pull:`61`). This might have some influences on results, since coordinate values change up to single precision (which is irrelevant for CORDEX domains).
+
 
 
 v0.4.0 (29 April 2022)


Test patch:
diff --git a/tests/test_domain.py b/tests/test_domain.py
index 21735c2..3828a8c 100644
--- a/tests/test_domain.py
+++ b/tests/test_domain.py
@@ -26,6 +26,9 @@ def test_domain_basic():
     assert "institution" in cx.cordex_domain("EUR-11", attrs="CORDEX").attrs
     assert "rotated_latitude_longitude" not in cx.cordex_domain("EUR-11i").data_vars
 
+    # ensure rounding errors fixed
+    assert np.float64(eur11.rlon.isel(rlon=34).data) == -24.635
+
 
 def test_cordex_regular():
     eur11i = cx.cordex_domain("EUR-11i")