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

char *HT4_codes[10] = {
  "NDAC data only",
  "FAST data only",
  "(not used)",
  "Very high background estimate (threshold 70~Hz)",
  "Possible interfering object in either field of view",
  "FAST quality flag set",
  "Perturbed for other identified reasons",
  "Observation during Sun-pointing mode",
  "Significant difference between FAST and NDAC data",
       NULL};
int read_hiptrans (FILE* fp, hiptrans* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hiptrans - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[hiptrans_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hiptrans NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hiptrans_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsFLOAT(token,&entry->HT1);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HT2);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->HT3);
	token=strtok(NULL,delimiter);
	strAsBITS(token,&entry->HT4);
   return 0;
} /* End of read_hiptrans */ 

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


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

	FLOATasStr((char *)&outputStr,"%14.5f",&entry->HT1 );
	printf("HT1  : %s       BJD(TT), (barycentric) Julian Date \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->HT2 );
	printf("HT2  : %s        Hp for the transit (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->HT3 );
	printf("HT3  : %s         sigma Hp (mag)\n",outputStr);
	BITSasStr((char *)&outputStr,"%8d",&entry->HT4 );
	printf("HT4  : %s             Quality flag \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(HT4_codes,&entry->HT4 );
   return (0);
} /* End of print_hiptrans */

int print_hiptrans_cols (hiptrans* 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_hiptrans_cols NULL pointer for entry\n");
      return (-1);
   }

	FLOATasStr((char *)&outputStr,"%10.5f",&entry->HT1);
	printf("%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->HT2);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.3f",&entry->HT3);
	printf("|%s",outputStr);
	BITSasStr((char *)&outputStr,"%3d",&entry->HT4);
	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(HT4_codes,&entry->HT4);
	}
	printf("\r\n");
	return (0);
} /* End of print_hiptrans_cols */

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

	printf("HT1       |HT2    |HT3  |HT4\n");
   return (0);
} /* End of print_hiptrans_headre */


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

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

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

