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

int read_hipdmcor (FILE* fp, hipdmcor* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hipdmcor - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[hipdmcor_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hipdmcor NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hipdmcor_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strcpy((char *)&entry->DC1,token);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->DC2);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->DC3,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->DC4,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->DC5,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->DC6,token);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->DCM1);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->DCM2);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->DCM3);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->DCM4);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->DCM5,token);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->DCM6);
	token=strtok(NULL,delimiter);
	strAsNONLINEAR(token,&entry->DCM9);
   return 0;
} /* End of read_hipdmcor */ 

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


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

	printf("DC1  : %10.10s           System identifier (CCDM number) \n",entry->DC1 );
	INTasStr((char *)&outputStr,"%8d",&entry->DC2 );
	printf("DC2  : %s             Solution identifier, S (1 to N_S) \n",outputStr);
	printf("DC3  : %1.1s                    Type of solution (F, I, L)\n",entry->DC3 );
	printf("DC4  : %1.1s                    Source of solution (C, F, N) \n",entry->DC4 );
	printf("DC5  : %1.1s                    Quality of solution (A, B, C, D) \n",entry->DC5 );
	printf("DC6  : %1.1s                    Flag indicating a note \n",entry->DC6 );
	INTasStr((char *)&outputStr,"%8d",&entry->DCM1);
	printf("DCM1 : %s             Number of solutions pertaining to the system, N_S \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->DCM2);
	printf("DCM2 : %s             Number of components in this solution, N_C \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->DCM3);
	printf("DCM3 : %s             Number of free parameters in this solution, N_P \n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->DCM4);
	printf("DCM4 : %s             Number of correlation records in this solution, N_R \n",outputStr);
	printf("DCM5 : %4.4s                 This field contains the word `CORR' \n",entry->DCM5);
	INTasStr((char *)&outputStr,"%8d",&entry->DCM6);
	printf("DCM6 : %s             Sequential number of this correlation record (1 to N_R) \n",outputStr);
	printf("DCM9: Up to 66 correlation coefficients in nonlinear coding \n");
	print_NONLINEAR_cols(&entry->DCM9);
	printf("\n");
   return (0);
} /* End of print_hipdmcor */

int print_hipdmcor_cols (hipdmcor* 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_hipdmcor_cols NULL pointer for entry\n");
      return (-1);
   }

	printf("%10s",entry->DC1);
	INTasStr((char *)&outputStr,"%1d",&entry->DC2);
	printf("|%s",outputStr);
	printf("|%1s",entry->DC3);
	printf("|%1s",entry->DC4);
	printf("|%1s",entry->DC5);
	printf("|%1s",entry->DC6);
	INTasStr((char *)&outputStr,"%1d",&entry->DCM1);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%2d",&entry->DCM2);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%2d",&entry->DCM3);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%2d",&entry->DCM4);
	printf("|%s",outputStr);
	printf("|%4s",entry->DCM5);
	INTasStr((char *)&outputStr,"%2d",&entry->DCM6);
	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->DCM9);
	}
	else
	{
	   NONLINEARasStr((char *)&outputStr,"%197s",&entry->DCM9);
	   printf("%s",outputStr);
	}
	printf("\r\n");
	return (0);
} /* End of print_hipdmcor_cols */

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

	printf("DC1       |2|3|4|5|6|1|M2|M3|M4|DCM5|M6|DCM9                                                                                                                                                                                                 \n");
   return (0);
} /* End of print_hipdmcor_headre */


/*  array routines  */ 
int read_array_hipdmcor (FILE* fp,int no_entries, array_hipdmcor* array) 
{
/* Read no_entries entries from the file into the array provided
   use read routine to achive this.
 */
   int i;
   
   array->no_entries=0;
   for (i=0; i<no_entries; i++)
   {
	if (read_hipdmcor (fp,&array->data[i]) == -1) 
	   break;
	
	if (array_hipdmcor_size == i) 
	{
	    fprintf(stderr," array_hipdmcor not big enough - all elements not loaded\n");
	    break;
	}
   }
   array->no_entries = i;
   return 0;
} /* End of read_array_hipdmcor */

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

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

