/* Output the data for a single variable, in NcML syntax. * TODO: currently not called, need option for NcML with values ... */ int vardatax( const ncvar_t *vp, /* variable */ size_t vdims[], /* variable dimension sizes */ int ncid, /* netcdf id */ int varid /* variable id */ ) { size_t *cor; /* corner coordinates */ size_t *edg; /* edges of hypercube */ size_t *add; /* "odometer" increment to next "row" */ void *vals; int id; int ir; size_t nels; size_t ncols; size_t nrows; int vrank = vp->ndims; cor = (size_t *) emalloc((vrank + 1) * sizeof(size_t)); edg = (size_t *) emalloc((vrank + 1) * sizeof(size_t)); add = (size_t *) emalloc((vrank + 1) * sizeof(size_t)); nels = 1; for (id = 0; id < vrank; id++) { cor[id] = 0; edg[id] = 1; nels *= vdims[id]; /* total number of values for variable */ } printf(" <values>\n "); set_indent (7); if (vrank < 1) { ncols = 1; } else { ncols = vdims[vrank-1]; /* size of "row" along last dimension */ edg[vrank-1] = vdims[vrank-1]; for (id = 0; id < vrank; id++) add[id] = 0; if (vrank > 1) add[vrank-2] = 1; } nrows = nels/ncols; /* number of "rows" */ vals = emalloc(ncols * vp->tinfo->size); for (ir = 0; ir < nrows; ir++) { size_t corsav; bool_t lastrow; if (vrank > 0) { corsav = cor[vrank-1]; } lastrow = (bool_t)(ir == nrows-1); if (vrank > 0) edg[vrank-1] = ncols; NC_CHECK(nc_get_vara(ncid, varid, cor, edg, vals) ); /* Test if we should treat array of chars as a string */ if(vp->type == NC_CHAR && (vp->fmt == 0 || STREQ(vp->fmt,"%s") || STREQ(vp->fmt,""))) { pr_tvalsx(vp, ncols, 0, lastrow, (char *) vals); } else { pr_any_valsx(vp, ncols, 0, lastrow, vals); } if (vrank > 0) cor[vrank-1] += ncols; if (vrank > 0) cor[vrank-1] = corsav; if (ir < nrows-1) if (!upcorner(vdims,vp->ndims,cor,add)) error("vardata: odometer overflowed!"); set_indent(2); } printf(" </values>\n"); free(vals); free(cor); free(edg); free(add); return 0; }
/* Output the data for a single variable, in NcML syntax. * TODO: currently not called, need option for NcML with values ... */ int vardatax( const ncvar_t *vp, /* variable */ size_t vdims[], /* variable dimension sizes */ int ncid, /* netcdf id */ int varid, /* variable id */ const fspec_t *fsp /* formatting specs */ ) { size_t cor[NC_MAX_DIMS]; /* corner coordinates */ size_t edg[NC_MAX_DIMS]; /* edges of hypercube */ size_t add[NC_MAX_DIMS]; /* "odometer" increment to next "row" */ size_t gulp; void *vals; int id; int ir; size_t nels; size_t ncols; size_t nrows; int vrank = vp->ndims; nels = 1; for (id = 0; id < vrank; id++) { cor[id] = 0; edg[id] = 1; nels *= vdims[id]; /* total number of values for variable */ } printf(" <values>\n "); set_indent (7); if (vrank < 1) { ncols = 1; } else { ncols = vdims[vrank-1]; /* size of "row" along last dimension */ edg[vrank-1] = vdims[vrank-1]; for (id = 0; id < vrank; id++) add[id] = 0; if (vrank > 1) add[vrank-2] = 1; } nrows = nels/ncols; /* number of "rows" */ gulp = ncols < VALBUFSIZ ? VALBUFSIZ : ncols; vals = emalloc(gulp * vp->tinfo->size); for (ir = 0; ir < nrows; ir++) { /* * rather than just printing a whole row at once (which might * exceed the capacity of some platforms), we break each row * into smaller chunks, if necessary. */ size_t corsav; int left = (int)ncols; boolean lastrow; if (vrank > 0) { corsav = cor[vrank-1]; } lastrow = (boolean)(ir == nrows-1); while (left > 0) { size_t toget = left < gulp ? left : gulp; if (vrank > 0) edg[vrank-1] = toget; NC_CHECK(nc_get_vara(ncid, varid, cor, edg, vals) ); /* Test if we should treat array of chars as a string */ if(vp->type == NC_CHAR && (vp->fmt == 0 || STREQ(vp->fmt,"%s") || STREQ(vp->fmt,""))) { pr_tvalsx(vp, toget, left > toget, lastrow, (char *) vals); } else { pr_any_valsx(vp, toget, left > toget, lastrow, vals); } left -= toget; if (vrank > 0) cor[vrank-1] += toget; } if (vrank > 0) cor[vrank-1] = corsav; if (ir < nrows-1) if (!upcorner(vdims,vp->ndims,cor,add)) error("vardata: odometer overflowed!"); set_indent(2); } printf(" </values>\n"); free(vals); return 0; }