/*  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 "hip_ep_c.h" 

int read_hip_ep_c (FILE* fp, hip_ep_c* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hip_ep_c - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[hip_ep_c_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hip_ep_c NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hip_ep_c_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsFLOAT(token,&entry->HCE1);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HCE2);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HCE3);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->HCE4);
   return 0;
} /* End of read_hip_ep_c */ 

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


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

	FLOATasStr((char *)&outputStr,"%10.1f",&entry->HCE1 );
	printf("HCE1  : %s           Distance from IFOV centre (arcsec)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->HCE2 );
	printf("HCE2  : %s           Magnitude (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->HCE3 );
	printf("HCE3  : %s           Colour index, if known (mag)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->HCE4 );
	printf("HCE4  : %s             Coincidence terminator flag\n",outputStr);
   return (0);
} /* End of print_hip_ep_c */

int print_hip_ep_c_cols (hip_ep_c* 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_hip_ep_c_cols NULL pointer for entry\n");
      return (-1);
   }

	FLOATasStr((char *)&outputStr,"%4.1f",&entry->HCE1);
	printf("%s",outputStr);
	FLOATasStr((char *)&outputStr,"%4.1f",&entry->HCE2);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%4.1f",&entry->HCE3);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->HCE4);
	printf("|%s",outputStr);
	printf("\r\n");
	return (0);
} /* End of print_hip_ep_c_cols */

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

	printf("HCE1|HCE2|HCE3|4\n");
   return (0);
} /* End of print_hip_ep_c_headre */


/*  array routines  */ 
int read_array_hip_ep_c (int no_entries, array_hip_ep_c* array, INT* offset)
{
/* In this case no_entries is actaully the offset to be added to the offset to
   give the first coincedence record in the file hip_ep_c.dat
 */
   FILE* datafile;
   int recNum = no_entries + offset->value; 
   if ( no_entries > 0 ) /* then there are entries */
   {
      /* First jump to correct location in data file */
      if ( (datafile = jump_hip_ep_c(recNum )) != NULL)
      {
   	/* Load coincedences until the array is full or we got them all */
	int finished=0,i=0;
	while (!finished)
	{
           if (read_hip_ep_c (datafile,&array->data[i]) == -1) 
	      return -1;
	   /* HCE4 is th terminator flag - says if we got them all	 */
	   finished = (i == array_hip_ep_c_size) || (array->data[i].HCE4.value == 1);
	   i++;
	}
	array->no_entries = i;
	return 0;
      } 
      else /* Some kind of error */
      {
	fprintf (stderr,"read_array_hip_ep_c could not get record %d\n ", recNum);
	return -1;
      }
   }/* Otherwise no entries but that is okay */
   return 0;
}
int print_array_hip_ep_c_cols (array_hip_ep_c* array, int decode) 
{
   int i;
   if (array == NULL) 
   {
      fprintf (stderr,"print_array_hip_ep_c NULL pointer for entry\n");
      return (-1);
   }
/*   printf("Array array_hip_ep_c contains %d entries :\n",array->no_entries);*/
      if (array->no_entries > 0)
      {
         print_hip_ep_c_header();
      }
      for (i=0; i<array->no_entries; i++)
      {
	 print_hip_ep_c_cols (&array->data[i],decode) ;
      }
      return (0);
} /* End of print_array_hip_ep_c_cols */

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

