/*  Hipparcos ASCII CD-ROM load and search routines Release 1.1 June 1997
    William O'Mullane 
    Astrophysics Division, ESTEC, Noordwijk, The Netherlands. 
    See the readme.pdf file for more information */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "utils.h" 
#include "hepetran.h" 

int read_hepetran (FILE* fp,hepetran* entry, INT* offset)
{
   char buffer[hepetran_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hepetran NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hepetran_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsFLOAT(token,&entry->HTE1);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HTE2);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->HTE3);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->HTE4);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->HTE5);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HTE6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HTE7);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->HTE8);
	read_array_hip_ep_c(entry->HTE8.value,&entry->COINCEDENCE,offset);
   return 0;
} /* End of read_hepetran */ 

FILE* jump_hepetran (long recNum) 
{
   static int init=0;
   static FILE* dataFile;
   if (init==0)
   {
      if ((dataFile = fopen ("hepetran.dat","r")) == NULL)
      {
         fprintf(stderr,"Could not open hepetran.dat \n");
         return NULL;
      }
      init=1;
   }
   if ( fseek (dataFile, (recNum - 1) * hepetran_REC_LEN, 0) == 0)
     return dataFile;
   else
     return NULL;
}


int print_hepetran (hepetran* entry, int decode) 
{
/*  Just print out each attribute with its value  */
   char outputStr[50];
   if (entry == NULL) 
   {
      fprintf (stderr,"print_hepetran NULL pointer for entry\n");
      return (-1);
   }

	FLOATasStr((char *)&outputStr,"%12.3f",&entry->HTE1 );
	printf("HTE1  : %s         Hpac for the transit (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->HTE2 );
	printf("HTE2  : %s         sigma Hpac (mag)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->HTE3 );
	printf("HTE3  : %s             Background, bF (Hz)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->HTE4 );
	printf("HTE4  : %s             Background, bN (Hz)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->HTE5 );
	printf("HTE5  : %s             Field of view index\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->HTE6 );
	printf("HTE6  : %s        RA of other field of view (degrees)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->HTE7 );
	printf("HTE7  : %s        Dec of other field of view (degrees)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->HTE8 );
	printf("HTE8  : %s             Coincidence index\n",outputStr);
	if (decode == ARRAYVERBOSE || 
	    decode ==(ARRAYVERBOSE+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE) || 
	    decode ==(ARRAYVERBOSE+EXTRACTCOEFF) ||
	    decode ==(PRINTSUBRECS+ARRAYVERBOSE+EXTRACTCOEFF) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+EXTRACTCOEFF) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+PRINTSUBRECS) || 
	    decode == DOALL )
	   print_array_hip_ep_c(&entry->COINCEDENCE,decode);
	else
	{
	   printf("COINCEDENCE\n");
	   print_array_hip_ep_c_cols(&entry->COINCEDENCE,decode);
	}
   return (0);
} /* End of print_hepetran */

int print_hepetran_cols (hepetran* entry, int decode) 
{
   char outputStr[200];
/*  print attributes accross screen as in datafile - 
    decode fields depending on the value of decode (values defined in utils.h */
   if (entry == NULL) 
   {
      fprintf (stderr,"print_hepetran_cols NULL pointer for entry\n");
      return (-1);
   }

	FLOATasStr((char *)&outputStr,"%6.3f",&entry->HTE1);
	printf("%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.3f",&entry->HTE2);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->HTE3);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->HTE4);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->HTE5);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%8.4f",&entry->HTE6);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%8.4f",&entry->HTE7);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%2d",&entry->HTE8);
	printf("|%s",outputStr);
	if ((entry->COINCEDENCE.no_entries > 0 )&&
	    (decode == PRINTSUBRECS ||
	     decode ==(EXTRACTCOEFF+PRINTSUBRECS) || 
	     decode ==(DECODEBITS+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+PRINTSUBRECS) ||
	     decode ==(DECODEBITS+EXTRACTCOEFF+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+DECODEBITS+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+EXTRACTCOEFF+PRINTSUBRECS) || 
	     decode == DOALL ) ) 
	{
	   printf("\n");
	   print_array_hip_ep_c_cols(&entry->COINCEDENCE,decode);
	}
	printf("\r\n");
	return (0);
} /* End of print_hepetran_cols */

int print_hepetran_header () 
{
/*  print col names accross screen as in datafile */

	printf("HTE1  |HTE2 |TE3|TE4|5|HTE6    |HTE7    |E8\n");
   return (0);
} /* End of print_hepetran_headre */


/*  array routines  */ 
int read_array_hepetran (FILE* fp,int no_entries, array_hepetran* array, INT* offset)
{
/* Read no_entries entries from the file into the array provided
   use read routine to achive this.
 */
   int i;
   
   array->no_entries=0;
   for (i=0; i<no_entries; i++)
   {
	if (read_hepetran (fp,&array->data[i],offset) == -1) 
	   break;
	
	if (array_hepetran_size == i) 
	{
	    fprintf(stderr," array_hepetran not big enough - all elements not loaded\n");
	    break;
	}
   }
   array->no_entries = i;
   return 0;
} /* End of read_array_hepetran */

int print_array_hepetran_cols (array_hepetran* array, int decode) 
{
   int i;
   if (array == NULL) 
   {
      fprintf (stderr,"print_array_hepetran NULL pointer for entry\n");
      return (-1);
   }
/*   printf("Array array_hepetran contains %d entries :\n",array->no_entries);*/
      if (array->no_entries > 0)
      {
         print_hepetran_header();
      }
      for (i=0; i<array->no_entries; i++)
      {
	 print_hepetran_cols (&array->data[i],decode) ;
      }
      return (0);
} /* End of print_array_hepetran_cols */

int print_array_hepetran (array_hepetran* array, int decode) 
{
   int i;
   if (array == NULL) 
   {
      fprintf (stderr,"print_array_hepetran NULL pointer for entry\n");
      return (-1);
   }
/*   printf("Array array_hepetran contains %d entries :\n",array->no_entries);*/
      for (i=0; i<array->no_entries; i++)
      {
	 print_hepetran (&array->data[i],decode) ;
      }
      return (0);
} /* End of print_array_hepetran */

