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

int read_tyc_main (FILE* fp, tyc_main* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * tyc_main - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[tyc_main_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_tyc_main NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,tyc_main_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strcpy((char *)&entry->T0,token);
	token=strtok(NULL,delimiter);
	read_tyc_id(token,&entry->T1);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T2,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T3,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T4,token);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T5);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T6,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T7,token);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T8);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T9);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T10,token);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T11);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T12);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T13);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T14);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T15);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T16);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T17);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T18);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T19);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T20);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T21);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T22);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T23);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T24);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T25);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T26);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T27);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T28);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->T29);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T30);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->T31);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T32);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T33);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T34);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T35);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T36,token);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T37);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T38);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T39,token);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->T40);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T41);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T42,token);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->T43);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T44);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T45);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->T46);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T47,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T48,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T49,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T50,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T51,token);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->T52);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->T53);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T54,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T55,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T56,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->T57,token);
   return 0;
} /* End of read_tyc_main */ 

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


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

	printf("T0  : %1.1s                    Catalogue (H = Hipparcos, T = Tycho)\n",entry->T0 );
	printf("T1  : ");
	print_tyc_id_cols(&entry->T1 ,decode);
	printf("         TYC1-3 (TYC number)\n");
	printf("T2  : %1.1s                    Proximity flag\n",entry->T2 );
	printf("T3  :  %11.11s         Identifier RA,  h m s (J1991.25)\n",entry->T3 );
	printf("T4  : %11.11s          Identifier Dec, d m s (J1991.25)\n",entry->T4 );
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->T5 );
	printf("T5  : %s          V (Johnson) magnitude \n",outputStr);
	printf("T6  : %1.1s                    Blank for Tycho\n",entry->T6 );
	printf("T7  : %1.1s                    Source of magnitude identifier\n",entry->T7 );
	FLOATasStr((char *)&outputStr,"%17.8f",&entry->T8 );
	printf("T8  : %s    alpha, degrees (J1991.25)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%17.8f",&entry->T9 );
	printf("T9  : %s    delta, degrees (J1991.25)\n",outputStr);
	printf("T10 : %1.1s                    Reference flag for astrometry\n",entry->T10);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->T11);
	printf("T11 : %s           Trigonometric parallax (mas)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->T12);
	printf("T12 : %s           mu_alpha* (mas/yr)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->T13);
	printf("T13 : %s           mu_delta (mas/yr)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->T14);
	printf("T14 : %s           Standard error in alpha* at J1991.25 (mas)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->T15);
	printf("T15 : %s           Standard error in delta at J1991.25 (mas)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->T16);
	printf("T16 : %s           Standard error in pi (mas)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->T17);
	printf("T17 : %s           Standard error in mu_alpha* (mas/yr)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->T18);
	printf("T18 : %s           Standard error in mu_delta (mas/yr)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->T19);
	printf("T19 : %s          Correlation: alpha*/delta\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->T20);
	printf("T20 : %s          Correlation: alpha*/pi\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->T21);
	printf("T21 : %s          Correlation: delta/pi\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->T22);
	printf("T22 : %s          Correlation: alpha*/mu_alpha*\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->T23);
	printf("T23 : %s          Correlation: delta/mu_alpha*\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->T24);
	printf("T24 : %s          Correlation: pi/mu_alpha*\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->T25);
	printf("T25 : %s          Correlation: alpha*/mu_delta\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->T26);
	printf("T26 : %s          Correlation: delta/mu_delta\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->T27);
	printf("T27 : %s          Correlation: pi/mu_delta\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->T28);
	printf("T28 : %s          Correlation: mu_alpha*/mu_delta\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->T29);
	printf("T29 : %s             Data points accepted, N astrom \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->T30);
	printf("T30 : %s          F2 (goodness of fit)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->T31);
	printf("T31 : %s             HIP number\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->T32);
	printf("T32 : %s         BT (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->T33);
	printf("T33 : %s         sigma BT (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->T34);
	printf("T34 : %s         VT (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->T35);
	printf("T35 : %s         sigma VT (mag)\n",outputStr);
	printf("T36 : %1.1s                    Reference flag for BT and VT \n",entry->T36);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->T37);
	printf("T37 : %s         B-V (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->T38);
	printf("T38 : %s         sigma B-V (mag)\n",outputStr);
	printf("T39 : %1.1s                    Blank for Tycho\n",entry->T39);
	INTasStr((char *)&outputStr,"%8d",&entry->T40);
	printf("T40 : %s             Astrometric quality flag, Q\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->T41);
	printf("T41 : %s           S/N ratio, Fs\n",outputStr);
	printf("T42 : %1.1s                    Source of astrometric data\n",entry->T42);
	INTasStr((char *)&outputStr,"%8d",&entry->T43);
	printf("T43 : %s             Nphotom\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->T44);
	printf("T44 : %s         VT scatter, s (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->T45);
	printf("T45 : %s          Mag at max, VT (15th percentile)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->T46);
	printf("T46 : %s          Mag at min, VT (85th percentile)\n",outputStr);
	printf("T47 : %1.1s                    Variability (from GCVS/NSV)\n",entry->T47);
	printf("T48 : %1.1s                    Variability (from Tycho)\n",entry->T48);
	printf("T49 : %1.1s                    Duplicity (from Tycho)\n",entry->T49);
	printf("T50 : %1.1s                    Flag (epoch data)\n",entry->T50);
	printf("T51 : %2.2s                   CCDM component identifier\n",entry->T51);
	INTasStr((char *)&outputStr,"%8d",&entry->T52);
	printf("T52 : %s             PPM identifier\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->T53);
	printf("T53 : %s             HD identifier\n",outputStr);
	printf("T54 : %10.10s           DM (BD) identifier\n",entry->T54);
	printf("T55 : %10.10s           DM (CoD) identifier\n",entry->T55);
	printf("T56 : %10.10s           DM (CPD) identifier\n",entry->T56);
	printf("T57 : %1.1s                    Notes\n",entry->T57);
   return (0);
} /* End of print_tyc_main */

int print_tyc_main_cols (tyc_main* 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_main_cols NULL pointer for entry\n");
      return (-1);
   }

	printf("%1s",entry->T0);
	printf("|");
	   print_tyc_id_cols(&entry->T1,decode);
	printf("|%1s",entry->T2);
	printf("|%11s",entry->T3);
	printf("|%11s",entry->T4);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->T5);
	printf("|%s",outputStr);
	printf("|%1s",entry->T6);
	printf("|%1s",entry->T7);
	FLOATasStr((char *)&outputStr,"%012.8f",&entry->T8);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%+012.8f",&entry->T9);
	printf("|%s",outputStr);
	printf("|%1s",entry->T10);
	FLOATasStr((char *)&outputStr,"%7.2f",&entry->T11);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%8.2f",&entry->T12);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%8.2f",&entry->T13);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->T14);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->T15);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->T16);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->T17);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->T18);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%+5.2f",&entry->T19);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%+5.2f",&entry->T20);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%+5.2f",&entry->T21);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%+5.2f",&entry->T22);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%+5.2f",&entry->T23);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%+5.2f",&entry->T24);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%+5.2f",&entry->T25);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%+5.2f",&entry->T26);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%+5.2f",&entry->T27);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%+5.2f",&entry->T28);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->T29);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->T30);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%6d",&entry->T31);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->T32);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.3f",&entry->T33);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->T34);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.3f",&entry->T35);
	printf("|%s",outputStr);
	printf("|%1s",entry->T36);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->T37);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.3f",&entry->T38);
	printf("|%s",outputStr);
	printf("|%1s",entry->T39);
	INTasStr((char *)&outputStr,"%1d",&entry->T40);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%4.1f",&entry->T41);
	printf("|%s",outputStr);
	printf("|%1s",entry->T42);
	INTasStr((char *)&outputStr,"%3d",&entry->T43);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.3f",&entry->T44);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->T45);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->T46);
	printf("|%s",outputStr);
	printf("|%1s",entry->T47);
	printf("|%1s",entry->T48);
	printf("|%1s",entry->T49);
	printf("|%1s",entry->T50);
	printf("|%2s",entry->T51);
	INTasStr((char *)&outputStr,"%6d",&entry->T52);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%6d",&entry->T53);
	printf("|%s",outputStr);
	printf("|%10s",entry->T54);
	printf("|%10s",entry->T55);
	printf("|%10s",entry->T56);
	printf("|%1s",entry->T57);
	printf("\r\n");
	return (0);
} /* End of print_tyc_main_cols */

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

	printf("0|T1          |2|T3         |T4         |T5   |6|7|T8          |T9          |0|T11    |T12     |T13     |T14   |T15   |T16   |T17   |T18   |T19  |T20  |T21  |T22  |T23  |T24  |T25  |T26  |T27  |T28  |T29|T30  |T31   |T32   |T33  |T34   |T35  |6|T37   |T38  |9|0|T41 |2|T43|T44  |T45  |T46  |7|8|9|0|51|T52   |T53   |T54       |T55       |T56       |7\n");
   return (0);
} /* End of print_tyc_main_headre */



FILE* search_tyc_main (tyc_id* key, tyc_main* arecord)
{
   int recMax,left,right,mid,found; 
   static idx_tyc_main index[idx_tyc_main_entries];
   FILE* dataFile=NULL;
   long recNum;
   static int init=0;
   if (init == 0)
   {
	if (load_idx_tyc_main("tyc_main.idx",(idx_tyc_main*)&index) !=0)
             return NULL;
	init=1;
   }
   if (key->tyc1.value > idx_tyc_main_entries || key->tyc1.value < 1 ||
       key->tyc2.value < 1 || key->tyc3.value < 1 || key->tyc3.value > 9 ) 
   {
	fprintf(stderr,"Bad TYC Id %d %d %d\n",key->tyc1.value,key->tyc2.value,key->tyc3.value); 
	return NULL;
   }
   recNum= index[key->tyc1.value-1].value;
   left = recNum;
   recMax= index[key->tyc1.value].value;	
   right = recMax;
   found = 0;
   /* We must search between recNum and recMax to find the corect record 
      can use a binary search for that - thanks to Volkmar Grossmann*/
   while (! found && !(left==mid && right==mid) && left <= right)
   {
      mid = (right +left) /2;
      dataFile=jump_tyc_main(mid);
      if (read_tyc_main (dataFile,arecord) != 0) break;
      found = (arecord->T1.tyc2.value == key->tyc2.value &&
	        arecord->T1.tyc1.value == key->tyc1.value); 
      if ( arecord->T1.tyc2.value > key->tyc2.value)
	right = mid; 
      if ( arecord->T1.tyc2.value < key->tyc2.value) 
	left = mid +1;
      /*printf (" %d,%d Betweeen %d and %d Key = %d\n",mid,arecord->T1.tyc2.value,left,right,key->tyc2.value); */
   }
   if (!found)
      return NULL;
   else
   { /* final step with tyc3 */
      if (arecord->T1.tyc3.value != key->tyc3.value)
      {
         recNum = mid - arecord->T1.tyc3.value + key->tyc3.value;
	 dataFile=jump_tyc_main(recNum);
         if (read_tyc_main (dataFile,arecord)==0)	
	    /* Just check we really have it */
	    if (arecord->T1.tyc3.value == key->tyc3.value)
		return dataFile;
	    else
		return NULL;
      }
      else
         return dataFile;
   }

}/* End search_idx_tyc_main */

