NetCDF  4.4.0
dvar.c
Go to the documentation of this file.
1 
8 #include "ncdispatch.h"
9 #include "netcdf_f.h"
10 
206 int
207 nc_def_var(int ncid, const char *name, nc_type xtype,
208  int ndims, const int *dimidsp, int *varidp)
209 {
210  NC* ncp;
211  int stat = NC_NOERR;
212 
213  if ((stat = NC_check_id(ncid, &ncp)))
214  return stat;
215  return ncp->dispatch->def_var(ncid, name, xtype, ndims,
216  dimidsp, varidp);
217 }
279 int
280 nc_rename_var(int ncid, int varid, const char *name)
281 {
282  NC* ncp;
283  int stat = NC_check_id(ncid, &ncp);
284  if(stat != NC_NOERR) return stat;
285  return ncp->dispatch->rename_var(ncid, varid, name);
286 }
292 int
293 NC_is_recvar(int ncid, int varid, size_t* nrecs)
294 {
295  int status = NC_NOERR;
296  int unlimid;
297  int ndims;
298  int dimset[NC_MAX_VAR_DIMS];
299 
300  status = nc_inq_unlimdim(ncid,&unlimid);
301  if(status != NC_NOERR) return 0; /* no unlimited defined */
302  status = nc_inq_varndims(ncid,varid,&ndims);
303  if(status != NC_NOERR) return 0; /* no unlimited defined */
304  if(ndims == 0) return 0; /* scalar */
305  status = nc_inq_vardimid(ncid,varid,dimset);
306  if(status != NC_NOERR) return 0; /* no unlimited defined */
307  status = nc_inq_dim(ncid,dimset[0],NULL,nrecs);
308  if(status != NC_NOERR) return 0;
309  return (dimset[0] == unlimid ? 1: 0);
310 }
311 
328 int
329 NC_inq_recvar(int ncid, int varid, int* nrecdimsp, int *is_recdim)
330 {
331  int status = NC_NOERR;
332  int unlimid;
333  int nvardims;
334  int dimset[NC_MAX_VAR_DIMS];
335  int dim;
336  int nrecdims = 0;
337 
338  status = nc_inq_varndims(ncid,varid,&nvardims);
339  if(status != NC_NOERR) return status;
340  if(nvardims == 0) return NC_NOERR; /* scalars have no dims */
341  for(dim = 0; dim < nvardims; dim++)
342  is_recdim[dim] = 0;
343  status = nc_inq_unlimdim(ncid, &unlimid);
344  if(status != NC_NOERR) return status;
345  if(unlimid == -1) return status; /* no unlimited dims for any variables */
346 #ifdef USE_NETCDF4
347  {
348  int nunlimdims;
349  int *unlimids;
350  int recdim;
351  status = nc_inq_unlimdims(ncid, &nunlimdims, NULL); /* for group or file, not variable */
352  if(status != NC_NOERR) return status;
353  if(nunlimdims == 0) return status;
354 
355  if (!(unlimids = malloc(nunlimdims * sizeof(int))))
356  return NC_ENOMEM;
357  status = nc_inq_unlimdims(ncid, &nunlimdims, unlimids); /* for group or file, not variable */
358  if(status != NC_NOERR) {
359  free(unlimids);
360  return status;
361  }
362  status = nc_inq_vardimid(ncid, varid, dimset);
363  if(status != NC_NOERR) {
364  free(unlimids);
365  return status;
366  }
367  for (dim = 0; dim < nvardims; dim++) { /* netCDF-4 rec dims need not be first dim for a rec var */
368  for(recdim = 0; recdim < nunlimdims; recdim++) {
369  if(dimset[dim] == unlimids[recdim]) {
370  is_recdim[dim] = 1;
371  nrecdims++;
372  }
373  }
374  }
375  free(unlimids);
376  }
377 #else
378  status = nc_inq_vardimid(ncid, varid, dimset);
379  if(status != NC_NOERR) return status;
380  if(dimset[0] == unlimid) {
381  is_recdim[0] = 1;
382  nrecdims++;
383  }
384 #endif /* USE_NETCDF4 */
385  if(nrecdimsp) *nrecdimsp = nrecdims;
386  return status;
387 }
388 
389 /* Ok to use NC pointers because
390  all IOSP's will use that structure,
391  but not ok to use e.g. NC_Var pointers
392  because they may be different structure
393  entirely.
394 */
395 
404 int
405 nctypelen(nc_type type)
406 {
407  switch(type){
408  case NC_CHAR :
409  return ((int)sizeof(char));
410  case NC_BYTE :
411  return ((int)sizeof(signed char));
412  case NC_SHORT :
413  return ((int)sizeof(short));
414  case NC_INT :
415  return ((int)sizeof(int));
416  case NC_FLOAT :
417  return ((int)sizeof(float));
418  case NC_DOUBLE :
419  return ((int)sizeof(double));
420 
421  /* These can occur in netcdf-3 code */
422  case NC_UBYTE :
423  return ((int)sizeof(unsigned char));
424  case NC_USHORT :
425  return ((int)(sizeof(unsigned short)));
426  case NC_UINT :
427  return ((int)sizeof(unsigned int));
428  case NC_INT64 :
429  return ((int)sizeof(signed long long));
430  case NC_UINT64 :
431  return ((int)sizeof(unsigned long long));
432 #ifdef USE_NETCDF4
433  case NC_STRING :
434  return ((int)sizeof(char*));
435 #endif /*USE_NETCDF4*/
436 
437  default:
438  return -1;
439  }
440 }
441 
445 size_t
446 NC_atomictypelen(nc_type xtype)
447 {
448  size_t sz = 0;
449  switch(xtype) {
450  case NC_NAT: sz = 0; break;
451  case NC_BYTE: sz = sizeof(signed char); break;
452  case NC_CHAR: sz = sizeof(char); break;
453  case NC_SHORT: sz = sizeof(short); break;
454  case NC_INT: sz = sizeof(int); break;
455  case NC_FLOAT: sz = sizeof(float); break;
456  case NC_DOUBLE: sz = sizeof(double); break;
457  case NC_INT64: sz = sizeof(signed long long); break;
458  case NC_UBYTE: sz = sizeof(unsigned char); break;
459  case NC_USHORT: sz = sizeof(unsigned short); break;
460  case NC_UINT: sz = sizeof(unsigned int); break;
461  case NC_UINT64: sz = sizeof(unsigned long long); break;
462 #ifdef USE_NETCDF4
463  case NC_STRING: sz = sizeof(char*); break;
464 #endif
465  default: break;
466  }
467  return sz;
468 }
469 
473 char *
474 NC_atomictypename(nc_type xtype)
475 {
476  char* nm = NULL;
477  switch(xtype) {
478  case NC_NAT: nm = "undefined"; break;
479  case NC_BYTE: nm = "byte"; break;
480  case NC_CHAR: nm = "char"; break;
481  case NC_SHORT: nm = "short"; break;
482  case NC_INT: nm = "int"; break;
483  case NC_FLOAT: nm = "float"; break;
484  case NC_DOUBLE: nm = "double"; break;
485  case NC_INT64: nm = "int64"; break;
486  case NC_UBYTE: nm = "ubyte"; break;
487  case NC_USHORT: nm = "ushort"; break;
488  case NC_UINT: nm = "uint"; break;
489  case NC_UINT64: nm = "uint64"; break;
490 #ifdef USE_NETCDF4
491  case NC_STRING: nm = "string"; break;
492 #endif
493  default: break;
494  }
495  return nm;
496 }
497 
502 int
503 NC_getshape(int ncid, int varid, int ndims, size_t* shape)
504 {
505  int dimids[NC_MAX_VAR_DIMS];
506  int i;
507  int status = NC_NOERR;
508 
509  if ((status = nc_inq_vardimid(ncid, varid, dimids)))
510  return status;
511  for(i = 0; i < ndims; i++)
512  if ((status = nc_inq_dimlen(ncid, dimids[i], &shape[i])))
513  break;
514 
515  return status;
516 }
517 
518 #ifdef USE_NETCDF4
519 
544 int
545 nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems,
546  float preemption)
547 {
548  NC* ncp;
549  int stat = NC_check_id(ncid, &ncp);
550  if(stat != NC_NOERR) return stat;
551  return ncp->dispatch->set_var_chunk_cache(ncid, varid, size,
552  nelems, preemption);
553 }
554 
582 int
583 nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp,
584  float *preemptionp)
585 {
586  NC* ncp;
587  int stat = NC_check_id(ncid, &ncp);
588  if(stat != NC_NOERR) return stat;
589  return ncp->dispatch->get_var_chunk_cache(ncid, varid, sizep,
590  nelemsp, preemptionp);
591 }
592 
606 int
607 nc_free_string(size_t len, char **data)
608 {
609  int i;
610  for (i = 0; i < len; i++)
611  free(data[i]);
612  return NC_NOERR;
613 }
614 
615 int
616 nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
617 {
618  NC* ncp;
619  int stat = NC_check_id(ncid,&ncp);
620  if(stat != NC_NOERR) return stat;
621  return ncp->dispatch->def_var_deflate(ncid,varid,shuffle,deflate,deflate_level);
622 }
623 
624 int
625 nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
626 {
627  NC* ncp;
628  int stat = NC_check_id(ncid,&ncp);
629  if(stat != NC_NOERR) return stat;
630  return ncp->dispatch->def_var_fletcher32(ncid,varid,fletcher32);
631 }
632 
633 int
634 nc_def_var_chunking(int ncid, int varid, int storage,
635  const size_t *chunksizesp)
636 {
637  NC* ncp;
638  int stat = NC_check_id(ncid, &ncp);
639  if(stat != NC_NOERR) return stat;
640  return ncp->dispatch->def_var_chunking(ncid, varid, storage,
641  chunksizesp);
642 }
643 
644 int
645 nc_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value)
646 {
647  NC* ncp;
648  int stat = NC_check_id(ncid,&ncp);
649  if(stat != NC_NOERR) return stat;
650  return ncp->dispatch->def_var_fill(ncid,varid,no_fill,fill_value);
651 }
652 
653 int
654 nc_def_var_endian(int ncid, int varid, int endian)
655 {
656  NC* ncp;
657  int stat = NC_check_id(ncid,&ncp);
658  if(stat != NC_NOERR) return stat;
659  return ncp->dispatch->def_var_endian(ncid,varid,endian);
660 }
661 
662 #endif /* USE_NETCDF4 */
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition: netcdf.h:395
#define NC_CHAR
ISO/ASCII character.
Definition: netcdf.h:39
int nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp, float *preemptionp)
Definition: dvar.c:583
EXTERNL int nc_inq_vardimid(int ncid, int varid, int *dimidsp)
Learn the dimension IDs associated with a variable.
Definition: dvarinq.c:213
int nc_def_var(int ncid, const char *name, nc_type xtype, int ndims, const int *dimidsp, int *varidp)
Define a new variable.
Definition: dvar.c:207
#define NC_UBYTE
unsigned 1 byte int
Definition: netcdf.h:45
#define NC_MAX_VAR_DIMS
max per variable dimensions
Definition: netcdf.h:269
#define NC_UINT
unsigned 4-byte int
Definition: netcdf.h:47
#define NC_INT64
signed 8-byte int
Definition: netcdf.h:48
#define NC_STRING
string
Definition: netcdf.h:50
#define NC_DOUBLE
double precision floating point number
Definition: netcdf.h:44
EXTERNL int nc_inq_dim(int ncid, int dimid, char *name, size_t *lenp)
Find the name and length of a dimension.
Definition: ddim.c:215
EXTERNL int nc_inq_varndims(int ncid, int varid, int *ndimsp)
Learn how many dimensions are associated with a variable.
Definition: dvarinq.c:191
int nc_type
The nc_type type is just an int.
Definition: netcdf.h:28
#define NC_BYTE
signed 1 byte integer
Definition: netcdf.h:38
int nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems, float preemption)
Definition: dvar.c:545
int nc_rename_var(int ncid, int varid, const char *name)
Rename a variable.
Definition: dvar.c:280
EXTERNL int nc_inq_dimlen(int ncid, int dimid, size_t *lenp)
Find the length of a dimension.
Definition: ddim.c:450
#define NC_INT
signed 4 byte integer
Definition: netcdf.h:41
#define NC_NAT
Not A Type.
Definition: netcdf.h:37
int nc_free_string(size_t len, char **data)
Free string space allocated by the library.
Definition: dvar.c:607
#define NC_USHORT
unsigned 2-byte int
Definition: netcdf.h:46
EXTERNL int nc_inq_unlimdim(int ncid, int *unlimdimidp)
Find the ID of the unlimited dimension.
Definition: ddim.c:336
#define NC_SHORT
signed 2 byte integer
Definition: netcdf.h:40
EXTERNL int nc_inq_unlimdims(int ncid, int *nunlimdimsp, int *unlimdimidsp)
Return number and list of unlimited dimensions.
Definition: dvarinq.c:555
#define NC_NOERR
No Error.
Definition: netcdf.h:315
#define NC_FLOAT
single precision floating point number
Definition: netcdf.h:43
#define NC_UINT64
unsigned 8-byte int
Definition: netcdf.h:49

Return to the Main Unidata NetCDF page.
Generated on Sun Mar 27 2016 11:43:30 for NetCDF. NetCDF is a Unidata library.