2017-03-09 08:01:10 +08:00
|
|
|
/*********************************************************************
|
2018-12-07 05:24:28 +08:00
|
|
|
* Copyright 2018, UCAR/Unidata
|
2017-03-09 08:01:10 +08:00
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "ncdispatch.h"
|
|
|
|
#include "ncd4dispatch.h"
|
|
|
|
#include "nc4internal.h"
|
|
|
|
#include "d4includes.h"
|
|
|
|
#include "d4odom.h"
|
|
|
|
|
|
|
|
/* Forward */
|
|
|
|
static int getvarx(int ncid, int varid, NCD4INFO**, NCD4node** varp, nc_type* xtypep, size_t*, nc_type* nc4typep, size_t*);
|
|
|
|
|
|
|
|
int
|
|
|
|
NCD4_get_vara(int ncid, int varid,
|
|
|
|
const size_t *start, const size_t *edges,
|
|
|
|
void *value,
|
|
|
|
nc_type memtype)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
/* TODO: optimize since we know stride is 1 */
|
2019-03-31 04:06:20 +08:00
|
|
|
ret = NCD4_get_vars(ncid,varid,start,edges,NC_stride_one,value,memtype);
|
2017-03-09 08:01:10 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
NCD4_get_vars(int ncid, int varid,
|
|
|
|
const size_t *start, const size_t *edges, const ptrdiff_t* stride,
|
|
|
|
void *memoryin, nc_type xtype)
|
|
|
|
{
|
|
|
|
int i,ret;
|
|
|
|
NCD4INFO* info;
|
|
|
|
NCD4meta* meta;
|
|
|
|
NCD4node* ncvar;
|
|
|
|
NCD4node* nctype;
|
|
|
|
D4odometer* odom = NULL;
|
|
|
|
nc_type nc4type;
|
2023-01-19 10:47:29 +08:00
|
|
|
size_t nc4size, xsize, dapsize;
|
2017-03-09 08:01:10 +08:00
|
|
|
void* instance = NULL; /* Staging area in case we have to convert */
|
|
|
|
NClist* blobs = NULL;
|
|
|
|
int rank;
|
|
|
|
size_t dimsizes[NC_MAX_VAR_DIMS];
|
|
|
|
d4size_t dimproduct;
|
|
|
|
size_t dstcount;
|
2022-11-16 11:29:21 +08:00
|
|
|
NCD4offset* offset = NULL;
|
2017-03-09 08:01:10 +08:00
|
|
|
|
2023-01-19 10:47:29 +08:00
|
|
|
/* Get netcdf type info */
|
2017-03-09 08:01:10 +08:00
|
|
|
if((ret=getvarx(ncid, varid, &info, &ncvar, &xtype, &xsize, &nc4type, &nc4size)))
|
2019-02-15 11:25:40 +08:00
|
|
|
{goto done;}
|
2017-03-09 08:01:10 +08:00
|
|
|
|
|
|
|
meta = info->substrate.metadata;
|
|
|
|
nctype = ncvar->basetype;
|
|
|
|
rank = nclistlength(ncvar->dims);
|
|
|
|
blobs = nclistnew();
|
|
|
|
|
2023-01-19 10:47:29 +08:00
|
|
|
/* Get the type's dapsize */
|
|
|
|
dapsize = nctype->meta.dapsize;
|
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
instance = malloc(nc4size);
|
|
|
|
if(instance == NULL)
|
|
|
|
{ret = THROW(NC_ENOMEM); goto done;}
|
|
|
|
|
|
|
|
dimproduct = NCD4_dimproduct(ncvar);
|
|
|
|
/* build size vector */
|
|
|
|
for(i=0;i<rank;i++) {
|
|
|
|
NCD4node* dim = nclistget(ncvar->dims,i);
|
|
|
|
dimsizes[i] = (size_t)dim->dim.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract and desired subset of data */
|
|
|
|
if(rank > 0)
|
|
|
|
odom = d4odom_new(rank,start,edges,stride,dimsizes);
|
|
|
|
else
|
|
|
|
odom = d4scalarodom_new();
|
|
|
|
dstcount = 0; /* We always write into dst starting at position 0*/
|
|
|
|
for(;d4odom_more(odom);dstcount++) {
|
|
|
|
void* xpos;
|
|
|
|
void* dst;
|
|
|
|
d4size_t count;
|
2022-11-16 11:29:21 +08:00
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
count = d4odom_next(odom);
|
|
|
|
if(count >= dimproduct) {
|
|
|
|
ret = THROW(NC_EINVALCOORDS);
|
|
|
|
goto done;
|
|
|
|
}
|
2022-11-16 11:29:21 +08:00
|
|
|
xpos = ((char*)memoryin)+(xsize * dstcount); /* ultimate destination */
|
2017-03-09 08:01:10 +08:00
|
|
|
/* We need to compute the offset in the dap4 data of this instance;
|
|
|
|
for fixed size types, this is easy, otherwise we have to walk
|
|
|
|
the variable size type
|
|
|
|
*/
|
2022-11-16 11:29:21 +08:00
|
|
|
/* Allocate the offset object */
|
|
|
|
if(offset) free(offset); /* Reclaim last loop */
|
|
|
|
offset = NULL;
|
|
|
|
offset = BUILDOFFSET(NULL,0);
|
|
|
|
BLOB2OFFSET(offset,ncvar->data.dap4data);
|
|
|
|
/* Move offset to the count'th element of the array */
|
2017-03-09 08:01:10 +08:00
|
|
|
if(nctype->meta.isfixedsize) {
|
2023-01-19 10:47:29 +08:00
|
|
|
INCR(offset,(dapsize*count));
|
2017-03-09 08:01:10 +08:00
|
|
|
} else {
|
|
|
|
/* We have to walk to the count'th location in the data */
|
2022-11-16 11:29:21 +08:00
|
|
|
if((ret=NCD4_moveto(meta,ncvar,count,offset)))
|
2019-02-15 11:25:40 +08:00
|
|
|
{goto done;}
|
2017-03-09 08:01:10 +08:00
|
|
|
}
|
|
|
|
dst = instance;
|
2022-11-16 11:29:21 +08:00
|
|
|
if((ret=NCD4_fillinstance(meta,nctype,offset,&dst,blobs)))
|
2019-02-15 11:25:40 +08:00
|
|
|
{goto done;}
|
2017-03-09 08:01:10 +08:00
|
|
|
if(xtype == nc4type) {
|
|
|
|
/* We can just copy out the data */
|
|
|
|
memcpy(xpos,instance,nc4size);
|
|
|
|
} else { /* Need to convert */
|
|
|
|
if((ret=NCD4_convert(nc4type,xtype,xpos,instance,1)))
|
2019-02-15 11:25:40 +08:00
|
|
|
{goto done;}
|
2017-03-09 08:01:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2022-11-16 11:29:21 +08:00
|
|
|
if(offset) free(offset); /* Reclaim last loop */
|
2017-03-09 08:01:10 +08:00
|
|
|
/* cleanup */
|
|
|
|
if(odom != NULL)
|
|
|
|
d4odom_free(odom);
|
|
|
|
if(instance != NULL)
|
|
|
|
free(instance);
|
2018-10-31 10:48:12 +08:00
|
|
|
if(ret != NC_NOERR) { /* reclaim all malloc'd data if there is an error*/
|
2017-03-09 08:01:10 +08:00
|
|
|
for(i=0;i<nclistlength(blobs);i++) {
|
|
|
|
nullfree(nclistget(blobs,i));
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 10:48:12 +08:00
|
|
|
if(blobs) nclistfree(blobs);
|
2017-03-09 08:01:10 +08:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
getvarx(int ncid, int varid, NCD4INFO** infop, NCD4node** varp,
|
|
|
|
nc_type* xtypep, size_t* xsizep, nc_type* nc4typep, size_t* nc4sizep)
|
|
|
|
{
|
|
|
|
int ret = NC_NOERR;
|
|
|
|
NC* ncp;
|
|
|
|
NCD4INFO* info;
|
|
|
|
NCD4meta* meta;
|
|
|
|
NCD4node* group;
|
|
|
|
NCD4node* var;
|
|
|
|
NCD4node* type;
|
|
|
|
nc_type xtype, actualtype;
|
|
|
|
size_t instancesize, xsize;
|
|
|
|
|
|
|
|
if((ret = NC_check_id(ncid, (NC**)&ncp)) != NC_NOERR)
|
2019-02-15 11:25:40 +08:00
|
|
|
goto done;
|
2017-03-09 08:01:10 +08:00
|
|
|
|
|
|
|
info = getdap(ncp);
|
|
|
|
meta = info->substrate.metadata;
|
2021-01-15 12:39:08 +08:00
|
|
|
|
|
|
|
/* If the data has not already been read and processed, then do so. */
|
|
|
|
if(meta->serial.dap == NULL) {
|
|
|
|
size_t len = 0;
|
|
|
|
void* content = NULL;
|
|
|
|
/* (Re)Build the meta data; sets serial.rawdata */
|
|
|
|
NCD4_resetMeta(info->substrate.metadata);
|
|
|
|
meta->controller = info;
|
|
|
|
meta->ncid = info->substrate.nc4id; /* Transfer netcdf ncid */
|
|
|
|
|
|
|
|
if((ret=NCD4_readDAP(info, info->controls.flags.flags))) goto done;
|
|
|
|
len = ncbyteslength(info->curl->packet);
|
|
|
|
content = ncbytesextract(info->curl->packet);
|
|
|
|
NCD4_resetSerial(&meta->serial, len, content);
|
|
|
|
/* Process the data part */
|
|
|
|
if((ret=NCD4_dechunk(meta))) goto done;
|
2021-05-22 10:46:56 +08:00
|
|
|
if((ret = NCD4_processdata(info->substrate.metadata))) goto done;
|
2021-01-15 12:39:08 +08:00
|
|
|
}
|
2017-03-09 08:01:10 +08:00
|
|
|
|
2020-05-31 07:36:25 +08:00
|
|
|
if((ret = NCD4_findvar(ncp,ncid,varid,&var,&group))) goto done;
|
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
type = var->basetype;
|
|
|
|
actualtype = type->meta.id;
|
|
|
|
instancesize = type->meta.memsize;
|
|
|
|
|
|
|
|
/* Figure out the type conversion, if any */
|
|
|
|
xtype = *xtypep;
|
|
|
|
if(xtype == NC_NAT)
|
|
|
|
xtype = actualtype;
|
|
|
|
if(xtype != actualtype && xtype > NC_MAX_ATOMIC_TYPE)
|
|
|
|
return THROW(NC_EBADTYPE);
|
|
|
|
if((xtype == NC_CHAR || xtype == NC_STRING)
|
|
|
|
&& (actualtype != NC_CHAR && actualtype != NC_STRING))
|
|
|
|
return THROW(NC_ECHAR);
|
|
|
|
if(xtype <= NC_MAX_ATOMIC_TYPE)
|
|
|
|
xsize = NCD4_typesize(xtype);
|
|
|
|
else
|
|
|
|
xsize = instancesize;
|
|
|
|
|
|
|
|
/* Return relevant info */
|
|
|
|
if(infop) *infop = info;
|
|
|
|
if(xtypep) *xtypep = xtype;
|
|
|
|
if(xsizep) *xsizep = xsize;
|
|
|
|
if(nc4typep) *nc4typep = actualtype;
|
|
|
|
if(nc4sizep) *nc4sizep = instancesize;
|
|
|
|
if(varp) *varp = var;
|
|
|
|
done:
|
2021-05-22 10:46:56 +08:00
|
|
|
if(meta->error.message != NULL)
|
|
|
|
NCD4_reporterror(info); /* Make sure the user sees this */
|
2017-03-09 08:01:10 +08:00
|
|
|
return THROW(ret);
|
|
|
|
}
|
|
|
|
|