11984
|
1 // Copyright (C) 2013 Alexander Barth <barth.alexander@gmail.com> |
|
2 // |
|
3 // This program is free software; you can redistribute it and/or modify it under |
|
4 // the terms of the GNU General Public License as published by the Free Software |
|
5 // Foundation; either version 2 of the License, or (at your option) any later |
|
6 // version. |
|
7 // |
|
8 // This program is distributed in the hope that it will be useful, but WITHOUT |
|
9 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
11 // details. |
|
12 // |
|
13 // You should have received a copy of the GNU General Public License along with |
|
14 // this program; if not, see <http://www.gnu.org/licenses/>. |
|
15 |
|
16 |
|
17 #include <octave/oct.h> |
|
18 #include <octave/ov-cell.h> |
|
19 |
|
20 #include <netcdf.h> |
|
21 |
|
22 #include <string> |
|
23 #include <map> |
|
24 #include <iostream> |
|
25 #include <algorithm> |
|
26 #include <vector> |
|
27 |
|
28 std::map<std::string, octave_value> netcdf_constants; |
|
29 |
|
30 void init() { |
|
31 #include "netcdf_constants.h" |
|
32 } |
|
33 |
|
34 void check_err(int status) |
|
35 { |
|
36 if (status != NC_NOERR) error("%s",nc_strerror(status)); |
|
37 } |
|
38 |
|
39 // convert name to upper-case and add "NC_" prefix if it is missing |
|
40 std::string normalize_ncname(std::string name) { |
|
41 std::string prefix = "NC_"; |
|
42 std::string ncname = name; |
|
43 // to upper case |
|
44 std::transform(ncname.begin(), ncname.end(),ncname.begin(), ::toupper); |
|
45 |
|
46 // add prefix if it is missing |
|
47 if (ncname.substr(0, prefix.size()) != prefix) { |
|
48 ncname = prefix + ncname; |
|
49 } |
|
50 return ncname; |
|
51 } |
|
52 |
|
53 octave_value netcdf_get_constant(octave_value ov) |
|
54 { |
|
55 if (netcdf_constants.empty()) |
|
56 { |
|
57 init(); |
|
58 } |
|
59 |
|
60 if (ov.is_scalar_type()) |
|
61 { |
|
62 return ov.scalar_value(); |
|
63 } |
|
64 |
|
65 std::string name = ov.string_value(); |
|
66 name = normalize_ncname(name); |
|
67 std::map<std::string, octave_value>::const_iterator cst = netcdf_constants.find(name); |
|
68 |
|
69 if (cst != netcdf_constants.end ()) |
|
70 { |
|
71 return cst->second; |
|
72 } |
|
73 else |
|
74 { |
|
75 error("unknown netcdf constant: %s",name.c_str()); |
|
76 } |
|
77 } |
|
78 |
|
79 |
|
80 void start_count_stride(int ncid, int varid, octave_value_list args,int len,size_t* start,size_t* count,ptrdiff_t* stride) |
|
81 { |
|
82 int ndims, dimids[NC_MAX_VAR_DIMS]; |
|
83 |
|
84 check_err(nc_inq_varndims (ncid, varid, &ndims)); |
|
85 check_err(nc_inq_vardimid (ncid, varid, dimids)); |
|
86 |
|
87 // default values for start, count and stride |
|
88 // i.e. all variable is loaded |
|
89 |
|
90 for (int i=0; i<ndims; i++) { |
|
91 check_err(nc_inq_dimlen(ncid,dimids[i],&(count[i]))); |
|
92 start[i] = 0; |
|
93 //cout << "count def " << count[i] << " " << i << endl; |
|
94 stride[i] = 1; |
|
95 } |
|
96 |
|
97 // start argument |
|
98 |
|
99 if (len > 2) |
|
100 { |
|
101 uint64NDArray tmp = args(2).uint64_array_value(); |
|
102 |
|
103 if (tmp.dims().numel() != ndims) |
|
104 { |
|
105 error("number of elements of argument %s should match the number " |
|
106 "of dimension of the netCDF variable", |
|
107 "start"); |
|
108 } |
|
109 |
|
110 for (int i=0; i<ndims; i++) |
|
111 { |
|
112 start[i] = (size_t)tmp(ndims-i-1); |
|
113 |
|
114 // if start is specified, the default for count is 1 (how odd!) |
|
115 count[i] = 1; |
|
116 //cout << "start " << start[i] << " " << i << endl; |
|
117 } |
|
118 } |
|
119 |
|
120 // count argument |
|
121 |
|
122 if (len > 3) |
|
123 { |
|
124 uint64NDArray tmp = args(3).uint64_array_value(); |
|
125 |
|
126 if (tmp.dims().numel() != ndims) |
|
127 { |
|
128 error("number of elements of argument %s should match the number " |
|
129 "of dimension of the netCDF variable", |
|
130 "count"); |
|
131 } |
|
132 |
|
133 for (int i=0; i<ndims; i++) |
|
134 { |
|
135 count[i] = (size_t)tmp(ndims-i-1); |
|
136 //count[i] = (size_t)tmp(i); |
|
137 //cout << "count " << count[i] << " " << i << endl; |
|
138 } |
|
139 } |
|
140 |
|
141 // stride argument |
|
142 |
|
143 if (len > 4) |
|
144 { |
|
145 int64NDArray tmp = args(4).int64_array_value(); |
|
146 |
|
147 if (tmp.dims().numel() != ndims) |
|
148 { |
|
149 error("number of elements of argument %s should match the number " |
|
150 "of dimension of the netCDF variable", |
|
151 "stride"); |
|
152 } |
|
153 |
|
154 for (int i=0; i<ndims; i++) |
|
155 { |
|
156 stride[i] = (ptrdiff_t)tmp(ndims-i-1); |
|
157 } |
|
158 } |
|
159 |
|
160 } |
|
161 |
|
162 |
|
163 DEFUN_DLD(netcdf_getConstant, args,, |
|
164 "-*- texinfo -*-\n\ |
|
165 @deftypefn {Loadable Function} {@var{value} =} netcdf_getConstant(@var{name}) \n\ |
|
166 Returns the value of a NetCDF constant called @var{name}.\n\ |
|
167 @seealso{netcdf_getConstantNames}\n\ |
|
168 @end deftypefn") |
|
169 { |
|
170 if (args.length() != 1) { |
|
171 print_usage (); |
|
172 return octave_value(); |
|
173 } |
|
174 |
|
175 return netcdf_get_constant(args(0)); |
|
176 } |
|
177 |
|
178 |
|
179 DEFUN_DLD(netcdf_getConstantNames, args,, |
|
180 "-*- texinfo -*-\n\ |
|
181 @deftypefn {Loadable Function} {@var{value} =} netcdf_getConstantNames() \n\ |
|
182 Returns a list of all constant names.\n\ |
|
183 @end deftypefn\n\ |
|
184 @seealso{netcdf_getConstant}\n") |
|
185 { |
|
186 |
|
187 if (args.length() != 0) { |
|
188 print_usage (); |
|
189 return octave_value(); |
|
190 } |
|
191 |
11989
|
192 if (netcdf_constants.empty()) |
|
193 { |
|
194 init(); |
|
195 } |
|
196 |
11984
|
197 Cell c = Cell (dim_vector(1,netcdf_constants.size())); |
|
198 |
|
199 int i = 0; |
|
200 for (std::map<std::string, octave_value>::const_iterator p = netcdf_constants.begin (); |
|
201 p != netcdf_constants.end (); p++) { |
|
202 c(i++) = octave_value(p->first); |
|
203 } |
|
204 |
|
205 return octave_value(c); |
|
206 |
|
207 } |
|
208 |
|
209 |
|
210 DEFUN_DLD(netcdf_inqLibVers, args,, |
|
211 "-*- texinfo -*-\n\ |
|
212 @deftypefn {Loadable Function} {@var{vers} =} netcdf_inqLibVers() \n\ |
|
213 Returns the version of the NetCDF library.\n\ |
|
214 @end deftypefn\n\ |
|
215 @seealso{netcdf_open}\n") |
|
216 { |
|
217 if (args.length() != 0) { |
|
218 print_usage (); |
|
219 return octave_value (); |
|
220 } |
|
221 |
|
222 return octave_value(std::string(nc_inq_libvers())); |
|
223 } |
|
224 |
|
225 DEFUN_DLD(netcdf_setDefaultFormat, args,, |
|
226 "-*- texinfo -*-\n\ |
|
227 @deftypefn {Loadable Function} {@var{old_format} =} netcdf_setDefaultFormat(@var{format}) \n\ |
|
228 Sets the default format of the NetCDF library and returns the previous default format (as a numeric value). @var{format} can be \n\ |
|
229 \"format_classic\", \"format_64bit\", \"format_netcdf4\" or \"format_netcdf4_classic\". \n\ |
|
230 @end deftypefn\n\ |
|
231 @seealso{netcdf_open}\n") |
|
232 { |
|
233 if (args.length() != 1) |
|
234 { |
|
235 print_usage (); |
|
236 return octave_value (); |
|
237 } |
|
238 |
|
239 int format = netcdf_get_constant(args(0)).int_value(); |
|
240 int old_format; |
|
241 |
|
242 check_err(nc_set_default_format(format, &old_format)); |
|
243 |
|
244 return octave_value(old_format); |
|
245 } |
|
246 |
|
247 |
|
248 // int nc_set_chunk_cache(size_t size, size_t nelems, float preemption); |
|
249 |
|
250 DEFUN_DLD(netcdf_setChunkCache, args,, |
|
251 "-*- texinfo -*-\n\ |
|
252 @deftypefn {Loadable Function} {} netcdf_setChunkCache(@var{size}, @var{nelems}, @var{preemption}) \n\ |
|
253 Sets the default chunk cache settins in the HDF5 library. The settings applies to all files which are subsequently opened or created.\n\ |
|
254 @end deftypefn\n\ |
|
255 @seealso{netcdf_getChunkCache}\n") |
|
256 { |
|
257 if (args.length() != 3) |
|
258 { |
|
259 print_usage (); |
|
260 return octave_value(); |
|
261 } |
|
262 |
|
263 size_t size = args(0).scalar_value(); |
|
264 size_t nelems = args(1).scalar_value(); |
|
265 float preemption = args(2).scalar_value(); |
|
266 |
|
267 if (error_state) |
|
268 { |
|
269 print_usage (); |
|
270 return octave_value(); |
|
271 } |
|
272 |
|
273 check_err(nc_set_chunk_cache(size, nelems, preemption)); |
|
274 |
|
275 return octave_value(); |
|
276 } |
|
277 |
|
278 |
|
279 // int nc_get_chunk_cache(size_t *sizep, size_t *nelemsp, float *preemptionp); |
|
280 |
|
281 DEFUN_DLD(netcdf_getChunkCache, args,, |
|
282 "-*- texinfo -*-\n\ |
|
283 @deftypefn {Loadable Function} {[@var{size}, @var{nelems}, @var{preemption}] =} netcdf_getChunkCache() \n\ |
|
284 Gets the default chunk cache settins in the HDF5 library. \n\ |
|
285 @end deftypefn\n\ |
|
286 @seealso{netcdf_setChunkCache}\n") |
|
287 { |
|
288 if (args.length() != 0) |
|
289 { |
|
290 print_usage (); |
|
291 return octave_value (); |
|
292 } |
|
293 |
|
294 size_t size; |
|
295 size_t nelems; |
|
296 float preemption; |
|
297 |
|
298 if (error_state) |
|
299 { |
|
300 print_usage (); |
|
301 return octave_value(); |
|
302 } |
|
303 |
|
304 check_err(nc_get_chunk_cache(&size, &nelems, &preemption)); |
|
305 octave_value_list retval; |
|
306 retval(0) = octave_value(size); |
|
307 retval(1) = octave_value(nelems); |
|
308 retval(2) = octave_value(preemption); |
|
309 |
|
310 return retval; |
|
311 } |
|
312 |
|
313 |
|
314 |
|
315 DEFUN_DLD(netcdf_create, args,, |
|
316 "-*- texinfo -*-\n\ |
|
317 @deftypefn {Loadable Function} {@var{ncid} =} netcdf_create(@var{filename},@var{mode}) \n\ |
|
318 Creates the file named @var{filename} in the mode @var{mode} which can have the \n\ |
|
319 following values: \n\ |
|
320 \"clobber\" (overwrite existing files), \n\ |
|
321 \"noclobber\" (prevent to overwrite existing files) \n\ |
|
322 \"64bit_offset\" (use the 64bit-offset format), \n\ |
|
323 \"netcdf4\" (use the NetCDF4, i.e. HDF5 format) or \n\ |
|
324 \"share\" (concurrent reading of the dataset). \n\ |
|
325 @var{mode} can also be the numeric value return by netcdf_getConstant. In the later-case it can be combined with a bitwise-or. \n\ |
|
326 @end deftypefn\n\ |
|
327 Example: \n\ |
|
328 @example \n\ |
|
329 mode = bitor(netcdf.getConstant(\"classic_model\"), ...\n\ |
|
330 netcdf.getConstant(\"netcdf4\")); \n\ |
|
331 ncid = netcdf.create(\"test.nc\",mode); \n\ |
|
332 @end example \n\ |
|
333 @seealso{netcdf_close}\n") |
|
334 { |
|
335 |
|
336 if (args.length() != 2) |
|
337 { |
|
338 print_usage (); |
|
339 return octave_value (); |
|
340 } |
|
341 |
|
342 std::string filename = args(0).string_value(); |
|
343 int mode = netcdf_get_constant(args(1)).int_value(); |
|
344 int ncid; |
|
345 |
|
346 check_err(nc_create(filename.c_str(), mode, &ncid)); |
|
347 |
|
348 return octave_value(ncid); |
|
349 } |
|
350 |
|
351 DEFUN_DLD(netcdf_open, args,, |
|
352 "-*- texinfo -*-\n\ |
|
353 @deftypefn {Loadable Function} {@var{ncid} =} netcdf_open(@var{filename},@var{mode}) \n\ |
|
354 Opens the file named @var{filename} in the mode @var{mode}.\n\ |
|
355 @end deftypefn\n\ |
|
356 @seealso{netcdf_close}\n") |
|
357 { |
|
358 |
|
359 if (args.length() != 2) { |
|
360 print_usage (); |
|
361 return octave_value(); |
|
362 } |
|
363 |
|
364 std::string filename = args(0).string_value(); |
|
365 int mode = netcdf_get_constant(args(1)).int_value(); |
|
366 int ncid; |
|
367 |
|
368 check_err(nc_open(filename.c_str(), mode, &ncid)); |
|
369 |
|
370 return octave_value(ncid); |
|
371 } |
|
372 |
|
373 |
|
374 |
|
375 DEFUN_DLD(netcdf_abort, args,, |
|
376 "-*- texinfo -*-\n\ |
|
377 @deftypefn {Loadable Function} {} netcdf_abort(@var{ncid}) \n\ |
|
378 Aborts all changes since the last time the dataset entered in define mode.\n\ |
|
379 @end deftypefn\n\ |
|
380 @seealso{netcdf_reDef}\n") |
|
381 { |
|
382 |
|
383 if (args.length() != 1) |
|
384 { |
|
385 print_usage (); |
|
386 return octave_value(); |
|
387 } |
|
388 |
|
389 int ncid = args(0).scalar_value(); |
|
390 |
|
391 if (error_state) |
|
392 { |
|
393 print_usage (); |
|
394 return octave_value(); |
|
395 } |
|
396 |
|
397 check_err(nc_abort(ncid)); |
|
398 |
|
399 return octave_value(); |
|
400 } |
|
401 |
|
402 |
|
403 DEFUN_DLD(netcdf_sync, args,, |
|
404 "-*- texinfo -*-\n\ |
|
405 @deftypefn {Loadable Function} {} netcdf_sync(@var{ncid}) \n\ |
|
406 Writes all changes to the disk and leaves the file open.\n\ |
|
407 @end deftypefn\n\ |
|
408 @seealso{netcdf_close}\n") |
|
409 { |
|
410 |
|
411 if (args.length() != 1) |
|
412 { |
|
413 print_usage (); |
|
414 return octave_value(); |
|
415 } |
|
416 |
|
417 int ncid = args(0).scalar_value(); |
|
418 |
|
419 if (error_state) |
|
420 { |
|
421 print_usage (); |
|
422 return octave_value(); |
|
423 } |
|
424 |
|
425 check_err(nc_sync(ncid)); |
|
426 |
|
427 return octave_value(); |
|
428 } |
|
429 |
|
430 DEFUN_DLD(netcdf_setFill, args,, |
|
431 "-*- texinfo -*-\n\ |
|
432 @deftypefn {Loadable Function} {@var{old_mode} =} netcdf_setFill(@var{ncid},@var{fillmode}) \n\ |
|
433 Change the fill mode (@var{fillmode}) of the data set @var{ncid}. The previous value of the fill mode is returned. @var{fillmode} can be either \"fill\" or \"nofill\".\n\ |
|
434 @end deftypefn\n\ |
|
435 @seealso{netcdf_open}\n") |
|
436 { |
|
437 |
|
438 if (args.length() != 2) |
|
439 { |
|
440 print_usage (); |
|
441 return octave_value(); |
|
442 } |
|
443 |
|
444 int ncid = args(0).scalar_value(); |
|
445 int fillmode = netcdf_get_constant(args(1)).int_value(); |
|
446 int old_mode; |
|
447 |
|
448 if (error_state) |
|
449 { |
|
450 print_usage (); |
|
451 return octave_value(); |
|
452 } |
|
453 |
|
454 check_err (nc_set_fill (ncid, fillmode, &old_mode)); |
|
455 |
|
456 return octave_value(old_mode); |
|
457 } |
|
458 |
|
459 |
|
460 //int nc_inq (int ncid, int *ndimsp, int *nvarsp, int *ngattsp, |
|
461 // int *unlimdimidp); |
|
462 DEFUN_DLD(netcdf_inq, args,, |
|
463 "-*- texinfo -*-\n\ |
|
464 @deftypefn {Loadable Function} {[@var{ndims},@var{nvars},@var{ngatts},@var{unlimdimid}] =} netcdf_inq(@var{ncid}) \n\ |
|
465 Return the number of dimension (@var{ndims}), the number of variables (@var{nvars}), the number of global attributes (@var{ngatts}) and the id of the unlimited dimension (@var{unlimdimid}). \n\ |
|
466 If no unlimited dimension is declared -1 is returned. For NetCDF4 files, one should use \n\ |
|
467 the function netcdf_inqUnlimDims as multiple unlimite dimension exists. \n\ |
|
468 @end deftypefn\n\ |
|
469 @seealso{netcdf_inqUnlimDims}\n") |
|
470 { |
|
471 if (args.length() != 1) { |
|
472 print_usage (); |
|
473 return octave_value(); |
|
474 } |
|
475 |
|
476 int ncid = args(0).scalar_value(); |
|
477 int ndims, nvars, ngatts, unlimdimid; |
|
478 octave_value_list retval; |
|
479 |
|
480 check_err(nc_inq(ncid,&ndims,&nvars,&ngatts,&unlimdimid)); |
|
481 |
|
482 retval(0) = octave_value(ndims); |
|
483 retval(1) = octave_value(nvars); |
|
484 retval(2) = octave_value(ngatts); |
|
485 retval(3) = octave_value(unlimdimid); |
|
486 return retval; |
|
487 } |
|
488 |
|
489 // int nc_inq_unlimdims(int ncid, int *nunlimdimsp, int *unlimdimidsp); |
|
490 DEFUN_DLD(netcdf_inqUnlimDims, args,, |
|
491 "-*- texinfo -*-\n\ |
|
492 @deftypefn {Loadable Function} {@var{unlimdimids} =} netcdf_inqUnlimDims(@var{ncid}) \n\ |
|
493 Return the id of all unlimited dimensions of the NetCDF file @var{ncid}.\n\ |
|
494 @end deftypefn\n\ |
|
495 @seealso{netcdf_inq}\n") |
|
496 { |
|
497 if (args.length() != 1) { |
|
498 print_usage (); |
|
499 return octave_value(); |
|
500 } |
|
501 |
|
502 int ncid = args(0).scalar_value(); |
|
503 int nunlimdims; |
|
504 |
|
505 check_err(nc_inq_unlimdims(ncid, &nunlimdims, NULL)); |
|
506 Array<int> unlimdimids = Array<int>(dim_vector(1,nunlimdims)); |
|
507 check_err(nc_inq_unlimdims(ncid, &nunlimdims, unlimdimids.fortran_vec())); |
|
508 |
|
509 return octave_value(unlimdimids); |
|
510 } |
|
511 |
|
512 |
|
513 // int nc_inq_format (int ncid, int *formatp); |
|
514 DEFUN_DLD(netcdf_inqFormat, args,, |
|
515 "-*- texinfo -*-\n\ |
|
516 @deftypefn {Loadable Function} {@var{format} =} netcdf_inqFormat(@var{ncid}) \n\ |
|
517 Return the NetCDF format of the dataset @var{ncid}.\n\ |
|
518 Format might be one of the following \n\ |
|
519 \"FORMAT_CLASSIC\", \"FORMAT_64BIT\", \"FORMAT_NETCDF4\" or \"FORMAT_NETCDF4_CLASSIC\" \n\ |
|
520 @end deftypefn\n\ |
|
521 @seealso{netcdf_inq}\n") |
|
522 { |
|
523 |
|
524 if (args.length() != 1) { |
|
525 print_usage (); |
|
526 return octave_value(); |
|
527 } |
|
528 |
|
529 int ncid = args(0).scalar_value(); |
|
530 int format; |
|
531 check_err(nc_inq_format(ncid, &format)); |
|
532 |
|
533 if (format == NC_FORMAT_CLASSIC) { |
|
534 return octave_value("FORMAT_CLASSIC"); |
|
535 } |
|
536 if (format == NC_FORMAT_64BIT) { |
|
537 return octave_value("FORMAT_64BIT"); |
|
538 } |
|
539 if (format == NC_FORMAT_NETCDF4) { |
|
540 return octave_value("FORMAT_NETCDF4"); |
|
541 } |
|
542 |
|
543 return octave_value("FORMAT_NETCDF4_CLASSIC"); |
|
544 } |
|
545 |
|
546 // int nc_def_dim (int ncid, const char *name, size_t len, int *dimidp); |
|
547 |
|
548 DEFUN_DLD(netcdf_defDim, args,, |
|
549 "-*- texinfo -*-\n\ |
|
550 @deftypefn {Loadable Function} {@var{dimid} =} netcdf_defDim(@var{ncid},@var{name},@var{len}) \n\ |
|
551 Define the dimension with the name @var{name} and the length @var{len} in the dataset @var{ncid}. The id of the dimension is returned.\n\ |
|
552 @end deftypefn\n\ |
|
553 @seealso{netcdf_defVar}\n") |
|
554 { |
|
555 |
|
556 if (args.length() != 3) { |
|
557 print_usage (); |
|
558 return octave_value(); |
|
559 } |
|
560 |
|
561 int ncid = args(0).scalar_value(); |
|
562 std::string name = args(1).string_value(); |
|
563 size_t len = args(2).scalar_value(); |
|
564 int dimid; |
|
565 |
|
566 check_err(nc_def_dim (ncid, name.c_str(), len, &dimid)); |
|
567 |
|
568 return octave_value(dimid); |
|
569 } |
|
570 |
|
571 |
|
572 // int nc_rename_dim(int ncid, int dimid, const char* name); |
|
573 |
|
574 |
|
575 DEFUN_DLD(netcdf_renameDim, args,, |
|
576 "-*- texinfo -*-\n\ |
|
577 @deftypefn {Loadable Function} {} netcdf_renameDim(@var{ncid},@var{dimid},@var{name}) \n\ |
|
578 Renames the dimension with the id @var{dimid} in the data set @var{ncid}. @var{name} is the new name of the dimension.\n\ |
|
579 @end deftypefn\n\ |
|
580 @seealso{netcdf_defDim}\n") |
|
581 { |
|
582 |
|
583 if (args.length() != 3) |
|
584 { |
|
585 print_usage (); |
|
586 return octave_value(); |
|
587 } |
|
588 |
|
589 int ncid = args(0).scalar_value(); |
|
590 int dimid = args(1).scalar_value(); |
|
591 std::string name = args(2).string_value(); |
|
592 |
|
593 if (error_state) |
|
594 { |
|
595 print_usage (); |
|
596 return octave_value(); |
|
597 } |
|
598 |
|
599 check_err(nc_rename_dim (ncid, dimid, name.c_str())); |
|
600 |
|
601 return octave_value(); |
|
602 } |
|
603 |
|
604 // int nc_def_var (int ncid, const char *name, nc_type xtype, |
|
605 // int ndims, const int dimids[], int *varidp); |
|
606 |
|
607 DEFUN_DLD(netcdf_defVar, args,, |
|
608 "-*- texinfo -*-\n\ |
|
609 @deftypefn {Loadable Function} {@var{varid} = } netcdf_defVar(@var{ncid},@var{name},@var{xtype},@var{dimids}) \n\ |
|
610 Defines a variable with the name @var{name} in the dataset @var{ncid}. @var{xtype} can be \"byte\", \"ubyte\", \"short\", \"ushort\", \"int\", \"uint\", \"int64\", \"uint64\", \"float\", \"double\", \"char\" or the corresponding number as returned by netcdf_getConstant. The parameter @var{dimids} define the ids of the dimension. For scalar this parameter is the empty array ([]). The variable id is returned. \n\ |
|
611 @end deftypefn\n\ |
|
612 @seealso{netcdf_open,netcdf_defDim}\n") |
|
613 { |
|
614 |
|
615 if (args.length() != 4) { |
|
616 print_usage (); |
|
617 return octave_value(); |
|
618 } |
|
619 |
|
620 int ncid = args(0).scalar_value(); |
|
621 std::string name = args(1).string_value (); |
|
622 int xtype = netcdf_get_constant(args(2)).int_value();; |
|
623 Array<double> tmp; |
|
624 |
|
625 if (!args(3).is_empty()) { |
|
626 tmp = args(3).vector_value (); |
|
627 } |
|
628 |
|
629 OCTAVE_LOCAL_BUFFER (int, dimids, tmp.numel()); |
|
630 |
|
631 for (int i = 0; i < tmp.numel(); i++) |
|
632 { |
|
633 dimids[i] = tmp(tmp.numel()-i-1); |
|
634 } |
|
635 |
|
636 int varid; |
|
637 |
|
638 check_err(nc_def_var (ncid, name.c_str(), xtype, tmp.numel(), dimids, &varid)); |
|
639 |
|
640 return octave_value(varid); |
|
641 } |
|
642 |
|
643 |
|
644 // int nc_rename_var(int ncid, int varid, const char* name); |
|
645 |
|
646 |
|
647 DEFUN_DLD(netcdf_renameVar, args,, |
|
648 "-*- texinfo -*-\n\ |
|
649 @deftypefn {Loadable Function} {} netcdf_renameVar(@var{ncid},@var{varid},@var{name}) \n\ |
|
650 Renames the variable with the id @var{varid} in the data set @var{ncid}. @var{name} is the new name of the variable.\n\ |
|
651 @end deftypefn\n\ |
|
652 @seealso{netcdf_defVar}\n") |
|
653 { |
|
654 |
|
655 if (args.length() != 3) |
|
656 { |
|
657 print_usage (); |
|
658 return octave_value(); |
|
659 } |
|
660 |
|
661 int ncid = args(0).scalar_value(); |
|
662 int varid = args(1).scalar_value(); |
|
663 std::string name = args(2).string_value(); |
|
664 |
|
665 if (error_state) |
|
666 { |
|
667 print_usage (); |
|
668 return octave_value(); |
|
669 } |
|
670 |
|
671 check_err(nc_rename_var (ncid, varid, name.c_str())); |
|
672 |
|
673 return octave_value(); |
|
674 } |
|
675 |
|
676 |
|
677 // int nc_def_var_fill(int ncid, int varid, int no_fill, void *fill_value); |
|
678 DEFUN_DLD(netcdf_defVarFill, args,, |
|
679 "-*- texinfo -*-\n\ |
|
680 @deftypefn {Loadable Function} {} netcdf_defVarFill(@var{ncid},@var{varid},@var{no_fill},@var{fillvalue}) \n\ |
|
681 Define the fill-value settings of the NetCDF variable @var{varid}.\n\ |
|
682 If @var{no_fill} is false, then the values between no-contiguous writes are filled with the value @var{fill_value}. This is disabled by setting @var{no_fill} to true.\n\ |
|
683 @end deftypefn\n\ |
|
684 @seealso{netcdf_inqVarFill}\n") |
|
685 { |
|
686 |
|
687 if (args.length() != 4) { |
|
688 print_usage (); |
|
689 return octave_value(); |
|
690 } |
|
691 |
|
692 int ncid = args(0).scalar_value(); |
|
693 int varid = args(1).scalar_value(); |
|
694 int no_fill = args(2).scalar_value(); // boolean |
|
695 octave_value fill_value = args(3); |
|
696 nc_type xtype; |
|
697 check_err(nc_inq_vartype (ncid, varid, &xtype)); |
|
698 |
|
699 switch (xtype) |
|
700 { |
|
701 #define OV_NETCDF_DEF_VAR_FILL(netcdf_type,c_type,method) \ |
|
702 case netcdf_type: \ |
|
703 { \ |
|
704 check_err(nc_def_var_fill(ncid, varid, no_fill, fill_value.method().fortran_vec())); \ |
|
705 break; \ |
|
706 } |
|
707 |
|
708 OV_NETCDF_DEF_VAR_FILL(NC_BYTE, signed char, int8_array_value) |
|
709 OV_NETCDF_DEF_VAR_FILL(NC_UBYTE, unsigned char, uint8_array_value) |
|
710 OV_NETCDF_DEF_VAR_FILL(NC_SHORT, short, int16_array_value) |
|
711 OV_NETCDF_DEF_VAR_FILL(NC_USHORT, unsigned short, uint16_array_value) |
|
712 OV_NETCDF_DEF_VAR_FILL(NC_INT, int, int32_array_value) |
|
713 OV_NETCDF_DEF_VAR_FILL(NC_UINT, unsigned int, uint32_array_value) |
|
714 OV_NETCDF_DEF_VAR_FILL(NC_INT64, long long, int64_array_value) |
|
715 OV_NETCDF_DEF_VAR_FILL(NC_UINT64, unsigned long long, uint64_array_value) |
|
716 |
|
717 OV_NETCDF_DEF_VAR_FILL(NC_FLOAT, float, float_array_value) |
|
718 OV_NETCDF_DEF_VAR_FILL(NC_DOUBLE,double,array_value) |
|
719 |
|
720 OV_NETCDF_DEF_VAR_FILL(NC_CHAR, char, char_array_value) |
|
721 } |
|
722 |
|
723 return octave_value(); |
|
724 } |
|
725 |
|
726 |
|
727 |
|
728 // int nc_def_var_fill(int ncid, int varid, int no_fill, void *fill_value); |
|
729 DEFUN_DLD(netcdf_inqVarFill, args,, |
|
730 "-*- texinfo -*-\n\ |
|
731 @deftypefn {Loadable Function} {[@var{no_fill},@var{fillvalue}] = } netcdf_inqVarFill(@var{ncid},@var{varid}) \n\ |
|
732 Determines the fill-value settings of the NetCDF variable @var{varid}.\n\ |
|
733 If @var{no_fill} is false, then the values between no-contiguous writes are filled with the value @var{fill_value}. This is disabled by setting @var{no_fill} to true.\n\ |
|
734 @end deftypefn\n\ |
|
735 @seealso{netcdf_defVarFill}\n") |
|
736 { |
|
737 |
|
738 if (args.length() != 2) { |
|
739 print_usage (); |
|
740 return octave_value(); |
|
741 } |
|
742 |
|
743 int ncid = args(0).scalar_value(); |
|
744 int varid = args(1).scalar_value(); |
|
745 int no_fill; |
|
746 nc_type xtype; |
|
747 octave_value_list retval; |
|
748 octave_value data; |
|
749 check_err(nc_inq_vartype (ncid, varid, &xtype)); |
|
750 |
|
751 switch (xtype) |
|
752 { |
|
753 #define OV_NETCDF_INQ_VAR_FILL(netcdf_type,c_type) \ |
|
754 case netcdf_type: \ |
|
755 { \ |
|
756 Array< c_type > fill_value = Array< c_type >(dim_vector(1,1)); \ |
|
757 check_err(nc_inq_var_fill(ncid, varid, &no_fill, \ |
|
758 fill_value.fortran_vec())); \ |
|
759 data = octave_value(fill_value); \ |
|
760 break; \ |
|
761 } |
|
762 |
|
763 OV_NETCDF_INQ_VAR_FILL(NC_BYTE,octave_int8) |
|
764 OV_NETCDF_INQ_VAR_FILL(NC_UBYTE,octave_uint8) |
|
765 OV_NETCDF_INQ_VAR_FILL(NC_SHORT,octave_int16) |
|
766 OV_NETCDF_INQ_VAR_FILL(NC_USHORT,octave_uint16) |
|
767 OV_NETCDF_INQ_VAR_FILL(NC_INT,octave_int32) |
|
768 OV_NETCDF_INQ_VAR_FILL(NC_UINT,octave_uint32) |
|
769 OV_NETCDF_INQ_VAR_FILL(NC_INT64,octave_int64) |
|
770 OV_NETCDF_INQ_VAR_FILL(NC_UINT64,octave_uint64) |
|
771 |
|
772 OV_NETCDF_INQ_VAR_FILL(NC_FLOAT,float) |
|
773 OV_NETCDF_INQ_VAR_FILL(NC_DOUBLE,double) |
|
774 |
|
775 OV_NETCDF_INQ_VAR_FILL(NC_CHAR,char) |
|
776 } |
|
777 |
|
778 //cout << "xtype3 " << xtype << " " << NC_DOUBLE << std::endl; |
|
779 retval(0) = octave_value(no_fill); |
|
780 retval(1) = data; |
|
781 return retval; |
|
782 } |
|
783 |
|
784 |
|
785 |
|
786 |
|
787 //nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, |
|
788 // int deflate_level); |
|
789 DEFUN_DLD(netcdf_defVarDeflate, args,, |
|
790 "-*- texinfo -*-\n\ |
|
791 @deftypefn {Loadable Function} {} netcdf_defVarDeflate (@var{ncid},@var{varid},@var{shuffle},@var{deflate},@var{deflate_level}) \n\ |
|
792 Define the compression settings NetCDF variable @var{varid}.\n\ |
|
793 If @var{deflate} is true, then the variable is compressed. The compression level @var{deflate_level} is an integer between 0 (no compression) and 9 (maximum compression).\n\ |
|
794 @end deftypefn\n\ |
|
795 @seealso{netcdf_inqVarDeflate}\n") |
|
796 { |
|
797 |
|
798 if (args.length() != 5) { |
|
799 print_usage (); |
|
800 return octave_value(); |
|
801 } |
|
802 |
|
803 int ncid = args(0).scalar_value(); |
|
804 int varid = args(1).scalar_value(); |
|
805 int shuffle = args(2).scalar_value(); // boolean |
|
806 int deflate = args(3).scalar_value(); // boolean |
|
807 int deflate_level = args(4).scalar_value(); |
|
808 |
|
809 check_err(nc_def_var_deflate (ncid, varid, shuffle, deflate, deflate_level)); |
|
810 return octave_value(); |
|
811 } |
|
812 |
|
813 |
|
814 //nc_inq_var_deflate(int ncid, int varid, int *shufflep, |
|
815 // int *deflatep, int *deflate_levelp); |
|
816 DEFUN_DLD(netcdf_inqVarDeflate, args,, |
|
817 "-*- texinfo -*-\n\ |
|
818 @deftypefn {Loadable Function} {[@var{shuffle},@var{deflate},@var{deflate_level}] = } netcdf_inqVarDeflate (@var{ncid},@var{varid}) \n\ |
|
819 Determines the compression settings NetCDF variable @var{varid}.\n\ |
|
820 If @var{deflate} is true, then the variable is compressed. The compression level @var{deflate_level} is an integer between 0 (no compression) and 9 (maximum compression).\n\ |
|
821 @end deftypefn\n\ |
|
822 @seealso{netcdf_defVarDeflate}\n") |
|
823 { |
|
824 |
|
825 if (args.length() != 2) { |
|
826 print_usage (); |
|
827 return octave_value(); |
|
828 } |
|
829 |
|
830 int ncid = args(0).scalar_value(); |
|
831 int varid = args(1).scalar_value(); |
|
832 int shuffle, deflate, deflate_level; |
|
833 octave_value_list retval; |
|
834 |
|
835 if (! error_state) { |
|
836 int format; |
|
837 check_err(nc_inq_format(ncid, &format)); |
|
838 |
|
839 // nc_inq_var_deflate returns garbage for classic or 64bit files |
|
840 if (format == NC_FORMAT_CLASSIC || format == NC_FORMAT_64BIT) { |
|
841 shuffle = 0; |
|
842 deflate = 0; |
|
843 deflate_level = 0; |
|
844 } |
|
845 else { |
|
846 check_err(nc_inq_var_deflate(ncid, varid, |
|
847 &shuffle,&deflate,&deflate_level)); |
|
848 } |
|
849 |
|
850 retval(0) = octave_value(shuffle); |
|
851 retval(1) = octave_value(deflate); |
|
852 retval(2) = octave_value(deflate_level); |
|
853 } |
|
854 |
|
855 return retval; |
|
856 } |
|
857 |
|
858 //int nc_def_var_chunking(int ncid, int varid, int storage, size_t *chunksizesp); |
|
859 //chunksizes can be ommited if storage is \"CONTIGUOUS\" |
|
860 DEFUN_DLD(netcdf_defVarChunking, args,, |
|
861 "-*- texinfo -*-\n\ |
|
862 @deftypefn {Loadable Function} {} netcdf_defVarChunking (@var{ncid},@var{varid},@var{storage},@var{chunkSizes}) \n\ |
|
863 Define the chunking settings of NetCDF variable @var{varid}.\n\ |
|
864 If @var{storage} is the string \"chunked\", the variable is stored by chunk of the size @var{chunkSizes}.\n\ |
|
865 If @var{storage} is the string \"contiguous\", the variable is stored in a contiguous way.\n\ |
|
866 @end deftypefn\n\ |
|
867 @seealso{netcdf_inqVarChunking}\n") |
|
868 { |
|
869 |
|
870 if (args.length() != 3 && args.length() != 4) { |
|
871 print_usage (); |
|
872 return octave_value(); |
|
873 } |
|
874 |
|
875 int ncid = args(0).scalar_value(); |
|
876 int varid = args(1).scalar_value(); |
|
877 std::string storagestr = args(2).string_value(); |
|
878 int storage; |
|
879 |
|
880 if (! error_state) { |
|
881 std::transform(storagestr.begin(), storagestr.end(),storagestr.begin(), ::toupper); |
|
882 |
|
883 if (storagestr == "CHUNKED") { |
|
884 storage = NC_CHUNKED; |
|
885 } |
|
886 else if (storagestr == "CONTIGUOUS") { |
|
887 storage = NC_CONTIGUOUS; |
|
888 } |
|
889 else { |
|
890 error("unknown storage %s",storagestr.c_str()); |
|
891 return octave_value(); |
|
892 } |
|
893 |
|
894 if (args.length() == 4) { |
|
895 Array<double> tmp = args(3).vector_value(); |
|
896 |
|
897 OCTAVE_LOCAL_BUFFER (size_t, chunksizes, tmp.numel()); |
|
898 for (int i = 0; i < tmp.numel(); i++) |
|
899 { |
|
900 chunksizes[i] = tmp(tmp.numel()-i-1); |
|
901 } |
|
902 |
|
903 check_err(nc_def_var_chunking(ncid, varid, storage, chunksizes)); |
|
904 } |
|
905 else { |
|
906 check_err(nc_def_var_chunking(ncid, varid, storage, NULL)); |
|
907 } |
|
908 } |
|
909 |
|
910 return octave_value(); |
|
911 } |
|
912 |
|
913 //int nc_inq_var_chunking(int ncid, int varid, int *storagep, size_t *chunksizesp); |
|
914 DEFUN_DLD(netcdf_inqVarChunking, args,, |
|
915 "-*- texinfo -*-\n\ |
|
916 @deftypefn {Loadable Function} {[@var{storage},@var{chunkSizes}] = } netcdf_inqVarChunking (@var{ncid},@var{varid}) \n\ |
|
917 Determines the chunking settings of NetCDF variable @var{varid}.\n\ |
|
918 If @var{storage} is the string \"chunked\", the variable is stored by chunk of the size @var{chunkSizes}.\n\ |
|
919 If @var{storage} is the string \"contiguous\", the variable is stored in a contiguous way.\n\ |
|
920 @end deftypefn\n\ |
|
921 @seealso{netcdf_defVarChunking}\n") |
|
922 { |
|
923 |
|
924 if (args.length() != 2) { |
|
925 print_usage (); |
|
926 return octave_value(); |
|
927 } |
|
928 |
|
929 int ncid = args(0).scalar_value(); |
|
930 int varid = args(1).scalar_value(); |
|
931 int storage; |
|
932 int ndims; |
|
933 octave_value_list retval; |
|
934 |
|
935 check_err(nc_inq_varndims (ncid, varid, &ndims)); |
|
936 OCTAVE_LOCAL_BUFFER (size_t, chunksizes, ndims); |
|
937 |
|
938 if (! error_state) { |
|
939 check_err(nc_inq_var_chunking(ncid, varid, &storage, chunksizes)); |
|
940 |
|
941 if (storage == NC_CHUNKED) { |
|
942 retval(0) = octave_value("chunked"); |
|
943 Array<int> chunkSizes = Array<int>(dim_vector(1,ndims)); |
|
944 |
|
945 for (int i = 0; i < ndims; i++) |
|
946 { |
|
947 chunkSizes(ndims-i-1) = chunksizes[i]; |
|
948 } |
|
949 retval(1) = octave_value(chunkSizes); |
|
950 } |
|
951 else { |
|
952 retval(0) = octave_value("contiguous"); |
|
953 retval(1) = octave_value(Array<double>()); |
|
954 } |
|
955 |
|
956 } |
|
957 |
|
958 return retval; |
|
959 } |
|
960 |
|
961 // nc_def_var_fletcher32(int ncid, int varid, int checksum); |
|
962 DEFUN_DLD(netcdf_defVarFletcher32, args,, |
|
963 "-*- texinfo -*-\n\ |
|
964 @deftypefn {Loadable Function} {} netcdf_defVarFletcher32(@var{ncid},@var{varid},@var{checksum}) \n\ |
|
965 Defines the checksum settings of the variable with the id @var{varid} in the data set @var{ncid}. If @var{checksum} is the string \"fletcher32\", then fletcher32 checksums will be turned on for this variable. If @var{checksum} is \"nochecksum\", then checksums will be disabled. \n\ |
|
966 @end deftypefn\n\ |
|
967 @seealso{netcdf_defVar,netcdf_inqVarFletcher32}\n") |
|
968 { |
|
969 |
|
970 if (args.length() != 3) |
|
971 { |
|
972 print_usage (); |
|
973 return octave_value(); |
|
974 } |
|
975 |
|
976 int ncid = args(0).scalar_value(); |
|
977 int varid = args(1).scalar_value(); |
|
978 int checksum = netcdf_get_constant(args(2)).int_value(); |
|
979 |
|
980 if (error_state) |
|
981 { |
|
982 print_usage (); |
|
983 return octave_value(); |
|
984 } |
|
985 |
|
986 check_err(nc_def_var_fletcher32(ncid, varid, checksum)); |
|
987 |
|
988 return octave_value(); |
|
989 } |
|
990 |
|
991 |
|
992 |
|
993 DEFUN_DLD(netcdf_inqVarFletcher32, args,, |
|
994 "-*- texinfo -*-\n\ |
|
995 @deftypefn {Loadable Function} {@var{checksum} =} netcdf_inqVarFletcher32(@var{ncid},@var{varid}) \n\ |
|
996 Determines the checksum settings of the variable with the id @var{varid} in the data set @var{ncid}. If fletcher32 checksums is turned on for this variable, then @var{checksum} is the string \"fletcher32\". Otherwise it is the string \"nochecksum\". \n\ |
|
997 @end deftypefn\n\ |
|
998 @seealso{netcdf_defVar,netcdf_inqVarFletcher32}\n") |
|
999 { |
|
1000 |
|
1001 if (args.length() != 2) |
|
1002 { |
|
1003 print_usage (); |
|
1004 return octave_value(); |
|
1005 } |
|
1006 |
|
1007 int ncid = args(0).scalar_value(); |
|
1008 int varid = args(1).scalar_value(); |
|
1009 int checksum; |
|
1010 |
|
1011 if (error_state) |
|
1012 { |
|
1013 print_usage (); |
|
1014 return octave_value(); |
|
1015 } |
|
1016 |
|
1017 check_err(nc_inq_var_fletcher32(ncid, varid, &checksum)); |
|
1018 |
|
1019 if (checksum == NC_FLETCHER32) |
|
1020 { |
|
1021 return octave_value("fletcher32"); |
|
1022 } |
|
1023 else |
|
1024 { |
|
1025 return octave_value("nochecksum"); |
|
1026 } |
|
1027 } |
|
1028 |
|
1029 |
|
1030 |
|
1031 DEFUN_DLD(netcdf_endDef, args,, |
|
1032 "-*- texinfo -*-\n\ |
|
1033 @deftypefn {Loadable Function} {} netcdf_endDef (@var{ncid}) \n\ |
|
1034 Leaves define-mode of NetCDF file @var{ncid}.\n\ |
|
1035 @end deftypefn\n\ |
|
1036 @seealso{netcdf_reDef}\n") |
|
1037 { |
|
1038 if (args.length() != 1) |
|
1039 { |
|
1040 print_usage (); |
|
1041 return octave_value(); |
|
1042 } |
|
1043 |
|
1044 int ncid = args(0).scalar_value(); |
|
1045 check_err(nc_enddef (ncid)); |
|
1046 |
|
1047 return octave_value(); |
|
1048 } |
|
1049 |
|
1050 DEFUN_DLD(netcdf_reDef, args,, |
|
1051 "-*- texinfo -*-\n\ |
|
1052 @deftypefn {Loadable Function} {} netcdf_reDef (@var{ncid}) \n\ |
|
1053 Enter define-mode of NetCDF file @var{ncid}.\n\ |
|
1054 @end deftypefn\n\ |
|
1055 @seealso{netcdf_endDef}\n") |
|
1056 { |
|
1057 if (args.length() != 1) |
|
1058 { |
|
1059 print_usage (); |
|
1060 return octave_value(); |
|
1061 } |
|
1062 |
|
1063 int ncid = args(0).scalar_value(); |
|
1064 check_err(nc_redef (ncid)); |
|
1065 |
|
1066 return octave_value(); |
|
1067 } |
|
1068 |
|
1069 // http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c/nc_005fput_005fvar_005f-type.html#nc_005fput_005fvar_005f-type |
|
1070 |
|
1071 DEFUN_DLD(netcdf_putVar, args,, |
|
1072 "-*- texinfo -*-\n\ |
|
1073 @deftypefn {Loadable Function} {} netcdf_putVar (@var{ncid},@var{varid},@var{data}) \n\ |
|
1074 @deftypefnx {Loadable Function} {} netcdf_putVar (@var{ncid},@var{varid},@var{start},@var{data}) \n\ |
|
1075 @deftypefnx {Loadable Function} {} netcdf_putVar (@var{ncid},@var{varid},@var{start},@var{count},@var{data}) \n\ |
|
1076 @deftypefnx {Loadable Function} {} netcdf_putVar (@var{ncid},@var{varid},@var{start},@var{count},@var{stride},@var{data}) \n\ |
|
1077 Put data in a NetCDF variable.\n\ |
|
1078 The data @var{data} is stored in the variable @var{varid} of the NetCDF file @var{ncid}. \n\ |
|
1079 @var{start} is the start index of each dimension (0-based and defaults to a vector of zeros), \n\ |
|
1080 @var{count} is the number of elements of to be written along each dimension (default all elements)\n\ |
|
1081 and @var{stride} is the sampling interval.\n\ |
|
1082 @end deftypefn\n\ |
|
1083 @seealso{netcdf_endDef}\n") |
|
1084 { |
|
1085 if (args.length() < 3 || args.length() > 6) |
|
1086 { |
|
1087 print_usage (); |
|
1088 return octave_value(); |
|
1089 } |
|
1090 |
|
1091 int ncid = args(0).scalar_value(); |
|
1092 int varid = args(1).scalar_value (); |
|
1093 octave_value data = args(args.length()-1); |
|
1094 size_t start[NC_MAX_VAR_DIMS]; |
|
1095 size_t count[NC_MAX_VAR_DIMS]; |
|
1096 ptrdiff_t stride[NC_MAX_VAR_DIMS]; |
|
1097 nc_type xtype; |
|
1098 |
|
1099 check_err(nc_inq_vartype (ncid, varid, &xtype)); |
|
1100 //int sliced_numel = tmp.numel(); |
|
1101 |
|
1102 start_count_stride(ncid, varid, args, args.length()-1, start, count, stride); |
|
1103 |
|
1104 // check if count matched size(data) |
|
1105 |
|
1106 switch (xtype) |
|
1107 { |
|
1108 #define OV_NETCDF_PUT_VAR(netcdf_type,c_type,method) \ |
|
1109 case netcdf_type: \ |
|
1110 { \ |
|
1111 check_err(nc_put_vars (ncid, varid, start, count, stride, (c_type*)data.method().fortran_vec())); \ |
|
1112 break; \ |
|
1113 } |
|
1114 |
|
1115 OV_NETCDF_PUT_VAR(NC_BYTE, signed char, int8_array_value) |
|
1116 OV_NETCDF_PUT_VAR(NC_UBYTE, unsigned char, uint8_array_value) |
|
1117 OV_NETCDF_PUT_VAR(NC_SHORT, short, int16_array_value) |
|
1118 OV_NETCDF_PUT_VAR(NC_USHORT, unsigned short, uint16_array_value) |
|
1119 OV_NETCDF_PUT_VAR(NC_INT, int, int32_array_value) |
|
1120 OV_NETCDF_PUT_VAR(NC_UINT, unsigned int, uint32_array_value) |
|
1121 OV_NETCDF_PUT_VAR(NC_INT64, long long, int64_array_value) |
|
1122 OV_NETCDF_PUT_VAR(NC_UINT64, unsigned long long, uint64_array_value) |
|
1123 |
|
1124 OV_NETCDF_PUT_VAR(NC_FLOAT, float, float_array_value) |
|
1125 OV_NETCDF_PUT_VAR(NC_DOUBLE,double,array_value) |
|
1126 |
|
1127 OV_NETCDF_PUT_VAR(NC_CHAR, char, char_array_value) |
|
1128 default: |
|
1129 { |
|
1130 error("unknown type %d" ,xtype); |
|
1131 } |
|
1132 } |
|
1133 return octave_value(); |
|
1134 } |
|
1135 |
|
1136 |
|
1137 |
|
1138 DEFUN_DLD(netcdf_getVar, args,, |
|
1139 "-*- texinfo -*-\n\ |
|
1140 @deftypefn {Loadable Function} {@var{data} =} netcdf_getVar (@var{ncid},@var{varid}) \n\ |
|
1141 @deftypefnx {Loadable Function} {@var{data} =} netcdf_getVar (@var{ncid},@var{varid},@var{start}) \n\ |
|
1142 @deftypefnx {Loadable Function} {@var{data} =} netcdf_getVar (@var{ncid},@var{varid},@var{start},@var{count}) \n\ |
|
1143 @deftypefnx {Loadable Function} {@var{data} =} netcdf_getVar (@var{ncid},@var{varid},@var{start},@var{count},@var{stride}) \n\ |
|
1144 Get the data from a NetCDF variable.\n\ |
|
1145 The data @var{data} is loaded from the variable @var{varid} of the NetCDF file @var{ncid}. \n\ |
|
1146 @var{start} is the start index of each dimension (0-based and defaults to a vector of zeros), \n\ |
|
1147 @var{count} is the number of elements of to be written along each dimension (default all elements)\n\ |
|
1148 and @var{stride} is the sampling interval.\n\ |
|
1149 @end deftypefn\n\ |
|
1150 @seealso{netcdf_putVar}\n") |
|
1151 { |
|
1152 if (args.length() < 2 || args.length() > 5) |
|
1153 { |
|
1154 print_usage (); |
|
1155 return octave_value(); |
|
1156 } |
|
1157 |
|
1158 int ncid = args(0).scalar_value(); |
|
1159 int varid = args(1).scalar_value (); |
|
1160 std::list<Range> ranges; |
|
1161 int ndims; |
|
1162 size_t start[NC_MAX_VAR_DIMS]; |
|
1163 size_t count[NC_MAX_VAR_DIMS]; |
|
1164 ptrdiff_t stride[NC_MAX_VAR_DIMS]; |
|
1165 |
|
1166 octave_value data; |
|
1167 nc_type xtype; |
|
1168 |
|
1169 check_err(nc_inq_vartype (ncid, varid, &xtype)); |
|
1170 check_err(nc_inq_varndims (ncid, varid, &ndims)); |
|
1171 int sz = 1; |
|
1172 |
|
1173 dim_vector sliced_dim_vector; |
|
1174 |
|
1175 if (ndims < 2) |
|
1176 { |
|
1177 sliced_dim_vector.resize(2); |
|
1178 sliced_dim_vector(0) = 1; |
|
1179 sliced_dim_vector(1) = 1; |
|
1180 } |
|
1181 else |
|
1182 { |
|
1183 sliced_dim_vector.resize(ndims); |
|
1184 } |
|
1185 |
|
1186 start_count_stride(ncid, varid, args, args.length(), start, count, stride); |
|
1187 |
|
1188 if (error_state) |
|
1189 { |
|
1190 print_usage (); |
|
1191 return octave_value(); |
|
1192 } |
|
1193 |
|
1194 |
|
1195 // total size sz |
|
1196 for (int i=0; i<ndims; i++) { |
|
1197 sz = sz * count[i]; |
|
1198 sliced_dim_vector(i) = count[ndims-i-1]; |
|
1199 //sliced_dim_vector(i) = count[i]; |
|
1200 } |
|
1201 |
|
1202 |
|
1203 //cout << "start " << start[0] << endl; |
|
1204 // need to take count and stride |
|
1205 |
|
1206 |
|
1207 |
|
1208 //cout << "sz " << sz << endl; |
|
1209 //cout << "sliced_dim_vector " << sliced_dim_vector(0) << " x " << sliced_dim_vector(1) << endl; |
|
1210 |
|
1211 switch (xtype) |
|
1212 { |
|
1213 #define OV_NETCDF_GET_VAR_CASE(netcdf_type,c_type) \ |
|
1214 case netcdf_type: \ |
|
1215 { \ |
|
1216 Array < c_type > arr = Array < c_type >(sliced_dim_vector); \ |
|
1217 check_err(nc_get_vars(ncid, varid, start, count, stride, arr.fortran_vec())); \ |
|
1218 data = octave_value(arr); \ |
|
1219 break; \ |
|
1220 } |
|
1221 |
|
1222 OV_NETCDF_GET_VAR_CASE(NC_BYTE,octave_int8) |
|
1223 OV_NETCDF_GET_VAR_CASE(NC_UBYTE,octave_uint8) |
|
1224 OV_NETCDF_GET_VAR_CASE(NC_SHORT,octave_int16) |
|
1225 OV_NETCDF_GET_VAR_CASE(NC_USHORT,octave_uint16) |
|
1226 OV_NETCDF_GET_VAR_CASE(NC_INT,octave_int32) |
|
1227 OV_NETCDF_GET_VAR_CASE(NC_UINT,octave_uint32) |
|
1228 OV_NETCDF_GET_VAR_CASE(NC_INT64,octave_int64) |
|
1229 OV_NETCDF_GET_VAR_CASE(NC_UINT64,octave_uint64) |
|
1230 |
|
1231 OV_NETCDF_GET_VAR_CASE(NC_FLOAT,float) |
|
1232 OV_NETCDF_GET_VAR_CASE(NC_DOUBLE,double) |
|
1233 |
|
1234 OV_NETCDF_GET_VAR_CASE(NC_CHAR, char) |
|
1235 |
|
1236 default: |
|
1237 { |
|
1238 error("unknown type %d" ,xtype); |
|
1239 } |
|
1240 |
|
1241 } |
|
1242 |
|
1243 return data; |
|
1244 } |
|
1245 |
|
1246 DEFUN_DLD(netcdf_close, args,, |
|
1247 "-*- texinfo -*-\n\ |
|
1248 @deftypefn {Loadable Function} {} netcdf_close(@var{ncid}) \n\ |
|
1249 Close the NetCDF file with the id @var{ncid}.\n\ |
|
1250 @end deftypefn\n\ |
|
1251 @seealso{netcdf_open}\n") |
|
1252 { |
|
1253 |
|
1254 if (args.length() != 1) { |
|
1255 print_usage (); |
|
1256 return octave_value(); |
|
1257 } |
|
1258 |
|
1259 int ncid = args(0).scalar_value(); |
|
1260 check_err(nc_close(ncid)); |
|
1261 return octave_value (); |
|
1262 } |
|
1263 |
|
1264 |
|
1265 |
|
1266 // int nc_inq_attname(int ncid, int varid, int attnum, char *name); |
|
1267 |
|
1268 DEFUN_DLD(netcdf_inqAttName, args,, |
|
1269 "-*- texinfo -*-\n\ |
|
1270 @deftypefn {Loadable Function} {@var{name} =} netcdf_inqAttName (@var{ncid},@var{varid},@var{attnum}) \n\ |
|
1271 Get the name of a NetCDF attribute.\n\ |
|
1272 This function returns the name of the attribute with the id @var{attnum} of the variable \n\ |
|
1273 @var{varid} in the NetCDF file @var{ncid}. For global attributes @var{varid} can be \n\ |
|
1274 netcdf_getConstant(\"global\").\n\ |
|
1275 @seealso{netcdf_inqAttName}\n\ |
|
1276 @end deftypefn") |
|
1277 { |
|
1278 if (args.length() != 3) { |
|
1279 print_usage (); |
|
1280 return octave_value(); |
|
1281 } |
|
1282 |
|
1283 int ncid = args(0).scalar_value(); |
|
1284 int varid = args(1).scalar_value(); |
|
1285 int attnum = args(2).scalar_value(); |
|
1286 char name[NC_MAX_NAME+1]; |
|
1287 |
|
1288 check_err(nc_inq_attname(ncid, varid, attnum, name)); |
|
1289 |
|
1290 return octave_value(std::string(name)); |
|
1291 } |
|
1292 |
|
1293 |
|
1294 DEFUN_DLD(netcdf_inqAttID, args,, |
|
1295 "-*- texinfo -*-\n\ |
|
1296 @deftypefn {Loadable Function} {@var{attnum} =} netcdf_inqAttID(@var{ncid},@var{varid},@var{attname}) \n\ |
|
1297 Return the attribute id @var{attnum} of the attribute named @var{attname} of the variable @var{varid} in the dataset @var{ncid}. \n\ |
|
1298 For global attributes @var{varid} can be \n\ |
|
1299 netcdf_getConstant(\"global\").\n\ |
|
1300 @seealso{netcdf_inqAttName}\n\ |
|
1301 @end deftypefn") |
|
1302 { |
|
1303 if (args.length() != 3) |
|
1304 { |
|
1305 print_usage (); |
|
1306 return octave_value (); |
|
1307 } |
|
1308 int ncid = args(0).scalar_value(); |
|
1309 int varid = args(1).scalar_value(); |
|
1310 std::string attname = args(2).string_value(); |
|
1311 int attnum; |
|
1312 |
|
1313 if (error_state) |
|
1314 { |
|
1315 print_usage (); |
|
1316 return octave_value (); |
|
1317 } |
|
1318 |
|
1319 check_err (nc_inq_attid (ncid, varid, attname.c_str(), &attnum)); |
|
1320 |
|
1321 return octave_value(attnum); |
|
1322 } |
|
1323 |
|
1324 |
|
1325 //int nc_inq_att (int ncid, int varid, const char *name, |
|
1326 // nc_type *xtypep, size_t *lenp); |
|
1327 |
|
1328 DEFUN_DLD(netcdf_inqAtt, args,, |
|
1329 "-*- texinfo -*-\n\ |
|
1330 @deftypefn {Loadable Function} {[@var{xtype},@var{len}] = } netcdf_inqAtt(@var{ncid},@var{varid},@var{name}) \n\ |
|
1331 Get attribute type and length.\n\ |
|
1332 @seealso{netcdf_inqAttName}\n\ |
|
1333 @end deftypefn") |
|
1334 { |
|
1335 if (args.length() != 3) { |
|
1336 print_usage (); |
|
1337 return octave_value(); |
|
1338 } |
|
1339 |
|
1340 int ncid = args(0).scalar_value(); |
|
1341 int varid = args(1).scalar_value(); |
|
1342 std::string name = args(2).string_value(); |
|
1343 int xtype; |
|
1344 size_t len; |
|
1345 octave_value_list retval; |
|
1346 |
|
1347 if (error_state) { |
|
1348 return octave_value(); |
|
1349 } |
|
1350 |
|
1351 check_err(nc_inq_att(ncid, varid, name.c_str(), &xtype, &len)); |
|
1352 |
|
1353 retval(0) = octave_value(xtype); |
|
1354 retval(1) = octave_value(len); |
|
1355 return retval; |
|
1356 } |
|
1357 |
|
1358 |
|
1359 DEFUN_DLD(netcdf_getAtt, args,, |
|
1360 "-*- texinfo -*-\n\ |
|
1361 @deftypefn {Loadable Function} {@var{data} =} netcdf_getAtt (@var{ncid},@var{varid},@var{name}) \n\ |
|
1362 Get the value of a NetCDF attribute.\n\ |
|
1363 This function returns the value of the attribute called @var{name} of the variable \n\ |
|
1364 @var{varid} in the NetCDF file @var{ncid}. For global attributes @var{varid} can be \n\ |
|
1365 netcdf_getConstant(\"global\").\n\ |
|
1366 @seealso{netcdf_putAtt}\n\ |
|
1367 @end deftypefn") |
|
1368 { |
|
1369 if (args.length() != 3) { |
|
1370 print_usage (); |
|
1371 return octave_value(); |
|
1372 } |
|
1373 |
|
1374 int ncid = args(0).scalar_value(); |
|
1375 int varid = args(1).scalar_value(); |
|
1376 std::string attname = args(2).string_value(); |
|
1377 nc_type xtype; |
|
1378 size_t len; |
|
1379 octave_value data; |
|
1380 |
|
1381 check_err(nc_inq_att(ncid, varid, attname.c_str(), &xtype, &len)); |
|
1382 |
|
1383 #define OV_NETCDF_GET_ATT_CASE(netcdf_type,c_type) \ |
|
1384 if (xtype == netcdf_type) \ |
|
1385 { \ |
|
1386 Array< c_type > arr = Array< c_type >(dim_vector(1,len)); \ |
|
1387 check_err(nc_get_att(ncid, varid, attname.c_str(), arr.fortran_vec())); \ |
|
1388 data = octave_value(arr); \ |
|
1389 } |
|
1390 OV_NETCDF_GET_ATT_CASE(NC_BYTE,octave_int8) |
|
1391 OV_NETCDF_GET_ATT_CASE(NC_UBYTE,octave_uint8) |
|
1392 OV_NETCDF_GET_ATT_CASE(NC_SHORT,octave_int16) |
|
1393 OV_NETCDF_GET_ATT_CASE(NC_USHORT,octave_uint16) |
|
1394 OV_NETCDF_GET_ATT_CASE(NC_INT,octave_int32) |
|
1395 OV_NETCDF_GET_ATT_CASE(NC_UINT,octave_uint32) |
|
1396 OV_NETCDF_GET_ATT_CASE(NC_INT64,octave_int64) |
|
1397 OV_NETCDF_GET_ATT_CASE(NC_UINT64,octave_uint64) |
|
1398 |
|
1399 OV_NETCDF_GET_ATT_CASE(NC_FLOAT,float) |
|
1400 OV_NETCDF_GET_ATT_CASE(NC_DOUBLE,double) |
|
1401 |
|
1402 OV_NETCDF_GET_ATT_CASE(NC_CHAR, char) |
|
1403 |
|
1404 |
|
1405 return data; |
|
1406 } |
|
1407 |
|
1408 |
|
1409 DEFUN_DLD(netcdf_putAtt, args,, |
|
1410 "-*- texinfo -*-\n\ |
|
1411 @deftypefn {Loadable Function} {} netcdf_putAtt (@var{ncid},@var{varid},@var{name},@var{data}) \n\ |
|
1412 Defines a NetCDF attribute.\n\ |
|
1413 This function defines the attribute called @var{name} of the variable \n\ |
|
1414 @var{varid} in the NetCDF file @var{ncid}. The value of the attribute will be @var{data}. \n\ |
|
1415 For global attributes @var{varid} can be \n\ |
|
1416 netcdf_getConstant(\"global\").\n\ |
|
1417 @seealso{netcdf_getAtt}\n\ |
|
1418 @end deftypefn") |
|
1419 { |
|
1420 if (args.length() != 4) { |
|
1421 print_usage (); |
|
1422 return octave_value(); |
|
1423 } |
|
1424 |
|
1425 int ncid = args(0).scalar_value(); |
|
1426 int varid = args(1).scalar_value(); |
|
1427 std::string attname = args(2).string_value(); |
|
1428 octave_value data = args(3); |
|
1429 |
|
1430 nc_type xtype; |
|
1431 xtype = NC_BYTE; |
|
1432 |
|
1433 // get matching netcdf type |
|
1434 |
|
1435 if (data.is_string()) |
|
1436 xtype = NC_CHAR; |
|
1437 else if (data.is_int8_type()) |
|
1438 xtype = NC_BYTE; |
|
1439 else if (data.is_uint8_type()) |
|
1440 xtype = NC_UBYTE; |
|
1441 else if (data.is_int16_type()) |
|
1442 xtype = NC_SHORT; |
|
1443 else if (data.is_uint16_type()) |
|
1444 xtype = NC_USHORT; |
|
1445 else if (data.is_int32_type()) |
|
1446 xtype = NC_INT; |
|
1447 else if (data.is_uint32_type()) |
|
1448 xtype = NC_UINT; |
|
1449 else if (data.is_int64_type()) |
|
1450 xtype = NC_INT64; |
|
1451 else if (data.is_uint64_type()) |
|
1452 xtype = NC_UINT64; |
|
1453 else if (data.is_single_type()) |
|
1454 xtype = NC_FLOAT; |
|
1455 else |
|
1456 xtype = NC_DOUBLE; |
|
1457 |
|
1458 //cout << "xtype " << xtype << endl; |
|
1459 size_t len = data.numel(); |
|
1460 |
|
1461 switch (xtype) |
|
1462 { |
|
1463 #define OV_NETCDF_PUT_ATT(netcdf_type,c_type,method) \ |
|
1464 case netcdf_type: \ |
|
1465 { \ |
|
1466 check_err(nc_put_att (ncid, varid, attname.c_str(), xtype, len, data.method().fortran_vec())); \ |
|
1467 break; \ |
|
1468 } |
|
1469 |
|
1470 OV_NETCDF_PUT_ATT(NC_BYTE, signed char, int8_array_value) |
|
1471 OV_NETCDF_PUT_ATT(NC_UBYTE, unsigned char, uint8_array_value) |
|
1472 OV_NETCDF_PUT_ATT(NC_SHORT, short, int16_array_value) |
|
1473 OV_NETCDF_PUT_ATT(NC_USHORT, unsigned short, uint16_array_value) |
|
1474 OV_NETCDF_PUT_ATT(NC_INT, int, int32_array_value) |
|
1475 OV_NETCDF_PUT_ATT(NC_UINT, unsigned int, uint32_array_value) |
|
1476 OV_NETCDF_PUT_ATT(NC_INT64, long long, int64_array_value) |
|
1477 OV_NETCDF_PUT_ATT(NC_UINT64, unsigned long long, uint64_array_value) |
|
1478 |
|
1479 OV_NETCDF_PUT_ATT(NC_FLOAT, float, float_array_value) |
|
1480 OV_NETCDF_PUT_ATT(NC_DOUBLE,double,array_value) |
|
1481 |
|
1482 OV_NETCDF_PUT_ATT(NC_CHAR, char, char_array_value) |
|
1483 } |
|
1484 |
|
1485 /* check_err(nc_put_att (int ncid, int varid, const char *name, nc_type xtype, |
|
1486 size_t len, const void *op));*/ |
|
1487 |
|
1488 return octave_value(); |
|
1489 |
|
1490 } |
|
1491 |
|
1492 |
|
1493 DEFUN_DLD(netcdf_copyAtt, args,, |
|
1494 "-*- texinfo -*-\n\ |
|
1495 @deftypefn {Loadable Function} {} netcdf_copyAtt (@var{ncid},@var{varid},@var{name},@var{ncid_out},@var{varid_out}) \n\ |
|
1496 Copies the attribute named @var{old_name} of the variable @var{varid} in the data set @var{ncid} \n\ |
|
1497 to the variable @var{varid_out} in the data set @var{ncid_out}. \n\ |
|
1498 To copy a global attribute use netcdf_getConstant(\"global\") for @var{varid} or @var{varid_out}.\n\ |
|
1499 @seealso{netcdf_getAtt,netcdf_getConstant}\n\ |
|
1500 @end deftypefn") |
|
1501 { |
|
1502 |
|
1503 if (args.length() != 5) |
|
1504 { |
|
1505 print_usage (); |
|
1506 return octave_value (); |
|
1507 } |
|
1508 |
|
1509 int ncid = args(0).scalar_value(); |
|
1510 int varid = args(1).scalar_value(); |
|
1511 std::string name = args(2).string_value(); |
|
1512 int ncid_out = args(3).scalar_value(); |
|
1513 int varid_out = args(4).scalar_value(); |
|
1514 |
|
1515 if (error_state) |
|
1516 { |
|
1517 print_usage (); |
|
1518 return octave_value (); |
|
1519 } |
|
1520 |
|
1521 check_err (nc_copy_att (ncid, varid, name.c_str(), |
|
1522 ncid_out, varid_out)); |
|
1523 |
|
1524 return octave_value (); |
|
1525 } |
|
1526 |
|
1527 |
|
1528 DEFUN_DLD(netcdf_renameAtt, args,, |
|
1529 "-*- texinfo -*-\n\ |
|
1530 @deftypefn {Loadable Function} {} netcdf_renameAtt(@var{ncid},@var{varid},@var{old_name},@var{new_name}) \n\ |
|
1531 Renames the attribute named @var{old_name} of the variable @var{varid} in the data set @var{ncid}. @var{new_name} is the new name of the attribute.\n\ |
|
1532 To rename a global attribute use netcdf_getConstant(\"global\") for @var{varid}.\n\ |
|
1533 @seealso{netcdf_copyAtt,netcdf_getConstant}\n\ |
|
1534 @end deftypefn") |
|
1535 { |
|
1536 |
|
1537 if (args.length() != 4) |
|
1538 { |
|
1539 print_usage (); |
|
1540 return octave_value (); |
|
1541 } |
|
1542 |
|
1543 int ncid = args(0).scalar_value(); |
|
1544 int varid = args(1).scalar_value(); |
|
1545 std::string old_name = args(2).string_value(); |
|
1546 std::string new_name = args(3).string_value(); |
|
1547 |
|
1548 if (error_state) |
|
1549 { |
|
1550 print_usage (); |
|
1551 return octave_value (); |
|
1552 } |
|
1553 |
|
1554 check_err(nc_rename_att (ncid, varid, old_name.c_str(), new_name.c_str())); |
|
1555 |
|
1556 return octave_value (); |
|
1557 } |
|
1558 |
|
1559 |
|
1560 DEFUN_DLD(netcdf_delAtt, args,, |
|
1561 "-*- texinfo -*-\n\ |
|
1562 @deftypefn {Loadable Function} {} netcdf_delAtt(@var{ncid},@var{varid},@var{name}) \n\ |
|
1563 Deletes the attribute named @var{name} of the variable @var{varid} in the data set @var{ncid}. \n\ |
|
1564 To delete a global attribute use netcdf_getConstant(\"global\") for @var{varid}.\n\ |
|
1565 @seealso{netcdf_defAtt,netcdf_getConstant}\n\ |
|
1566 @end deftypefn") |
|
1567 { |
|
1568 |
|
1569 if (args.length() != 3) |
|
1570 { |
|
1571 print_usage (); |
|
1572 return octave_value (); |
|
1573 } |
|
1574 |
|
1575 int ncid = args(0).scalar_value(); |
|
1576 int varid = args(1).scalar_value(); |
|
1577 std::string name = args(2).string_value(); |
|
1578 |
|
1579 if (error_state) |
|
1580 { |
|
1581 print_usage (); |
|
1582 return octave_value (); |
|
1583 } |
|
1584 |
|
1585 check_err(nc_del_att (ncid, varid, name.c_str())); |
|
1586 |
|
1587 return octave_value (); |
|
1588 } |
|
1589 |
|
1590 |
|
1591 DEFUN_DLD(netcdf_inqVarID, args,, |
|
1592 "-*- texinfo -*-\n\ |
|
1593 @deftypefn {Loadable Function} {@var{varid} = } netcdf_inqVarID (@var{ncid},@var{name}) \n\ |
|
1594 Return the id of a variable based on its name.\n\ |
|
1595 @seealso{netcdf_defVar,netcdf_inqVarIDs}\n\ |
|
1596 @end deftypefn") |
|
1597 { |
|
1598 |
|
1599 if (args.length() != 2) { |
|
1600 print_usage (); |
|
1601 return octave_value(); |
|
1602 } |
|
1603 |
|
1604 int ncid = args(0).scalar_value(); |
|
1605 std::string varname = args(1).string_value(); |
|
1606 int varid; |
|
1607 |
|
1608 check_err(nc_inq_varid(ncid,varname.c_str(), &varid)); |
|
1609 |
|
1610 return octave_value(varid); |
|
1611 } |
|
1612 |
|
1613 DEFUN_DLD(netcdf_inqVarIDs, args,, |
|
1614 "-*- texinfo -*-\n\ |
|
1615 @deftypefn {Loadable Function} {@var{varids} = } netcdf_inqVarID (@var{ncid}) \n\ |
|
1616 Return all variable ids.\n\ |
|
1617 This functions returns all variable ids in a NetCDF file or NetCDF group.\n\ |
|
1618 @seealso{netcdf_inqVarID}\n\ |
|
1619 @end deftypefn") |
|
1620 { |
|
1621 |
|
1622 if (args.length() != 1) { |
|
1623 print_usage (); |
|
1624 return octave_value (); |
|
1625 } |
|
1626 |
|
1627 int ncid = args(0).scalar_value(); |
|
1628 int nvars; |
|
1629 |
|
1630 check_err(nc_inq_varids(ncid, &nvars, NULL)); |
|
1631 Array<int> varids = Array<int>(dim_vector(1,nvars)); |
|
1632 check_err(nc_inq_varids(ncid, &nvars, varids.fortran_vec())); |
|
1633 |
|
1634 return octave_value(varids); |
|
1635 } |
|
1636 |
|
1637 DEFUN_DLD(netcdf_inqVar, args,, |
|
1638 "-*- texinfo -*-\n\ |
|
1639 @deftypefn {Loadable Function} {[@var{name},@var{nctype},@var{dimids},@var{nattr}] = } netcdf_inqVarID (@var{ncid},@var{varid}) \n\ |
|
1640 Inquires information about a NetCDF variable.\n\ |
|
1641 This functions returns the @var{name}, the NetCDF type @var{nctype}, an array of dimension ids \n\ |
|
1642 @var{dimids} and the number of attributes @var{nattr} of the NetCDF variable. @var{nctype} in an \n\ |
|
1643 integer corresponding NetCDF constants.\n\ |
|
1644 @seealso{netcdf_inqVarID,netcdf_getConstant}\n\ |
|
1645 @end deftypefn") |
|
1646 { |
|
1647 |
|
1648 if (args.length() != 2) |
|
1649 { |
|
1650 print_usage (); |
|
1651 return octave_value(); |
|
1652 } |
|
1653 |
|
1654 int ncid = args(0).scalar_value(); |
|
1655 int varid = args(1).scalar_value(); |
|
1656 char name[NC_MAX_NAME+1]; |
|
1657 int ndims, dimids[NC_MAX_VAR_DIMS], natts; |
|
1658 nc_type xtype; |
|
1659 octave_value_list retval; |
|
1660 |
|
1661 if (error_state) |
|
1662 { |
|
1663 print_usage (); |
|
1664 return octave_value(); |
|
1665 } |
|
1666 |
|
1667 check_err(nc_inq_varndims(ncid, varid, &ndims)); |
|
1668 |
|
1669 if (error_state) |
|
1670 { |
|
1671 return octave_value(); |
|
1672 } |
|
1673 |
|
1674 check_err(nc_inq_var(ncid, varid, name, &xtype, |
|
1675 &ndims, dimids, &natts)); |
|
1676 |
|
1677 retval(0) = octave_value(std::string(name)); |
|
1678 retval(1) = octave_value(xtype); |
|
1679 |
|
1680 // copy output arguments |
|
1681 Array<double> dimids_ = Array<double>(dim_vector(1,ndims)); |
|
1682 for (int i = 0; i < ndims; i++) |
|
1683 { |
|
1684 dimids_(i) = dimids[ndims-i-1]; |
|
1685 } |
|
1686 |
|
1687 retval(2) = octave_value(dimids_); |
|
1688 retval(3) = octave_value(natts); |
|
1689 |
|
1690 return retval; |
|
1691 } |
|
1692 |
|
1693 |
|
1694 |
|
1695 DEFUN_DLD(netcdf_inqDim, args,, |
|
1696 "-*- texinfo -*-\n\ |
|
1697 @deftypefn {Loadable Function} {[@var{name},@var{length}] =} netcdf_inqDim(@var{ncid},@var{dimid}) \n\ |
|
1698 Returns the name and length of a NetCDF dimension.\n\ |
|
1699 @seealso{netcdf_inqDimID}\n\ |
|
1700 @end deftypefn") |
|
1701 { |
|
1702 |
|
1703 if (args.length() != 2) { |
|
1704 print_usage (); |
|
1705 return octave_value(); |
|
1706 } |
|
1707 |
|
1708 int ncid = args(0).scalar_value(); |
|
1709 int dimid = args(1).scalar_value(); |
|
1710 octave_value_list retval; |
|
1711 |
|
1712 if (! error_state) { |
|
1713 char name[NC_MAX_NAME+1]; |
|
1714 size_t length; |
|
1715 check_err(nc_inq_dim(ncid, dimid, name, &length)); |
|
1716 |
|
1717 retval(0) = octave_value(std::string(name)); |
|
1718 retval(1) = octave_value(length); |
|
1719 } |
|
1720 |
|
1721 return retval; |
|
1722 } |
|
1723 |
|
1724 |
|
1725 DEFUN_DLD(netcdf_inqDimID, args,, |
|
1726 "-*- texinfo -*-\n\ |
|
1727 @deftypefn {Loadable Function} {@var{dimid} =} netcdf_inqDimID(@var{ncid},@var{dimname}) \n\ |
|
1728 Return the id of a NetCDF dimension.\n\ |
|
1729 @seealso{netcdf_inqDim}\n\ |
|
1730 @end deftypefn") |
|
1731 { |
|
1732 |
|
1733 if (args.length() != 2) { |
|
1734 print_usage (); |
|
1735 return octave_value(); |
|
1736 } |
|
1737 |
|
1738 int ncid = args(0).scalar_value(); |
|
1739 std::string dimname = args(1).string_value(); |
|
1740 int id; |
|
1741 octave_value_list retval; |
|
1742 |
|
1743 if (! error_state) { |
|
1744 check_err(nc_inq_dimid(ncid, dimname.c_str(), &id)); |
|
1745 |
|
1746 retval(0) = octave_value(id); |
|
1747 } |
|
1748 |
|
1749 return retval; |
|
1750 } |
|
1751 |
|
1752 // int nc_inq_dimids(int ncid, int *ndims, int *dimids, int include_parents); |
|
1753 DEFUN_DLD(netcdf_inqDimIDs, args,, |
|
1754 "-*- texinfo -*-\n\ |
|
1755 @deftypefn {Loadable Function} {@var{dimids} =} netcdf_inqDimID(@var{ncid}) \n\ |
|
1756 @deftypefnx {Loadable Function} {@var{dimids} =} netcdf_inqDimID(@var{ncid},@var{include_parents}) \n\ |
|
1757 Return the dimension ids defined in a NetCDF file.\n\ |
|
1758 If @var{include_parents} is 1, the dimension ids of the parent group are also returned.\n\ |
|
1759 Per default this is not the case (@var{include_parents} is 0).\n\ |
|
1760 @seealso{netcdf_inqDim}\n\ |
|
1761 @end deftypefn") |
|
1762 { |
|
1763 if (args.length() != 1 && args.length() != 2) { |
|
1764 print_usage (); |
|
1765 return octave_value(); |
|
1766 } |
|
1767 |
|
1768 int ncid = args(0).scalar_value(); |
|
1769 int include_parents = 0; |
|
1770 if (args.length() == 2) { |
|
1771 include_parents = args(0).scalar_value(); |
|
1772 } |
|
1773 |
|
1774 int ndims; |
|
1775 check_err(nc_inq_ndims(ncid, &ndims)); |
|
1776 Array<int> dimids = Array<int>(dim_vector(1,ndims)); |
|
1777 check_err(nc_inq_dimids(ncid, &ndims, dimids.fortran_vec(),include_parents)); |
|
1778 |
|
1779 return octave_value(dimids); |
|
1780 } |
|
1781 |
|
1782 |
|
1783 |
|
1784 // groups |
|
1785 |
|
1786 //int nc_def_grp(int parent_ncid, const char *name, int *new_ncid); |
|
1787 |
|
1788 DEFUN_DLD(netcdf_defGrp, args,, |
|
1789 "-*- texinfo -*-\n\ |
|
1790 @deftypefn {Loadable Function} {@var{new_ncid} =} netcdf_defGrp(@var{ncid},@var{name}) \n\ |
|
1791 Define a group in a NetCDF file.\n\ |
|
1792 @seealso{netcdf_inqGrps}\n\ |
|
1793 @end deftypefn") |
|
1794 { |
|
1795 |
|
1796 if (args.length() != 2) { |
|
1797 print_usage (); |
|
1798 return octave_value(); |
|
1799 } |
|
1800 |
|
1801 int parent_ncid = args(0).scalar_value(); |
|
1802 std::string name = args(1).string_value(); |
|
1803 int new_ncid; |
|
1804 |
|
1805 check_err(nc_def_grp(parent_ncid, name.c_str(), &new_ncid)); |
|
1806 return octave_value(new_ncid); |
|
1807 } |
|
1808 |
|
1809 |
|
1810 // int nc_inq_grps(int ncid, int *numgrps, int *ncids); |
|
1811 DEFUN_DLD(netcdf_inqGrps, args,, |
|
1812 "-*- texinfo -*-\n\ |
|
1813 @deftypefn {Loadable Function} {@var{ncids} =} netcdf_inqGrps(@var{ncid}) \n\ |
|
1814 Return all groups ids in a NetCDF file.\n\ |
|
1815 @seealso{netcdf_inqGrps}\n\ |
|
1816 @end deftypefn") |
|
1817 { |
|
1818 if (args.length() != 1) { |
|
1819 print_usage (); |
|
1820 return octave_value(); |
|
1821 } |
|
1822 |
|
1823 int ncid = args(0).scalar_value(); |
|
1824 int numgrps; |
|
1825 |
|
1826 check_err(nc_inq_grps(ncid, &numgrps, NULL)); |
|
1827 Array<int> ncids = Array<int>(dim_vector(1,numgrps)); |
|
1828 check_err(nc_inq_grps(ncid, NULL, ncids.fortran_vec())); |
|
1829 |
|
1830 return octave_value(ncids); |
|
1831 } |
|
1832 |
|
1833 //int nc_inq_grpname(int ncid, char *name); |
|
1834 DEFUN_DLD(netcdf_inqGrpName, args,, |
|
1835 "-*- texinfo -*-\n\ |
|
1836 @deftypefn {Loadable Function} {@var{name} =} netcdf_inqGrpName(@var{ncid}) \n\ |
|
1837 Return group name in a NetCDF file.\n\ |
|
1838 @seealso{netcdf_inqGrps}\n\ |
|
1839 @end deftypefn") |
|
1840 { |
|
1841 if (args.length() != 1) { |
|
1842 print_usage (); |
|
1843 return octave_value(); |
|
1844 } |
|
1845 |
|
1846 int ncid = args(0).scalar_value(); |
|
1847 char name[NC_MAX_NAME+1]; |
|
1848 |
|
1849 check_err(nc_inq_grpname(ncid, name)); |
|
1850 return octave_value(std::string(name)); |
|
1851 } |
|
1852 |
|
1853 //int nc_inq_grpname_full(int ncid, size_t *lenp, char *full_name); |
|
1854 DEFUN_DLD(netcdf_inqGrpNameFull, args,, |
|
1855 "-*- texinfo -*-\n\ |
|
1856 @deftypefn {Loadable Function} {@var{name} =} netcdf_inqGrpNameFull(@var{ncid}) \n\ |
|
1857 Return full name of group in NetCDF file.\n\ |
|
1858 @seealso{netcdf_inqGrpName}\n\ |
|
1859 @end deftypefn") |
|
1860 { |
|
1861 if (args.length() != 1) { |
|
1862 print_usage (); |
|
1863 return octave_value(); |
|
1864 } |
|
1865 |
|
1866 int ncid = args(0).scalar_value(); |
|
1867 size_t len; |
|
1868 check_err(nc_inq_grpname_len(ncid,&len)); |
|
1869 char* name = new char[len+1]; |
|
1870 octave_value retval; |
|
1871 |
|
1872 check_err(nc_inq_grpname_full(ncid, &len, name)); |
|
1873 retval = octave_value(std::string(name)); |
|
1874 delete name; |
|
1875 return retval; |
|
1876 } |
|
1877 |
|
1878 // int nc_inq_grp_parent(int ncid, int *parent_ncid); |
|
1879 DEFUN_DLD(netcdf_inqGrpParent, args,, |
|
1880 "-*- texinfo -*-\n\ |
|
1881 @deftypefn {Loadable Function} {@var{parent_ncid} =} netcdf_inqGrpParent(@var{ncid}) \n\ |
|
1882 Return id of the parent group\n\ |
|
1883 @seealso{netcdf_inqGrpName}\n\ |
|
1884 @end deftypefn") |
|
1885 { |
|
1886 if (args.length() != 1) { |
|
1887 print_usage (); |
|
1888 return octave_value(); |
|
1889 } |
|
1890 |
|
1891 int ncid = args(0).scalar_value(); |
|
1892 int parent_ncid; |
|
1893 |
|
1894 check_err(nc_inq_grp_parent(ncid, &parent_ncid)); |
|
1895 return octave_value(parent_ncid); |
|
1896 } |
|
1897 |
|
1898 // int nc_inq_grp_full_ncid(int ncid, char *full_name, int *grp_ncid); |
|
1899 DEFUN_DLD(netcdf_inqGrpFullNcid, args,, |
|
1900 "-*- texinfo -*-\n\ |
|
1901 @deftypefn {Loadable Function} {@var{grp_ncid} =} netcdf_inqGrpFullNcid(@var{ncid},@var{name}) \n\ |
|
1902 Return the group id based on the full group name.\n\ |
|
1903 @seealso{netcdf_inqGrpName}\n\ |
|
1904 @end deftypefn") |
|
1905 { |
|
1906 if (args.length() != 2) { |
|
1907 print_usage (); |
|
1908 return octave_value(); |
|
1909 } |
|
1910 |
|
1911 int ncid = args(0).scalar_value(); |
|
1912 std::string name = args(1).string_value(); |
|
1913 int grp_ncid; |
|
1914 |
|
1915 int format; |
|
1916 check_err(nc_inq_format(ncid, &format)); |
|
1917 |
|
1918 if (error_state) |
|
1919 { |
|
1920 return octave_value(); |
|
1921 } |
|
1922 |
|
1923 if (format == NC_FORMAT_CLASSIC || format == NC_FORMAT_64BIT) |
|
1924 { |
|
1925 if (name == "/") |
|
1926 { |
|
1927 return octave_value(ncid); |
|
1928 } |
|
1929 else |
|
1930 { |
|
1931 error("groups are not supported in this format"); |
|
1932 return octave_value(); |
|
1933 } |
|
1934 } |
|
1935 |
|
1936 // nc_inq_grp_full_ncid makes a segmentation fault if |
|
1937 // file is in non-HDF5 format |
|
1938 check_err(nc_inq_grp_full_ncid(ncid, name.c_str(),&grp_ncid)); |
|
1939 return octave_value(grp_ncid); |
|
1940 } |
|
1941 |
|
1942 |
|
1943 |
|
1944 // int nc_inq_ncid(int ncid, const char *name, int *grp_ncid); |
|
1945 DEFUN_DLD(netcdf_inqNcid, args,, |
|
1946 "-*- texinfo -*-\n\ |
|
1947 @deftypefn {Loadable Function} {@var{grp_ncid} =} netcdf_inqNcid(@var{ncid},@var{name}) \n\ |
|
1948 Return group id based on its name\n\ |
|
1949 @seealso{netcdf_inqGrpFullNcid}\n\ |
|
1950 @end deftypefn") |
|
1951 { |
|
1952 if (args.length() != 2) { |
|
1953 print_usage (); |
|
1954 return octave_value(); |
|
1955 } |
|
1956 |
|
1957 int ncid = args(0).scalar_value(); |
|
1958 std::string name = args(1).string_value(); |
|
1959 int grp_ncid; |
|
1960 |
|
1961 check_err(nc_inq_ncid(ncid, name.c_str(), &grp_ncid)); |
|
1962 return octave_value(grp_ncid); |
|
1963 } |