NCAS-CMS / cf-python

A CF-compliant Earth Science data analysis library
http://ncas-cms.github.io/cf-python
MIT License
127 stars 19 forks source link

Size 0 axis error with subspacing #799

Open moonbooks73 opened 3 months ago

moonbooks73 commented 3 months ago

When I try to create a subspace within a region and for a specific longitudean Index error occures, stating the field has a size 0 axis, I did this in VS code

> import cfplot as cfp
> import cf
> import numpy
> import matplotlib.pyplot as plt
> 
> orog = cf.read('~/cfplot_data/1km_elevation.nc')[0]
> print(orog)
> region = [(-6, 3), (50, 53)]
> orog_region = orog.subspace(longitude=cf.wi(*region[0]), latitude=cf.wi(*region[1]))
> print(orog_region.construct("latitude").array)
> elevation = orog_region.subspace(longitude = 2.9875)
> Exception has occurred: IndexError
> Indices [slice(None, None, None), array([], dtype=int64)] result in a subspaced shape of (360, 0), but can't create a subspace of Field that has a size 0 axis
>   File "/home/natalia/cfplot_data/orography_vertical_try.py", line 17, in <module>
>     elevation = orog_region.subspace(longitude = 2.9875)

However when I try the same code in the terminal, it works fine and produces the graph I want with the rest of the code.

Environment

cf.environment(paths=False) In VS code (not working)

Platform: Linux-5.4.0-131-generic-x86_64-with-glibc2.31
HDF5 library: 1.14.3
netcdf library: 4.9.2
udunits2 library: /home/natalia/cfplot_data/.conda/lib/libudunits2.so.0
esmpy/ESMF: 8.6.1
Python: 3.10.14
dask: 2024.7.1
netCDF4: 1.6.5
psutil: 6.0.0
packaging: 24.1
numpy: 2.0.1
scipy: 1.14.0
matplotlib: 3.9.1
cftime: 1.6.4
cfunits: 3.3.7
cfplot: 3.3.0
cfdm: 1.11.1.0
cf: 3.16.2

In the terminal

Platform: Linux-5.4.0-131-generic-x86_64-with-glibc2.31
HDF5 library: 1.12.1
netcdf library: 4.8.1
udunits2 library: /home/natalia/anaconda3/lib/libudunits2.so.0
esmpy/ESMF: not available
Python: 3.12.2
dask: 2024.5.0
netCDF4: 1.6.2
psutil: 5.9.0
packaging: 23.2
numpy: 1.26.4
scipy: 1.13.1
matplotlib: 3.8.4
cftime: 1.6.4
cfunits: 3.3.7
cfplot: 3.3.0
cfdm: 1.11.1.0
cf: 3.16.1
sadielbartholomew commented 3 months ago

Thanks Natalia, really useful report. We will investigate this shortly and let you know what we find here.

sadielbartholomew commented 3 months ago

Ah, I have now realised the reason for this behaviour difference! It is not a bug in cf per se, though it is a downstream effect that is leading to, at present, some errors such as this one which shouldn't be occurring and which are also obscure in terms of the user-facing message.

It is due to NumPy 2 and its backwards-incompatible API and other bevhaviour differences from v1 to v2: notice the environment with the error has numpy: 2.0.1 and the working environment has numpy: 1.26.4.

@davidhassell I am going to create a numpy-2 label to mark Issues arising relating to that, so we can keep track and discuss how to handle them. Today Sharar was also seeing funky error messages I have never seen before with cf-plot, which I have only just clocked are due to her using an environment with NumPy 2. So NumPy 2 is starting to cause trouble for users already - would be good to review any updates we need for our libraries sooner rather than later.