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
10 changes: 7 additions & 3 deletions py_mmd_tools/nc_to_mmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,10 +1255,14 @@ def get_geographic_extent_rectangle(self, mmd_element, ncin):
self.missing_attributes["errors"].append(
"%s must be convertible to float type." % acdd_key
)
# Make sure longitudes are within +/-180 degrees
# Make sure longitudes are within +/-180 degrees, preserving original
# string precision to avoid floating-point representation artifacts.
if not fail:
data["east"] = str((float(data["east"]) + 180.) % 360. - 180.)
data["west"] = str((float(data["west"]) + 180.) % 360. - 180.)
for key in ("east", "west"):
val_str = str(data[key])
decimal_places = len(val_str.split(".")[-1]) if "." in val_str else 0
normalized = (float(val_str) + 180.) % 360. - 180.
data[key] = str(round(normalized, decimal_places))

return data

Expand Down
21 changes: 21 additions & 0 deletions tests/test_nc_to_mmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,27 @@ def test_geographic_extent_is_string(self):
self.assertIsInstance(value["north"], str)
self.assertIsInstance(value["south"], str)

def test_geographic_extent_no_float_precision_artifacts(self):
"""Test that longitude normalization does not introduce floating-point
precision artifacts. E.g., input '41.76' should remain '41.76', not
become '41.75999999999999'.
"""
mmd_yaml = yaml.load(
resource_string('py_mmd_tools', 'mmd_elements.yaml'), Loader=yaml.FullLoader
)
md = Nc_to_mmd(os.path.abspath('tests/data/reference_nc.nc'), check_only=True)
ncin = Dataset(md.netcdf_file, "w", diskless=True)
ncin.geospatial_lat_max = "60.158733"
ncin.geospatial_lat_min = "59.78492"
ncin.geospatial_lon_max = "41.76"
ncin.geospatial_lon_min = "10.5"
value = md.get_geographic_extent_rectangle(
mmd_yaml['geographic_extent']['rectangle'], ncin)
self.assertEqual(value["east"], "41.76")
self.assertEqual(value["west"], "10.5")
self.assertEqual(value["north"], "60.158733")
self.assertEqual(value["south"], "59.78492")

def test_geographic_extent_rectangle_is_floatable(self):
""" Test that the provided geospatial coordinates can be
converted to float.
Expand Down
Loading