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

char *TH15_codes[8] = {
  "approximate magnitude (see Field T36)",
  "variable/suspected variable from GCVS/NSV (set if Field T47 non-blank)",
  "variable/suspected variable from Tycho analysis (set if Field T48 = `U', `V' or `W')",
  "close pair, known from Hipparcos or Tycho (set if Field T2 non-blank)",
  "double/suspected double, unresolved by Tycho (set if Field T49 = `D', `R' or `S')",
  "variable/suspected variable from Hipparcos analysis (set if Field H6 non-blank)",
  "",
       NULL};
char *TH16_codes[4] = {
  "standard star for photometric calibration (in normal processing)",
  "standard star for photometric calibration (in reprocessing)",
  "photometric reduction used instrument parameters from reprocessing",
       NULL};
int read_tyc_ep (FILE* fp, tyc_ep* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * tyc_ep - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[tyc_ep_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_tyc_ep NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,tyc_ep_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->TH1);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->TH2);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->TH3);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->TH4);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->TH5);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TH6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TH7);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TH8);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TH9);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->TH10);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TH11);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TH12);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TH13);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TH14);
	token=strtok(NULL,delimiter);
	strAsBITS(token,&entry->TH15);
	token=strtok(NULL,delimiter);
	strAsBITS(token,&entry->TH16);
	read_array_tyctrans(fp,entry->TH4.value,&entry->TRANSITS);
   return 0;
} /* End of read_tyc_ep */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->TH1 );
	printf("TH1  : %s             TYC1 (GSC region)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->TH2 );
	printf("TH2  : %s             TYC2 (star number)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->TH3 );
	printf("TH3  : %s             TYC3 (component)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->TH4 );
	printf("TH4  : %s             Number of transits in epoch photometry \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->TH5 );
	printf("TH5  : %s             Number of detected and measured transits in BT\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->TH6 );
	printf("TH6  : %s         Mean magnitude BT (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->TH7 );
	printf("TH7  : %s         sigma BT (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->TH8 );
	printf("TH8  : %s         15th percentile for BT (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->TH9 );
	printf("TH9  : %s         85th percentile for BT (mag)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->TH10);
	printf("TH10 : %s             Number of detected and measured transits in VT\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->TH11);
	printf("TH11 : %s         Mean magnitude VT (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->TH12);
	printf("TH12 : %s         sigma VT (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->TH13);
	printf("TH13 : %s         15th percentile for VT (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->TH14);
	printf("TH14 : %s         85th percentile for VT (mag)\n",outputStr);
	BITSasStr((char *)&outputStr,"%8d",&entry->TH15);
	printf("TH15 : %s             Header flags \n",outputStr);
	if (decode == DECODEBITS || 
	    decode ==(DECODEBITS+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+EXTRACTCOEFF) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+EXTRACTCOEFF) ||
	    decode == DOALL )
	   showFlags(TH15_codes,&entry->TH15);
	BITSasStr((char *)&outputStr,"%8d",&entry->TH16);
	printf("TH16 : %s             Header flags\n",outputStr);
	if (decode == DECODEBITS || 
	    decode ==(DECODEBITS+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+EXTRACTCOEFF) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+EXTRACTCOEFF) ||
	    decode == DOALL )
	   showFlags(TH16_codes,&entry->TH16);
	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_tyctrans(&entry->TRANSITS,decode);
	else
	{
	   printf("TRANSITS\n");
	   print_array_tyctrans_cols(&entry->TRANSITS,decode);
	}
   return (0);
} /* End of print_tyc_ep */

int print_tyc_ep_cols (tyc_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_tyc_ep_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%4d",&entry->TH1);
	printf("%s",outputStr);
	INTasStr((char *)&outputStr,"%5d",&entry->TH2);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->TH3);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->TH4);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->TH5);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->TH6);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.3f",&entry->TH7);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->TH8);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->TH9);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->TH10);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->TH11);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.3f",&entry->TH12);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->TH13);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->TH14);
	printf("|%s",outputStr);
	BITSasStr((char *)&outputStr,"%2d",&entry->TH15);
	printf("|%s",outputStr);
	if (decode == DECODEBITS || 
	    decode ==(DECODEBITS+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+EXTRACTCOEFF) ||
	    decode ==(DECODEBITS+ARRAYVERBOSE) ||
	    decode ==(PRINTSUBRECS+DECODEBITS+EXTRACTCOEFF) ||
	    decode ==(ARRAYVERBOSE+DECODEBITS+EXTRACTCOEFF) ||
	    decode ==(ARRAYVERBOSE+DECODEBITS+PRINTSUBRECS) || 
	    decode == DOALL ) 
	{
	   showFlags(TH15_codes,&entry->TH15);
	}
	BITSasStr((char *)&outputStr,"%2d",&entry->TH16);
	printf("|%s",outputStr);
	if (decode == DECODEBITS || 
	    decode ==(DECODEBITS+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+EXTRACTCOEFF) ||
	    decode ==(DECODEBITS+ARRAYVERBOSE) ||
	    decode ==(PRINTSUBRECS+DECODEBITS+EXTRACTCOEFF) ||
	    decode ==(ARRAYVERBOSE+DECODEBITS+EXTRACTCOEFF) ||
	    decode ==(ARRAYVERBOSE+DECODEBITS+PRINTSUBRECS) || 
	    decode == DOALL ) 
	{
	   showFlags(TH16_codes,&entry->TH16);
	}
	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_tyctrans_cols(&entry->TRANSITS,decode);
	}
	printf("\r\n");
	return (0);
} /* End of print_tyc_ep_cols */

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

	printf("TH1 |TH2  |3|TH4|TH5|TH6   |TH7  |TH8   |TH9   |H10|TH11  |TH12 |TH13  |TH14  |15|16\n");
   return (0);
} /* End of print_tyc_ep_headre */



FILE* search_tyc_ep (tyc_id* key, tyc_ep* arecord)
{
   FILE* dataFile=NULL;
   long recNum;
   
   recNum= find_idx_tyc_ep (key);
   if (recNum >= 0)
   {
	dataFile=jump_tyc_ep(recNum);
	if (read_tyc_ep (dataFile,arecord)==0)
	   return dataFile;
	else
	   return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_tyc_ep */

