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

int read_hipoint (FILE* fp, hipoint* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hipoint - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[hipoint_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hipoint NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hipoint_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->JP1);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP2);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP3);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP4);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP5);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP6);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP7);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP8);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP9);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP10);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP11);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP12);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP13);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP14);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP15);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP16);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP17);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP18);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP19);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP20);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP21);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP22);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP23);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP24);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP25);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP26);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JP27);
   return 0;
} /* End of read_hipoint */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->JP1 );
	printf("JP1  : %s             Index (1 to 3) for the HIP identifier of target pos 1 \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP2 );
	printf("JP2  : %s             Offset in alpha for target pos 1 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP3 );
	printf("JP3  : %s             Offset in delta for target pos 1 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP4 );
	printf("JP4  : %s             Index (1 to 3) for the HIP identifier of target pos 2 \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP5 );
	printf("JP5  : %s             Offset in alpha for target pos 2 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP6 );
	printf("JP6  : %s             Offset in delta for target pos 2 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP7 );
	printf("JP7  : %s             Index (1 to 3) for the HIP identifier of target pos 3 \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP8 );
	printf("JP8  : %s             Offset in alpha for target pos 3 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP9 );
	printf("JP9  : %s             Offset in delta for target pos 3 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP10);
	printf("JP10 : %s             Index (1 to 3) for the HIP identifier of target pos 4 \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP11);
	printf("JP11 : %s             Offset in alpha for target pos 4 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP12);
	printf("JP12 : %s             Offset in delta for target pos 4 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP13);
	printf("JP13 : %s             Index (1 to 3) for the HIP identifier of target pos 5 \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP14);
	printf("JP14 : %s             Offset in alpha for target pos 5 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP15);
	printf("JP15 : %s             Offset in delta for target pos 5 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP16);
	printf("JP16 : %s             Index (1 to 3) for the HIP identifier of target pos 6 \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP17);
	printf("JP17 : %s             Offset in alpha for target pos 6 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP18);
	printf("JP18 : %s             Offset in delta for target pos 6 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP19);
	printf("JP19 : %s             Index (1 to 3) for the HIP identifier of target pos 7 \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP20);
	printf("JP20 : %s             Offset in alpha for target pos 7 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP21);
	printf("JP21 : %s             Offset in delta for target pos 7 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP22);
	printf("JP22 : %s             Index (1 to 3) for the HIP identifier of target pos 8 \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP23);
	printf("JP23 : %s             Offset in alpha for target pos 8 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP24);
	printf("JP24 : %s             Offset in delta for target pos 8 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP25);
	printf("JP25 : %s             Index (1 to 3) for the HIP identifier of target pos 9 \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP26);
	printf("JP26 : %s             Offset in alpha for target pos 9 (arcsec) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JP27);
	printf("JP27 : %s             Offset in delta for target pos 9 (arcsec) \n",outputStr);
   return (0);
} /* End of print_hipoint */

int print_hipoint_cols (hipoint* 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_hipoint_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%1d",&entry->JP1);
	printf("%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP2);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP3);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->JP4);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP5);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP6);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->JP7);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP8);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP9);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->JP10);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP11);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP12);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->JP13);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP14);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP15);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->JP16);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP17);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP18);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->JP19);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP20);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP21);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->JP22);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP23);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP24);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->JP25);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP26);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JP27);
	printf("|%s",outputStr);
	printf("\r\n");
	return (0);
} /* End of print_hipoint_cols */

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

	printf("1|JP2|JP3|4|JP5|JP6|7|JP8|JP9|0|P11|P12|3|P14|P15|6|P17|P18|9|P20|P21|2|P23|P24|5|P26|P27\n");
   return (0);
} /* End of print_hipoint_headre */


