Hello Matlab Fans,
I am having major issues writing out 4d ocean model data to a NetCDF file using direct calls to mexnc (see mwe below), can anyone help me? I have checked that the data looks fine on the way in, but loaded back into Matlab it looks completely scrambled…I'm using mexnc because I dont have access to the netcdf.func routines and the implimented way of doing it by calling ncfile{'ncvar'}=variable no longer works (post matlab-r2008ish).
% Test case for NetCDF writing
clear allncdata_T=1:1:200;ncdata_X=1.4062:2.8125:358.59375;ncdata_Y=-88.59375:2.8125:88.59375;ncdata_Z=[-25,-85,-170,-290,-455,-670,-935,-1250,... -1615,-2030,-2495,-3010,-3575,-4190,-4855];load clown% Manipulate clown data to reflect my model dimensions
X=flipud(X);xtmp=1:(320/128):320;ytmp=1:(200/64):200;[xi,yi]=meshgrid(xtmp,ytmp);ncdata_clownface=interp2(X,xi,yi);clear X xtmp ytmp xi yi% Create 4d dataset x,y,z,t
ncdata_clownface=repmat(ncdata_clownface',[1 1 15 200]);% Open new file
[ncid,status] = mexnc ('create','mitgcm_clownface.nc','clobber');% Define dimensions
% [dimid,status] = mexnc('def_dim',ncid,dim_name,dim_length);
% T is the unlimited dimension, hence size is zero.
[dimid.T,status] = mexnc('def_dim',ncid,'T',0);[dimid.X,status] = mexnc('def_dim',ncid,'X',128);[dimid.Y,status] = mexnc('def_dim',ncid,'Y',64);[dimid.Z,status] = mexnc('def_dim',ncid,'Z',15);% Define variables
% [varid,status] = mexnc('def_var',ncid,var_name,data_type,ndims,dimids);
[varid.T,status] = mexnc('def_var',ncid,'T','double',1,dimid.T);[varid.X,status] = mexnc('def_var',ncid,'X','double',1,dimid.X);[varid.Y,status] = mexnc('def_var',ncid,'Y','double',1,dimid.Y);[varid.Z,status] = mexnc('def_var',ncid,'Z','double',1,dimid.Z);[varid.clown,status] = mexnc('def_var',ncid,'Clownface','float',... 4,[dimid.T dimid.Z dimid.Y dimid.X]);% End definition mode
status = mexnc('enddef',ncid);% Add Data
%status = mexnc('put_vara_datatype',ncid,varid,start,count,data);
status=mexnc('put_vara_double',ncid,varid.T,[0],[200],ncdata_T);status=mexnc('put_vara_double',ncid,varid.X,[0],[128],ncdata_X);status=mexnc('put_vara_double',ncid,varid.Y,[0],[64],ncdata_Y);status=mexnc('put_vara_double',ncid,varid.Z,[0],[15],ncdata_Z);% Reshape data to match the data I get after processing. It is in t,z,y,x order
% changing pref('SNCTOOLS','PRESERVE_FVD', true/false);
% doesnt seem to make a difference
ncdata_clownface=permute(ncdata_clownface,[4,3,2,1]);status=mexnc('put_vara_double',ncid,varid.clown,[0 0 0 0],... [200 15 64 128],ncdata_clownface);% Close the NetCDF file
status=mexnc('close',ncid);
Unfortunately, when I load the just-written netcdf data, I find that:
pcolor(squeeze(ncdata_clownface(1,1,:,:))) % plot the input
is not the same as:
ncload('mitgcm_clownface.nc')pcolor(squeeze(Clownface(1,1,:,:))) % plot the output
The data ranges are indeed the same and size(Clownface)==size(ncdata_clownface). The 1d variables are written and recovered correctly. To paraphrase Eric Morecambe, I think the right numbers are being output, but not necessarily in the right order….any ideas?
Thanks, Jonathan Lauderdale
Best Answer