view main/netcdf/src/ncwriteschema.m @ 11796:472856b658d9 octave-forge

initial import
author abarth93
date Sat, 15 Jun 2013 12:52:22 +0000
parents
children 6878acd4d824
line wrap: on
line source

function ncwriteschema(filename,s)

% normalize schema

if ~isfield(s,'Attributes')
  s.Attributes = struct('Name',{},'Value',{});
end

if ~isfield(s,'Variables')
  s.Variables = struct('Name',{},'Dimensions',{},'Datatype',{});;
end


mode = format2mode(s.Format);
ncid = netcdf_create(filename,mode);

% dimension
for i = 1:length(s.Dimensions)
  s.Dimensions(i).id = netcdf_defDim(ncid,s.Dimensions(i).Name,...
                                          s.Dimensions(i).Length);  
end

% global attributes
gid = netcdf_getConstant('NC_GLOBAL');
for j = 1:length(s.Attributes)
  netcdf_putAtt(ncid,gid,s.Attributes(j).Name,s.Attributes(j).Value);
end

% variables
for i = 1:length(s.Variables)  
  v = s.Variables(i);
  v.Name
  % get dimension id
  dimids = zeros(length(v.Dimensions),1);
  for j = 1:length(v.Dimensions)
    ind = find(strcmp({s.Dimensions.Name},v.Dimensions(j).Name));    
    if isempty(ind)
      error('netcdf:unknownDim','Unkown dimension %s',Dimensions(j).Name);
    end
    
    dimids(j) = s.Dimensions(ind).id;
  end
  
  % define variable
  switch lower(v.Datatype)
   case 'int32'
    dtype = 'int';
   case 'single'
    dtype = 'float';
   case 'double'
    dtype = 'double';
   case 'char'
    dtype = 'char';
   otherwise
    error('netcdf:unkownType','unknown type %s',v.Datatype);
  end  
  
  varid = netcdf_defVar(ncid,v.Name,dtype,dimids);
  
  % define attributes
  for j = 1:length(v.Attributes)
    netcdf_putAtt(ncid,varid,v.Attributes(j).Name,v.Attributes(j).Value);
  end
end

netcdf_close(ncid);