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