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

int read_hip_i (FILE* fp, hip_i* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hip_i - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[hip_i_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hip_i NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hip_i_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->IH1);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IH2);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IH3);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IH4);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IH5);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IH6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IH7);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->IH8,token);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->IH9);
	read_array_hipabsc(fp,entry->IH9.value,&entry->ABCISSAE);
   return 0;
} /* End of read_hip_i */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->IH1  );
	printf("IH1   : %s             Hipparcos Catalogue (HIP) identifier \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->IH2  );
	printf("IH2   : %s          Provisional Hp magnitude used for the merging (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%17.8f",&entry->IH3  );
	printf("IH3   : %s    Right ascension alpha (deg)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%17.8f",&entry->IH4  );
	printf("IH4   : %s    Declination delta (deg)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->IH5  );
	printf("IH5   : %s          Trigonometric parallax pi (mas)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->IH6  );
	printf("IH6   : %s          Proper motion in right ascension mu_alpha* (mas/year)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->IH7  );
	printf("IH7   : %s          Proper motion in declination mu_delta (mas/year)\n",outputStr);
	printf("IH8   : %1.1s                    Code for adopted solution (5, 7, 9, C, O, V, X, -)\n",entry->IH8  );
	INTasStr((char *)&outputStr,"%8d",&entry->IH9  );
	printf("IH9   : %s             Number of following abscissae records, N_A\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_hipabsc(&entry->ABCISSAE,decode);
	else
	{
	   printf("ABCISSAE\n");
	   print_array_hipabsc_cols(&entry->ABCISSAE,decode);
	}
   return (0);
} /* End of print_hip_i */

int print_hip_i_cols (hip_i* 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_i_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%6d",&entry->IH1 );
	printf("%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->IH2 );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%12.8f",&entry->IH3 );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%12.8f",&entry->IH4 );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->IH5 );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%8.2f",&entry->IH6 );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%8.2f",&entry->IH7 );
	printf("|%s",outputStr);
	printf("|%1s",entry->IH8 );
	INTasStr((char *)&outputStr,"%3d",&entry->IH9 );
	printf("|%s",outputStr);
	if ((entry->ABCISSAE.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_hipabsc_cols(&entry->ABCISSAE,decode);
	}
	printf("\r\n");
	return (0);
} /* End of print_hip_i_cols */

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

	printf("IH1   |IH2  |IH3         |IH4         |IH5   |IH6     |IH7     | |H9 \n");
   return (0);
} /* End of print_hip_i_headre */



FILE* search_hip_i (idx_hip_i* key, hip_i* arecord)
{
   FILE* dataFile=NULL;
   long recNum;
   
   recNum= find_idx_hip_i (key);
   if (recNum >= 0)
   {
	dataFile=jump_hip_i(recNum);
	if (read_hip_i (dataFile,arecord)==0)
	   return dataFile;
	else
	   return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_hip_i */

