Geotiff raster saving broken with numpy >= 2.4.0

Open Beginner friendly
#7,133 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
numpy, python

Research direction

Start in lib/iris/experimental/raster.py around the _gdal_write_array call at line 210, then reproduce the issue with the provided Cube example under numpy >=2.4. Ensure the x/y minimum and step values passed to GDAL are scalar numeric values, and verify that export_geotiff creates the GeoTIFF without raising TypeError.

Written by the indexing model from the issue text.

Description

Good First Issue Type: Bug

🐛 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:
https://github.com/SciTools/iris/blob/6d9ef5e7af7dfb0e26bac398650606827c883a07/lib/iris/experimental/raster.py#L210

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).

Dominant language
Python
Stars
724
Forks
317
Avg merge
3d 20h
Merged PRs (30d)
9

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from SciTools/iris

All issues in SciTools/iris

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.