Changeset 404 for trunk/src/struct2nc.m


Ignore:
Timestamp:
Apr 30, 2012, 6:46:45 PM (13 years ago)
Author:
sommeria
Message:

various bugs corrected. nc2struct_toolbox suppressed (correspond to obsolete versions of Matlab)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/struct2nc.m

    r382 r404  
    4242    return
    4343end
    44 hhh=which('netcdf.create');% look for built-in matlab netcdf library
    45 
    46 %USE OF built-in matlab netcdf library
    47 if ~isequal(hhh,'')
    48     FilePath=fileparts(flname);
    49     if ~strcmp(FilePath,'') && ~exist(FilePath,'dir')
    50         errormsg=['directory ' FilePath ' needs to be created'];
    51         return
    52     end
    53     [Data,errormsg]=check_field_structure(Data);%check the validity of the input field structure
    54     if ~isempty(errormsg)
    55         errormsg=['error in struct2nc:invalid input structure_' errormsg];
    56         return
    57     end
    58     ListVarName=Data.ListVarName;
    59     nc=netcdf.create(flname,'NC_CLOBBER');%,'clobber'); %create the netcdf file with name flname   
    60     %write global constants
    61     if isfield(Data,'ListGlobalAttribute')
    62         keys=Data.ListGlobalAttribute;
    63         for iattr=1:length(keys)
    64             if isfield(Data,keys{iattr})
    65                  testvar=0;
    66                 for ivar=1:length(ListVarName)% eliminate possible global attributes with the same name as a variable
    67                     if isequal(ListVarName{ivar}, keys{iattr})
    68                         testvar=1;
    69                         break
    70                     end
     44FilePath=fileparts(flname);
     45if ~strcmp(FilePath,'') && ~exist(FilePath,'dir')
     46    errormsg=['directory ' FilePath ' needs to be created'];
     47    return
     48end
     49%  [Data,errormsg]=check_field_structure(Data);%check the validity of the input field structure
     50[errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data);
     51if ~isempty(errormsg)
     52    errormsg=['error in struct2nc:invalid input structure_' errormsg];
     53    return
     54end
     55ListVarName=Data.ListVarName;
     56nc=netcdf.create(flname,'NC_CLOBBER');%,'clobber'); %create the netcdf file with name flname   
     57%write global constants
     58if isfield(Data,'ListGlobalAttribute')
     59    keys=Data.ListGlobalAttribute;
     60    for iattr=1:length(keys)
     61        if isfield(Data,keys{iattr})
     62             testvar=0;
     63            for ivar=1:length(ListVarName)% eliminate possible global attributes with the same name as a variable
     64                if isequal(ListVarName{ivar}, keys{iattr})
     65                    testvar=1;
     66                    break
    7167                end
    72                 if ~testvar               
    73                     eval(['cte=Data.' keys{iattr} ';'])
    74                     if (ischar(cte) ||isnumeric(cte)) &&  ~isempty(cte)%&& ~isequal(cte,'')
    75                         %write constant only if it is numeric or char string, and not empty
    76                         netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),keys{iattr},cte)
    77                     end
     68            end
     69            if ~testvar               
     70                eval(['cte=Data.' keys{iattr} ';'])
     71                if (ischar(cte) ||isnumeric(cte)) &&  ~isempty(cte)%&& ~isequal(cte,'')
     72                    %write constant only if it is numeric or char string, and not empty
     73                    netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),keys{iattr},cte)
    7874                end
    7975            end
    8076        end
    8177    end
    82     %create dimensions
    83     dimid=zeros(1,length(Data.ListDimName));
    84     for idim=1:length(Data.ListDimName)
    85          dimid(idim) = netcdf.defDim(nc,Data.ListDimName{idim},Data.DimValue(idim));
    86     end
    87     VarAttribute={}; %default
    88     testattr=0;
    89     if isfield(Data,'VarAttribute')
    90         VarAttribute=Data.VarAttribute;
    91         testattr=1;
    92     end
    93     varid=zeros(1,length(Data.ListVarName));
    94     for ivar=1:length(ListVarName)
    95         varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},'nc_double',dimid(Data.VarDimIndex{ivar}));%define variable 
    96     end
    97      %write variable attributes
    98     if testattr
    99         for ivar=1:min(numel(VarAttribute),numel(ListVarName)) 
    100             if isstruct(VarAttribute{ivar})
    101                 attr_names=fields(VarAttribute{ivar});
    102                 for iattr=1:length(attr_names)
    103                     eval(['attr_val=VarAttribute{ivar}.' attr_names{iattr} ';']);
    104                     if ~isempty(attr_names{iattr})&& ~isempty(attr_val)
    105                         netcdf.putAtt(nc,varid(ivar),attr_names{iattr},attr_val);
    106                     end
     78end
     79%create dimensions
     80dimid=zeros(1,length(ListDimName));
     81for idim=1:length(ListDimName)
     82     dimid(idim) = netcdf.defDim(nc,ListDimName{idim},DimValue(idim));
     83end
     84VarAttribute={}; %default
     85testattr=0;
     86if isfield(Data,'VarAttribute')
     87    VarAttribute=Data.VarAttribute;
     88    testattr=1;
     89end
     90varid=zeros(1,length(Data.ListVarName));
     91for ivar=1:length(ListVarName)
     92    varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},'nc_double',dimid(VarDimIndex{ivar}));%define variable 
     93end
     94 %write variable attributes
     95if testattr
     96    for ivar=1:min(numel(VarAttribute),numel(ListVarName)) 
     97        if isstruct(VarAttribute{ivar})
     98            attr_names=fields(VarAttribute{ivar});
     99            for iattr=1:length(attr_names)
     100                eval(['attr_val=VarAttribute{ivar}.' attr_names{iattr} ';']);
     101                if ~isempty(attr_names{iattr})&& ~isempty(attr_val)
     102                    netcdf.putAtt(nc,varid(ivar),attr_names{iattr},attr_val);
    107103                end
    108104            end
    109105        end
    110106    end
    111     netcdf.endDef(nc); %put in data mode
    112     for ivar=1:length(ListVarName)
    113         if isfield(Data,ListVarName{ivar})
    114             VarVal=Data.(ListVarName{ivar});
    115             %varval=values of the current variable
    116             VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions
    117             VarDimName=Data.VarDimName{ivar};
    118             if ischar(VarDimName)
    119                 VarDimName={VarDimName};
    120             end
    121             siz=size(VarVal);
    122             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.
    123             testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), Data.DimValue(VarDimIndex));%matlab vector
    124             testcolumn=isequal(length(siz),2) && isequal(siz(1), Data.DimValue(VarDimIndex))&& isequal(siz(2),1);%matlab column vector
    125 %             if ~testrange && ~testline && ~testcolumn && ~isequal(siz,Data.DimValue(VarDimIndex))
     107end
     108netcdf.endDef(nc); %put in data mode
     109for ivar=1:length(ListVarName)
     110    if isfield(Data,ListVarName{ivar})
     111        VarVal=Data.(ListVarName{ivar});
     112        %varval=values of the current variable
     113%        VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions
     114        VarDimName=Data.VarDimName{ivar};
     115        if ischar(VarDimName)
     116            VarDimName={VarDimName};
     117        end
     118        siz=size(VarVal);
     119        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.
     120        testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), DimValue(VarDimIndex{ivar}));%matlab vector
     121        testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector
     122%             if ~testrange && ~testline && ~testcolumn && ~isequal(siz,DimValue(VarDimIndex))
    126123%                 errormsg=['wrong dimensions declared for ' ListVarName{ivar} ' in struct2nc.m'];
    127124%                 break
    128125%             end
    129             if testline || testrange
    130                 if testrange
    131                     VarVal=linspace(VarVal(1),VarVal(2),Data.DimValue(VarDimIndex));% restitute the whole array of coordinate values
    132                 end
    133                 netcdf.putVar(nc,varid(ivar), double(VarVal'));
    134             else
    135                 netcdf.putVar(nc,varid(ivar), double(VarVal));
    136             end     
    137         end
     126        if testline || testrange
     127            if testrange
     128                VarVal=linspace(VarVal(1),VarVal(2),DimValue(VarDimIndex{ivar}));% restitute the whole array of coordinate values
     129            end
     130            netcdf.putVar(nc,varid(ivar), double(VarVal'));
     131        else
     132            netcdf.putVar(nc,varid(ivar), double(VarVal));
     133        end     
    138134    end
    139     netcdf.close(nc)
    140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    141 %OLD netcdf toolbox
    142 else
    143     errormsg=struct2nc_toolbox(flname,Data);
    144135end
     136netcdf.close(nc)
    145137
     138
Note: See TracChangeset for help on using the changeset viewer.