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


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


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

	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_hipdmcom(&entry->COMPONENTS ,decode);
	else
	{
	   printf("COMPONENTS \n");
	   print_array_hipdmcom_cols(&entry->COMPONENTS ,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_hipdmcor(&entry->CORRELATIONS,decode);
	else
	{
	   printf("CORRELATIONS\n");
	   print_array_hipdmcor_cols(&entry->CORRELATIONS,decode);
	}
   return (0);
} /* End of print_hip_dm_c */

int print_hip_dm_c_cols (hip_dm_c* 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_c_cols NULL pointer for entry\n");
      return (-1);
   }

	if ((entry->COMPONENTS.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_hipdmcom_cols(&entry->COMPONENTS,decode);
	}
	if ((entry->CORRELATIONS.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_hipdmcor_cols(&entry->CORRELATIONS,decode);
	}
	printf("\r\n");
	return (0);
} /* End of print_hip_dm_c_cols */

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

	printf("\n");
   return (0);
} /* End of print_hip_dm_c_headre */


/*  array routines  */ 
int read_array_hip_dm_c (int recNum, array_hip_dm_c* array)
{
/* Jump to recNum then read first record to get the 
   number of soloutions  (DCM1 of the COMP record)*/
   int i=0,finished=0,no_components,no_correlations;
   hipdmcom *toprec;
   FILE* fp=jump_hip_dm_c(recNum);
   while (!finished)
   {
      if (read_hipdmcom (fp,&array->data[i].COMPONENTS.data[0]) == -1)
         return -1;
      toprec = &array->data[i].COMPONENTS.data[0] ;
      no_components = toprec->DCM2.value;
      no_correlations = toprec->DCM4.value;
      /* we already read the first component but we let read_array_dmcomp
	 take care of that */
      read_array_hipdmcom(fp,no_components,&array->data[i].COMPONENTS);
      read_array_hipdmcor(fp,no_correlations,&array->data[i].CORRELATIONS);
      i++;
      finished=(toprec->DCM1.value == i);
   }
   array->no_entries=i;
   return 0;
}
int print_array_hip_dm_c_cols (array_hip_dm_c* array, int decode) 
{
   int i;
   if (array == NULL) 
   {
      fprintf (stderr,"print_array_hip_dm_c NULL pointer for entry\n");
      return (-1);
   }
/*   printf("Array array_hip_dm_c contains %d entries :\n",array->no_entries);*/
      if (array->no_entries > 0)
      {
         print_hip_dm_c_header();
      }
      for (i=0; i<array->no_entries; i++)
      {
	 print_hip_dm_c_cols (&array->data[i],decode) ;
      }
      return (0);
} /* End of print_array_hip_dm_c_cols */

int print_array_hip_dm_c (array_hip_dm_c* array, int decode) 
{
   int i;
   if (array == NULL) 
   {
      fprintf (stderr,"print_array_hip_dm_c NULL pointer for entry\n");
      return (-1);
   }
/*   printf("Array array_hip_dm_c contains %d entries :\n",array->no_entries);*/
      for (i=0; i<array->no_entries; i++)
      {
	 print_hip_dm_c (&array->data[i],decode) ;
      }
      return (0);
} /* End of print_array_hip_dm_c */


FILE* search_hip_dm_c (ccdm* key, array_hip_dm_c* array)
{
   FILE* dataFile=NULL;
   long recNum;
   
   recNum= find_idx_hip_dm_c (key);
   if (recNum >= 0)
   {
      if (read_array_hip_dm_c (recNum,array)==0)
	return jump_hip_dm_c(recNum + array->no_entries) ;
      else
	return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_hip_dm_c */

