NetCDF  4.7.0
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. */
9 #include "ncdispatch.h"
10 #include "netcdf_f.h"
11 
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 }
219 
298 int
299 nc_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value)
300 {
301  NC* ncp;
302  int stat = NC_check_id(ncid,&ncp);
303  if(stat != NC_NOERR) return stat;
304 
305  /* Using NC_GLOBAL is illegal, as this API has no provision for
306  * specifying the type of the fillvalue, it must of necessity be
307  * using the type of the variable to interpret the bytes of the
308  * fill_value argument. */
309  if (varid == NC_GLOBAL) return NC_EGLOBAL;
310 
311  return ncp->dispatch->def_var_fill(ncid,varid,no_fill,fill_value);
312 }
313 
314 #ifdef USE_NETCDF4
315 
406 int
407 nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
408 {
409  NC* ncp;
410  int stat = NC_check_id(ncid,&ncp);
411  if(stat != NC_NOERR) return stat;
412  return ncp->dispatch->def_var_deflate(ncid,varid,shuffle,deflate,deflate_level);
413 }
414 
444 int
445 nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
446 {
447  NC* ncp;
448  int stat = NC_check_id(ncid,&ncp);
449  if(stat != NC_NOERR) return stat;
450  return ncp->dispatch->def_var_fletcher32(ncid,varid,fletcher32);
451 }
452 
544 int
545 nc_def_var_chunking(int ncid, int varid, int storage,
546  const size_t *chunksizesp)
547 {
548  NC* ncp;
549  int stat = NC_check_id(ncid, &ncp);
550  if(stat != NC_NOERR) return stat;
551  return ncp->dispatch->def_var_chunking(ncid, varid, storage,
552  chunksizesp);
553 }
554 
623 int
624 nc_def_var_endian(int ncid, int varid, int endian)
625 {
626  NC* ncp;
627  int stat = NC_check_id(ncid,&ncp);
628  if(stat != NC_NOERR) return stat;
629  return ncp->dispatch->def_var_endian(ncid,varid,endian);
630 }
631 
645 int
646 nc_def_var_filter(int ncid, int varid, unsigned int id,
647  size_t nparams, const unsigned int* parms)
648 {
649  NC* ncp;
650  int stat = NC_check_id(ncid,&ncp);
651  if(stat != NC_NOERR) return stat;
652  return ncp->dispatch->def_var_filter(ncid,varid,id,nparams,parms);
653 }
654 #endif /* USE_NETCDF4 */
655 
722 int
723 nc_rename_var(int ncid, int varid, const char *name)
724 {
725  NC* ncp;
726  int stat = NC_check_id(ncid, &ncp);
727  if(stat != NC_NOERR) return stat;
728  TRACE(nc_rename_var);
729  return ncp->dispatch->rename_var(ncid, varid, name);
730 }
742 int
743 NC_is_recvar(int ncid, int varid, size_t* nrecs)
744 {
745  int status = NC_NOERR;
746  int unlimid;
747  int ndims;
748  int dimset[NC_MAX_VAR_DIMS];
749 
750  status = nc_inq_unlimdim(ncid,&unlimid);
751  if(status != NC_NOERR) return 0; /* no unlimited defined */
752  status = nc_inq_varndims(ncid,varid,&ndims);
753  if(status != NC_NOERR) return 0; /* no unlimited defined */
754  if(ndims == 0) return 0; /* scalar */
755  status = nc_inq_vardimid(ncid,varid,dimset);
756  if(status != NC_NOERR) return 0; /* no unlimited defined */
757  status = nc_inq_dim(ncid,dimset[0],NULL,nrecs);
758  if(status != NC_NOERR) return 0;
759  return (dimset[0] == unlimid ? 1: 0);
760 }
761 
787 int
788 NC_inq_recvar(int ncid, int varid, int* nrecdimsp, int *is_recdim)
789 {
790  int status = NC_NOERR;
791  int unlimid;
792  int nvardims;
793  int dimset[NC_MAX_VAR_DIMS];
794  int dim;
795  int nrecdims = 0;
796 
797  status = nc_inq_varndims(ncid,varid,&nvardims);
798  if(status != NC_NOERR) return status;
799  if(nvardims == 0) return NC_NOERR; /* scalars have no dims */
800  for(dim = 0; dim < nvardims; dim++)
801  is_recdim[dim] = 0;
802  status = nc_inq_unlimdim(ncid, &unlimid);
803  if(status != NC_NOERR) return status;
804  if(unlimid == -1) return status; /* no unlimited dims for any variables */
805 #ifdef USE_NETCDF4
806  {
807  int nunlimdims;
808  int *unlimids;
809  int recdim;
810  status = nc_inq_unlimdims(ncid, &nunlimdims, NULL); /* for group or file, not variable */
811  if(status != NC_NOERR) return status;
812  if(nunlimdims == 0) return status;
813 
814  if (!(unlimids = malloc(nunlimdims * sizeof(int))))
815  return NC_ENOMEM;
816  status = nc_inq_unlimdims(ncid, &nunlimdims, unlimids); /* for group or file, not variable */
817  if(status != NC_NOERR) {
818  free(unlimids);
819  return status;
820  }
821  status = nc_inq_vardimid(ncid, varid, dimset);
822  if(status != NC_NOERR) {
823  free(unlimids);
824  return status;
825  }
826  for (dim = 0; dim < nvardims; dim++) { /* netCDF-4 rec dims need not be first dim for a rec var */
827  for(recdim = 0; recdim < nunlimdims; recdim++) {
828  if(dimset[dim] == unlimids[recdim]) {
829  is_recdim[dim] = 1;
830  nrecdims++;
831  }
832  }
833  }
834  free(unlimids);
835  }
836 #else
837  status = nc_inq_vardimid(ncid, varid, dimset);
838  if(status != NC_NOERR) return status;
839  if(dimset[0] == unlimid) {
840  is_recdim[0] = 1;
841  nrecdims++;
842  }
843 #endif /* USE_NETCDF4 */
844  if(nrecdimsp) *nrecdimsp = nrecdims;
845  return status;
846 }
847 
848 /* Ok to use NC pointers because
849  all IOSP's will use that structure,
850  but not ok to use e.g. NC_Var pointers
851  because they may be different structure
852  entirely.
853 */
854 
865 int
866 nctypelen(nc_type type)
867 {
868  switch(type){
869  case NC_CHAR :
870  return ((int)sizeof(char));
871  case NC_BYTE :
872  return ((int)sizeof(signed char));
873  case NC_SHORT :
874  return ((int)sizeof(short));
875  case NC_INT :
876  return ((int)sizeof(int));
877  case NC_FLOAT :
878  return ((int)sizeof(float));
879  case NC_DOUBLE :
880  return ((int)sizeof(double));
881 
882  /* These can occur in netcdf-3 code */
883  case NC_UBYTE :
884  return ((int)sizeof(unsigned char));
885  case NC_USHORT :
886  return ((int)(sizeof(unsigned short)));
887  case NC_UINT :
888  return ((int)sizeof(unsigned int));
889  case NC_INT64 :
890  return ((int)sizeof(signed long long));
891  case NC_UINT64 :
892  return ((int)sizeof(unsigned long long));
893 #ifdef USE_NETCDF4
894  case NC_STRING :
895  return ((int)sizeof(char*));
896 #endif /*USE_NETCDF4*/
897 
898  default:
899  return -1;
900  }
901 }
902 
911 size_t
912 NC_atomictypelen(nc_type xtype)
913 {
914  size_t sz = 0;
915  switch(xtype) {
916  case NC_NAT: sz = 0; break;
917  case NC_BYTE: sz = sizeof(signed char); break;
918  case NC_CHAR: sz = sizeof(char); break;
919  case NC_SHORT: sz = sizeof(short); break;
920  case NC_INT: sz = sizeof(int); break;
921  case NC_FLOAT: sz = sizeof(float); break;
922  case NC_DOUBLE: sz = sizeof(double); break;
923  case NC_INT64: sz = sizeof(signed long long); break;
924  case NC_UBYTE: sz = sizeof(unsigned char); break;
925  case NC_USHORT: sz = sizeof(unsigned short); break;
926  case NC_UINT: sz = sizeof(unsigned int); break;
927  case NC_UINT64: sz = sizeof(unsigned long long); break;
928 #ifdef USE_NETCDF4
929  case NC_STRING: sz = sizeof(char*); break;
930 #endif
931  default: break;
932  }
933  return sz;
934 }
935 
944 char *
945 NC_atomictypename(nc_type xtype)
946 {
947  char* nm = NULL;
948  switch(xtype) {
949  case NC_NAT: nm = "undefined"; break;
950  case NC_BYTE: nm = "byte"; break;
951  case NC_CHAR: nm = "char"; break;
952  case NC_SHORT: nm = "short"; break;
953  case NC_INT: nm = "int"; break;
954  case NC_FLOAT: nm = "float"; break;
955  case NC_DOUBLE: nm = "double"; break;
956  case NC_INT64: nm = "int64"; break;
957  case NC_UBYTE: nm = "ubyte"; break;
958  case NC_USHORT: nm = "ushort"; break;
959  case NC_UINT: nm = "uint"; break;
960  case NC_UINT64: nm = "uint64"; break;
961 #ifdef USE_NETCDF4
962  case NC_STRING: nm = "string"; break;
963 #endif
964  default: break;
965  }
966  return nm;
967 }
968 
986 int
987 NC_getshape(int ncid, int varid, int ndims, size_t* shape)
988 {
989  int dimids[NC_MAX_VAR_DIMS];
990  int i;
991  int status = NC_NOERR;
992 
993  if ((status = nc_inq_vardimid(ncid, varid, dimids)))
994  return status;
995  for(i = 0; i < ndims; i++)
996  if ((status = nc_inq_dimlen(ncid, dimids[i], &shape[i])))
997  break;
998 
999  return status;
1000 }
1001 
1026 int
1027 NC_check_nulls(int ncid, int varid, const size_t *start, size_t **count,
1028  ptrdiff_t **stride)
1029 {
1030  int varndims;
1031  int stat;
1032 
1033  if ((stat = nc_inq_varndims(ncid, varid, &varndims)))
1034  return stat;
1035 
1036  /* For non-scalar vars, start is required. */
1037  if (!start && varndims)
1038  return NC_EINVALCOORDS;
1039 
1040  /* If count is NULL, assume full extent of var. */
1041  if (!*count)
1042  {
1043  if (!(*count = malloc(varndims * sizeof(size_t))))
1044  return NC_ENOMEM;
1045  if ((stat = NC_getshape(ncid, varid, varndims, *count)))
1046  {
1047  free(*count);
1048  *count = NULL;
1049  return stat;
1050  }
1051  }
1052 
1053  /* If stride is NULL, do nothing, if *stride is NULL use all
1054  * 1s. */
1055  if (stride && !*stride)
1056  {
1057  int i;
1058 
1059  if (!(*stride = malloc(varndims * sizeof(ptrdiff_t))))
1060  return NC_ENOMEM;
1061  for (i = 0; i < varndims; i++)
1062  (*stride)[i] = 1;
1063  }
1064 
1065  return NC_NOERR;
1066 }
1067 
1089 int
1090 nc_free_string(size_t len, char **data)
1091 {
1092  int i;
1093  for (i = 0; i < len; i++)
1094  free(data[i]);
1095  return NC_NOERR;
1096 }
1099 #ifdef USE_NETCDF4
1100 
1162 int
1163 nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems,
1164  float preemption)
1165 {
1166  NC* ncp;
1167  int stat = NC_check_id(ncid, &ncp);
1168  if(stat != NC_NOERR) return stat;
1169  return ncp->dispatch->set_var_chunk_cache(ncid, varid, size,
1170  nelems, preemption);
1171 }
1172 
1200 int
1201 nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp,
1202  float *preemptionp)
1203 {
1204  NC* ncp;
1205  int stat = NC_check_id(ncid, &ncp);
1206  if(stat != NC_NOERR) return stat;
1207  return ncp->dispatch->get_var_chunk_cache(ncid, varid, sizep,
1208  nelemsp, preemptionp);
1209 }
1211 #endif /* USE_NETCDF4 */
1212 
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition: netcdf.h:405
#define NC_CHAR
ISO/ASCII character.
Definition: netcdf.h:36
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:1201
EXTERNL int nc_inq_vardimid(int ncid, int varid, int *dimidsp)
Learn the dimension IDs associated with a variable.
Definition: dvarinq.c:227
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:42
#define NC_MAX_VAR_DIMS
max per variable dimensions
Definition: netcdf.h:276
#define NC_UINT
unsigned 4-byte int
Definition: netcdf.h:44
#define NC_EINVALCOORDS
Index exceeds dimension bound.
Definition: netcdf.h:357
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:407
#define NC_INT64
signed 8-byte int
Definition: netcdf.h:45
#define NC_STRING
string
Definition: netcdf.h:47
#define NC_DOUBLE
double precision floating point number
Definition: netcdf.h:41
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:216
EXTERNL int nc_inq_varndims(int ncid, int varid, int *ndimsp)
Learn how many dimensions are associated with a variable.
Definition: dvarinq.c:204
int nc_type
The nc_type type is just an int.
Definition: netcdf.h:25
#define NC_BYTE
signed 1 byte integer
Definition: netcdf.h:35
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:1163
int nc_rename_var(int ncid, int varid, const char *name)
Rename a variable.
Definition: dvar.c:723
EXTERNL int nc_inq_dimlen(int ncid, int dimid, size_t *lenp)
Find the length of a dimension.
Definition: ddim.c:467
#define NC_EGLOBAL
Action prohibited on NC_GLOBAL varid.
Definition: netcdf.h:380
#define NC_INT
signed 4 byte integer
Definition: netcdf.h:38
int nc_def_var_chunking(int ncid, int varid, int storage, const size_t *chunksizesp)
Define chunking parameters for a variable.
Definition: dvar.c:545
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:646
#define NC_NAT
Not A Type.
Definition: netcdf.h:34
int nc_free_string(size_t len, char **data)
Free string space allocated by the library.
Definition: dvar.c:1090
#define NC_USHORT
unsigned 2-byte int
Definition: netcdf.h:43
int nc_def_var_endian(int ncid, int varid, int endian)
Define endianness of a variable.
Definition: dvar.c:624
EXTERNL int nc_inq_unlimdim(int ncid, int *unlimdimidp)
Find the ID of the unlimited dimension.
Definition: ddim.c:350
#define NC_SHORT
signed 2 byte integer
Definition: netcdf.h:37
EXTERNL int nc_inq_unlimdims(int ncid, int *nunlimdimsp, int *unlimdimidsp)
Return number and list of unlimited dimensions.
Definition: dvarinq.c:596
#define NC_NOERR
No Error.
Definition: netcdf.h:325
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:299
#define NC_GLOBAL
Attribute id to put/get a global attribute.
Definition: netcdf.h:248
#define NC_FLOAT
single precision floating point number
Definition: netcdf.h:40
#define NC_UINT64
unsigned 8-byte int
Definition: netcdf.h:46
int nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
Set checksum for a var.
Definition: dvar.c:445

Return to the Main Unidata NetCDF page.
Generated on Mon Apr 29 2019 10:59:59 for NetCDF. NetCDF is a Unidata library.