Creating netCDF files: How can I set the order of the dimensions in a field?

7 views (last 30 days)
I have created a netCDF file with 3 dimensions (dim1,dim2,dim3) and a field that is defined on the (dim1,dim2,dim3) space. ncdump gives me
NetCDF-3 Classic myfile.nc {
dimensions:
dim1 = 12 ;
dim2 = 96 ;
dim3 = 14 ;
variables:
// Preference 'PRESERVE_FVD': false,
// dimensions consistent with ncBrowse, not with native MATLAB netcdf package.
single dim1(dim1), shape = [12]
single dim2(dim2), shape = [96]
int32 dim3(dim3), shape = [14]
single field(dim3,dim2,dim1), shape = [14 96 12]
How can I set the order of the dimensions on which the field is defined, i.e. so that ncdump will give me
single field(dim1,dim2,dim3), shape = [12 96 14]
? This is what I run for creating the file:
file='infile.nc';
dim2 = nc_varget ( file, 'dim2' );
dim3 = nc_varget ( file, 'dim3' );
field = nc_varget ( file, 'field' );
vardata_dim1 = [1 2 3 4 5 6 7 8 9 10 11 12];
vardata_dim2 = dim2;
vardata_dim3 = dim3;
vardata_field = field;
ncid = netcdf.create('myfile.nc','NC_WRITE');
dim_dim1 = netcdf.defDim(ncid,'dim1',12);
dim_dim2 = netcdf.defDim(ncid,'dim2',96);
dim_dim3 = netcdf.defDim(ncid,'dim3',14);
varID_dim1 = netcdf.defVar(ncid,'dim1','float',dim_dim1);
varID_dim2 = netcdf.defVar(ncid,'dim2','float',dim_dim2);
varID_dim3 = netcdf.defVar(ncid,'dim3','int',dim_dim3);
varID_field = netcdf.defVar(ncid,'field','float',[dim_dim1 dim_dim2 dim_dim3]);
netcdf.endDef(ncid);
netcdf.putVar(ncid,varID_dim1,vardata_dim1);
netcdf.putVar(ncid,varID_dim2,vardata_dim2);
netcdf.putVar(ncid,varID_dim3,vardata_dim3);
netcdf.putVar(ncid,varID_field,vardata_field);
netcdf.close(ncid)

Accepted Answer

Ashish Uthama
Ashish Uthama on 19 Feb 2013
%Account for moving from Column major to Row major ordering
colMajorDims = [dim_dim1 dim_dim2 dim_dim3];
rowMajorDims = fliplr(colMajorDims);
varID_field = netcdf.defVar(ncid,'field','float',rowMajorDims);
vardata_field = permute(vardata_field, [3 2 1]);
This post http://www.mathworks.com/matlabcentral/newsreader/view_thread/245539 explains the observed behavior in some detail. Please post back if any of that explanation is not clear.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!