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

int read_hipabsc (FILE* fp, hipabsc* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hipabsc - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[hipabsc_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hipabsc NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hipabsc_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->IA1);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->IA2,token);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IA3);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IA4);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IA5);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IA6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IA7);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IA8);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IA9);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->IA10);
	entry->ORBIT=find_hip_rgc(&entry->IA1);
   return 0;
} /* End of read_hipabsc */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->IA1   );
	printf("IA1    : %s             Orbit number \n",outputStr);
	printf("IA2    : %1.1s                    `F' or `f' if FAST data; `N' or `n' if NDAC data\n",entry->IA2   );
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->IA3   );
	printf("IA3    : %s        Abscissa partial derivative with respect to alpha* \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->IA4   );
	printf("IA4    : %s        Abscissa partial derivative with respect to delta \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->IA5   );
	printf("IA5    : %s        Abscissa partial derivative with respect to pi \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->IA6   );
	printf("IA6    : %s        Abscissa partial derivative with respect to mu_alpha*\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->IA7   );
	printf("IA7    : %s        Abscissa partial derivative with respect to mu_delta\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->IA8   );
	printf("IA8    : %s          Abscissa residual wrt reference astrometric parameters (mas)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->IA9   );
	printf("IA9    : %s          Standard error of the abscissa (mas) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->IA10  );
	printf("IA10   : %s         Correlation coefficient between FAST and NDAC abscissae\n",outputStr);
	printf("ORBIT                         The Reference Great Circle for this Abscissa\n");
	print_hip_rgc_cols(entry->ORBIT ,decode);
   return (0);
} /* End of print_hipabsc */

int print_hipabsc_cols (hipabsc* 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_hipabsc_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%4d",&entry->IA1  );
	printf("%s",outputStr);
	printf("|%1s",entry->IA2  );
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->IA3  );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->IA4  );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->IA5  );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->IA6  );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->IA7  );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%8.2f",&entry->IA8  );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.2f",&entry->IA9  );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.3f",&entry->IA10 );
	printf("|%s",outputStr);
	if ((entry->ORBIT)&&
	    (decode == PRINTSUBRECS || decode == DOALL))  
	{
	   printf("\n");
	   print_hip_rgc_cols(entry->ORBIT,decode);
	}
	printf("\r\n");
	return (0);
} /* End of print_hipabsc_cols */

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

	printf("A1  | |IA3    |IA4    |IA5    |IA6    |IA7    |IA8     |IA9    |IA10 \n");
   return (0);
} /* End of print_hipabsc_headre */


/*  array routines  */ 
int read_array_hipabsc (FILE* fp,int no_entries, array_hipabsc* array) 
{
/* Read no_entries entries from the file into the array provided
   use read routine to achive this.
 */
   int i;
   
   array->no_entries=0;
   for (i=0; i<no_entries; i++)
   {
	if (read_hipabsc (fp,&array->data[i]) == -1) 
	   break;
	
	if (array_hipabsc_size == i) 
	{
	    fprintf(stderr," array_hipabsc not big enough - all elements not loaded\n");
	    break;
	}
   }
   array->no_entries = i;
   return 0;
} /* End of read_array_hipabsc */

int print_array_hipabsc_cols (array_hipabsc* array, int decode) 
{
   int i;
   if (array == NULL) 
   {
      fprintf (stderr,"print_array_hipabsc NULL pointer for entry\n");
      return (-1);
   }
/*   printf("Array array_hipabsc contains %d entries :\n",array->no_entries);*/
      if (array->no_entries > 0)
      {
         print_hipabsc_header();
      }
      for (i=0; i<array->no_entries; i++)
      {
	 print_hipabsc_cols (&array->data[i],decode) ;
      }
      return (0);
} /* End of print_array_hipabsc_cols */

int print_array_hipabsc (array_hipabsc* array, int decode) 
{
   int i;
   if (array == NULL) 
   {
      fprintf (stderr,"print_array_hipabsc NULL pointer for entry\n");
      return (-1);
   }
/*   printf("Array array_hipabsc contains %d entries :\n",array->no_entries);*/
      for (i=0; i<array->no_entries; i++)
      {
	 print_hipabsc (&array->data[i],decode) ;
      }
      return (0);
} /* End of print_array_hipabsc */

