0

I'm trying to transform a .nc file in .tiff using this code :

import xarray as xr
import rioxarray as rio

nc_file = xr.open_dataset("C:/Users/aurel/OneDrive/Documents/Mémoire/Datasets/CCI_lake/chla_EOF_Tanganyika_2016.nc")

bT = nc_file["chla"]
bT = bT.rio.set_spatial_dims(x_dim = 'longitude', y_dim = 'latitude')
bT.rio.crs
bT.rio.write_crs("epsg:4326", inplace=True)
bT.rio.to_raster("C:/Users/aurel/OneDrive/Documents/Mémoire/Datasets/CCI_lake/2016.tiff")
print(bT)

But when I run it I got this error :

PS C:\Users\aurel> & C:/Users/aurel/AppData/Local/Programs/Python/Python311/python.exe c:/Users/aurel/OneDrive/Documents/Mémoire/Codes/nc_to_tiff.py
Traceback (most recent call last):
  File "C:\Users\aurel\AppData\Local\Programs\Python\Python311\Lib\site-packages\xarray\backends\file_manager.py", line 211, in _acquire_with_cache_info
    file = self._cache[self._key]
           ~~~~~~~~~~~^^^^^^^^^^^
  File "C:\Users\aurel\AppData\Local\Programs\Python\Python311\Lib\site-packages\xarray\backends\lru_cache.py", line 56, in __getitem__
    value = self._cache[key]
            ~~~~~~~~~~~^^^^^
KeyError: [<class 'netCDF4._netCDF4.Dataset'>, ('C:\\Users\\aurel\\OneDrive\\Documents\\Mémoire\\Datasets\\CCI_lake\\chla_EOF_Tanganyika_2016.nc',), 'r', (('clobber', True), ('diskless', False), ('format', 'NETCDF4'), ('persist', False)), '0b840565-c21c-4a19-a981-4a48646df11c']

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\aurel\OneDrive\Documents\Mémoire\Codes\nc_to_tiff.py", line 4, in <module>
    nc_file = xr.open_dataset("C:/Users/aurel/OneDrive/Documents/Mémoire/Datasets/CCI_lake/chla_EOF_Tanganyika_2016.nc")
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aurel\AppData\Local\Programs\Python\Python311\Lib\site-packages\xarray\backends\api.py", line 572, in open_dataset
    backend_ds = backend.open_dataset(
                 ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aurel\AppData\Local\Programs\Python\Python311\Lib\site-packages\xarray\backends\netCDF4_.py", line 593, in open_dataset
    store = NetCDF4DataStore.open(
            ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aurel\AppData\Local\Programs\Python\Python311\Lib\site-packages\xarray\backends\netCDF4_.py", line 400, in open
    return cls(manager, group=group, mode=mode, lock=lock, autoclose=autoclose)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aurel\AppData\Local\Programs\Python\Python311\Lib\site-packages\xarray\backends\netCDF4_.py", line 347, in __init__
    self.format = self.ds.data_model
                  ^^^^^^^
  File "C:\Users\aurel\AppData\Local\Programs\Python\Python311\Lib\site-packages\xarray\backends\netCDF4_.py", line 409, in ds
    return self._acquire()
           ^^^^^^^^^^^^^^^
  File "C:\Users\aurel\AppData\Local\Programs\Python\Python311\Lib\site-packages\xarray\backends\netCDF4_.py", line 403, in _acquire
    with self._manager.acquire_context(needs_lock) as root:
  File "C:\Users\aurel\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "C:\Users\aurel\AppData\Local\Programs\Python\Python311\Lib\site-packages\xarray\backends\file_manager.py", line 199, in acquire_context
    file, cached = self._acquire_with_cache_info(needs_lock)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\aurel\AppData\Local\Programs\Python\Python311\Lib\site-packages\xarray\backends\file_manager.py", line 217, in _acquire_with_cache_info
    file = self._opener(*self._args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "src\\netCDF4\\_netCDF4.pyx", line 2469, in netCDF4._netCDF4.Dataset.__init__
  File "src\\netCDF4\\_netCDF4.pyx", line 2028, in netCDF4._netCDF4._ensure_nc_success
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\aurel\\OneDrive\\Documents\\Mémoire\\Datasets\\CCI_lake\\chla_EOF_Tanganyika_2016.nc'

I don't understand because my code worked well 2 months ago and the file is well located at the path. I can open the .nc file with rasterio but not with xarray.

1
  • Are you using Windows? Try: 1) nc_file = xr.open_dataset(rC:/Users/aurel/OneDrive/Documents/Mémoire/Datasets/CCI_lake/chla_EOF_Tanganyika_2016.nc') 2) copy locally and see OneDrive has any access issues due to unknown reasons.
    – PDash
    Commented Feb 3 at 10:45

0

Browse other questions tagged or ask your own question.