Imported Upstream version 1.3.0
[debian/splat] / utils / usgs2sdf.c
1 /****************************************************************************
2 *            USGS2SDF: USGS to SPLAT Data File Converter Utility            *
3 *               Copyright John A. Magliacane, KD2BD 1997-2009               *
4 *                         Last update: 14-Mar-2009                          *
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];
66         char string[40];
67         double max_el, min_el,  max_west, min_west, max_north, min_north;
68         int x, y, z, c, array[1202][1202];
69         char splatfile[25];
70         FILE *fd;
71
72         if (argc!=2)
73         {
74                 fprintf(stderr,"Usage: usgs2sdf uncompressed_delimited_usgs_datafile (ie: wilmington-e)\n");
75                 exit(0);
76         }
77
78         fd=fopen(argv[1],"rb");
79
80         if (fd!=NULL)
81         {
82                 fprintf(stdout,"Reading \"%s\"...",argv[1]);
83                 fflush(stdout);
84
85                 /* Skip first 548 bytes */
86
87                 for (x=0; x<548; x++)
88                         getc(fd);
89
90                 /* Read quadrangle corners */
91         
92                 /* Read southwest longitude */
93
94                 for (x=0; x<22; x++)
95                         swlong[x]=getc(fd);
96                 swlong[x]=0;
97
98                 /* Skip 2 bytes */
99
100                 for (x=0; x<2; x++)
101                         getc(fd);
102
103                 /* Read southwest latitude */
104
105                 for (x=0; x<22; x++)
106                         swlat[x]=getc(fd);
107                 swlat[x]=0;
108
109                 /* Skip 2 bytes */
110
111                 for (x=0; x<2; x++)
112                         getc(fd);
113
114                 /* Read northwest longitude */
115
116                 for (x=0; x<22; x++)
117                         nwlong[x]=getc(fd);
118                 nwlong[x]=0;
119
120                 /* Skip 2 bytes */
121
122                 for (x=0; x<2; x++)
123                         getc(fd);
124
125                 /* Read northwest latitude */
126
127                 for (x=0; x<22; x++)
128                         nwlat[x]=getc(fd);
129                 nwlat[x]=0;
130
131                 /* Skip 2 bytes */
132
133                 for (x=0; x<2; x++)
134                         getc(fd);
135
136                 /* Read northeast longitude */
137
138                 for (x=0; x<22; x++)
139                         nelong[x]=getc(fd);
140                 nelong[x]=0;
141
142                 /* Skip 2 bytes */
143
144                 for (x=0; x<2; x++)
145                         getc(fd);
146
147                 /* Read northeast latitude */
148
149                 for (x=0; x<22; x++)
150                         nelat[x]=getc(fd);
151                 nelat[x]=0;
152
153                 /* Skip 2 bytes */
154
155                 for (x=0; x<2; x++)
156                         getc(fd);
157
158                 /* Read southeast longitude */
159
160                 for (x=0; x<22; x++)
161                         selong[x]=getc(fd);
162                 selong[x]=0;
163
164                 /* Skip 2 bytes */
165
166                 for (x=0; x<2; x++)
167                         getc(fd);
168
169                 /* Read southeast latitude */
170
171                 for (x=0; x<22; x++)
172                         selat[x]=getc(fd);
173                 selat[x]=0;
174
175                 /* Skip 2 bytes */
176
177                 for (x=0; x<2; x++)
178                         getc(fd);
179
180                 /* Read minimum elevation */ 
181
182                 for (x=0; x<22; x++)
183                         minimum[x]=getc(fd);
184                 minimum[x]=0;
185
186                 /* Skip 2 bytes */
187
188                 for (x=0; x<2; x++)
189                         getc(fd);
190
191                 /* Read maximum elevation */
192
193                 for (x=0; x<22; x++)
194                         maximum[x]=getc(fd);
195
196                 maximum[x]=0;
197
198                 sscanf(d2e((char*)minimum),"%lG",&min_el);
199                 sscanf(d2e((char*)maximum),"%lf",&max_el);
200
201                 sscanf(d2e((char*)swlong),"%lf",&max_west);
202                 sscanf(d2e((char*)swlat),"%lf",&min_north);
203
204                 sscanf(d2e((char*)nelong),"%lf",&min_west);
205                 sscanf(d2e((char*)nelat),"%lf",&max_north);
206
207                 max_west/=-3600.0;
208                 min_north/=3600.0;
209                 max_north/=3600.0;
210                 min_west/=-3600.0;
211
212                 /* Skip 84 Bytes */
213
214                 for (x=0; x<84; x++)
215                         getc(fd);
216
217                 /* Read elevation data... */
218
219                 for (x=1200; x>=0; x--)
220                 {
221                         if (x==900)
222                         {
223                                 printf(" 25%c...",37);
224                                 fflush(stdout);
225                         }
226
227                         if (x==600)
228                         {
229                                 printf(" 50%c...",37);
230                                 fflush(stdout);
231                         }
232
233                         if (x==300)
234                         {
235                                 printf(" 75%c... ",37);
236                                 fflush(stdout);
237                         }
238
239                         /* Skip over 9 strings of data */
240
241                         for (y=0; y<9; y++)
242                         {
243                                 string[0]=0;
244
245                                 do
246                                 {
247                                         c=getc(fd);
248
249                                 } while (c==' ' || c=='\n');
250
251                                 for (z=0; c!=' ' && c!='\n' && z<28; z++)
252                                 {
253                                         string[z]=c;
254                                         c=getc(fd);
255                                 }
256
257                                 string[z]=0;    
258                         }
259
260                         /* Store elevation data in array */
261
262                         for (y=0; y<1201; y++)
263                         {
264                                 string[0]=0;
265
266                                 do
267                                 {
268                                         c=getc(fd);
269
270                                 } while (c==' ' || c=='\n');
271
272                                 for (z=0; c!=' ' && c!='\n' && z<28; z++)
273                                 {
274                                         string[z]=c;
275                                         c=getc(fd);
276                                 }
277
278                                 string[z]=0;    
279                                 sscanf(string,"%d",&array[y][x]);
280                         }
281                 }
282
283                 fclose(fd);
284
285                 /* Write splat data file to disk */
286
287                 sprintf(splatfile,"%.0f:%.0f:%.0f:%.0f.sdf",min_north,max_north,min_west,max_west);
288
289                 fprintf(stdout," Done!\nWriting \"%s\"... ",splatfile);
290                 fflush(stdout);
291
292                 fd=fopen(splatfile,"w");
293
294                 fprintf(fd,"%.0f\n%.0f\n%.0f\n%.0f\n", max_west, min_north, min_west, max_north);
295
296                 for (x=0; x<1200; x++)
297                         for (y=0; y<1200; y++)
298                                 fprintf(fd,"%d\n",array[x][y]);
299
300                 fclose(fd);
301                 fprintf(stdout,"Done!\n");
302                 fflush(stdout);
303         }
304
305         if (fd==NULL)
306         {
307                 fprintf(stderr,"*** %c%s%c: File Not Found!\n",34,argv[1],34);
308                 exit(-1);
309         }
310         else
311                 exit(0);
312 }