1 | function nc_addvar ( ncfile, varstruct ) |
---|
2 | % NC_ADDVAR: adds a variable to a NetCDF file |
---|
3 | % |
---|
4 | % USAGE: nc_addvar ( ncfile, varstruct ); |
---|
5 | % |
---|
6 | % PARAMETERS: |
---|
7 | % Input |
---|
8 | % ncfile: |
---|
9 | % varstruct: |
---|
10 | % This is a structure with four fields: |
---|
11 | % |
---|
12 | % Name |
---|
13 | % Nctype |
---|
14 | % Dimension |
---|
15 | % Attribute |
---|
16 | % |
---|
17 | % "Name" is just that, the name of the variable to be defined. |
---|
18 | % |
---|
19 | % "Nctype" should be |
---|
20 | % 'double', 'float', 'int', 'short', or 'byte', or 'char' |
---|
21 | % 'NC_DOUBLE', 'NC_FLOAT', 'NC_INT', 'NC_SHORT', 'NC_BYTE', 'NC_CHAR' |
---|
22 | % |
---|
23 | % "Dimension" is a cell array of dimension names. |
---|
24 | % |
---|
25 | % "Attribute" is also a structure array. Each element has two |
---|
26 | % fields, "Name", and "Value". |
---|
27 | % |
---|
28 | % Output: |
---|
29 | % None. In case of an error, an exception is thrown. |
---|
30 | % |
---|
31 | % AUTHOR: |
---|
32 | % john.g.evans.ne@gmail.com |
---|
33 | % |
---|
34 | |
---|
35 | |
---|
36 | error(nargchk(2,2,nargin,'struct')); |
---|
37 | |
---|
38 | if ~ischar(ncfile) |
---|
39 | error ( 'SNCTOOLS:NC_ADDVAR:badInput', 'file argument must be character' ); |
---|
40 | end |
---|
41 | |
---|
42 | if ( ~isstruct(varstruct) ) |
---|
43 | error ( 'SNCTOOLS:NC_ADDVAR:badInput', '2nd argument must be a structure' ); |
---|
44 | end |
---|
45 | |
---|
46 | |
---|
47 | varstruct = validate_varstruct ( varstruct ); |
---|
48 | |
---|
49 | |
---|
50 | switch ( version('-release') ) |
---|
51 | case { '11', '12', '13', '14', '2006a', '2006b', '2007a', '2007b', '2008a' } |
---|
52 | nc_addvar_mexnc(ncfile,varstruct); |
---|
53 | otherwise |
---|
54 | nc_addvar_tmw(ncfile,varstruct); |
---|
55 | end |
---|
56 | |
---|
57 | % Now just use nc_attput to put in the attributes |
---|
58 | for j = 1:length(varstruct.Attribute) |
---|
59 | attname = varstruct.Attribute(j).Name; |
---|
60 | attval = varstruct.Attribute(j).Value; |
---|
61 | nc_attput ( ncfile, varstruct.Name, attname, attval ); |
---|
62 | end |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | %-------------------------------------------------------------------------- |
---|
69 | function nc_addvar_tmw(ncfile,varstruct) |
---|
70 | |
---|
71 | ncid = netcdf.open(ncfile, nc_write_mode ); |
---|
72 | |
---|
73 | % |
---|
74 | % determine the dimids of the named dimensions |
---|
75 | num_dims = length(varstruct.Dimension); |
---|
76 | dimids = zeros(1,num_dims); |
---|
77 | for j = 1:num_dims |
---|
78 | dimids(1,j) = netcdf.inqDimID(ncid, varstruct.Dimension{j} ); |
---|
79 | end |
---|
80 | |
---|
81 | % If we are old school, we need to flip the dimensions. |
---|
82 | if ~getpref('SNCTOOLS','PRESERVE_FVD',false) |
---|
83 | dimids = fliplr(dimids); |
---|
84 | end |
---|
85 | |
---|
86 | % |
---|
87 | % go into define mode |
---|
88 | netcdf.reDef(ncid); |
---|
89 | |
---|
90 | netcdf.defVar(ncid, varstruct.Name, varstruct.Nctype, dimids ); |
---|
91 | |
---|
92 | netcdf.endDef(ncid ); |
---|
93 | netcdf.close(ncid ); |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | %-------------------------------------------------------------------------- |
---|
103 | function nc_addvar_mexnc(ncfile,varstruct) |
---|
104 | |
---|
105 | [ncid, status] = mexnc ( 'open', ncfile, nc_write_mode ); |
---|
106 | if ( status ~= 0 ) |
---|
107 | ncerr = mexnc ( 'strerror', status ); |
---|
108 | error ( 'SNCTOOLS:NC_ADDVAR:MEXNC:OPEN', ... |
---|
109 | 'OPEN failed on %s, ''%s''', ncfile, ncerr); |
---|
110 | end |
---|
111 | |
---|
112 | % |
---|
113 | % determine the dimids of the named dimensions |
---|
114 | num_dims = length(varstruct.Dimension); |
---|
115 | dimids = zeros(num_dims,1); |
---|
116 | for j = 1:num_dims |
---|
117 | [dimids(j), status] = mexnc ( 'dimid', ncid, varstruct.Dimension{j} ); |
---|
118 | if ( status ~= 0 ) |
---|
119 | mexnc ( 'close', ncid ); |
---|
120 | ncerr = mexnc ( 'strerror', status ); |
---|
121 | error ( 'SNCTOOLS:NC_ADDVAR:MEXNC:DIMID', ncerr ); |
---|
122 | end |
---|
123 | end |
---|
124 | |
---|
125 | % If preserving the fastest varying dimension in mexnc, we have to |
---|
126 | % reverse their order. |
---|
127 | if getpref('SNCTOOLS','PRESERVE_FVD',false) |
---|
128 | dimids = flipud(dimids); |
---|
129 | end |
---|
130 | |
---|
131 | |
---|
132 | status = mexnc ( 'redef', ncid ); |
---|
133 | if ( status ~= 0 ) |
---|
134 | ncerr = mexnc ( 'strerror', status ); |
---|
135 | mexnc ( 'close', ncid ); |
---|
136 | error ( 'SNCTOOLS:NC_ADDVAR:MEXNC:REDEF', ncerr ); |
---|
137 | end |
---|
138 | |
---|
139 | [varid, status] = mexnc ( 'DEF_VAR', ncid, varstruct.Name, varstruct.Nctype, num_dims, dimids ); |
---|
140 | if ( status ~= 0 ) |
---|
141 | ncerr = mexnc ( 'strerror', status ); |
---|
142 | mexnc ( 'endef', ncid ); |
---|
143 | mexnc ( 'close', ncid ); |
---|
144 | error ( 'SNCTOOLS:NC_ADDVAR:MEXNC:DEF_VAR', ncerr ); |
---|
145 | end |
---|
146 | |
---|
147 | status = mexnc ( 'enddef', ncid ); |
---|
148 | if ( status ~= 0 ) |
---|
149 | ncerr = mexnc ( 'strerror', status ); |
---|
150 | mexnc ( 'close', ncid ); |
---|
151 | error ( 'SNCTOOLS:NC_ADDVAR:MEXNC:ENDDEF', ncerr ); |
---|
152 | end |
---|
153 | |
---|
154 | status = mexnc ( 'close', ncid ); |
---|
155 | if ( status ~= 0 ) |
---|
156 | ncerr = mexnc ( 'strerror', status ); |
---|
157 | error ( 'SNCTOOLS:NC_ADDVAR:MEXNC:CLOSE', ncerr ); |
---|
158 | end |
---|
159 | |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | return |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
170 | function varstruct = validate_varstruct ( varstruct ) |
---|
171 | |
---|
172 | % |
---|
173 | % Check that required fields are there. |
---|
174 | % Must at least have a name. |
---|
175 | if ~isfield ( varstruct, 'Name' ) |
---|
176 | error ( 'SNCTOOLS:NC_ADDVAR:badInput', 'structure argument must have at least the ''Name'' field.' ); |
---|
177 | end |
---|
178 | |
---|
179 | % |
---|
180 | % Check that required fields are there. |
---|
181 | % Default Nctype is double. |
---|
182 | if ~isfield ( varstruct, 'Nctype' ) |
---|
183 | varstruct.Nctype = 'double'; |
---|
184 | end |
---|
185 | |
---|
186 | % |
---|
187 | % Are there any unrecognized fields? |
---|
188 | fnames = fieldnames ( varstruct ); |
---|
189 | for j = 1:length(fnames) |
---|
190 | fname = fnames{j}; |
---|
191 | switch ( fname ) |
---|
192 | |
---|
193 | case { 'Nctype', 'Name', 'Dimension', 'Attribute' } |
---|
194 | % |
---|
195 | % These are used to create the variable. They are ok. |
---|
196 | |
---|
197 | case { 'Unlimited', 'Size', 'Rank' } |
---|
198 | % |
---|
199 | % These come from the output of nc_getvarinfo. We don't |
---|
200 | % use them, but let's not give the user a warning about |
---|
201 | % them either. |
---|
202 | |
---|
203 | otherwise |
---|
204 | fprintf ( 2, '%s: unrecognized field name ''%s''. Ignoring it...\n', mfilename, fname ); |
---|
205 | end |
---|
206 | end |
---|
207 | |
---|
208 | % If the datatype is not a string. |
---|
209 | % Change suggested by Brian Powell |
---|
210 | if ( isa(varstruct.Nctype, 'double') && varstruct.Nctype < 7 ) |
---|
211 | types={ 'byte' 'char' 'short' 'int' 'float' 'double'}; |
---|
212 | varstruct.Nctype = char(types(varstruct.Nctype)); |
---|
213 | end |
---|
214 | |
---|
215 | |
---|
216 | % |
---|
217 | % Check that the datatype is known. |
---|
218 | switch ( varstruct.Nctype ) |
---|
219 | case { 'NC_DOUBLE', 'double', ... |
---|
220 | 'NC_FLOAT', 'float', ... |
---|
221 | 'NC_INT', 'int', ... |
---|
222 | 'NC_SHORT', 'short', ... |
---|
223 | 'NC_BYTE', 'byte', ... |
---|
224 | 'NC_CHAR', 'char' } |
---|
225 | % |
---|
226 | % Do nothing |
---|
227 | otherwise |
---|
228 | error ( 'SNCTOOLS:NC_ADDVAR:unknownDatatype', 'unknown type ''%s''\n', mfilename, varstruct.Nctype ); |
---|
229 | end |
---|
230 | |
---|
231 | |
---|
232 | % |
---|
233 | % Check that required fields are there. |
---|
234 | % Default Dimension is none. Singleton scalar. |
---|
235 | if ~isfield ( varstruct, 'Dimension' ) |
---|
236 | varstruct.Dimension = []; |
---|
237 | end |
---|
238 | |
---|
239 | % |
---|
240 | % Check that required fields are there. |
---|
241 | % Default Attributes are none |
---|
242 | if ~isfield ( varstruct, 'Attribute' ) |
---|
243 | varstruct.Attribute = []; |
---|
244 | end |
---|
245 | |
---|