/* file: rdtxt.c G. Moody 4 June 1995 Last revised: 29 January 1996 This program extracts parameters from a set of `.txt' files. Copyright (C) Massachusetts Institute of Technology 1996. All rights reserved. */ #include #include #define SEGLEN 600 /* length of a segment, in seconds */ char *pname; int len; main(argc, argv) int argc; char **argv; { char *getparam(), *prog_name(); char *p, *parameter = NULL, *record = NULL; char ifname[20], *ifpath; FILE *ifile; int fields_needed = 1, i, n, segment, last_segment; double h, m, s, t, tstart = 0.0, tstop = 0.0, tdiv = 0.0; void help(); pname = prog_name(argv[0]); for (i = 1; i < argc; i++) { if (*argv[i] == '-') switch (*(argv[i]+1)) { case 'f': /* start time follows */ if (++i >= argc) { fprintf(stderr, "%s: stop time must follow -t\n", pname); exit(1); } n = sscanf(argv[i], "%lf:%lf:%lf", &h, &m, &s); switch (n){ case 1: tstart = h; break; case 2: tstart = 60.0*h + m; break; case 3: tstart = 3600.0*h + 60.0*m + s; break; } break; case 'h': /* help requested */ help(); exit(0); break; case 'n': /* number of fields needed follows */ if (++i >= argc) { fprintf(stderr, "%s: number of fields needed must follow -n\n", pname); exit(1); } fields_needed = atoi(argv[i]); if (fields_needed < 0 || fields_needed > 3) fields_needed = 0; break; case 'p': /* parameter name follows */ if (++i >= argc) { fprintf(stderr, "%s: parameter name must follow -r\n", pname); exit(1); } parameter = argv[i]; len = strlen(parameter); break; case 'r': /* record name follows */ if (++i >= argc) { fprintf(stderr, "%s: record name must follow -r\n", pname); exit(1); } record = argv[i]; break; case 't': /* stop time follows */ if (++i >= argc) { fprintf(stderr, "%s: stop time must follow -t\n", pname); exit(1); } n = sscanf(argv[i], "%lf:%lf:%lf", &h, &m, &s); switch (n){ case 1: tstop = h; break; case 2: tstop = 60.0*h + m; break; case 3: tstop = 3600.0*h + 60.0*m + s; break; } break; case 'V': /* alternate verbose mode, print output sample times */ tdiv = 1.0; if (argv[i][2] == 'm') tdiv = 60.0; else if (argv[i][2] == 'h') tdiv = 3600.0; break; default: (void)fprintf(stderr, "%s: unrecognized option %s\n", pname, argv[i]); exit(1); } else { (void)fprintf(stderr, "%s: unrecognized argument %s\n", pname, argv[i]); exit(1); } } if (record == NULL || parameter == NULL) { help(); exit(1); } /* Open the first segment that contains data of interest. */ segment = tstart/SEGLEN + 1; t = 600.0*(segment-1); sprintf(ifname, "%s%05d.txt", record, segment); if ((ifpath = dbfile(ifname, NULL)) == NULL || (ifile = fopen(ifpath, "r")) == NULL) { fprintf(stderr, "%s: can't open data file `%s'\n", pname, ifname); exit(2); } /* Get the first recorded value for the parameter of interest. */ if ((p = getparam(ifile, parameter)) == NULL) { fclose(ifile); fprintf(stderr, "%s: parameter `%s' not found in data file `%s'\n", pname, parameter, ifpath); exit(3); } while (tstop == 0. || t <= tstop) { if (t >= tstart) { int a, b, c, n; n = sscanf(p + len, "%d%d%d", &a, &b, &c); if (n >= fields_needed) { if (tdiv > 0.0) printf("%g\t", t/tdiv); printf("%s", p); } } t += 1.024; if ((p = getparam(ifile, parameter)) == NULL) { fclose(ifile); /* Open the next segment */ sprintf(ifname, "%s%05d.txt", record, ++segment); if ((ifpath = dbfile(ifname, NULL)) == NULL || (ifile = fopen(ifpath, "r")) == NULL) { if (tstop == 0.) exit(0); else { fprintf(stderr, "%s: can't open data file `%s'\n", pname, ifname); exit(2); } } if ((p = getparam(ifile, parameter)) == NULL) { fclose(ifile); fprintf(stderr, "%s: parameter `%s' not found in data file `%s'\n", pname, parameter, ifpath); exit(3); } } } fclose(ifile); exit(0); } char *getparam(ifile, parameter) FILE *ifile; char *parameter; { static char buf[80]; while (fgets(buf, sizeof(buf), ifile)) if (strncmp(buf, parameter, len) == 0) return (buf); return (NULL); } char *prog_name(s) char *s; { char *p = s + strlen(s); #ifdef MSDOS while (p >= s && *p != '\\' && *p != ':') { if (*p == '.') *p = '\0'; /* strip off extension */ if ('A' <= *p && *p <= 'Z') *p += 'a' - 'A'; /* convert to lower case */ p--; } #else while (p >= s && *p != '/') p--; #endif return (p+1); } static char *help_strings[] = { "usage: %s -r RECORD -p PARAMETER [OPTIONS ...]\n", "where RECORD specifies the input, PARAMETER matches a string in the input", "and OPTIONS may include:", " -f TIME start at specified TIME", " -h print this usage summary", " -n N do not print lines with fewer than N numeric fields (N < 4)", " -t TIME stop at specified TIME", " -Vs (or -V) print elapsed time in seconds before each output sample value", " -Vm print elapsed time in minutes before each output sample value", " -Vh print elapsed time in hours before each output sample value", NULL }; void help() { int i; (void)fprintf(stderr, help_strings[0], pname); for (i = 1; help_strings[i] != NULL; i++) (void)fprintf(stderr, "%s\n", help_strings[i]); }