#include "soleph.h"  /* Header file for BBSO ephemeris programs */

/*
##############################################################
# TEST PROGRAM FOR THE BBSO UPDATE ROUTINES
############################################# AJO 1997-11-01 #
*/

void main()
{
 
  /* DECLARATIONS */
  Cstruct NewStruct;
  short ierr, step;
  long i, counter;
  time_t Current_Time, Last;
  struct tm *Current_Time_UT;
  EphType Ephem;
  TargetType target;
  double Flex_LUT[2][145], H_ref;

  /* START TEST */
  step=10; /* time step in seconds */
  printf("\n Test of coordinate update (%d s steps). \n \n \n",step);
  printf("Type   Num  Helio Long.  Helio Lat.        X Y        Time (UT)"); 
  printf("    Flexure \n");

  /* GET TIME */
  time(&Current_Time);
  Current_Time_UT=gmtime(&Current_Time);

  /* INIT UPDATE */
  strcpy(target.name,"current");
  target.coord1=0.0;
  target.coord2=100.0;
  target.conversion_mode=CART;
  target.update_mode=ROTATION_AND_FLEXURE; 
  ierr=init_struct(&NewStruct, target, &Ephem, INIT_ON);
  printf("Init error code: %d \n", ierr);
  printf("L0: %10.2f \n", Ephem.l0);
  printf("P: %10.2f \n", Ephem.p);
  printf("B0: %10.2f \n", Ephem.b);

  /* GET FLEXURE LOOK-UP-TABLE (LUT) */
  ierr=get_flexure_LUT(TELESCOPE_10INCH,Flex_LUT);
  printf("Flexure error code: %d \n", ierr);

  /* SET HOUR ANGLE OF FLEXURE REFERENCE */
  H_ref=-2.0;

  /* TEST UPDATE ROUTINE */
  Last=Current_Time;
  i=1;
  ierr=OK;
  counter=1;
  while( (counter < 10000) && ((ierr == OK) || (ierr = BADXY)) )
  {

     time(&Current_Time);
     Current_Time_UT=gmtime(&Current_Time);

     if (Current_Time-Last >= step)
     {

        counter++;

	/* UPDATE TIME */
	Last=Current_Time;

	/* UPDATE ROUTINE (SET FOR 10 INCH SCOPE) */
        printf("Newstruct %f %f \n",NewStruct.xpos,NewStruct.ypos);
        ierr=update(&NewStruct,H_ref,Flex_LUT,Ephem);
        printf("Update error code: %d \n", ierr);
        printf("EPHEM : %10.7f \n",Ephem.l0);

        /* PRINT CALCULATED UPDATES */
	if ((ierr == OK) || (ierr == BADXY))
	{
	   if (NewStruct.rupdate == 1) printf("R");
           if (NewStruct.fupdate == 1) printf("F");
           printf(" %7d %10.6f", i, NewStruct.rlong); 
           printf("   %10.6f ", NewStruct.rlat); 
           printf("   %6.1f %6.1f ", NewStruct.xpos, NewStruct.ypos); 
           printf("   %2.2d:", Current_Time_UT->tm_hour);
           printf("%2.2d:", Current_Time_UT->tm_min);
           printf("%2.2d ", Current_Time_UT->tm_sec); 
           printf("   %4.1f %4.1f \n", NewStruct.f_offx, NewStruct.f_offy); 
           i++;
	}
	else
	{
	   printf("\n ERROR - target is behind the limb \n");
	}

      }

   }

}
