#include <xpap.h>
#define NXPA 1

int main(int argc, char *argv[])
{
   char *bufs[NXPA], *names[NXPA], *msgs[NXPA];
   char *parms;
   int lens[NXPA];
   int i, got;
   FILE *fd[NXPA];

   XPA UNC = XPAOpen(NULL);
	
do {	
   fd[0] = fopen("ds9.reg", "w");
   got = XPAGetFd(UNC, "SOAR:Goodman", "file ds9.reg", NULL, fd, 
	names, msgs, NXPA);
   fclose( fd[0] );
   for( i =0; i < got; i++ ) {
      if ( msgs[i] == NULL ) {
	fd[0] = fopen("ds9.reg","w");
	fwrite(bufs[i],sizeof(char),lens[i],fd);
	fclose( fd[0] ); 
	free( bufs[i] );
      } else {
	puts("error processing");
      }
      if ( names[i] ) free( names[i] );
      if ( msgs[i] ) free( msgs[i] );
  }

   fd[0] = fopen("outjpg.fits.gz", "r");
   got = XPASetFd(UNC, "SOAR:Goodman", "buf outjpg.fits.gz", NULL, fd,
	   names, msgs, NXPA);
   fclose( fd[0] );
   for( i=0; i < got; i++ ) {
	if ( msgs[i] != NULL )
	   fprintf(stderr, "ERROR: %s (%s)\n", msgs[i], names[i]);
	if ( names[i] ) free( names[i] );
	if ( msgs[i] ) free( msgs[i] );
   }
   
   fd[0] = fopen("regions.fits.gz", "r");
   got = XPASetFd(UNC, "SOAR:Goodman", "buf regions.fits.gz", NULL, fd,
	   names, msgs, NXPA);
   fclose( fd[0] );   
   for( i =0; i < got; i++ ) {
	if ( msgs[i] != NULL )
	   fprintf(stderr, "ERROR: %s (%s)\n", msgs[i], names[i]);
	if ( names[i] ) free( names[i] );
	if ( msgs[i] ) free( msgs[i] );
  }
  
  (void)XPAInfo(UNC, "IMAGE", "desquish", NULL, NULL, NULL, 0);
  (void)XPAInfo(UNC, "IMAGE", "ds9 out1.fits", NULL, NULL, NULL, 0);
  (void)XPAInfo(UNC, "IMAGE", "response", NULL, NULL, msgs, 0);
} while( strncmp(msgs[0],"q",1) );	// loop until q 
  
  fd[0] = fopen("out.fitz", "r");
  got = XPASetFd(UNC, "SOAR:Goodman", "buf out.fitz", NULL, fd,
	   names, msgs, NXPA);
  fclose( fd[0] );

  for( i=0; i < got; i++ ) {
	if ( msgs[i] != NULL )
	   fprintf(stderr, "ERROR: %s (%s)\n", msgs[i], names[i]);
	if ( names[i] ) free( names[i] );
	if ( msgs[i] ) free( msgs[i] );
  }
   XPAClose( UNC );
   return 0;
}

