ncdf4a13/ncdump/dumplib.c File Reference

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "netcdf.h"
#include "dumplib.h"
#include "ncdump.h"

Include dependency graph for dumplib.c:

Go to the source code of this file.

Defines

#define LINEPIND   " "
#define C_FMT_NAME   "C_format"
#define MAX_CFMT_LEN   100

Functions

static char * has_c_format_att (int ncid, int varid)
static vnodenewvnode (void)
void error (const char *fmt,...)
void * emalloc (size_t size)
void set_indent (int in)
void set_max_len (int len)
void lput (const char *cp)
void set_formats (int float_digits, int double_digits)
const char * get_fmt (int ncid, int varid, nc_type type)
vnodenewvlist (void)
void varadd (vnode *vlist, int varid)
int varmember (const vnode *vlist, int varid)
int iscoordvar (int ncid, int varid)
int isrecvar (int ncid, int varid)

Variables

int float_precision_specified = 0
int double_precision_specified = 0
char float_var_fmt [] = "%.NNg"
char double_var_fmt [] = "%.NNg"
char float_att_fmt [] = "%#.NNgf"
char double_att_fmt [] = "%#.NNg"
static int linep
static int max_line_len


Define Documentation

#define C_FMT_NAME   "C_format"
 

#define LINEPIND   " "
 

Definition at line 61 of file dumplib.c.

Referenced by lput().

#define MAX_CFMT_LEN   100
 

Referenced by has_c_format_att().


Function Documentation

void* emalloc size_t  size  ) 
 

Definition at line 49 of file dumplib.c.

Referenced by add_att(), add_dim(), add_var(), cstring(), cstrstr(), define_netcdf(), do_ncdump_rec(), do_ncdumpx(), f_var_init(), fstring(), fstrstr(), grow_aarray(), grow_darray(), grow_iarray(), grow_varray(), install(), iscoordvar(), isrecvar(), main(), make_lvars(), name_path(), newvnode(), pr_att(), pr_attx(), pr_shape(), test_ncattcopy(), test_ncattdel(), test_ncattget(), test_ncattinq(), test_ncattname(), test_ncattput(), test_ncattrename(), test_ncdiminq(), test_ncdimrename(), test_ncendef(), test_ncinquire(), test_ncrecget(), test_ncrecput(), test_ncredef(), test_ncsync(), test_ncvardef(), test_ncvarget(), test_ncvargetg(), test_ncvarid(), test_ncvarinq(), test_ncvarput(), test_ncvarputg(), test_ncvarrename(), test_slabs(), test_varputget(), test_varputgetg(), and yyparse().

00051 {
00052     void   *p;
00053 
00054     p = (void *) malloc (size);
00055     if (p == 0) {
00056         error ("out of memory\n");
00057     }
00058     return p;
00059 }

void error const char *  fmt,
  ...
 

Definition at line 34 of file dumplib.c.

References EXIT_FAILURE, and progname.

00035 {
00036     va_list args ;
00037 
00038     (void) fprintf(stderr,"%s: ", progname);
00039     va_start(args, fmt) ;
00040     (void) vfprintf(stderr,fmt,args) ;
00041     va_end(args) ;
00042 
00043     (void) fprintf(stderr, "\n") ;
00044     (void) fflush(stderr);      /* to ensure log files are current */
00045     exit(EXIT_FAILURE);
00046 }

const char* get_fmt int  ncid,
int  varid,
nc_type  type
 

Definition at line 149 of file dumplib.c.

References double_precision_specified, double_var_fmt, error(), float_precision_specified, float_var_fmt, has_c_format_att(), NC_BYTE, NC_CHAR, NC_DOUBLE, NC_FLOAT, NC_INT, and NC_SHORT.

Referenced by vardata(), and vardatax().

00154 {
00155     char *c_format_att;
00156 
00157     /* float or double precision specified with -p option overrides any
00158        C_format attribute value, so check for that first. */
00159 
00160     if (float_precision_specified && type == NC_FLOAT)
00161         return float_var_fmt;
00162 
00163     if (double_precision_specified && type == NC_DOUBLE)
00164         return double_var_fmt;
00165 
00166     /* If C_format attribute exists, return it */
00167     c_format_att = has_c_format_att(ncid, varid);
00168     if (c_format_att)
00169       return c_format_att;    
00170 
00171     /* Otherwise return sensible default. */
00172     switch (type) {
00173       case NC_BYTE:
00174         return "%d";
00175       case NC_CHAR:
00176         return "%s";
00177       case NC_SHORT:
00178         return "%d";
00179       case NC_INT:
00180         return "%d";
00181       case NC_FLOAT:
00182         return float_var_fmt;
00183       case NC_DOUBLE:
00184         return double_var_fmt;
00185       default:
00186         error("pr_vals: bad type");
00187     }
00188 
00189     return 0;
00190 }

static char * has_c_format_att int  ncid,
int  varid
[static]
 

Definition at line 106 of file dumplib.c.

References MAX_CFMT_LEN, NC_CHAR, NC_ENOTATT, nc_get_att_text, nc_inq_att, NC_NOERR, and nc_strerror.

Referenced by get_fmt().

00110 {
00111     nc_type cfmt_type;
00112     size_t cfmt_len;
00113 #define C_FMT_NAME      "C_format" /* name of C format attribute */
00114 #define MAX_CFMT_LEN    100     /* max length of C format attribute */
00115     static char cfmt[MAX_CFMT_LEN];
00116     
00117     /* we expect nc_inq_att to fail if there is no "C_format" attribute */
00118     int nc_stat = nc_inq_att(ncid, varid, "C_format", &cfmt_type, &cfmt_len);
00119 
00120     switch(nc_stat) {
00121     case NC_NOERR:
00122         if (cfmt_type == NC_CHAR && cfmt_len != 0 && cfmt_len < MAX_CFMT_LEN) {
00123             int nc_stat = nc_get_att_text(ncid, varid, "C_format", cfmt);
00124             if(nc_stat != NC_NOERR) {
00125                 fprintf(stderr, "Getting 'C_format' attribute %s\n", 
00126                         nc_strerror(nc_stat));
00127                 (void) fflush(stderr);
00128             }
00129             return &cfmt[0];
00130         }
00131         break;
00132     case NC_ENOTATT:
00133         break;
00134     default:
00135         fprintf(stderr, "Inquiring about 'C_format' attribute %s\n", 
00136                 nc_strerror(nc_stat));
00137         (void) fflush(stderr);
00138         break;
00139     }
00140     return 0;
00141 }

int iscoordvar int  ncid,
int  varid
 

Definition at line 248 of file dumplib.c.

References emalloc(), NC_CHECK, nc_inq_dimname, nc_inq_ndims, nc_inq_varname, nc_inq_varndims, NC_MAX_NAME, and ndims.

Referenced by do_ncdump_rec(), and do_ncdumpx().

00249 {
00250     int ndims;
00251     int dimid;
00252     ncdim_t *dims;
00253     int is_coord = 0;           /* true if variable is a coordinate variable */
00254     char varname[NC_MAX_NAME];
00255     int varndims;
00256 
00257     NC_CHECK( nc_inq_ndims(ncid, &ndims) );
00258     dims = (ncdim_t *) emalloc((ndims + 1) * sizeof(ncdim_t));
00259     for (dimid = 0; dimid < ndims; dimid++) {
00260         NC_CHECK( nc_inq_dimname(ncid, dimid, dims[dimid].name) );
00261     }
00262     NC_CHECK( nc_inq_varname(ncid, varid, varname) );
00263     NC_CHECK( nc_inq_varndims(ncid, varid, &varndims) );
00264    
00265     for (dimid = 0; dimid < ndims; dimid++) {
00266         if (strcmp(dims[dimid].name, varname) == 0 && varndims == 1) {
00267             is_coord = 1;
00268             break;
00269         }
00270     }
00271     free(dims);
00272     return is_coord;
00273 }

int isrecvar int  ncid,
int  varid
 

Definition at line 281 of file dumplib.c.

References emalloc(), NC_CHECK, nc_inq_unlimdim, nc_inq_vardimid, nc_inq_varndims, and ndims.

Referenced by do_ncdump_rec(), and do_ncdumpx().

00282 {
00283     int recdimid;
00284     int ndims;
00285     int is_recvar = 0;
00286     int *dimids;
00287 
00288     NC_CHECK( nc_inq_unlimdim(ncid, &recdimid) );
00289     NC_CHECK( nc_inq_varndims(ncid, varid, &ndims) );
00290     if (ndims > 0) {
00291         dimids = (int *) emalloc((ndims + 1) * sizeof(int));
00292         NC_CHECK( nc_inq_vardimid(ncid, varid, dimids) );
00293         /* TODO assumes only one unlimited dimension, but netcdf4 alllows multiple */
00294         if(dimids[0] == recdimid)
00295             is_recvar = 1;
00296         free(dimids);
00297     }
00298     return is_recvar;
00299 }

void lput const char *  cp  ) 
 

Definition at line 81 of file dumplib.c.

References linep, LINEPIND, and max_line_len.

Referenced by lastdelim2(), lastdelim2x(), pr_bvals(), pr_bvalsx(), pr_dvals(), pr_dvalsx(), pr_fvals(), pr_fvalsx(), pr_ivals(), pr_ivalsx(), pr_svals(), pr_svalsx(), pr_tvals(), and pr_tvalsx().

00082 {
00083     size_t nn = strlen(cp);
00084 
00085     if (nn+linep > max_line_len && nn > 2) {
00086         (void) fputs("\n", stdout);
00087         (void) fputs(LINEPIND, stdout);
00088         linep = (int)strlen(LINEPIND);
00089     }
00090     (void) fputs(cp,stdout);
00091     linep += nn;
00092 }

vnode* newvlist void   ) 
 

Definition at line 205 of file dumplib.c.

References newvnode().

Referenced by do_ncdump_rec(), and do_ncdumpx().

00206 {
00207     vnode *vp = newvnode();
00208 
00209     vp -> next = 0;
00210     vp -> id = -1;              /* bad id */
00211 
00212     return vp;
00213 }

static vnode * newvnode void   )  [static]
 

Definition at line 194 of file dumplib.c.

References emalloc().

Referenced by newvlist(), and varadd().

00195 {
00196     vnode *newvp = (vnode*) emalloc(sizeof(vnode));
00197     return newvp;
00198 }

void set_formats int  float_digits,
int  double_digits
 

Definition at line 96 of file dumplib.c.

References double_att_fmt, double_var_fmt, float_att_fmt, and float_var_fmt.

Referenced by main(), set_precision(), and set_sigdigs().

00097 {
00098     (void) sprintf(float_var_fmt, "%%.%dg", float_digits);
00099     (void) sprintf(double_var_fmt, "%%.%dg", double_digits);
00100     (void) sprintf(float_att_fmt, "%%#.%dgf", float_digits);
00101     (void) sprintf(double_att_fmt, "%%#.%dg", double_digits);
00102 }

void set_indent int  in  ) 
 

Definition at line 67 of file dumplib.c.

References linep.

Referenced by vardata(), and vardatax().

00068 {
00069     linep = in;
00070 }

void set_max_len int  len  ) 
 

Definition at line 74 of file dumplib.c.

References max_line_len.

Referenced by main().

00075 {
00076     max_line_len = len-2;
00077 }

void varadd vnode vlist,
int  varid
 

Definition at line 217 of file dumplib.c.

References newvnode().

Referenced by do_ncdump_rec(), and do_ncdumpx().

00218 {
00219     vnode *newvp = newvnode();
00220     
00221     newvp -> next = vlist -> next;
00222     newvp -> id = varid;
00223     vlist -> next = newvp;
00224 }

int varmember const vnode vlist,
int  varid
 

Definition at line 232 of file dumplib.c.

References vnode::id, and vnode::next.

Referenced by do_ncdump_rec(), and do_ncdumpx().

00233 {
00234     vnode *vp = vlist -> next;
00235 
00236     for (; vp ; vp = vp->next)
00237       if (vp->id == varid)
00238         return 1;
00239     return 0;    
00240 }


Variable Documentation

char double_att_fmt[] = "%#.NNg"
 

Definition at line 28 of file dumplib.c.

Referenced by pr_att_vals(), pr_att_valsx(), and set_formats().

int double_precision_specified = 0
 

Definition at line 24 of file dumplib.c.

Referenced by get_fmt(), and set_precision().

char double_var_fmt[] = "%.NNg"
 

Definition at line 26 of file dumplib.c.

Referenced by get_fmt(), and set_formats().

char float_att_fmt[] = "%#.NNgf"
 

Definition at line 27 of file dumplib.c.

Referenced by pr_att_vals(), and set_formats().

int float_precision_specified = 0
 

Definition at line 23 of file dumplib.c.

Referenced by get_fmt(), and set_precision().

char float_var_fmt[] = "%.NNg"
 

Definition at line 25 of file dumplib.c.

Referenced by get_fmt(), and set_formats().

int linep [static]
 

Definition at line 63 of file dumplib.c.

Referenced by lput(), and set_indent().

int max_line_len [static]
 

Definition at line 64 of file dumplib.c.

Referenced by lput(), and set_max_len().


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