NetCDF 4.8.0
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. */
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
211int
212nc_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
303int
304nc_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
455int
456nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
457{
458 NC* ncp;
459 int stat = NC_check_id(ncid,&ncp);
460 if(stat != NC_NOERR) return stat;
461 return ncp->dispatch->def_var_deflate(ncid,varid,shuffle,deflate,deflate_level);
462}
463
501int
502nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
503{
504 NC* ncp;
505 int stat = NC_check_id(ncid,&ncp);
506 if(stat != NC_NOERR) return stat;
507 return ncp->dispatch->def_var_fletcher32(ncid,varid,fletcher32);
508}
509
621int
622nc_def_var_chunking(int ncid, int varid, int storage, const size_t *chunksizesp)
623{
624 NC* ncp;
625 int stat = NC_check_id(ncid, &ncp);
626 if(stat != NC_NOERR) return stat;
627 return ncp->dispatch->def_var_chunking(ncid, varid, storage,
628 chunksizesp);
629}
630
699int
700nc_def_var_endian(int ncid, int varid, int endian)
701{
702 NC* ncp;
703 int stat = NC_check_id(ncid,&ncp);
704 if(stat != NC_NOERR) return stat;
705 return ncp->dispatch->def_var_endian(ncid,varid,endian);
706}
707
756int
757nc_def_var_szip(int ncid, int varid, int options_mask, int pixels_per_block)
758{
759 int ret;
760
761 /* This will cause H5Pset_szip to be called when the var is
762 * created. */
763 unsigned int params[2];
764 params[0] = options_mask;
765 params[1] = pixels_per_block;
766 if ((ret = nc_def_var_filter(ncid, varid, HDF5_FILTER_SZIP, 2, params)))
767 return ret;
768
769 return NC_NOERR;
770}
771
838int
839nc_rename_var(int ncid, int varid, const char *name)
840{
841 NC* ncp;
842 int stat = NC_check_id(ncid, &ncp);
843 if(stat != NC_NOERR) return stat;
844 TRACE(nc_rename_var);
845 return ncp->dispatch->rename_var(ncid, varid, name);
846}
858int
859NC_is_recvar(int ncid, int varid, size_t* nrecs)
860{
861 int status = NC_NOERR;
862 int unlimid;
863 int ndims;
864 int dimset[NC_MAX_VAR_DIMS];
865
866 status = nc_inq_unlimdim(ncid,&unlimid);
867 if(status != NC_NOERR) return 0; /* no unlimited defined */
868 status = nc_inq_varndims(ncid,varid,&ndims);
869 if(status != NC_NOERR) return 0; /* no unlimited defined */
870 if(ndims == 0) return 0; /* scalar */
871 status = nc_inq_vardimid(ncid,varid,dimset);
872 if(status != NC_NOERR) return 0; /* no unlimited defined */
873 status = nc_inq_dim(ncid,dimset[0],NULL,nrecs);
874 if(status != NC_NOERR) return 0;
875 return (dimset[0] == unlimid ? 1: 0);
876}
877
903int
904NC_inq_recvar(int ncid, int varid, int* nrecdimsp, int *is_recdim)
905{
906 int status = NC_NOERR;
907 int unlimid;
908 int nvardims;
909 int dimset[NC_MAX_VAR_DIMS];
910 int dim;
911 int nrecdims = 0;
912
913 status = nc_inq_varndims(ncid,varid,&nvardims);
914 if(status != NC_NOERR) return status;
915 if(nvardims == 0) return NC_NOERR; /* scalars have no dims */
916 for(dim = 0; dim < nvardims; dim++)
917 is_recdim[dim] = 0;
918 status = nc_inq_unlimdim(ncid, &unlimid);
919 if(status != NC_NOERR) return status;
920 if(unlimid == -1) return status; /* no unlimited dims for any variables */
921#ifdef USE_NETCDF4
922 {
923 int nunlimdims;
924 int *unlimids;
925 int recdim;
926 status = nc_inq_unlimdims(ncid, &nunlimdims, NULL); /* for group or file, not variable */
927 if(status != NC_NOERR) return status;
928 if(nunlimdims == 0) return status;
929
930 if (!(unlimids = malloc(nunlimdims * sizeof(int))))
931 return NC_ENOMEM;
932 status = nc_inq_unlimdims(ncid, &nunlimdims, unlimids); /* for group or file, not variable */
933 if(status != NC_NOERR) {
934 free(unlimids);
935 return status;
936 }
937 status = nc_inq_vardimid(ncid, varid, dimset);
938 if(status != NC_NOERR) {
939 free(unlimids);
940 return status;
941 }
942 for (dim = 0; dim < nvardims; dim++) { /* netCDF-4 rec dims need not be first dim for a rec var */
943 for(recdim = 0; recdim < nunlimdims; recdim++) {
944 if(dimset[dim] == unlimids[recdim]) {
945 is_recdim[dim] = 1;
946 nrecdims++;
947 }
948 }
949 }
950 free(unlimids);
951 }
952#else
953 status = nc_inq_vardimid(ncid, varid, dimset);
954 if(status != NC_NOERR) return status;
955 if(dimset[0] == unlimid) {
956 is_recdim[0] = 1;
957 nrecdims++;
958 }
959#endif /* USE_NETCDF4 */
960 if(nrecdimsp) *nrecdimsp = nrecdims;
961 return status;
962}
963
964/* Ok to use NC pointers because
965 all IOSP's will use that structure,
966 but not ok to use e.g. NC_Var pointers
967 because they may be different structure
968 entirely.
969*/
970
981int
983{
984 switch(type){
985 case NC_CHAR :
986 return ((int)sizeof(char));
987 case NC_BYTE :
988 return ((int)sizeof(signed char));
989 case NC_SHORT :
990 return ((int)sizeof(short));
991 case NC_INT :
992 return ((int)sizeof(int));
993 case NC_FLOAT :
994 return ((int)sizeof(float));
995 case NC_DOUBLE :
996 return ((int)sizeof(double));
997
998 /* These can occur in netcdf-3 code */
999 case NC_UBYTE :
1000 return ((int)sizeof(unsigned char));
1001 case NC_USHORT :
1002 return ((int)(sizeof(unsigned short)));
1003 case NC_UINT :
1004 return ((int)sizeof(unsigned int));
1005 case NC_INT64 :
1006 return ((int)sizeof(signed long long));
1007 case NC_UINT64 :
1008 return ((int)sizeof(unsigned long long));
1009#ifdef USE_NETCDF4
1010 case NC_STRING :
1011 return ((int)sizeof(char*));
1012#endif /*USE_NETCDF4*/
1013
1014 default:
1015 return -1;
1016 }
1017}
1018
1027size_t
1029{
1030 size_t sz = 0;
1031 switch(xtype) {
1032 case NC_NAT: sz = 0; break;
1033 case NC_BYTE: sz = sizeof(signed char); break;
1034 case NC_CHAR: sz = sizeof(char); break;
1035 case NC_SHORT: sz = sizeof(short); break;
1036 case NC_INT: sz = sizeof(int); break;
1037 case NC_FLOAT: sz = sizeof(float); break;
1038 case NC_DOUBLE: sz = sizeof(double); break;
1039 case NC_INT64: sz = sizeof(signed long long); break;
1040 case NC_UBYTE: sz = sizeof(unsigned char); break;
1041 case NC_USHORT: sz = sizeof(unsigned short); break;
1042 case NC_UINT: sz = sizeof(unsigned int); break;
1043 case NC_UINT64: sz = sizeof(unsigned long long); break;
1044#ifdef USE_NETCDF4
1045 case NC_STRING: sz = sizeof(char*); break;
1046#endif
1047 default: break;
1048 }
1049 return sz;
1050}
1051
1060char *
1062{
1063 char* nm = NULL;
1064 switch(xtype) {
1065 case NC_NAT: nm = "undefined"; break;
1066 case NC_BYTE: nm = "byte"; break;
1067 case NC_CHAR: nm = "char"; break;
1068 case NC_SHORT: nm = "short"; break;
1069 case NC_INT: nm = "int"; break;
1070 case NC_FLOAT: nm = "float"; break;
1071 case NC_DOUBLE: nm = "double"; break;
1072 case NC_INT64: nm = "int64"; break;
1073 case NC_UBYTE: nm = "ubyte"; break;
1074 case NC_USHORT: nm = "ushort"; break;
1075 case NC_UINT: nm = "uint"; break;
1076 case NC_UINT64: nm = "uint64"; break;
1077#ifdef USE_NETCDF4
1078 case NC_STRING: nm = "string"; break;
1079#endif
1080 default: break;
1081 }
1082 return nm;
1083}
1084
1102int
1103NC_getshape(int ncid, int varid, int ndims, size_t* shape)
1104{
1105 int dimids[NC_MAX_VAR_DIMS];
1106 int i;
1107 int status = NC_NOERR;
1108
1109 if ((status = nc_inq_vardimid(ncid, varid, dimids)))
1110 return status;
1111 for(i = 0; i < ndims; i++)
1112 if ((status = nc_inq_dimlen(ncid, dimids[i], &shape[i])))
1113 break;
1114
1115 return status;
1116}
1117
1142int
1143NC_check_nulls(int ncid, int varid, const size_t *start, size_t **count,
1144 ptrdiff_t **stride)
1145{
1146 int varndims;
1147 int stat;
1148
1149 if ((stat = nc_inq_varndims(ncid, varid, &varndims)))
1150 return stat;
1151
1152 /* For non-scalar vars, start is required. */
1153 if (!start && varndims)
1154 return NC_EINVALCOORDS;
1155
1156 /* If count is NULL, assume full extent of var. */
1157 if (!*count)
1158 {
1159 if (!(*count = malloc(varndims * sizeof(size_t))))
1160 return NC_ENOMEM;
1161 if ((stat = NC_getshape(ncid, varid, varndims, *count)))
1162 {
1163 free(*count);
1164 *count = NULL;
1165 return stat;
1166 }
1167 }
1168
1169 /* If stride is NULL, do nothing, if *stride is NULL use all
1170 * 1s. */
1171 if (stride && !*stride)
1172 {
1173 int i;
1174
1175 if (!(*stride = malloc(varndims * sizeof(ptrdiff_t))))
1176 return NC_ENOMEM;
1177 for (i = 0; i < varndims; i++)
1178 (*stride)[i] = 1;
1179 }
1180
1181 return NC_NOERR;
1182}
1183
1205int
1206nc_free_string(size_t len, char **data)
1207{
1208 int i;
1209 for (i = 0; i < len; i++)
1210 free(data[i]);
1211 return NC_NOERR;
1212}
1283int
1284nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems,
1285 float preemption)
1286{
1287 NC* ncp;
1288 int stat = NC_check_id(ncid, &ncp);
1289 if(stat != NC_NOERR) return stat;
1290 return ncp->dispatch->set_var_chunk_cache(ncid, varid, size,
1291 nelems, preemption);
1292}
1293
1324int
1325nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp,
1326 float *preemptionp)
1327{
1328 NC* ncp;
1329 int stat = NC_check_id(ncid, &ncp);
1330 if(stat != NC_NOERR) return stat;
1331 return ncp->dispatch->get_var_chunk_cache(ncid, varid, sizep,
1332 nelemsp, preemptionp);
1333}
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_is_recvar(int ncid, int varid, size_t *nrecs)
Definition dvar.c:859
int nc_def_var_endian(int ncid, int varid, int endian)
Define endianness of a variable.
Definition dvar.c:700
int nc_free_string(size_t len, char **data)
Free string space allocated by the library.
Definition dvar.c:1206
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:757
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:1284
char * NC_atomictypename(nc_type xtype)
Definition dvar.c:1061
size_t NC_atomictypelen(nc_type xtype)
Definition dvar.c:1028
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:456
int NC_getshape(int ncid, int varid, int ndims, size_t *shape)
Definition dvar.c:1103
int nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
Set checksum for a var.
Definition dvar.c:502
int NC_check_nulls(int ncid, int varid, const size_t *start, size_t **count, ptrdiff_t **stride)
Definition dvar.c:1143
int nctypelen(nc_type type)
Definition dvar.c:982
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:304
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
int NC_inq_recvar(int ncid, int varid, int *nrecdimsp, int *is_recdim)
Definition dvar.c:904
int nc_rename_var(int ncid, int varid, const char *name)
Rename a variable.
Definition dvar.c:839
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:622
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:1325
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:277
#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:642
#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:410
#define NC_SHORT
signed 2 byte integer
Definition netcdf.h:37
#define NC_EINVALCOORDS
Index exceeds dimension bound.
Definition netcdf.h:362
#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:249
#define NC_UINT64
unsigned 8-byte int
Definition netcdf.h:46
#define NC_NOERR
No Error.
Definition netcdf.h:330
#define NC_USHORT
unsigned 2-byte int
Definition netcdf.h:43
#define NC_STRING
string
Definition netcdf.h:47
#define NC_EGLOBAL
Action prohibited on NC_GLOBAL varid.
Definition netcdf.h:385
#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