Imported Upstream version 1.1.0
[debian/splat] / utils / usgs2sdf.c
1 /****************************************************************************
2 *            USGS2SDF: USGS to SPLAT Data File Converter Utility            *
3 *               Copyright John A. Magliacane, KD2BD 1997-2001               *
4 *                         Last update: 13-Apr-2002                          *
5 *****************************************************************************
6 *                                                                           *
7 * This program reads files containing delimited US Geological Survey        *
8 * Digital Elevation Model Data files, and creates Splat Data Files          *
9 * containing ONLY the raw information needed by SPLAT!, thereby saving      *
10 * disk space, as well as read/write time.                                   *
11 *                                                                           *
12 * The format of .sdf files created by this utility is as follows:           *
13 *                                                                           *
14 *       maximum west longitude (degrees West)                               *
15 *       minimum north latitude (degrees North)                              *
16 *       minimum west longitude (degrees West)                               *
17 *       maximum north latitude (degrees North)                              *
18 *       ...1440000 elevation points... (1200x1200)                          *
19 *                                                                           *
20 * All data is represented as integers.  A single '\n' follows each value.   *
21 *                                                                           *
22 * SPLAT Data Files are named according to the geographical locations        *
23 * they represent (ie: min_north:max_north:min_west:max_west.sdf).           *
24 *                                                                           *
25 *****************************************************************************
26 *          To compile: gcc -Wall -O6 -s splat2sdf.c -o splat2sdf            *
27 *****************************************************************************
28 *                                                                           *
29 * This program is free software; you can redistribute it and/or modify it   *
30 * under the terms of the GNU General Public License as published by the     *
31 * Free Software Foundation; either version 2 of the License or any later    *
32 * version.                                                                  *
33 *                                                                           *
34 * This program is distributed in the hope that it will useful, but WITHOUT  *
35 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     *
36 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License     *
37 * for more details.                                                         *
38 *                                                                           *
39 *****************************************************************************/
40
41 #include <stdio.h>
42 #include <stdlib.h>
43
44 char *d2e(string)
45 char *string;
46 {
47         /* This function is used to replace 'D's with 'E's for proper
48            exponential notation of numeric strings read from delimited
49            USGS data files.  It returns a pointer to a string. */
50
51         unsigned char x;
52
53         for (x=0; string[x]!=0; x++)
54                 if (string[x]=='D')
55                         string[x]='E';
56         return (string);
57 }
58
59 int main(argc,argv)
60 int argc;
61 char *argv[];
62 {
63         unsigned char minimum[30], maximum[30], swlong[30], swlat[30],
64                  nwlong[30], nwlat[30], nelong[30], nelat[30], selong[30],
65                  selat[30], string[40];
66         double max_el, min_el,  max_west, min_west, max_north, min_north;
67         int x, y, z, c, array[1202][1202];
68         char splatfile[25];
69         FILE *fd;
70
71         if (argc!=2)
72         {
73                 fprintf(stderr,"Usage: usgs2sdf uncompressed_delimited_usgs_datafile (ie: wilmington-e)\n");
74                 exit(0);
75         }
76
77         fd=fopen(argv[1],"rb");
78
79         if (fd!=NULL)
80         {
81                 fprintf(stdout,"Reading \"%s\"...",argv[1]);
82                 fflush(stdout);
83
84                 /* Skip first 548 bytes */
85
86                 for (x=0; x<548; x++)
87                         getc(fd);
88
89                 /* Read quadrangle corners */
90         
91                 /* Read southwest longitude */
92
93                 for (x=0; x<22; x++)
94                         swlong[x]=getc(fd);
95                 swlong[x]=0;
96
97                 /* Skip 2 bytes */
98
99                 for (x=0; x<2; x++)
100                         getc(fd);
101
102                 /* Read southwest latitude */
103
104                 for (x=0; x<22; x++)
105                         swlat[x]=getc(fd);
106                 swlat[x]=0;
107
108                 /* Skip 2 bytes */
109
110                 for (x=0; x<2; x++)
111                         getc(fd);
112
113                 /* Read northwest longitude */
114
115                 for (x=0; x<22; x++)
116                         nwlong[x]=getc(fd);
117                 nwlong[x]=0;
118
119                 /* Skip 2 bytes */
120
121                 for (x=0; x<2; x++)
122                         getc(fd);
123
124                 /* Read northwest latitude */
125
126                 for (x=0; x<22; x++)
127                         nwlat[x]=getc(fd);
128                 nwlat[x]=0;
129
130                 /* Skip 2 bytes */
131
132                 for (x=0; x<2; x++)
133                         getc(fd);
134
135                 /* Read northeast longitude */
136
137                 for (x=0; x<22; x++)
138                         nelong[x]=getc(fd);
139                 nelong[x]=0;
140
141                 /* Skip 2 bytes */
142
143                 for (x=0; x<2; x++)
144                         getc(fd);
145
146                 /* Read northeast latitude */
147
148                 for (x=0; x<22; x++)
149                         nelat[x]=getc(fd);
150                 nelat[x]=0;
151
152                 /* Skip 2 bytes */
153
154                 for (x=0; x<2; x++)
155                         getc(fd);
156
157                 /* Read southeast longitude */
158
159                 for (x=0; x<22; x++)
160                         selong[x]=getc(fd);
161                 selong[x]=0;
162
163                 /* Skip 2 bytes */
164
165                 for (x=0; x<2; x++)
166                         getc(fd);
167
168                 /* Read southeast latitude */
169
170                 for (x=0; x<22; x++)
171                         selat[x]=getc(fd);
172                 selat[x]=0;
173
174                 /* Skip 2 bytes */
175
176                 for (x=0; x<2; x++)
177                         getc(fd);
178
179                 /* Read minimum elevation */ 
180
181                 for (x=0; x<22; x++)
182                         minimum[x]=getc(fd);
183                 minimum[x]=0;
184
185                 /* Skip 2 bytes */
186
187                 for (x=0; x<2; x++)
188                         getc(fd);
189
190                 /* Read maximum elevation */
191
192                 for (x=0; x<22; x++)
193                         maximum[x]=getc(fd);
194
195                 maximum[x]=0;
196
197                 sscanf(d2e(minimum),"%lf",&min_el);
198                 sscanf(d2e(maximum),"%lf",&max_el);
199
200                 sscanf(d2e(swlong),"%lf",&max_west);
201                 sscanf(d2e(swlat),"%lf",&min_north);
202
203                 sscanf(d2e(nelong),"%lf",&min_west);
204                 sscanf(d2e(nelat),"%lf",&max_north);
205
206                 max_west/=-3600.0;
207                 min_north/=3600.0;
208                 max_north/=3600.0;
209                 min_west/=-3600.0;
210
211                 /* Skip 84 Bytes */
212
213                 for (x=0; x<84; x++)
214                         getc(fd);
215
216                 /* Read elevation data... */
217
218                 for (x=1200; x>=0; x--)
219                 {
220                         if (x==900)
221                         {
222                                 printf(" 25%c...",37);
223                                 fflush(stdout);
224                         }
225
226                         if (x==600)
227                         {
228                                 printf(" 50%c...",37);
229                                 fflush(stdout);
230                         }
231
232                         if (x==300)
233                         {
234                                 printf(" 75%c... ",37);
235                                 fflush(stdout);
236                         }
237
238                         /* Skip over 9 strings of data */
239
240                         for (y=0; y<9; y++)
241                         {
242                                 string[0]=0;
243
244                                 do
245                                 {
246                                         c=getc(fd);
247
248                                 } while (c==' ' || c=='\n');
249
250                                 for (z=0; c!=' ' && c!='\n' && z<28; z++)
251                                 {
252                                         string[z]=c;
253                                         c=getc(fd);
254                                 }
255
256                                 string[z]=0;    
257                         }
258
259                         /* Store elevation data in array */
260
261                         for (y=0; y<1201; y++)
262                         {
263                                 string[0]=0;
264
265                                 do
266                                 {
267                                         c=getc(fd);
268
269                                 } while (c==' ' || c=='\n');
270
271                                 for (z=0; c!=' ' && c!='\n' && z<28; z++)
272                                 {
273                                         string[z]=c;
274                                         c=getc(fd);
275                                 }
276
277                                 string[z]=0;    
278                                 sscanf(string,"%d",&array[y][x]);
279                         }
280                 }
281
282                 fclose(fd);
283
284                 /* Write splat data file to disk */
285
286                 sprintf(splatfile,"%.0f:%.0f:%.0f:%.0f.sdf",min_north,max_north,min_west,max_west);
287
288                 fprintf(stdout," Done!\nWriting \"%s\"... ",splatfile);
289                 fflush(stdout);
290
291                 fd=fopen(splatfile,"w");
292
293                 fprintf(fd,"%.0f\n%.0f\n%.0f\n%.0f\n", max_west, min_north, min_west, max_north);
294
295                 for (x=0; x<1200; x++)
296                         for (y=0; y<1200; y++)
297                                 fprintf(fd,"%d\n",array[x][y]);
298
299                 fclose(fd);
300                 fprintf(stdout,"Done!\n");
301                 fflush(stdout);
302         }
303
304         if (fd==NULL)
305         {
306                 fprintf(stderr,"*** %c%s%c: File Not Found!\n",34,argv[1],34);
307                 exit(-1);
308         }
309         else
310                 exit(0);
311 }