Writing serveal arrays to an axis in a netcdf file

3 views (last 30 days)
Hi,
I have a 114*150 cell array where each cell contains a vector that varies in size from cell to cell. I want to write these cell arrays to a netcdf file, so that I can correspondingly access each cell in the netcdf file (i.e. solution = netcdf.getVar(ncID,n)). I do not get the vectors I want when doing so. I hope someone with netcdf experience can help me.
Here is my code:
function obj = dumpNETCDF_NR(obj,Specs)
depthSize = cell2num(obj.Solutions.cord.z{end});
offsetSize = cell2num(obj.Solutions.cord.x{end});
ncID = netcdf.create(Specs.solutionFilePath,'NC_SHARE');
%Define dimensions
dimID_Solutions = netcdf.defDim(ncID,'PLF',netcdf.getConstant('NC_UNLIMITED'));
dimID_Depth = netcdf.defDim(ncID,'Depth',depthSize);
dimID_Offset = netcdf.defDim(ncID,'Offset',offsetSize);
%Define axis for depth
varID_Depth = netcdf.defVar(ncID,'Depth','double',dimID_Depth);
netcdf.putAtt(ncID,varID_Depth,'long_name','Depth');
netcdf.putAtt(ncID,varID_Depth,'units','Meters*10^1');
%Define axis for offset
varID_Offset = netcdf.defVar(ncID,'Offset','double',dimID_Offset);
netcdf.putAtt(ncID,varID_Offset,'long_name','Offset');
netcdf.putAtt(ncID,varID_Offset,'units','Meters*10^1');
% Define soltuions(PLF)axis:
varID_PLF = netcdf.defVar(ncID,'Porosity','double',dimID_Solutions); netcdf.putAtt(ncID,varID_PLF,'long_name','Porosity'));
netcdf.putAtt(ncID,varID_PLF,'units','Volume fractions'));
netcdf.putAtt(ncID,varID_PLF,'missing_value',-9999);
netcdf.endDef(ncID)
% Write solutions to netCDF:
for x = 1:offsetSize
for z = 1:depthSize
%Write solutions netcdf.putVar(ncID,varID_PLF,0,numel(obj.Solutions.Prop3.Porosity{z}{x}),...
obj.Solutions.Prop3.Porosity{z}{x})
end
end
%%Write depth and offset values to netCDF
netcdf.putVar(ncID,varID_Depth,1:depthSize);
netcdf.putVar(ncID,varID_Offset,1:offsetSize);
%%Read the netCDF file
netcdf.close(ncID)
obj.ncID = {};
obj.ncID = netcdf.open(fullfile(Specs.solutionFilePath),'NC_SHARE');
netcdf.close(obj.ncID)
Best regards, Kenneth
  1 Comment
Ashish Uthama
Ashish Uthama on 4 Jan 2013
Any reason this has to be a netcdf file? The data doesnt look well suited for this format. HDF5 might be more suitable perhaps.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!