0

I am trying to convert the NETCDF file into NetCDF to GeoTIFF using the following python code.

import xarray as xr 

import rioxarray as rio 


nc_file = xr.open_dataset('/air.2019.nc')

bT = nc_file['air']
bT = bT.rio.set_spatial_dims(x_dim='lon', y_dim='lat')

bT.rio.to_raster(r"/air.2019.tiff")

I am getting the following error message:

TooManyDimensions( rioxarray.exceptions.TooManyDimensions: Only 2D and 3D data arrays supported. Data variable: air

This is the NETCDF file property.

<xarray.Dataset> Dimensions: (level: 17, lat: 73, lon: 144, time: 1460) Coordinates:

  • level (level) float32 1e+03 925.0 850.0 700.0 ... 50.0 30.0 20.0 10.0
  • lat (lat) float32 90.0 87.5 85.0 82.5 80.0 ... -82.5 -85.0 -87.5 -90.0
  • lon (lon) float32 0.0 2.5 5.0 7.5 10.0 ... 350.0 352.5 355.0 357.5
  • time (time) datetime64[ns] 2021-01-01 ... 2021-12-31T18:00:00 Data variables: air (time, level, lat, lon) float32 ... Attributes: Conventions: COARDS title: 4x daily NMC reanalysis (2014) history: created 2013/12 by Hoop (netCDF2.3) description: Data is from NMC initialized reanalysis\n(4x/day). It co... platform: Model dataset_title: NCEP-NCAR Reanalysis 1 References: http://www.psl.noaa.gov/data/gridded/data.ncep.reanalysis...

How do I resolve this?

I am trying to follow the code given at

https://help.marine.copernicus.eu/en/articles/5029956-how-to-convert-netcdf-to-geotiff#h_088eb0feb8

1 Answer 1

0

The error message indicates that the air variable in the nc_file dataset has more than three dimensions, which is not supported by the rioxarray module. In order to use the set_spatial_dims and to_raster functions, the air variable must be a 2D or 3D data array.

To fix the error, you can try using the squeeze method to remove any dimensions with size 1 from the air variable. For example:

nc_file = xr.open_dataset('/air.2019.nc')

bT = nc_file['air']
bT = bT.squeeze()  # remove dimensions with size 1
bT = bT.rio.set_spatial_dims(x_dim='lon', y_dim='lat')

bT.rio.to_raster(r"/air.2019.tiff")

This will remove any dimensions with size 1 from the air variable, which should allow you to use the rioxarray functions on it. Keep in mind that this may also affect the shape and data in the air variable, so you may need to check its dimensions and values after using the squeeze method to make sure it is still valid for your purposes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.