Odd bug loading PP fields

Aperta
#7,376 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
58/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
data

Direzione di ricerca

Start in lib/iris/analysis/_interpolation.py, where _canonical_sample_points calls cftime.date2num and converts its results. Check how scalar and one-point inputs behave with cftime 1.6.4, then add or update interpolation tests for those cases. Done when numeric points are handled without the empty-array conversion failure and the tests pass.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Something rather peculiar has come up, when loading some fairly ordinary PP data, getting an error like :

Traceback (most recent call last):
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/fileformats/rules.py", line 284, in _ensure_aligned
    i = grids.index(target_dimcoords)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: (<DimCoord: time / (hours since 1970-01-01 00:00:00)  [1970-01-01 00:00:00]>, <DimCoord: latitude / (degrees)  [-89.375, -88.125, ..., 88.125, 89.375]  shape(144,)>, <DimCoord: longitude / (degrees)  [ 0. , 1.875, ..., 356.25 , 358.125]  shape(192,)>) is not in list

During handling of the above exception, another exception occurred:

TypeError: only length-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/users/patrick.peglar/worklog/period_20261102_support/support/MattMiz_pp_load_problem/work/demo.py", line 83, in <module>
    cl = iris.load(filepths)
         ^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/loading.py", line 229, in load
    cubes = _load_collection(uris, constraints, callback).combined().cubes()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/loading.py", line 197, in _load_collection
    result = _CubeFilterCollection.from_cubes(cubes, constraints)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/loading.py", line 153, in from_cubes
    for c in cubes:
             ^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/loading.py", line 94, in _generate_cubes
    for cube in iris.io.load_files(part_names, callback, constraints):
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/io/__init__.py", line 224, in load_files
    for cube in handling_format_spec.handler(fnames, callback, constraints):
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/fileformats/rules.py", line 506, in load_cubes
    for cube, field in _load_pairs_from_fields_and_filenames(
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/fileformats/rules.py", line 457, in _load_pairs_from_fields_and_filenames
    cube = _resolve_factory_references(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/fileformats/rules.py", line 376, in _resolve_factory_references
    args, cube = _dereference_args(
                 ^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/fileformats/rules.py", line 166, in _dereference_args
    src, cube = _ensure_aligned(regrid_cache, src, cube)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/fileformats/rules.py", line 288, in _ensure_aligned
    result_cube = _regrid_to_target(src_cube, target_dimcoords, target_cube)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/fileformats/rules.py", line 204, in _regrid_to_target
    result_cube = src_cube.interpolate(sample_points, Linear())
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/cube.py", line 5390, in interpolate
    return interp(points, collapse_scalar=collapse_scalar)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/analysis/_interpolation.py", line 625, in __call__
    sample_points = _canonical_sample_points(self._src_coords, sample_points)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/iris/analysis/_interpolation.py", line 55, in _canonical_sample_points
    points = convert_dates(points)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/numpy/lib/_function_base_impl.py", line 2544, in __call__
    return self._call_as_normal(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/numpy/lib/_function_base_impl.py", line 2537, in _call_as_normal
    return self._vectorize_call(func=func, args=vargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2026_06_10/lib/python3.12/site-packages/numpy/lib/_function_base_impl.py", line 2628, in _vectorize_call
    res = asanyarray(outputs, dtype=otypes[0])
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: setting an array element with a sequence.

The context of the error is quite confusing.
It turns out that the initial Exception in the above is expected -- it just means that it found no existing cached reference cube.
Then in an 'except' clause, it attempts to produce a new orography reference field by interpolating, but fails within this sequence of calls ...

  • ensure_aligned
  • _regrid_to_target
  • _regrid_to_target
  • cube.interpolate

Underlying cause

The fundamental problem, it turns out, is that since cftime >= 1.6.4, cftime has a specific, peculiar behaviour change...
WAS cftime <= 1.6.3

>>> print(cftime.__version__)
1.6.3
>>> print(cftime.date2num(0., 'days since 1907-01-01', 'gregorian'))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "src/cftime/_cftime.pyx", line 281, in cftime._cftime.date2num
  File "src/cftime/_cftime.pyx", line 394, in cftime._cftime.to_calendar_specific_datetime
AttributeError: 'numpy.float64' object has no attribute 'year'

NOW cftime >= 1.6.4

>>> print(cftime.__version__)
1.6.4
>>> print(cftime.date2num(0., 'days since 1907-01-01', 'gregorian'))"
[]

but that changed behaviour only occurs when the argument is a single zero (0, 0., [0], [0.], etc)
-- because of this code, introduced here
(which seems to be a plain mistake, since it then returns an empty array for a scalar input)

Effect on iris

So, Iris is relying on cftime to raise an error when coord points are not datetime objects
in this code - so that it can skip a 'date2num' translation when it is not applicable
.. whereas it is now getting an empty array (instead of the expected error), which it returns in place of a scalar or one-point result.
.. which leads to the error within the "vectorized" numpy operation

Ideas?

IMHO we could/should fix this by applying a more intelligent test to the 'points', e.g. hasattr("year"), instead of catching an exception.

I also don't think we should check whether each point individually is a datetime : It would be better to just check points[0], and then call date2num if needed.
If that fails because other points are not datetimes, then I expect that an error is the right answer.
And then we wouldn't need to use 'vectorize' at all.

Lingua principale
Python
Stelle
730
Fork
323
Merge medio
3g 21h
PR unite (30g)
16

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di SciTools/iris

Tutte le issue di SciTools/iris

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.