source: trunk/src/private/struct2nc.m @ 8

Last change on this file since 8 was 8, checked in by gostiaux, 11 years ago
File size: 6.5 KB
Line 
1%'struct2nc': create a netcdf file from a Matlab structure
2%---------------------------------------------------------------------
3% errormsg=struct2nc(flname,Data)
4%
5%OUPUT:
6%errormsg=error message, =[]: default, no error
7%
8%INPUT:
9%flname: name of the netcdf file to create (must end with the extension '.nc')
10%  Data: structure containing all the information of the netcdf file (or netcdf object)
11%           with fields:
12%    .ListGlobalAttribute: cell listing the names of the global attributes (note that a global atribute with the same name as a variable is excluded)
13%        .Att_1,Att_2... : values of the global attributes
14%            .ListDimName: cell listing the names of the array dimensions
15%               .DimValue: array dimension values (Matlab vector with the same length as .ListDimName
16%            .ListVarName: cell listing the names of the variables
17%            .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName
18%           .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName
19%        .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
20%
21%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
22%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
23%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
24%     This file is part of the toolbox UVMAT.
25%
26%     UVMAT is free software; you can redistribute it and/or modify
27%     it under the terms of the GNU General Public License as published by
28%     the Free Software Foundation; either version 2 of the License, or
29%     (at your option) any later version.
30%
31%     UVMAT is distributed in the hope that it will be useful,
32%     but WITHOUT ANY WARRANTY; without even the implied warranty of
33%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
35%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
36
37function errormsg=struct2nc(flname,Data)
38if ~ischar(flname)
39    errormsg='no name input for the netcf file';
40    return
41end
42if ~exist('Data','var')
43     errormsg='no data  input for the netcdf file';
44    return
45end
46hhh=which('netcdf.create');% look for built-in matlab library
47
48%USE OF built-in  netcdf library
49if ~isequal(hhh,'')
50    FilePath=fileparts(flname);
51    if ~strcmp(FilePath,'') && ~exist(FilePath,'dir')
52        errormsg=['directory ' FilePath ' needs to be created'];
53        return
54    end
55    [Data,errormsg]=check_field_structure(Data);%check the validity of the input field structure
56    ListVarName=Data.ListVarName;
57    nc=netcdf.create(flname,'NC_CLOBBER');%,'clobber'); %create the netcdf file with name flname   
58    %write global constants
59    if isfield(Data,'ListGlobalAttribute')
60        keys=Data.ListGlobalAttribute;
61        for iattr=1:length(keys)
62            if isfield(Data,keys{iattr})
63                 testvar=0;
64                for ivar=1:length(ListVarName)% eliminate possible global attributes with the same name as a variable
65                    if isequal(ListVarName{ivar}, keys{iattr})
66                        testvar=1;
67                        break
68                    end
69                end
70                if ~testvar               
71                    eval(['cte=Data.' keys{iattr} ';'])
72                    if (ischar(cte) ||isnumeric(cte)) &&  ~isempty(cte)&& ~isequal(cte,'')
73                        netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),keys{iattr},cte)
74                    else
75                        errormsg='global attributes must be characters or numbers';
76                        return
77                    end
78                end
79            end
80        end
81    end
82    %create dimensions
83    dimid=[];
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=[];
94    for ivar=1:length(ListVarName)
95        varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},'double',dimid(Data.VarDimIndex{ivar}));%define variable 
96    end
97     %write variable attributes
98    if testattr
99        for ivar=1:length(VarAttribute) 
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                    netcdf.putAtt(nc,varid(ivar),attr_names{iattr},attr_val);
105                end
106            end
107        end
108    end
109    netcdf.endDef(nc); %put in data mode
110    for ivar=1:length(ListVarName)
111        if isfield(Data,ListVarName{ivar})
112            eval(['VarVal=Data.' ListVarName{ivar} ';'])%varval=values of the current variable
113            VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions
114            siz=size(VarVal);
115            VarDimName=Data.VarDimName{ivar};%NEW
116            if ischar(VarDimName)%NEW
117                VarDimName={VarDimName};%NEW
118            end%NEW
119            testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2); %NEW
120            testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), Data.DimValue(VarDimIndex));
121            testcolumn=isequal(length(siz),2) && isequal(siz(1), Data.DimValue(VarDimIndex))&& isequal(siz(2),1);
122            if ~testline && ~testcolumn && ~isequal(siz,Data.DimValue(VarDimIndex))
123                errormsg=['wrong dimensions declared for ' ListVarName{ivar} ' in struct2nc.m'];
124                break
125            end
126            if testline || testrange%NEW
127                if testrange
128                    VarVal=linspace(VarVal(1),VarVal(2),Data.DimValue(VarDimIndex));%NEW
129                end
130               %nc{ListVarName{ivar}}=ncfloat(Data.ListDimName(VarDimIndex));%vector of x coordinates
131               netcdf.putVar(nc,varid(ivar), VarVal');
132            else
133                netcdf.putVar(nc,varid(ivar), VarVal);
134                %nc{ListVarName{ivar}}(:) = VarVal;
135            end
136           
137        end
138    end
139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140%OLD netcdf toolbox
141else
142    errormsg=struct2nc_toolbox(flname,Data);
143end
144
Note: See TracBrowser for help on using the repository browser.