Skip to content

Geotiff raster saving broken with numpy >= 2.4.0 #7133

Description

@ukmo-ccbunney

🐛 Bug Report

The function iris.experimental.raster.export_geotiff fails with numpy>=2.4.0 with the following GDAL error:

  File " ... /site-packages/osgeo/gdal.py", line 4468, in SetGeoTransform
    return _gdal.Dataset_SetGeoTransform(self, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: not a number

This is due to how the parameters are passed to the ...

How To Reproduce

Steps to reproduce the behaviour:

from iris.cube import Cube
from iris.coords import DimCoord
from iris.experimental.raster import export_geotiff
import numpy as np

lats = DimCoord(np.arange(5), standard_name="latitude", units="degrees")
lats.guess_bounds()

lons = DimCoord(np.arange(5), standard_name="longitude", units="degrees")
lons.guess_bounds()

c = Cube(
    data=np.arange(25, dtype=np.float64).reshape(5,5),
    standard_name="depth",
    dim_coords_and_dims=((lats, 0), (lons,1))
)

export_geotiff(c, '/tmp/geo.tiff') # throws a TypeError

Expected behaviour

Does not throw a TypeError!

Environment

  • OS & Version: RHEL10
  • Iris Version: 3.15.0

Proposed Fix

The issue appears to be with the values passed to _gdal_write_array in experimental/raster.py:

_gdal_write_array(x_min, x_step, y_max, y_step, coord_system, data, fname, "GTiff")

The [xy]_min and [xy]_step values should be plain python floats, or numpy floats, but this routine passes in the following:

x_min=np.float64(-0.5), x_step=array([1.]), y_max=np.float64(4.5), y_step=array([-1.])

which is a mixture of scalar and 1D array numpy values. This appeared to silently work up until numpy==2.4.0, but is now failing.

The fix should be as simple as ensuring those values are plain python floats, or a numpy scalar, perhaps by using .item() which is safe to use on scalars and 1D arrays (and will throw an error if the arrays is greater than length 1).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Good First IssueA good issue to take on if you're just getting started with Iris developmentType: Bug

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions