1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" |
---|
2 | "http://www.w3.org/TR/REC-html40/loose.dtd"> |
---|
3 | <html> |
---|
4 | <head> |
---|
5 | <title>Description of struct2nc</title> |
---|
6 | <meta name="keywords" content="struct2nc"> |
---|
7 | <meta name="description" content="'struct2nc': create a netcdf file from a Matlab structure"> |
---|
8 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
---|
9 | <meta name="generator" content="m2html © 2003 Guillaume Flandin"> |
---|
10 | <meta name="robots" content="index, follow"> |
---|
11 | <link type="text/css" rel="stylesheet" href="../m2html.css"> |
---|
12 | </head> |
---|
13 | <body> |
---|
14 | <a name="_top"></a> |
---|
15 | <div><a href="../index.html">Home</a> > <a href="index.html">.</a> > struct2nc.m</div> |
---|
16 | |
---|
17 | <!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png"> Master index</a></td> |
---|
18 | <td align="right"><a href="index.html">Index for . <img alt=">" border="0" src="../right.png"></a></td></tr></table>--> |
---|
19 | |
---|
20 | <h1>struct2nc |
---|
21 | </h1> |
---|
22 | |
---|
23 | <h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
24 | <div class="box"><strong>'struct2nc': create a netcdf file from a Matlab structure</strong></div> |
---|
25 | |
---|
26 | <h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
27 | <div class="box"><strong>function errormsg=struct2nc(flname,Data) </strong></div> |
---|
28 | |
---|
29 | <h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
30 | <div class="fragment"><pre class="comment">'struct2nc': create a netcdf file from a Matlab structure |
---|
31 | --------------------------------------------------------------------- |
---|
32 | error=struct2nc(flname,Data) |
---|
33 | |
---|
34 | OUPUT: |
---|
35 | error=error message, =0: default, no error |
---|
36 | INPUT: |
---|
37 | flname: name of the netcdf file to create (must end with the extension '.nc') |
---|
38 | Data: structure containing all the information of the netcdf file (or netcdf object) |
---|
39 | with fields: |
---|
40 | .ListGlobalAttribute: cell listing the names of the global attributes (note that a global atribute with the same name as a variable is excluded) |
---|
41 | .Att_1,Att_2... : values of the global attributes |
---|
42 | .ListDimName: cell listing the names of the array dimensions |
---|
43 | .DimValue: array dimension values (Matlab vector with the same length as .ListDimName |
---|
44 | .ListVarName: cell listing the names of the variables |
---|
45 | .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName |
---|
46 | .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName |
---|
47 | .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName |
---|
48 | |
---|
49 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
50 | Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org. |
---|
51 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
52 | This file is part of the toolbox UVMAT. |
---|
53 | |
---|
54 | UVMAT is free software; you can redistribute it and/or modify |
---|
55 | it under the terms of the GNU General Public License as published by |
---|
56 | the Free Software Foundation; either version 2 of the License, or |
---|
57 | (at your option) any later version. |
---|
58 | |
---|
59 | UVMAT is distributed in the hope that it will be useful, |
---|
60 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
61 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
62 | GNU General Public License (file UVMAT/COPYING.txt) for more details. |
---|
63 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</pre></div> |
---|
64 | |
---|
65 | <!-- crossreference --> |
---|
66 | <h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
67 | This function calls: |
---|
68 | <ul style="list-style-image:url(../matlabicon.gif)"> |
---|
69 | <li><a href="check_field_structure.html" class="code" title="function [DataOut,errormsg]=check_field_structure(Data)">check_field_structure</a> 'check_field_structure': check the representation of fields by a Matlab structure</li><li><a href="struct2nc_toolbox.html" class="code" title="function errormsg=struct2nc_toolbox(flname,Data)">struct2nc_toolbox</a> 'struct2nc_toolbox': create a netcdf file from a Matlab structure: use of netcdf toolbox</li></ul> |
---|
70 | This function is called by: |
---|
71 | <ul style="list-style-image:url(../matlabicon.gif)"> |
---|
72 | <li><a href="RUN_STLIN.html" class="code" title="function RUN_STLIN(file_A,file_B,vel_type,file_st,nx_patch,ny_patch,thresh_patch,fileAxml,fileBxml)">RUN_STLIN</a> 'RUN_STLIN': combine velocity fields for stereo PIV</li><li><a href="series.html" class="code" title="function varargout = series(varargin)">series</a> 'series': master function associated to the GUI series.m for analysis field series</li></ul> |
---|
73 | <!-- crossreference --> |
---|
74 | |
---|
75 | |
---|
76 | <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
77 | <div class="fragment"><pre>0001 <span class="comment">%'struct2nc': create a netcdf file from a Matlab structure</span> |
---|
78 | 0002 <span class="comment">%---------------------------------------------------------------------</span> |
---|
79 | 0003 <span class="comment">% error=struct2nc(flname,Data)</span> |
---|
80 | 0004 <span class="comment">%</span> |
---|
81 | 0005 <span class="comment">%OUPUT:</span> |
---|
82 | 0006 <span class="comment">%error=error message, =0: default, no error</span> |
---|
83 | 0007 <span class="comment">%INPUT:</span> |
---|
84 | 0008 <span class="comment">%flname: name of the netcdf file to create (must end with the extension '.nc')</span> |
---|
85 | 0009 <span class="comment">% Data: structure containing all the information of the netcdf file (or netcdf object)</span> |
---|
86 | 0010 <span class="comment">% with fields:</span> |
---|
87 | 0011 <span class="comment">% .ListGlobalAttribute: cell listing the names of the global attributes (note that a global atribute with the same name as a variable is excluded)</span> |
---|
88 | 0012 <span class="comment">% .Att_1,Att_2... : values of the global attributes</span> |
---|
89 | 0013 <span class="comment">% .ListDimName: cell listing the names of the array dimensions</span> |
---|
90 | 0014 <span class="comment">% .DimValue: array dimension values (Matlab vector with the same length as .ListDimName</span> |
---|
91 | 0015 <span class="comment">% .ListVarName: cell listing the names of the variables</span> |
---|
92 | 0016 <span class="comment">% .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName</span> |
---|
93 | 0017 <span class="comment">% .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName</span> |
---|
94 | 0018 <span class="comment">% .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName</span> |
---|
95 | 0019 <span class="comment">%</span> |
---|
96 | 0020 <span class="comment">%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</span> |
---|
97 | 0021 <span class="comment">% Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.</span> |
---|
98 | 0022 <span class="comment">%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</span> |
---|
99 | 0023 <span class="comment">% This file is part of the toolbox UVMAT.</span> |
---|
100 | 0024 <span class="comment">%</span> |
---|
101 | 0025 <span class="comment">% UVMAT is free software; you can redistribute it and/or modify</span> |
---|
102 | 0026 <span class="comment">% it under the terms of the GNU General Public License as published by</span> |
---|
103 | 0027 <span class="comment">% the Free Software Foundation; either version 2 of the License, or</span> |
---|
104 | 0028 <span class="comment">% (at your option) any later version.</span> |
---|
105 | 0029 <span class="comment">%</span> |
---|
106 | 0030 <span class="comment">% UVMAT is distributed in the hope that it will be useful,</span> |
---|
107 | 0031 <span class="comment">% but WITHOUT ANY WARRANTY; without even the implied warranty of</span> |
---|
108 | 0032 <span class="comment">% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span> |
---|
109 | 0033 <span class="comment">% GNU General Public License (file UVMAT/COPYING.txt) for more details.</span> |
---|
110 | 0034 <span class="comment">%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</span> |
---|
111 | 0035 |
---|
112 | 0036 <a name="_sub0" href="#_subfunctions" class="code">function errormsg=struct2nc(flname,Data)</a> |
---|
113 | 0037 |
---|
114 | 0038 hhh=which(<span class="string">'netcdf.create'</span>);<span class="comment">% look for built-in matlab library</span> |
---|
115 | 0039 |
---|
116 | 0040 <span class="comment">%USE OF built-in netcdf library</span> |
---|
117 | 0041 <span class="keyword">if</span> ~isequal(hhh,<span class="string">''</span>) |
---|
118 | 0042 FilePath=fileparts(flname); |
---|
119 | 0043 <span class="keyword">if</span> ~exist(FilePath,<span class="string">'dir'</span>) |
---|
120 | 0044 errormsg=[<span class="string">'directory '</span> FilePath <span class="string">' needs to be created'</span>]; |
---|
121 | 0045 <span class="keyword">return</span> |
---|
122 | 0046 <span class="keyword">end</span> |
---|
123 | 0047 [Data,errormsg]=<a href="check_field_structure.html" class="code" title="function [DataOut,errormsg]=check_field_structure(Data)">check_field_structure</a>(Data); |
---|
124 | 0048 ListVarName=Data.ListVarName; |
---|
125 | 0049 nc=netcdf.create(flname,<span class="string">'NC_CLOBBER'</span>);<span class="comment">%,'clobber'); %create the netcdf file with name flname</span> |
---|
126 | 0050 <span class="comment">%write global constants</span> |
---|
127 | 0051 <span class="keyword">if</span> isfield(Data,<span class="string">'ListGlobalAttribute'</span>) |
---|
128 | 0052 keys=Data.ListGlobalAttribute; |
---|
129 | 0053 <span class="keyword">for</span> iattr=1:length(keys) |
---|
130 | 0054 <span class="keyword">if</span> isfield(Data,keys{iattr}) |
---|
131 | 0055 testvar=0; |
---|
132 | 0056 <span class="keyword">for</span> ivar=1:length(ListVarName)<span class="comment">% eliminate possible global attributes with the same name as a variable</span> |
---|
133 | 0057 <span class="keyword">if</span> isequal(ListVarName{ivar}, keys{iattr}) |
---|
134 | 0058 testvar=1; |
---|
135 | 0059 <span class="keyword">break</span> |
---|
136 | 0060 <span class="keyword">end</span> |
---|
137 | 0061 <span class="keyword">end</span> |
---|
138 | 0062 <span class="keyword">if</span> ~testvar |
---|
139 | 0063 eval([<span class="string">'cte=Data.'</span> keys{iattr} <span class="string">';'</span>]) |
---|
140 | 0064 <span class="keyword">if</span> (ischar(cte) ||isnumeric(cte)) && ~isempty(cte)&& ~isequal(cte,<span class="string">''</span>) |
---|
141 | 0065 netcdf.putAtt(nc,netcdf.getConstant(<span class="string">'NC_GLOBAL'</span>),keys{iattr},cte) |
---|
142 | 0066 <span class="keyword">else</span> |
---|
143 | 0067 errormsg=<span class="string">'global attributes must be characters or numbers'</span>; |
---|
144 | 0068 <span class="keyword">return</span> |
---|
145 | 0069 <span class="keyword">end</span> |
---|
146 | 0070 <span class="keyword">end</span> |
---|
147 | 0071 <span class="keyword">end</span> |
---|
148 | 0072 <span class="keyword">end</span> |
---|
149 | 0073 <span class="keyword">end</span> |
---|
150 | 0074 <span class="comment">%create dimensions</span> |
---|
151 | 0075 dimid=[]; |
---|
152 | 0076 <span class="keyword">for</span> idim=1:length(Data.ListDimName) |
---|
153 | 0077 dimid(idim) = netcdf.defDim(nc,Data.ListDimName{idim},Data.DimValue(idim)); |
---|
154 | 0078 <span class="keyword">end</span> |
---|
155 | 0079 VarAttribute={}; <span class="comment">%default</span> |
---|
156 | 0080 testattr=0; |
---|
157 | 0081 <span class="keyword">if</span> isfield(Data,<span class="string">'VarAttribute'</span>) |
---|
158 | 0082 VarAttribute=Data.VarAttribute; |
---|
159 | 0083 testattr=1; |
---|
160 | 0084 <span class="keyword">end</span> |
---|
161 | 0085 varid=[]; |
---|
162 | 0086 <span class="keyword">for</span> ivar=1:length(ListVarName) |
---|
163 | 0087 varid(ivar)=netcdf.defVar(nc,ListVarName{ivar},<span class="string">'double'</span>,dimid(Data.VarDimIndex{ivar}));<span class="comment">%define variable</span> |
---|
164 | 0088 <span class="keyword">end</span> |
---|
165 | 0089 <span class="comment">%write variable attributes</span> |
---|
166 | 0090 <span class="keyword">if</span> testattr |
---|
167 | 0091 <span class="keyword">for</span> ivar=1:length(VarAttribute) |
---|
168 | 0092 <span class="keyword">if</span> isstruct(VarAttribute{ivar}) |
---|
169 | 0093 attr_names=fields(VarAttribute{ivar}); |
---|
170 | 0094 <span class="keyword">for</span> iattr=1:length(attr_names) |
---|
171 | 0095 eval([<span class="string">'attr_val=VarAttribute{ivar}.'</span> attr_names{iattr} <span class="string">';'</span>]); |
---|
172 | 0096 netcdf.putAtt(nc,varid(ivar),attr_names{iattr},attr_val); |
---|
173 | 0097 <span class="keyword">end</span> |
---|
174 | 0098 <span class="keyword">end</span> |
---|
175 | 0099 <span class="keyword">end</span> |
---|
176 | 0100 <span class="keyword">end</span> |
---|
177 | 0101 netcdf.endDef(nc); <span class="comment">%put in data mode</span> |
---|
178 | 0102 <span class="keyword">for</span> ivar=1:length(ListVarName) |
---|
179 | 0103 <span class="keyword">if</span> isfield(Data,ListVarName{ivar}) |
---|
180 | 0104 eval([<span class="string">'VarVal=Data.'</span> ListVarName{ivar} <span class="string">';'</span>])<span class="comment">%varval=values of the current variable</span> |
---|
181 | 0105 VarDimIndex=Data.VarDimIndex{ivar}; <span class="comment">%indices of the variable dimensions in the list of dimensions</span> |
---|
182 | 0106 siz=size(VarVal); |
---|
183 | 0107 VarDimName=Data.VarDimName{ivar};<span class="comment">%NEW</span> |
---|
184 | 0108 <span class="keyword">if</span> ischar(VarDimName)<span class="comment">%NEW</span> |
---|
185 | 0109 VarDimName={VarDimName};<span class="comment">%NEW</span> |
---|
186 | 0110 <span class="keyword">end</span><span class="comment">%NEW</span> |
---|
187 | 0111 testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2); <span class="comment">%NEW</span> |
---|
188 | 0112 testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), Data.DimValue(VarDimIndex)); |
---|
189 | 0113 testcolumn=isequal(length(siz),2) && isequal(siz(1), Data.DimValue(VarDimIndex))&& isequal(siz(2),1); |
---|
190 | 0114 <span class="keyword">if</span> ~testline && ~testcolumn && ~isequal(siz,Data.DimValue(VarDimIndex)) |
---|
191 | 0115 errormsg=[<span class="string">'wrong dimensions declared for '</span> ListVarName{ivar} <span class="string">' in struct2nc.m'</span>]; |
---|
192 | 0116 <span class="keyword">break</span> |
---|
193 | 0117 <span class="keyword">end</span> |
---|
194 | 0118 <span class="keyword">if</span> testline || testrange<span class="comment">%NEW</span> |
---|
195 | 0119 <span class="keyword">if</span> testrange |
---|
196 | 0120 VarVal=linspace(VarVal(1),VarVal(2),Data.DimValue(VarDimIndex));<span class="comment">%NEW</span> |
---|
197 | 0121 <span class="keyword">end</span> |
---|
198 | 0122 <span class="comment">%nc{ListVarName{ivar}}=ncfloat(Data.ListDimName(VarDimIndex));%vector of x coordinates</span> |
---|
199 | 0123 netcdf.putVar(nc,varid(ivar), VarVal'); |
---|
200 | 0124 <span class="keyword">else</span> |
---|
201 | 0125 netcdf.putVar(nc,varid(ivar), VarVal); |
---|
202 | 0126 <span class="comment">%nc{ListVarName{ivar}}(:) = VarVal;</span> |
---|
203 | 0127 <span class="keyword">end</span> |
---|
204 | 0128 |
---|
205 | 0129 <span class="keyword">end</span> |
---|
206 | 0130 <span class="keyword">end</span> |
---|
207 | 0131 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span> |
---|
208 | 0132 <span class="comment">%OLD netcdf toolbox</span> |
---|
209 | 0133 <span class="keyword">else</span> |
---|
210 | 0134 errormsg=<a href="struct2nc_toolbox.html" class="code" title="function errormsg=struct2nc_toolbox(flname,Data)">struct2nc_toolbox</a>(flname,Data); |
---|
211 | 0135 <span class="keyword">end</span> |
---|
212 | 0136</pre></div> |
---|
213 | <hr><address>Generated on Fri 13-Nov-2009 11:17:03 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> © 2003</address> |
---|
214 | </body> |
---|
215 | </html> |
---|