ncdf4a13/ncdump/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"

Include dependency graph for vardata.c:

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
#define VALBUFSIZ   1000

Functions

static float float_epsilon (void)
static double double_epsilon (void)
static void init_epsilons (void)
static void printbval (char *sout, const char *fmt, const ncvar_t *varp, signed char val)
static void printsval (char *sout, const char *fmt, const ncvar_t *varp, short val)
static void printival (char *sout, const char *fmt, const ncvar_t *varp, int val)
static void printfval (char *sout, const char *fmt, const ncvar_t *varp, float val)
static void printdval (char *sout, const char *fmt, const ncvar_t *varp, double val)
static void lastdelim (boolean more, boolean lastrow)
static void annotate (const ncvar_t *vp, const fspec_t *fsp, const size_t *cor, long iel)
static void pr_tvals (const ncvar_t *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const char *vals, const fspec_t *fsp, const size_t *cor)
static void pr_bvals (const ncvar_t *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const signed char *vals, const fspec_t *fsp, const size_t *cor)
static void pr_svals (const ncvar_t *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const short *vals, const fspec_t *fsp, const size_t *cor)
static void pr_ivals (const ncvar_t *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const int *vals, const fspec_t *fsp, const size_t *cor)
static void pr_fvals (const ncvar_t *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const float *vals, const fspec_t *fsp, const size_t *cor)
static void pr_dvals (const ncvar_t *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const double *vals, const fspec_t *fsp, const size_t *cor)
static int upcorner (const size_t *dims, int ndims, size_t *odom, const size_t *add)
static void lastdelim2 (boolean more, boolean lastrow)
int vardata (const ncvar_t *vp, size_t vdims[], int ncid, int varid, const fspec_t *fsp)
static void lastdelim2x (boolean more, boolean lastrow)
static void pr_tvalsx (size_t len, const char *fmt, boolean more, boolean lastrow, const char *vals, const fspec_t *fsp)
static void pr_bvalsx (const ncvar_t *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const signed char *vals)
static void pr_svalsx (const ncvar_t *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const short *vals)
static void pr_ivalsx (const ncvar_t *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const int *vals)
static void pr_fvalsx (const ncvar_t *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const float *vals)
static void pr_dvalsx (const ncvar_t *vp, size_t len, const char *fmt, boolean more, boolean lastrow, const double *vals)
int vardatax (const ncvar_t *vp, size_t vdims[], int ncid, int varid, const fspec_t *fsp)

Variables

static float float_eps
static double double_eps


Define Documentation

#define absval  )     ( (x) < 0 ? -(x) : (x) )
 

Definition at line 203 of file vardata.c.

Referenced by printdval(), printfval(), and test_ncvardef().

#define STREQ a,
 )     (*(a) == *(b) && strcmp((a), (b)) == 0)
 

Definition at line 58 of file vardata.c.

#define VALBUFSIZ   1000
 

#define VALBUFSIZ   1000
 

Referenced by vardata(), and vardatax().


Function Documentation

static void annotate const ncvar_t vp,
const fspec_t fsp,
const size_t *  cor,
long  iel
[static]
 

Definition at line 298 of file vardata.c.

References fspec_t::data_lang, LANG_C, LANG_F, ncvar_t::name, ncvar_t::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 }

static double double_epsilon void   )  [static]
 

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 }

static float float_epsilon void   )  [static]
 

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 }

static void init_epsilons void   )  [static]
 

Definition at line 126 of file vardata.c.

References double_eps, double_epsilon(), float_eps, and float_epsilon().

Referenced by test_ncvardef(), vardata(), and vardatax().

00127 {
00128     float_eps = float_epsilon();
00129     double_eps = double_epsilon();
00130 }

static void lastdelim boolean  more,
boolean  lastrow
[static]
 

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().

00262 {
00263     if (more) {
00264         Printf(", ");
00265     } else {
00266         if(lastrow) {
00267             Printf(";");
00268         } else {
00269             Printf(",");
00270         }
00271     }
00272 }

static void lastdelim2 boolean  more,
boolean  lastrow
[static]
 

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().

00279 {
00280     if (more) {
00281         lput(", ");
00282     } else {
00283         if(lastrow) {
00284             lput(" ;");
00285             lput("\n");
00286         } else {
00287             lput(",\n");
00288             lput("  ");
00289         }
00290     }
00291 }

static void lastdelim2x boolean  more,
boolean  lastrow
[static]
 

Definition at line 868 of file vardata.c.

References lput().

Referenced by pr_bvalsx(), pr_dvalsx(), pr_fvalsx(), pr_ivalsx(), pr_svalsx(), and pr_tvalsx().

00869 {
00870     if (more) {
00871         lput(" ");
00872     } else {
00873         if(lastrow) {
00874             lput("\n   ");
00875         } else {
00876             lput("\n     ");
00877         }
00878     }
00879 }

static void pr_bvals const ncvar_t vp,
size_t  len,
const char *  fmt,
boolean  more,
boolean  lastrow,
const signed char *  vals,
const fspec_t fsp,
const size_t *  cor
[static]
 

Definition at line 435 of file vardata.c.

References annotate(), fspec_t::full_data_cmnts, lastdelim(), lastdelim2(), lput(), printbval(), and Printf.

Referenced by vardata().

00451 {
00452     long iel;
00453     char sout[100];             /* temporary string for each encoded output */
00454 
00455     for (iel = 0; iel < len-1; iel++) {
00456         printbval(sout, fmt, vp, *vals++);
00457         if (fsp->full_data_cmnts) {
00458             Printf(sout);
00459             Printf(",");
00460             annotate (vp, fsp, cor, iel);
00461         } else {
00462             (void) strcat(sout, ", ");
00463             lput(sout);
00464         }
00465     }
00466     printbval(sout, fmt, vp, *vals++);
00467     if (fsp->full_data_cmnts) {
00468         Printf(sout);
00469         lastdelim (more, lastrow);
00470         annotate (vp, fsp, cor, iel);
00471     } else {
00472         lput(sout);
00473         lastdelim2 (more, lastrow);
00474     }
00475 }

static void pr_bvalsx const ncvar_t vp,
size_t  len,
const char *  fmt,
boolean  more,
boolean  lastrow,
const signed char *  vals
[static]
 

Definition at line 968 of file vardata.c.

References lastdelim2x(), lput(), and printbval().

Referenced by vardatax().

00982 {
00983     long iel;
00984     char sout[100];             /* temporary string for each encoded output */
00985 
00986     for (iel = 0; iel < len-1; iel++) {
00987         printbval(sout, fmt, vp, *vals++);
00988         (void) strcat(sout, " ");
00989         lput(sout);
00990     }
00991     printbval(sout, fmt, vp, *vals++);
00992     lput(sout);
00993     lastdelim2x (more, lastrow);
00994 }

static void pr_dvals const ncvar_t vp,
size_t  len,
const char *  fmt,
boolean  more,
boolean  lastrow,
const double *  vals,
const fspec_t fsp,
const size_t *  cor
[static]
 

Definition at line 629 of file vardata.c.

References annotate(), fspec_t::full_data_cmnts, lastdelim(), lastdelim2(), lput(), printdval(), and Printf.

Referenced by vardata().

00645 {
00646     long iel;
00647     char sout[100];             /* temporary string for each encoded output */
00648 
00649     for (iel = 0; iel < len-1; iel++) {
00650         printdval(sout, fmt, vp, *vals++);
00651         if (fsp->full_data_cmnts) {
00652             Printf(sout);
00653             Printf(",");
00654             annotate (vp, fsp, cor, iel);
00655         } else {
00656             (void) strcat(sout, ", ");
00657             lput(sout);
00658         }
00659     }
00660     printdval(sout, fmt, vp, *vals++);
00661     if (fsp->full_data_cmnts) {
00662         Printf(sout);
00663         lastdelim (more, lastrow);
00664         annotate (vp, fsp, cor, iel);
00665     } else {
00666         lput(sout);
00667         lastdelim2 (more, lastrow);
00668     }
00669 }

static void pr_dvalsx const ncvar_t vp,
size_t  len,
const char *  fmt,
boolean  more,
boolean  lastrow,
const double *  vals
[static]
 

Definition at line 1102 of file vardata.c.

References lastdelim2x(), lput(), and printdval().

Referenced by vardatax().

01116 {
01117     long iel;
01118     char sout[100];             /* temporary string for each encoded output */
01119 
01120     for (iel = 0; iel < len-1; iel++) {
01121         printdval(sout, fmt, vp, *vals++);
01122         (void) strcat(sout, " ");
01123         lput(sout);
01124     }
01125     printdval(sout, fmt, vp, *vals++);
01126     lput(sout);
01127     lastdelim2x (more, lastrow);
01128 }

static void pr_fvals const ncvar_t vp,
size_t  len,
const char *  fmt,
boolean  more,
boolean  lastrow,
const float *  vals,
const fspec_t fsp,
const size_t *  cor
[static]
 

Definition at line 581 of file vardata.c.

References annotate(), fspec_t::full_data_cmnts, lastdelim(), lastdelim2(), lput(), Printf, and printfval().

Referenced by vardata().

00597 {
00598     long iel;
00599     char sout[100];             /* temporary string for each encoded output */
00600 
00601     for (iel = 0; iel < len-1; iel++) {
00602         printfval(sout, fmt, vp, *vals++);
00603         if (fsp->full_data_cmnts) {
00604             Printf(sout);
00605             Printf(",");
00606             annotate (vp, fsp, cor, iel);
00607         } else {
00608             (void) strcat(sout, ", ");
00609             lput(sout);
00610         }
00611     }
00612     printfval(sout, fmt, vp, *vals++);
00613     if (fsp->full_data_cmnts) {
00614         Printf(sout);
00615         lastdelim (more, lastrow);
00616         annotate (vp, fsp, cor, iel);
00617     } else {
00618         lput(sout);
00619         lastdelim2 (more, lastrow);
00620     }
00621 }

static void pr_fvalsx const ncvar_t vp,
size_t  len,
const char *  fmt,
boolean  more,
boolean  lastrow,
const float *  vals
[static]
 

Definition at line 1069 of file vardata.c.

References lastdelim2x(), lput(), and printfval().

Referenced by vardatax().

01083 {
01084     long iel;
01085     char sout[100];             /* temporary string for each encoded output */
01086 
01087     for (iel = 0; iel < len-1; iel++) {
01088         printfval(sout, fmt, vp, *vals++);
01089         (void) strcat(sout, " ");
01090         lput(sout);
01091     }
01092     printfval(sout, fmt, vp, *vals++);
01093     lput(sout);
01094     lastdelim2x (more, lastrow);
01095 }

static void pr_ivals const ncvar_t vp,
size_t  len,
const char *  fmt,
boolean  more,
boolean  lastrow,
const int *  vals,
const fspec_t fsp,
const size_t *  cor
[static]
 

Definition at line 533 of file vardata.c.

References annotate(), fspec_t::full_data_cmnts, lastdelim(), lastdelim2(), lput(), Printf, and printival().

Referenced by vardata().

00549 {
00550     long iel;
00551     char sout[100];             /* temporary string for each encoded output */
00552 
00553     for (iel = 0; iel < len-1; iel++) {
00554         printival(sout, fmt, vp, *vals++);
00555         if (fsp->full_data_cmnts) {
00556             Printf(sout);
00557             Printf(",");
00558             annotate (vp, fsp, cor, iel);
00559         } else {
00560             (void) strcat(sout, ", ");
00561             lput(sout);
00562         }
00563     }
00564     printival(sout, fmt, vp, *vals++);
00565     if (fsp->full_data_cmnts) {
00566         Printf(sout);
00567         lastdelim (more, lastrow);
00568         annotate (vp, fsp, cor, iel);
00569     } else {
00570         lput(sout);
00571         lastdelim2 (more, lastrow);
00572     }
00573 }

static void pr_ivalsx const ncvar_t vp,
size_t  len,
const char *  fmt,
boolean  more,
boolean  lastrow,
const int *  vals
[static]
 

Definition at line 1036 of file vardata.c.

References lastdelim2x(), lput(), and printival().

Referenced by vardatax().

01050 {
01051     long iel;
01052     char sout[100];             /* temporary string for each encoded output */
01053 
01054     for (iel = 0; iel < len-1; iel++) {
01055         printival(sout, fmt, vp, *vals++);
01056         (void) strcat(sout, " ");
01057         lput(sout);
01058     }
01059     printival(sout, fmt, vp, *vals++);
01060     lput(sout);
01061     lastdelim2x (more, lastrow);
01062 }

static void pr_svals const ncvar_t vp,
size_t  len,
const char *  fmt,
boolean  more,
boolean  lastrow,
const short *  vals,
const fspec_t fsp,
const size_t *  cor
[static]
 

Definition at line 483 of file vardata.c.

References annotate(), fspec_t::full_data_cmnts, lastdelim(), lastdelim2(), lput(), Printf, and printsval().

Referenced by vardata().

00499 {
00500     long iel;
00501     char sout[100];             /* temporary string for each encoded output */
00502 
00503     for (iel = 0; iel < len-1; iel++) {
00504         printsval(sout, fmt, vp, *vals++);
00505         if (fsp->full_data_cmnts) {
00506             Printf(sout);
00507             Printf(",");
00508             annotate (vp, fsp, cor, iel);
00509         } else {
00510             (void) strcat(sout, ", ");
00511             lput(sout);
00512         }
00513     }
00514     printsval(sout, fmt, vp, *vals++);
00515     if (fsp->full_data_cmnts) {
00516         Printf(sout);
00517         lastdelim (more, lastrow);
00518         annotate (vp, fsp, cor, iel);
00519     } else {
00520         lput(sout);
00521         lastdelim2 (more, lastrow);
00522     }
00523 }

static void pr_svalsx const ncvar_t vp,
size_t  len,
const char *  fmt,
boolean  more,
boolean  lastrow,
const short *  vals
[static]
 

Definition at line 1001 of file vardata.c.

References lastdelim2x(), lput(), and printsval().

Referenced by vardatax().

01015 {
01016     long iel;
01017     char sout[100];             /* temporary string for each encoded output */
01018 
01019     for (iel = 0; iel < len-1; iel++) {
01020         printsval(sout, fmt, vp, *vals++);
01021         (void) strcat(sout, " ");
01022         lput(sout);
01023     }
01024     printsval(sout, fmt, vp, *vals++);
01025     lput(sout);
01026     lastdelim2x (more, lastrow);
01027 }

static void pr_tvals const ncvar_t vp,
size_t  len,
const char *  fmt,
boolean  more,
boolean  lastrow,
const char *  vals,
const fspec_t fsp,
const size_t *  cor
[static]
 

Definition at line 334 of file vardata.c.

References annotate(), fspec_t::full_data_cmnts, 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             lastdelim (more, lastrow);
00401             annotate (vp, fsp,  (size_t *)cor, 0L);
00402         }
00403     } else {            /* use format from C_format attribute */
00404         for (iel = 0; iel < len-1; iel++) {
00405             if (fsp->full_data_cmnts) {
00406                 Printf(fmt, *vals++);
00407                 Printf(", ");
00408                 annotate (vp, fsp,  (size_t *)cor, iel);
00409             } else {
00410                 (void) sprintf(sout, fmt, *vals++);
00411                 (void) strcat(sout, ", ");
00412                 lput(sout);
00413             }
00414         }
00415         if (fsp->full_data_cmnts) {
00416             Printf(fmt, *vals++);
00417             lastdelim (more, lastrow);
00418             annotate (vp, fsp,  (size_t *)cor, iel);
00419         } else {
00420             (void) sprintf(sout, fmt, *vals++);
00421             lput(sout);
00422         }
00423     }
00424     if (!fsp->full_data_cmnts) {
00425         lastdelim2 (more, lastrow);
00426     }
00427 }

static void pr_tvalsx size_t  len,
const char *  fmt,
boolean  more,
boolean  lastrow,
const char *  vals,
const fspec_t fsp
[static]
 

Definition at line 886 of file vardata.c.

References fspec_t::full_data_cmnts, lastdelim2x(), lput(), Printf, and STREQ.

Referenced by vardatax().

00900 {
00901     long iel;
00902     const char *sp;
00903     unsigned char uc;
00904     char sout[100];             /* temporary string for each encoded output */
00905 
00906     if (fmt == 0 || STREQ(fmt,"%s") || STREQ(fmt,"")) { /* as string */
00907         Printf("\"");
00908         /* adjust len so trailing nulls don't get printed */
00909         sp = vals + len;
00910         while (len != 0 && *--sp == '\0')
00911             len--;
00912         for (iel = 0; iel < len; iel++)
00913             switch (uc = *vals++ & 0377) {
00914             case '\b':
00915                 Printf("\\b");
00916                 break;
00917             case '\f':
00918                 Printf("\\f");
00919                 break;
00920             case '\n':  /* generate linebreaks after new-lines */
00921                 Printf("\\n\",\n    \"");
00922                 break;
00923             case '\r':
00924                 Printf("\\r");
00925                 break;
00926             case '\t':
00927                 Printf("\\t");
00928                 break;
00929             case '\v':
00930                 Printf("\\v");
00931                 break;
00932             case '\\':
00933                 Printf("\\\\");
00934                 break;
00935             case '\'':
00936                 Printf("\\\'");
00937                 break;
00938             case '\"':
00939                 Printf("\\\"");
00940                 break;
00941             default:
00942                 if (isprint(uc))
00943                     Printf("%c",uc);
00944                 else
00945                     Printf("\\%.3o",uc);
00946                 break;
00947             }
00948         Printf("\"");
00949     } else {            /* use format from C_format attribute */
00950         for (iel = 0; iel < len-1; iel++) {
00951             (void) sprintf(sout, fmt, *vals++);
00952             (void) strcat(sout, " ");
00953             lput(sout);
00954         }
00955         (void) sprintf(sout, fmt, *vals++);
00956         lput(sout);
00957     }
00958     if (!fsp->full_data_cmnts) {
00959         lastdelim2x (more, lastrow);
00960     }
00961 }

static void printbval char *  sout,
const char *  fmt,
const ncvar_t varp,
signed char  val
[static]
 

Definition at line 138 of file vardata.c.

References FILL_STRING, ncvar_t::fillval, and ncvar_t::has_fillval.

Referenced by pr_bvals(), and pr_bvalsx().

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 }

static void printdval char *  sout,
const char *  fmt,
const ncvar_t varp,
double  val
[static]
 

Definition at line 238 of file vardata.c.

References absval, double_eps, FILL_STRING, ncvar_t::fillval, and ncvar_t::has_fillval.

Referenced by pr_dvals(), and pr_dvalsx().

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 }

static void printfval char *  sout,
const char *  fmt,
const ncvar_t varp,
float  val
[static]
 

Definition at line 212 of file vardata.c.

References absval, FILL_STRING, ncvar_t::fillval, float_eps, and ncvar_t::has_fillval.

Referenced by pr_fvals(), and pr_fvalsx().

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 }

static void printival char *  sout,
const char *  fmt,
const ncvar_t varp,
int  val
[static]
 

Definition at line 185 of file vardata.c.

References FILL_STRING, ncvar_t::fillval, and ncvar_t::has_fillval.

Referenced by pr_ivals(), and pr_ivalsx().

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 }

static void printsval char *  sout,
const char *  fmt,
const ncvar_t varp,
short  val
[static]
 

Definition at line 161 of file vardata.c.

References FILL_STRING, ncvar_t::fillval, and ncvar_t::has_fillval.

Referenced by pr_svals(), and pr_svalsx().

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 }

static int upcorner const size_t *  dims,
int  ndims,
size_t *  odom,
const size_t *  add
[static]
 

Definition at line 677 of file vardata.c.

Referenced by vardata(), and vardatax().

00683 {
00684     int id;
00685     int ret = 1;
00686 
00687     for (id = ndims-1; id > 0; id--) {
00688         odom[id] += add[id];
00689         if(odom[id] >= dims[id]) {
00690             odom[id-1]++;
00691             odom[id] -= dims[id];
00692         }
00693     }
00694     odom[0] += add[0];
00695     if (odom[0] >= dims[0])
00696       ret = 0;
00697     return ret;
00698 }

int vardata const ncvar_t vp,
size_t  vdims[],
int  ncid,
int  varid,
const fspec_t fsp
 

Definition at line 703 of file vardata.c.

References fspec_t::brief_data_cmnts, fspec_t::data_lang, error(), get_fmt(), init_epsilons(), LANG_C, LANG_F, ncvar_t::name, NC_BYTE, NC_CHAR, NC_CHECK, NC_DOUBLE, NC_FLOAT, nc_get_vara_double, nc_get_vara_float, nc_get_vara_int, nc_get_vara_schar, nc_get_vara_short, nc_get_vara_text, NC_INT, NC_MAX_DIMS, NC_SHORT, ncvar_t::ndims, pr_bvals(), pr_dvals(), pr_fvals(), pr_ivals(), pr_svals(), pr_tvals(), Printf, set_indent(), ncvar_t::type, upcorner(), and VALBUFSIZ.

Referenced by do_ncdump_rec().

00710 {
00711     size_t cor[NC_MAX_DIMS];    /* corner coordinates */
00712     size_t edg[NC_MAX_DIMS];    /* edges of hypercube */
00713     size_t add[NC_MAX_DIMS];    /* "odometer" increment to next "row"  */
00714 #define VALBUFSIZ 1000
00715     double vals[VALBUFSIZ] ; /* aligned buffer */
00716 
00717     int gulp = VALBUFSIZ;
00718 
00719     int id;
00720     int ir;
00721     size_t nels;
00722     size_t ncols;
00723     size_t nrows;
00724     int vrank = vp->ndims;
00725     static int initeps = 0;
00726 
00727     /* printf format used to print each value */
00728     const char *fmt = get_fmt(ncid, varid, vp->type);
00729 
00730     if (!initeps) {             /* make sure epsilons get initialized */
00731         init_epsilons();
00732         initeps = 1;
00733     }
00734 
00735     nels = 1;
00736     for (id = 0; id < vrank; id++) {
00737         cor[id] = 0;
00738         edg[id] = 1;
00739         nels *= vdims[id];      /* total number of values for variable */
00740     }
00741 
00742     if (vrank <= 1) {
00743         Printf("\n %s = ", vp->name);
00744         set_indent ((int)strlen(vp->name) + 4);
00745     } else {
00746         Printf("\n %s =\n  ", vp->name);
00747         set_indent (2);
00748     }
00749 
00750     if (vrank < 1) {
00751         ncols = 1;
00752     } else {
00753         ncols = vdims[vrank-1]; /* size of "row" along last dimension */
00754         edg[vrank-1] = vdims[vrank-1];
00755         for (id = 0; id < vrank; id++)
00756           add[id] = 0;
00757         if (vrank > 1)
00758           add[vrank-2] = 1;
00759     }
00760     nrows = nels/ncols;         /* number of "rows" */
00761     
00762     for (ir = 0; ir < nrows; ir++) {
00763         /*
00764          * rather than just printing a whole row at once (which might exceed
00765          * the capacity of MSDOS platforms, for example), we break each row
00766          * into smaller chunks, if necessary.
00767          */
00768         size_t corsav;
00769         int left = (int)ncols;
00770         boolean lastrow;
00771 
00772         if (vrank > 0) {
00773             corsav = cor[vrank-1];
00774             if (fsp->brief_data_cmnts != false
00775                 && vrank > 1
00776                 && left > 0) {  /* print brief comment with indices range */
00777                 Printf("// %s(",vp->name);
00778                 switch (fsp->data_lang) {
00779                   case LANG_C:
00780                     /* print brief comment with C variable indices */
00781                     for (id = 0; id < vrank-1; id++)
00782                       Printf("%lu,", (unsigned long)cor[id]);
00783                     if (vdims[vrank-1] == 1)
00784                       Printf("0");
00785                     else
00786                       Printf(" 0-%lu", (unsigned long)vdims[vrank-1]-1);
00787                     break;
00788                   case LANG_F:
00789                     /* print brief comment with Fortran variable indices */
00790                     if (vdims[vrank-1] == 1)
00791                       Printf("1");
00792                     else
00793                       Printf("1-%lu ", (unsigned long)vdims[vrank-1]);
00794                     for (id = vrank-2; id >=0 ; id--) {
00795                         Printf(",%lu", (unsigned long)(1 + cor[id]));
00796                     }
00797                     break;
00798                 }
00799                 Printf(")\n    ");
00800                 set_indent(4);
00801             }
00802         }
00803         lastrow = (boolean)(ir == nrows-1);
00804         while (left > 0) {
00805             size_t toget = left < gulp ? left : gulp;
00806             if (vrank > 0)
00807               edg[vrank-1] = toget;
00808             switch(vp->type) {
00809             case NC_CHAR:
00810                 NC_CHECK(
00811                     nc_get_vara_text(ncid, varid, cor, edg, (char *)vals) );
00812                 pr_tvals(vp, toget, fmt, left > toget, lastrow,
00813                          (char *) vals, fsp, cor);
00814                 break;
00815             case NC_BYTE:
00816                 NC_CHECK(
00817                     nc_get_vara_schar(ncid, varid, cor, edg, (signed char *)vals) );
00818                 pr_bvals(vp, toget, fmt, left > toget, lastrow,
00819                          (signed char *) vals, fsp, cor);
00820                 break;
00821             case NC_SHORT:
00822                 NC_CHECK(
00823                     nc_get_vara_short(ncid, varid, cor, edg, (short *)vals) );
00824                 pr_svals(vp, toget, fmt, left > toget, lastrow,
00825                          (short *) vals, fsp, cor);
00826                 break;
00827             case NC_INT:
00828                 NC_CHECK(
00829                     nc_get_vara_int(ncid, varid, cor, edg, (int *)vals) );
00830                 pr_ivals(vp, toget, fmt, left > toget, lastrow,
00831                          (int *) vals, fsp, cor);
00832                 break;
00833             case NC_FLOAT:
00834                 NC_CHECK(
00835                     nc_get_vara_float(ncid, varid, cor, edg, (float *)vals) );
00836                 pr_fvals(vp, toget, fmt, left > toget, lastrow,
00837                          (float *) vals, fsp, cor);
00838                 break;
00839             case NC_DOUBLE:
00840                 NC_CHECK(
00841                     nc_get_vara_double(ncid, varid, cor, edg, (double *)vals) );
00842                 pr_dvals(vp, toget, fmt, left > toget, lastrow,
00843                          (double *) vals, fsp, cor);
00844                 break;
00845             default:
00846                 error("vardata: bad type");
00847             }
00848             left -= toget;
00849             if (vrank > 0)
00850               cor[vrank-1] += toget;
00851         }
00852         if (vrank > 0)
00853           cor[vrank-1] = corsav;
00854         if (ir < nrows-1)
00855           if (!upcorner(vdims,vp->ndims,cor,add))
00856             error("vardata: odometer overflowed!");
00857         set_indent(2);
00858     }
00859 
00860     return 0;
00861 }

int vardatax const ncvar_t vp,
size_t  vdims[],
int  ncid,
int  varid,
const fspec_t fsp
 

Definition at line 1133 of file vardata.c.

References error(), get_fmt(), init_epsilons(), NC_BYTE, NC_CHAR, NC_CHECK, NC_COMPOUND, NC_DOUBLE, NC_FLOAT, nc_get_vara_double, nc_get_vara_float, nc_get_vara_int, nc_get_vara_schar, nc_get_vara_short, nc_get_vara_text, NC_INT, NC_INT64, NC_MAX_DIMS, NC_OPAQUE, NC_SHORT, NC_STRING, NC_UBYTE, NC_UINT, NC_UINT64, NC_USHORT, NC_VLEN, ncvar_t::ndims, pr_bvalsx(), pr_dvalsx(), pr_fvalsx(), pr_ivalsx(), pr_svalsx(), pr_tvalsx(), Printf, set_indent(), ncvar_t::type, upcorner(), and VALBUFSIZ.

Referenced by do_ncdumpx().

01140 {
01141     size_t cor[NC_MAX_DIMS];    /* corner coordinates */
01142     size_t edg[NC_MAX_DIMS];    /* edges of hypercube */
01143     size_t add[NC_MAX_DIMS];    /* "odometer" increment to next "row"  */
01144 #define VALBUFSIZ 1000
01145     double vals[VALBUFSIZ] ; /* aligned buffer */
01146 
01147     int gulp = VALBUFSIZ;
01148 
01149     int id;
01150     int ir;
01151     size_t nels;
01152     size_t ncols;
01153     size_t nrows;
01154     int vrank = vp->ndims;
01155     static int initeps = 0;
01156 
01157     /* printf format used to print each value */
01158     const char *fmt = get_fmt(ncid, varid, vp->type);
01159 
01160     if (!initeps) {             /* make sure epsilons get initialized */
01161         init_epsilons();
01162         initeps = 1;
01163     }
01164 
01165     nels = 1;
01166     for (id = 0; id < vrank; id++) {
01167         cor[id] = 0;
01168         edg[id] = 1;
01169         nels *= vdims[id];      /* total number of values for variable */
01170     }
01171 
01172     Printf("    <values>\n     ");
01173     set_indent (7);
01174 
01175     if (vrank < 1) {
01176         ncols = 1;
01177     } else {
01178         ncols = vdims[vrank-1]; /* size of "row" along last dimension */
01179         edg[vrank-1] = vdims[vrank-1];
01180         for (id = 0; id < vrank; id++)
01181           add[id] = 0;
01182         if (vrank > 1)
01183           add[vrank-2] = 1;
01184     }
01185     nrows = nels/ncols;         /* number of "rows" */
01186     
01187     for (ir = 0; ir < nrows; ir++) {
01188         /*
01189          * rather than just printing a whole row at once (which might exceed
01190          * the capacity of MSDOS platforms, for example), we break each row
01191          * into smaller chunks, if necessary.
01192          */
01193         size_t corsav;
01194         int left = (int)ncols;
01195         boolean lastrow;
01196 
01197         if (vrank > 0) {
01198             corsav = cor[vrank-1];
01199         }
01200         lastrow = (boolean)(ir == nrows-1);
01201         while (left > 0) {
01202             size_t toget = left < gulp ? left : gulp;
01203             if (vrank > 0)
01204               edg[vrank-1] = toget;
01205             switch(vp->type) {
01206             case NC_CHAR:
01207                 NC_CHECK(
01208                     nc_get_vara_text(ncid, varid, cor, edg, (char *)vals) );
01209                 pr_tvalsx(toget, fmt, left > toget, lastrow,
01210                          (char *) vals, fsp);
01211                 break;
01212             case NC_BYTE:
01213                 NC_CHECK(
01214                     nc_get_vara_schar(ncid, varid, cor, edg, (signed char *)vals) );
01215                 pr_bvalsx(vp, toget, fmt, left > toget, lastrow,
01216                          (signed char *) vals);
01217                 break;
01218             case NC_SHORT:
01219                 NC_CHECK(
01220                     nc_get_vara_short(ncid, varid, cor, edg, (short *)vals) );
01221                 pr_svalsx(vp, toget, fmt, left > toget, lastrow,
01222                          (short *) vals);
01223                 break;
01224             case NC_INT:
01225                 NC_CHECK(
01226                     nc_get_vara_int(ncid, varid, cor, edg, (int *)vals) );
01227                 pr_ivalsx(vp, toget, fmt, left > toget, lastrow,
01228                          (int *) vals);
01229                 break;
01230             case NC_FLOAT:
01231                 NC_CHECK(
01232                     nc_get_vara_float(ncid, varid, cor, edg, (float *)vals) );
01233                 pr_fvalsx(vp, toget, fmt, left > toget, lastrow,
01234                          (float *) vals);
01235                 break;
01236             case NC_DOUBLE:
01237                 NC_CHECK(
01238                     nc_get_vara_double(ncid, varid, cor, edg, (double *)vals) );
01239                 pr_dvalsx(vp, toget, fmt, left > toget, lastrow,
01240                          (double *) vals);
01241                 break;
01242 #ifdef USE_NETCDF4
01243             case NC_UBYTE:
01244                 /* TODO */
01245                 break;
01246             case NC_USHORT:
01247                 /* TODO */
01248                 break;
01249             case NC_UINT:
01250                 /* TODO */
01251                 break;
01252             case NC_INT64:
01253                 /* TODO */
01254                 break;
01255             case NC_UINT64:
01256                 /* TODO */
01257                 break;
01258             case NC_STRING:
01259                 /* TODO */
01260                 break;
01261             case NC_VLEN:
01262                 /* TODO */
01263                 break;
01264             case NC_OPAQUE:
01265                 /* TODO */
01266                 break;
01267             case NC_COMPOUND:
01268                 /* TODO */
01269                 break;
01270 #endif /* USE_NETCDF4 */
01271             default:
01272                 error("vardata: bad type");
01273             }
01274             left -= toget;
01275             if (vrank > 0)
01276               cor[vrank-1] += toget;
01277         }
01278         if (vrank > 0)
01279           cor[vrank-1] = corsav;
01280         if (ir < nrows-1)
01281           if (!upcorner(vdims,vp->ndims,cor,add))
01282             error("vardata: odometer overflowed!");
01283         set_indent(2);
01284     }
01285     Printf(" </values>\n");
01286     return 0;
01287 }


Variable Documentation

double double_eps [static]
 

Definition at line 61 of file vardata.c.

Referenced by double_epsilon(), init_epsilons(), printdval(), and test_ncvardef().

float float_eps [static]
 

Definition at line 60 of file vardata.c.

Referenced by float_epsilon(), init_epsilons(), printfval(), and test_ncvardef().


Generated on Thu Mar 16 18:12:16 2006 for nco by  doxygen 1.4.4