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

int read_hip_dm_g (FILE* fp, hip_dm_g* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hip_dm_g - 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_g_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hip_dm_g NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hip_dm_g_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->DG1);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DG2);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DG3);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DG4);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DG5);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DG6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DG7);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DG8);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DG9);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DG10);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DG11);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->DG12,token);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->DGM1);
	token=strtok(NULL,delimiter);
	strAsNONLINEAR(token,&entry->DGM2);
	if (entry->DG12[0]=='D' || entry->DG12[0]=='W' || 
	    entry->DG12[0]=='X' || entry->DG12[0]=='Z' )
	   search_hd_notes(&entry->DG1,&entry->HDNOTES);
	else
	   entry->HDNOTES.no_entries=0;
	if (entry->DG12[0]=='G' || entry->DG12[0]=='Y' || 
	    entry->DG12[0]=='X' || entry->DG12[0]=='Z' )
	   search_hg_notes(&entry->DG1,&entry->HGNOTES);
	else
	   entry->HGNOTES.no_entries=0;
	if (entry->DG12[0]=='P' || entry->DG12[0]=='W' || 
	    entry->DG12[0]=='Y' || entry->DG12[0]=='Z' )
	   search_hp_notes(&entry->DG1,&entry->HPNOTES);
	else
	   entry->HPNOTES.no_entries=0;
   return 0;
} /* End of read_hip_dm_g */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->DG1 );
	printf("DG1  : %s             Hipparcos Catalogue (HIP) identifier \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DG2 );
	printf("DG2  : %s          g_alpha* = dmu_alpha*/dt at J1991.25 (mas/yr^2) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DG3 );
	printf("DG3  : %s          g_delta = dmu_delta/dt at J1991.25 (mas/yr^2) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DG4 );
	printf("DG4  : %s          Standard error of g_alpha*, sigma_g_alpha* (mas/yr^2) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DG5 );
	printf("DG5  : %s          Standard error of g_delta, sigma_g_delta (mas/yr^2) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DG6 );
	printf("DG6  : %s          Significance of the g terms, F_g \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DG7 );
	printf("DG7  : %s          dot g_alpha* = d^2mu_alpha*/dt^2 at J1991.25 (mas/yr^3) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DG8 );
	printf("DG8  : %s          dot g_delta = d^2mu_delta/dt^2 at J1991.25 (mas/yr^3) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DG9 );
	printf("DG9  : %s          Standard error of dot g_alpha*, sigma_dot g_alpha* (mas/yr^3) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DG10);
	printf("DG10 : %s          Standard error of dot g_delta, sigma_dot g_delta (mas/yr^3) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DG11);
	printf("DG11 : %s          Significance of the dot g terms, F_dot g \n",outputStr);
	printf("DG12 : %1.1s                    Flag indicating a note at the end of relevant volume \n",entry->DG12);
	INTasStr((char *)&outputStr,"%8d",&entry->DGM1);
	printf("DGM1 : %s             Number of astrometric parameters, n=7 or 9 \n",outputStr);
	printf("DGM2: (Up to) 36 correlation coefficients in a non-linear coding \n");
	print_NONLINEAR_cols(&entry->DGM2);
	printf("\n");
	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_g */

int print_hip_dm_g_cols (hip_dm_g* 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_g_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%6d",&entry->DG1);
	printf("%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.2f",&entry->DG2);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.2f",&entry->DG3);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.2f",&entry->DG4);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.2f",&entry->DG5);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->DG6);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.2f",&entry->DG7);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.2f",&entry->DG8);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.2f",&entry->DG9);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.2f",&entry->DG10);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->DG11);
	printf("|%s",outputStr);
	printf("|%1s",entry->DG12);
	INTasStr((char *)&outputStr,"%1d",&entry->DGM1);
	printf("|%s",outputStr);
	printf("|");
	if (decode == EXTRACTCOEFF || 
	    decode ==(EXTRACTCOEFF+PRINTSUBRECS) || 
	    decode ==(DECODEBITS+EXTRACTCOEFF) ||
	    decode ==(ARRAYVERBOSE+EXTRACTCOEFF) ||
	    decode ==(PRINTSUBRECS+DECODEBITS+EXTRACTCOEFF) ||
	    decode ==(ARRAYVERBOSE+DECODEBITS+EXTRACTCOEFF) ||
	    decode ==(ARRAYVERBOSE+EXTRACTCOEFF+PRINTSUBRECS) || 
	    decode == DOALL ) 
	{
	   print_NONLINEAR_cols(&entry->DGM2);
	}
	else
	{
	   NONLINEARasStr((char *)&outputStr,"%107s",&entry->DGM2);
	   printf("%s",outputStr);
	}
	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_g_cols */

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

	printf("DG1   |DG2    |DG3    |DG4    |DG5    |DG6  |DG7    |DG8    |DG9    |DG10   |DG11 |2|1|DGM2                                                                                                       \n");
   return (0);
} /* End of print_hip_dm_g_headre */



FILE* search_hip_dm_g (INT* key, hip_dm_g* arecord)
{
   FILE* dataFile=NULL;
   idx_hip_dm* recNum;
   
   recNum= find_idx_hip_dm (key);
   if (recNum != NULL && (recNum->hdm_idx2[0] == 'G'))
   {
	dataFile=jump_hip_dm_g(recNum->hdm_idx3.value);
	if (read_hip_dm_g (dataFile,arecord)==0)
	   return dataFile;
	else
	   return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_hip_dm */

