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

int read_hip_va_2 (FILE* fp, hip_va_2* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hip_va_2 - 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_2_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hip_va_2 NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hip_va_2_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->U1);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->U2,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->U3,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->U4,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->U5,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->U6,token);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->U7);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->U8,token);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->U9);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->U10);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->U11);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->U12);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->U13,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->U14,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->U15,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->U16,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->U17,token);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->U18);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->U19);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->U20);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->U21);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->U22,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->U23,token);
	search_hp_auth(&entry->U1,&entry->AUTHREF);
	if (entry->U16[0]=='D' || entry->U16[0]=='W' || 
	    entry->U16[0]=='X' || entry->U16[0]=='Z' )
	   search_hd_notes(&entry->U1,&entry->HDNOTES);
	else
	   entry->HDNOTES.no_entries=0;
	if (entry->U16[0]=='G' || entry->U16[0]=='Y' || 
	    entry->U16[0]=='X' || entry->U16[0]=='Z' )
	   search_hg_notes(&entry->U1,&entry->HGNOTES);
	else
	   entry->HGNOTES.no_entries=0;
	if (entry->U16[0]=='P' || entry->U16[0]=='W' || 
	    entry->U16[0]=='Y' || entry->U16[0]=='Z' )
	   search_hp_notes(&entry->U1,&entry->HPNOTES);
	else
	   entry->HPNOTES.no_entries=0;
   return 0;
} /* End of read_hip_va_2 */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->U1 );
	printf("U1  : %s             Identifier (HIP)\n",outputStr);
	printf("U2  : %1.1s                    Flag if new variable\n",entry->U2 );
	printf("U3  : %12.12s         Spectral type\n",entry->U3 );
	printf("U4  : %1.1s                    Variability type (1-letter)\n",entry->U4 );
	printf("U5  : %6.6s               Variability type (6-letter)\n",entry->U5 );
	printf("U6  : %1.1s                    Flag if newly classified by Hipparcos\n",entry->U6 );
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->U7 );
	printf("U7  : %s         Magnitude at max curve fitting if significant\n",outputStr);
	printf("U8  : %1.1s                    Limit flag (>)\n",entry->U8 );
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->U9 );
	printf("U9  : %s         Magnitude at min curve fitting if significant\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->U10);
	printf("U10 : %s         Median Hp (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->U11);
	printf("U11 : %s         Intrinsic variability amplitude, A (mag)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->U12);
	printf("U12 : %s         sigma_A\n",outputStr);
	printf("U13 : %9.9s            Blank for unsolved variables\n",entry->U13);
	printf("U14 : %1.1s                    Blank for unsolved variables\n",entry->U14);
	printf("U15 : %1.1s                    Annex flag\n",entry->U15);
	printf("U16 : %1.1s                    Note flag\n",entry->U16);
	printf("U17 : %12.12s         Variable star name\n",entry->U17);
	FLOATasStr((char *)&outputStr,"%14.5f",&entry->U18);
	printf("U18 : %s       Period from literature (days)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->U19);
	printf("U19 : %s          Epoch from literature (Julian Date)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->U20);
	printf("U20 : %s          Magnitude at max from literature\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->U21);
	printf("U21 : %s          Magnitude at min from literature\n",outputStr);
	printf("U22 : %1.1s                    Photometric band\n",entry->U22);
	printf("U23 : %1.1s                    Reference flag\n",entry->U23);
	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_2 */

int print_hip_va_2_cols (hip_va_2* 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_2_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%6d",&entry->U1);
	printf("%s",outputStr);
	printf("|%1s",entry->U2);
	printf("|%12s",entry->U3);
	printf("|%1s",entry->U4);
	printf("|%6s",entry->U5);
	printf("|%1s",entry->U6);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->U7);
	printf("|%s",outputStr);
	printf("|%1s",entry->U8);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->U9);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->U10);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%12.3f",&entry->U11);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.3f",&entry->U12);
	printf("|%s",outputStr);
	printf("|%9s",entry->U13);
	printf("|%1s",entry->U14);
	printf("|%1s",entry->U15);
	printf("|%1s",entry->U16);
	printf("|%12s",entry->U17);
	FLOATasStr((char *)&outputStr,"%10.5f",&entry->U18);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%10.2f",&entry->U19);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->U20);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->U21);
	printf("|%s",outputStr);
	printf("|%1s",entry->U22);
	printf("|%1s",entry->U23);
	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_2_cols */

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

	printf("U1    |2|U3          |4|U5    |6|U7    |8|U9    |U10   |U11         |U12   |U13      |4|5|6|U17         |U18       |U19       |U20  |U21  |2|3\n");
   return (0);
} /* End of print_hip_va_2_headre */



FILE* search_hip_va_2 (INT* key, hip_va_2* arecord)
{
   FILE* dataFile=NULL;
   idx_hip_va* recNum;
   
   recNum= find_idx_hip_va (key);
   if (recNum != NULL && (recNum->ANNEX.value == 2))
   {
	dataFile=jump_hip_va_2(recNum->RECNO.value);
	if (read_hip_va_2 (dataFile,arecord)==0)
	   return dataFile;
	else
	   return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_hip_va */

