home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
-
- /********************************************************
- ** Read a USGS DEM file and convert to binary form.
- ** Alan L. Jones, January 25, 1995
- **
- ** Input: 9 MB ASCII file from the web:
- **
- ** http://edcwww.cr.usgs.gov/glis/hyper/guide/l_dgr_dem
- **
- ** Output: Binary (2 bytes per integer) file of 1201x1201
- ** points covering 1 deg x 1 deg with a data point (height in m)
- ** every 3 arc-seconds.
- **
- ** Found that every 1024 bytes, counting from
- ** the beginning of the file, there is an
- ** adjustment so a number does not cross a
- ** 1024 boundary.
- ********************************************************/
-
-
- #define BUFSIZE 1201
- #define INBUF 1024
- main(argc, argv)
- int argc;
- char **argv;
- {
- FILE *fp1, *fp2;
- short outbuf[BUFSIZE];
- int i, j, k;
- int pos = 0; /* Keeps track of where we are in file */
- int dd;
- char *fn1, *fn2;
- float f;
- char buffer[INBUF+1];
-
- if(argc < 3) {
- printf("Syntax:\n\dem2bin in_file out_file\n");
- exit(1);
- }
- fn1 = argv[1];
- fn2 = argv[2];
- fp1 = fopen(fn1,"rt");
- if(fp1 == NULL) {
- printf("Can't find input file\n");
- exit(1);
- }
- fp2 = fopen(fn2,"wb");
- /* Print out header */
- fread(buffer,INBUF,1,fp1);
- buffer[INBUF] = 0;
- printf("\n%s\n",buffer);
-
- for(i=0;i<BUFSIZE;i++)
- {
- pos = 144; /* Data starts 144 bytes after header */
- fread(buffer,INBUF,1,fp1);
- if(0 == (i%100))
- printf("\n");
- if(0 == (i%10))
- printf("%d ",i);
- for(j = 0;j<BUFSIZE;j++) {
- if(0==sscanf(buffer+pos,"%d",&dd)) {
- /* Need next block */
- fread(buffer,INBUF,1,fp1);
- pos = 0;
- if(0==sscanf(buffer+pos,"%d",&dd)) {
- printf("Problem\n");
- exit(1);
- }
- }
- /* Byte swap and put in outbuf */
- /* Don't do this if on a PC */
- /* outbuf[j] = (dd >> 8) + (dd << 8); */
- /* Instead do: */
- outbuf[j] = dd;
-
- pos += 6;
- }
- fwrite(outbuf,BUFSIZE,sizeof(dd),fp2);
- }
- printf("\n");
- fclose(fp1);
- fclose(fp2);
- return 0;
- }
-