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

int read_hip_ep (FILE* fp, hip_ep* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hip_ep - 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_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hip_ep NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hip_ep_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->HH1);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->HH2,token);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HH3);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->HH4);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->HH5);
	token=strtok(NULL,delimiter);
	fgets((char *)&buffer,hip_ep_REC_LEN+2,fp);
	token=strtok((char *)&buffer,(char *)&delimiter);
	strAsFLOAT(token,&entry->HH6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HH7);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HH8);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HH9);
	token=strtok(NULL,delimiter);
	fgets((char *)&buffer,hip_ep_REC_LEN+2,fp);
	token=strtok((char *)&buffer,(char *)&delimiter);
	strAsFLOAT(token,&entry->HH10);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HH11);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->HH12,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->HH13,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->HH14,token);
	read_array_hiptrans(fp,entry->HH4.value,&entry->TRANSITS);
   return 0;
} /* End of read_hip_ep */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->HH1 );
	printf("HH1  : %s             HIP number\n",outputStr);
	printf("HH2  : %1.1s                    Component flag\n",entry->HH2 );
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->HH3 );
	printf("HH3  : %s         V-I (mag)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->HH4 );
	printf("HH4  : %s             Total transits, Ntot\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->HH5 );
	printf("HH5  : %s             Accepted transits, Nacc\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->HH6 );
	printf("HH6  : %s        Median magnitude,  Hp (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->HH7 );
	printf("HH7  : %s        sigma Hp (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->HH8 );
	printf("HH8  : %s           5th percentile (max) (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->HH9 );
	printf("HH9  : %s          95th percentile (min) (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%16.7f",&entry->HH10);
	printf("HH10 : %s     Variability period, P (day)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->HH11);
	printf("HH11 : %s        Reference epoch (day)\n",outputStr);
	printf("HH12 : %1.1s                    Variability type\n",entry->HH12);
	printf("HH13 : %1.1s                    Annex flag (tables)\n",entry->HH13);
	printf("HH14 : %2.2s                   Annex flag (light curves)\n",entry->HH14);
	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_hiptrans(&entry->TRANSITS,decode);
	else
	{
	   printf("TRANSITS\n");
	   print_array_hiptrans_cols(&entry->TRANSITS,decode);
	}
   return (0);
} /* End of print_hip_ep */

int print_hip_ep_cols (hip_ep* 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_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%6d",&entry->HH1);
	printf("%s",outputStr);
	printf("|%1s",entry->HH2);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->HH3);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->HH4);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->HH5);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->HH6);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.4f",&entry->HH7);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->HH8);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->HH9);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%11.7f",&entry->HH10);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%9.4f",&entry->HH11);
	printf("|%s",outputStr);
	printf("|%1s",entry->HH12);
	printf("|%1s",entry->HH13);
	printf("|%1s",entry->HH14);
	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_hiptrans_cols(&entry->TRANSITS,decode);
	}
	printf("\r\n");
	return (0);
} /* End of print_hip_ep_cols */

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

	printf("HH1   |2|HH3   |HH4|HH5|HH6    |HH7   |HH8  |HH9  |HH10       |HH11     |2|3|4\n");
   return (0);
} /* End of print_hip_ep_headre */



FILE* search_hip_ep (idx_hip_ep* key, hip_ep* arecord)
{
   FILE* dataFile=NULL;
   long recNum;
   
   recNum= find_idx_hip_ep (key);
   if (recNum >= 0)
   {
	dataFile=jump_hip_ep(recNum);
	if (read_hip_ep (dataFile,arecord)==0)
	   return dataFile;
	else
	   return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_hip_ep */

