NetCDF  4.6.1
dvar.c
Go to the documentation of this file.
1 /* Copyright 2010-2018 University Corporation for Atmospheric
2  Research/Unidata. See COPYRIGHT file for more info. */
8 #include "ncdispatch.h"
9 #include "netcdf_f.h"
10 
208 int
209 nc_def_var(int ncid, const char *name, nc_type xtype,
210  int ndims, const int *dimidsp, int *varidp)
211 {
212  NC* ncp;
213  int stat = NC_NOERR;
214 
215  if ((stat = NC_check_id(ncid, &ncp)))
216  return stat;
217  TRACE(nc_def_var);
218  return ncp->dispatch->def_var(ncid, name, xtype, ndims,
219  dimidsp, varidp);
220 }
288 int
289 nc_rename_var(int ncid, int varid, const char *name)
290 {
291  NC* ncp;
292  int stat = NC_check_id(ncid, &ncp);
293  if(stat != NC_NOERR) return stat;
294  TRACE(nc_rename_var);
295  return ncp->dispatch->rename_var(ncid, varid, name);
296 }
309 int
310 NC_is_recvar(int ncid, int varid, size_t* nrecs)
311 {
312  int status = NC_NOERR;
313  int unlimid;
314  int ndims;
315  int dimset[NC_MAX_VAR_DIMS];
316 
317  status = nc_inq_unlimdim(ncid,&unlimid);
318  if(status != NC_NOERR) return 0; /* no unlimited defined */
319  status = nc_inq_varndims(ncid,varid,&ndims);
320  if(status != NC_NOERR) return 0; /* no unlimited defined */
321  if(ndims == 0) return 0; /* scalar */
322  status = nc_inq_vardimid(ncid,varid,dimset);
323  if(status != NC_NOERR) return 0; /* no unlimited defined */
324  status = nc_inq_dim(ncid,dimset[0],NULL,nrecs);
325  if(status != NC_NOERR) return 0;
326  return (dimset[0] == unlimid ? 1: 0);
327 }
328 
355 int
356 NC_inq_recvar(int ncid, int varid, int* nrecdimsp, int *is_recdim)
357 {
358  int status = NC_NOERR;
359  int unlimid;
360  int nvardims;
361  int dimset[NC_MAX_VAR_DIMS];
362  int dim;
363  int nrecdims = 0;
364 
365  status = nc_inq_varndims(ncid,varid,&nvardims);
366  if(status != NC_NOERR) return status;
367  if(nvardims == 0) return NC_NOERR; /* scalars have no dims */
368  for(dim = 0; dim < nvardims; dim++)
369  is_recdim[dim] = 0;
370  status = nc_inq_unlimdim(ncid, &unlimid);
371  if(status != NC_NOERR) return status;
372  if(unlimid == -1) return status; /* no unlimited dims for any variables */
373 #ifdef USE_NETCDF4
374  {
375  int nunlimdims;
376  int *unlimids;
377  int recdim;
378  status = nc_inq_unlimdims(ncid, &nunlimdims, NULL); /* for group or file, not variable */
379  if(status != NC_NOERR) return status;
380  if(nunlimdims == 0) return status;
381 
382  if (!(unlimids = malloc(nunlimdims * sizeof(int))))
383  return NC_ENOMEM;
384  status = nc_inq_unlimdims(ncid, &nunlimdims, unlimids); /* for group or file, not variable */
385  if(status != NC_NOERR) {
386  free(unlimids);
387  return status;
388  }
389  status = nc_inq_vardimid(ncid, varid, dimset);
390  if(status != NC_NOERR) {
391  free(unlimids);
392  return status;
393  }
394  for (dim = 0; dim < nvardims; dim++) { /* netCDF-4 rec dims need not be first dim for a rec var */
395  for(recdim = 0; recdim < nunlimdims; recdim++) {
396  if(dimset[dim] == unlimids[recdim]) {
397  is_recdim[dim] = 1;
398  nrecdims++;
399  }
400  }
401  }
402  free(unlimids);
403  }
404 #else
405  status = nc_inq_vardimid(ncid, varid, dimset);
406  if(status != NC_NOERR) return status;
407  if(dimset[0] == unlimid) {
408  is_recdim[0] = 1;
409  nrecdims++;
410  }
411 #endif /* USE_NETCDF4 */
412  if(nrecdimsp) *nrecdimsp = nrecdims;
413  return status;
414 }
415 
416 /* Ok to use NC pointers because
417  all IOSP's will use that structure,
418  but not ok to use e.g. NC_Var pointers
419  because they may be different structure
420  entirely.
421 */
422 
434 int
435 nctypelen(nc_type type)
436 {
437  switch(type){
438  case NC_CHAR :
439  return ((int)sizeof(char));
440  case NC_BYTE :
441  return ((int)sizeof(signed char));
442  case NC_SHORT :
443  return ((int)sizeof(short));
444  case NC_INT :
445  return ((int)sizeof(int));
446  case NC_FLOAT :
447  return ((int)sizeof(float));
448  case NC_DOUBLE :
449  return ((int)sizeof(double));
450 
451  /* These can occur in netcdf-3 code */
452  case NC_UBYTE :
453  return ((int)sizeof(unsigned char));
454  case NC_USHORT :
455  return ((int)(sizeof(unsigned short)));
456  case NC_UINT :
457  return ((int)sizeof(unsigned int));
458  case NC_INT64 :
459  return ((int)sizeof(signed long long));
460  case NC_UINT64 :
461  return ((int)sizeof(unsigned long long));
462 #ifdef USE_NETCDF4
463  case NC_STRING :
464  return ((int)sizeof(char*));
465 #endif /*USE_NETCDF4*/
466 
467  default:
468  return -1;
469  }
470 }
471 
475 size_t
476 NC_atomictypelen(nc_type xtype)
477 {
478  size_t sz = 0;
479  switch(xtype) {
480  case NC_NAT: sz = 0; break;
481  case NC_BYTE: sz = sizeof(signed char); break;
482  case NC_CHAR: sz = sizeof(char); break;
483  case NC_SHORT: sz = sizeof(short); break;
484  case NC_INT: sz = sizeof(int); break;
485  case NC_FLOAT: sz = sizeof(float); break;
486  case NC_DOUBLE: sz = sizeof(double); break;
487  case NC_INT64: sz = sizeof(signed long long); break;
488  case NC_UBYTE: sz = sizeof(unsigned char); break;
489  case NC_USHORT: sz = sizeof(unsigned short); break;
490  case NC_UINT: sz = sizeof(unsigned int); break;
491  case NC_UINT64: sz = sizeof(unsigned long long); break;
492 #ifdef USE_NETCDF4
493  case NC_STRING: sz = sizeof(char*); break;
494 #endif
495  default: break;
496  }
497  return sz;
498 }
499 
503 char *
504 NC_atomictypename(nc_type xtype)
505 {
506  char* nm = NULL;
507  switch(xtype) {
508  case NC_NAT: nm = "undefined"; break;
509  case NC_BYTE: nm = "byte"; break;
510  case NC_CHAR: nm = "char"; break;
511  case NC_SHORT: nm = "short"; break;
512  case NC_INT: nm = "int"; break;
513  case NC_FLOAT: nm = "float"; break;
514  case NC_DOUBLE: nm = "double"; break;
515  case NC_INT64: nm = "int64"; break;
516  case NC_UBYTE: nm = "ubyte"; break;
517  case NC_USHORT: nm = "ushort"; break;
518  case NC_UINT: nm = "uint"; break;
519  case NC_UINT64: nm = "uint64"; break;
520 #ifdef USE_NETCDF4
521  case NC_STRING: nm = "string"; break;
522 #endif
523  default: break;
524  }
525  return nm;
526 }
527 
532 int
533 NC_getshape(int ncid, int varid, int ndims, size_t* shape)
534 {
535  int dimids[NC_MAX_VAR_DIMS];
536  int i;
537  int status = NC_NOERR;
538 
539  if ((status = nc_inq_vardimid(ncid, varid, dimids)))
540  return status;
541  for(i = 0; i < ndims; i++)
542  if ((status = nc_inq_dimlen(ncid, dimids[i], &shape[i])))
543  break;
544 
545  return status;
546 }
547 
612 int
613 nc_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value)
614 {
615  NC* ncp;
616  int stat = NC_check_id(ncid,&ncp);
617  if(stat != NC_NOERR) return stat;
618 
619  /* Dennis Heimbigner: (Using NC_GLOBAL is ilegal, as this API) has no
620  * provision for specifying the type of the fillvalue, it must of necessity
621  * be using the type of the variable to interpret the bytes of the
622  * fill_value argument.
623  */
624  if (varid == NC_GLOBAL) return NC_EGLOBAL;
625 
626  return ncp->dispatch->def_var_fill(ncid,varid,no_fill,fill_value);
627 }
628 
629 #ifdef USE_NETCDF4
630 
689 int
690 nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems,
691  float preemption)
692 {
693  NC* ncp;
694  int stat = NC_check_id(ncid, &ncp);
695  if(stat != NC_NOERR) return stat;
696  return ncp->dispatch->set_var_chunk_cache(ncid, varid, size,
697  nelems, preemption);
698 }
699 
730 int
731 nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp,
732  float *preemptionp)
733 {
734  NC* ncp;
735  int stat = NC_check_id(ncid, &ncp);
736  if(stat != NC_NOERR) return stat;
737  return ncp->dispatch->get_var_chunk_cache(ncid, varid, sizep,
738  nelemsp, preemptionp);
739 }
740 
754 int
755 nc_free_string(size_t len, char **data)
756 {
757  int i;
758  for (i = 0; i < len; i++)
759  free(data[i]);
760  return NC_NOERR;
761 }
762 
856 int
857 nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
858 {
859  NC* ncp;
860  int stat = NC_check_id(ncid,&ncp);
861  if(stat != NC_NOERR) return stat;
862  return ncp->dispatch->def_var_deflate(ncid,varid,shuffle,deflate,deflate_level);
863 }
864 
896 int
897 nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
898 {
899  NC* ncp;
900  int stat = NC_check_id(ncid,&ncp);
901  if(stat != NC_NOERR) return stat;
902  return ncp->dispatch->def_var_fletcher32(ncid,varid,fletcher32);
903 }
904 
997 int
998 nc_def_var_chunking(int ncid, int varid, int storage,
999  const size_t *chunksizesp)
1000 {
1001  NC* ncp;
1002  int stat = NC_check_id(ncid, &ncp);
1003  if(stat != NC_NOERR) return stat;
1004  return ncp->dispatch->def_var_chunking(ncid, varid, storage,
1005  chunksizesp);
1006 }
1007 
1075 int
1076 nc_def_var_endian(int ncid, int varid, int endian)
1077 {
1078  NC* ncp;
1079  int stat = NC_check_id(ncid,&ncp);
1080  if(stat != NC_NOERR) return stat;
1081  return ncp->dispatch->def_var_endian(ncid,varid,endian);
1082 }
1083 
1097 int
1098 nc_def_var_filter(int ncid, int varid, unsigned int id, size_t nparams, const unsigned int* parms)
1099 {
1100  NC* ncp;
1101  int stat = NC_check_id(ncid,&ncp);
1102  if(stat != NC_NOERR) return stat;
1103  return ncp->dispatch->def_var_filter(ncid,varid,id,nparams,parms);
1104 }
1105 
1106 #endif /* USE_NETCDF4 */
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition: netcdf.h:395
#define NC_CHAR
ISO/ASCII character.
Definition: netcdf.h:35
int nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp, float *preemptionp)
Get the per-variable chunk cache settings from the HDF5 layer.
Definition: dvar.c:731
EXTERNL int nc_inq_vardimid(int ncid, int varid, int *dimidsp)
Learn the dimension IDs associated with a variable.
Definition: dvarinq.c:225
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:209
#define NC_UBYTE
unsigned 1 byte int
Definition: netcdf.h:41
int nc_def_var_filter(int ncid, int varid, unsigned int id, size_t nparams, const unsigned int *parms)
Define a new variable filter.
Definition: dvar.c:1098
#define NC_MAX_VAR_DIMS
max per variable dimensions
Definition: netcdf.h:266
#define NC_UINT
unsigned 4-byte int
Definition: netcdf.h:43
int nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
Set the compression settings for a netCDF-4/HDF5 variable.
Definition: dvar.c:857
#define NC_INT64
signed 8-byte int
Definition: netcdf.h:44
#define NC_STRING
string
Definition: netcdf.h:46
#define NC_DOUBLE
double precision floating point number
Definition: netcdf.h:40
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:202
int nc_type
The nc_type type is just an int.
Definition: netcdf.h:24
#define NC_BYTE
signed 1 byte integer
Definition: netcdf.h:34
int nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems, float preemption)
Change the cache settings for a chunked variable.
Definition: dvar.c:690
int nc_rename_var(int ncid, int varid, const char *name)
Rename a variable.
Definition: dvar.c:289
EXTERNL int nc_inq_dimlen(int ncid, int dimid, size_t *lenp)
Find the length of a dimension.
Definition: ddim.c:459
#define NC_EGLOBAL
Action prohibited on NC_GLOBAL varid.
Definition: netcdf.h:370
#define NC_INT
signed 4 byte integer
Definition: netcdf.h:37
int nc_def_var_chunking(int ncid, int varid, int storage, const size_t *chunksizesp)
Define chunking parameters for a variable.
Definition: dvar.c:998
#define NC_NAT
Not A Type.
Definition: netcdf.h:33
int nc_free_string(size_t len, char **data)
Free string space allocated by the library.
Definition: dvar.c:755
#define NC_USHORT
unsigned 2-byte int
Definition: netcdf.h:42
int nc_def_var_endian(int ncid, int varid, int endian)
Define endianness of a variable.
Definition: dvar.c:1076
EXTERNL int nc_inq_unlimdim(int ncid, int *unlimdimidp)
Find the ID of the unlimited dimension.
Definition: ddim.c:343
#define NC_SHORT
signed 2 byte integer
Definition: netcdf.h:36
EXTERNL int nc_inq_unlimdims(int ncid, int *nunlimdimsp, int *unlimdimidsp)
Return number and list of unlimited dimensions.
Definition: dvarinq.c:594
#define NC_NOERR
No Error.
Definition: netcdf.h:315
int nc_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value)
Set the fill value for a variable.
Definition: dvar.c:613
#define NC_GLOBAL
Attribute id to put/get a global attribute.
Definition: netcdf.h:238
#define NC_FLOAT
single precision floating point number
Definition: netcdf.h:39
#define NC_UINT64
unsigned 8-byte int
Definition: netcdf.h:45
int nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
Set checksum for a var.
Definition: dvar.c:897

Return to the Main Unidata NetCDF page.
Generated on Tue Mar 20 2018 06:30:46 for NetCDF. NetCDF is a Unidata library.