source: trunk/src/struct2nc.m @ 439

Last change on this file since 439 was 404, checked in by sommeria, 13 years ago

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

File size: 6.0 KB
RevLine 
[106]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%       (optional) .ListGlobalAttribute: list (cell array of character strings) of the names of the global attributes Att_1, Att_2...
13%                  .Att_1,Att_2...: values of the global attributes
14%      (requested) .ListVarName: list of the variable names Var_1, Var_2....(cell array of character strings).
15%      (requested) .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells)
16%       (optional) .VarAttribute: cell array of structures of the form .VarAttribute{ivar}.key=value, defining an attribute key name and value for the variable #ivar
17%      (requested) .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
18
19%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
20%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
21%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
22%     This file is part of the toolbox UVMAT.
23%
24%     UVMAT is free software; you can redistribute it and/or modify
25%     it under the terms of the GNU General Public License as published by
26%     the Free Software Foundation; either version 2 of the License, or
27%     (at your option) any later version.
28%
29%     UVMAT is distributed in the hope that it will be useful,
30%     but WITHOUT ANY WARRANTY; without even the implied warranty of
31%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
33%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
34
35function errormsg=struct2nc(flname,Data)
36if ~ischar(flname)
[246]37    errormsg='invalid input for the netcf file name';
[106]38    return
39end
40if ~exist('Data','var')
41     errormsg='no data  input for the netcdf file';
42    return
43end
[404]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
[106]67                end
[404]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)
[106]74                end
75            end
76        end
77    end
[404]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);
[106]103                end
104            end
105        end
106    end
[404]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))
[382]123%                 errormsg=['wrong dimensions declared for ' ListVarName{ivar} ' in struct2nc.m'];
124%                 break
125%             end
[404]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     
[106]134    end
135end
[404]136netcdf.close(nc)
[106]137
[404]138
Note: See TracBrowser for help on using the repository browser.