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

Return to the Main Unidata NetCDF page.
Generated on Tue Oct 22 2019 13:02:54 for NetCDF. NetCDF is a Unidata library.