2010-06-03 21:24:43 +08:00
|
|
|
/*********************************************************************
|
2018-12-07 06:36:53 +08:00
|
|
|
* Copyright 2018, UCAR/Unidata
|
2010-06-03 21:24:43 +08:00
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
* $Header: /upc/share/CVS/netcdf-3/ncdump/vardata.c,v 1.48 2010/05/05 22:15:39 dmh Exp $
|
|
|
|
*********************************************************************/
|
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
#include "config.h"
|
2010-06-03 21:24:43 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-09-01 02:28:08 +08:00
|
|
|
#include <assert.h>
|
2010-06-03 21:24:43 +08:00
|
|
|
#include <netcdf.h>
|
2011-09-13 05:31:08 +08:00
|
|
|
#include "utils.h"
|
2018-10-31 10:48:12 +08:00
|
|
|
#include "netcdf.h"
|
2011-09-13 05:31:08 +08:00
|
|
|
#include "nccomps.h"
|
2010-06-03 21:24:43 +08:00
|
|
|
#include "dumplib.h"
|
2011-09-22 07:10:03 +08:00
|
|
|
#include "ncdump.h"
|
2010-06-03 21:24:43 +08:00
|
|
|
#include "indent.h"
|
|
|
|
#include "vardata.h"
|
2018-11-16 01:00:38 +08:00
|
|
|
#include "netcdf_aux.h"
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* maximum len of string needed for one value of a primitive type */
|
|
|
|
#define MAX_OUTPUT_LEN 100
|
|
|
|
|
|
|
|
#define LINEPIND " " /* indent of continued lines */
|
|
|
|
|
2018-10-31 10:48:12 +08:00
|
|
|
#define LBRACE "{"
|
|
|
|
#define RBRACE "}"
|
|
|
|
|
2011-09-01 02:28:08 +08:00
|
|
|
extern fspec_t formatting_specs; /* set from command-line options */
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/* Only read this many values at a time, if last dimension is larger
|
|
|
|
than this */
|
2012-11-17 05:37:43 +08:00
|
|
|
#define VALBUFSIZ 10000
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
static int linep; /* line position, not counting global indent */
|
|
|
|
static int max_line_len; /* max chars per line, not counting global indent */
|
|
|
|
|
|
|
|
|
|
|
|
/* set position in line before lput() calls */
|
|
|
|
static void
|
|
|
|
set_indent(int in) {
|
|
|
|
linep = in;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
set_max_len(int len) {
|
|
|
|
max_line_len = len-2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-01 05:16:47 +08:00
|
|
|
/*
|
2010-06-03 21:24:43 +08:00
|
|
|
* Output a string that should not be split across lines. If it would
|
|
|
|
* make current line too long, first output a newline and current
|
|
|
|
* (nested group) indentation, then continuation indentation, then
|
2016-01-01 05:16:47 +08:00
|
|
|
* output string. If string ends with a newline to force short line,
|
2012-11-17 05:37:43 +08:00
|
|
|
* reset indentation after output.
|
2010-06-03 21:24:43 +08:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
lput(const char *cp) {
|
|
|
|
size_t nn = strlen(cp);
|
|
|
|
|
|
|
|
if (nn+linep > max_line_len && nn > 2) {
|
|
|
|
(void) fputs("\n", stdout);
|
|
|
|
indent_out();
|
|
|
|
(void) fputs(LINEPIND, stdout);
|
|
|
|
linep = (int)strlen(LINEPIND) + indent_get();
|
|
|
|
}
|
|
|
|
(void) fputs(cp,stdout);
|
2016-08-26 01:32:49 +08:00
|
|
|
if (nn > 0 && cp[nn - 1] == '\n') {
|
2012-11-17 05:37:43 +08:00
|
|
|
linep = indent_get();
|
|
|
|
} else
|
|
|
|
linep += nn;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-01 02:28:08 +08:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* Support function for print_att_times.
|
|
|
|
* Output a string that should not be split across lines.
|
|
|
|
* Keep track of position on print line.
|
|
|
|
* Wrap print lines as needed to keep within requested line length.
|
|
|
|
* Start CDL comment on each new line.
|
|
|
|
* Handle line indentation.
|
|
|
|
*
|
|
|
|
* This function is like lput in vardata.c, with variations.
|
|
|
|
* Hopefully this function will later be absorbed into a more
|
|
|
|
* general lput-type function.
|
|
|
|
*/
|
|
|
|
|
2012-04-24 07:59:24 +08:00
|
|
|
#define CDL_COMMENT_PREFIX "// "
|
2011-09-01 02:28:08 +08:00
|
|
|
void
|
|
|
|
lput2(
|
|
|
|
const char *cp, /* string to print */
|
2012-11-17 05:37:43 +08:00
|
|
|
bool_t first_item, /* identify first item in list */
|
|
|
|
bool_t wrap /* line wrap control: true=enable,
|
2011-09-01 02:28:08 +08:00
|
|
|
* false=stay on same line */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
static int linep; /* current line position (number of */
|
|
|
|
/* chars); saved between calls */
|
2012-04-24 07:59:24 +08:00
|
|
|
int len_prefix = strlen (CDL_COMMENT_PREFIX);
|
2012-11-17 05:37:43 +08:00
|
|
|
bool_t make_newline;
|
2016-01-01 05:16:47 +08:00
|
|
|
|
2011-09-01 02:28:08 +08:00
|
|
|
size_t len1 = strlen(cp); /* length of input string */
|
|
|
|
|
|
|
|
assert (len1 > 0);
|
|
|
|
|
|
|
|
/* (1) Single space or newline/indent sequence, as needed. */
|
2016-01-01 05:16:47 +08:00
|
|
|
|
2011-09-01 02:28:08 +08:00
|
|
|
linep = linep + 1 + len1; /* new line position, without newline */
|
2015-08-20 17:42:05 +08:00
|
|
|
/* add 1 extra for preceding space */
|
2016-01-01 05:16:47 +08:00
|
|
|
|
2011-09-01 02:28:08 +08:00
|
|
|
make_newline = (wrap && (first_item || linep > max_line_len + 2));
|
|
|
|
/* NEVER new line in no-wrap mode */
|
2016-01-01 05:16:47 +08:00
|
|
|
|
2011-09-01 02:28:08 +08:00
|
|
|
if (make_newline) { /* start new line, if needed */
|
|
|
|
printf ("\n");
|
|
|
|
indent_out(); /* same exact indentation as pr_att */
|
|
|
|
printf ("\t\t"); /* (possible problem here) */
|
|
|
|
printf (" "); /* add indent for CDL comment */
|
|
|
|
linep = 16 + 2 + len1; /* recompute new line position */
|
|
|
|
/* with newline + indents */
|
|
|
|
} else {
|
|
|
|
printf (" "); /* always one space, if not newline */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Add CDL comment prefix, if needed. */
|
|
|
|
|
|
|
|
if (len_prefix > 0) {
|
|
|
|
if (first_item || make_newline) {
|
2012-04-24 07:59:24 +08:00
|
|
|
printf (CDL_COMMENT_PREFIX);
|
2011-09-01 02:28:08 +08:00
|
|
|
linep = linep + len_prefix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Output caller's string value. */
|
|
|
|
|
2012-04-24 07:59:24 +08:00
|
|
|
printf ("%s", cp);
|
2011-09-01 02:28:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/*
|
|
|
|
* Output a value of an attribute.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
print_any_att_val (
|
|
|
|
struct safebuf_t *sb, /* string where output goes */
|
|
|
|
const ncatt_t *attp, /* attrbute */
|
|
|
|
const void *valp /* pointer to the value */
|
|
|
|
) {
|
|
|
|
nctype_t *typ = attp->tinfo;
|
|
|
|
(*typ->typ_tostring)(typ, sb, valp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Output a value of a variable, except if there is a fill value for
|
|
|
|
* the variable and the value is the fill value, print the fill-value string
|
|
|
|
* instead. (Floating-point fill values need only be within machine epsilon of
|
|
|
|
* defined fill value.)
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
print_any_val(
|
|
|
|
safebuf_t *sb, /* string where output goes */
|
|
|
|
const ncvar_t *varp, /* variable */
|
|
|
|
const void *valp /* pointer to the value */
|
|
|
|
)
|
|
|
|
{
|
2016-01-01 05:16:47 +08:00
|
|
|
if (varp->has_fillval &&
|
|
|
|
(*(varp->tinfo->val_equals))((const nctype_t *)varp->tinfo,
|
2010-06-03 21:24:43 +08:00
|
|
|
(const void*)varp->fillvalp, valp) ) {
|
|
|
|
sbuf_cpy(sb, FILL_STRING);
|
|
|
|
} else {
|
|
|
|
(*varp->val_tostring)(varp, sb, valp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* print last delimiter in each line before annotation (, or ;)
|
|
|
|
*/
|
|
|
|
static void
|
2012-11-17 05:37:43 +08:00
|
|
|
lastdelim (bool_t more, bool_t lastrow)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
|
|
|
if (more) {
|
|
|
|
printf(", ");
|
|
|
|
} else {
|
|
|
|
if(lastrow) {
|
|
|
|
printf(";");
|
|
|
|
} else {
|
|
|
|
printf(",");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* print last delimiter in each line before annotation (, or ;)
|
|
|
|
*/
|
|
|
|
static void
|
2012-11-17 05:37:43 +08:00
|
|
|
lastdelim2 (bool_t more, bool_t lastrow)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
|
|
|
if (more) {
|
|
|
|
lput(", ");
|
|
|
|
} else {
|
|
|
|
if(lastrow) {
|
|
|
|
lput(" ;");
|
|
|
|
lput("\n");
|
|
|
|
} else {
|
|
|
|
lput(",\n");
|
|
|
|
lput(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print a number of attribute values
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pr_any_att_vals(
|
|
|
|
const ncatt_t *ap, /* attribute */
|
|
|
|
const void *vals /* pointer to block of values */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
size_t iel;
|
|
|
|
size_t len = ap->len; /* number of values to print */
|
|
|
|
const char *valp = (const char *)vals;
|
|
|
|
safebuf_t *sb = sbuf_new();
|
|
|
|
|
|
|
|
for (iel = 0; iel < len - 1; iel++) {
|
|
|
|
print_any_att_val(sb, ap, (void *)valp);
|
|
|
|
valp += ap->tinfo->size; /* next value according to type */
|
|
|
|
sbuf_cat(sb, iel == len - 1 ? "" : ", ");
|
|
|
|
lput(sbuf_str(sb));
|
|
|
|
}
|
|
|
|
print_any_att_val(sb, ap, (void *)valp);
|
|
|
|
lput(sbuf_str(sb));
|
|
|
|
sbuf_free(sb);
|
|
|
|
}
|
|
|
|
|
2016-01-01 05:16:47 +08:00
|
|
|
/*
|
2012-11-17 05:37:43 +08:00
|
|
|
* Prints brief annotation for a row of data values
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
annotate_brief(
|
|
|
|
const ncvar_t *vp, /* variable */
|
|
|
|
const size_t *cor, /* corner coordinates */
|
|
|
|
size_t vdims[] /* variable dimension sizes */
|
2016-01-01 05:16:47 +08:00
|
|
|
)
|
2012-11-17 05:37:43 +08:00
|
|
|
{
|
|
|
|
int vrank = vp->ndims;
|
|
|
|
int id;
|
|
|
|
printf ("// ");
|
|
|
|
print_name(vp->name);
|
|
|
|
printf("(");
|
|
|
|
|
|
|
|
switch (formatting_specs.data_lang) {
|
|
|
|
case LANG_C:
|
|
|
|
/* print brief comment with C variable indices */
|
|
|
|
for (id = 0; id < vrank-1; id++)
|
|
|
|
printf("%lu,", (unsigned long)cor[id]);
|
|
|
|
if (vdims[vrank-1] == 1)
|
|
|
|
printf("0");
|
|
|
|
else
|
|
|
|
printf(" 0-%lu", (unsigned long)vdims[vrank-1]-1);
|
|
|
|
break;
|
|
|
|
case LANG_F:
|
|
|
|
/* print brief comment with Fortran variable indices */
|
|
|
|
if (vdims[vrank-1] == 1)
|
|
|
|
printf("1");
|
|
|
|
else
|
|
|
|
printf("1-%lu ", (unsigned long)vdims[vrank-1]);
|
|
|
|
for (id = vrank-2; id >=0 ; id--) {
|
|
|
|
printf(",%lu", (unsigned long)(1 + cor[id]));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf(")\n");
|
|
|
|
indent_out();
|
|
|
|
printf(" ");
|
|
|
|
set_indent(4 + indent_get());
|
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Annotates a value in data section with var name and indices in comment
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
annotate(
|
2012-11-17 05:37:43 +08:00
|
|
|
const ncvar_t *vp, /* variable */
|
2010-06-03 21:24:43 +08:00
|
|
|
const size_t *cor, /* corner coordinates */
|
|
|
|
long iel /* which element in current row */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int vrank = vp->ndims;
|
|
|
|
int id;
|
2016-01-01 05:16:47 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/* print indices according to data_lang */
|
|
|
|
/* printf(" // %s(", vp->name); */
|
|
|
|
printf(" // ");
|
|
|
|
print_name(vp->name);
|
|
|
|
printf("(");
|
2011-09-01 02:28:08 +08:00
|
|
|
switch (formatting_specs.data_lang) {
|
2010-06-03 21:24:43 +08:00
|
|
|
case LANG_C:
|
|
|
|
/* C variable indices */
|
|
|
|
for (id = 0; id < vrank-1; id++)
|
|
|
|
printf("%lu,", (unsigned long) cor[id]);
|
|
|
|
printf("%lu", (unsigned long) cor[id] + iel);
|
|
|
|
break;
|
|
|
|
case LANG_F:
|
|
|
|
/* Fortran variable indices */
|
|
|
|
printf("%lu", (unsigned long) cor[vrank-1] + iel + 1);
|
|
|
|
for (id = vrank-2; id >=0 ; id--) {
|
|
|
|
printf(",%lu", 1 + (unsigned long) cor[id]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf(")\n ");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print a number of char variable values as a text string, where the
|
|
|
|
* optional comments for each value identify the variable, and each
|
|
|
|
* dimension index.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pr_tvals(
|
|
|
|
const ncvar_t *vp, /* variable */
|
|
|
|
size_t len, /* number of values to print */
|
|
|
|
const char *vals, /* pointer to block of values */
|
|
|
|
const size_t *cor /* corner coordinates */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
long iel;
|
|
|
|
const char *sp;
|
|
|
|
|
|
|
|
printf("\"");
|
|
|
|
/* adjust len so trailing nulls don't get printed */
|
|
|
|
sp = vals + len;
|
|
|
|
while (len != 0 && *--sp == '\0')
|
|
|
|
len--;
|
|
|
|
for (iel = 0; iel < len; iel++) {
|
|
|
|
unsigned char uc;
|
|
|
|
switch (uc = *vals++ & 0377) {
|
|
|
|
case '\b':
|
|
|
|
printf("\\b");
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
printf("\\f");
|
|
|
|
break;
|
|
|
|
case '\n': /* generate linebreaks after new-lines */
|
|
|
|
printf("\\n\",\n \"");
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
printf("\\r");
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
printf("\\t");
|
|
|
|
break;
|
|
|
|
case '\v':
|
|
|
|
printf("\\v");
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
printf("\\\\");
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
printf("\\\'");
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
printf("\\\"");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (isprint(uc))
|
|
|
|
printf("%c",uc);
|
|
|
|
else
|
|
|
|
printf("\\%.3o",uc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\"");
|
2014-08-08 02:49:36 +08:00
|
|
|
/* if (formatting_specs.full_data_cmnts) { */
|
|
|
|
/* lastdelim (0, lastrow); */
|
|
|
|
/* annotate (vp, (size_t *)cor, 0L); */
|
|
|
|
/* } */
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Updates a vector of ints, odometer style. Returns 0 if odometer
|
|
|
|
* overflowed, else 1.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
upcorner(
|
|
|
|
const size_t *dims, /* The "odometer" limits for each dimension */
|
|
|
|
int ndims, /* Number of dimensions */
|
|
|
|
size_t* odom, /* The "odometer" vector to be updated */
|
|
|
|
const size_t* add /* A vector to "add" to odom on each update */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int id;
|
|
|
|
int ret = 1;
|
|
|
|
|
|
|
|
for (id = ndims-1; id > 0; id--) {
|
|
|
|
odom[id] += add[id];
|
|
|
|
if(odom[id] >= dims[id]) {
|
|
|
|
odom[id-1]++;
|
|
|
|
odom[id] -= dims[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
odom[0] += add[0];
|
|
|
|
if (odom[0] >= dims[0])
|
|
|
|
ret = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-01 05:16:47 +08:00
|
|
|
/* Print data values for variable varid.
|
2012-11-17 05:37:43 +08:00
|
|
|
*
|
|
|
|
* Recursive to handle possibility of variables with multiple
|
|
|
|
* unlimited dimensions, for which the CDL syntax requires use of "{"
|
|
|
|
* and "}" in data section to disambiguate the size of nested records
|
|
|
|
* in a simple linear list of values.
|
|
|
|
*/
|
|
|
|
static int
|
2016-01-01 05:16:47 +08:00
|
|
|
print_rows(
|
2012-11-17 05:37:43 +08:00
|
|
|
int level, /* 0 at top-level, incremented for each recursive level */
|
|
|
|
int ncid, /* netcdf id */
|
|
|
|
int varid, /* variable id */
|
|
|
|
const ncvar_t *vp, /* variable */
|
|
|
|
size_t vdims[], /* variable dimension sizes */
|
|
|
|
size_t cor[], /* corner coordinates */
|
|
|
|
size_t edg[], /* edges of hypercube */
|
|
|
|
void *vals, /* allocated buffer for ncols values in a row */
|
|
|
|
int marks_pending /* number of pending closing "}" record markers */
|
2016-01-01 05:16:47 +08:00
|
|
|
)
|
2012-11-17 05:37:43 +08:00
|
|
|
{
|
2014-08-08 02:49:36 +08:00
|
|
|
int rank = vp->ndims;
|
|
|
|
size_t ncols = rank > 0 ? vdims[rank - 1] : 1; /* number of values in a row */
|
2012-11-17 05:37:43 +08:00
|
|
|
int d0 = 0;
|
|
|
|
size_t inc = 1;
|
|
|
|
int i;
|
|
|
|
bool_t mark_record = (level > 0 && is_unlim_dim(ncid, vp->dims[level]));
|
|
|
|
safebuf_t *sb = sbuf_new();
|
|
|
|
if (rank > 0)
|
|
|
|
d0 = vdims[level];
|
|
|
|
for(i = level + 1; i < rank; i++) {
|
|
|
|
inc *= vdims[i];
|
|
|
|
}
|
|
|
|
if(mark_record) { /* the whole point of this recursion is printing these "{}" */
|
2018-10-31 10:48:12 +08:00
|
|
|
lput(LBRACE);
|
2012-11-17 05:37:43 +08:00
|
|
|
marks_pending++; /* matching "}"s to emit after last "row" */
|
|
|
|
}
|
|
|
|
if(rank - level > 1) { /* this level is just d0 next levels */
|
|
|
|
size_t *local_cor = emalloc((rank + 1) * sizeof(size_t));
|
|
|
|
size_t *local_edg = emalloc((rank + 1) * sizeof(size_t));
|
|
|
|
for(i = 0; i < rank; i++) {
|
|
|
|
local_cor[i] = cor[i];
|
|
|
|
local_edg[i] = edg[i];
|
|
|
|
}
|
|
|
|
local_cor[level] = 0;
|
|
|
|
local_edg[level] = 1;
|
|
|
|
for(i = 0; i < d0 - 1; i++) {
|
2016-01-01 05:16:47 +08:00
|
|
|
print_rows(level + 1, ncid, varid, vp, vdims,
|
2012-11-17 05:37:43 +08:00
|
|
|
local_cor, local_edg, vals, 0);
|
|
|
|
local_cor[level] += 1;
|
|
|
|
}
|
2016-01-01 05:16:47 +08:00
|
|
|
print_rows(level + 1, ncid, varid, vp, vdims,
|
2012-11-17 05:37:43 +08:00
|
|
|
local_cor, local_edg, vals, marks_pending);
|
|
|
|
free(local_edg);
|
|
|
|
free(local_cor);
|
|
|
|
} else { /* bottom out of recursion */
|
2012-11-20 04:02:22 +08:00
|
|
|
char *valp = vals;
|
2012-11-17 05:37:43 +08:00
|
|
|
bool_t lastrow;
|
2013-11-12 21:40:03 +08:00
|
|
|
int j;
|
2014-08-08 02:49:36 +08:00
|
|
|
if(formatting_specs.brief_data_cmnts && rank > 1 && ncols > 0) {
|
2013-11-14 02:03:18 +08:00
|
|
|
annotate_brief(vp, cor, vdims);
|
|
|
|
}
|
2012-11-20 04:02:22 +08:00
|
|
|
NC_CHECK(nc_get_vara(ncid, varid, cor, edg, (void *)valp));
|
2014-08-08 02:49:36 +08:00
|
|
|
|
|
|
|
/* Test if we should treat array of chars as strings along last dimension */
|
2018-05-12 22:55:51 +08:00
|
|
|
if(vp->type == NC_CHAR && (vp->fmt == 0 || NCSTREQ(vp->fmt,"%s") || NCSTREQ(vp->fmt,""))) {
|
2014-08-08 02:49:36 +08:00
|
|
|
pr_tvals(vp, ncols, vals, cor);
|
|
|
|
} else { /* for non-text variables */
|
|
|
|
for(i=0; i < d0 - 1; i++) {
|
|
|
|
print_any_val(sb, vp, (void *)valp);
|
|
|
|
valp += vp->tinfo->size; /* next value according to type */
|
|
|
|
if (formatting_specs.full_data_cmnts) {
|
|
|
|
printf("%s, ", sb->buf);
|
|
|
|
annotate (vp, cor, i);
|
|
|
|
} else {
|
|
|
|
sbuf_cat(sb, ", ");
|
|
|
|
lput(sbuf_str(sb));
|
|
|
|
}
|
2012-11-17 05:37:43 +08:00
|
|
|
}
|
2014-08-08 02:49:36 +08:00
|
|
|
print_any_val(sb, vp, (void *)valp);
|
2012-11-17 05:37:43 +08:00
|
|
|
}
|
2018-11-16 01:00:38 +08:00
|
|
|
/* In case vals has memory hanging off e.g. vlen or string, make sure to reclaim it */
|
Fix various problem around VLEN's
re: https://github.com/Unidata/netcdf-c/issues/541
re: https://github.com/Unidata/netcdf-c/issues/1208
re: https://github.com/Unidata/netcdf-c/issues/2078
re: https://github.com/Unidata/netcdf-c/issues/2041
re: https://github.com/Unidata/netcdf-c/issues/2143
For a long time, there have been known problems with the
management of complex types containing VLENs. This also
involves the string type because it is stored as a VLEN of
chars.
This PR (mostly) fixes this problem. But note that it adds new
functions to netcdf.h (see below) and this may require bumping
the .so number. These new functions can be removed, if desired,
in favor of functions in netcdf_aux.h, but netcdf.h seems the
better place for them because they are intended as alternatives
to the nc_free_vlen and nc_free_string functions already in
netcdf.h.
The term complex type refers to any type that directly or
transitively references a VLEN type. So an array of VLENS, a
compound with a VLEN field, and so on.
In order to properly handle instances of these complex types, it
is necessary to have function that can recursively walk
instances of such types to perform various actions on them. The
term "deep" is also used to mean recursive.
At the moment, the two operations needed by the netcdf library are:
* free'ing an instance of the complex type
* copying an instance of the complex type.
The current library does only shallow free and shallow copy of
complex types. This means that only the top level is properly
free'd or copied, but deep internal blocks in the instance are
not touched.
Note that the term "vector" will be used to mean a contiguous (in
memory) sequence of instances of some type. Given an array with,
say, dimensions 2 X 3 X 4, this will be stored in memory as a
vector of length 2*3*4=24 instances.
The use cases are primarily these.
## nc_get_vars
Suppose one is reading a vector of instances using nc_get_vars
(or nc_get_vara or nc_get_var, etc.). These functions will
return the vector in the top-level memory provided. All
interior blocks (form nested VLEN or strings) will have been
dynamically allocated.
After using this vector of instances, it is necessary to free
(aka reclaim) the dynamically allocated memory, otherwise a
memory leak occurs. So, the recursive reclaim function is used
to walk the returned instance vector and do a deep reclaim of
the data.
Currently functions are defined in netcdf.h that are supposed to
handle this: nc_free_vlen(), nc_free_vlens(), and
nc_free_string(). Unfortunately, these functions only do a
shallow free, so deeply nested instances are not properly
handled by them.
Note that internally, the provided data is immediately written so
there is no need to copy it. But the caller may need to reclaim the
data it passed into the function.
## nc_put_att
Suppose one is writing a vector of instances as the data of an attribute
using, say, nc_put_att.
Internally, the incoming attribute data must be copied and stored
so that changes/reclamation of the input data will not affect
the attribute.
Again, the code inside the netcdf library does only shallow copying
rather than deep copy. As a result, one sees effects such as described
in Github Issue https://github.com/Unidata/netcdf-c/issues/2143.
Also, after defining the attribute, it may be necessary for the user
to free the data that was provided as input to nc_put_att().
## nc_get_att
Suppose one is reading a vector of instances as the data of an attribute
using, say, nc_get_att.
Internally, the existing attribute data must be copied and returned
to the caller, and the caller is responsible for reclaiming
the returned data.
Again, the code inside the netcdf library does only shallow copying
rather than deep copy. So this can lead to memory leaks and errors
because the deep data is shared between the library and the user.
# Solution
The solution is to build properly recursive reclaim and copy
functions and use those as needed.
These recursive functions are defined in libdispatch/dinstance.c
and their signatures are defined in include/netcdf.h.
For back compatibility, corresponding "ncaux_XXX" functions
are defined in include/netcdf_aux.h.
````
int nc_reclaim_data(int ncid, nc_type xtypeid, void* memory, size_t count);
int nc_reclaim_data_all(int ncid, nc_type xtypeid, void* memory, size_t count);
int nc_copy_data(int ncid, nc_type xtypeid, const void* memory, size_t count, void* copy);
int nc_copy_data_all(int ncid, nc_type xtypeid, const void* memory, size_t count, void** copyp);
````
There are two variants. The first two, nc_reclaim_data() and
nc_copy_data(), assume the top-level vector is managed by the
caller. For reclaim, this is so the user can use, for example, a
statically allocated vector. For copy, it assumes the user
provides the space into which the copy is stored.
The second two, nc_reclaim_data_all() and
nc_copy_data_all(), allows the functions to manage the
top-level. So for nc_reclaim_data_all, the top level is
assumed to be dynamically allocated and will be free'd by
nc_reclaim_data_all(). The nc_copy_data_all() function
will allocate the top level and return a pointer to it to the
user. The user can later pass that pointer to
nc_reclaim_data_all() to reclaim the instance(s).
# Internal Changes
The netcdf-c library internals are changed to use the proper
reclaim and copy functions. It turns out that the places where
these functions are needed is quite pervasive in the netcdf-c
library code. Using these functions also allows some
simplification of the code since the stdata and vldata fields of
NC_ATT_INFO are no longer needed. Currently this is commented
out using the SEPDATA \#define macro. When any bugs are largely
fixed, all this code will be removed.
# Known Bugs
1. There is still one known failure that has not been solved.
All the failures revolve around some variant of this .cdl file.
The proximate cause of failure is the use of a VLEN FillValue.
````
netcdf x {
types:
float(*) row_of_floats ;
dimensions:
m = 5 ;
variables:
row_of_floats ragged_array(m) ;
row_of_floats ragged_array:_FillValue = {-999} ;
data:
ragged_array = {10, 11, 12, 13, 14}, {20, 21, 22, 23}, {30, 31, 32},
{40, 41}, _ ;
}
````
When a solution is found, I will either add it to this PR or post a new PR.
# Related Changes
* Mark nc_free_vlen(s) as deprecated in favor of ncaux_reclaim_data.
* Remove the --enable-unfixed-memory-leaks option.
* Remove the NC_VLENS_NOTEST code that suppresses some vlen tests.
* Document this change in docs/internal.md
* Disable the tst_vlen_data test in ncdump/tst_nccopy4.sh.
* Mark types as fixed size or not (transitively) to optimize the reclaim
and copy functions.
# Misc. Changes
* Make Doxygen process libdispatch/daux.c
* Make sure the NC_ATT_INFO_T.container field is set.
2022-01-09 09:30:00 +08:00
|
|
|
NC_CHECK(nc_reclaim_data(ncid,vp->type,vals,ncols));
|
2018-11-16 01:00:38 +08:00
|
|
|
|
2012-11-17 05:37:43 +08:00
|
|
|
/* determine if this is the last row */
|
|
|
|
lastrow = true;
|
2013-11-12 21:40:03 +08:00
|
|
|
for(j = 0; j < rank - 1; j++) {
|
2018-10-31 10:48:12 +08:00
|
|
|
if (cor[j] != vdims[j] - 1) {
|
2012-11-17 05:37:43 +08:00
|
|
|
lastrow = false;
|
|
|
|
break;
|
2018-10-31 10:48:12 +08:00
|
|
|
}
|
2013-11-12 21:40:03 +08:00
|
|
|
}
|
2012-11-17 05:37:43 +08:00
|
|
|
if (formatting_specs.full_data_cmnts) {
|
2020-04-29 05:49:03 +08:00
|
|
|
for (j = 0; j < marks_pending; j++) {
|
2018-10-31 10:48:12 +08:00
|
|
|
sbuf_cat(sb, RBRACE);
|
2020-04-29 05:49:03 +08:00
|
|
|
}
|
|
|
|
printf("%s", sbuf_str(sb));
|
|
|
|
lastdelim (0, lastrow);
|
|
|
|
annotate (vp, cor, (d0 > 0 ? d0-1 : d0));
|
2012-11-17 05:37:43 +08:00
|
|
|
} else {
|
2018-10-31 10:48:12 +08:00
|
|
|
for (j = 0; j < marks_pending; j++) {
|
|
|
|
sbuf_cat(sb, RBRACE);
|
|
|
|
}
|
|
|
|
lput(sbuf_str(sb));
|
|
|
|
lastdelim2 (0, lastrow);
|
2016-01-01 05:16:47 +08:00
|
|
|
}
|
2012-11-17 05:37:43 +08:00
|
|
|
}
|
|
|
|
sbuf_free(sb);
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/* Output the data for a single variable, in CDL syntax. */
|
|
|
|
int
|
|
|
|
vardata(
|
|
|
|
const ncvar_t *vp, /* variable */
|
|
|
|
size_t vdims[], /* variable dimension sizes */
|
|
|
|
int ncid, /* netcdf id */
|
2011-09-01 02:28:08 +08:00
|
|
|
int varid /* variable id */
|
2010-06-03 21:24:43 +08:00
|
|
|
)
|
|
|
|
{
|
2010-07-30 06:41:05 +08:00
|
|
|
size_t *cor; /* corner coordinates */
|
|
|
|
size_t *edg; /* edges of hypercube */
|
|
|
|
size_t *add; /* "odometer" increment to next "row" */
|
2010-06-03 21:24:43 +08:00
|
|
|
void *vals;
|
|
|
|
|
|
|
|
int id;
|
|
|
|
size_t nels;
|
|
|
|
size_t ncols;
|
|
|
|
int vrank = vp->ndims;
|
|
|
|
|
2014-08-08 07:03:27 +08:00
|
|
|
int level = 0;
|
|
|
|
int marks_pending = 0;
|
|
|
|
|
2010-08-04 00:09:01 +08:00
|
|
|
cor = (size_t *) emalloc((1 + vrank) * sizeof(size_t));
|
|
|
|
edg = (size_t *) emalloc((1 + vrank) * sizeof(size_t));
|
|
|
|
add = (size_t *) emalloc((1 + vrank) * sizeof(size_t));
|
2010-07-30 06:41:05 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
nels = 1;
|
|
|
|
if(vrank == 0) { /*scalar*/
|
|
|
|
cor[0] = 0;
|
|
|
|
edg[0] = 1;
|
2012-11-17 05:37:43 +08:00
|
|
|
} else {
|
|
|
|
for (id = 0; id < vrank; id++) {
|
|
|
|
cor[id] = 0;
|
|
|
|
edg[id] = 1;
|
|
|
|
nels *= vdims[id]; /* total number of values for variable */
|
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
indent_out();
|
2012-11-17 05:37:43 +08:00
|
|
|
printf(" ");
|
|
|
|
print_name(vp->name);
|
2010-06-03 21:24:43 +08:00
|
|
|
if (vrank <= 1) {
|
|
|
|
printf(" = ");
|
|
|
|
set_indent ((int)strlen(vp->name) + 4 + indent_get());
|
|
|
|
} else {
|
|
|
|
printf(" =\n ");
|
|
|
|
set_indent (2 + indent_get());
|
|
|
|
}
|
|
|
|
|
2012-11-17 05:37:43 +08:00
|
|
|
if (vrank == 0) {
|
2010-06-03 21:24:43 +08:00
|
|
|
ncols = 1;
|
|
|
|
} else {
|
|
|
|
ncols = vdims[vrank-1]; /* size of "row" along last dimension */
|
2012-11-17 05:37:43 +08:00
|
|
|
edg[vrank-1] = ncols;
|
2010-06-03 21:24:43 +08:00
|
|
|
for (id = 0; id < vrank; id++)
|
|
|
|
add[id] = 0;
|
|
|
|
if (vrank > 1)
|
|
|
|
add[vrank-2] = 1;
|
|
|
|
}
|
2012-11-17 05:37:43 +08:00
|
|
|
vals = emalloc(ncols * vp->tinfo->size);
|
2016-01-01 05:16:47 +08:00
|
|
|
|
2014-08-08 02:49:36 +08:00
|
|
|
NC_CHECK(print_rows(level, ncid, varid, vp, vdims, cor, edg, vals, marks_pending));
|
2010-06-03 21:24:43 +08:00
|
|
|
free(vals);
|
2010-07-30 06:41:05 +08:00
|
|
|
free(cor);
|
|
|
|
free(edg);
|
|
|
|
free(add);
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* print last delimiter in each line before annotation (, or ;)
|
|
|
|
*/
|
|
|
|
static void
|
2012-11-17 05:37:43 +08:00
|
|
|
lastdelim2x (bool_t more, bool_t lastrow)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
|
|
|
if (more) {
|
|
|
|
lput(" ");
|
|
|
|
} else {
|
|
|
|
if(lastrow) {
|
|
|
|
lput("\n ");
|
|
|
|
} else {
|
|
|
|
lput("\n ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print a number of char variable values as a text string for NcML
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pr_tvalsx(
|
|
|
|
const ncvar_t *vp, /* variable */
|
|
|
|
size_t len, /* number of values to print */
|
2012-11-17 05:37:43 +08:00
|
|
|
bool_t more, /* true if more data for this row will
|
2010-06-03 21:24:43 +08:00
|
|
|
* follow, so add trailing comma */
|
2012-11-17 05:37:43 +08:00
|
|
|
bool_t lastrow, /* true if this is the last row for this
|
2010-06-03 21:24:43 +08:00
|
|
|
* variable, so terminate with ";" instead
|
|
|
|
* of "," */
|
|
|
|
const char *vals /* pointer to block of values */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
long iel;
|
|
|
|
const char *sp;
|
|
|
|
|
|
|
|
printf("\"");
|
|
|
|
/* adjust len so trailing nulls don't get printed */
|
|
|
|
sp = vals + len;
|
|
|
|
while (len != 0 && *--sp == '\0')
|
|
|
|
len--;
|
|
|
|
for (iel = 0; iel < len; iel++) {
|
|
|
|
unsigned char uc;
|
|
|
|
switch (uc = *vals++ & 0377) {
|
|
|
|
case '\b':
|
|
|
|
printf("\\b");
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
printf("\\f");
|
|
|
|
break;
|
|
|
|
case '\n': /* generate linebreaks after new-lines */
|
|
|
|
printf("\\n\",\n \"");
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
printf("\\r");
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
printf("\\t");
|
|
|
|
break;
|
|
|
|
case '\v':
|
|
|
|
printf("\\v");
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
printf("\\\\");
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
printf("\\\'");
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
printf("\\\"");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (isprint(uc))
|
|
|
|
printf("%c",uc);
|
|
|
|
else
|
|
|
|
printf("\\%.3o",uc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\"");
|
|
|
|
lastdelim2x (more, lastrow);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print a number of variable values for NcML
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pr_any_valsx(
|
|
|
|
const ncvar_t *vp, /* variable */
|
|
|
|
size_t len, /* number of values to print */
|
2012-11-17 05:37:43 +08:00
|
|
|
bool_t more, /* true if more data for this row will
|
2010-06-03 21:24:43 +08:00
|
|
|
* follow, so add trailing comma */
|
2012-11-17 05:37:43 +08:00
|
|
|
bool_t lastrow, /* true if this is the last row for this
|
2010-06-03 21:24:43 +08:00
|
|
|
* variable, so terminate with ";" instead
|
|
|
|
* of "," */
|
|
|
|
const void *vals /* pointer to block of values */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
long iel;
|
|
|
|
safebuf_t *sb = sbuf_new();
|
|
|
|
const char *valp = (const char *)vals;
|
|
|
|
|
|
|
|
for (iel = 0; iel < len-1; iel++) {
|
|
|
|
print_any_val(sb, vp, (void *)valp);
|
|
|
|
valp += vp->tinfo->size; /* next value according to type */
|
|
|
|
sbuf_cat(sb, " ");
|
|
|
|
lput(sbuf_str(sb));
|
|
|
|
}
|
|
|
|
print_any_val(sb, vp, (void *)valp);
|
|
|
|
lput(sbuf_str(sb));
|
|
|
|
lastdelim2x (more, lastrow);
|
|
|
|
sbuf_free(sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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 */
|
2011-09-01 02:28:08 +08:00
|
|
|
int varid /* variable id */
|
2010-06-03 21:24:43 +08:00
|
|
|
)
|
|
|
|
{
|
2010-07-30 06:41:05 +08:00
|
|
|
size_t *cor; /* corner coordinates */
|
|
|
|
size_t *edg; /* edges of hypercube */
|
|
|
|
size_t *add; /* "odometer" increment to next "row" */
|
2010-06-03 21:24:43 +08:00
|
|
|
void *vals;
|
|
|
|
|
|
|
|
int id;
|
|
|
|
int ir;
|
|
|
|
size_t nels;
|
|
|
|
size_t ncols;
|
|
|
|
size_t nrows;
|
|
|
|
int vrank = vp->ndims;
|
|
|
|
|
2010-08-11 12:58:14 +08:00
|
|
|
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));
|
2010-07-30 06:41:05 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
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" */
|
2012-11-17 05:37:43 +08:00
|
|
|
vals = emalloc(ncols * vp->tinfo->size);
|
2016-01-01 05:16:47 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
for (ir = 0; ir < nrows; ir++) {
|
2014-08-08 02:49:36 +08:00
|
|
|
size_t corsav = 0;
|
2012-11-17 05:37:43 +08:00
|
|
|
bool_t lastrow;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
if (vrank > 0) {
|
|
|
|
corsav = cor[vrank-1];
|
|
|
|
}
|
2012-11-17 05:37:43 +08:00
|
|
|
lastrow = (bool_t)(ir == nrows-1);
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2012-11-17 05:37:43 +08:00
|
|
|
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 */
|
2016-01-01 05:16:47 +08:00
|
|
|
if(vp->type == NC_CHAR &&
|
2018-05-12 22:55:51 +08:00
|
|
|
(vp->fmt == 0 || NCSTREQ(vp->fmt,"%s") || NCSTREQ(vp->fmt,""))) {
|
2012-11-17 05:37:43 +08:00
|
|
|
pr_tvalsx(vp, ncols, 0, lastrow, (char *) vals);
|
|
|
|
} else {
|
|
|
|
pr_any_valsx(vp, ncols, 0, lastrow, vals);
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
2016-01-01 05:16:47 +08:00
|
|
|
|
2012-11-17 05:37:43 +08:00
|
|
|
if (vrank > 0)
|
|
|
|
cor[vrank-1] += ncols;
|
2016-01-01 05:16:47 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
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);
|
2010-07-30 06:41:05 +08:00
|
|
|
free(cor);
|
|
|
|
free(edg);
|
|
|
|
free(add);
|
2010-06-03 21:24:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|