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

int read_hip_ep_e (FILE* fp, hip_ep_e* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hip_ep_e - 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_e_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hip_ep_e NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hip_ep_e_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->HHE1);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->HHE2,token);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HHE3);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->HHE4);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->HHE5);
	token=strtok(NULL,delimiter);
	fgets((char *)&buffer,hip_ep_e_REC_LEN+2,fp);
	token=strtok((char *)&buffer,(char *)&delimiter);
	strAsFLOAT(token,&entry->HHE6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HHE7);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HHE8);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HHE9);
	token=strtok(NULL,delimiter);
	fgets((char *)&buffer,hip_ep_e_REC_LEN+2,fp);
	token=strtok((char *)&buffer,(char *)&delimiter);
	strAsINT(token,&entry->HHE10);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HHE11);
	read_array_hepetran(fp,entry->HHE4.value,&entry->TRANSITS,&entry->HHE10);
   return 0;
} /* End of read_hip_ep_e */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->HHE1 );
	printf("HHE1  : %s             HIP number\n",outputStr);
	printf("HHE2  : %1.1s                    Component flag\n",entry->HHE2 );
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->HHE3 );
	printf("HHE3  : %s         V-I (mag)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->HHE4 );
	printf("HHE4  : %s             Total transits, Ntot\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->HHE5 );
	printf("HHE5  : %s             Accepted transits, Nacc\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->HHE6 );
	printf("HHE6  : %s        Median magnitude for the modulated signal component\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->HHE7 );
	printf("HHE7  : %s        Standard error of the median Hp(ac)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->HHE8 );
	printf("HHE8  : %s         Hp(dc) - Hp(ac)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->HHE9 );
	printf("HHE9  : %s         Ratio of the Standard errors on the medians\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->HHE10);
	printf("HHE10 : %s             Coincidence pointer offset\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->HHE11);
	printf("HHE11 : %s        Double star correction (mag)\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_hepetran(&entry->TRANSITS,decode);
	else
	{
	   printf("TRANSITS\n");
	   print_array_hepetran_cols(&entry->TRANSITS,decode);
	}
   return (0);
} /* End of print_hip_ep_e */

int print_hip_ep_e_cols (hip_ep_e* 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_e_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%6d",&entry->HHE1);
	printf("%s",outputStr);
	printf("|%1s",entry->HHE2);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->HHE3);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->HHE4);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->HHE5);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->HHE6);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.4f",&entry->HHE7);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->HHE8);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->HHE9);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%6d",&entry->HHE10);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->HHE11);
	printf("|%s",outputStr);
	if ((entry->TRANSITS.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_hepetran_cols(&entry->TRANSITS,decode);
	}
	printf("\r\n");
	return (0);
} /* End of print_hip_ep_e_cols */

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

	printf("HHE1  |2|HHE3  |HE4|HE5|HHE6   |HHE7  |HHE8  |HHE9  |HHE10 |HHE11  \n");
   return (0);
} /* End of print_hip_ep_e_headre */



FILE* search_hip_ep_e (idx_hip_ep* key, hip_ep_e* arecord)
{
   FILE* dataFile=NULL;
   long recNum;
   
   recNum= find_idx_hip_ep (key);
   if (recNum >= 0)
   {
	dataFile=jump_hip_ep_e(recNum);
	if (read_hip_ep_e (dataFile,arecord)==0)
	   return dataFile;
	else
	   return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_hip_ep */

