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

int read_hip_va_1 (FILE* fp, hip_va_1* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hip_va_1 - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[hip_va_1_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hip_va_1 NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hip_va_1_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->P1);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->P2,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->P3,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->P4,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->P5,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->P6,token);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->P7);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->P8,token);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->P9);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->P10);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->P11);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->P12);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->P13);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->P14);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->P15,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->P16,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->P17,token);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->P18);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->P19);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->P20);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->P21);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->P22,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->P23,token);
	search_hp_auth(&entry->P1,&entry->AUTHREF);
	if (entry->P16[0]=='D' || entry->P16[0]=='W' || 
	    entry->P16[0]=='X' || entry->P16[0]=='Z' )
	   search_hd_notes(&entry->P1,&entry->HDNOTES);
	else
	   entry->HDNOTES.no_entries=0;
	if (entry->P16[0]=='G' || entry->P16[0]=='Y' || 
	    entry->P16[0]=='X' || entry->P16[0]=='Z' )
	   search_hg_notes(&entry->P1,&entry->HGNOTES);
	else
	   entry->HGNOTES.no_entries=0;
	if (entry->P16[0]=='P' || entry->P16[0]=='W' || 
	    entry->P16[0]=='Y' || entry->P16[0]=='Z' )
	   search_hp_notes(&entry->P1,&entry->HPNOTES);
	else
	   entry->HPNOTES.no_entries=0;
   return 0;
} /* End of read_hip_va_1 */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->P1 );
	printf("P1  : %s             Identifier (HIP)\n",outputStr);
	printf("P2  : %1.1s                    Flag if new variable\n",entry->P2 );
	printf("P3  : %12.12s         Spectral type\n",entry->P3 );
	printf("P4  : %1.1s                    Variability type (1-letter)\n",entry->P4 );
	printf("P5  : %6.6s               Variability type (6-letter)\n",entry->P5 );
	printf("P6  : %1.1s                    Flag if newly classified by Hipparcos\n",entry->P6 );
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->P7 );
	printf("P7  : %s         Magnitude at max from curve fitting\n",outputStr);
	printf("P8  : %1.1s                    Limit flag (>)\n",entry->P8 );
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->P9 );
	printf("P9  : %s         Magnitude at min from curve fitting\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->P10);
	printf("P10 : %s           log_10(sigma_A/A)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%16.7f",&entry->P11);
	printf("P11 : %s     Mean period (days)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->P12);
	printf("P12 : %s           log_10sigma_P\n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->P13);
	printf("P13 : %s        Epoch from solution (days)\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->P14);
	printf("P14 : %s             Precision flag\n",outputStr);
	printf("P15 : %1.1s                    Annex flag\n",entry->P15);
	printf("P16 : %1.1s                    Note flag\n",entry->P16);
	printf("P17 : %12.12s         Variable star name^1\n",entry->P17);
	FLOATasStr((char *)&outputStr,"%14.5f",&entry->P18);
	printf("P18 : %s       Period from literature (days)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->P19);
	printf("P19 : %s          Epoch from literature (Julian Date)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->P20);
	printf("P20 : %s          Magnitude at max from literature\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->P21);
	printf("P21 : %s          Magnitude at min from literature\n",outputStr);
	printf("P22 : %1.1s                    Photometric band\n",entry->P22);
	printf("P23 : %1.1s                    Reference flag\n",entry->P23);
	if (decode == ARRAYVERBOSE || 
	    decode ==(ARRAYVERBOSE+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE) || 
	    decode ==(ARRAYVERBOSE+EXTRACTCOEFF) ||
	    decode ==(PRINTSUBRECS+ARRAYVERBOSE+EXTRACTCOEFF) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+EXTRACTCOEFF) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+PRINTSUBRECS) || 
	    decode == DOALL )
	   print_array_hp_auth(&entry->AUTHREF,decode);
	else
	{
	   printf("AUTHREF\n");
	   print_array_hp_auth_cols(&entry->AUTHREF,decode);
	}
	if (decode == ARRAYVERBOSE || 
	    decode ==(ARRAYVERBOSE+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE) || 
	    decode ==(ARRAYVERBOSE+EXTRACTCOEFF) ||
	    decode ==(PRINTSUBRECS+ARRAYVERBOSE+EXTRACTCOEFF) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+EXTRACTCOEFF) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+PRINTSUBRECS) || 
	    decode == DOALL )
	   print_array_hg_notes(&entry->HGNOTES,decode);
	else
	{
	   printf("HGNOTES\n");
	   print_array_hg_notes_cols(&entry->HGNOTES,decode);
	}
	if (decode == ARRAYVERBOSE || 
	    decode ==(ARRAYVERBOSE+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE) || 
	    decode ==(ARRAYVERBOSE+EXTRACTCOEFF) ||
	    decode ==(PRINTSUBRECS+ARRAYVERBOSE+EXTRACTCOEFF) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+EXTRACTCOEFF) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+PRINTSUBRECS) || 
	    decode == DOALL )
	   print_array_hd_notes(&entry->HDNOTES,decode);
	else
	{
	   printf("HDNOTES\n");
	   print_array_hd_notes_cols(&entry->HDNOTES,decode);
	}
	if (decode == ARRAYVERBOSE || 
	    decode ==(ARRAYVERBOSE+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE) || 
	    decode ==(ARRAYVERBOSE+EXTRACTCOEFF) ||
	    decode ==(PRINTSUBRECS+ARRAYVERBOSE+EXTRACTCOEFF) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+EXTRACTCOEFF) || 
	    decode ==(DECODEBITS+ARRAYVERBOSE+PRINTSUBRECS) || 
	    decode == DOALL )
	   print_array_hp_notes(&entry->HPNOTES,decode);
	else
	{
	   printf("HPNOTES\n");
	   print_array_hp_notes_cols(&entry->HPNOTES,decode);
	}
   return (0);
} /* End of print_hip_va_1 */

int print_hip_va_1_cols (hip_va_1* 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_hip_va_1_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%6d",&entry->P1);
	printf("%s",outputStr);
	printf("|%1s",entry->P2);
	printf("|%12s",entry->P3);
	printf("|%1s",entry->P4);
	printf("|%6s",entry->P5);
	printf("|%1s",entry->P6);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->P7);
	printf("|%s",outputStr);
	printf("|%1s",entry->P8);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->P9);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.1f",&entry->P10);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%12.7f",&entry->P11);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.1f",&entry->P12);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%9.4f",&entry->P13);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%1d",&entry->P14);
	printf("|%s",outputStr);
	printf("|%1s",entry->P15);
	printf("|%1s",entry->P16);
	printf("|%12s",entry->P17);
	FLOATasStr((char *)&outputStr,"%10.5f",&entry->P18);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%10.2f",&entry->P19);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->P20);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->P21);
	printf("|%s",outputStr);
	printf("|%1s",entry->P22);
	printf("|%1s",entry->P23);
	if ((entry->AUTHREF.no_entries > 0 )&&
	    (decode == PRINTSUBRECS ||
	     decode ==(EXTRACTCOEFF+PRINTSUBRECS) || 
	     decode ==(DECODEBITS+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+PRINTSUBRECS) ||
	     decode ==(DECODEBITS+EXTRACTCOEFF+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+DECODEBITS+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+EXTRACTCOEFF+PRINTSUBRECS) || 
	     decode == DOALL ) ) 
	{
	   printf("\n");
	   print_array_hp_auth_cols(&entry->AUTHREF,decode);
	}
	if ((entry->HGNOTES.no_entries > 0 )&&
	    (decode == PRINTSUBRECS ||
	     decode ==(EXTRACTCOEFF+PRINTSUBRECS) || 
	     decode ==(DECODEBITS+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+PRINTSUBRECS) ||
	     decode ==(DECODEBITS+EXTRACTCOEFF+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+DECODEBITS+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+EXTRACTCOEFF+PRINTSUBRECS) || 
	     decode == DOALL ) ) 
	{
	   printf("\n");
	   print_array_hg_notes_cols(&entry->HGNOTES,decode);
	}
	if ((entry->HDNOTES.no_entries > 0 )&&
	    (decode == PRINTSUBRECS ||
	     decode ==(EXTRACTCOEFF+PRINTSUBRECS) || 
	     decode ==(DECODEBITS+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+PRINTSUBRECS) ||
	     decode ==(DECODEBITS+EXTRACTCOEFF+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+DECODEBITS+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+EXTRACTCOEFF+PRINTSUBRECS) || 
	     decode == DOALL ) ) 
	{
	   printf("\n");
	   print_array_hd_notes_cols(&entry->HDNOTES,decode);
	}
	if ((entry->HPNOTES.no_entries > 0 )&&
	    (decode == PRINTSUBRECS ||
	     decode ==(EXTRACTCOEFF+PRINTSUBRECS) || 
	     decode ==(DECODEBITS+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+PRINTSUBRECS) ||
	     decode ==(DECODEBITS+EXTRACTCOEFF+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+DECODEBITS+PRINTSUBRECS) ||
	     decode ==(ARRAYVERBOSE+EXTRACTCOEFF+PRINTSUBRECS) || 
	     decode == DOALL ) ) 
	{
	   printf("\n");
	   print_array_hp_notes_cols(&entry->HPNOTES,decode);
	}
	printf("\r\n");
	return (0);
} /* End of print_hip_va_1_cols */

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

	printf("P1    |2|P3          |4|P5    |6|P7    |8|P9    |P10   |P11         |P12   |P13      |4|5|6|P17         |P18       |P19       |P20  |P21  |2|3\n");
   return (0);
} /* End of print_hip_va_1_headre */



FILE* search_hip_va_1 (INT* key, hip_va_1* arecord)
{
   FILE* dataFile=NULL;
   idx_hip_va* recNum;
   
   recNum= find_idx_hip_va (key);
   if (recNum != NULL && (recNum->ANNEX.value == 1))
   {
	dataFile=jump_hip_va_1(recNum->RECNO.value);
	if (read_hip_va_1 (dataFile,arecord)==0)
	   return dataFile;
	else
	   return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_hip_va */

