NetCDF  4.4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
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  TRACE(nc_def_var);
216  return ncp->dispatch->def_var(ncid, name, xtype, ndims,
217  dimidsp, varidp);
218 }
280 int
281 nc_rename_var(int ncid, int varid, const char *name)
282 {
283  NC* ncp;
284  int stat = NC_check_id(ncid, &ncp);
285  if(stat != NC_NOERR) return stat;
286  TRACE(nc_rename_var);
287  return ncp->dispatch->rename_var(ncid, varid, name);
288 }
294 int
295 NC_is_recvar(int ncid, int varid, size_t* nrecs)
296 {
297  int status = NC_NOERR;
298  int unlimid;
299  int ndims;
300  int dimset[NC_MAX_VAR_DIMS];
301 
302  status = nc_inq_unlimdim(ncid,&unlimid);
303  if(status != NC_NOERR) return 0; /* no unlimited defined */
304  status = nc_inq_varndims(ncid,varid,&ndims);
305  if(status != NC_NOERR) return 0; /* no unlimited defined */
306  if(ndims == 0) return 0; /* scalar */
307  status = nc_inq_vardimid(ncid,varid,dimset);
308  if(status != NC_NOERR) return 0; /* no unlimited defined */
309  status = nc_inq_dim(ncid,dimset[0],NULL,nrecs);
310  if(status != NC_NOERR) return 0;
311  return (dimset[0] == unlimid ? 1: 0);
312 }
313 
330 int
331 NC_inq_recvar(int ncid, int varid, int* nrecdimsp, int *is_recdim)
332 {
333  int status = NC_NOERR;
334  int unlimid;
335  int nvardims;
336  int dimset[NC_MAX_VAR_DIMS];
337  int dim;
338  int nrecdims = 0;
339 
340  status = nc_inq_varndims(ncid,varid,&nvardims);
341  if(status != NC_NOERR) return status;
342  if(nvardims == 0) return NC_NOERR; /* scalars have no dims */
343  for(dim = 0; dim < nvardims; dim++)
344  is_recdim[dim] = 0;
345  status = nc_inq_unlimdim(ncid, &unlimid);
346  if(status != NC_NOERR) return status;
347  if(unlimid == -1) return status; /* no unlimited dims for any variables */
348 #ifdef USE_NETCDF4
349  {
350  int nunlimdims;
351  int *unlimids;
352  int recdim;
353  status = nc_inq_unlimdims(ncid, &nunlimdims, NULL); /* for group or file, not variable */
354  if(status != NC_NOERR) return status;
355  if(nunlimdims == 0) return status;
356 
357  if (!(unlimids = malloc(nunlimdims * sizeof(int))))
358  return NC_ENOMEM;
359  status = nc_inq_unlimdims(ncid, &nunlimdims, unlimids); /* for group or file, not variable */
360  if(status != NC_NOERR) {
361  free(unlimids);
362  return status;
363  }
364  status = nc_inq_vardimid(ncid, varid, dimset);
365  if(status != NC_NOERR) {
366  free(unlimids);
367  return status;
368  }
369  for (dim = 0; dim < nvardims; dim++) { /* netCDF-4 rec dims need not be first dim for a rec var */
370  for(recdim = 0; recdim < nunlimdims; recdim++) {
371  if(dimset[dim] == unlimids[recdim]) {
372  is_recdim[dim] = 1;
373  nrecdims++;
374  }
375  }
376  }
377  free(unlimids);
378  }
379 #else
380  status = nc_inq_vardimid(ncid, varid, dimset);
381  if(status != NC_NOERR) return status;
382  if(dimset[0] == unlimid) {
383  is_recdim[0] = 1;
384  nrecdims++;
385  }
386 #endif /* USE_NETCDF4 */
387  if(nrecdimsp) *nrecdimsp = nrecdims;
388  return status;
389 }
390 
391 /* Ok to use NC pointers because
392  all IOSP's will use that structure,
393  but not ok to use e.g. NC_Var pointers
394  because they may be different structure
395  entirely.
396 */
397 
406 int
407 nctypelen(nc_type type)
408 {
409  switch(type){
410  case NC_CHAR :
411  return ((int)sizeof(char));
412  case NC_BYTE :
413  return ((int)sizeof(signed char));
414  case NC_SHORT :
415  return ((int)sizeof(short));
416  case NC_INT :
417  return ((int)sizeof(int));
418  case NC_FLOAT :
419  return ((int)sizeof(float));
420  case NC_DOUBLE :
421  return ((int)sizeof(double));
422 
423  /* These can occur in netcdf-3 code */
424  case NC_UBYTE :
425  return ((int)sizeof(unsigned char));
426  case NC_USHORT :
427  return ((int)(sizeof(unsigned short)));
428  case NC_UINT :
429  return ((int)sizeof(unsigned int));
430  case NC_INT64 :
431  return ((int)sizeof(signed long long));
432  case NC_UINT64 :
433  return ((int)sizeof(unsigned long long));
434 #ifdef USE_NETCDF4
435  case NC_STRING :
436  return ((int)sizeof(char*));
437 #endif /*USE_NETCDF4*/
438 
439  default:
440  return -1;
441  }
442 }
443 
447 size_t
448 NC_atomictypelen(nc_type xtype)
449 {
450  size_t sz = 0;
451  switch(xtype) {
452  case NC_NAT: sz = 0; break;
453  case NC_BYTE: sz = sizeof(signed char); break;
454  case NC_CHAR: sz = sizeof(char); break;
455  case NC_SHORT: sz = sizeof(short); break;
456  case NC_INT: sz = sizeof(int); break;
457  case NC_FLOAT: sz = sizeof(float); break;
458  case NC_DOUBLE: sz = sizeof(double); break;
459  case NC_INT64: sz = sizeof(signed long long); break;
460  case NC_UBYTE: sz = sizeof(unsigned char); break;
461  case NC_USHORT: sz = sizeof(unsigned short); break;
462  case NC_UINT: sz = sizeof(unsigned int); break;
463  case NC_UINT64: sz = sizeof(unsigned long long); break;
464 #ifdef USE_NETCDF4
465  case NC_STRING: sz = sizeof(char*); break;
466 #endif
467  default: break;
468  }
469  return sz;
470 }
471 
475 char *
476 NC_atomictypename(nc_type xtype)
477 {
478  char* nm = NULL;
479  switch(xtype) {
480  case NC_NAT: nm = "undefined"; break;
481  case NC_BYTE: nm = "byte"; break;
482  case NC_CHAR: nm = "char"; break;
483  case NC_SHORT: nm = "short"; break;
484  case NC_INT: nm = "int"; break;
485  case NC_FLOAT: nm = "float"; break;
486  case NC_DOUBLE: nm = "double"; break;
487  case NC_INT64: nm = "int64"; break;
488  case NC_UBYTE: nm = "ubyte"; break;
489  case NC_USHORT: nm = "ushort"; break;
490  case NC_UINT: nm = "uint"; break;
491  case NC_UINT64: nm = "uint64"; break;
492 #ifdef USE_NETCDF4
493  case NC_STRING: nm = "string"; break;
494 #endif
495  default: break;
496  }
497  return nm;
498 }
499 
504 int
505 NC_getshape(int ncid, int varid, int ndims, size_t* shape)
506 {
507  int dimids[NC_MAX_VAR_DIMS];
508  int i;
509  int status = NC_NOERR;
510 
511  if ((status = nc_inq_vardimid(ncid, varid, dimids)))
512  return status;
513  for(i = 0; i < ndims; i++)
514  if ((status = nc_inq_dimlen(ncid, dimids[i], &shape[i])))
515  break;
516 
517  return status;
518 }
519 
520 #ifdef USE_NETCDF4
521 
546 int
547 nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems,
548  float preemption)
549 {
550  NC* ncp;
551  int stat = NC_check_id(ncid, &ncp);
552  if(stat != NC_NOERR) return stat;
553  return ncp->dispatch->set_var_chunk_cache(ncid, varid, size,
554  nelems, preemption);
555 }
556 
584 int
585 nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp,
586  float *preemptionp)
587 {
588  NC* ncp;
589  int stat = NC_check_id(ncid, &ncp);
590  if(stat != NC_NOERR) return stat;
591  return ncp->dispatch->get_var_chunk_cache(ncid, varid, sizep,
592  nelemsp, preemptionp);
593 }
594 
608 int
609 nc_free_string(size_t len, char **data)
610 {
611  int i;
612  for (i = 0; i < len; i++)
613  free(data[i]);
614  return NC_NOERR;
615 }
616 
617 int
618 nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
619 {
620  NC* ncp;
621  int stat = NC_check_id(ncid,&ncp);
622  if(stat != NC_NOERR) return stat;
623  return ncp->dispatch->def_var_deflate(ncid,varid,shuffle,deflate,deflate_level);
624 }
625 
626 int
627 nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
628 {
629  NC* ncp;
630  int stat = NC_check_id(ncid,&ncp);
631  if(stat != NC_NOERR) return stat;
632  return ncp->dispatch->def_var_fletcher32(ncid,varid,fletcher32);
633 }
634 
701 int
702 nc_def_var_chunking(int ncid, int varid, int storage,
703  const size_t *chunksizesp)
704 {
705  NC* ncp;
706  int stat = NC_check_id(ncid, &ncp);
707  if(stat != NC_NOERR) return stat;
708  return ncp->dispatch->def_var_chunking(ncid, varid, storage,
709  chunksizesp);
710 }
711 
712 int
713 nc_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value)
714 {
715  NC* ncp;
716  int stat = NC_check_id(ncid,&ncp);
717  if(stat != NC_NOERR) return stat;
718  return ncp->dispatch->def_var_fill(ncid,varid,no_fill,fill_value);
719 }
720 
721 int
722 nc_def_var_endian(int ncid, int varid, int endian)
723 {
724  NC* ncp;
725  int stat = NC_check_id(ncid,&ncp);
726  if(stat != NC_NOERR) return stat;
727  return ncp->dispatch->def_var_endian(ncid,varid,endian);
728 }
729 
730 #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:585
EXTERNL int nc_inq_vardimid(int ncid, int varid, int *dimidsp)
Learn the dimension IDs associated with a variable.
Definition: dvarinq.c:214
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:218
EXTERNL int nc_inq_varndims(int ncid, int varid, int *ndimsp)
Learn how many dimensions are associated with a variable.
Definition: dvarinq.c:192
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:547
int nc_rename_var(int ncid, int varid, const char *name)
Rename a variable.
Definition: dvar.c:281
EXTERNL int nc_inq_dimlen(int ncid, int dimid, size_t *lenp)
Find the length of a dimension.
Definition: ddim.c:458
#define NC_INT
signed 4 byte integer
Definition: netcdf.h:41
int nc_def_var_chunking(int ncid, int varid, int storage, const size_t *chunksizesp)
Define chunking parameters for a variable.
Definition: dvar.c:702
#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:609
#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:342
#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:600
#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 Fri Dec 16 2016 12:39:53 for NetCDF. NetCDF is a Unidata library.