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

int read_hip_dm_o (FILE* fp, hip_dm_o* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hip_dm_o - 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_o_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hip_dm_o NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hip_dm_o_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->DO1);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DO2);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DO3);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DO4);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DO5);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DO6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DO7);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DO8);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DO9);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DO10);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DO11);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DO12);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DO13);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DO14);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DO15);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->DO16);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->DO17,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->DOM1,token);
	token=strtok(NULL,delimiter);
	strAsNONLINEAR(token,&entry->DOM2);
	search_dmsa_o(&entry->DO16,&entry->ORBPARLIT);
	if (entry->DO17[0]=='D' || entry->DO17[0]=='W' || 
	    entry->DO17[0]=='X' || entry->DO17[0]=='Z' )
	   search_hd_notes(&entry->DO1,&entry->HDNOTES);
	else
	   entry->HDNOTES.no_entries=0;
	if (entry->DO17[0]=='G' || entry->DO17[0]=='Y' || 
	    entry->DO17[0]=='X' || entry->DO17[0]=='Z' )
	   search_hg_notes(&entry->DO1,&entry->HGNOTES);
	else
	   entry->HGNOTES.no_entries=0;
	if (entry->DO17[0]=='P' || entry->DO17[0]=='W' || 
	    entry->DO17[0]=='Y' || entry->DO17[0]=='Z' )
	   search_hp_notes(&entry->DO1,&entry->HPNOTES);
	else
	   entry->HPNOTES.no_entries=0;
   return 0;
} /* End of read_hip_dm_o */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->DO1 );
	printf("DO1  : %s             Hipparcos Catalogue (HIP) identifier \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->DO2 );
	printf("DO2  : %s        Orbital period, P (days) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->DO3 );
	printf("DO3  : %s        Time of periastron passage, T (days from JD2,440,000.0) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DO4 );
	printf("DO4  : %s          Semi-major axis of photocentre orbit, a_0 (mas) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->DO5 );
	printf("DO5  : %s        Eccentricity, e \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DO6 );
	printf("DO6  : %s          Argument of periastron, omega (deg) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DO7 );
	printf("DO7  : %s          Inclination, i (deg) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DO8 );
	printf("DO8  : %s          Position angle of the node, Omega (deg) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->DO9 );
	printf("DO9  : %s        Standard error of P (days) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->DO10);
	printf("DO10 : %s        Standard error of T (days) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DO11);
	printf("DO11 : %s          Standard error of a_0 (mas) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%13.4f",&entry->DO12);
	printf("DO12 : %s        Standard error of e \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DO13);
	printf("DO13 : %s          Standard error of omega (deg) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DO14);
	printf("DO14 : %s          Standard error of i (deg) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DO15);
	printf("DO15 : %s          Standard error of Omega (deg) \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->DO16);
	printf("DO16 : %s             Reference to the literature for the orbital parameters \n",outputStr);
	printf("DO17 : %1.1s                    Flag indicating a note at the end of relevant volume \n",entry->DO17);
	printf("DOM1 : %12.12s         Status flags for the 12 astrometric and orbital parameters taken  \n",entry->DOM1);
	printf("DOM2: 66 correlation coefficients in a non-linear coding \n");
	print_NONLINEAR_cols(&entry->DOM2);
	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_dmsa_o(&entry->ORBPARLIT,decode);
	else
	{
	   printf("ORBPARLIT\n");
	   print_array_dmsa_o_cols(&entry->ORBPARLIT,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_dm_o */

int print_hip_dm_o_cols (hip_dm_o* 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_o_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%6d",&entry->DO1);
	printf("%s",outputStr);
	FLOATasStr((char *)&outputStr,"%10.4f",&entry->DO2);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%11.4f",&entry->DO3);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%8.2f",&entry->DO4);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.4f",&entry->DO5);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->DO6);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->DO7);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->DO8);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%8.4f",&entry->DO9);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%9.4f",&entry->DO10);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->DO11);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.4f",&entry->DO12);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->DO13);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->DO14);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->DO15);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%3d",&entry->DO16);
	printf("|%s",outputStr);
	printf("|%1s",entry->DO17);
	printf("|%12s",entry->DOM1);
	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->DOM2);
	}
	else
	{
	   NONLINEARasStr((char *)&outputStr,"%197s",&entry->DOM2);
	   printf("%s",outputStr);
	}
	if ((entry->ORBPARLIT.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_dmsa_o_cols(&entry->ORBPARLIT,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_dm_o_cols */

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

	printf("DO1   |DO2       |DO3        |DO4     |DO5   |DO6   |DO7   |DO8   |DO9     |DO10     |DO11 |DO12  |DO13  |DO14  |DO15  |O16|7|DOM1        |DOM2                                                                                                                                                                                                 \n");
   return (0);
} /* End of print_hip_dm_o_headre */



FILE* search_hip_dm_o (INT* key, hip_dm_o* arecord)
{
   FILE* dataFile=NULL;
   idx_hip_dm* recNum;
   
   recNum= find_idx_hip_dm (key);
   if (recNum != NULL && (recNum->hdm_idx2[0] == 'O'))
   {
	dataFile=jump_hip_dm_o(recNum->hdm_idx3.value);
	if (read_hip_dm_o (dataFile,arecord)==0)
	   return dataFile;
	else
	   return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_hip_dm */

