Seb
Seb

Reputation: 1775

Concatenate netcdf files with multiple 'record' dimensions

Let's consider those two files:

from netCDF4 import Dataset as dset

for i in range(2):
     with dset('test_{}.nc'.format(i),'w') as f:
         f.createDimension('A',5)
         f.createDimension('B',8)
         f.createVariable('v1',float,('A',))
         f.createVariable('v2',float,('B',))
         f['v1'][:] = range(i*5,i*5+5)
         f['v2'][:] = range(i*8,i*8+8)

If the two dimensions are unlimited ncrcat works directly and concatenate v2 along B and v1 along A.

ncrcat test_0.nc test_1.nc test_01.nc

However if the dimensions are fixed size like in the example above I have to successively set A and B as record dimension to make them unlimited and then concatenate

ncks --mk_rec_dmn A test_0.nc test_0u.nc ; mv test_0u.nc test_0.nc
ncks --mk_rec_dmn B test_0.nc test_0u.nc ; mv test_0u.nc test_0.nc
ncks --mk_rec_dmn A test_1.nc test_1u.nc ; mv test_1u.nc test_1.nc
ncks --mk_rec_dmn B test_1.nc test_1u.nc ; mv test_1u.nc test_1.nc
ncrcat test_0.nc test_1.nc test_01.nc

Is there another way to do this with less lines?

Upvotes: 0

Views: 471

Answers (1)

Charlie Zender
Charlie Zender

Reputation: 6352

Unfortunate --mk_rec_dmn only changes one dimension per invocation. Changing multiple fixed dimensions into record dimensions is on our TODO list (#1129). However, you can eliminate the mv statements by using the overwrite functionality with -O:

ncks -O --mk_rec_dmn A test_0.nc test_0.nc
ncks -O --mk_rec_dmn B test_0.nc test_0.nc
ncks -O --mk_rec_dmn A test_1.nc test_1.nc
ncks -O --mk_rec_dmn B test_1.nc test_1.nc
ncrcat test_0.nc test_1.nc test_01.nc

HTH, cz

Upvotes: 1

Related Questions