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

int read_solar_hp (FILE* fp, solar_hp* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * solar_hp - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[solar_hp_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_solar_hp NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,solar_hp_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->SHP1);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->SHP2);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->SHP3);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->SHP4);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->SHP5);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->SHP6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->SHP7);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->SHP8);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->SHP9);
   return 0;
} /* End of read_solar_hp */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->SHP1 );
	printf("SHP1  : %s             Object number \n",outputStr);
	FLOATasStr((char *)&outputStr,"%14.5f",&entry->SHP2 );
	printf("SHP2  : %s       Observation epoch, JD(TT)-2440000.0\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->SHP3 );
	printf("SHP3  : %s        Hp_dc (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->SHP4 );
	printf("SHP4  : %s        sigma_Hp_dc\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->SHP5 );
	printf("SHP5  : %s        Hp_ac (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->SHP6 );
	printf("SHP6  : %s        sigma_Hp_ac\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->SHP7 );
	printf("SHP7  : %s         Distance Sun-asteroid d (AU)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->SHP8 );
	printf("SHP8  : %s         Distance satellite-asteroid Delta (AU)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->SHP9 );
	printf("SHP9  : %s          Solar phase angle alpha (degrees)\n",outputStr);
   return (0);
} /* End of print_solar_hp */

int print_solar_hp_cols (solar_hp* 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_hp_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%3d",&entry->SHP1);
	printf("%s",outputStr);
	FLOATasStr((char *)&outputStr,"%11.5f",&entry->SHP2);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->SHP3);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.4f",&entry->SHP4);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.4f",&entry->SHP5);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.4f",&entry->SHP6);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.3f",&entry->SHP7);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.3f",&entry->SHP8);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->SHP9);
	printf("|%s",outputStr);
	printf("\r\n");
	return (0);
} /* End of print_solar_hp_cols */

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

	printf("HP1|SHP2       |SHP3   |SHP4  |SHP5   |SHP6  |SHP7 |SHP8 |SHP9 \n");
   return (0);
} /* End of print_solar_hp_headre */


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

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

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

