Loading...
Searching...
No Matches
dvar.c
Go to the documentation of this file.
/* Copyright 2010-2018 University Corporation for Atmospheric
Research/Unidata. See COPYRIGHT file for more info. */
#include "config.h"
#include "netcdf.h"
#include "netcdf_filter.h"
#include "ncdispatch.h"
#include "nc4internal.h"
#include "netcdf_f.h"
#include "nc4internal.h"
int
nc_def_var(int ncid, const char *name, nc_type xtype,
int ndims, const int *dimidsp, int *varidp)
{
NC* ncp;
int stat = NC_NOERR;
if ((stat = NC_check_id(ncid, &ncp)))
return stat;
TRACE(nc_def_var);
return ncp->dispatch->def_var(ncid, name, xtype, ndims,
dimidsp, varidp);
}
int
nc_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value)
{
NC* ncp;
int stat = NC_check_id(ncid,&ncp);
if(stat != NC_NOERR) return stat;
/* Using NC_GLOBAL is illegal, as this API has no provision for
* specifying the type of the fillvalue, it must of necessity be
* using the type of the variable to interpret the bytes of the
* fill_value argument. */
if (varid == NC_GLOBAL) return NC_EGLOBAL;
return ncp->dispatch->def_var_fill(ncid,varid,no_fill,fill_value);
}
int
nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
{
NC* ncp;
int stat = NC_check_id(ncid,&ncp);
if(stat != NC_NOERR) return stat;
return ncp->dispatch->def_var_deflate(ncid,varid,shuffle,deflate,deflate_level);
}
int
nc_def_var_quantize(int ncid, int varid, int quantize_mode, int nsd)
{
NC* ncp;
int stat = NC_check_id(ncid,&ncp);
if(stat != NC_NOERR) return stat;
/* Using NC_GLOBAL is illegal. */
if (varid == NC_GLOBAL) return NC_EGLOBAL;
return ncp->dispatch->def_var_quantize(ncid,varid,quantize_mode,nsd);
}
int
nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
{
NC* ncp;
int stat = NC_check_id(ncid,&ncp);
if(stat != NC_NOERR) return stat;
return ncp->dispatch->def_var_fletcher32(ncid,varid,fletcher32);
}
int
nc_def_var_chunking(int ncid, int varid, int storage, const size_t *chunksizesp)
{
NC* ncp;
int stat = NC_check_id(ncid, &ncp);
if(stat != NC_NOERR) return stat;
return ncp->dispatch->def_var_chunking(ncid, varid, storage,
chunksizesp);
}
int
nc_def_var_endian(int ncid, int varid, int endian)
{
NC* ncp;
int stat = NC_check_id(ncid,&ncp);
if(stat != NC_NOERR) return stat;
return ncp->dispatch->def_var_endian(ncid,varid,endian);
}
int
nc_def_var_szip(int ncid, int varid, int options_mask, int pixels_per_block)
{
int ret;
/* This will cause H5Pset_szip to be called when the var is
* created. */
unsigned int params[2] = {(unsigned int)options_mask, (unsigned int)pixels_per_block};
if ((ret = nc_def_var_filter(ncid, varid, H5Z_FILTER_SZIP, 2, params)))
return ret;
return NC_NOERR;
}
int
nc_rename_var(int ncid, int varid, const char *name)
{
NC* ncp;
int stat = NC_check_id(ncid, &ncp);
if(stat != NC_NOERR) return stat;
TRACE(nc_rename_var);
return ncp->dispatch->rename_var(ncid, varid, name);
}
int
NC_is_recvar(int ncid, int varid, size_t* nrecs)
{
int status = NC_NOERR;
int unlimid;
int ndims;
int dimset[NC_MAX_VAR_DIMS];
status = nc_inq_unlimdim(ncid,&unlimid);
if(status != NC_NOERR) return 0; /* no unlimited defined */
status = nc_inq_varndims(ncid,varid,&ndims);
if(status != NC_NOERR) return 0; /* no unlimited defined */
if(ndims == 0) return 0; /* scalar */
status = nc_inq_vardimid(ncid,varid,dimset);
if(status != NC_NOERR) return 0; /* no unlimited defined */
status = nc_inq_dim(ncid,dimset[0],NULL,nrecs);
if(status != NC_NOERR) return 0;
return (dimset[0] == unlimid ? 1: 0);
}
int
NC_inq_recvar(int ncid, int varid, int* nrecdimsp, int *is_recdim)
{
int status = NC_NOERR;
int unlimid;
int nvardims;
int dimset[NC_MAX_VAR_DIMS];
int dim;
int nrecdims = 0;
status = nc_inq_varndims(ncid,varid,&nvardims);
if(status != NC_NOERR) return status;
if(nvardims == 0) return NC_NOERR; /* scalars have no dims */
for(dim = 0; dim < nvardims; dim++)
is_recdim[dim] = 0;
status = nc_inq_unlimdim(ncid, &unlimid);
if(status != NC_NOERR) return status;
if(unlimid == -1) return status; /* no unlimited dims for any variables */
#ifdef USE_NETCDF4
{
int nunlimdims;
int *unlimids;
int recdim;
status = nc_inq_unlimdims(ncid, &nunlimdims, NULL); /* for group or file, not variable */
if(status != NC_NOERR) return status;
if(nunlimdims == 0) return status;
if (!(unlimids = malloc((size_t)nunlimdims * sizeof(int))))
return NC_ENOMEM;
status = nc_inq_unlimdims(ncid, &nunlimdims, unlimids); /* for group or file, not variable */
if(status != NC_NOERR) {
free(unlimids);
return status;
}
status = nc_inq_vardimid(ncid, varid, dimset);
if(status != NC_NOERR) {
free(unlimids);
return status;
}
for (dim = 0; dim < nvardims; dim++) { /* netCDF-4 rec dims need not be first dim for a rec var */
for(recdim = 0; recdim < nunlimdims; recdim++) {
if(dimset[dim] == unlimids[recdim]) {
is_recdim[dim] = 1;
nrecdims++;
}
}
}
free(unlimids);
}
#else
status = nc_inq_vardimid(ncid, varid, dimset);
if(status != NC_NOERR) return status;
if(dimset[0] == unlimid) {
is_recdim[0] = 1;
nrecdims++;
}
#endif /* USE_NETCDF4 */
if(nrecdimsp) *nrecdimsp = nrecdims;
return status;
}
/* Ok to use NC pointers because
all IOSP's will use that structure,
but not ok to use e.g. NC_Var pointers
because they may be different structure
entirely.
*/
int
nctypelen(nc_type type)
{
switch(type){
case NC_CHAR :
return ((int)sizeof(char));
case NC_BYTE :
return ((int)sizeof(signed char));
case NC_SHORT :
return ((int)sizeof(short));
case NC_INT :
return ((int)sizeof(int));
case NC_FLOAT :
return ((int)sizeof(float));
case NC_DOUBLE :
return ((int)sizeof(double));
/* These can occur in netcdf-3 code */
case NC_UBYTE :
return ((int)sizeof(unsigned char));
case NC_USHORT :
return ((int)(sizeof(unsigned short)));
case NC_UINT :
return ((int)sizeof(unsigned int));
case NC_INT64 :
return ((int)sizeof(signed long long));
case NC_UINT64 :
return ((int)sizeof(unsigned long long));
#ifdef USE_NETCDF4
case NC_STRING :
return ((int)sizeof(char*));
#endif /*USE_NETCDF4*/
default:
return -1;
}
}
size_t
NC_atomictypelen(nc_type xtype)
{
size_t sz = 0;
switch(xtype) {
case NC_NAT: sz = 0; break;
case NC_BYTE: sz = sizeof(signed char); break;
case NC_CHAR: sz = sizeof(char); break;
case NC_SHORT: sz = sizeof(short); break;
case NC_INT: sz = sizeof(int); break;
case NC_FLOAT: sz = sizeof(float); break;
case NC_DOUBLE: sz = sizeof(double); break;
case NC_INT64: sz = sizeof(signed long long); break;
case NC_UBYTE: sz = sizeof(unsigned char); break;
case NC_USHORT: sz = sizeof(unsigned short); break;
case NC_UINT: sz = sizeof(unsigned int); break;
case NC_UINT64: sz = sizeof(unsigned long long); break;
#ifdef USE_NETCDF4
case NC_STRING: sz = sizeof(char*); break;
#endif
default: break;
}
return sz;
}
char *
NC_atomictypename(nc_type xtype)
{
char* nm = NULL;
switch(xtype) {
case NC_NAT: nm = "undefined"; break;
case NC_BYTE: nm = "byte"; break;
case NC_CHAR: nm = "char"; break;
case NC_SHORT: nm = "short"; break;
case NC_INT: nm = "int"; break;
case NC_FLOAT: nm = "float"; break;
case NC_DOUBLE: nm = "double"; break;
case NC_INT64: nm = "int64"; break;
case NC_UBYTE: nm = "ubyte"; break;
case NC_USHORT: nm = "ushort"; break;
case NC_UINT: nm = "uint"; break;
case NC_UINT64: nm = "uint64"; break;
#ifdef USE_NETCDF4
case NC_STRING: nm = "string"; break;
#endif
default: break;
}
return nm;
}
int
NC_getshape(int ncid, int varid, int ndims, size_t* shape)
{
int dimids[NC_MAX_VAR_DIMS];
int i;
int status = NC_NOERR;
if ((status = nc_inq_vardimid(ncid, varid, dimids)))
return status;
for(i = 0; i < ndims; i++)
if ((status = nc_inq_dimlen(ncid, dimids[i], &shape[i])))
break;
return status;
}
int
NC_check_nulls(int ncid, int varid, const size_t *start, size_t **count,
ptrdiff_t **stride)
{
int varndims;
int stat;
if ((stat = nc_inq_varndims(ncid, varid, &varndims)))
return stat;
/* For non-scalar vars, start is required. */
if (!start && varndims)
return NC_EINVALCOORDS;
/* If count is NULL, assume full extent of var. */
if (!*count)
{
if (!(*count = malloc((size_t)varndims * sizeof(size_t))))
return NC_ENOMEM;
if ((stat = NC_getshape(ncid, varid, varndims, *count)))
{
free(*count);
*count = NULL;
return stat;
}
}
/* If stride is NULL, do nothing, if *stride is NULL use all
* 1s. */
if (stride && !*stride)
{
int i;
if (!(*stride = malloc((size_t)varndims * sizeof(ptrdiff_t))))
return NC_ENOMEM;
for (i = 0; i < varndims; i++)
(*stride)[i] = 1;
}
return NC_NOERR;
}
int
nc_free_string(size_t len, char **data)
{
size_t i;
for (i = 0; i < len; i++)
free(data[i]);
return NC_NOERR;
}
int
nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems,
float preemption)
{
NC* ncp;
int stat = NC_check_id(ncid, &ncp);
if(stat != NC_NOERR) return stat;
return ncp->dispatch->set_var_chunk_cache(ncid, varid, size,
nelems, preemption);
}
int
nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp,
float *preemptionp)
{
NC* ncp;
int stat = NC_check_id(ncid, &ncp);
if(stat != NC_NOERR) return stat;
return ncp->dispatch->get_var_chunk_cache(ncid, varid, sizep,
nelemsp, preemptionp);
}
#ifndef USE_NETCDF4
/* Make sure the fortran API is defined, even if it only returns errors */
int
nc_set_chunk_cache_ints(int size, int nelems, int preemption)
{
return NC_ENOTBUILT;
}
int
nc_get_chunk_cache_ints(int *sizep, int *nelemsp, int *preemptionp)
{
return NC_ENOTBUILT;
}
int
nc_set_var_chunk_cache_ints(int ncid, int varid, int size, int nelems,
int preemption)
{
return NC_ENOTBUILT;
}
int
nc_get_var_chunk_cache_ints(int ncid, int varid, int *sizep,
int *nelemsp, int *preemptionp)
{
return NC_ENOTBUILT;
}
#endif /*USE_NETCDF4*/
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_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_unlimdim
EXTERNL int nc_inq_unlimdim(int ncid, int *unlimdimidp)
Find the ID of the unlimited dimension.
Definition ddim.c:350
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:202
nc_def_var_endian
int nc_def_var_endian(int ncid, int varid, int endian)
Define endianness of a variable.
Definition dvar.c:810
nc_free_string
int nc_free_string(size_t len, char **data)
Free string space allocated by the library.
Definition dvar.c:1320
nc_def_var_szip
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
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:1398
nc_def_var_deflate
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
nc_def_var_fletcher32
int nc_def_var_fletcher32(int ncid, int varid, int fletcher32)
Set checksum for a var.
Definition dvar.c:612
nc_def_var_quantize
int nc_def_var_quantize(int ncid, int varid, int quantize_mode, int nsd)
Turn on quantization for a variable.
Definition dvar.c:563
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:225
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:311
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:216
nc_rename_var
int nc_rename_var(int ncid, int varid, const char *name)
Rename a variable.
Definition dvar.c:948
nc_def_var_chunking
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
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:1439
netcdf.h
Main header file for the C API.
NC_UINT
#define NC_UINT
unsigned 4-byte int
Definition netcdf.h:44
NC_INT
#define NC_INT
signed 4 byte integer
Definition netcdf.h:38
NC_MAX_VAR_DIMS
#define NC_MAX_VAR_DIMS
max per variable dimensions
Definition netcdf.h:331
NC_BYTE
#define NC_BYTE
signed 1 byte integer
Definition netcdf.h:35
nc_inq_unlimdims
EXTERNL int nc_inq_unlimdims(int ncid, int *nunlimdimsp, int *unlimdimidsp)
Return number and list of unlimited dimensions.
Definition dvarinq.c:673
NC_NAT
#define NC_NAT
Not A Type.
Definition netcdf.h:34
NC_DOUBLE
#define NC_DOUBLE
double precision floating point number
Definition netcdf.h:41
NC_UBYTE
#define NC_UBYTE
unsigned 1 byte int
Definition netcdf.h:42
NC_FLOAT
#define NC_FLOAT
single precision floating point number
Definition netcdf.h:40
NC_ENOMEM
#define NC_ENOMEM
Memory allocation (malloc) failure.
Definition netcdf.h:497
NC_SHORT
#define NC_SHORT
signed 2 byte integer
Definition netcdf.h:37
NC_EINVALCOORDS
#define NC_EINVALCOORDS
Index exceeds dimension bound.
Definition netcdf.h:449
NC_INT64
#define NC_INT64
signed 8-byte int
Definition netcdf.h:45
NC_GLOBAL
#define NC_GLOBAL
Attribute id to put/get a global attribute.
Definition netcdf.h:303
NC_UINT64
#define NC_UINT64
unsigned 8-byte int
Definition netcdf.h:46
NC_NOERR
#define NC_NOERR
No Error.
Definition netcdf.h:417
nc_def_var_filter
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
NC_USHORT
#define NC_USHORT
unsigned 2-byte int
Definition netcdf.h:43
NC_STRING
#define NC_STRING
string
Definition netcdf.h:47
NC_ENOTBUILT
#define NC_ENOTBUILT
Attempt to use feature that was not turned on when netCDF was built.
Definition netcdf.h:557
NC_EGLOBAL
#define NC_EGLOBAL
Action prohibited on NC_GLOBAL varid.
Definition netcdf.h:472
NC_CHAR
#define NC_CHAR
ISO/ASCII character.
Definition netcdf.h:36
nc_type
int nc_type
The nc_type type is just an int.
Definition netcdf.h:25
netcdf_filter.h