Home > . > struct2nc_toolbox.m

struct2nc_toolbox

PURPOSE ^

'struct2nc_toolbox': create a netcdf file from a Matlab structure: use of netcdf toolbox

SYNOPSIS ^

function errormsg=struct2nc_toolbox(flname,Data)

DESCRIPTION ^

'struct2nc_toolbox': create a netcdf file from a Matlab structure: use of netcdf toolbox
---------------------------------------------------------------------
 errormsg=struct2nc_toolbox(flname,Data)

OUPUT:
errormsg=error message, =0: default, no error
INPUT:
flname: name of the netcdf file to create (must end with the extension '.nc')
  Data: structure containing all the information of the netcdf file (or netcdf object)
           with fields:
    .ListGlobalAttribute: cell listing the names of the global attributes (note that a global atribute with the same name as a variable is excluded)
        .Att_1,Att_2... : values of the global attributes
            .ListDimName: cell listing the names of the array dimensions
               .DimValue: array dimension values (Matlab vector with the same length as .ListDimName
            .ListVarName: cell listing the names of the variables
            .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName
           .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName
        .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
     This file is part of the toolbox UVMAT.
 
     UVMAT is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
     UVMAT is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License (file UVMAT/COPYING.txt) for more details.
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %'struct2nc_toolbox': create a netcdf file from a Matlab structure: use of netcdf toolbox
0002 %---------------------------------------------------------------------
0003 % errormsg=struct2nc_toolbox(flname,Data)
0004 %
0005 %OUPUT:
0006 %errormsg=error message, =0: default, no error
0007 %INPUT:
0008 %flname: name of the netcdf file to create (must end with the extension '.nc')
0009 %  Data: structure containing all the information of the netcdf file (or netcdf object)
0010 %           with fields:
0011 %    .ListGlobalAttribute: cell listing the names of the global attributes (note that a global atribute with the same name as a variable is excluded)
0012 %        .Att_1,Att_2... : values of the global attributes
0013 %            .ListDimName: cell listing the names of the array dimensions
0014 %               .DimValue: array dimension values (Matlab vector with the same length as .ListDimName
0015 %            .ListVarName: cell listing the names of the variables
0016 %            .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName
0017 %           .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName
0018 %        .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
0019 %
0020 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
0021 %  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
0022 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
0023 %     This file is part of the toolbox UVMAT.
0024 %
0025 %     UVMAT is free software; you can redistribute it and/or modify
0026 %     it under the terms of the GNU General Public License as published by
0027 %     the Free Software Foundation; either version 2 of the License, or
0028 %     (at your option) any later version.
0029 %
0030 %     UVMAT is distributed in the hope that it will be useful,
0031 %     but WITHOUT ANY WARRANTY; without even the implied warranty of
0032 %     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0033 %     GNU General Public License (file UVMAT/COPYING.txt) for more details.
0034 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
0035 
0036 function errormsg=struct2nc_toolbox(flname,Data)
0037 
0038 FilePath=fileparts(flname);
0039 if ~exist(FilePath,'dir')
0040     errormsg=['directory ' FilePath ' needs to be created'];
0041     return
0042 end
0043 [Data,errormsg]=check_field_structure(Data);
0044 if ~isempty(errormsg)
0045     return
0046 end
0047 ListVarName=Data.ListVarName;
0048 nc=netcdf(flname,'clobber'); %create the netcdf file with name flname
0049 %write global constants
0050 if isfield(Data,'ListGlobalAttribute')
0051     keys=Data.ListGlobalAttribute;
0052     for iattr=1:length(keys)
0053         if isfield(Data,keys{iattr})
0054              testvar=0;
0055             for ivar=1:length(ListVarName)% eliminate possible global attributes with the same name as a variable
0056                 if isequal(ListVarName{ivar}, keys{iattr})
0057                     testvar=1;
0058                     break
0059                 end
0060             end
0061             if ~testvar               
0062                 eval(['cte=Data.' keys{iattr} ';'])
0063                 if ischar(cte) && ~isequal(cte,'')
0064                     eval(['nc.' keys{iattr} '=''' cte ''';']);
0065                 elseif isnumeric(cte)&& ~isempty(cte)
0066                     eval(['nc.' keys{iattr} '= cte; ']);
0067                 else
0068                     errormsg='global attributes must be characters or numbers';
0069                     return
0070                 end
0071             end
0072         end
0073     end
0074 end
0075 for idim=1:length(Data.ListDimName)
0076     nc(Data.ListDimName{idim})=Data.DimValue(idim);%create dimensions
0077 end
0078 
0079 VarAttribute={}; %default
0080 testattr=0;
0081 if isfield(Data,'VarAttribute')
0082     VarAttribute=Data.VarAttribute;
0083     testattr=1;
0084 end
0085 for ivar=1:length(ListVarName)
0086     if isfield(Data,ListVarName{ivar})
0087         eval(['VarVal=Data.' ListVarName{ivar} ';'])%varval=values of the current variable
0088         siz=size(VarVal);
0089         VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions
0090         VarDimName=Data.VarDimName{ivar};%NEW
0091         if ischar(VarDimName)%NEW
0092             VarDimName={VarDimName};%NEW
0093         end%NEW
0094         testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2); %NEW
0095         testline=isequal(length(siz),2) & isequal(siz(1),1)& isequal(siz(2), Data.DimValue(VarDimIndex));
0096         testcolumn=isequal(length(siz),2) & isequal(siz(1), Data.DimValue(VarDimIndex))& isequal(siz(2),1);
0097         if ~testrange && ~testline && ~testcolumn && ~isequal(siz,Data.DimValue(VarDimIndex))
0098             errormsg=['wrong dimensions declared for ' ListVarName{ivar} ' in struct2nc.m'];
0099             break
0100         end 
0101         if testline || testrange%NEW
0102            dimname=Data.ListDimName{VarDimIndex};
0103            if testrange%NEW
0104                VarVal=linspace(VarVal(1),VarVal(2),Data.DimValue(VarDimIndex));%NEW
0105            end%NEW
0106            nc{ListVarName{ivar}}=ncfloat(dimname);%vector of x coordinates
0107            nc{ListVarName{ivar}}(:) = VarVal';  
0108         else
0109             nc{ListVarName{ivar}}=ncfloat(Data.ListDimName(VarDimIndex));%vector of x coordinates
0110             nc{ListVarName{ivar}}(:) = VarVal;
0111         end
0112         %write variable attributes
0113         if testattr
0114             for ivar=1:length(VarAttribute)  
0115                 if isstruct(VarAttribute{ivar})
0116                     attr_names=fields(VarAttribute{ivar});
0117                     for iattr=1:length(attr_names)
0118                         eval(['attr_val=VarAttribute{ivar}.' attr_names{iattr} ';']);
0119                         if ischar(attr_val) && ~isequal(attr_val,'')
0120                             eval(['nc{''' ListVarName{ivar} '''}.' attr_names{iattr} '=''' attr_val ''';'])
0121                         elseif isnumeric(attr_val)&& ~isempty(attr_val)
0122                              eval(['nc{''' ListVarName{ivar} '''}.' attr_names{iattr} '=attr_val ;'])
0123                         end
0124                     end
0125                 end
0126             end
0127          end
0128     end
0129 end
0130 
0131 close(nc);

Generated on Fri 13-Nov-2009 11:17:03 by m2html © 2003