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

/* 
########################################################
# INIT_STRUCT                                          #
#          - program to init. and update the BBSO      #
#            telescope structure (of type Cstruct).    #
#            The telescope position is given in either #
#            cartesian or heliographic coordinates     #
#            and the update is made according to the   #
#            selected mode.                            #
#                                                      #
#          - mode = CART   converts (x,y) to (L,B)     #
#            mode = HELIO  converts (L,B) to (x,y)     #
#                                                      #
#          - Ephem will contain current: P, B0, L0, S  #
#                                                      #
#          - returns 0 in case of error                #
#                    1 otherwise                       #
#                                                      #
#       by: Anders Johannesson                         #
#                                                      #
#  Modifications:  13-Oct-97  AJO  Created             #
#                  21-Oct-97  AJO  Changed time update #
#                  01-Nov-97  AJO  Absolute Flexure    #
#                  06-Nov-97  AJO  Error Codes         #
#                                                      #
#       Part of the PDP replacement project 1997       #
########################################################
*/

short init_struct(Cstruct *NewStruct, TargetType target,
                  EphType *Ephem, short init_mode)

{

   /* DECLARATIONS */
   double S, B0, P, L0, B, L, x, y;
   short  ondisk, error_code;
   struct tm *Current_Time_UT;
   time_t Current_Time;
   TimeType Etime;
   int old_fupdate;

   /* CURRENT COORDINATES ARE ON THE DISK -  EVERYTHING OK */
   error_code=OK;

   /* GET CURRENT TIME */
   time(&Current_Time);
   Current_Time_UT=gmtime(&Current_Time);
   Etime.iy=Current_Time_UT->tm_year+1900;
   Etime.im=Current_Time_UT->tm_mon+1;
   Etime.id=Current_Time_UT->tm_mday;
   Etime.ih=Current_Time_UT->tm_hour;
   Etime.imi=Current_Time_UT->tm_min;
   Etime.is=Current_Time_UT->tm_sec; 

   /* CALCULATE EPHEMERID */
   soleph(Etime,Ephem);
   S=Ephem->rad;
   B0=Ephem->b;
   P=Ephem->p;
   L0=0.0; /* use central meridian as reference */

   /* REGION NAME */
   strcpy(NewStruct->name,target.name);

   /* SET UPDATE FLAGS */
   if (init_mode == INIT_ON)
      old_fupdate = 0;
   else
      old_fupdate=NewStruct->fupdate;
   if ((target.update_mode == ROTATION) || (target.update_mode == ROTATION_AND_FLEXURE))
      NewStruct->rupdate=1;
   else
      NewStruct->rupdate=0;
   if ((target.update_mode == FLEXURE) || (target.update_mode == ROTATION_AND_FLEXURE))
      NewStruct->fupdate=1;
   else
      NewStruct->fupdate=0;
   
   /* FILL IN COORDINATES - GIVEN CART COORDINATES */
   if (target.conversion_mode == CART)
   {

      /* TARGET COORDINATES */
      x=target.coord1;
      y=target.coord2;
      NewStruct->xpos=x;
      NewStruct->ypos=y;

      /* FIND HELIOGRAPHIC COORDINATES */
      error_code=cart_to_helio(S,B0,P,L0,x,y,&B,&L);

      if (error_code == OK)
      {
         NewStruct->rlong=L;
         NewStruct->rlat=B;
      }
      else
      {
         NewStruct->rlong=-9999.9;
         NewStruct->rlat=-9999.9;
      }

   }

   /* FILL IN COORDINATES - GIVEN HELIO COORDINATES */
   if (target.conversion_mode == HELIO)
   {

      /* TARGET COORDINATES */
      L=target.coord1;
      B=target.coord2;

      /* FIND NEW PROJECTION ON DISK */
      error_code=helio_to_cart(S,B0,P,L0,B,L,&x,&y);

      if (error_code == OK)
      {
         NewStruct->xpos=x;
         NewStruct->ypos=y;
         NewStruct->rlong=L;
         NewStruct->rlat=B;
      }

   }

   /* SET UPDATE TIME FOR ROTATION */
   if (NewStruct->rupdate == 1)
   {
       NewStruct->r_hour=Current_Time_UT->tm_hour;
       NewStruct->r_minute=Current_Time_UT->tm_min;
       NewStruct->r_second=Current_Time_UT->tm_sec;
       NewStruct->r_year=Current_Time_UT->tm_year+1900;
       NewStruct->r_month=Current_Time_UT->tm_mon+1;
       NewStruct->r_day=Current_Time_UT->tm_mday;
   }

   /* ZERO FLEXURE */
   if (init_mode == INIT_ON)
   {
      /*
      ABSOLUTE FLEXURE VALUES WILL REPLACE ZEROS AFTER FIRST UPDATE
      IF THE FUPDATE FLAG IS SET. FLEXURE CANNOT BE CALCULATED NOW
      SINCE THE FLEXURE LUT IS NOT DEFINED YET.
      */
      NewStruct->f_offx=0.0;
      NewStruct->f_offy=0.0;
   }
   else
   {
      /* SET FLEXURE TO ZERO IF FLAG CHANGED TO ZERO */
      if ((old_fupdate == 1) && (NewStruct->fupdate == 0))
      {
         NewStruct->f_offx=0.0;
         NewStruct->f_offy=0.0;
      }
   }

   return(error_code);

}


