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

int read_hip_j (FILE* fp, hip_j* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hip_j - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[hip_j_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hip_j NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hip_j_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->JH1);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JH2);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JH3);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JH4);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JH5);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JH6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JH7);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JH8);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JH9);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JH10);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JH11);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JH12);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JH13);
	read_hipoint(fp,&entry->POINTING);
	read_array_hipjtran(fp,entry->JH5.value,&entry->TRANSITS);
   return 0;
} /* End of read_hip_j */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->JH1 );
	printf("JH1  : %s             HIP identifier for the first pointing in the system \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JH2 );
	printf("JH2  : %s             Additional HIP identifier (set to 0 if not used) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JH3 );
	printf("JH3  : %s             Additional HIP identifier (set to 0 if not used) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JH4 );
	printf("JH4  : %s             Number of different target positions, NP \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JH5 );
	printf("JH5  : %s             Number of following transit records, NT \n",outputStr);
	FLOATasStr((char *)&outputStr,"%17.8f",&entry->JH6 );
	printf("JH6  : %s    Right ascension of reference point, alpha0 (deg) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%17.8f",&entry->JH7 );
	printf("JH7  : %s    Declination of reference point, delta0 (deg) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->JH8 );
	printf("JH8  : %s          Parallax of reference point, pi0 (mas) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->JH9 );
	printf("JH9  : %s          Proper motion in alpha of reference point, mu_alpha*0 (mas/yr) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->JH10);
	printf("JH10 : %s          Proper motion in delta of reference point, mu_delta 0 (mas/yr) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->JH11);
	printf("JH11 : %s         Assumed V-I for HIP identifier in Field JH1 (mag) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->JH12);
	printf("JH12 : %s         Assumed V-I for HIP identifier in Field JH2 (mag) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->JH13);
	printf("JH13 : %s         Assumed V-I for HIP identifier in Field JH3 (mag) \n",outputStr);
	printf("POINTING                        Pointing Record\n");
	print_hipoint(&entry->POINTING,decode);
	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_hipjtran(&entry->TRANSITS,decode);
	else
	{
	   printf("TRANSITS\n");
	   print_array_hipjtran_cols(&entry->TRANSITS,decode);
	}
   return (0);
} /* End of print_hip_j */

int print_hip_j_cols (hip_j* 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_j_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%6d",&entry->JH1);
	printf("%s",outputStr);
	INTasStr((char *)&outputStr,"%6d",&entry->JH2);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%6d",&entry->JH3);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%2d",&entry->JH4);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->JH5);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%12.8f",&entry->JH6);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%12.8f",&entry->JH7);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->JH8);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%8.2f",&entry->JH9);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%8.2f",&entry->JH10);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.3f",&entry->JH11);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.3f",&entry->JH12);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.3f",&entry->JH13);
	printf("|%s",outputStr);
	if ( 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_hipoint_header();
	   print_hipoint_cols(&entry->POINTING,decode);
	}
	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_hipjtran_cols(&entry->TRANSITS,decode);
	}
	printf("\r\n");
	return (0);
} /* End of print_hip_j_cols */

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

	printf("JH1   |JH2   |JH3   |H4|JH5|JH6         |JH7         |JH8   |JH9     |JH10    |JH11   |JH12   |JH13   \n");
   return (0);
} /* End of print_hip_j_headre */



FILE* search_hip_j (idx_hip_j* key, hip_j* arecord)
{
   FILE* dataFile=NULL;
   long recNum;
   
   recNum= find_idx_hip_j (key);
   if (recNum >= 0)
   {
	dataFile=jump_hip_j(recNum);
	if (read_hip_j (dataFile,arecord)==0)
	   return dataFile;
	else
	   return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_hip_j */

