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

int read_solar_t (FILE* fp, solar_t* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * solar_t - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[solar_t_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_solar_t NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,solar_t_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->ST1);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->ST2);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->ST3);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->ST4);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->ST5);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->ST6);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->ST7);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->ST8);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->ST9);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->ST10);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->ST11);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->ST12);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->ST13);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->ST14);
   return 0;
} /* End of read_solar_t */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->ST1 );
	printf("ST1  : %s             Object number \n",outputStr);
	FLOATasStr((char *)&outputStr,"%16.7f",&entry->ST2 );
	printf("ST2  : %s     Observation epoch, JD(TT)-2440000.0\n",outputStr);
	FLOATasStr((char *)&outputStr,"%16.7f",&entry->ST3 );
	printf("ST3  : %s     alpha (degrees)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%16.7f",&entry->ST4 );
	printf("ST4  : %s     delta (degrees)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->ST5 );
	printf("ST5  : %s          B_T (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->ST6 );
	printf("ST6  : %s          V_T (mag)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->ST7 );
	printf("ST7  : %s             Transit flag\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->ST8 );
	printf("ST8  : %s           sigma_alpha* (mas)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->ST9 );
	printf("ST9  : %s           sigma_delta (mas)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->ST10);
	printf("ST10 : %s          Correlation: alpha*/delta\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->ST11);
	printf("ST11 : %s          Position angle theta (degrees) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->ST12);
	printf("ST12 : %s             sgn(z)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->ST13);
	printf("ST13 : %s           sigma_1 (mas)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->ST14);
	printf("ST14 : %s           sigma_2 (mas)\n",outputStr);
   return (0);
} /* End of print_solar_t */

int print_solar_t_cols (solar_t* 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_solar_t_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%3d",&entry->ST1);
	printf("%s",outputStr);
	FLOATasStr((char *)&outputStr,"%13.7f",&entry->ST2);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%11.7f",&entry->ST3);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%11.7f",&entry->ST4);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->ST5);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->ST6);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->ST7);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.1f",&entry->ST8);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.1f",&entry->ST9);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->ST10);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->ST11);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%2d",&entry->ST12);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.1f",&entry->ST13);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.1f",&entry->ST14);
	printf("|%s",outputStr);
	printf("\r\n");
	return (0);
} /* End of print_solar_t_cols */

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

	printf("ST1|ST2          |ST3        |ST4        |ST5  |ST6  |7|ST8  |ST9  |ST10 |ST11  |12|ST13 |ST14 \n");
   return (0);
} /* End of print_solar_t_headre */


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

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

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

