source: trunk/src/struct2nc.m @ 323

Last change on this file since 323 was 246, checked in by sommeria, 14 years ago

thin plate shell (patch) introduced

File size: 6.6 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%       (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)
37    errormsg='invalid input for the netcf file name';
38    return
39end
40if ~exist('Data','var')
41     errormsg='no data  input for the netcdf file';
42    return
43end
44hhh=which('netcdf.create');% look for built-in matlab netcdf library
45
46%USE OF built-in matlab netcdf library
47if ~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=['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
71                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
78                end
79            end