NetCDF 4.10.1
Loading...
Searching...
No Matches
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. */
10
11#include "config.h"
12#include "netcdf.h"
13#include "netcdf_filter.h"
14#include "ncdispatch.h"
15#include "nc4internal.h"
16#include "netcdf_f.h"
17#include "nc4internal.h"
18
132
140
215int
216nc_def_var(int ncid, const char *name, nc_type xtype,
217 int ndims, const int *dimidsp, int *varidp)
218{
219 NC* ncp;
220 int stat = NC_NOERR;
221
222 if ((stat = NC_check_id(ncid, &ncp)))
223 return stat;
224 TRACE(nc_def_var);
225 return ncp->dispatch->def_var(ncid, name, xtype, ndims,
226 dimidsp, varidp);
227}
228
310int
311nc_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value)
312{
313 NC* ncp;
314 int stat = NC_check_id(ncid,&ncp);
315 if(stat != NC_NOERR) return stat;
316
317 /* Using NC_GLOBAL is illegal, as this API has no provision for
318 * specifying the type of the fillvalue, it must of necessity be
319 * using the type of the variable to interpret the bytes of the
320 * fill_value argument. */
321 if (varid == NC_GLOBAL) return NC_EGLOBAL;
322
323 return ncp->dispatch->def_var_fill(ncid,varid,no_fill,fill_value);
324}
325
462int
463nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
464{
465 NC* ncp;
466 int stat = NC_check_id(ncid,&ncp);
467 if(stat != NC_NOERR) return stat;
468 return ncp->dispatch->def_var_deflate(ncid,varid,shuffle,deflate,deflate_level);
469}
470
562int
563nc_def_var_quantize(int ncid, int varid, int quantize_mode, int nsd)
564{
565 NC* ncp;
566 int stat = NC_check_id(ncid,&ncp);
567 if(stat != NC_NOERR) return stat;
568
569 /* Using NC_GLOBAL is illegal. */
570 if (varid == NC_GLOBAL) return NC_EGLOBAL;
571 return ncp->dispatch->def_var_quantize(ncid,varid,quantize_mode,nsd);
572}
573
611int
612nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
613{
614 NC* ncp;
615 int stat = NC_check_id(ncid,&ncp);
616 if(stat != NC_NOERR) return stat;
617 return ncp->dispatch->def_var_fletcher32(ncid,varid,fletcher32);
618}
619
731int
732nc_def_var_chunking(int ncid, int varid, int storage, const size_t *chunksizesp)
733{
734 NC* ncp;
735 int stat = NC_check_id(ncid, &ncp);
736 if(stat != NC_NOERR) return stat;
737 return ncp->dispatch->def_var_chunking(ncid, varid, storage,
738 chunksizesp);
739}
740
809int
810nc_def_var_endian(int ncid, int varid, int endian)
811{
812 NC* ncp;
813 int stat = NC_check_id(ncid,&ncp);
814 if(stat != NC_NOERR) return stat;
815 return ncp->dispatch->def_var_endian(ncid,varid,endian);
816}
817
867int
868nc_def_var_szip(int ncid, int varid, int options_mask, int pixels_per_block)
869{
870 int ret;
871
872 /* This will cause H5Pset_szip to be called when the var is
873 * created. */
874 unsigned int params[2] = {(unsigned int)options_mask, (unsigned int)pixels_per_block};
875 if ((ret = nc_def_var_filter(ncid, varid, H5Z_FILTER_SZIP, 2, params)))
876 return ret;
877
878 return NC_NOERR;
879}
880
882
889
947int
948nc_rename_var(int ncid, int varid, const char *name)
949{
950 NC* ncp;
951 int stat = NC_check_id(ncid, &ncp);
952 if(stat != NC_NOERR) return stat;
953 TRACE(nc_rename_var);
954 return ncp->dispatch->rename_var(ncid, varid, name);
955}
956
957
967int
968NC_is_recvar(int ncid, int varid, size_t* nrecs)
969{
970 int status = NC_NOERR;
971 int unlimid;
972 int ndims;
973 int dimset[NC_MAX_VAR_DIMS];
974
975 status = nc_inq_unlimdim(ncid,&unlimid);
976 if(status != NC_NOERR) return 0; /* no unlimited defined */
977 status = nc_inq_varndims(ncid,varid,&ndims);
978 if(status != NC_NOERR) return 0; /* no unlimited defined */
979 if(ndims == 0) return 0; /* scalar */
980 status = nc_inq_vardimid(ncid,varid,dimset);
981 if(status != NC_NOERR) return 0; /* no unlimited defined */
982 status = nc_inq_dim(ncid,dimset[0],NULL,nrecs);
983 if(status != NC_NOERR) return 0;
984 return (dimset[0] == unlimid ? 1: 0);
985}
986
1012int
1013NC_inq_recvar(int ncid, int varid, int* nrecdimsp, int *is_recdim)
1014{
1015 int status = NC_NOERR;
1016 int unlimid;
1017 int nvardims;
1018 int dimset[NC_MAX_VAR_DIMS];
1019 int dim;
1020 int nrecdims = 0;
1021
1022 status = nc_inq_varndims(ncid,varid,&nvardims);
1023 if(status != NC_NOERR) return status;
1024 if(nvardims == 0) return NC_NOERR; /* scalars have no dims */
1025 for(dim = 0; dim < nvardims; dim++)
1026 is_recdim[dim] = 0;
1027 status = nc_inq_unlimdim(ncid, &unlimid);
1028 if(status != NC_NOERR) return status;
1029 if(unlimid == -1) return status; /* no unlimited dims for any variables */
1030#ifdef USE_NETCDF4
1031 {
1032 int nunlimdims;
1033 int *unlimids;
1034 int recdim;
1035 status = nc_inq_unlimdims(ncid, &nunlimdims, NULL); /* for group or file, not variable */
1036 if(status != NC_NOERR) return status;
1037 if(nunlimdims == 0) return status;
1038
1039 if (!(unlimids = malloc((size_t)nunlimdims * sizeof(int))))
1040 return NC_ENOMEM;
1041 status = nc_inq_unlimdims(ncid, &nunlimdims, unlimids); /* for group or file, not variable */
1042 if(status != NC_NOERR) {
1043 free(unlimids);
1044 return status;
1045 }
1046 status = nc_inq_vardimid(ncid, varid, dimset);
1047 if(status != NC_NOERR) {
1048 free(unlimids);
1049 return status;
1050 }
1051 for (dim = 0; dim < nvardims; dim++) { /* netCDF-4 rec dims need not be first dim for a rec var */
1052 for(recdim = 0; recdim < nunlimdims; recdim++) {
1053 if(dimset[dim] == unlimids[recdim]) {
1054 is_recdim[dim] = 1;
1055 nrecdims++;
1056 }
1057 }
1058 }
1059 free(unlimids);
1060 }
1061#else
1062 status = nc_inq_vardimid(ncid, varid, dimset);
1063 if(status != NC_NOERR) return status;
1064 if(dimset[0] == unlimid) {
1065 is_recdim[0] = 1;
1066 nrecdims++;
1067 }
1068#endif /* USE_NETCDF4 */
1069 if(nrecdimsp) *nrecdimsp = nrecdims;
1070 return status;
1071}
1072
1073/* Ok to use NC pointers because
1074 all IOSP's will use that structure,
1075 but not ok to use e.g. NC_Var pointers
1076 because they may be different structure
1077 entirely.
1078*/
1079
1090int
1091nctypelen(nc_type type)
1092{
1093 switch(type){
1094 case NC_CHAR :
1095 return ((int)sizeof(char));
1096 case NC_BYTE :
1097 return ((int)sizeof(signed char));
1098 case NC_SHORT :
1099 return ((int)sizeof(short));
1100 case NC_INT :
1101 return ((int)sizeof(int));
1102 case NC_FLOAT :
1103 return ((int)sizeof(float));
1104 case NC_DOUBLE :
1105 return ((int)sizeof(double));
1106
1107 /* These can occur in netcdf-3 code */
1108 case NC_UBYTE :
1109 return ((int)sizeof(unsigned char));
1110 case NC_USHORT :
1111 return ((int)(sizeof(unsigned short)));
1112 case NC_UINT :
1113 return ((int)sizeof(unsigned int));
1114 case NC_INT64 :
1115 return ((int)sizeof(signed long long));
1116 case NC_UINT64 :
1117 return ((int)sizeof(unsigned long long));
1118#ifdef USE_NETCDF4
1119 case NC_STRING :
1120 return ((int)sizeof(char*));
1121#endif /*USE_NETCDF4*/
1122
1123 default:
1124 return -1;
1125 }
1126}
1127
1136size_t
1137NC_atomictypelen(nc_type xtype)
1138{
1139 size_t sz = 0;
1140 switch(xtype) {
1141 case NC_NAT: sz = 0; break;
1142 case NC_BYTE: sz = sizeof(signed char); break;
1143 case NC_CHAR: sz = sizeof(char); break;
1144 case NC_SHORT: sz = sizeof(short); break;
1145 case NC_INT: sz = sizeof(int); break;
1146 case NC_FLOAT: sz = sizeof(float); break;
1147 case NC_DOUBLE: sz = sizeof(double); break;
1148 case NC_INT64: sz = sizeof(signed long long); break;
1149 case NC_UBYTE: sz = sizeof(unsigned char); break;
1150 case NC_USHORT: sz = sizeof(unsigned short); break;
1151 case NC_UINT: sz = sizeof(unsigned int); break;
1152 case NC_UINT64: sz = sizeof(unsigned long long); break;
1153#ifdef USE_NETCDF4
1154 case NC_STRING: sz = sizeof(char*); break;
1155#endif
1156 default: break;
1157 }
1158 return sz;
1159}
1160
1169char *
1170NC_atomictypename(nc_type xtype)
1171{
1172 char* nm = NULL;
1173 switch(xtype) {
1174 case NC_NAT: nm = "undefined"; break;
1175 case NC_BYTE: nm = "byte"; break;
1176 case NC_CHAR: nm = "char"; break;
1177 case NC_SHORT: nm = "short"; break;
1178 case NC_INT: nm = "int"; break;
1179 case NC_FLOAT: nm = "float"; break;
1180 case NC_DOUBLE: nm = "double"; break;
1181 case NC_INT64: nm = "int64"; break;
1182 case NC_UBYTE: nm = "ubyte"; break;
1183 case NC_USHORT: nm = "ushort"; break;
1184 case NC_UINT: nm = "uint"; break;
1185 case NC_UINT64: nm = "uint64"; break;
1186#ifdef USE_NETCDF4
1187 case NC_STRING: nm = "string"; break;
1188#endif
1189 default: break;
1190 }
1191 return nm;
1192}
1193
1211int
1212NC_getshape(int ncid, int varid, int ndims, size_t* shape)
1213{
1214 int dimids[NC_MAX_VAR_DIMS];
1215 int i;
1216 int status = NC_NOERR;
1217
1218 if ((status = nc_inq_vardimid(ncid, varid, dimids)))
1219 return status;
1220 for(i = 0; i < ndims; i++)
1221 if ((status = nc_inq_dimlen(ncid, dimids[i], &shape[i])))
1222 break;
1223
1224 return status;
1225}
1226
1254int
1255NC_check_nulls(int ncid, int varid, const size_t *start, size_t **count,
1256 ptrdiff_t **stride)
1257{
1258 int varndims;
1259 int stat;
1260
1261 if ((stat = nc_inq_varndims(ncid, varid, &varndims)))
1262 return stat;
1263
1264 /* For non-scalar vars, start is required. */
1265 if (!start && varndims)
1266 return NC_EINVALCOORDS;
1267
1268 /* If count is NULL, assume full extent of var. */
1269 if (!*count)
1270 {
1271 if (!(*count = malloc((size_t)varndims * sizeof(size_t))))
1272 return NC_ENOMEM;
1273 if ((stat = NC_getshape(ncid, varid, varndims, *count)))
1274 {
1275 free(*count);
1276 *count = NULL;
1277 return stat;
1278 }
1279 }
1280
1281 /* If stride is NULL, do nothing, if *stride is NULL use all
1282 * 1s. */
1283 if (stride && !*stride)
1284 {
1285 int i;
1286
1287 if (!(*stride = malloc((size_t)varndims * sizeof(ptrdiff_t))))
1288 return NC_ENOMEM;
1289 for (i = 0; i < varndims; i++)
1290 (*stride)[i] = 1;
1291 }
1292
1293 return NC_NOERR;
1294}
1295
1319int
1320nc_free_string(size_t len, char **data)
1321{
1322 size_t i;
1323 for (i = 0; i < len; i++)
1324 free(data[i]);
1325 return NC_NOERR;
1326}
1327
1328
1397int
1398nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems,
1399 float preemption)
1400{
1401 NC* ncp;
1402 int stat = NC_check_id(ncid, &ncp);
1403 if(stat != NC_NOERR) return stat;
1404 return ncp->dispatch->set_var_chunk_cache(ncid, varid, size,
1405 nelems, preemption);
1406}
1407
1438int
1439nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp,
1440 float *preemptionp)
1441{
1442 NC* ncp;
1443 int stat = NC_check_id(ncid, &ncp);
1444 if(stat != NC_NOERR) return stat;
1445 return ncp->dispatch->get_var_chunk_cache(ncid, varid, sizep,
1446 nelemsp, preemptionp);
1447}
1448
1449#ifndef USE_NETCDF4
1450/* Make sure the fortran API is defined, even if it only returns errors */
1451
1452int
1453nc_set_chunk_cache_ints(int size, int nelems, int preemption)
1454{
1455 return NC_ENOTBUILT;
1456}
1457
1458int
1459nc_get_chunk_cache_ints(int *sizep, int *nelemsp, int *preemptionp)
1460{
1461 return NC_ENOTBUILT;
1462}
1463
1464int
1465nc_set_var_chunk_cache_ints(int ncid, int varid, int size, int nelems,
1466 int preemption)
1467{
1468 return NC_ENOTBUILT;
1469}
1470
1471int
1472nc_get_var_chunk_cache_ints(int ncid, int varid, int *sizep,
1473 int *nelemsp, int *preemptionp)
1474{
1475 return NC_ENOTBUILT;
1476}
1477
1478#endif /*USE_NETCDF4*/
1479
EXTERNL int nc_inq_dimlen(int ncid, int dimid, size_t *lenp)
Find the length of a dimension.
Definition ddim.c:467
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_unlimdim(int ncid, int *unlimdimidp)
Find the ID of the unlimited dimension.
Definition ddim.c:350
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_def_var_endian(int ncid, int varid, int endian)
Define endianness of a variable.
Definition dvar.c:810
int nc_free_string(size_t len, char **data)
Free string space allocated by the library.
Definition dvar.c:1320
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:868
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:1398
int nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
Set the zlib compression and shuffle settings for a variable in an netCDF/HDF5 file.
Definition dvar.c:463
int nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
Set checksum for a var.
Definition dvar.c:612
int nc_def_var_quantize(int ncid, int varid, int quantize_mode, int nsd)
Turn on quantization for a variable.
Definition dvar.c:563
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_fill(int ncid, int varid, int no_fill, const void *fill_value)
Set the fill value for a variable.
Definition dvar.c:311
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:216
int nc_rename_var(int ncid, int varid, const char *name)
Rename a variable.
Definition dvar.c:948
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:732
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:1439
Main header file for the C API.
#define NC_UINT
unsigned 4-byte int
Definition netcdf.h:44
#define NC_INT
signed 4 byte integer
Definition netcdf.h:38
#define NC_MAX_VAR_DIMS
max per variable dimensions
Definition netcdf.h:331
#define NC_BYTE
signed 1 byte integer
Definition netcdf.h:35
EXTERNL int nc_inq_unlimdims(int ncid, int *nunlimdimsp, int *unlimdimidsp)
Return number and list of unlimited dimensions.
Definition dvarinq.c:673
#define NC_NAT
Not A Type.
Definition netcdf.h:34
#define NC_DOUBLE
double precision floating point number
Definition netcdf.h:41
#define NC_UBYTE
unsigned 1 byte int
Definition netcdf.h:42
#define NC_FLOAT
single precision floating point number
Definition netcdf.h:40
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition netcdf.h:497
#define NC_SHORT
signed 2 byte integer
Definition netcdf.h:37
#define NC_EINVALCOORDS
Index exceeds dimension bound.
Definition netcdf.h:449
#define NC_INT64
signed 8-byte int
Definition netcdf.h:45
#define NC_GLOBAL
Attribute id to put/get a global attribute.
Definition netcdf.h:303
#define NC_UINT64
unsigned 8-byte int
Definition netcdf.h:46
#define NC_NOERR
No Error.
Definition netcdf.h:417
EXTERNL int nc_def_var_filter(int ncid, int varid, unsigned int id, size_t nparams, const unsigned int *parms)
Define a new variable filter Assumes HDF5 format using unsigned ints.
Definition dfilter.c:129
#define NC_USHORT
unsigned 2-byte int
Definition netcdf.h:43
#define NC_STRING
string
Definition netcdf.h:47
#define NC_ENOTBUILT
Attempt to use feature that was not turned on when netCDF was built.
Definition netcdf.h:557
#define NC_EGLOBAL
Action prohibited on NC_GLOBAL varid.
Definition netcdf.h:472
#define NC_CHAR
ISO/ASCII character.
Definition netcdf.h:36
int nc_type
The nc_type type is just an int.
Definition netcdf.h:25