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

int read_solar_ha (FILE* fp, solar_ha* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * solar_ha - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[solar_ha_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_solar_ha NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,solar_ha_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->SHA1);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->SHA2);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->SHA3);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->SHA4);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->SHA5);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->SHA6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->SHA7);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->SHA8);
   return 0;
} /* End of read_solar_ha */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->SHA1 );
	printf("SHA1  : %s             Object number \n",outputStr);
	FLOATasStr((char *)&outputStr,"%16.7f",&entry->SHA2 );
	printf("SHA2  : %s     Reference point RA alpha_0 (degrees)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%16.7f",&entry->SHA3 );
	printf("SHA3  : %s     Reference point Dec delta_0 (degrees)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%16.7f",&entry->SHA4 );
	printf("SHA4  : %s     Observation epoch, JD(TT)-2440000.0 (days)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->SHA5 );
	printf("SHA5  : %s          Light time delay satellite-Earth Deltatau (seconds)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->SHA6 );
	printf("SHA6  : %s         Position angle theta (degrees) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->SHA7 );
	printf("SHA7  : %s          sigma_v* (mas)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->SHA8 );
	printf("SHA8  : %s             NDAC (1) or FAST (2) flag \n",outputStr);
   return (0);
} /* End of print_solar_ha */

int print_solar_ha_cols (solar_ha* 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_ha_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%3d",&entry->SHA1);
	printf("%s",outputStr);
	FLOATasStr((char *)&outputStr,"%11.7f",&entry->SHA2);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%11.7f",&entry->SHA3);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%13.7f",&entry->SHA4);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->SHA5);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.3f",&entry->SHA6);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->SHA7);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->SHA8);
	printf("|%s",outputStr);
	printf("\r\n");
	return (0);
} /* End of print_solar_ha_cols */

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

	printf("HA1|SHA2       |SHA3       |SHA4         |SHA5 |SHA6   |SHA7  |8\n");
   return (0);
} /* End of print_solar_ha_headre */


/*  array routines  */ 
int read_array_solar_ha (int no_entries, array_solar_ha* 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_ha(offset->value); 
   array->no_entries=0;
   for (i=0; i<no_entries; i++)
   {
	if (read_solar_ha (fp,&array->data[i]) == -1) 
	   break;
	
	if (array_solar_ha_size == i) 
	{
	    fprintf(stderr," array_solar_ha not big enough - all elements not loaded\n");
	    break;
	}
   }
   array->no_entries = i;
   return 0;
} /* End of read_array_solar_ha */

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

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

