home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / gmt_os2.zip / src / minmax.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-03  |  5.1 KB  |  180 lines

  1. /*--------------------------------------------------------------------
  2.  *    The GMT-system:    @(#)minmax.c    2.17  03 Aug 1995
  3.  *
  4.  *    Copyright (c) 1991-1995 by P. Wessel and W. H. F. Smith
  5.  *    See README file for copying and redistribution conditions.
  6.  *--------------------------------------------------------------------*/
  7. /*
  8.  * minmax.c will read an ascii table and report the
  9.  * extreme values for all columns
  10.  *
  11.  * Author:    Paul Wessel
  12.  * Date:    22-Jun-1991-1995
  13.  * Version:    2.0
  14.  */
  15.  
  16. #include "gmt.h"
  17.  
  18. main (argc, argv)
  19. int argc;
  20. char **argv; {
  21.     int n, i, ncol, n_files = 0, fno, n_read, n_args, ix, iy;
  22.     int  error = FALSE, nofile = TRUE, done = FALSE, step = FALSE;
  23.     double value, dx, dy, *xyzmin, *xyzmax, west, east, south, north;
  24.     char line[BUFSIZ], buffer[BUFSIZ], file[BUFSIZ], format[BUFSIZ], *p;
  25.     FILE *fp = NULL;
  26.     
  27.     argc = gmt_begin (argc, argv);
  28.     
  29.     for (i = 1; i < argc; i++) {
  30.         if (argv[i][0] == '-') {
  31.             switch (argv[i][1]) {
  32.                 /* Common parameters */
  33.                       
  34.                 case 'H':
  35.                 case ':':
  36.                 case '\0':
  37.                     error += get_common_args (argv[i], 0, 0, 0, 0);
  38.                     break;
  39.  
  40.                 /* Supplemental parameters */
  41.  
  42.                 case 'I':
  43.                     gmt_getinc (&argv[i][2], &dx, &dy);
  44.                     step = TRUE;
  45.                     break;
  46.                 default:
  47.                     error = TRUE;
  48.                     gmt_default_error (argv[i][1]);
  49.                     break;
  50.             }
  51.         }
  52.         else
  53.             n_files++;
  54.     }
  55.     
  56.     if (step && (dx <= 0.0 || dy <= 0.0)) {
  57.         fprintf (stderr, "%s: GMT SYNTAX ERROR -I option.  Must specify positive increment(s)\n", gmt_program);
  58.         error++;
  59.     }
  60.     if (error || gmt_quick) {    /* Because it's ok to give no arguments */
  61.         fprintf (stderr, "minmax %s - Find extreme values in ASCII tables\n\n", GMT_VERSION);
  62.         fprintf (stderr, "usage: minmax [files] [-H] [-Idx[/dy]] [-:]\n");
  63.              
  64.                   if (gmt_quick) exit(-1);
  65.  
  66.         explain_option ('H');
  67.         fprintf (stderr, "    -I returns textstring -Rw/e/s/n to nearest multiple of dx/dy\n");
  68.         explain_option (':');
  69.         exit (-1);
  70.     }
  71.     
  72.     if (n_files > 0)
  73.         nofile = FALSE;
  74.     else
  75.         n_files = 1;
  76.     
  77.     n_args = (argc > 1) ? argc : 2;
  78.     west = south = MAXDOUBLE;    east = north = -MAXDOUBLE;
  79.     
  80.     xyzmin = (double *) memory ((char *)NULL, 1, sizeof (double), "minmax");
  81.     xyzmax = (double *) memory ((char *)NULL, 1, sizeof (double), "minmax");
  82.     
  83.     if (step)
  84.         sprintf (format, "-R%s/%s/%s/%s\n", gmtdefs.d_format, gmtdefs.d_format, gmtdefs.d_format, gmtdefs.d_format);
  85.     else
  86.         sprintf (format, "\t<%s/%s>", gmtdefs.d_format, gmtdefs.d_format);
  87.  
  88.     ix = (gmtdefs.xy_toggle) ? 1 : 0;        iy = 1 - ix;              /* Set up which columns have x and y */
  89.  
  90.     for (fno = 1; !done && fno < n_args; fno++) {     /* Loop over input files, if any */
  91.         if (!nofile && argv[fno][0] == '-') continue;
  92.  
  93.         
  94.         if (nofile) {   /* Just read standard input */
  95.             fp = stdin;
  96.             done = TRUE;
  97.             strcpy (file, "<stdin>");
  98.             if (gmtdefs.verbose) fprintf (stderr, "minmax: Reading from standard input\n");
  99.         }
  100.         else {
  101.             strcpy (file, argv[fno]);
  102.             if ((fp = fopen (file, "r")) == NULL) {
  103.                 fprintf (stderr, "minmax: Cannot open file %s\n", file);
  104.                 continue;
  105.             }
  106.         }
  107.         
  108.         if (gmtdefs.io_header) for (i = 0; i < gmtdefs.n_header_recs; i++) fgets (line, BUFSIZ, fp);
  109.         
  110.         n = ncol = 0;
  111.         while (fgets (line, BUFSIZ, fp)) {
  112.             if (line[0] == '>') continue;    /* Multi-segment header */
  113.             if (ncol == 0) {    /* First time, allocate # of columns */
  114.                 strcpy (buffer, line);
  115.                 p = strtok (buffer, " \t\n");
  116.                 while (p) {    /* Count # of fields */
  117.                     ncol++;
  118.                     p = strtok ((char *)NULL, " \t\n");
  119.                 }
  120.             
  121.                 /* Now we know # of columns, so allocate memory */
  122.             
  123.                 xyzmin = (double *) memory ((char *)xyzmin, ncol, sizeof (double), "minmax");
  124.                 xyzmax = (double *) memory ((char *)xyzmax, ncol, sizeof (double), "minmax");
  125.             
  126.                 for (i = 0; i < ncol; i++) {    /* Initialize */
  127.                     xyzmin[i] = MAXDOUBLE;
  128.                     xyzmax[i] = -MAXDOUBLE;
  129.                 }
  130.                 if (step && ncol < 2) step = FALSE;
  131.             }
  132.         
  133.             /* Decode all fields and update minmax arrays */
  134.         
  135.             p = strtok (line, " \t\n");
  136.             i = 0;
  137.             while (p && i < ncol) {
  138.                 n_read = sscanf (p, "%lf", &value);
  139.                 if (n_read) {
  140.                     if (value < xyzmin[i]) xyzmin[i] = value;
  141.                     if (value > xyzmax[i]) xyzmax[i] = value;
  142.                 }
  143.                 i++;
  144.                 p = strtok ((char *)NULL, " \t\n");
  145.             }
  146.         
  147.             if (i != ncol) fprintf (stderr, "minmax (%s):  Expected %d but found %d fields in record # %d\n", 
  148.                 file, ncol, i, n);
  149.         
  150.             n++;
  151.         }
  152.         if (fp != stdin) fclose (fp);
  153.         
  154.         if (step) {
  155.             west  = MIN (west, xyzmin[ix]);    east  = MAX (east, xyzmax[ix]);
  156.             south = MIN (south, xyzmin[iy]);    north = MAX (north, xyzmax[iy]);
  157.         }
  158.         else {
  159.             printf ("%s: N = %d", file, n);
  160.             for (i = 0; i < ncol; i++) {
  161.                 if (xyzmin[i] == MAXDOUBLE)
  162.                     printf ("\t<NaN/NaN>");
  163.                 else
  164.                     printf (format, xyzmin[i], xyzmax[i]);
  165.             }
  166.             printf ("\n");
  167.         }
  168.     }
  169.     if (step) {
  170.         west  = floor (west / dx) * dx;        east  = ceil (east / dx) * dx;
  171.         south = floor (south / dy) * dy;    north = ceil (north / dy) * dy;
  172.         printf (format, west, east, south, north);
  173.     }
  174.     
  175.     free ((char *)xyzmin);
  176.     free ((char *)xyzmax);
  177.     
  178.     gmt_end (argc, argv);
  179. }
  180.