示例#1
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;
}
示例#2
0
/*  Print data values for variable varid.  
 *
 * 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
print_rows(     
    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 */
    ) 
{
    int rank = vp->ndims;
    size_t ncols = rank > 0 ? vdims[rank - 1] : 1; /* number of values in a row */
    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 "{}" */
	lput("{");
	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++) {
	    print_rows(level + 1, ncid, varid, vp, vdims, 
		       local_cor, local_edg, vals, 0);
	    local_cor[level] += 1;
	}
	print_rows(level + 1, ncid, varid, vp, vdims, 
		   local_cor, local_edg, vals, marks_pending);
	free(local_edg);
	free(local_cor);
    } else {			/* bottom out of recursion */
	char *valp = vals;
	bool_t lastrow;
	int j;
	if(formatting_specs.brief_data_cmnts && rank > 1 && ncols > 0) {
	    annotate_brief(vp, cor, vdims);
	}
	NC_CHECK(nc_get_vara(ncid, varid, cor, edg, (void *)valp));

	/* Test if we should treat array of chars as strings along last dimension  */
	if(vp->type == NC_CHAR && (vp->fmt == 0 || STREQ(vp->fmt,"%s") || STREQ(vp->fmt,""))) {
	    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));
		}
	    }
	    print_any_val(sb, vp, (void *)valp);
	}
	/* determine if this is the last row */
	lastrow = true;
	for(j = 0; j < rank - 1; j++) {
	    if (cor[j] != vdims[j] - 1) {
		lastrow = false;
		break;
	    }
	}
	if (formatting_specs.full_data_cmnts) {
	    for (j = 0; j < marks_pending; j++) {
		sbuf_cat(sb, "}");
	    }
	    printf("%s", sbuf_str(sb));
	    lastdelim (0, lastrow);
	    annotate (vp, cor, d0);
	} else {
	    for (j = 0; j < marks_pending; j++) {
		sbuf_cat(sb, "}");
	    }
	    lput(sbuf_str(sb));
	    lastdelim2 (0, lastrow);
	}    
    }
    sbuf_free(sb);
    return NC_NOERR;
}
示例#3
0
文件: vardata.c 项目: mmase/wgrib2
/* 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;
}
示例#4
0
/* Output the data for a single variable, in CDL syntax. */
int
vardata(
     const struct ncvar *vp,	/* variable */
     long vdims[],		/* variable dimension sizes */
     int ncid,			/* netcdf id */
     int varid,			/* variable id */
     const struct fspec* fsp	/* formatting specs */
     )
{
    long cor[NC_MAX_DIMS];	/* corner coordinates */
    long edg[NC_MAX_DIMS];	/* edges of hypercube */
    long add[NC_MAX_DIMS];      /* "odometer" increment to next "row"  */
#define VALBUFSIZ 1000
    double vals[VALBUFSIZ] ; /* aligned buffer */

    int gulp = VALBUFSIZ;

    int id;
    int ir;
    long nels;
    long ncols;
    long nrows;
    int vrank = vp->ndims;
    static int initeps = 0;

    /* printf format used to print each value */
    char *fmt = get_fmt(ncid, varid, vp->type);

    if (!initeps) {		/* make sure epsilons get initialized */
	init_epsilons();
	initeps = 1;
    }

    nels = 1;
    for (id = 0; id < vrank; id++) {
	cor[id] = 0;
	edg[id] = 1;
	nels *= vdims[id];	/* total number of values for variable */
    }

    if (vrank <= 1) {
	Printf("\n %s = ", vp->name);
	set_indent ((int)strlen(vp->name) + 4);
    } else {
	Printf("\n %s =\n  ", vp->name);
	set_indent (2);
    }

    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" */
    
    for (ir = 0; ir < nrows; ir++) {
	/*
	 * rather than just printing a whole row at once (which might exceed
	 * the capacity of MSDOS platforms, for example), we break each row
	 * into smaller chunks, if necessary.
	 */
	long corsav;
	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);
		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    ");
		set_indent(4);
	    }
	}
	lastrow = (boolean)(ir == nrows-1);
	while (left > 0) {
	    long toget = left < gulp ? left : gulp;
	    if (vrank > 0)
	      edg[vrank-1] = toget;
	    switch(vp->type) {
	    case NC_CHAR:
		NC_CHECK(
		    ncvarget(ncid, varid, cor, edg, (char *)vals) );
	        pr_tvals(vp, toget, fmt, left > toget, lastrow,
			 (char *) vals, fsp, cor);
		break;
	    case NC_BYTE:
		NC_CHECK(
		    ncvarget(ncid, varid, cor, edg, (signed char *)vals) );
	        pr_bvals(vp, toget, fmt, left > toget, lastrow,
			 (signed char *) vals, fsp, cor);
		break;
	    case NC_SHORT:
		NC_CHECK(
		    ncvarget(ncid, varid, cor, edg, (short *)vals) );
	        pr_svals(vp, toget, fmt, left > toget, lastrow,
			 (short *) vals, fsp, cor);
		break;
	    case NC_INT:
		NC_CHECK(
		    ncvarget(ncid, varid, cor, edg, (int *)vals) );
	        pr_ivals(vp, toget, fmt, left > toget, lastrow,
			 (int *) vals, fsp, cor);
		break;
	    case NC_FLOAT:
		NC_CHECK(
		    ncvarget(ncid, varid, cor, edg, (float *)vals) );
	        pr_fvals(vp, toget, fmt, left > toget, lastrow,
			 (float *) vals, fsp, cor);
		break;
	    case NC_DOUBLE:
		NC_CHECK(
		    ncvarget(ncid, varid, cor, edg, (double *)vals) );
	        pr_dvals(vp, toget, fmt, left > toget, lastrow,
			 (double *) vals, fsp, cor);
		break;
	    default:
		error("vardata: bad type");
	    }
	    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);
    }

    return 0;
}