Search code examples
pythonnetcdf

Adding unlimited dimension to existing netcdf file


I currently have several netCDF4 files with a variable "pp" with three dimensions: pp(time, lat, lon). Each separate file has monthly soil moisture data for a year, and the time dimension stands for each month in the year. This is an example of the pp variable in the p04 file, which stores date for 2004:

p04.variables['pp']
Out[56]: 
<class 'netCDF4._netCDF4.Variable'>
int16 pp(time, lat, lon)
    _FillValue: -999.9
    units: mm
    long_name: Total precipitation
    add_offset: 19.0333409628
    scale_factor: -0.000580886924338
unlimited dimensions: 
current shape = (12, 8520, 7320)
filling on

I would like to add a fourth unlimited dimension so that the variable would have the shape pp(unl, time, lat, lon) and I could aggregate with MFDataset the multiple files from different years. I have tried to use createDimension but it erases all the information in the data.

I was looking for a way to add this extra dimension without having to re-write all the files. How would I do that? Thanks!


Solution

  • NCO's ncecat concatenates files by creating a new unlimited "glue" dimension:

    ncecat -u unl in*.nc out.nc