Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

dumplib.c

Go to the documentation of this file.
00001 /*********************************************************************
00002  *   Copyright 1993, University Corporation for Atmospheric Research
00003  *   See netcdf/README file for copying and redistribution conditions.
00004  *   $Header: /misc/elrond0/share/cvs/AFNI/src/netcdf-3.5.0/src/ncdump/dumplib.c,v 1.5 2004/04/02 15:12:42 rwcox Exp $
00005  *********************************************************************/
00006 
00007 /*
00008  * We potentially include <stdarg.h> before <stdio.h> in order to obtain a
00009  * definition for va_list from the GNU C compiler.
00010  */
00011 #include <stdarg.h>
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 
00016 #include "netcdf.h"
00017 #include "dumplib.h"
00018 
00019 static char* has_c_format_att(int ncid, int varid);
00020 static vnode* newvnode(void);
00021 
00022 int float_precision_specified = 0; /* -p option specified float precision */
00023 int double_precision_specified = 0; /* -p option specified double precision */
00024 char float_var_fmt[] = "%.NNg";
00025 char double_var_fmt[] = "%.NNg";
00026 char float_att_fmt[] = "%#.NNgf";
00027 char double_att_fmt[] = "%#.NNg";
00028 
00029 /*
00030  * Print error message to stderr and exit
00031  */
00032 void
00033 error(const char *fmt, ...)
00034 {
00035     va_list args ;
00036 
00037     (void) fprintf(stderr,"%s: ", progname);
00038     va_start(args, fmt) ;
00039     (void) vfprintf(stderr,fmt,args) ;
00040     va_end(args) ;
00041 
00042     (void) fprintf(stderr, "\n") ;
00043     (void) fflush(stderr);      /* to ensure log files are current */
00044     exit(EXIT_FAILURE);
00045 }
00046 
00047 #define LINEPIND        "    "  /* indent of continued lines */
00048 
00049 static int linep;
00050 static int max_line_len;
00051 
00052 void
00053 set_indent(int in)
00054 {
00055     linep = in;
00056 }
00057 
00058 
00059 void
00060 set_max_len(int len)
00061 {
00062     max_line_len = len-2;
00063 }
00064 
00065 
00066 void
00067 lput(const char *cp)
00068 {
00069     size_t nn = strlen(cp);
00070 
00071     if (nn+linep > max_line_len && nn > 2) {
00072         (void) fputs("\n", stdout);
00073         (void) fputs(LINEPIND, stdout);
00074         linep = (int)strlen(LINEPIND);
00075     }
00076     (void) fputs(cp,stdout);
00077     linep += nn;
00078 }
00079 
00080 /* In case different formats specified with -d option, set them here. */
00081 void
00082 set_formats(int float_digits, int double_digits)
00083 {
00084     (void) sprintf(float_var_fmt, "%%.%dg", float_digits);
00085     (void) sprintf(double_var_fmt, "%%.%dg", double_digits);
00086     (void) sprintf(float_att_fmt, "%%#.%dgf", float_digits);
00087     (void) sprintf(double_att_fmt, "%%#.%dg", double_digits);
00088 }
00089 
00090 
00091 static char *
00092 has_c_format_att(
00093     int ncid,                   /* netcdf id */
00094     int varid                   /* variable id */
00095     )
00096 {
00097     nc_type cfmt_type;
00098     size_t cfmt_len;
00099 #define C_FMT_NAME      "C_format" /* name of C format attribute */
00100 #define MAX_CFMT_LEN    100     /* max length of C format attribute */
00101     static char cfmt[MAX_CFMT_LEN];
00102     
00103     /* we expect nc_inq_att to fail if there is no "C_format" attribute */
00104     int nc_stat = nc_inq_att(ncid, varid, "C_format", &cfmt_type, &cfmt_len);
00105 
00106     switch(nc_stat) {
00107     case NC_NOERR:
00108         if (cfmt_type == NC_CHAR && cfmt_len != 0 && cfmt_len < MAX_CFMT_LEN) {
00109             int nc_stat = nc_get_att_text(ncid, varid, "C_format", cfmt);
00110             if(nc_stat != NC_NOERR)
00111                 nc_advise("Getting 'C_format' attribute", nc_stat, "");
00112             return &cfmt[0];
00113         }
00114         break;
00115     case NC_ENOTATT:
00116         break;
00117     default:
00118         nc_advise("Inquiring about 'C_format' attribute", nc_stat, "");
00119         break;
00120     }
00121     return 0;
00122 }
00123 
00124 
00125 /*
00126  * Determine print format to use for each value for this variable.  Use value
00127  * of attribute C_format if it exists, otherwise a sensible default.
00128  */
00129 char *
00130 get_fmt(
00131      int ncid,                  /* netcdf id */
00132      int varid,                 /* variable id */
00133      nc_type type               /* netCDF data type */
00134      )
00135 {
00136     char *c_format_att;
00137 
00138     /* float or double precision specified with -p option overrides any
00139        C_format attribute value, so check for that first. */
00140 
00141     if (float_precision_specified && type == NC_FLOAT)
00142         return float_var_fmt;
00143 
00144     if (double_precision_specified && type == NC_DOUBLE)
00145         return double_var_fmt;
00146 
00147     /* If C_format attribute exists, return it */
00148     c_format_att = has_c_format_att(ncid, varid);
00149     if (c_format_att)
00150       return c_format_att;    
00151 
00152     /* Otherwise return sensible default. */
00153     switch (type) {
00154       case NC_BYTE:
00155         return "%d";
00156       case NC_CHAR:
00157         return "%s";
00158       case NC_SHORT:
00159         return "%d";
00160       case NC_INT:
00161         return "%d";
00162       case NC_FLOAT:
00163         return float_var_fmt;
00164       case NC_DOUBLE:
00165         return double_var_fmt;
00166       default:
00167         error("pr_vals: bad type");
00168     }
00169 
00170     return 0;
00171 }
00172 
00173 
00174 static vnode*
00175 newvnode(void)
00176 {
00177     vnode *newvp = (vnode*) malloc(sizeof(vnode));
00178     
00179     if (!newvp) {
00180         error("out of memory!");
00181     }
00182     return newvp;
00183 }
00184 
00185 
00186 /*
00187  * Get a new, empty variable list.
00188  */
00189 vnode*
00190 newvlist(void)
00191 {
00192     vnode *vp = newvnode();
00193 
00194     vp -> next = 0;
00195     vp -> id = -1;              /* bad id */
00196 
00197     return vp;
00198 }
00199 
00200 
00201 void
00202 varadd(vnode* vlist, int varid)
00203 {
00204     vnode *newvp = newvnode();
00205     
00206     newvp -> next = vlist -> next;
00207     newvp -> id = varid;
00208     vlist -> next = newvp;
00209 }
00210 
00211 
00212 int
00213 varmember(const vnode* vlist, int varid)
00214 {
00215     vnode *vp = vlist -> next;
00216 
00217     for (; vp ; vp = vp->next)
00218       if (vp->id == varid)
00219         return 1;
00220     return 0;    
00221 }
00222 
00223 
 

Powered by Plone

This site conforms to the following standards: