Why can't the downloaded nc file be read with the xarray library?

import cdsapi

c = cdsapi.Client()

c.retrieve(
‘reanalysis-era5-land’,
{
‘variable’: [
‘10m_u_component_of_wind’,
‘10m_v_component_of_wind’,
‘total_precipitation’,
],
‘year’: [
‘2023’,‘2024’,
],
‘month’: ‘07’,
‘day’: [
‘22’,‘23’,‘24’,‘25’,‘26’,‘27’,‘28’,‘29’,‘30’,‘31’,
],
‘time’: [
‘00:00’,‘01:00’,‘02:00’,‘03:00’,‘04:00’,‘05:00’,
‘06:00’,‘07:00’,‘08:00’,‘09:00’,‘10:00’,‘11:00’,
‘12:00’,‘13:00’,‘14:00’,‘15:00’,‘16:00’,‘17:00’,
‘18:00’,‘19:00’,‘20:00’,‘21:00’,‘22:00’,‘23:00’,
],
‘format’: ‘netcdf’,
‘area’: [25.933333, 117.583333, 24.383333, 119.083333],
},
‘tp.nc’
)

I have two nc files, opened using xarray, one can be read normally, one reports an error. The normal file was downloaded on 2025.01.08, the one that reported the error was downloaded on 2025.02.10 When I looked for a solution on the internet, I was told that it was a corrupted file

This may or may not be the case in this situation, but I had a similar issue.

It turned out my NetCDF file wasn’t a NetCDF file at all, but a zip-file.

Somewhere along the line the generation of files has changed drastically and if you request data across certain type-boundaries, like seas vs land or instant vs accumulated, the API will no longer honor your request for a singular NetCDF file, and instead generate a zip-file with multiple NetCDF files instead.

This zip-file will then contain a .nc file for each of those groups. In my case a single file now suddenly had become three. One for “instant land”, one for “accumulated land” and one for “accumulated sea” values. I believe this has to do with improvements to data formatting itself, even if I don’t like how these changes are being made without proper API versioning.

1 Like

This is probably the issue. I had to address this myself for the R API client, which detects if it is a zip upon download and renames the file consistently. Sadly, no such feature exists in the official python package it seems.

Thanks for the ideas, my problem may be due to downloading data across oceans and land, I’ll try again.

I had the same issue and I know the solution. The keyword to specify the output format has changed and now it is data_format and not format as it was previously. If you use latter, the output will be in GRIB format - the default format (without any warning).
If you are lucky you can just open it with xarray + cfgrib, although the data structure will be a bit different…

Thanks for your ideas!GOOD LUCK!