File: usgs2sdf.c

package info (click to toggle)
splat 1.2.1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,144 kB
  • ctags: 238
  • sloc: cpp: 6,260; ansic: 761; sh: 251; lisp: 244; makefile: 54
file content (355 lines) | stat: -rw-r--r-- 8,351 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/***************************************************************************\
*            USGS2SDF: USGS to SPLAT Data File Converter Utility            *
*               Copyright John A. Magliacane, KD2BD 1997-2001               *
*                         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        *
* Digital Elevation Model Data files, and creates Splat Data Files          *
* containing ONLY the raw information needed by SPLAT!, thereby saving      *
* disk space, as well as read/write time.                                   *
*                                                                           *
* The format of .sdf files created by this utility is as follows:           *
*                                                                           *
*	maximum west longitude (degrees West)                               *
*	minimum north latitude (degrees North)                              *
*	minimum west longitude (degrees West)                               *
*	maximum north latitude (degrees North)                              *
*       ...1440000 elevation points... (1200x1200)                          *
*                                                                           *
* All data is represented as integers.  A single '\n' follows each value.   *
*                                                                           *
* SPLAT Data Files are named according to the geographical locations        *
* they represent (ie: min_north:max_north:min_west:max_west.sdf).           *
*                                                                           *
*****************************************************************************
*           To compile: gcc -Wall -O3 -s usgs2sdf.c -o usgs2sdf             *
*****************************************************************************
*                                                                           *
* This program is free software; you can redistribute it and/or modify it   *
* under the terms of the GNU General Public License as published by the     *
* Free Software Foundation; either version 2 of the License or any later    *
* version.                                                                  *
*                                                                           *
* This program is distributed in the hope that it will useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     *
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License     *
* for more details.                                                         *
*                                                                           *
\***************************************************************************/

#include <stdio.h>
#include <stdlib.h>

char *d2e(string)
char *string;
{
	/* This function is used to replace 'D's with 'E's for proper
	   exponential notation of numeric strings read from delimited
	   USGS data files.  It returns a pointer to a string. */

	unsigned char x;

	for (x=0; string[x]!=0; x++)
		if (string[x]=='D')
			string[x]='E';
	return (string);
}

int main(argc,argv)
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];
	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;

	if (argc!=2)
	{
		fprintf(stderr,"Usage: usgs2sdf uncompressed_delimited_usgs_datafile (ie: wilmington-e)\n");
		exit(0);
	}

	fd=fopen(argv[1],"rb");

	if (fd!=NULL)
	{
		fprintf(stdout,"Reading \"%s\"...",argv[1]);
		fflush(stdout);

		/* Skip first 548 bytes */

		for (x=0; x<548; x++)
			getc(fd);

		/* Read quadrangle corners */
	
		/* Read southwest longitude */

		for (x=0; x<22; x++)
			swlong[x]=getc(fd);

		swlong[x]=0;

		/* Skip 2 bytes */

		for (x=0; x<2; x++)
			getc(fd);

		/* Read southwest latitude */

		for (x=0; x<22; x++)
			swlat[x]=getc(fd);

		swlat[x]=0;

		/* Skip 2 bytes */

		for (x=0; x<2; x++)
			getc(fd);

		/* Read northwest longitude */

		for (x=0; x<22; x++)
			nwlong[x]=getc(fd);

		nwlong[x]=0;

		/* Skip 2 bytes */

		for (x=0; x<2; x++)
			getc(fd);

		/* Read northwest latitude */

		for (x=0; x<22; x++)
			nwlat[x]=getc(fd);

		nwlat[x]=0;

		/* Skip 2 bytes */

		for (x=0; x<2; x++)
			getc(fd);

		/* Read northeast longitude */

		for (x=0; x<22; x++)
			nelong[x]=getc(fd);

		nelong[x]=0;

		/* Skip 2 bytes */

		for (x=0; x<2; x++)
			getc(fd);

		/* Read northeast latitude */

		for (x=0; x<22; x++)
			nelat[x]=getc(fd);

		nelat[x]=0;

		/* Skip 2 bytes */

		for (x=0; x<2; x++)
			getc(fd);

		/* Read southeast longitude */

		for (x=0; x<22; x++)
			selong[x]=getc(fd);

		selong[x]=0;

		/* Skip 2 bytes */

		for (x=0; x<2; x++)
			getc(fd);

		/* Read southeast latitude */

		for (x=0; x<22; x++)
			selat[x]=getc(fd);

		selat[x]=0;

		/* Skip 2 bytes */

		for (x=0; x<2; x++)
			getc(fd);

		/* Read minimum elevation */ 

		for (x=0; x<22; x++)
			minimum[x]=getc(fd);

		minimum[x]=0;

		/* Skip 2 bytes */

		for (x=0; x<2; x++)
			getc(fd);

		/* Read maximum elevation */

		for (x=0; x<22; x++)
			maximum[x]=getc(fd);

		maximum[x]=0;

		sscanf(d2e((char*)minimum),"%lG",&min_el);
		sscanf(d2e((char*)maximum),"%lf",&max_el);

		sscanf(d2e((char*)swlong),"%lf",&max_west);
		sscanf(d2e((char*)swlat),"%lf",&min_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;

		/* 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++;


		/* Skip 84 Bytes  - Modified to 238 by jgabby 07/05 */

		for (x=0; x<238; x++)
			getc(fd);

		/* Read elevation data... */

		for (x=1200; x>=0; x--)
		{
			if (x==900)
			{
				printf(" 25%c...",37);
				fflush(stdout);
			}

			if (x==600)
			{
				printf(" 50%c...",37);
				fflush(stdout);
			}

			if (x==300)
			{
				printf(" 75%c... ",37);
				fflush(stdout);
			}

			/* Skip over 9 strings of data */

			for (y=0; y<9; y++)
			{
				string[0]=0;

				do
				{
					c=getc(fd);

				} while (c==' ' || c=='\n');

				for (z=0; c!=' ' && c!='\n' && z<28; z++)
				{
					string[z]=c;
					c=getc(fd);
				}

				string[z]=0;	
			}

			/* Store elevation data in array */

			for (y=0; y<1201; y++)
			{
				string[0]=0;

				do
				{
					c=getc(fd);

				} while (c==' ' || c=='\n');

				for (z=0; c!=' ' && c!='\n' && z<28; z++)
				{
					string[z]=c;
					c=getc(fd);
				}

				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);

		/* Write splat data file to disk */

		sprintf(splatfile,"%.0f:%.0f:%.0f:%.0f.sdf",min_north,max_north,min_west,max_west);

		fprintf(stdout," Done!\nWriting \"%s\"... ",splatfile);
		fflush(stdout);

		fd=fopen(splatfile,"w");

		fprintf(fd,"%.0f\n%.0f\n%.0f\n%.0f\n", max_west, min_north, min_west, max_north);

		for (x=0; x<1200; x++)
			for (y=0; y<1200; y++)
				fprintf(fd,"%d\n",array[x][y]);

		fclose(fd);
		fprintf(stdout,"Done!\n");
		fflush(stdout);
	}

	if (fd==NULL)
	{
		fprintf(stderr,"*** %c%s%c: File Not Found!\n",34,argv[1],34);
		exit(-1);
	}

	else
		exit(0);
}