Ignore all generated/compiled files
[gwave-svn.git] / spicefile / sp2sp.c
blob901d73a3b0e50d4d77a0af1525943e327e501eff
1 /*
2 * sp2sp - test program for spicestream library and
3 * rudimentary spicefile format converter.
5 * Copyright (C) 1998,1999 Stephen G. Tell
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <float.h>
27 #include <errno.h>
28 #include <glib.h>
29 #include <limits.h>
30 #include "spicestream.h"
32 #define SWEEP_NONE 0
33 #define SWEEP_PREPEND 1
34 #define SWEEP_HEAD 2
36 static const char NPY_MAGIC[] = "\x93NUMPY";
37 static const int NPY_MAJOR = 1;
38 static const int NPY_MINOR = 0;
39 static const int NPY_MAX_HDR_LEN = 256 * 256;
40 static const int NPY_PREAMBLE_LEN = 6 + 1 + 1 + 2;
42 #if __BYTE_ORDER == __LITTLE_ENDIAN
43 static const char NPY_ENDIAN_CHAR = '<';
44 #else
45 static const char NPY_ENDIAN_CHAR = '>';
46 #endif
49 int g_verbose = 0;
50 int sweep_mode = SWEEP_PREPEND;
51 char *progname = "sp2sp";
53 static void ascii_header_output(SpiceStream *sf, int *enab, int nidx, FILE *of);
54 static void ascii_data_output(SpiceStream *sf, int *enab, int nidx,
55 double begin_val, double end_val, int ndigits, FILE *of);
56 static void numpy_output(SpiceStream *sf, int *enab, int nidx,
57 double begin_val, double end_val, int ndigits, FILE *of);
58 static int numpy_header_output(int ndims, int* shape, int len, FILE *of);
59 static int numpy_footer_output(SpiceStream *sf, int *indices, int nidx,
60 int *sweeprows, FILE *of);
61 static int parse_field_numbers(int **index, int *idxsize, int *nsel,
62 char *list, int nfields);
63 static int parse_field_names(int **index, int *idxsize, int *nsel,
64 char *list, SpiceStream *sf);
65 static VarType get_vartype_code(char *vartype);
67 static void usage()
69 int i;
70 char *s;
72 fprintf(stderr, "usage: %s [options] file\n", progname);
73 fprintf(stderr, " options:\n");
74 fprintf(stderr, " -b V begin output after independent-variable value V is reached\n");
75 fprintf(stderr, " instead of start of input\n");
76 fprintf(stderr, " -c T Convert output to type T\n");
77 fprintf(stderr, " -d N use N significant digits in output\n");
78 fprintf(stderr, " -e V stop after independent-variable value V is reached\n");
79 fprintf(stderr, " instead of end of input.\n");
81 fprintf(stderr, " -f f1,f2,... Output only fields named f1, f2, etc.\n");
82 fprintf(stderr, " -n n1,n2,... Output only fields n1, n2, etc;\n");
83 fprintf(stderr, " independent variable is field number 0\n");
84 fprintf(stderr, " -o file Output to named file instead of default '-' stdout.\n");
85 fprintf(stderr, " -u U Output only variables with units of type; U\n");
86 fprintf(stderr, " U = volts, amps, etc.\n");
87 fprintf(stderr, " -s S Handle sweep parameters as S:\n");
88 fprintf(stderr, " -s head add header-like comment line\n");
89 fprintf(stderr, " -s prepend prepend columns to all output lines\n");
90 fprintf(stderr, " -s none ignore sweep info\n");
91 fprintf(stderr, " -t T Assume that input is of type T\n");
92 fprintf(stderr, " -v Verbose - print detailed signal information\n");
93 fprintf(stderr, " output format types:\n");
94 fprintf(stderr, " none - no data output\n");
95 fprintf(stderr, " ascii - lines of space-seperated numbers, with header\n");
96 fprintf(stderr, " nohead - lines of space-seperated numbers, no headers\n");
97 fprintf(stderr, " cazm - CAzM format\n");
98 fprintf(stderr, " numpy - .npy data followed by python str(dict) and footer len uint16\n");
99 fprintf(stderr, " input format types:\n");
101 i = 0;
102 while(s = ss_filetype_name(i++)) {
103 fprintf(stderr, " %s\n", s);
108 main(int argc, char **argv)
110 SpiceStream *sf;
111 FILE *of;
113 int i;
114 int idx;
115 extern int optind;
116 extern char *optarg;
117 int x_flag = 0;
118 int errflg = 0;
119 char *infiletype = "hspice";
120 char *outfiletype = "ascii";
121 char *outfilename = "-";
122 char *fieldnamelist = NULL;
123 char *fieldnumlist = NULL;
124 int *out_indices = NULL;
125 int outi_size = 0;
126 int nsel;
127 VarType vartype = UNKNOWN;
128 int c;
129 int ndigits = 7;
130 double begin_val = -DBL_MAX;
131 double end_val = DBL_MAX;
133 while ((c = getopt (argc, argv, "b:c:d:e:f:n:s:t:u:o:vx")) != EOF) {
134 switch(c) {
135 case 'v':
136 spicestream_msg_level = DBG;
137 g_verbose = 1;
138 break;
139 case 'b':
140 begin_val = atof(optarg);
141 break;
142 case 'c':
143 outfiletype = optarg;
144 break;
145 case 'd':
146 ndigits = atoi(optarg);
147 if(ndigits < 5)
148 ndigits = 5;
149 break;
150 case 'e':
151 end_val = atof(optarg);
152 break;
153 case 'f':
154 fieldnamelist = optarg;
155 break;
156 case 'n':
157 fieldnumlist = optarg;
158 break;
159 case 's':
160 if(strcmp(optarg, "none") == 0)
161 sweep_mode = SWEEP_NONE;
162 else if(strcmp(optarg, "prepend") == 0)
163 sweep_mode = SWEEP_PREPEND;
164 else if(strcmp(optarg, "head") == 0)
165 sweep_mode = SWEEP_HEAD;
166 else {
167 fprintf(stderr, "unknown sweep-data style %s\n", optarg);
168 exit(1);
170 break;
171 case 't':
172 infiletype = optarg;
173 break;
174 case 'u':
175 vartype = get_vartype_code(optarg);
176 break;
177 case 'x':
178 spicestream_msg_level = DBG;
179 x_flag = 1;
180 break;
181 case 'o':
182 outfilename = optarg;
183 break;
184 default:
185 errflg = 1;
186 break;
190 if(errflg || optind >= argc) {
191 usage();
192 exit(1);
195 sf = ss_open(argv[optind], infiletype);
196 if(!sf) {
197 if(errno)
198 perror(argv[optind]);
199 fprintf(stderr, "unable to read file\n");
200 exit(1);
202 if(g_verbose) {
203 printf("filename: \"%s\"\n", sf->filename);
204 printf(" columns: %d\n", sf->ncols);
205 printf(" tables: %d\n", sf->ntables);
206 printf("independent variable:\n");
207 printf(" name: \"%s\"\n", sf->ivar->name);
208 printf(" type: %s\n", vartype_name_str(sf->ivar->type));
209 printf(" col: %d\n", sf->ivar->col);
210 printf(" ncols: %d\n", sf->ivar->ncols);
211 printf("sweep parameters: %d\n", sf->nsweepparam);
212 for(i = 0; i < sf->nsweepparam; i++) {
213 printf(" name: \"%s\"\n", sf->spar[i].name);
214 printf(" type: %s\n", vartype_name_str(sf->spar[i].type));
216 printf("dependent variables: %d\n", sf->ndv);
217 for(i = 0; i < sf->ndv; i++) {
218 SpiceVar *dvar;
219 dvar = ss_dvar(sf, i);
220 printf(" dv[%d] \"%s\" ", i, dvar->name);
221 printf(" (type=%s col=%d ncols=%d)\n",
222 vartype_name_str(dvar->type),
223 dvar->col,
224 dvar->ncols);
228 if(strcmp(outfilename, "-") == 0) {
229 if(strcmp(outfiletype, "numpy") == 0) {
230 fprintf(stderr, "cannot output to stdout for 'numpy' format, use -o\n");
231 exit(1);
233 of = stdout;
234 } else {
235 of = (FILE *)fopen64(outfilename, "w"); /* DJW: why is the cast needed? */
236 if(!of) {
237 if(errno)
238 perror(outfilename);
239 fprintf(stderr, "unable to open output file\n");
240 exit(1);
244 if(fieldnamelist == NULL && fieldnumlist == NULL) {
245 out_indices = g_new0(int, sf->ndv+1);
246 nsel = 0;
247 idx = 0;
248 for(i = 0; i < sf->ndv+1; i++) {
249 SpiceVar *dvar;
250 dvar = ss_dvar(sf, i-1);
252 if(i == 0 ||
253 (vartype == UNKNOWN
254 || dvar->type == vartype)) {
255 out_indices[idx++] = i;
256 nsel++;
260 if(fieldnumlist)
261 if(parse_field_numbers(&out_indices, &outi_size, &nsel,
262 fieldnumlist, sf->ndv+1) < 0)
263 exit(1);
264 if(fieldnamelist)
265 if(parse_field_names(&out_indices, &outi_size, &nsel,
266 fieldnamelist, sf) < 0)
267 exit(1);
268 if(nsel == 0) {
269 fprintf(stderr, "No fields selected for output\n");
270 exit(0);
273 if(strcmp(outfiletype, "cazm") == 0) {
274 fprintf(of, "* CAZM-format output converted with sp2sp\n");
275 fprintf(of, "\n");
276 fprintf(of, "TRANSIENT ANALYSIS\n");
277 ascii_header_output(sf, out_indices, nsel, of);
278 ascii_data_output(sf, out_indices, nsel, begin_val, end_val, ndigits, of);
279 } else if(strcmp(outfiletype, "ascii") == 0) {
280 ascii_header_output(sf, out_indices, nsel, of);
281 ascii_data_output(sf, out_indices, nsel, begin_val, end_val, ndigits, of);
282 } else if(strcmp(outfiletype, "nohead") == 0) {
283 ascii_data_output(sf, out_indices, nsel, begin_val, end_val, ndigits, of);
284 } else if(strcmp(outfiletype, "numpy") == 0) {
285 numpy_output(sf, out_indices, nsel, begin_val, end_val, ndigits, of);
286 } else if(strcmp(outfiletype, "none") == 0) {
287 /* do nothing */
288 } else {
289 fprintf(stderr, "%s: invalid output type name: %s\n",
290 progname, outfiletype);
293 ss_close(sf);
294 close(of);
296 exit(0);
300 * print all column headers.
301 * For multicolumn variables, ss_var_name will generate a column name
302 * consisting of the variable name plus a suffix.
304 static void
305 ascii_header_output(SpiceStream *sf, int *indices, int nidx, FILE *of)
307 int i, j;
308 char buf[1024];
310 if((sf->nsweepparam > 0) && (sweep_mode == SWEEP_PREPEND)) {
311 for(i = 0; i < sf->nsweepparam; i++) {
312 fprintf(of, "%s ", sf->spar[i].name);
315 for(i = 0; i < nidx; i++) {
316 if(i > 0)
317 fputc(' ', of);
318 if(indices[i] == 0) {
319 ss_var_name(sf->ivar, 0, buf, 1024);
320 fprintf(of, "%s", buf);
321 } else {
322 int varno = indices[i]-1;
323 SpiceVar *dvar;
324 dvar = ss_dvar(sf, varno);
325 for(j = 0; j < dvar->ncols; j++) {
326 if(j > 0)
327 fputc(' ', of);
328 ss_var_name(dvar, j, buf, 1024);
329 fprintf(of, "%s", buf);
333 fputc('\n', of);
337 * print data as space-seperated columns.
339 static void
340 ascii_data_output(SpiceStream *sf, int *indices, int nidx,
341 double begin_val, double end_val, int ndigits, FILE *of)
343 int i, j, tab;
344 int rc;
345 double ival;
346 double *dvals;
347 double *spar = NULL;
348 int done;
350 dvals = g_new(double, sf->ncols);
351 if(sf->nsweepparam > 0)
352 spar = g_new(double, sf->nsweepparam);
354 done = 0;
355 tab = 0;
356 while(!done) {
357 if(sf->nsweepparam > 0) {
358 if(ss_readsweep(sf, spar) <= 0)
359 break;
361 if(tab > 0 && sweep_mode == SWEEP_HEAD) {
362 fprintf(of, "# sweep %d;", tab);
363 for(i = 0; i < sf->nsweepparam; i++) {
364 fprintf(of, " %s=%g", sf->spar[i].name, spar[i]);
366 fputc('\n', of);
368 while((rc = ss_readrow(sf, &ival, dvals)) > 0) {
369 if(ival < begin_val)
370 continue;
371 if(ival > end_val) {
372 /* past end_val, but can only stop reading
373 early if if there is only one sweep-table
374 in the file. */
375 if(sf->ntables == 1)
376 break;
377 else
378 continue;
381 if((sf->nsweepparam > 0) && (sweep_mode == SWEEP_PREPEND)) {
382 for(i = 0; i < sf->nsweepparam; i++) {
383 fprintf(of, "%.*g ", ndigits, spar[i]);
386 for(i = 0; i < nidx; i++) {
387 if(i > 0)
388 fputc(' ', of);
389 if(indices[i] == 0)
390 fprintf(of, "%.*g", ndigits, ival);
391 else {
392 int varno = indices[i]-1;
393 SpiceVar *dvar = ss_dvar(sf, varno);
394 int dcolno = dvar->col - 1;
395 for(j = 0; j < dvar->ncols; j++) {
396 if(j > 0)
397 fputc(' ', of);
398 fprintf(of, "%.*g", ndigits,
399 dvals[dcolno+j]);
403 fputc('\n', of);
405 if(rc == -2) { /* end of sweep, more follow */
406 if(sf->nsweepparam == 0)
407 sweep_mode = SWEEP_HEAD;
408 tab++;
409 } else { /* EOF or error */
410 done = 1;
413 g_free(dvals);
414 if(spar)
415 g_free(spar);
418 static int
419 numpy_header_output(int ndims, int *shape, int len, FILE *of)
421 /* More documentation about the npy format at numpy/lib/format.py */
423 char header[NPY_MAX_HDR_LEN];
424 char descr[5];
425 int i;
426 int hlen;
427 int nspaces = 0;
429 strcpy(header, "{'descr':'");
431 descr[0] = NPY_ENDIAN_CHAR;
432 descr[1] = 'f';
433 sprintf(descr+2, "%d", (int) sizeof(float));
435 strcat(header, descr);
436 strcat(header, "', 'fortran_order':False, 'shape': (");
437 for(i=0; i < ndims; i++) {
438 hlen = strlen(header);
439 sprintf(header+hlen, "%d", shape[i]);
441 if(i != ndims-1) {
442 strcat(header, ", ");
445 strcat(header, ") }");
446 hlen = strlen(header);
448 if(len == -1) {
449 /* bogus header values or just write the length needed */
450 nspaces = 16 - ((NPY_PREAMBLE_LEN + hlen + 1) % 16);
452 } else if(len < hlen) {
453 fprintf(stderr, "numpy_header_output: requested header len is too small\n");
455 } else if(((len + NPY_PREAMBLE_LEN) % 16) != 0) {
456 fprintf(stderr, "requested header len does not align to mult of 16\n");
458 } else {
459 /* pad to the (longer) header length */
460 nspaces = len - hlen - 1;
463 if(hlen + nspaces + 1 > NPY_MAX_HDR_LEN) {
464 fprintf(stderr, "npy header too long (%d)\n", hlen+nspaces+1);
465 return 0;
468 for(i=0; i < nspaces; i++) {
469 strcat(header, " ");
471 strcat(header, "\n");
472 hlen = strlen(header);
474 /* preamble */
475 fprintf(of, NPY_MAGIC);
476 fwrite(&NPY_MAJOR, sizeof(char), 1, of);
477 fwrite(&NPY_MINOR, sizeof(char), 1, of);
478 fputc((0xff & hlen), of);
479 fputc((0xff & (hlen >> 8)), of);
480 fwrite(header, sizeof(char), hlen, of);
482 return hlen;
486 static int
487 numpy_footer_output(SpiceStream *sf, int *indices, int nidx, int *sweeprows, FILE *of)
489 int i, j;
490 int foot_start = 0;
491 int foot_len = 0;
492 char buf[1024];
493 double *spar = NULL;
495 foot_start = ftell(of);
498 * make footer dict
500 fprintf(of, "{ ");
502 /* sweep variables, these values prepend the data columns for each row */
503 fprintf(of, "'sweepvars':(");
504 /*for(i=0; i < sf->ntables; i++) {*/
505 if(sf->ntables > 1) {
506 for(j = 0; j < sf->nsweepparam; j++) {
507 fprintf(of, "'%s'", sf->spar[j].name);
508 fprintf(of, ",");
511 fprintf(of, "), ");
513 fprintf(of, "'sweeprows':(");
514 if(sf->ntables > 1) {
515 for(i=0; i < sf->ntables; i++) {
516 fprintf(of, "(%d,%d),", sweeprows[2*i], sweeprows[2*i+1]);
519 fprintf(of, "), ");
521 fprintf(of, "'cols':(");
522 for(i = 0; i < nidx; i++) {
523 if(indices[i] == 0) {
524 ss_var_name(sf->ivar, 0, buf, 1024);
525 fprintf(of, "'%s', ", buf);
526 } else {
527 int varno = indices[i]-1;
528 SpiceVar *dvar = ss_dvar(sf, varno);
529 int dcolno = dvar->col - 1;
530 for(j = 0; j < dvar->ncols; j++) {
531 ss_var_name(dvar, j, buf, 1024);
532 fprintf(of, "'%s', ", buf);
536 fprintf(of, ") }");
538 /* space-pad to end on a 16 byte boundary */
539 while(((ftell(of)+3) % 16) != 0) {
540 fputc(' ', of);
542 fputc('\n', of);
543 foot_len = ftell(of) - foot_start + 2;
544 fputc((0xFF & foot_len), of);
545 fputc((0xFF & (foot_len >> 8)), of);
547 return foot_len;
553 * print data as a .npy format array
554 * See numpy/lib/format.py for details of the .npy file format.
556 static void
557 numpy_output(SpiceStream *sf, int *indices, int nidx,
558 double begin_val, double end_val, int ndigits, FILE *of)
560 int i, j, tab;
561 int rc;
562 int npy_hlen = 0;
563 int foot_len = 0;
564 int npy_end = 0;
565 int ndims = 0, ncols = 0, nrows = 0;
566 int *sweeprows;
567 int shape[3];
568 char buf[1024];
569 char npy_descr[5], npy_preamble[NPY_PREAMBLE_LEN], npy_header[NPY_MAX_HDR_LEN];
570 float val;
571 double ival;
572 double *dvals;
573 double *spar = NULL;
574 int done;
578 * write sham npy preamble + header to reserve space, don't know nrows yet
580 ndims = 2;
581 shape[0] = INT_MAX;
582 shape[1] = INT_MAX;
583 npy_hlen = numpy_header_output(ndims, shape, -1, of);
585 /* now write rows */
586 dvals = g_new(double, sf->ncols);
587 sweeprows = g_new(int, 2 * sf->ntables);
588 if(sf->nsweepparam > 0)
589 spar = g_new(double, sf->nsweepparam);
591 done = 0;
592 tab = 0;
593 nrows = 0;
594 sweeprows[2*tab] = nrows;
595 while(!done) {
596 if(sf->nsweepparam > 0) {
597 if(ss_readsweep(sf, spar) <= 0)
598 break;
601 while((rc = ss_readrow(sf, &ival, dvals)) > 0) {
602 if(ival < begin_val)
603 continue;
604 if(ival > end_val) {
605 /* past end_val, but can only stop reading
606 early if if there is only one sweep-table
607 in the file. */
608 if(sf->ntables == 1)
609 break;
610 else
611 continue;
615 if(sf->nsweepparam > 0) {
616 for(i = 0; i < sf->nsweepparam; i++) {
617 val = (float)spar[i];
618 fwrite(&val, sizeof(float), 1, of);
621 for(i = 0; i < nidx; i++) {
622 if(indices[i] == 0) {
623 val = (float)ival;
624 fwrite(&val, sizeof(float), 1, of);
625 } else {
626 int varno = indices[i]-1;
627 SpiceVar *dvar = ss_dvar(sf, varno);
628 int dcolno = dvar->col - 1;
629 for(j = 0; j < dvar->ncols; j++) {
630 val = (float)dvals[dcolno+j];
631 fwrite(&val, sizeof(float), 1, of);
635 nrows += 1;
637 if(rc == -2) { /* end of sweep, more follow */
638 if(sf->nsweepparam == 0)
639 sweep_mode = SWEEP_HEAD;
641 sweeprows[2*tab+1] = nrows-1;
642 tab++;
643 sweeprows[2*tab] = nrows;
644 } else { /* EOF or error */
645 done = 1;
646 sweeprows[2*tab+1] = nrows-1;
650 npy_end = ftell(of);
653 /* write trailing string representation of a python dict describing the data */
654 foot_len = numpy_footer_output(sf, indices, nidx, sweeprows, of);
658 * go back and overwrite npy header with correct values
660 fseek(of, 0, SEEK_SET);
662 /* calc num cols of data, second dimension */
663 ncols = sf->nsweepparam;
664 for(i = 0; i < nidx; i++) {
665 if(indices[i] == 0) { /* independent var */
666 ncols += 1;
667 } else {
668 int varno = indices[i]-1;
669 SpiceVar *dvar = ss_dvar(sf, varno);
670 ncols += dvar->ncols;
673 shape[0] = nrows;
674 shape[1] = ncols;
675 int npy_hlen2 = numpy_header_output(ndims, shape, npy_hlen, of);
677 if(npy_hlen2 != npy_hlen) {
678 fprintf(stderr, "numpy OOPS, inconsistent header lengths\n");
681 fclose(of);
683 g_free(dvals);
684 g_free(sweeprows);
685 if(spar)
686 g_free(spar);
693 static int parse_field_numbers(int **indices, int *idxsize, int *nidx,
694 char *list, int nfields)
696 int n, i;
697 char *fnum;
698 int err = 0;
699 int *idx;
700 if(!*indices || idxsize == 0) {
701 *idxsize = nfields*2;
702 idx = g_new0(int, *idxsize);
703 *indices = idx;
704 *nidx = 0;
707 fnum = strtok(list, ", \t");
708 i = 0;
709 while(fnum) {
710 if(*nidx >= *idxsize) {
711 *idxsize *= 2;
712 idx = g_realloc(idx, (*idxsize) * sizeof(int));
713 *indices = idx;
715 n = atoi(fnum);
716 if(n < 0 || n >= nfields) {
717 fprintf(stderr, "bad field number in -n option: %s\n", fnum);
718 err = -1;
719 } else {
720 idx[i++] = n;
721 (*nidx)++;
723 fnum = strtok(NULL, ", \t");
725 return err;
730 * Try looking for named dependent variable. Try twice,
731 * first as-is, then with "v(" prepended the way hspice mangles things.
733 static int find_dv_by_name(char *name, SpiceStream *sf)
735 int i;
736 SpiceVar *dvar;
738 for(i = 0; i < sf->ndv; i++) {
739 dvar = ss_dvar(sf, i);
740 if(strcasecmp(name, dvar->name) == 0)
741 return i;
743 for(i = 0; i < sf->ndv; i++) {
744 dvar = ss_dvar(sf, i);
745 if(strncasecmp("v(", dvar->name, 2) == 0
746 && strcasecmp(name, &dvar->name[2]) == 0)
747 return i;
749 return -1;
753 * parse comma-seperated list of field names. Turn on the output-enables
754 * for the listed fields.
756 static int parse_field_names(int **indices, int *idxsize, int *nidx,
757 char *list, SpiceStream *sf)
759 int err = 0;
760 int n;
761 char *fld;
762 int i;
763 int *idx;
765 if(!*indices || idxsize == 0) {
766 *idxsize = (sf->ndv+1)*2;
767 idx = g_new0(int, *idxsize);
768 *indices = idx;
769 *nidx = 0;
772 fld = strtok(list, ", \t");
773 i = 0;
774 while(fld) {
775 if(*nidx >= *idxsize) {
776 *idxsize *= 2;
777 idx = g_realloc(idx, (*idxsize) * sizeof(int));
778 *indices = idx;
780 if(strcasecmp(fld, sf->ivar->name)==0) {
781 idx[i++] = 0;
782 (*nidx)++;
783 } else if((n = find_dv_by_name(fld, sf)) >= 0) {
784 idx[i++] = n+1;
785 (*nidx)++;
786 } else {
787 fprintf(stderr, "field name in -f option not found in file: %s\n", fld);
788 err = -1;
790 fld = strtok(NULL, ", \t");
792 return err;
796 struct vtlistel {
797 VarType t;
798 char *s;
800 static struct vtlistel vtlist[] = {
801 {TIME, "time"},
802 {VOLTAGE, "volt"},
803 {VOLTAGE, "volts"},
804 {VOLTAGE, "voltage"},
805 {CURRENT, "current"},
806 {CURRENT, "amps"},
807 {FREQUENCY, "freq"},
808 {FREQUENCY, "frequency"},
809 {FREQUENCY, "hertz"},
810 {UNKNOWN, NULL},
814 * Given a variable type name, return a numeric VarType.
815 * Returns 0 (UNKNOWN) if no match.
817 static VarType get_vartype_code(char *vartype)
819 int i;
820 for(i = 0; vtlist[i].s; i++) {
821 if(strcasecmp(vartype, vtlist[i].s) == 0)
822 return vtlist[i].t;
824 return UNKNOWN;
827 * vim:tabstop=4 noexpandtab