NetCDF  4.6.0
dvar.c
Go to the documentation of this file.
1 
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 }
284 int
285 nc_rename_var(int ncid, int varid, const char *name)
286 {
287  NC* ncp;
288  int stat = NC_check_id(ncid, &ncp);
289  if(stat != NC_NOERR) return stat;
290  TRACE(nc_rename_var);
291  return ncp->dispatch->rename_var(ncid, varid, name);
292 }
305 int
306 NC_is_recvar(int ncid, int varid, size_t* nrecs)
307 {
308  int status = NC_NOERR;
309  int unlimid;
310  int ndims;
311  int dimset[NC_MAX_VAR_DIMS];
312 
313  status = nc_inq_unlimdim(ncid,&unlimid);
314  if(status != NC_NOERR) return 0; /* no unlimited defined */
315  status = nc_inq_varndims(ncid,varid,&ndims);
316  if(status != NC_NOERR) return 0; /* no unlimited defined */
317  if(ndims == 0) return 0; /* scalar */
318  status = nc_inq_vardimid(ncid,varid,dimset);
319  if(status != NC_NOERR) return 0; /* no unlimited defined */
320  status = nc_inq_dim(ncid,dimset[0],NULL,nrecs);
321  if(status != NC_NOERR) return 0;
322  return (dimset[0] == unlimid ? 1: 0);
323 }
324 
351 int
352 NC_inq_recvar(int ncid, int varid, int* nrecdimsp, int *is_recdim)
353 {
354  int status = NC_NOERR;
355  int unlimid;
356  int nvardims;
357  int dimset[NC_MAX_VAR_DIMS];
358  int dim;
359  int nrecdims = 0;
360 
361  status = nc_inq_varndims(ncid,varid,&nvardims);
362  if(status != NC_NOERR) return status;
363  if(nvardims == 0) return NC_NOERR; /* scalars have no dims */
364  for(dim = 0; dim < nvardims; dim++)
365  is_recdim[dim] = 0;
366  status = nc_inq_unlimdim(ncid, &unlimid);
367  if(status != NC_NOERR) return status;
368  if(unlimid == -1) return status; /* no unlimited dims for any variables */
369 #ifdef USE_NETCDF4
370  {
371  int nunlimdims;
372  int *unlimids;
373  int recdim;
374  status = nc_inq_unlimdims(ncid, &nunlimdims, NULL); /* for group or file, not variable */
375  if(status != NC_NOERR) return status;
376  if(nunlimdims == 0) return status;
377 
378  if (!(unlimids = malloc(nunlimdims * sizeof(int))))
379  return NC_ENOMEM;
380  status = nc_inq_unlimdims(ncid, &nunlimdims, unlimids); /* for group or file, not variable */
381  if(status != NC_NOERR) {
382  free(unlimids);
383  return status;
384  }
385  status = nc_inq_vardimid(ncid, varid, dimset);
386  if(status != NC_NOERR) {
387  free(unlimids);
388  return status;
389  }
390  for (dim = 0; dim < nvardims; dim++) { /* netCDF-4 rec dims need not be first dim for a rec var */
391  for(recdim = 0; recdim < nunlimdims; recdim++) {
392  if(dimset[dim] == unlimids[recdim]) {
393  is_recdim[dim] = 1;
394  nrecdims++;
395  }
396  }
397  }
398  free(unlimids);
399  }
400 #else
401  status = nc_inq_vardimid(ncid, varid, dimset);
402  if(status != NC_NOERR) return status;
403  if(dimset[0] == unlimid) {
404  is_recdim[0] = 1;
405  nrecdims++;
406  }
407 #endif /* USE_NETCDF4 */
408  if(nrecdimsp) *nrecdimsp = nrecdims;
409  return status;
410 }
411 
412 /* Ok to use NC pointers because
413  all IOSP's will use that structure,
414  but not ok to use e.g. NC_Var pointers
415  because they may be different structure
416  entirely.
417 */
418 
427 int
428 nctypelen(nc_type type)
429 {
430  switch(type){
431  case NC_CHAR :
432  return ((int)sizeof(char));
433  case NC_BYTE :
434  return ((int)sizeof(signed char));
435  case NC_SHORT :
436  return ((int)sizeof(short));
437  case NC_INT :
438  return ((int)sizeof(int));
439  case NC_FLOAT :
440  return ((int)sizeof(float));
441  case NC_DOUBLE :
442  return ((int)sizeof(double));
443 
444  /* These can occur in netcdf-3 code */
445  case NC_UBYTE :
446  return ((int)sizeof(unsigned char));
447  case NC_USHORT :
448  return ((int)(sizeof(unsigned short)));
449  case NC_UINT :
450  return ((int)sizeof(unsigned int));
451  case NC_INT64 :
452  return ((int)sizeof(signed long long));
453  case NC_UINT64 :
454  return ((int)sizeof(unsigned long long));
455 #ifdef USE_NETCDF4
456  case NC_STRING :
457  return ((int)sizeof(char*));
458 #endif /*USE_NETCDF4*/
459 
460  default:
461  return -1;
462  }
463 }
464 
468 size_t
469 NC_atomictypelen(nc_type xtype)
470 {
471  size_t sz = 0;
472  switch(xtype) {
473  case NC_NAT: sz = 0; break;
474  case NC_BYTE: sz = sizeof(signed char); break;
475  case NC_CHAR: sz = sizeof(char); break;
476  case NC_SHORT: sz = sizeof(short); break;
477  case NC_INT: sz = sizeof(int); break;
478  case NC_FLOAT: sz = sizeof(float); break;
479  case NC_DOUBLE: sz = sizeof(double); break;
480  case NC_INT64: sz = sizeof(signed long long); break;
481  case NC_UBYTE: sz = sizeof(unsigned char); break;
482  case NC_USHORT: sz = sizeof(unsigned short); break;
483  case NC_UINT: sz = sizeof(unsigned int); break;
484  case NC_UINT64: sz = sizeof(unsigned long long); break;
485 #ifdef USE_NETCDF4
486  case NC_STRING: sz = sizeof(char*); break;
487 #endif
488  default: break;
489  }
490  return sz;
491 }
492 
496 char *
497 NC_atomictypename(nc_type xtype)
498 {
499  char* nm = NULL;
500  switch(xtype) {
501  case NC_NAT: nm = "undefined"; break;
502  case NC_BYTE: nm = "byte"; break;
503  case NC_CHAR: nm = "char"; break;
504  case NC_SHORT: nm = "short"; break;
505  case NC_INT: nm = "int"; break;
506  case NC_FLOAT: nm = "float"; break;
507  case NC_DOUBLE: nm = "double"; break;
508  case NC_INT64: nm = "int64"; break;
509  case NC_UBYTE: nm = "ubyte"; break;
510  case NC_USHORT: nm = "ushort"; break;
511  case NC_UINT: nm = "uint"; break;
512  case NC_UINT64: nm = "uint64"; break;
513 #ifdef USE_NETCDF4
514  case NC_STRING: nm = "string"; break;
515 #endif
516  default: break;
517  }
518  return nm;
519 }
520 
525 int
526 NC_getshape(int ncid, int varid, int ndims, size_t* shape)
527 {
528  int dimids[NC_MAX_VAR_DIMS];
529  int i;
530  int status = NC_NOERR;
531 
532  if ((status = nc_inq_vardimid(ncid, varid, dimids)))
533  return status;
534  for(i = 0; i < ndims; i++)
535  if ((status = nc_inq_dimlen(ncid, dimids[i], &shape[i])))
536  break;
537 
538  return status;
539 }
540 
541 #ifdef USE_NETCDF4
542 
601 int
602 nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems,
603  float preemption)
604 {
605  NC* ncp;
606  int stat = NC_check_id(ncid, &ncp);
607  if(stat != NC_NOERR) return stat;
608  return ncp->dispatch->set_var_chunk_cache(ncid, varid, size,
609  nelems, preemption);
610 }
611 
642 int
643 nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp,
644  float *preemptionp)
645 {
646  NC* ncp;
647  int stat = NC_check_id(ncid, &ncp);
648  if(stat != NC_NOERR) return stat;
649  return ncp->dispatch->get_var_chunk_cache(ncid, varid, sizep,
650  nelemsp, preemptionp);
651 }
652 
666 int
667 nc_free_string(size_t len, char **data)
668 {
669  int i;
670  for (i = 0; i < len; i++)
671  free(data[i]);
672  return NC_NOERR;
673 }
674 
756 int
757 nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
758 {
759  NC* ncp;
760  int stat = NC_check_id(ncid,&ncp);
761  if(stat != NC_NOERR) return stat;
762  return ncp->dispatch->def_var_deflate(ncid,varid,shuffle,deflate,deflate_level);
763 }
764 
790 int
791 nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
792 {
793  NC* ncp;
794  int stat = NC_check_id(ncid,&ncp);
795  if(stat != NC_NOERR) return stat;
796  return ncp->dispatch->def_var_fletcher32(ncid,varid,fletcher32);
797 }
798 
891 int
892 nc_def_var_chunking(int ncid, int varid, int storage,
893  const size_t *chunksizesp)
894 {
895  NC* ncp;
896  int stat = NC_check_id(ncid, &ncp);
897  if(stat != NC_NOERR) return stat;
898  return ncp->dispatch->def_var_chunking(ncid, varid, storage,
899  chunksizesp);
900 }
901 
967 int
968 nc_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value)
969 {
970  NC* ncp;
971  int stat = NC_check_id(ncid,&ncp);
972  if(stat != NC_NOERR) return stat;
973  return ncp->dispatch->def_var_fill(ncid,varid,no_fill,fill_value);
974 }
975 
1043 int
1044 nc_def_var_endian(int ncid, int varid, int endian)
1045 {
1046  NC* ncp;
1047  int stat = NC_check_id(ncid,&ncp);
1048  if(stat != NC_NOERR) return stat;
1049  return ncp->dispatch->def_var_endian(ncid,varid,endian);
1050 }
1051 
1064 int
1065 nc_def_var_filter(int ncid, int varid, unsigned int id, size_t nparams, const unsigned int* parms)
1066 {
1067  NC* ncp;
1068  int stat = NC_check_id(ncid,&ncp);
1069  if(stat != NC_NOERR) return stat;
1070  return ncp->dispatch->def_var_filter(ncid,varid,id,nparams,parms);
1071 }
1072 
1073 #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:643
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:1065
#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:757
#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:602
int nc_rename_var(int ncid, int varid, const char *name)
Rename a variable.
Definition: dvar.c:285
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: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:892
#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:667
#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:1044
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: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 netCDF4/HDF5 variable.
Definition: dvar.c:968
#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:791

Return to the Main Unidata NetCDF page.
Generated on Fri Feb 9 2018 19:21:24 for NetCDF. NetCDF is a Unidata library.