home *** CD-ROM | disk | FTP | other *** search
/ Earthquakes & Eruptions / EQERUPT.iso / seismic / dem2bin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-24  |  2.1 KB  |  89 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /********************************************************
  5. ** Read a USGS DEM file and convert to binary form.
  6. ** Alan L. Jones, January 25, 1995
  7. **
  8. ** Input: 9 MB ASCII file from the web:
  9. **
  10. ** http://edcwww.cr.usgs.gov/glis/hyper/guide/l_dgr_dem
  11. **
  12. ** Output: Binary (2 bytes per integer) file of 1201x1201
  13. ** points covering 1 deg x 1 deg with a data point (height in m)
  14. ** every 3 arc-seconds.
  15. **
  16. ** Found that every 1024 bytes, counting from
  17. ** the beginning of the file, there is an
  18. ** adjustment so a number does not cross a
  19. ** 1024 boundary.
  20. ********************************************************/
  21.  
  22.  
  23. #define BUFSIZE 1201
  24. #define INBUF 1024
  25. main(argc, argv)
  26.   int argc;
  27.   char **argv;
  28. {
  29.   FILE *fp1, *fp2;
  30.   short outbuf[BUFSIZE];
  31.   int i, j, k;
  32.   int pos = 0;          /* Keeps track of where we are in file */
  33.   int dd;
  34.   char *fn1, *fn2;
  35.   float f;
  36.   char buffer[INBUF+1];
  37.  
  38.   if(argc < 3) {
  39.     printf("Syntax:\n\dem2bin in_file out_file\n");
  40.     exit(1);
  41.   }
  42.   fn1 = argv[1];
  43.   fn2 = argv[2];
  44.   fp1 = fopen(fn1,"rt");
  45.   if(fp1 == NULL) {
  46.     printf("Can't find input file\n");
  47.     exit(1);
  48.   }
  49.   fp2 = fopen(fn2,"wb");
  50.   /* Print out header */
  51.   fread(buffer,INBUF,1,fp1);
  52.   buffer[INBUF] = 0;
  53.   printf("\n%s\n",buffer);
  54.  
  55.   for(i=0;i<BUFSIZE;i++)
  56.   {
  57.     pos = 144;  /* Data starts 144 bytes after header */
  58.     fread(buffer,INBUF,1,fp1);
  59.     if(0 == (i%100))
  60.       printf("\n");
  61.     if(0 == (i%10))
  62.       printf("%d ",i);
  63.     for(j = 0;j<BUFSIZE;j++) {
  64.       if(0==sscanf(buffer+pos,"%d",&dd)) {
  65.         /* Need next block */
  66.         fread(buffer,INBUF,1,fp1);
  67.         pos = 0;
  68.         if(0==sscanf(buffer+pos,"%d",&dd)) {
  69.           printf("Problem\n");
  70.           exit(1);
  71.         }
  72.       }
  73.       /* Byte swap and put in outbuf */
  74.       /* Don't do this if on a PC */
  75.       /* outbuf[j] = (dd >> 8) + (dd << 8); */
  76.       /* Instead do: */
  77.       outbuf[j] = dd;
  78.  
  79.       pos += 6;
  80.     }
  81.     fwrite(outbuf,BUFSIZE,sizeof(dd),fp2);
  82.   }
  83.   printf("\n");
  84.   fclose(fp1);
  85.   fclose(fp2);
  86.   return 0;
  87. }
  88.  
  89.