source: trunk/src/struct2nc.m @ 60

Last change on this file since 60 was 55, checked in by sommeria, 14 years ago

-use of a single parameter file PARAM.xml (instead of PARAM_WIN and PARAM_LINUX).
-correction of a bug for opening an existing projection object
-manual editing of vectors made available with the builtin netcdf tool.
-bug fix for reading netcdf files with the builtin netcdf tool.

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