Sunday, August 29, 2010

Changing a fixed record dimension to unlimited

Sometimes, especially for concatenation, you need to change a fixed length dimension (usually time) to `UNLIMITED`.

To do this, create a netcdf file with only one (unlimited) dimension the same size and name as the dimension you want to convert.

nc = netCDF4.Dataset('unl_dim.nc', 'w', format='NETCDF3_CLASSIC')
nc.createDimension('time')
nc.dimensions['time'] = -1
nc.close()

This [python code](http://scipy.org), for example, creates an unlimited dimension with a length of one. Next, use `ncks` (one of the [netcdf operators](http://nco.sourceforge.net).) to append the old information to the new file.

ncks -A original_netcdf.nc unl_dim.nc

You will then need to move `unl_dim.nc` to the original file name.

Not the simplest hack, but pretty simple, and works.