Changeset 919 for trunk/src/struct2nc.m


Ignore:
Timestamp:
Jun 26, 2015, 5:14:45 PM (9 years ago)
Author:
sommeria
Message:

netcdf files stored in 32 bits (not double)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/struct2nc.m

    r913 r919  
    4949    return
    5050end
    51 %check the validity of the input field structure
     51
     52%% check the validity of the input field structure
    5253[errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data);
    5354if ~isempty(errormsg)
     
    5657end
    5758ListVarName=Data.ListVarName;
    58 %nc=netcdf.create(flname,'NC_CLOBBER');%,'clobber'); %create the netcdf file with name flname
     59
     60%% create the netcdf file with name flname in format NETCDF4
    5961cmode = netcdf.getConstant('NETCDF4');
    6062cmode = bitor(cmode, netcdf.getConstant('CLASSIC_MODEL'));
    6163cmode = bitor(cmode, netcdf.getConstant('CLOBBER'));
    62 nc = netcdf.create(flname, cmode)
    63 
    64 %write global constants
     64nc = netcdf.create(flname, cmode);
     65
     66%% write global constants
    6567if isfield(Data,'ListGlobalAttribute')
    6668    keys=Data.ListGlobalAttribute;
     
    8486    end
    8587end
    86 %create dimensions
     88
     89%% create the dimensions
    8790dimid=zeros(1,length(ListDimName));
    8891for idim=1:length(ListDimName)
     
    9598    testattr=1;
    9699end
    97 varid=zeros(1,length(Data.ListVarName));
     100
     101
     102%% create the variables
     103varid=nan(1,length(Data.ListVarName));
    98104for ivar=1:length(ListVarName)
    99     varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},'nc_double',dimid(VarDimIndex{ivar}));%define variable 
    100 end
    101  %write variable attributes
     105    if isfield(Data,ListVarName{ivar})
     106        VarClass=class(Data.(ListVarName{ivar}));
     107        VarType='';
     108        switch VarClass
     109            case {'single','double'}
     110                VarType='nc_float'; % store all floating reals as single
     111            case {'uint8','int16','uint16','int32','uint32','int64','uint64'}
     112                VarType='nc_int';
     113            case  'logical'
     114                VarType='nc_byte';
     115        end
     116        if ~isempty(VarType)
     117            varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},VarType,dimid(VarDimIndex{ivar}));%define variable
     118        end
     119    end
     120end
     121
     122%% write variable attributes
    102123if testattr
    103124    for ivar=1:min(numel(VarAttribute),numel(ListVarName)) 
    104         if isstruct(VarAttribute{ivar})
     125        if isstruct(VarAttribute{ivar}) && ~isnan(varid(ivar))
    105126            attr_names=fields(VarAttribute{ivar});
    106127            for iattr=1:length(attr_names)
     
    114135end
    115136netcdf.endDef(nc); %put in data mode
     137
     138%% fill the variables with input data
    116139for ivar=1:length(ListVarName)
    117     if isfield(Data,ListVarName{ivar})
     140    if ~isnan(varid(ivar))
    118141        VarVal=Data.(ListVarName{ivar});
    119142        %varval=values of the current variable
    120 %        VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions
    121143        VarDimName=Data.VarDimName{ivar};
    122144        if ischar(VarDimName)
     
    126148        testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2);% case of a coordinate defined on a regular mesh by the first and last values.
    127149        testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), DimValue(VarDimIndex{ivar}));%matlab vector
    128         testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector
    129 %             if ~testrange && ~testline && ~testcolumn && ~isequal(siz,DimValue(VarDimIndex))
    130 %                 errormsg=['wrong dimensions declared for ' ListVarName{ivar} ' in struct2nc.m'];
    131 %                 break
    132 %             end
     150        %testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector
    133151        if testline || testrange
    134152            if testrange
Note: See TracChangeset for help on using the changeset viewer.