NetCDF  4.7.4
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 "config.h"
10 #include "netcdf.h"
11 #include "netcdf_filter.h"
12 #include "ncdispatch.h"
13 #include "nc4internal.h"
14 #include "netcdf_f.h"
15 #include "nc4internal.h"
16 
211 int
212 nc_def_var(int ncid, const char *name, nc_type xtype,
213  int ndims, const int *dimidsp, int *varidp)
214 {
215  NC* ncp;
216  int stat = NC_NOERR;
217 
218  if ((stat = NC_check_id(ncid, &ncp)))
219  return stat;
220  TRACE(nc_def_var);
221  return ncp->dispatch->def_var(ncid, name, xtype, ndims,
222  dimidsp, varidp);
223 }
224 
303 int
304 nc_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value)
305 {
306  NC* ncp;
307  int stat = NC_check_id(ncid,&ncp);
308  if(stat != NC_NOERR) return stat;
309 
310  /* Using NC_GLOBAL is illegal, as this API has no provision for
311  * specifying the type of the fillvalue, it must of necessity be
312  * using the type of the variable to interpret the bytes of the
313  * fill_value argument. */
314  if (varid == NC_GLOBAL) return NC_EGLOBAL;
315 
316  return ncp->dispatch->def_var_fill(ncid,varid,no_fill,fill_value);
317 }
318 
418 int
419 nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
420 {
421  NC* ncp;
422  int stat = NC_check_id(ncid,&ncp);
423  if(stat != NC_NOERR) return stat;
424  return ncp->dispatch->def_var_deflate(ncid,varid,shuffle,deflate,deflate_level);
425 }
426 
464 int
465 nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
466 {
467  NC* ncp;
468  int stat = NC_check_id(ncid,&ncp);
469  if(stat != NC_NOERR) return stat;
470  return ncp->dispatch->def_var_fletcher32(ncid,varid,fletcher32);
471 }
472 
584 int
585 nc_def_var_chunking(int ncid, int varid, int storage, const size_t *chunksizesp)
586 {
587  NC* ncp;
588  int stat = NC_check_id(ncid, &ncp);
589  if(stat != NC_NOERR) return stat;
590  return ncp->dispatch->def_var_chunking(ncid, varid, storage,
591  chunksizesp);
592 }
593 
662 int
663 nc_def_var_endian(int ncid, int varid, int endian)
664 {
665  NC* ncp;
666  int stat = NC_check_id(ncid,&ncp);
667  if(stat != NC_NOERR) return stat;
668  return ncp->dispatch->def_var_endian(ncid,varid,endian);
669 }
670 
719 int
720 nc_def_var_szip(int ncid, int varid, int options_mask, int pixels_per_block)
721 {
722  int ret;
723 
724  /* This will cause H5Pset_szip to be called when the var is
725  * created. */
726  unsigned int params[2];
727  params[0] = options_mask;
728  params[1] = pixels_per_block;
729  if ((ret = nc_def_var_filter(ncid, varid, HDF5_FILTER_SZIP, 2, params)))
730  return ret;
731 
732  return NC_NOERR;
733 }
734 
801 int
802 nc_rename_var(int ncid, int varid, const char *name)
803 {
804  NC* ncp;
805  int stat = NC_check_id(ncid, &ncp);
806  if(stat != NC_NOERR) return stat;
807  TRACE(nc_rename_var);
808  return ncp->dispatch->rename_var(ncid, varid, name);
809 }
821 int
822 NC_is_recvar(int ncid, int varid, size_t* nrecs)
823 {
824  int status = NC_NOERR;
825  int unlimid;
826  int ndims;
827  int dimset[NC_MAX_VAR_DIMS];
828 
829  status = nc_inq_unlimdim(ncid,&unlimid);
830  if(status != NC_NOERR) return 0; /* no unlimited defined */
831  status = nc_inq_varndims(ncid,varid,&ndims);
832  if(status != NC_NOERR) return 0; /* no unlimited defined */
833  if(ndims == 0) return 0; /* scalar */
834  status = nc_inq_vardimid(ncid,varid,dimset);
835  if(status != NC_NOERR) return 0; /* no unlimited defined */
836  status = nc_inq_dim(ncid,dimset[0],NULL,nrecs);
837  if(status != NC_NOERR) return 0;
838  return (dimset[0] == unlimid ? 1: 0);
839 }
840 
866 int
867 NC_inq_recvar(int ncid, int varid, int* nrecdimsp, int *is_recdim)
868 {
869  int status = NC_NOERR;
870  int unlimid;
871  int nvardims;
872  int dimset[NC_MAX_VAR_DIMS];
873  int dim;
874  int nrecdims = 0;
875 
876  status = nc_inq_varndims(ncid,varid,&nvardims);
877  if(status != NC_NOERR) return status;
878  if(nvardims == 0) return NC_NOERR; /* scalars have no dims */
879  for(dim = 0; dim < nvardims; dim++)
880  is_recdim[dim] = 0;
881  status = nc_inq_unlimdim(ncid, &unlimid);
882  if(status != NC_NOERR) return status;
883  if(unlimid == -1) return status; /* no unlimited dims for any variables */
884 #ifdef USE_NETCDF4
885  {
886  int nunlimdims;
887  int *unlimids;
888  int recdim;
889  status = nc_inq_unlimdims(ncid, &nunlimdims, NULL); /* for group or file, not variable */
890  if(status != NC_NOERR) return status;
891  if(nunlimdims == 0) return status;
892 
893  if (!(unlimids = malloc(nunlimdims * sizeof(int))))
894  return NC_ENOMEM;
895  status = nc_inq_unlimdims(ncid, &nunlimdims, unlimids); /* for group or file, not variable */
896  if(status != NC_NOERR) {
897  free(unlimids);
898  return status;
899  }
900  status = nc_inq_vardimid(ncid, varid, dimset);
901  if(status != NC_NOERR) {
902  free(unlimids);
903  return status;
904  }
905  for (dim = 0; dim < nvardims; dim++) { /* netCDF-4 rec dims need not be first dim for a rec var */
906  for(recdim = 0; recdim < nunlimdims; recdim++) {
907  if(dimset[dim] == unlimids[recdim]) {
908  is_recdim[dim] = 1;
909  nrecdims++;
910  }
911  }
912  }
913  free(unlimids);
914  }
915 #else
916  status = nc_inq_vardimid(ncid, varid, dimset);
917  if(status != NC_NOERR) return status;
918  if(dimset[0] == unlimid) {
919  is_recdim[0] = 1;
920  nrecdims++;
921  }
922 #endif /* USE_NETCDF4 */
923  if(nrecdimsp) *nrecdimsp = nrecdims;
924  return status;
925 }
926 
927 /* Ok to use NC pointers because
928  all IOSP's will use that structure,
929  but not ok to use e.g. NC_Var pointers
930  because they may be different structure
931  entirely.
932 */
933 
944 int
945 nctypelen(nc_type type)
946 {
947  switch(type){
948  case NC_CHAR :
949  return ((int)sizeof(char));
950  case NC_BYTE :
951  return ((int)sizeof(signed char));
952  case NC_SHORT :
953  return ((int)sizeof(short));
954  case NC_INT :
955  return ((int)sizeof(int));
956  case NC_FLOAT :
957  return ((int)sizeof(float));
958  case NC_DOUBLE :
959  return ((int)sizeof(double));
960 
961  /* These can occur in netcdf-3 code */
962  case NC_UBYTE :
963  return ((int)sizeof(unsigned char));
964  case NC_USHORT :
965  return ((int)(sizeof(unsigned short)));
966  case NC_UINT :
967  return ((int)sizeof(unsigned int));
968  case NC_INT64 :
969  return ((int)sizeof(signed long long));
970  case NC_UINT64 :
971  return ((int)sizeof(unsigned long long));
972 #ifdef USE_NETCDF4
973  case NC_STRING :
974  return ((int)sizeof(char*));
975 #endif /*USE_NETCDF4*/
976 
977  default:
978  return -1;
979  }
980 }
981 
990 size_t
991 NC_atomictypelen(nc_type xtype)
992 {
993  size_t sz = 0;
994  switch(xtype) {
995  case NC_NAT: sz = 0; break;
996  case NC_BYTE: sz = sizeof(signed char); break;
997  case NC_CHAR: sz = sizeof(char); break;
998  case NC_SHORT: sz = sizeof(short); break;
999  case NC_INT: sz = sizeof(int); break;
1000  case NC_FLOAT: sz = sizeof(float); break;
1001  case NC_DOUBLE: sz = sizeof(double); break;
1002  case NC_INT64: sz = sizeof(signed long long); break;
1003  case NC_UBYTE: sz = sizeof(unsigned char); break;
1004  case NC_USHORT: sz = sizeof(unsigned short); break;
1005  case NC_UINT: sz = sizeof(unsigned int); break;
1006  case NC_UINT64: sz = sizeof(unsigned long long); break;
1007 #ifdef USE_NETCDF4
1008  case NC_STRING: sz = sizeof(char*); break;
1009 #endif
1010  default: break;
1011  }
1012  return sz;
1013 }
1014 
1023 char *
1024 NC_atomictypename(nc_type xtype)
1025 {
1026  char* nm = NULL;
1027  switch(xtype) {
1028  case NC_NAT: nm = "undefined"; break;
1029  case NC_BYTE: nm = "byte"; break;
1030  case NC_CHAR: nm = "char"; break;
1031  case NC_SHORT: nm = "short"; break;
1032  case NC_INT: nm = "int"; break;
1033  case NC_FLOAT: nm = "float"; break;
1034  case NC_DOUBLE: nm = "double"; break;
1035  case NC_INT64: nm = "int64"; break;
1036  case NC_UBYTE: nm = "ubyte"; break;
1037  case NC_USHORT: nm = "ushort"; break;
1038  case NC_UINT: nm = "uint"; break;
1039  case NC_UINT64: nm = "uint64"; break;
1040 #ifdef USE_NETCDF4
1041  case NC_STRING: nm = "string"; break;
1042 #endif
1043  default: break;
1044  }
1045  return nm;
1046 }
1047 
1065 int
1066 NC_getshape(int ncid, int varid, int ndims, size_t* shape)
1067 {
1068  int dimids[NC_MAX_VAR_DIMS];
1069  int i;
1070  int status = NC_NOERR;
1071 
1072  if ((status = nc_inq_vardimid(ncid, varid, dimids)))
1073  return status;
1074  for(i = 0; i < ndims; i++)
1075  if ((status = nc_inq_dimlen(ncid, dimids[i], &shape[i])))
1076  break;
1077 
1078  return status;
1079 }
1080 
1105 int
1106 NC_check_nulls(int ncid, int varid, const size_t *start, size_t **count,
1107  ptrdiff_t **stride)
1108 {
1109  int varndims;
1110  int stat;
1111 
1112  if ((stat = nc_inq_varndims(ncid, varid, &varndims)))
1113  return stat;
1114 
1115  /* For non-scalar vars, start is required. */
1116  if (!start && varndims)
1117  return NC_EINVALCOORDS;
1118 
1119  /* If count is NULL, assume full extent of var. */
1120  if (!*count)
1121  {
1122  if (!(*count = malloc(varndims * sizeof(size_t))))
1123  return NC_ENOMEM;
1124  if ((stat = NC_getshape(ncid, varid, varndims, *count)))
1125  {
1126  free(*count);
1127  *count = NULL;
1128  return stat;
1129  }
1130  }
1131 
1132  /* If stride is NULL, do nothing, if *stride is NULL use all
1133  * 1s. */
1134  if (stride && !*stride)
1135  {
1136  int i;
1137 
1138  if (!(*stride = malloc(varndims * sizeof(ptrdiff_t))))
1139  return NC_ENOMEM;
1140  for (i = 0; i < varndims; i++)
1141  (*stride)[i] = 1;
1142  }
1143 
1144  return NC_NOERR;
1145 }
1146 
1168 int
1169 nc_free_string(size_t len, char **data)
1170 {
1171  int i;
1172  for (i = 0; i < len; i++)
1173  free(data[i]);
1174  return NC_NOERR;
1175 }
1246 int
1247 nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems,
1248  float preemption)
1249 {
1250  NC* ncp;
1251  int stat = NC_check_id(ncid, &ncp);
1252  if(stat != NC_NOERR) return stat;
1253  return ncp->dispatch->set_var_chunk_cache(ncid, varid, size,
1254  nelems, preemption);
1255 }
1256 
1287 int
1288 nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp,
1289  float *preemptionp)
1290 {
1291  NC* ncp;
1292  int stat = NC_check_id(ncid, &ncp);
1293  if(stat != NC_NOERR) return stat;
1294  return ncp->dispatch->get_var_chunk_cache(ncid, varid, sizep,
1295  nelemsp, preemptionp);
1296 }
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition: netcdf.h:409
#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:1288
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:212
#define NC_UBYTE
unsigned 1 byte int
Definition: netcdf.h:42
#define NC_MAX_VAR_DIMS
max per variable dimensions
Definition: netcdf.h:276
int nc_def_var_szip(int ncid, int varid, int options_mask, int pixels_per_block)
Set szip compression settings on a variable.
Definition: dvar.c:720
#define NC_UINT
unsigned 4-byte int
Definition: netcdf.h:44
#define NC_EINVALCOORDS
Index exceeds dimension bound.
Definition: netcdf.h:361
int nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
Set the zlib compression settings for a netCDF-4/HDF5 variable.
Definition: dvar.c:419
Main header file for the C API.
#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:202
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:1247
int nc_rename_var(int ncid, int varid, const char *name)
Rename a variable.
Definition: dvar.c:802
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:384
#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 storage and, if chunked storage is used, chunking parameters for a variable.
Definition: dvar.c:585
#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:1169
#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:663
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:620
#define NC_NOERR
No Error.
Definition: netcdf.h:329
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:304
#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:465

Return to the Main Unidata NetCDF page.
Generated on Tue Nov 17 2020 14:09:02 for NetCDF. NetCDF is a Unidata library.