Imported Debian patch 1.1.1-1
[debian/splat] / utils / usgs2sdf.c
index 0c1f1faa61bb13d31395f6a2f7ff3863b525054a..acfb7f887e87e421750ccc54ebba2d89e87484b7 100644 (file)
@@ -1,7 +1,10 @@
-/****************************************************************************
+/***************************************************************************\
 *            USGS2SDF: USGS to SPLAT Data File Converter Utility            *
 *               Copyright John A. Magliacane, KD2BD 1997-2001               *
-*                         Last update: 13-Apr-2002                          *
+*                         Last update: 05-Sep-2005                          *
+*****************************************************************************
+* Updated July 2005 by John Gabrysch (jgabby@gmail.com) to properly handle  *
+*   the updated USGS DEM file format and to properly scale Alaska tiles.    *
 *****************************************************************************
 *                                                                           *
 * This program reads files containing delimited US Geological Survey        *
@@ -23,7 +26,7 @@
 * they represent (ie: min_north:max_north:min_west:max_west.sdf).           *
 *                                                                           *
 *****************************************************************************
-*          To compile: gcc -Wall -O6 -s splat2sdf.c -o splat2sdf            *
+*           To compile: gcc -Wall -O3 -s usgs2sdf.c -o usgs2sdf             *
 *****************************************************************************
 *                                                                           *
 * This program is free software; you can redistribute it and/or modify it   *
@@ -36,7 +39,7 @@
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License     *
 * for more details.                                                         *
 *                                                                           *
-*****************************************************************************/
+\***************************************************************************/
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -61,10 +64,12 @@ int argc;
 char *argv[];
 {
        unsigned char minimum[30], maximum[30], swlong[30], swlat[30],
-                nwlong[30], nwlat[30], nelong[30], nelat[30], selong[30],
-                selat[30], string[40];
-       double max_el, min_el,  max_west, min_west, max_north, min_north;
+                     nwlong[30], nwlat[30], nelong[30], nelat[30],
+                     selong[30], selat[30];
+       char string[40];
+       double max_el, min_el, max_west, min_west, max_north, min_north;
        int x, y, z, c, array[1202][1202];
+       int arc_mod=0;
        char splatfile[25];
        FILE *fd;
 
@@ -92,6 +97,7 @@ char *argv[];
 
                for (x=0; x<22; x++)
                        swlong[x]=getc(fd);
+
                swlong[x]=0;
 
                /* Skip 2 bytes */
@@ -103,6 +109,7 @@ char *argv[];
 
                for (x=0; x<22; x++)
                        swlat[x]=getc(fd);
+
                swlat[x]=0;
 
                /* Skip 2 bytes */
@@ -114,6 +121,7 @@ char *argv[];
 
                for (x=0; x<22; x++)
                        nwlong[x]=getc(fd);
+
                nwlong[x]=0;
 
                /* Skip 2 bytes */
@@ -125,6 +133,7 @@ char *argv[];
 
                for (x=0; x<22; x++)
                        nwlat[x]=getc(fd);
+
                nwlat[x]=0;
 
                /* Skip 2 bytes */
@@ -136,6 +145,7 @@ char *argv[];
 
                for (x=0; x<22; x++)
                        nelong[x]=getc(fd);
+
                nelong[x]=0;
 
                /* Skip 2 bytes */
@@ -147,6 +157,7 @@ char *argv[];
 
                for (x=0; x<22; x++)
                        nelat[x]=getc(fd);
+
                nelat[x]=0;
 
                /* Skip 2 bytes */
@@ -158,6 +169,7 @@ char *argv[];
 
                for (x=0; x<22; x++)
                        selong[x]=getc(fd);
+
                selong[x]=0;
 
                /* Skip 2 bytes */
@@ -169,6 +181,7 @@ char *argv[];
 
                for (x=0; x<22; x++)
                        selat[x]=getc(fd);
+
                selat[x]=0;
 
                /* Skip 2 bytes */
@@ -180,6 +193,7 @@ char *argv[];
 
                for (x=0; x<22; x++)
                        minimum[x]=getc(fd);
+
                minimum[x]=0;
 
                /* Skip 2 bytes */
@@ -194,23 +208,34 @@ char *argv[];
 
                maximum[x]=0;
 
-               sscanf(d2e(minimum),"%lf",&min_el);
-               sscanf(d2e(maximum),"%lf",&max_el);
+               sscanf(d2e((char*)minimum),"%lG",&min_el);
+               sscanf(d2e((char*)maximum),"%lf",&max_el);
 
-               sscanf(d2e(swlong),"%lf",&max_west);
-               sscanf(d2e(swlat),"%lf",&min_north);
+               sscanf(d2e((char*)swlong),"%lf",&max_west);
+               sscanf(d2e((char*)swlat),"%lf",&min_north);
 
-               sscanf(d2e(nelong),"%lf",&min_west);
-               sscanf(d2e(nelat),"%lf",&max_north);
+               sscanf(d2e((char*)nelong),"%lf",&min_west);
+               sscanf(d2e((char*)nelat),"%lf",&max_north);
 
                max_west/=-3600.0;
                min_north/=3600.0;
                max_north/=3600.0;
                min_west/=-3600.0;
 
-               /* Skip 84 Bytes */
+               /* If the latitude is between 50 and 70, there are only 600 vertical lines of data.  
+                  If the latitude is greater than 70, there are only 400.  arc_mod is the flag for
+                  use later */
+
+               if (min_north >= 50.0)
+                       arc_mod++;
+               
+               if (min_north >= 70.0)
+                       arc_mod++;
 
-               for (x=0; x<84; x++)
+
+               /* Skip 84 Bytes  - Modified to 238 by jgabby 07/05 */
+
+               for (x=0; x<238; x++)
                        getc(fd);
 
                /* Read elevation data... */
@@ -274,9 +299,26 @@ char *argv[];
                                        c=getc(fd);
                                }
 
-                               string[z]=0;    
+                               string[z]=0;
+
                                sscanf(string,"%d",&array[y][x]);
+
+                               /* The next few lines either duplicate or triplicate the lines to
+                                  ensure a 1200x1200 result, depending on how arc_mod was set earlier */
+
+                               if (arc_mod > 0)
+                                       sscanf(string,"%d",&array[y][x-1]);
+
+                               if (arc_mod > 1)
+                                       sscanf(string,"%d",&array[y][x-2]);                             
                        }
+                       
+                       if (arc_mod > 0)
+                               x--;
+
+
+                       if (arc_mod > 1)
+                               x--;
                }
 
                fclose(fd);
@@ -306,6 +348,8 @@ char *argv[];
                fprintf(stderr,"*** %c%s%c: File Not Found!\n",34,argv[1],34);
                exit(-1);
        }
+
        else
                exit(0);
 }
+