pydata / xarray

N-D labeled arrays and datasets in Python
https://xarray.dev
Apache License 2.0
3.61k stars 1.08k forks source link

Getting information on netcdf file with unlimited dimensions #7517

Open oliviermarti opened 1 year ago

oliviermarti commented 1 year ago

What is your issue?

When one reads a netCDF file, there is not solution to determine if there is an unlimited dimension, and determine which one.

I really need to be able to handle that. I need to process a variable, and write the result with the exact same informations about dimensions and coordinates, with all attributes and characteristics.

Thanks,

Olivier

import xarray as xr, os

print ( '==== Get an example file' )
file   = 'tas_Amon_IPSL-CM6A-LR_piControl_r1i1p1f1_gr_185001-234912.nc'
h_file = f"https://vesg.ipsl.upmc.fr/thredds/fileServer/cmip6/CMIP/IPSL/IPSL-CM6A-LR/piControl/r1i1p1f1/Amon/tas/gr/v20200326/{file}"

print ( '\n ==== Getting file ')
os.system ( f"wget --no-clobber {h_file}")

print ( '\n ==== File header : this file has an unlimited dimension "time"' )
os.system ( f"ncdump -h  {file} | head")

dd = xr.open_dataset ( file, decode_times=True, use_cftime=True)

xr.set_options ( display_expand_attrs=True)
print ( '\n ==== General information : no information about the unlimited dimension(s)' )
print (dd)

print ( '\n ==== Dimensions : no information about the unlimited dimension(s)')
print ( dd.dims )

print ( '\n === Attributes : no information about the unlimited dimension(s)' )
for attr in dd.attrs :
    print ( f'{attr} : {dd.attrs[attr]}' )
kmuehlbauer commented 1 year ago

Information on that is located in .encoding

dd.encoding
{'unlimited_dims': {'time'},
 'source': '/home/kai/python/gists/xarray/tas_Amon_IPSL-CM6A-LR_piControl_r1i1p1f1_gr_185001-234912.nc'}
kmuehlbauer commented 1 year ago

@oliviermarti After trying to find the related information in the documentation, I totally understand the question.

Here are some pointers, but the documentation should really be improved so that this is more discoverable:

https://docs.xarray.dev/en/stable/search.html?q=%22unlimited_dims%22#

Maybe those who have this in everyday use can immediately see where the docs can be enhanced.

oliviermarti commented 1 year ago

@kmuehlbauer thanks for the help, and thanks for the doc :-)