Esempio n. 1
0
/*
 * 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
 * output string.  If string ends with a newline to force short line,
 * reset indentation after output.
 */
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);
    if (cp[nn - 1] == '\n') {
	linep = indent_get();
    } else
	linep += nn;
}
Esempio n. 2
0
/*
 * 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 */
    )
{
    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());
}
Esempio n. 3
0
/* 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 */
     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((1 + vrank) * sizeof(size_t));
    edg = (size_t *) emalloc((1 + vrank) * sizeof(size_t));
    add = (size_t *) emalloc((1 + vrank) * sizeof(size_t));

    nels = 1;
    if(vrank == 0) { /*scalar*/
	cor[0] = 0;
	edg[0] = 1;
    } else {
	for (id = 0; id < vrank; id++) {
	    cor[id] = 0;
	    edg[id] = 1;
	    nels *= vdims[id];	/* total number of values for variable */
	}
    }
    printf("\n");
    indent_out();
    printf(" ");
    print_name(vp->name);
    if (vrank <= 1) {
	printf(" = ");
	set_indent ((int)strlen(vp->name) + 4 + indent_get());
    } else {
	printf(" =\n  ");
	set_indent (2 + indent_get());
    }

    if (vrank == 0) {
	ncols = 1;
    } else {
	ncols = vdims[vrank-1];	/* size of "row" along last dimension */
	edg[vrank-1] = ncols;
	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);

    /* 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,""))) {
	for (ir = 0; ir < nrows; ir++) {
	    if (vrank > 0) {
		if (formatting_specs.brief_data_cmnts != false && vrank > 1 && ncols > 0) {
		    annotate_brief(vp, cor, vdims);
		}
	    }
	    NC_CHECK(nc_get_vara(ncid, varid, cor, edg, vals));
	    pr_tvals(vp, ncols, (ir == nrows-1), (char *) vals, cor);
	    if (ir < nrows-1)
	      if (!upcorner(vdims, vp->ndims, cor, add))
	        error("vardata: odometer overflowed!");
	    set_indent(2);
	}
    } else {
	int level = 0;
	int rank = vp->ndims;
	int marks_pending = 0;
	NC_CHECK(print_rows(level, ncid, varid, vp, ncols, rank, vdims, cor, edg,
			    vals, marks_pending));
    }
    free(vals);
    free(cor);
    free(edg);
    free(add);

    return 0;
}
Esempio n. 4
0
/* 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 */
     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("\n");
    indent_out();
/* 	printf(" %s = ", vp->name); */
/*          or      */
/* 	printf(" %s =\n  ", vp->name); */
	printf(" ");
	print_name(vp->name);
    if (vrank <= 1) {
	printf(" = ");
	set_indent ((int)strlen(vp->name) + 4 + indent_get());
    } else {
	printf(" =\n  ");
	set_indent (2 + indent_get());
    }

    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 ? ncols : VALBUFSIZ;
    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 = 0;
	int left = (int)ncols;
	boolean lastrow;

	if (vrank > 0) {
	    corsav = cor[vrank-1];
	    if (fsp->brief_data_cmnts != false
		&& vrank > 1
		&& left > 0) {	/* print brief comment with indices range */
/* 		printf("// %s(",vp->name); */
		printf("// ");
		printf(vp->name);
		printf("(");
		switch (fsp->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());
	    }
	}
	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_tvals(vp, toget, left > toget, lastrow, (char *) vals, 
			 fsp, cor);
	    } else {
	        pr_any_vals(vp, toget, left > toget, lastrow, vals, fsp, cor);
	    }

	    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);
    }

    free(vals);
    return 0;
}
Esempio n. 5
0
/* 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 */
     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;
    size_t nels;
    size_t ncols;
    size_t nrows;
    int vrank = vp->ndims;

    int level = 0;
    int marks_pending = 0;

    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));

    nels = 1;
    if(vrank == 0) { /*scalar*/
	cor[0] = 0;
	edg[0] = 1;
    } else {
	for (id = 0; id < vrank; id++) {
	    cor[id] = 0;
	    edg[id] = 1;
	    nels *= vdims[id];	/* total number of values for variable */
	}
    }
    printf("\n");
    indent_out();
    printf(" ");
    print_name(vp->name);
    if (vrank <= 1) {
	printf(" = ");
	set_indent ((int)strlen(vp->name) + 4 + indent_get());
    } else {
	printf(" =\n  ");
	set_indent (2 + indent_get());
    }

    if (vrank == 0) {
	ncols = 1;
    } else {
	ncols = vdims[vrank-1];	/* size of "row" along last dimension */
	edg[vrank-1] = ncols;
	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);
    
    NC_CHECK(print_rows(level, ncid, varid, vp, vdims, cor, edg, vals, marks_pending));
    free(vals);
    free(cor);
    free(edg);
    free(add);

    return 0;
}