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


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


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

	INTasStr((char *)&outputStr,"%8d",&entry->IR1  );
	printf("IR1   : %s             Orbit number \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->IR2  );
	printf("IR2   : %s        FAST orbit mean epoch (year relative to J1991.25)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%17.8f",&entry->IR3  );
	printf("IR3   : %s    Right ascension of the FAST great-circle pole (deg)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%17.8f",&entry->IR4  );
	printf("IR4   : %s    Declination of the FAST great-circle pole (deg)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->IR5  );
	printf("IR5   : %s        NDAC orbit mean epoch (year relative to J1991.25)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%17.8f",&entry->IR6  );
	printf("IR6   : %s    Right ascension of the NDAC great-circle pole (deg)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%17.8f",&entry->IR7  );
	printf("IR7   : %s    Declination of the NDAC great-circle pole (deg)\n",outputStr);
   return (0);
} /* End of print_hip_rgc */

int print_hip_rgc_cols (hip_rgc* 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_rgc_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%4d",&entry->IR1 );
	printf("%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->IR2 );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%12.8f",&entry->IR3 );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%12.8f",&entry->IR4 );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->IR5 );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%12.8f",&entry->IR6 );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%12.8f",&entry->IR7 );
	printf("|%s",outputStr);
	printf("\r\n");
	return (0);
} /* End of print_hip_rgc_cols */

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

	printf("IR1 |IR2    |IR3         |IR4         |IR5    |IR6         |IR7         \n");
   return (0);
} /* End of print_hip_rgc_headre */



int load_hip_rgc (char* ifile,hip_rgc* index) 
{
/* Routine to load an index from a file. The structure for the index must
 * already exist as must the file of course
 */
   hip_rgc idx;
   FILE *fp;
   int i;

   if ( (fp=fopen(ifile,"r"))==NULL)
   {
	fprintf(stderr,"load_hip_rgc unable to open %s \n",ifile);
	return (-1);
   }
   if (index == NULL) 
   {
      fprintf (stderr,"load_hip_rgc NULL pointer for index\n");
      return (-1);
   }
   /*printf("Loading index hip_rgc from %s.. ",ifile);*/
   for (i=0;i<hip_rgc_entries;i++)
   {
	if ( (getnext_hip_rgc (fp,&index[i])) != 0)
   	{
	   fprintf(stderr,"load_hip_rgc problem with entry %d \n",i);
	   break;
	}
   /*     if  ( (i % 5000) == 0) printf("#");*/
   }
   if ( i < hip_rgc_entries) 
   {
	fprintf(stderr,"Premature end of index file \n");
   	return -1;
   }
   /* printf(" done \n"); */
   return 0;
} /* End of load_hip_rgc*/

int getnext_hip_rgc (FILE* fp, hip_rgc* entry)
{
   char buffer[hip_rgc_REC_LEN + 2];
   if (fgets((char *)&buffer,hip_rgc_REC_LEN+1,fp))
   { 
	char delimiter[3]="|\r\0";
	char *token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->IR1);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IR2);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IR3);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IR4);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IR5);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IR6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IR7);
   return 0;
   }
   else
        return -1;
} /* End of getnext_hip_rgc */

hip_rgc* find_hip_rgc (INT* key)
{
   static int init=0;
   static hip_rgc index[hip_rgc_entries];
   int mid,lpos,rpos,found;
   if (init == 0)
   {
        if (load_hip_rgc("hip_rgc.dat",(hip_rgc*)&index) !=0)
             return NULL;
        init=1;
   }

   /*  Binary search */ 
   rpos = key->value; 
   lpos = 0;
   if (rpos > hip_rgc_entries) rpos = hip_rgc_entries;
   found = 0;
   while (!found && !(lpos==mid && rpos==mid || lpos > rpos))
   {
	mid =  (rpos + lpos) /2;
        found = index[mid].IR1.value == key->value;
     	if (key->value > index[mid].IR1.value )
	   lpos = mid+1;
	else
	   if (key->value < index[mid].IR1.value)
	      rpos = mid;
	/*printf (" %d,%d Betweeen %d,%d and %d,%d Key = %d\n",mid,index[mid].IR1.value,lpos,index[lpos].IR1.value,rpos,index[rpos].IR1.value,key->value);*/
   }
   if (found)
	return &index[mid];
   else
   {
      return NULL;
   }
   
} /* End of find_hip_rgc */


