home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / gmt_os2.zip / src / grdreformat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-20  |  4.8 KB  |  155 lines

  1. /*--------------------------------------------------------------------
  2.  *    The GMT-system:    @(#)grdreformat.c    3.11  3/13/95
  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.  * grdreformat.c reads a grd file in one format and outputs it in another
  9.  *
  10.  * Author:    Paul Wessel
  11.  * Date:    3-JAN-1991-1995
  12.  * Version:    3.0
  13.  */
  14.  
  15. #include "gmt.h"
  16.  
  17. float *z;
  18.  
  19. main (argc, argv)
  20. int argc;
  21. char **argv; {
  22.     int i, nfiles = 0, nm, nx, ny, one_or_zero, dummy[4];
  23.     int error = FALSE, global = FALSE;
  24.     double w, e, s, n, scale = 1.0, offset = 0.0;
  25.     char *file[2], fname[2][BUFSIZ], line[BUFSIZ];
  26.     struct GRD_HEADER grd;
  27.     FILE *fp;
  28.     char *gmt_data_path;
  29.     argc = gmt_begin (argc, argv);
  30.  
  31.     w = e = s = n = 0.0;
  32.     dummy[0] = dummy[1] = dummy[2] = dummy[3] = 0;
  33.     
  34.     for (i = 1; i < argc; i++) {
  35.         if (argv[i][0] == '-') {
  36.             switch (argv[i][1]) {
  37.                 /* Common parameters */
  38.             
  39.                 case 'R':
  40.                 case 'V':
  41.                 case '\0':
  42.                     error += get_common_args (argv[i], &w, &e, &s, &n);
  43.                     break;
  44.                     
  45.                 default:
  46.                     error = TRUE;
  47.                     gmt_default_error (argv[i][1]);
  48.                     break;
  49.             }
  50.         }
  51.         else if (nfiles < 2) {
  52.             file[nfiles] = argv[i];
  53.             nfiles++;
  54.         }
  55.         else
  56.             nfiles++;
  57.     }
  58.     
  59.     if (argc == 1 || gmt_quick) {    /* Display usage */
  60.         fprintf (stderr, "grdreformat %s - Converting between different grdfile formats\n\n", GMT_VERSION);
  61.         fprintf( stderr, "usage: grdreformat ingrdfile[=id[/scale/offset]] outgrdfile[=id[/scale/offset]] [-Rw/e/s/n] [-V]\n");
  62.  
  63.         if (gmt_quick) exit (-1);
  64.         
  65.         fprintf (stderr, "    ingrdfile is the grd file to convert\n");
  66.         fprintf (stderr, "    outgrdfile is the new converted grd file\n");
  67.         fprintf( stderr, "       scale and offset, if given, will multiply data by scale and add offset.\n");
  68.         fprintf (stderr, "\n\tOPTIONS:\n");
  69.         explain_option ('r');
  70.         explain_option ('V');
  71.         
  72.         fprintf (stderr, "    The following formats are supported\n\n");
  73. /*                                                                           */        
  74. /*   Modifications to permit the use of an environmental variable (specified */
  75. /*   in "gmt_os2.h") to point to the directory path.                         */
  76. /*                                                                           */
  77.        gmt_data_path = getenv (GMTDATAPATH);
  78.         sprintf (line, "%s/gmtformats.d\0", gmt_data_path);
  79.       
  80. /*        sprintf (line, "%s/gmtformats.d\0", LIBDIR);    */
  81.         if ((fp = fopen (line, "r")) == NULL) {
  82.             fprintf (stderr, "%s: GMT ERROR: Cannot read file %s\n", gmt_program, line);
  83.             exit (-1);
  84.         }
  85.         while (fgets (line, BUFSIZ, fp)) fprintf (stderr, "\t%s", line);
  86.         fclose (fp);
  87.         exit (-1);
  88.     }
  89.     
  90.     if (nfiles != 2) {
  91.         fprintf (stderr, "%s: GMT SYNTAX ERROR: Must specify both input and output file names\n", gmt_program);
  92.         error++;
  93.     }
  94.     
  95.     if (error) exit (-1);
  96.     
  97.     grd_i_format = grd_get_i_format (file[0], fname[0], &scale, &offset);
  98.     grd_o_format = grd_get_o_format (file[1], fname[1], &scale, &offset);
  99.     
  100.     if (grd_i_format == grd_o_format) {
  101.         fprintf (stderr, "grdreformat: Formats are identical - aborts\n");
  102.         exit (-1);
  103.     }
  104.     
  105.     if (gmtdefs.verbose) {
  106.         if (file[0][0] == '=') strcpy (fname[0], "<stdin>");
  107.         if (file[1][0] == '=') strcpy (fname[1], "<stdout>");
  108.         fprintf (stderr, "grdreformat: Translating file %s (format = %d) to file %s (format = %d)\n", fname[0], grd_i_format, fname[1], grd_o_format);
  109.     }
  110.     
  111.     if (read_grd_info (file[0], &grd)) {
  112.         fprintf (stderr, "grdreformat: Error opening file %s\n", fname[0]);
  113.         exit (-1);
  114.     }
  115.     
  116.     nm = grd.nx * grd.ny;
  117.     
  118.     if (e > w && n > s) {
  119.         global = (fabs (grd.x_max - grd.x_min) == 360.0);
  120.         if (!global && (w < grd.x_min || e > grd.x_max)) error = TRUE;
  121.         if (s < grd.y_min || n > grd.y_max) error = TRUE;
  122.         if (error) {
  123.             fprintf (stderr, "grdreformat: Subset exceeds data domain!\n");
  124.             exit (-1);
  125.         }
  126.         one_or_zero = (grd.node_offset) ? 0 : 1;
  127.         nx = rint ((e - w) / grd.x_inc) + one_or_zero;
  128.         ny = rint ((n - s) / grd.y_inc) + one_or_zero;
  129.         
  130.         z = (float *) memory (CNULL, nx * ny, sizeof (float), "grdreformat");
  131.         
  132.         if (read_grd (file[0], &grd, z, w, e, s, n, dummy, FALSE)) {
  133.             fprintf (stderr, "grdreformat: Error reading file %s\n", fname[0]);
  134.             exit (-1);
  135.         }
  136.     }
  137.     else {
  138.         z = (float *) memory (CNULL, nm, sizeof (float), "grdreformat");
  139.  
  140.         if (read_grd (file[0], &grd, z, 0.0, 0.0, 0.0, 0.0, dummy, FALSE)) {
  141.             fprintf (stderr, "grdreformat: Error reading file %s\n", fname[0]);
  142.             exit (-1);
  143.         }
  144.     }
  145.  
  146.     if (write_grd (file[1], &grd, z, 0.0, 0.0, 0.0, 0.0, dummy, FALSE)) {
  147.         fprintf (stderr, "grdreformat: Error writing file %s\n", fname[1]);
  148.         exit (-1);
  149.     }
  150.     
  151.     free ((char *)z);
  152.         
  153.     gmt_end (argc, argv);
  154. }
  155.