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

int read_hip_dm_x (FILE* fp, hip_dm_x* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hip_dm_x - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[hip_dm_x_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hip_dm_x NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hip_dm_x_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->DX1);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DX2);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DX3);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->DX4,token);
	if (entry->DX4[0]=='D' || entry->DX4[0]=='W' || 
	    entry->DX4[0]=='X' || entry->DX4[0]=='Z' )
	   search_hd_notes(&entry->DX1,&entry->HDNOTES);
	else
	   entry->HDNOTES.no_entries=0;
	if (entry->DX4[0]=='G' || entry->DX4[0]=='Y' || 
	    entry->DX4[0]=='X' || entry->DX4[0]=='Z' )
	   search_hg_notes(&entry->DX1,&entry->HGNOTES);
	else
	   entry->HGNOTES.no_entries=0;
	if (entry->DX4[0]=='P' || entry->DX4[0]=='W' || 
	    entry->DX4[0]=='Y' || entry->DX4[0]=='Z' )
	   search_hp_notes(&entry->DX1,&entry->HPNOTES);
	else
	   entry->HPNOTES.no_entries=0;
   return 0;
} /* End of read_hip_dm_x */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->DX1  );
	printf("DX1   : %s             Hipparcos Catalogue (HIP) identifier \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DX2  );
	printf("DX2   : %s          Cosmic error, epsilon (mas) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DX3  );
	printf("DX3   : %s          Standard error of epsilon, sigma_epsilon (mas) \n",outputStr);
	printf("DX4   : %1.1s                    Flag indicating a note at the end of relevant volume \n",entry->DX4  );
	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_dm_x */

int print_hip_dm_x_cols (hip_dm_x* 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_dm_x_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%6d",&entry->DX1 );
	printf("%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->DX2 );
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->DX3 );
	printf("|%s",outputStr);
	printf("|%1s",entry->DX4 );
	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_dm_x_cols */

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

	printf("DX1   |DX2   |DX3   | \n");
   return (0);
} /* End of print_hip_dm_x_headre */



FILE* search_hip_dm_x (INT* key, hip_dm_x* arecord)
{
   FILE* dataFile=NULL;
   idx_hip_dm* recNum;
   
   recNum= find_idx_hip_dm (key);
   if (recNum != NULL && (recNum->hdm_idx2[0] == 'X'))
   {
	dataFile=jump_hip_dm_x(recNum->hdm_idx3.value);
	if (read_hip_dm_x (dataFile,arecord)==0)
	   return dataFile;
	else
	   return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_hip_dm */

