source: trunk/src/struct2nc_toolbox.m @ 611

Last change on this file since 611 was 107, checked in by sommeria, 14 years ago

aver_stat and time_series: bug repairs for reading images, for time series, the projection of images on a line now works
get_field: various bug repairs, export field on work space activated
struct2nc and struct2nc_toolbox.m: small bug repaired
nomtype2pair: test on string input introduced to avoid error
cell2tab: test on multiline input introduced
civ: pb about cell repair (bug with recent versions of matlab)

File size: 5.8 KB
Line 
1%'struct2nc_toolbox': create a netcdf file from a Matlab structure: use of netcdf toolbox
2%---------------------------------------------------------------------
3% errormsg=struct2nc_toolbox(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_toolbox(flname,Data)
38
39FilePath=fileparts(flname);
40if ~strcmp(FilePath,'') &&~exist(FilePath,'dir')
41    errormsg=['directory ' FilePath ' needs to be created'];
42    return
43end
44[Data,errormsg]=check_field_structure(Data);
45if ~isempty(errormsg)
46    return
47end
48ListVarName=Data.ListVarName;
49nc=netcdf(flname,'clobber'); %create the netcdf file with name flname
50%write global constants
51if isfield(Data,'ListGlobalAttribute')
52    keys=Data.ListGlobalAttribute;
53    for iattr=1:length(keys)
54        if isfield(Data,keys{iattr})
55             testvar=0;
56            for ivar=1:length(ListVarName)% eliminate possible global attributes with the same name as a variable
57                if isequal(ListVarName{ivar}, keys{iattr})
58                    testvar=1;
59                    break
60                end
61            end
62            if ~testvar               
63                eval(['cte=Data.' keys{iattr} ';'])
64                if ischar(cte) && ~isequal(cte,'')
65                    eval(['nc.' keys{iattr} '=''' cte ''';']);
66                elseif isnumeric(cte)&& ~isempty(cte)
67                    eval(['nc.' keys{iattr} '= cte; ']);
68                else
69                    errormsg='global attributes must be characters or numbers';
70                    return
71                end
72            end
73        end
74    end
75end
76for idim=1:length(Data.ListDimName)
77    nc(Data.ListDimName{idim})=Data.DimValue(idim);%create dimensions
78end
79
80VarAttribute={}; %default
81testattr=0;
82if isfield(Data,'VarAttribute')
83    VarAttribute=Data.VarAttribute;
84    testattr=1;
85end
86for ivar=1:length(ListVarName)
87    if isfield(Data,ListVarName{ivar})
88        eval(['VarVal=Data.' ListVarName{ivar} ';'])%varval=values of the current variable
89        siz=size(VarVal);
90        VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions
91        VarDimName=Data.VarDimName{ivar};
92        if ischar(VarDimName)
93            VarDimName={VarDimName};
94        end
95        testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2);
96        testline=isequal(length(siz),2) & isequal(siz(1),1)& isequal(siz(2), Data.DimValue(VarDimIndex));
97        testcolumn=isequal(length(siz),2) & isequal(siz(1), Data.DimValue(VarDimIndex))& isequal(siz(2),1);
98        if ~testrange && ~testline && ~testcolumn && ~isequal(siz,Data.DimValue(VarDimIndex))
99            errormsg=['wrong dimensions declared for ' ListVarName{ivar} ' in struct2nc.m'];
100            break
101        end
102        if testline || testrange
103           dimname=Data.ListDimName{VarDimIndex};
104           if testrange
105               VarVal=linspace(VarVal(1),VarVal(2),Data.DimValue(VarDimIndex));
106           end
107           nc{ListVarName{ivar}}=ncfloat(dimname);%vector of x coordinates
108           nc{ListVarName{ivar}}(:) = VarVal'; 
109        else
110            nc{ListVarName{ivar}}=ncfloat(Data.ListDimName(VarDimIndex));%vector of x coordinates
111            nc{ListVarName{ivar}}(:) = VarVal;
112        end
113    end
114end
115%write variable attributes
116if testattr
117    for ivar=1:min(numel(VarAttribute),numel(ListVarName))  %loop on the attributes of variable ivar
118        if isstruct(VarAttribute{ivar})
119            attr_names=fields(VarAttribute{ivar});
120            for iattr=1:length(attr_names)
121                eval(['attr_val=VarAttribute{ivar}.' attr_names{iattr} ';']);
122                if ischar(attr_val) && ~isequal(attr_val,'')
123                    eval(['nc{''' ListVarName{ivar} '''}.' attr_names{iattr} '=''' attr_val ''';'])
124                elseif isnumeric(attr_val)&& ~isempty(attr_val)
125                     eval(['nc{''' ListVarName{ivar} '''}.' attr_names{iattr} '=attr_val ;'])
126                end
127            end
128        end
129    end
130 end
131
132
133close(nc);
Note: See TracBrowser for help on using the repository browser.