/* Kurt Schwehr - NASA Ames Research Center - 8/10/94       */
/* schwehr@cs.stanford.edu                                  */
/* Program to put out the end of a file after so many bites */
/* Use this to split appart the header label text and the   */
/* trailing binary image file.  Dumb, but it works          */

/*
   The usgs viking cdrom images are in the standard PDS format
   with a human/computer readable header and a "flat" binary
   image file.  Unfortunately, grass can only handle the binary
   portion in a file of exactly the size of the image data.  I
   used this program to split the pds files into a label header
   of text and an image file of binary data.  I can then copy
   the binary data into a prexisting grass location/mapset's
   cell directory.  Use r.support and the info in the header
   to bring the data into grass.
*/

/* HeaderSize = (FileSize)-(LINE * LINESAMPLES) * (SAMPLE_BITS/8) */


#include <stdio.h>


void main() {

  char infile[255],txt_outfile[255],img_outfile[255];
  FILE *InFile,*OutFile;
  int length,buffer,count=0;

  printf ("File to read from:");
  scanf ("%s",infile);
  printf ("Img Output file:");
  scanf ("%s",img_outfile);
  printf ("Text Output file:");
  scanf ("%s",txt_outfile);
  printf ("Text Header Size:");
  scanf ("%d",&length);
  InFile = fopen (infile,"r");

  /* Read/Write the Text Header */
  OutFile = fopen (txt_outfile,"w");
  for (count=0;count<length;count++) {
    buffer=getc(InFile);
    putc(buffer,OutFile);
  }
  printf ("%d characters written\n",count);
  fclose (OutFile);

  /* Read/Write the trailing binary image */
  OutFile = fopen (img_outfile,"w");
  count=0;
  while ( (buffer=getc(InFile)) != EOF ) {
    putc(buffer,OutFile);
    count++;
  }

  printf ("%d characters written\n",count);

  fclose (InFile);
  fclose (OutFile);
}
