/***********************************************************************

*_Title ADDLAB - Combine VICAR label and PDS label/edr data file

*

*_Descr ADDLAB combines the VICAR label and the PDS label/edr data files

*       that were created for CDROM into the original NIMS edr file format 

*       of 1024 byte records.  ADDLAB is used solely to create a file for

* 	the VICAR/NIMS software systems which reqire a VICAR formatted

*	file.

*

*       ADDLAB has two command-line arguments - the first parameter must

*	contain the name of the PDS/EDR labeled file.  Specify the complete

*	pathname, including the directory specification.  The program will

*	determine the name of the VICAR label file.  The second parameter

*	is the output file.

*

*	The commands to compile, link and run this program on a DEC VAX

*   	and a SUN UNIX workstation are below.

*

*	VAX:

*		cc addlab

*	        link addlab

*          To run a C program with command-line arguments on a VAX the

*          program must be installed as a foreign command:

*            eg:   addf=="$disk:[directory]addlab.exe"    

*	   To run:

*            eg:   addf pdsfile outfile        

*

*

*	SUN UNIX:

*		cc -o addlab.exe addlab.c          will compile and link

*						     the program.

*		addlab pdsfile outfile             will run the program.

*

*_Hist   Nov 18 1992  Tracie Stoewe, PDS Imaging Node, USGS, Flagstaff Az

*

*_End

************************************************************************/

#include <stdio.h>

#include <stdlib.h>

#include <string.h>





#define IREC_LEN1 1024

#define IREC_LEN2 512

#define	OREC_LEN 1024

#define VIC "VIC"

#define vic "vic"



int vicname();



main(argc, argv)

	int argc;

	char *argv[];

{

	static char vicfile[81];	/* Name of VICAR label file */

	static char pdsfile[81];	/* Name of PDS file */

	static char outfile[81];	/* Name of out file(VICAR label + PDS file) */

	char ibuf[OREC_LEN];



	int ierr;



	FILE *fpi1;

	FILE *fpi2;

	FILE *fpo;



	if (argc != 3)

	{

	  printf("You must specify the PDS image filename in 1st argument\n");

	  printf("You must specify output filename in 2nd argument\n");

	  exit;

	}

			

/**************************************************************************

*  The VICAR label file is constructed from the PDS filename

***************************************************************************/

	strcpy(pdsfile,argv[1]);

	strcpy(outfile,argv[2]);

	ierr = vicname(vicfile,pdsfile);

	if (ierr != 0) exit;



/***************************************************************************

* Open input files

****************************************************************************/

	if ( (fpi1 = fopen(vicfile,"rb")) == NULL )

	{

	  printf(" *** ERROR *** Can't open VICAR label file %s\n",vicfile);

	  exit;

	}



	if ( (fpi2 = fopen(pdsfile,"rb")) == NULL )

	{

	  printf(" *** ERROR *** Can't open PDS image file %s\n",pdsfile);

	  exit;

	}



/**************************************************************************

* Open output file ( VICAR label + PDS edr file including PDS labels

***************************************************************************/

#	ifdef vax

	  fpo = fopen(outfile,"wb","rfm=fix","fop=cbt","mrs=1024");

#	else

	  fpo = fopen(outfile,"wb");

#	endif

	

	if (fpo == NULL)

	{

	  printf(" *** ERROR *** Can't open outpuf image file %s\n",outfile);

	  exit;

	}



/*************************************************************************

* Read VICAR label and write to ouput

**************************************************************************/

	fread(ibuf, IREC_LEN1, 1, fpi1);

	fwrite(ibuf, OREC_LEN, 1, fpo);

	fclose(fpi1);



/*************************************************************************

*  Read PDS labels and image and write to output- input has 512 byte records,

*  output has 1024 byte records

/*************************************************************************/



	while (!feof(fpi2)) 

	{

	  fread(ibuf,IREC_LEN2, 1, fpi2);

	  fread(ibuf+IREC_LEN2,IREC_LEN2,1,fpi2);

	  if (!feof(fpi2)) fwrite(ibuf, OREC_LEN, 1, fpo);

	}



	fclose(fpi2);

	fclose(fpo);



}



int vicname(vicfile, pdsfile)

	char *vicfile;

	char *pdsfile;

/***************************************************************************

*  vicname creates the VICAR label filename from the PDS edr filename 

*  which is input

/***************************************************************************/

{

	char *i;



	strcpy(vicfile, pdsfile);

	i = strstr(vicfile, "EDR");

	if (i == NULL) 	i = strstr(vicfile, "edr");

	if (i == NULL) goto err;

	if (*i == 'E') strncpy(i,VIC,3);

	if (*i == 'e') strncpy(i,vic,3);

	

	

	i = strrchr(vicfile,']');

        if (i == NULL)  i = strrchr(vicfile,'/');

        if (i == NULL)  i = strrchr(vicfile,'\\');

        if (i == NULL)  i = strrchr(vicfile,':');

	if (i == NULL) goto err;

	if ( (toupper(*(++i))) != 'E') goto err;

	*i = (*i == 'E')?'V':'v';



	return 0;



 err: printf("*** ERROR *** Can not build VICAR file name\n");

      printf("from the PDS file name: %s\n",pdsfile);

      printf("You must include the full directory name");

      return -1;

}