NetCDF 4.9.3
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. */
8
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
130
138
213int
214nc_def_var(int ncid, const char *name, nc_type xtype,
215 int ndims, const int *dimidsp, int *varidp)
216{
217 NC* ncp;
218 int stat = NC_NOERR;
219
220 if ((stat = NC_check_id(ncid, &ncp)))
221 return stat;
222 TRACE(nc_def_var);
223 return ncp->dispatch->def_var(ncid, name, xtype, ndims,
224 dimidsp, varidp);
225}
226
308int
309nc_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value)
310{
311 NC* ncp;
312 int stat = NC_check_id(ncid,&ncp);
313 if(stat != NC_NOERR) return stat;
314
315 /* Using NC_GLOBAL is illegal, as this API has no provision for
316 * specifying the type of the fillvalue, it must of necessity be
317 * using the type of the variable to interpret the bytes of the
318 * fill_value argument. */
319 if (varid == NC_GLOBAL) return NC_EGLOBAL;
320
321 return ncp->dispatch->def_var_fill(ncid,varid,no_fill,fill_value);
322}
323
460int
461nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
462{
463 NC* ncp;
464 int stat = NC_check_id(ncid,&ncp);
465 if(stat != NC_NOERR) return stat;
466 return ncp->dispatch->def_var_deflate(ncid,varid,shuffle,deflate,deflate_level);
467}
468
559int
560nc_def_var_quantize(int ncid, int varid, int quantize_mode, int nsd)
561{
562 NC* ncp;
563 int stat = NC_check_id(ncid,&ncp);
564 if(stat != NC_NOERR) return stat;
565
566 /* Using NC_GLOBAL is illegal. */
567 if (varid == NC_GLOBAL) return NC_EGLOBAL;
568 return ncp->dispatch->def_var_quantize(ncid,varid,quantize_mode,nsd);
569}
570
608int
609nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
610{
611 NC* ncp;
612 int stat = NC_check_id(ncid,&ncp);
613 if(stat != NC_NOERR) return stat;
614 return ncp->dispatch->def_var_fletcher32(ncid,varid,fletcher32);
615}
616
728int
729nc_def_var_chunking(int ncid, int varid, int storage, const size_t *chunksizesp)
730{
731 NC* ncp;
732 int stat = NC_check_id(ncid, &ncp);
733 if(stat != NC_NOERR) return stat;
734 return ncp->dispatch->def_var_chunking(ncid, varid, storage,
735 chunksizesp);
736}
737
806int
807nc_def_var_endian(int ncid, int varid, int endian)
808{
809 NC* ncp;
810 int stat = NC_check_id(ncid,&ncp);
811 if(stat != NC_NOERR) return stat;
812 return ncp->dispatch->def_var_endian(ncid,varid,endian);
813}
814
863int
864nc_def_var_szip(int ncid, int varid, int options_mask, int pixels_per_block)
865{
866 int ret;
867
868 /* This will cause H5Pset_szip to be called when the var is
869 * created. */
870 unsigned int params[2] = {(unsigned int)options_mask, (unsigned int)pixels_per_block};
871 if ((ret = nc_def_var_filter(ncid, varid, H5Z_FILTER_SZIP, 2, params)))
872 return ret;
873
874 return NC_NOERR;
875}
876
878
885
943int
944nc_rename_var(int ncid, int varid, const char *name)
945{
946 NC* ncp;
947 int stat = NC_check_id(ncid, &ncp);
948 if(stat != NC_NOERR) return stat;
949 TRACE(nc_rename_var);
950 return ncp->dispatch->rename_var(ncid, varid, name);
951}
952
953
963int
964NC_is_recvar(int ncid, int varid, size_t* nrecs)
965{
966 int status = NC_NOERR;
967 int unlimid;
968 int ndims;
969 int dimset[NC_MAX_VAR_DIMS];
970
971 status = nc_inq_unlimdim(ncid,&unlimid);
972 if(status != NC_NOERR) return 0; /* no unlimited defined */
973 status = nc_inq_varndims(ncid,varid,&ndims);
974 if(status != NC_NOERR) return 0; /* no unlimited defined */
975 if(ndims == 0) return 0; /* scalar */
976 status = nc_inq_vardimid(ncid,varid,dimset);
977 if(status != NC_NOERR) return 0; /* no unlimited defined */
978 status = nc_inq_dim(ncid,dimset[0],NULL,nrecs);
979 if(status != NC_NOERR) return 0;
980 return (dimset[0] == unlimid ? 1: 0);
981}
982
1008int
1009NC_inq_recvar(int ncid, int varid, int* nrecdimsp, int *is_recdim)
1010{
1011 int status = NC_NOERR;
1012 int unlimid;
1013 int nvardims;
1014 int dimset[NC_MAX_VAR_DIMS];
1015 int dim;
1016 int nrecdims = 0;
1017
1018 status = nc_inq_varndims(ncid,varid,&nvardims);
1019 if(status != NC_NOERR) return status;
1020 if(nvardims == 0) return NC_NOERR; /* scalars have no dims */
1021 for(dim = 0; dim < nvardims; dim++)
1022 is_recdim[dim] = 0;
1023 status = nc_inq_unlimdim(ncid, &unlimid);
1024 if(status != NC_NOERR) return status;
1025 if(unlimid == -1) return status; /* no unlimited dims for any variables */
1026#ifdef USE_NETCDF4
1027 {
1028 int nunlimdims;
1029 int *unlimids;
1030 int recdim;
1031 status = nc_inq_unlimdims(ncid, &nunlimdims, NULL); /* for group or file, not variable */
1032 if(status != NC_NOERR) return status;
1033 if(nunlimdims == 0) return status;
1034
1035 if (!(unlimids = malloc((size_t)nunlimdims * sizeof(int))))
1036 return NC_ENOMEM;
1037 status = nc_inq_unlimdims(ncid, &nunlimdims, unlimids); /* for group or file, not variable */
1038 if(status != NC_NOERR) {
1039 free(unlimids);
1040 return status;
1041 }
1042 status = nc_inq_vardimid(ncid, varid, dimset);
1043 if(status != NC_NOERR) {
1044 free(unlimids);
1045 return status;
1046 }
1047 for (dim = 0; dim < nvardims; dim++) { /* netCDF-4 rec dims need not be first dim for a rec var */
1048 for(recdim = 0; recdim < nunlimdims; recdim++) {
1049 if(dimset[dim] == unlimids[recdim]) {
1050 is_recdim[dim] = 1;
1051 nrecdims++;
1052 }
1053 }
1054 }
1055 free(unlimids);
1056 }
1057#else
1058 status = nc_inq_vardimid(ncid, varid, dimset);
1059 if(status != NC_NOERR) return status;
1060 if(dimset[0] == unlimid) {
1061 is_recdim[0] = 1;
1062 nrecdims++;
1063 }
1064#endif /* USE_NETCDF4 */
1065 if(nrecdimsp) *nrecdimsp = nrecdims;
1066 return status;
1067}
1068
1069/* Ok to use NC pointers because
1070 all IOSP's will use that structure,
1071 but not ok to use e.g. NC_Var pointers
1072 because they may be different structure
1073 entirely.
1074*/
1075
1086int
1087nctypelen(nc_type type)
1088{
1089 switch(type){
1090 case NC_CHAR :
1091 return ((int)sizeof(char));
1092 case NC_BYTE :
1093 return ((int)sizeof(signed char));
1094 case NC_SHORT :
1095 return ((int)sizeof(short));
1096 case NC_INT :
1097 return ((int)sizeof(int));
1098 case NC_FLOAT :
1099 return ((int)sizeof(float));
1100 case NC_DOUBLE :
1101 return ((int)sizeof(double));
1102
1103 /* These can occur in netcdf-3 code */
1104 case NC_UBYTE :
1105 return ((int)sizeof(unsigned char));
1106 case NC_USHORT :
1107 return ((int)(sizeof(unsigned short)));
1108 case NC_UINT :
1109 return ((int)sizeof(unsigned int));
1110 case NC_INT64 :
1111 return ((int)sizeof(signed long long));
1112 case NC_UINT64 :
1113 return ((int)sizeof(unsigned long long));
1114#ifdef USE_NETCDF4
1115 case NC_STRING :
1116 return ((int)sizeof(char*));
1117#endif /*USE_NETCDF4*/
1118
1119 default:
1120 return -1;
1121 }
1122}
1123
1132size_t
1133NC_atomictypelen(nc_type xtype)
1134{
1135 size_t sz = 0;
1136 switch(xtype) {
1137 case NC_NAT: sz = 0; break;
1138 case NC_BYTE: sz = sizeof(signed char); break;
1139 case NC_CHAR: sz = sizeof(char); break;
1140 case NC_SHORT: sz = sizeof(short); break;
1141 case NC_INT: sz = sizeof(int); break;
1142 case NC_FLOAT: sz = sizeof(float); break;
1143 case NC_DOUBLE: sz = sizeof(double); break;
1144 case NC_INT64: sz = sizeof(signed long long); break;
1145 case NC_UBYTE: sz = sizeof(unsigned char); break;
1146 case NC_USHORT: sz = sizeof(unsigned short); break;
1147 case NC_UINT: sz = sizeof(unsigned int); break;
1148 case NC_UINT64: sz = sizeof(unsigned long long); break;
1149#ifdef USE_NETCDF4
1150 case NC_STRING: sz = sizeof(char*); break;
1151#endif
1152 default: break;
1153 }
1154 return sz;
1155}
1156
1165char *
1166NC_atomictypename(nc_type xtype)
1167{
1168 char* nm = NULL;
1169 switch(xtype) {
1170 case NC_NAT: nm = "undefined"; break;
1171 case NC_BYTE: nm = "byte"; break;
1172 case NC_CHAR: nm = "char"; break;
1173 case NC_SHORT: nm = "short"; break;
1174 case NC_INT: nm = "int"; break;
1175 case NC_FLOAT: nm = "float"; break;
1176 case NC_DOUBLE: nm = "double"; break;
1177 case NC_INT64: nm = "int64"; break;
1178 case NC_UBYTE: nm = "ubyte"; break;
1179 case NC_USHORT: nm = "ushort"; break;
1180 case NC_UINT: nm = "uint"; break;
1181 case NC_UINT64: nm = "uint64"; break;
1182#ifdef USE_NETCDF4
1183 case NC_STRING: nm = "string"; break;
1184#endif
1185 default: break;
1186 }
1187 return nm;
1188}
1189
1207int
1208NC_getshape(int ncid, int varid, int ndims, size_t* shape)
1209{
1210 int dimids[NC_MAX_VAR_DIMS];
1211 int i;
1212 int status = NC_NOERR;
1213
1214 if ((status = nc_inq_vardimid(ncid, varid, dimids)))
1215 return status;
1216 for(i = 0; i < ndims; i++)
1217 if ((status = nc_inq_dimlen(ncid, dimids[i], &shape[i])))
1218 break;
1219
1220 return status;
1221}
1222
1250int
1251NC_check_nulls(int ncid, int varid, const size_t *start, size_t **count,
1252 ptrdiff_t **stride)
1253{
1254 int varndims;
1255 int stat;
1256
1257 if ((stat = nc_inq_varndims(ncid, varid, &varndims)))
1258 return stat;
1259
1260 /* For non-scalar vars, start is required. */
1261 if (!start && varndims)
1262 return NC_EINVALCOORDS;
1263
1264 /* If count is NULL, assume full extent of var. */
1265 if (!*count)
1266 {
1267 if (!(*count = malloc((size_t)varndims * sizeof(size_t))))
1268 return NC_ENOMEM;
1269 if ((stat = NC_getshape(ncid, varid, varndims, *count)))
1270 {
1271 free(*count);
1272 *count = NULL;
1273 return stat;
1274 }
1275 }
1276
1277 /* If stride is NULL, do nothing, if *stride is NULL use all
1278 * 1s. */
1279 if (stride && !*stride)
1280 {
1281 int i;
1282
1283 if (!(*stride = malloc((size_t)varndims * sizeof(ptrdiff_t))))
1284 return NC_ENOMEM;
1285 for (i = 0; i < varndims; i++)
1286 (*stride)[i] = 1;
1287 }
1288
1289 return NC_NOERR;
1290}
1291
1315int
1316nc_free_string(size_t len, char **data)
1317{
1318 size_t i;
1319 for (i = 0; i < len; i++)
1320 free(data[i]);
1321 return NC_NOERR;
1322}
1323
1324
1393int
1394nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems,
1395 float preemption)
1396{
1397 NC* ncp;
1398 int stat = NC_check_id(ncid, &ncp);
1399 if(stat != NC_NOERR) return stat;
1400 return ncp->dispatch->set_var_chunk_cache(ncid, varid, size,
1401 nelems, preemption);
1402}
1403
1434int
1435nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp,
1436 float *preemptionp)
1437{
1438 NC* ncp;
1439 int stat = NC_check_id(ncid, &ncp);
1440 if(stat != NC_NOERR) return stat;
1441 return ncp->dispatch->get_var_chunk_cache(ncid, varid, sizep,
1442 nelemsp, preemptionp);
1443}
1444
1445#ifndef USE_NETCDF4
1446/* Make sure the fortran API is defined, even if it only returns errors */
1447
1448int
1449nc_set_chunk_cache_ints(int size, int nelems, int preemption)
1450{
1451 return NC_ENOTBUILT;
1452}
1453
1454int
1455nc_get_chunk_cache_ints(int *sizep, int *nelemsp, int *preemptionp)
1456{
1457 return NC_ENOTBUILT;
1458}
1459
1460int
1461nc_set_var_chunk_cache_ints(int ncid, int varid, int size, int nelems,
1462 int preemption)
1463{
1464 return NC_ENOTBUILT;
1465}
1466
1467int
1468nc_get_var_chunk_cache_ints(int ncid, int varid, int *sizep,
1469 int *nelemsp, int *preemptionp)
1470{
1471 return NC_ENOTBUILT;
1472}
1473
1474#endif /*USE_NETCDF4*/
1475
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:807
int nc_free_string(size_t len, char **data)
Free string space allocated by the library.
Definition dvar.c:1316
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:864
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:1394
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:461
int nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
Set checksum for a var.
Definition dvar.c:609
int nc_def_var_quantize(int ncid, int varid, int quantize_mode, int nsd)
Turn on quantization for a variable.
Definition dvar.c:560
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:309
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:214
int nc_rename_var(int ncid, int varid, const char *name)
Rename a variable.
Definition dvar.c:944
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:729
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:1435
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:292
#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:672
#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:458
#define NC_SHORT
signed 2 byte integer
Definition netcdf.h:37
#define NC_EINVALCOORDS
Index exceeds dimension bound.
Definition netcdf.h:410
#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:264
#define NC_UINT64
unsigned 8-byte int
Definition netcdf.h:46
#define NC_NOERR
No Error.
Definition netcdf.h:378
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:126
#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:518
#define NC_EGLOBAL
Action prohibited on NC_GLOBAL varid.
Definition netcdf.h:433
#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