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
00003
00004
00005
00006
00007
00008
00009
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;
00023 int double_precision_specified = 0;
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
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);
00044 exit(EXIT_FAILURE);
00045 }
00046
00047 #define LINEPIND " "
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
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,
00094 int varid
00095 )
00096 {
00097 nc_type cfmt_type;
00098 size_t cfmt_len;
00099 #define C_FMT_NAME "C_format"
00100 #define MAX_CFMT_LEN 100
00101 static char cfmt[MAX_CFMT_LEN];
00102
00103
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
00127
00128
00129 char *
00130 get_fmt(
00131 int ncid,
00132 int varid,
00133 nc_type type
00134 )
00135 {
00136 char *c_format_att;
00137
00138
00139
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
00148 c_format_att = has_c_format_att(ncid, varid);
00149 if (c_format_att)
00150 return c_format_att;
00151
00152
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
00188
00189 vnode*
00190 newvlist(void)
00191 {
00192 vnode *vp = newvnode();
00193
00194 vp -> next = 0;
00195 vp -> id = -1;
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