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

char *TT13_codes[6] = {
  "magnitude not measured in Bt",
  "magnitude not measured in Vt",
  "transit rejected in astrometry and in de-censoring (due to suspected parasite)",
  "transit on the inclined slit",
  "transit in preceding field",
       NULL};
int read_tyctrans (FILE* fp, tyctrans* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * tyctrans - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[tyctrans_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_tyctrans NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,tyctrans_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsFLOAT(token,&entry->TT1);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TT2);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TT3);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TT4);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TT5);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TT6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TT7);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TT8);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TT9);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->TT10);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->TT11);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->TT12);
	token=strtok(NULL,delimiter);
	strAsBITS(token,&entry->TT13);
   return 0;
} /* End of read_tyctrans */ 

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


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

	FLOATasStr((char *)&outputStr,"%14.5f",&entry->TT1 );
	printf("TT1  : %s       BJD(TT), (barycentric) Julian Date \n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->TT2 );
	printf("TT2  : %s         BT for the transit (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->TT3 );
	printf("TT3  : %s         sigma BT (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->TT4 );
	printf("TT4  : %s          Background, bB (cts/smp)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->TT5 );
	printf("TT5  : %s         VT for the transit (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->TT6 );
	printf("TT6  : %s         sigma VT (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->TT7 );
	printf("TT7  : %s          Background, bV (cts/smp)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->TT8 );
	printf("TT8  : %s           z-coordinate, z (arcsec)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->TT9 );
	printf("TT9  : %s           Position angle, pn (degrees)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->TT10);
	printf("TT10 : %s             Residual, Delta u (mas)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->TT11);
	printf("TT11 : %s             Standard error, sigma u (mas)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->TT12);
	printf("TT12 : %s           Goodness-of-fit\n",outputStr);
	BITSasStr((char *)&outputStr,"%8d",&entry->TT13);
	printf("TT13 : %s             Transit flags \n",outputStr);
	if (decode == DECODEBITS || 
	    decode ==(DECODEBITS+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+EXTRACTCOEFF) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+EXTRACTCOEFF) ||
	    decode == DOALL )
	   showFlags(TT13_codes,&entry->TT13);
   return (0);
} /* End of print_tyctrans */

int print_tyctrans_cols (tyctrans* 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_tyctrans_cols NULL pointer for entry\n");
      return (-1);
   }

	FLOATasStr((char *)&outputStr,"%10.5f",&entry->TT1);
	printf("%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->TT2);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.3f",&entry->TT3);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->TT4);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->TT5);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.3f",&entry->TT6);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->TT7);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.1f",&entry->TT8);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.1f",&entry->TT9);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%5d",&entry->TT10);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%4d",&entry->TT11);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.1f",&entry->TT12);
	printf("|%s",outputStr);
	BITSasStr((char *)&outputStr,"%2d",&entry->TT13);
	printf("|%s",outputStr);
	if (decode == DECODEBITS || 
	    decode ==(DECODEBITS+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+EXTRACTCOEFF) ||
	    decode ==(DECODEBITS+ARRAYVERBOSE) ||
	    decode ==(PRINTSUBRECS+DECODEBITS+EXTRACTCOEFF) ||
	    decode ==(ARRAYVERBOSE+DECODEBITS+EXTRACTCOEFF) ||
	    decode ==(ARRAYVERBOSE+DECODEBITS+PRINTSUBRECS) || 
	    decode == DOALL ) 
	{
	   showFlags(TT13_codes,&entry->TT13);
	}
	printf("\r\n");
	return (0);
} /* End of print_tyctrans_cols */

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

	printf("TT1       |TT2   |TT3  |TT4   |TT5   |TT6  |TT7   |TT8    |TT9  |TT10 |TT11|TT12 |13\n");
   return (0);
} /* End of print_tyctrans_headre */


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

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

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

