Error Reading NetCDF File with xclim in Python: NameError for 'xr'

54 Views Asked by At

I am working with the xclim package in Python to process climate data from a NetCDF file. However, when I try to read the NetCDF file, I encounter a NameError. Below is the code snippet that is causing the issue:

from xclim.core.calendar import percentile_doy
from xclim.indices import cold_spell_duration_index

path_to_tasmin_file = "/scratch/aez849/CCCS/BIAS_CORR_DAYMET/Merge_Hist_rcp45/K/tmin.hist_rcp45_merged.CanESM2.CRCM5-UQAM.day.NAM-44i.mbcn-Daymet.nc"
tasmin = xr.open_dataset(path_to_tasmin_file).tasmin.isel(lat=0, lon=0)

When I run the above code, I receive the following error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/scratch/aez849/CCCS/azal/lib/python3.10/site-packages/xarray/backends/a                                                                                                                                                             pi.py", line 573, in open_dataset
    backend_ds = backend.open_dataset(
  File "/scratch/aez849/CCCS/azal/lib/python3.10/site-packages/xarray/backends/n                                                                                                                                                             etCDF4_.py", line 603, in open_dataset
    store = NetCDF4DataStore.open(
  File "/scratch/aez849/CCCS/azal/lib/python3.10/site-packages/xarray/backends/n                                                                                                                                                             etCDF4_.py", line 367, in open
    import netCDF4
  File "/scratch/aez849/CCCS/azal/lib/python3.10/site-packages/netCDF4/__init__.                                                                                                                                                             py", line 3, in <module>
    from ._netCDF4 import *
  File "src/netCDF4/_netCDF4.pyx", line 1, in init netCDF4._netCDF4
ModuleNotFoundError: No module named 'mpi4py'

Any insights or suggestions on how to resolve this would be greatly appreciated. I'm having trouble with the installation of 'mpi4py' module. Apparently, it does not match with the version of 'XClim' that I have or something.

1

There are 1 best solutions below

1
Timeless On

xclim is built using xarray and one of the backends of the latter is the netcdf4 Python interface. And from netcdf4-python/src/netCDF4/_netCDF4.pyx, we can read:

To use parallel IO, your program must be running in an MPI environment using mpi4py.

So you can try installing mpi4py before running your code (see here).