NetCDF  4.6.2
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 
209 int
210 nc_def_var(int ncid, const char *name, nc_type xtype,
211  int ndims, const int *dimidsp, int *varidp)
212 {
213  NC* ncp;
214  int stat = NC_NOERR;
215 
216  if ((stat = NC_check_id(ncid, &ncp)))
217  return stat;
218  TRACE(nc_def_var);
219  return ncp->dispatch->def_var(ncid, name, xtype, ndims,
220  dimidsp, varidp);
221 }
289 int
290 nc_rename_var(int ncid, int varid, const char *name)
291 {
292  NC* ncp;
293  int stat = NC_check_id(ncid, &ncp);
294  if(stat != NC_NOERR) return stat;
295  TRACE(nc_rename_var);
296  return ncp->dispatch->rename_var(ncid, varid, name);
297 }
310 int
311 NC_is_recvar(int ncid, int varid, size_t* nrecs)
312 {
313  int status = NC_NOERR;
314  int unlimid;
315  int ndims;
316  int dimset[NC_MAX_VAR_DIMS];
317 
318  status = nc_inq_unlimdim(ncid,&unlimid);
319  if(status != NC_NOERR) return 0; /* no unlimited defined */
320  status = nc_inq_varndims(ncid,varid,&ndims);
321  if(status != NC_NOERR) return 0; /* no unlimited defined */
322  if(ndims == 0) return 0; /* scalar */
323  status = nc_inq_vardimid(ncid,varid,dimset);
324  if(status != NC_NOERR) return 0; /* no unlimited defined */
325  status = nc_inq_dim(ncid,dimset[0],NULL,nrecs);
326  if(status != NC_NOERR) return 0;
327  return (dimset[0] == unlimid ? 1: 0);
328 }
329 
356 int
357 NC_inq_recvar(int ncid, int varid, int* nrecdimsp, int *is_recdim)
358 {
359  int status = NC_NOERR;
360  int unlimid;
361  int nvardims;
362  int dimset[NC_MAX_VAR_DIMS];
363  int dim;
364  int nrecdims = 0;
365 
366  status = nc_inq_varndims(ncid,varid,&nvardims);
367  if(status != NC_NOERR) return status;
368  if(nvardims == 0) return NC_NOERR; /* scalars have no dims */
369  for(dim = 0; dim < nvardims; dim++)
370  is_recdim[dim] = 0;
371  status = nc_inq_unlimdim(ncid, &unlimid);
372  if(status != NC_NOERR) return status;
373  if(unlimid == -1) return status; /* no unlimited dims for any variables */
374 #ifdef USE_NETCDF4
375  {
376  int nunlimdims;
377  int *unlimids;
378  int recdim;
379  status = nc_inq_unlimdims(ncid, &nunlimdims, NULL); /* for group or file, not variable */
380  if(status != NC_NOERR) return status;
381  if(nunlimdims == 0) return status;
382 
383  if (!(unlimids = malloc(nunlimdims * sizeof(int))))
384  return NC_ENOMEM;
385  status = nc_inq_unlimdims(ncid, &nunlimdims, unlimids); /* for group or file, not variable */
386  if(status != NC_NOERR) {
387  free(unlimids);
388  return status;
389  }
390  status = nc_inq_vardimid(ncid, varid, dimset);
391  if(status != NC_NOERR) {
392  free(unlimids);
393  return status;
394  }
395  for (dim = 0; dim < nvardims; dim++) { /* netCDF-4 rec dims need not be first dim for a rec var */
396  for(recdim = 0; recdim < nunlimdims; recdim++) {
397  if(dimset[dim] == unlimids[recdim]) {
398  is_recdim[dim] = 1;
399  nrecdims++;
400  }
401  }
402  }
403  free(unlimids);
404  }
405 #else
406  status = nc_inq_vardimid(ncid, varid, dimset);
407  if(status != NC_NOERR) return status;
408  if(dimset[0] == unlimid) {
409  is_recdim[0] = 1;
410  nrecdims++;
411  }
412 #endif /* USE_NETCDF4 */
413  if(nrecdimsp) *nrecdimsp = nrecdims;
414  return status;
415 }
416 
417 /* Ok to use NC pointers because
418  all IOSP's will use that structure,
419  but not ok to use e.g. NC_Var pointers
420  because they may be different structure
421  entirely.
422 */
423 
435 int
436 nctypelen(nc_type type)
437 {
438  switch(type){
439  case NC_CHAR :
440  return ((int)sizeof(char));
441  case NC_BYTE :
442  return ((int)sizeof(signed char));
443  case NC_SHORT :
444  return ((int)sizeof(short));
445  case NC_INT :
446  return ((int)sizeof(int));
447  case NC_FLOAT :
448  return ((int)sizeof(float));
449  case NC_DOUBLE :
450  return ((int)sizeof(double));
451 
452  /* These can occur in netcdf-3 code */
453  case NC_UBYTE :
454  return ((int)sizeof(unsigned char));
455  case NC_USHORT :
456  return ((int)(sizeof(unsigned short)));
457  case NC_UINT :
458  return ((int)sizeof(unsigned int));
459  case NC_INT64 :
460  return ((int)sizeof(signed long long));
461  case NC_UINT64 :
462  return ((int)sizeof(unsigned long long));
463 #ifdef USE_NETCDF4
464  case NC_STRING :
465  return ((int)sizeof(char*));
466 #endif /*USE_NETCDF4*/
467 
468  default:
469  return -1;
470  }
471 }
472 
476 size_t
477 NC_atomictypelen(nc_type xtype)
478 {
479  size_t sz = 0;
480  switch(xtype) {
481  case NC_NAT: sz = 0; break;
482  case NC_BYTE: sz = sizeof(signed char); break;
483  case NC_CHAR: sz = sizeof(char); break;
484  case NC_SHORT: sz = sizeof(short); break;
485  case NC_INT: sz = sizeof(int); break;
486  case NC_FLOAT: sz = sizeof(float); break;
487  case NC_DOUBLE: sz = sizeof(double); break;
488  case NC_INT64: sz = sizeof(signed long long); break;
489  case NC_UBYTE: sz = sizeof(unsigned char); break;
490  case NC_USHORT: sz = sizeof(unsigned short); break;
491  case NC_UINT: sz = sizeof(unsigned int); break;
492  case NC_UINT64: sz = sizeof(unsigned long long); break;
493 #ifdef USE_NETCDF4
494  case NC_STRING: sz = sizeof(char*); break;
495 #endif
496  default: break;
497  }
498  return sz;
499 }
500 
504 char *
505 NC_atomictypename(nc_type xtype)
506 {
507  char* nm = NULL;
508  switch(xtype) {
509  case NC_NAT: nm = "undefined"; break;
510  case NC_BYTE: nm = "byte"; break;
511  case NC_CHAR: nm = "char"; break;
512  case NC_SHORT: nm = "short"; break;
513  case NC_INT: nm = "int"; break;
514  case NC_FLOAT: nm = "float"; break;
515  case NC_DOUBLE: nm = "double"; break;
516  case NC_INT64: nm = "int64"; break;
517  case NC_UBYTE: nm = "ubyte"; break;
518  case NC_USHORT: nm = "ushort"; break;
519  case NC_UINT: nm = "uint"; break;
520  case NC_UINT64: nm = "uint64"; break;
521 #ifdef USE_NETCDF4
522  case NC_STRING: nm = "string"; break;
523 #endif
524  default: break;
525  }
526  return nm;
527 }
528 
533 int
534 NC_getshape(int ncid, int varid, int ndims, size_t* shape)
535 {
536  int dimids[NC_MAX_VAR_DIMS];
537  int i;
538  int status = NC_NOERR;
539 
540  if ((status = nc_inq_vardimid(ncid, varid, dimids)))
541  return status;
542  for(i = 0; i < ndims; i++)
543  if ((status = nc_inq_dimlen(ncid, dimids[i], &shape[i])))
544  break;
545 
546  return status;
547 }
548 
629 int
630 nc_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value)
631 {
632  NC* ncp;
633  int stat = NC_check_id(ncid,&ncp);
634  if(stat != NC_NOERR) return stat;
635 
636  /* Dennis Heimbigner: Using NC_GLOBAL is illegal, as this API has no
637  * provision for specifying the type of the fillvalue, it must of necessity
638  * be using the type of the variable to interpret the bytes of the
639  * fill_value argument.
640  */
641  if (varid == NC_GLOBAL) return NC_EGLOBAL;
642 
643  return ncp->dispatch->def_var_fill(ncid,varid,no_fill,fill_value);
644 }
645 
670 int
671 NC_check_nulls(int ncid, int varid, const size_t *start, size_t **count,
672  ptrdiff_t **stride)
673 {
674  int varndims;
675  int stat;
676 
677  if ((stat = nc_inq_varndims(ncid, varid, &varndims)))
678  return stat;
679 
680  /* For non-scalar vars, start is required. */
681  if (!start && varndims)
682  return NC_EINVALCOORDS;
683 
684  /* If count is NULL, assume full extent of var. */
685  if (!*count)
686  {
687  if (!(*count = malloc(varndims * sizeof(size_t))))
688  return NC_ENOMEM;
689  if ((stat = NC_getshape(ncid, varid, varndims, *count)))
690  {
691  free(*count);
692  *count = NULL;
693  return stat;
694  }
695  }
696 
697  /* If stride is NULL, do nothing, if *stride is NULL use all 1s. */
698  if (stride && !*stride)
699  {
700  int i;
701 
702  if (!(*stride = malloc(varndims * sizeof(size_t))))
703  return NC_ENOMEM;
704  for (i = 0; i < varndims; i++)
705  *stride[i] = 1;
706  }
707 
708  return NC_NOERR;
709 }
710 
724 int
725 nc_free_string(size_t len, char **data)
726 {
727  int i;
728  for (i = 0; i < len; i++)
729  free(data[i]);
730  return NC_NOERR;
731 }
732 
733 #ifdef USE_NETCDF4
734 
793 int
794 nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems,
795  float preemption)
796 {
797  NC* ncp;
798  int stat = NC_check_id(ncid, &ncp);
799  if(stat != NC_NOERR) return stat;
800  return ncp->dispatch->set_var_chunk_cache(ncid, varid, size,
801  nelems, preemption);
802 }
803 
834 int
835 nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp,
836  float *preemptionp)
837 {
838  NC* ncp;
839  int stat = NC_check_id(ncid, &ncp);
840  if(stat != NC_NOERR) return stat;
841  return ncp->dispatch->get_var_chunk_cache(ncid, varid, sizep,
842  nelemsp, preemptionp);
843 }
844 
938 int
939 nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
940 {
941  NC* ncp;
942  int stat = NC_check_id(ncid,&ncp);
943  if(stat != NC_NOERR) return stat;
944  return ncp->dispatch->def_var_deflate(ncid,varid,shuffle,deflate,deflate_level);
945 }
946 
978 int
979 nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
980 {
981  NC* ncp;
982  int stat = NC_check_id(ncid,&ncp);
983  if(stat != NC_NOERR) return stat;
984  return ncp->dispatch->def_var_fletcher32(ncid,varid,fletcher32);
985 }
986 
1079 int
1080 nc_def_var_chunking(int ncid, int varid, int storage,
1081  const size_t *chunksizesp)
1082 {
1083  NC* ncp;
1084  int stat = NC_check_id(ncid, &ncp);
1085  if(stat != NC_NOERR) return stat;
1086  return ncp->dispatch->def_var_chunking(ncid, varid, storage,
1087  chunksizesp);
1088 }
1089 
1160 int
1161 nc_def_var_endian(int ncid, int varid, int endian)
1162 {
1163  NC* ncp;
1164  int stat = NC_check_id(ncid,&ncp);
1165  if(stat != NC_NOERR) return stat;
1166  return ncp->dispatch->def_var_endian(ncid,varid,endian);
1167 }
1168 
1182 int
1183 nc_def_var_filter(int ncid, int varid, unsigned int id, size_t nparams, const unsigned int* parms)
1184 {
1185  NC* ncp;
1186  int stat = NC_check_id(ncid,&ncp);
1187  if(stat != NC_NOERR) return stat;
1188  return ncp->dispatch->def_var_filter(ncid,varid,id,nparams,parms);
1189 }
1190 
1191 #endif /* USE_NETCDF4 */
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition: netcdf.h:402
#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:835
EXTERNL int nc_inq_vardimid(int ncid, int varid, int *dimidsp)
Learn the dimension IDs associated with a variable.
Definition: dvarinq.c:226
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:210
#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:1183
#define NC_MAX_VAR_DIMS
max per variable dimensions
Definition: netcdf.h:273
#define NC_UINT
unsigned 4-byte int
Definition: netcdf.h:43
#define NC_EINVALCOORDS
Index exceeds dimension bound.
Definition: netcdf.h:354
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:939
#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:203
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:794
int nc_rename_var(int ncid, int varid, const char *name)
Rename a variable.
Definition: dvar.c:290
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:377
#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:1080
#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:725
#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:1161
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:595
#define NC_NOERR
No Error.
Definition: netcdf.h:322
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:630
#define NC_GLOBAL
Attribute id to put/get a global attribute.
Definition: netcdf.h:245
#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:979

Return to the Main Unidata NetCDF page.
Generated on Wed Mar 4 2020 06:29:56 for NetCDF. NetCDF is a Unidata library.