Doxygen Source Code Documentation
vardata.c File Reference
#include <stdio.h>#include <ctype.h>#include <stdlib.h>#include <string.h>#include <float.h>#include <netcdf.h>#include "ncdump.h"#include "dumplib.h"#include "vardata.h"Go to the source code of this file.
Defines | |
| #define | STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0) |
| #define | absval(x) ( (x) < 0 ? -(x) : (x) ) |
| #define | VALBUFSIZ 1000 |
Functions | |
| float | float_epsilon (void) |
| double | double_epsilon (void) |
| void | init_epsilons (void) |
| void | printbval (char *sout, const char *fmt, const struct ncvar *varp, signed char val) |
| void | printsval (char *sout, const char *fmt, const struct ncvar *varp, short val) |
| void | printival (char *sout, const char *fmt, const struct ncvar *varp, int val) |
| void | printfval (char *sout, const char *fmt, const struct ncvar *varp, float val) |
| void | printdval (char *sout, const char *fmt, const struct ncvar *varp, double val) |
| void | lastdelim (boolean more, boolean lastrow) |
| void | annotate (const struct ncvar *vp, const struct fspec *fsp, const size_t *cor, long iel) |
| void | pr_tvals (const struct ncvar *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const char *vals, const struct fspec *fsp, const size_t *cor) |
| void | pr_bvals (const struct ncvar *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const signed char *vals, const struct fspec *fsp, const size_t *cor) |
| void | pr_svals (const struct ncvar *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const short *vals, const struct fspec *fsp, const size_t *cor) |
| void | pr_ivals (const struct ncvar *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const int *vals, const struct fspec *fsp, const size_t *cor) |
| void | pr_fvals (const struct ncvar *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const float *vals, const struct fspec *fsp, const size_t *cor) |
| void | pr_dvals (const struct ncvar *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const double *vals, const struct fspec *fsp, const size_t *cor) |
| int | upcorner (const size_t *dims, int ndims, size_t *odom, const size_t *add) |
| void | lastdelim2 (boolean more, boolean lastrow) |
| int | vardata (const struct ncvar *vp, size_t vdims[], int ncid, int varid, const struct fspec *fsp) |
Variables | |
| float | float_eps |
| double | double_eps |
Define Documentation
|
|
Definition at line 203 of file vardata.c. Referenced by printdval(), printfval(), and qh_maxabsval(). |
|
|
|
|
|
|
Function Documentation
|
||||||||||||||||||||
|
Definition at line 298 of file vardata.c. References fspec::data_lang, LANG_C, LANG_F, ncvar::name, ncvar::ndims, and Printf. Referenced by pr_bvals(), pr_dvals(), pr_fvals(), pr_ivals(), pr_svals(), and pr_tvals().
00304 {
00305 int vrank = vp->ndims;
00306 int id;
00307
00308 /* print indices according to data_lang */
00309 (void) printf(" // %s(", vp->name);
00310 switch (fsp->data_lang) {
00311 case LANG_C:
00312 /* C variable indices */
00313 for (id = 0; id < vrank-1; id++)
00314 Printf("%lu,", (unsigned long) cor[id]);
00315 Printf("%lu", (unsigned long) cor[id] + iel);
00316 break;
00317 case LANG_F:
00318 /* Fortran variable indices */
00319 Printf("%lu", (unsigned long) cor[vrank-1] + iel + 1);
00320 for (id = vrank-2; id >=0 ; id--) {
00321 Printf(",%lu", 1 + (unsigned long) cor[id]);
00322 }
00323 break;
00324 }
00325 Printf(")\n ");
00326 }
|
|
|
Definition at line 95 of file vardata.c. References double_eps. Referenced by init_epsilons().
00096 {
00097 double double_eps;
00098 #ifndef NO_FLOAT_H
00099 double_eps = DBL_EPSILON;
00100 #else /* NO_FLOAT_H */
00101 {
00102 double etop, ebot, eps;
00103 double one = 1.0;
00104 double two = 2.0;
00105 etop = 1.0;
00106 ebot = 0.0;
00107 eps = ebot + (etop - ebot)/two;
00108 while (eps != ebot && eps != etop) {
00109 double epsp1;
00110
00111 epsp1 = one + eps;
00112 if (epsp1 > one)
00113 etop = eps;
00114 else
00115 ebot = eps;
00116 eps = ebot + (etop - ebot)/two;
00117 }
00118 double_eps = two * etop;
00119 }
00120 #endif /* NO_FLOAT_H */
00121 return double_eps;
00122 }
|
|
|
Definition at line 64 of file vardata.c. References float_eps. Referenced by init_epsilons().
00065 {
00066 float float_eps;
00067 #ifndef NO_FLOAT_H
00068 float_eps = FLT_EPSILON;
00069 #else /* NO_FLOAT_H */
00070 {
00071 float etop, ebot, eps;
00072 float one = 1.0;
00073 float two = 2.0;
00074 etop = 1.0;
00075 ebot = 0.0;
00076 eps = ebot + (etop - ebot)/two;
00077 while (eps != ebot && eps != etop) {
00078 float epsp1;
00079
00080 epsp1 = one + eps;
00081 if (epsp1 > one)
00082 etop = eps;
00083 else
00084 ebot = eps;
00085 eps = ebot + (etop - ebot)/two;
00086 }
00087 float_eps = two * etop;
00088 }
00089 #endif /* NO_FLOAT_H */
00090 return float_eps;
00091 }
|
|
|
Definition at line 126 of file vardata.c. References double_eps, double_epsilon(), float_eps, and float_epsilon(). Referenced by vardata().
00127 {
00128 float_eps = float_epsilon();
00129 double_eps = double_epsilon();
00130 }
|
|
||||||||||||
|
Definition at line 261 of file vardata.c. References Printf. Referenced by pr_bvals(), pr_dvals(), pr_fvals(), pr_ivals(), pr_svals(), and pr_tvals().
|
|
||||||||||||
|
Definition at line 278 of file vardata.c. References lput(). Referenced by pr_bvals(), pr_dvals(), pr_fvals(), pr_ivals(), pr_svals(), and pr_tvals().
|
|
||||||||||||||||||||||||||||||||||||
|
Definition at line 436 of file vardata.c. References annotate(), fspec::full_data_cmnts, lastdelim(), lastdelim2(), lput(), printbval(), and Printf. Referenced by vardata().
00452 {
00453 long iel;
00454 char sout[100]; /* temporary string for each encoded output */
00455
00456 for (iel = 0; iel < len-1; iel++) {
00457 printbval(sout, fmt, vp, *vals++);
00458 if (fsp->full_data_cmnts) {
00459 Printf(sout);
00460 Printf(",");
00461 annotate (vp, fsp, cor, iel);
00462 } else {
00463 (void) strcat(sout, ", ");
00464 lput(sout);
00465 }
00466 }
00467 printbval(sout, fmt, vp, *vals++);
00468 if (fsp->full_data_cmnts) {
00469 Printf(sout);
00470 lastdelim (more, lastrow);
00471 annotate (vp, fsp, cor, iel);
00472 } else {
00473 lput(sout);
00474 lastdelim2 (more, lastrow);
00475 }
00476 }
|
|
||||||||||||||||||||||||||||||||||||
|
Definition at line 630 of file vardata.c. References annotate(), fspec::full_data_cmnts, lastdelim(), lastdelim2(), lput(), printdval(), and Printf. Referenced by vardata().
00646 {
00647 long iel;
00648 char sout[100]; /* temporary string for each encoded output */
00649
00650 for (iel = 0; iel < len-1; iel++) {
00651 printdval(sout, fmt, vp, *vals++);
00652 if (fsp->full_data_cmnts) {
00653 Printf(sout);
00654 Printf(",");
00655 annotate (vp, fsp, cor, iel);
00656 } else {
00657 (void) strcat(sout, ", ");
00658 lput(sout);
00659 }
00660 }
00661 printdval(sout, fmt, vp, *vals++);
00662 if (fsp->full_data_cmnts) {
00663 Printf(sout);
00664 lastdelim (more, lastrow);
00665 annotate (vp, fsp, cor, iel);
00666 } else {
00667 lput(sout);
00668 lastdelim2 (more, lastrow);
00669 }
00670 }
|
|
||||||||||||||||||||||||||||||||||||
|
Definition at line 582 of file vardata.c. References annotate(), fspec::full_data_cmnts, lastdelim(), lastdelim2(), lput(), Printf, and printfval(). Referenced by vardata().
00598 {
00599 long iel;
00600 char sout[100]; /* temporary string for each encoded output */
00601
00602 for (iel = 0; iel < len-1; iel++) {
00603 printfval(sout, fmt, vp, *vals++);
00604 if (fsp->full_data_cmnts) {
00605 Printf(sout);
00606 Printf(",");
00607 annotate (vp, fsp, cor, iel);
00608 } else {
00609 (void) strcat(sout, ", ");
00610 lput(sout);
00611 }
00612 }
00613 printfval(sout, fmt, vp, *vals++);
00614 if (fsp->full_data_cmnts) {
00615 Printf(sout);
00616 lastdelim (more, lastrow);
00617 annotate (vp, fsp, cor, iel);
00618 } else {
00619 lput(sout);
00620 lastdelim2 (more, lastrow);
00621 }
00622 }
|
|
||||||||||||||||||||||||||||||||||||
|
Definition at line 534 of file vardata.c. References annotate(), fspec::full_data_cmnts, lastdelim(), lastdelim2(), lput(), Printf, and printival(). Referenced by vardata().
00550 {
00551 long iel;
00552 char sout[100]; /* temporary string for each encoded output */
00553
00554 for (iel = 0; iel < len-1; iel++) {
00555 printival(sout, fmt, vp, *vals++);
00556 if (fsp->full_data_cmnts) {
00557 Printf(sout);
00558 Printf(",");
00559 annotate (vp, fsp, cor, iel);
00560 } else {
00561 (void) strcat(sout, ", ");
00562 lput(sout);
00563 }
00564 }
00565 printival(sout, fmt, vp, *vals++);
00566 if (fsp->full_data_cmnts) {
00567 Printf(sout);
00568 lastdelim (more, lastrow);
00569 annotate (vp, fsp, cor, iel);
00570 } else {
00571 lput(sout);
00572 lastdelim2 (more, lastrow);
00573 }
00574 }
|
|
||||||||||||||||||||||||||||||||||||
|
Definition at line 484 of file vardata.c. References annotate(), fspec::full_data_cmnts, lastdelim(), lastdelim2(), lput(), Printf, and printsval(). Referenced by vardata().
00500 {
00501 long iel;
00502 char sout[100]; /* temporary string for each encoded output */
00503
00504 for (iel = 0; iel < len-1; iel++) {
00505 printsval(sout, fmt, vp, *vals++);
00506 if (fsp->full_data_cmnts) {
00507 Printf(sout);
00508 Printf(",");
00509 annotate (vp, fsp, cor, iel);
00510 } else {
00511 (void) strcat(sout, ", ");
00512 lput(sout);
00513 }
00514 }
00515 printsval(sout, fmt, vp, *vals++);
00516 if (fsp->full_data_cmnts) {
00517 Printf(sout);
00518 lastdelim (more, lastrow);
00519 annotate (vp, fsp, cor, iel);
00520 } else {
00521 lput(sout);
00522 lastdelim2 (more, lastrow);
00523 }
00524 }
|
|
||||||||||||||||||||||||||||||||||||
|
Definition at line 334 of file vardata.c. References annotate(), fspec::full_data_cmnts, L, lastdelim(), lastdelim2(), lput(), Printf, and STREQ. Referenced by vardata().
00350 {
00351 long iel;
00352 const char *sp;
00353 unsigned char uc;
00354 char sout[100]; /* temporary string for each encoded output */
00355
00356 if (fmt == 0 || STREQ(fmt,"%s") || STREQ(fmt,"")) { /* as string */
00357 Printf("\"");
00358 /* adjust len so trailing nulls don't get printed */
00359 sp = vals + len;
00360 while (len != 0 && *--sp == '\0')
00361 len--;
00362 for (iel = 0; iel < len; iel++)
00363 switch (uc = *vals++ & 0377) {
00364 case '\b':
00365 Printf("\\b");
00366 break;
00367 case '\f':
00368 Printf("\\f");
00369 break;
00370 case '\n': /* generate linebreaks after new-lines */
00371 Printf("\\n\",\n \"");
00372 break;
00373 case '\r':
00374 Printf("\\r");
00375 break;
00376 case '\t':
00377 Printf("\\t");
00378 break;
00379 case '\v':
00380 Printf("\\v");
00381 break;
00382 case '\\':
00383 Printf("\\\\");
00384 break;
00385 case '\'':
00386 Printf("\\\'");
00387 break;
00388 case '\"':
00389 Printf("\\\"");
00390 break;
00391 default:
00392 if (isprint(uc))
00393 Printf("%c",uc);
00394 else
00395 Printf("\\%.3o",uc);
00396 break;
00397 }
00398 Printf("\"");
00399 if (fsp->full_data_cmnts) {
00400 Printf("\"");
00401 lastdelim (more, lastrow);
00402 annotate (vp, fsp, (size_t *)cor, 0L);
00403 }
00404 } else { /* use format from C_format attribute */
00405 for (iel = 0; iel < len-1; iel++) {
00406 if (fsp->full_data_cmnts) {
00407 Printf(fmt, *vals++);
00408 Printf(", ");
00409 annotate (vp, fsp, (size_t *)cor, iel);
00410 } else {
00411 (void) sprintf(sout, fmt, *vals++);
00412 (void) strcat(sout, ", ");
00413 lput(sout);
00414 }
00415 }
00416 if (fsp->full_data_cmnts) {
00417 Printf(fmt, *vals++);
00418 lastdelim (more, lastrow);
00419 annotate (vp, fsp, (size_t *)cor, iel);
00420 } else {
00421 (void) sprintf(sout, fmt, *vals++);
00422 lput(sout);
00423 }
00424 }
00425 if (!fsp->full_data_cmnts) {
00426 lastdelim2 (more, lastrow);
00427 }
00428 }
|
|
||||||||||||||||||||
|
Definition at line 138 of file vardata.c. References FILL_STRING, ncvar::fillval, and ncvar::has_fillval. Referenced by pr_bvals().
00144 {
00145 if (varp->has_fillval) {
00146 double fillval = varp->fillval;
00147 if(fillval == val) {
00148 (void) sprintf(sout, FILL_STRING);
00149 return;
00150 }
00151 }
00152 (void) sprintf(sout, fmt, val);
00153 }
|
|
||||||||||||||||||||
|
Definition at line 238 of file vardata.c. References absval, double_eps, FILL_STRING, ncvar::fillval, and ncvar::has_fillval. Referenced by pr_dvals().
00244 {
00245 if(varp->has_fillval) {
00246 double fillval = varp->fillval;
00247 if((val > 0) == (fillval > 0) && /* prevents potential overflow */
00248 (absval(val - fillval) <= absval(double_eps * fillval))) {
00249 (void) sprintf(sout, FILL_STRING);
00250 return;
00251 }
00252 }
00253 (void) sprintf(sout, fmt, val);
00254 }
|
|
||||||||||||||||||||
|
Definition at line 212 of file vardata.c. References absval, FILL_STRING, ncvar::fillval, float_eps, and ncvar::has_fillval. Referenced by pr_fvals().
00218 {
00219 if(varp->has_fillval) {
00220 double fillval = varp->fillval;
00221 if((val > 0) == (fillval > 0) && /* prevents potential overflow */
00222 (absval(val - fillval) <= absval(float_eps * fillval))) {
00223 (void) sprintf(sout, FILL_STRING);
00224 return;
00225 }
00226 }
00227 (void) sprintf(sout, fmt, val);
00228 }
|
|
||||||||||||||||||||
|
Definition at line 185 of file vardata.c. References FILL_STRING, ncvar::fillval, and ncvar::has_fillval. Referenced by pr_ivals().
00191 {
00192 if (varp->has_fillval) {
00193 int fillval = (int)varp->fillval;
00194 if(fillval == val) {
00195 (void) sprintf(sout, FILL_STRING);
00196 return;
00197 }
00198 }
00199 (void) sprintf(sout, fmt, val);
00200 }
|
|
||||||||||||||||||||
|
Definition at line 161 of file vardata.c. References FILL_STRING, ncvar::fillval, and ncvar::has_fillval. Referenced by pr_svals().
00167 {
00168 if (varp->has_fillval) {
00169 double fillval = varp->fillval;
00170 if(fillval == val) {
00171 (void) sprintf(sout, FILL_STRING);
00172 return;
00173 }
00174 }
00175 (void) sprintf(sout, fmt, val);
00176 }
|
|
||||||||||||||||||||
|
Definition at line 678 of file vardata.c. References ndims. Referenced by vardata().
00684 {
00685 int id;
00686 int ret = 1;
00687
00688 for (id = ndims-1; id > 0; id--) {
00689 odom[id] += add[id];
00690 if(odom[id] >= dims[id]) {
00691 odom[id-1]++;
00692 odom[id] -= dims[id];
00693 }
00694 }
00695 odom[0] += add[0];
00696 if (odom[0] >= dims[0])
00697 ret = 0;
00698 return ret;
00699 }
|
|
||||||||||||||||||||||||
|
Definition at line 704 of file vardata.c. References fspec::brief_data_cmnts, fspec::data_lang, error(), get_fmt(), init_epsilons(), LANG_C, LANG_F, left, ncvar::name, NC_CHECK, nc_get_vara_double(), nc_get_vara_float(), nc_get_vara_int(), nc_get_vara_schar(), nc_get_vara_short(), nc_get_vara_text(), ncvar::ndims, pr_bvals(), pr_dvals(), pr_fvals(), pr_ivals(), pr_svals(), pr_tvals(), Printf, set_indent(), ncvar::type, and upcorner(). Referenced by do_ncdump().
00711 {
00712 size_t cor[NC_MAX_DIMS]; /* corner coordinates */
00713 size_t edg[NC_MAX_DIMS]; /* edges of hypercube */
00714 size_t add[NC_MAX_DIMS]; /* "odometer" increment to next "row" */
00715 #define VALBUFSIZ 1000
00716 double vals[VALBUFSIZ] ; /* aligned buffer */
00717
00718 int gulp = VALBUFSIZ;
00719
00720 int id;
00721 int ir;
00722 size_t nels;
00723 size_t ncols;
00724 size_t nrows;
00725 int vrank = vp->ndims;
00726 static int initeps = 0;
00727
00728 /* printf format used to print each value */
00729 char *fmt = get_fmt(ncid, varid, vp->type);
00730
00731 if (!initeps) { /* make sure epsilons get initialized */
00732 init_epsilons();
00733 initeps = 1;
00734 }
00735
00736 nels = 1;
00737 for (id = 0; id < vrank; id++) {
00738 cor[id] = 0;
00739 edg[id] = 1;
00740 nels *= vdims[id]; /* total number of values for variable */
00741 }
00742
00743 if (vrank <= 1) {
00744 Printf("\n %s = ", vp->name);
00745 set_indent ((int)strlen(vp->name) + 4);
00746 } else {
00747 Printf("\n %s =\n ", vp->name);
00748 set_indent (2);
00749 }
00750
00751 if (vrank < 1) {
00752 ncols = 1;
00753 } else {
00754 ncols = vdims[vrank-1]; /* size of "row" along last dimension */
00755 edg[vrank-1] = vdims[vrank-1];
00756 for (id = 0; id < vrank; id++)
00757 add[id] = 0;
00758 if (vrank > 1)
00759 add[vrank-2] = 1;
00760 }
00761 nrows = nels/ncols; /* number of "rows" */
00762
00763 for (ir = 0; ir < nrows; ir++) {
00764 /*
00765 * rather than just printing a whole row at once (which might exceed
00766 * the capacity of MSDOS platforms, for example), we break each row
00767 * into smaller chunks, if necessary.
00768 */
00769 size_t corsav;
00770 int left = (int)ncols;
00771 boolean lastrow;
00772
00773 if (vrank > 0) {
00774 corsav = cor[vrank-1];
00775 if (fsp->brief_data_cmnts != false
00776 && vrank > 1
00777 && left > 0) { /* print brief comment with indices range */
00778 Printf("// %s(",vp->name);
00779 switch (fsp->data_lang) {
00780 case LANG_C:
00781 /* print brief comment with C variable indices */
00782 for (id = 0; id < vrank-1; id++)
00783 Printf("%lu,", (unsigned long)cor[id]);
00784 if (vdims[vrank-1] == 1)
00785 Printf("0");
00786 else
00787 Printf(" 0-%lu", (unsigned long)vdims[vrank-1]-1);
00788 break;
00789 case LANG_F:
00790 /* print brief comment with Fortran variable indices */
00791 if (vdims[vrank-1] == 1)
00792 Printf("1");
00793 else
00794 Printf("1-%lu ", (unsigned long)vdims[vrank-1]);
00795 for (id = vrank-2; id >=0 ; id--) {
00796 Printf(",%lu", (unsigned long)(1 + cor[id]));
00797 }
00798 break;
00799 }
00800 Printf(")\n ");
00801 set_indent(4);
00802 }
00803 }
00804 lastrow = (boolean)(ir == nrows-1);
00805 while (left > 0) {
00806 size_t toget = left < gulp ? left : gulp;
00807 if (vrank > 0)
00808 edg[vrank-1] = toget;
00809 switch(vp->type) {
00810 case NC_CHAR:
00811 NC_CHECK(
00812 nc_get_vara_text(ncid, varid, cor, edg, (char *)vals) );
00813 pr_tvals(vp, toget, fmt, left > toget, lastrow,
00814 (char *) vals, fsp, cor);
00815 break;
00816 case NC_BYTE:
00817 NC_CHECK(
00818 nc_get_vara_schar(ncid, varid, cor, edg, (signed char *)vals) );
00819 pr_bvals(vp, toget, fmt, left > toget, lastrow,
00820 (signed char *) vals, fsp, cor);
00821 break;
00822 case NC_SHORT:
00823 NC_CHECK(
00824 nc_get_vara_short(ncid, varid, cor, edg, (short *)vals) );
00825 pr_svals(vp, toget, fmt, left > toget, lastrow,
00826 (short *) vals, fsp, cor);
00827 break;
00828 case NC_INT:
00829 NC_CHECK(
00830 nc_get_vara_int(ncid, varid, cor, edg, (int *)vals) );
00831 pr_ivals(vp, toget, fmt, left > toget, lastrow,
00832 (int *) vals, fsp, cor);
00833 break;
00834 case NC_FLOAT:
00835 NC_CHECK(
00836 nc_get_vara_float(ncid, varid, cor, edg, (float *)vals) );
00837 pr_fvals(vp, toget, fmt, left > toget, lastrow,
00838 (float *) vals, fsp, cor);
00839 break;
00840 case NC_DOUBLE:
00841 NC_CHECK(
00842 nc_get_vara_double(ncid, varid, cor, edg, (double *)vals) );
00843 pr_dvals(vp, toget, fmt, left > toget, lastrow,
00844 (double *) vals, fsp, cor);
00845 break;
00846 default:
00847 error("vardata: bad type");
00848 }
00849 left -= toget;
00850 if (vrank > 0)
00851 cor[vrank-1] += toget;
00852 }
00853 if (vrank > 0)
00854 cor[vrank-1] = corsav;
00855 if (ir < nrows-1)
00856 if (!upcorner(vdims,vp->ndims,cor,add))
00857 error("vardata: odometer overflowed!");
00858 set_indent(2);
00859 }
00860
00861 return 0;
00862 }
|
Variable Documentation
|
|
Definition at line 61 of file vardata.c. Referenced by double_epsilon(), init_epsilons(), and printdval(). |
|
|
Definition at line 60 of file vardata.c. Referenced by float_epsilon(), init_epsilons(), and printfval(). |