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

int read_hip_dm_v (FILE* fp, hip_dm_v* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hip_dm_v - 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_v_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hip_dm_v NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hip_dm_v_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strAsINT(token,&entry->DV1);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DV2);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DV3);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DV4);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DV5);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DV6);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DV7);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DV8);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DV9);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DV10);
	token=strtok(NULL,delimiter);
	strAsFLOAT(token,&entry->DV11);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->DV12,token);
	token=strtok(NULL,delimiter);
	strAsNONLINEAR(token,&entry->DVM1);
	if (entry->DV12[0]=='D' || entry->DV12[0]=='W' || 
	    entry->DV12[0]=='X' || entry->DV12[0]=='Z' )
	   search_hd_notes(&entry->DV1,&entry->HDNOTES);
	else
	   entry->HDNOTES.no_entries=0;
	if (entry->DV12[0]=='G' || entry->DV12[0]=='Y' || 
	    entry->DV12[0]=='X' || entry->DV12[0]=='Z' )
	   search_hg_notes(&entry->DV1,&entry->HGNOTES);
	else
	   entry->HGNOTES.no_entries=0;
	if (entry->DV12[0]=='P' || entry->DV12[0]=='W' || 
	    entry->DV12[0]=='Y' || entry->DV12[0]=='Z' )
	   search_hp_notes(&entry->DV1,&entry->HPNOTES);
	else
	   entry->HPNOTES.no_entries=0;
   return 0;
} /* End of read_hip_dm_v */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->DV1 );
	printf("DV1  : %s             Hipparcos Catalogue (HIP) identifier \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DV2 );
	printf("DV2  : %s          Adopted reference magnitude, Hp_ref (mag) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DV3 );
	printf("DV3  : %s          VIM element in right ascension, D_alpha* (mas) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DV4 );
	printf("DV4  : %s          VIM element in declination, D_delta (mas) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DV5 );
	printf("DV5  : %s          Standard error of D_alpha*, Sigma_D_alpha* (mas)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DV6 );
	printf("DV6  : %s          Standard error of D_delta, Sigma_D_delta (mas)\n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DV7 );
	printf("DV7  : %s          Significance of the VIM elements, F_D \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DV8 );
	printf("DV8  : %s          Position angle of the constant component, theta_C (deg) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%11.2f",&entry->DV9 );
	printf("DV9  : %s          Standard error of the position angle, sigma_theta_C (deg) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->DV10);
	printf("DV10 : %s           Lower limit for the separation of the binary, varrho_min (mas) \n",outputStr);
	FLOATasStr((char *)&outputStr,"%10.1f",&entry->DV11);
	printf("DV11 : %s           Displacement of photocentre between max and min\n",outputStr);
	printf("DV12 : %1.1s                    Flag indicating a note at the end of relevant volume \n",entry->DV12);
	printf("DVM1: 21 correlation coefficients in a non-linear coding\n");
	print_NONLINEAR_cols(&entry->DVM1);
	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_v */

int print_hip_dm_v_cols (hip_dm_v* 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_v_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%6d",&entry->DV1);
	printf("%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->DV2);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.2f",&entry->DV3);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.2f",&entry->DV4);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.2f",&entry->DV5);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%7.2f",&entry->DV6);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%5.2f",&entry->DV7);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->DV8);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.2f",&entry->DV9);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.1f",&entry->DV10);
	printf("|%s",outputStr);
	FLOATasStr((char *)&outputStr,"%6.1f",&entry->DV11);
	printf("|%s",outputStr);
	printf("|%1s",entry->DV12);
	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->DVM1);
	}
	else
	{
	   NONLINEARasStr((char *)&outputStr,"%63s",&entry->DVM1);
	   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_v_cols */

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

	printf("DV1   |DV2  |DV3    |DV4    |DV5    |DV6    |DV7  |DV8   |DV9   |DV10  |DV11  |2|DVM1                                                           \n");
   return (0);
} /* End of print_hip_dm_v_headre */



FILE* search_hip_dm_v (INT* key, hip_dm_v* arecord)
{
   FILE* dataFile=NULL;
   idx_hip_dm* recNum;
   
   recNum= find_idx_hip_dm (key);
   if (recNum != NULL && (recNum->hdm_idx2[0] == 'V'))
   {
	dataFile=jump_hip_dm_v(recNum->hdm_idx3.value);
	if (read_hip_dm_v (dataFile,arecord)==0)
	   return dataFile;
	else
	   return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_hip_dm */

