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

int read_hipjtran (FILE* fp, hipjtran* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hipjtran - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[hipjtran_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hipjtran NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hipjtran_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->JT1);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JT2);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JT3);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JT4);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JT5);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JT6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JT7);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JT8);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JT9);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JT10);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JT11);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JT12);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JT13);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JT14);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JT15);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JT16);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JT17);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->JT18);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->JT19);
   return 0;
} /* End of read_hipjtran */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->JT1 );
	printf("JT1  : %s             Target position index (1 to NP) for this record, IP \n",outputStr);
	FLOATasStr((char *)&outputStr,"%16.7f",&entry->JT2 );
	printf("JT2  : %s     Epoch of the transit, t, in years from J1991.25(TT) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JT3 );
	printf("JT3  : %s             Spatial frequency fx (rad/rad) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JT4 );
	printf("JT4  : %s             Spatial frequency fy (rad/rad) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JT5 );
	printf("JT5  : %s             Parallax phase factor fp (rad/rad) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->JT6 );
	printf("JT6  : %s         Natural logarithm of b1, ln b1 \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->JT7 );
	printf("JT7  : %s        Normalised signal parameter, b2/b1 \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->JT8 );
	printf("JT8  : %s        Normalised signal parameter, b3/b1 \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->JT9 );
	printf("JT9  : %s        Normalised signal parameter, b4/b1 \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->JT10);
	printf("JT10 : %s        Normalised signal parameter, b5/b1 \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->JT11);
	printf("JT11 : %s          Natural logarithm of sigma 1, lnsigma 1 \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->JT12);
	printf("JT12 : %s          Natural logarithm of sigma 2, lnsigma 2 \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->JT13);
	printf("JT13 : %s          Natural logarithm of sigma 3, lnsigma 3 \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->JT14);
	printf("JT14 : %s          Natural logarithm of sigma 4, lnsigma 4 \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->JT15);
	printf("JT15 : %s          Natural logarithm of sigma 5, lnsigma 5 \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->JT16);
	printf("JT16 : %s          Colour correction factor, s1 (mag^-1) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->JT17);
	printf("JT17 : %s          Colour correction factor, s2 (mag^-1) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->JT18);
	printf("JT18 : %s           Additional attitude noise, sigma att (mas) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JT19);
	printf("JT19 : %s             Flag for computed (0) or assumed (1) standard errors \n",outputStr);
   return (0);
} /* End of print_hipjtran */

int print_hipjtran_cols (hipjtran* 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_hipjtran_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%1d",&entry->JT1);
	printf("%s",outputStr);
	FLOATasStr((char *)&outputStr,"%10.7f",&entry->JT2);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JT3);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JT4);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->JT5);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->JT6);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->JT7);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->JT8);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->JT9);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->JT10);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->JT11);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->JT12);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->JT13);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->JT14);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->JT15);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%4.2f",&entry->JT16);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%4.2f",&entry->JT17);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%4.1f",&entry->JT18);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->JT19);
	printf("|%s",outputStr);
	printf("\r\n");
	return (0);
} /* End of print_hipjtran_cols */

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

	printf("1|JT2       |JT3     |JT4     |JT5     |JT6   |JT7    |JT8    |JT9    |JT10   |JT11 |JT12 |JT13 |JT14 |JT15 |JT16|JT17|JT18|9\n");
   return (0);
} /* End of print_hipjtran_headre */


/*  array routines  */ 
int read_array_hipjtran (FILE* fp,int no_entries, array_hipjtran* 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_hipjtran (fp,&array->data[i]) == -1) 
	   break;
	
	if (array_hipjtran_size == i) 
	{
	    fprintf(stderr," array_hipjtran not big enough - all elements not loaded\n");
	    break;
	}
   }
   array->no_entries = i;
   return 0;
} /* End of read_array_hipjtran */

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

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

