/***************************************************************

   LST_CONF.C

   Write out configuration settings vs. time if the settings change.  

--------------------------------------------------------------------

control file structure:                                         

     dbname:    
     output: 
     time_ranges:
     (list of YMDHMS time pairs:) 
        (yy/mm/dd hh:mm:ss) to (yy/mm/dd hh:mm:ss)
        .
        .
****************************************************************/

#include "geninc.h" 
#include "ioserv.h"
#include "use_db.h"
#include "adcp.h"
#include "data_id.h"
#include "dbinc.h"

#define  TRUE  1
#define  FALSE 0
#define  tol 0.0001
#define  equal(x,y) ( fabs((x)-(y)) <= tol ? 1 : 0 )

#if PROTOTYPE_ALLOWED
void do_it(FILE *fp_cnt);
void print_hed(FILE *fp_out);
unsigned int print_bit_short_pattern(FILE *fp, SHORT *k);
void int_bit_short_pattern(SHORT k, char *b);
#else
void do_it();
void print_hed();
unsigned int print_bit_short_pattern();
void int_bit_short_pattern();
#endif

/*
Main routine
*/
#if PROTOTYPE_ALLOWED
void do_it(FILE *fp_cnt)
#else
void do_it(fp_cnt)
FILE *fp_cnt;
#endif
{
   FILE *fp_out;
   char btrac[4];
   int  ierr;
   YMDHMS_TIME_TYPE start, end,            /* for time range */
                    this_time;             /* current profile time */
   CONFIGURATION_1_TYPE conf;
   FLOAT save_avg_interval = 0, save_tr_depth = 0, save_bin_length = 0, 
         save_pls_length = 0,  save_blank_length = 0, save_hd_offset = 0,
         save_heading_bias = 0;
   SHORT save_bot_track = 0, save_top_ref_bin = 0,save_bot_ref_bin = 0, save_num_bins = 0,
         save_compensation = 0;

   /*---------- end of auto declarations for do_it -----------------*/

   static   FILE_NAME_TYPE  dbname, out_filename;
   static   int   steps = 1;

   static   PARAMETER_LIST_ENTRY_TYPE parameters[] =
   {
      { " dbname:",     " %79s", dbname,                 TYPE_STRING },
      { " output:",     " %79s", out_filename,           TYPE_STRING },
      { NULL,           NULL,    NULL,                   0           }
   };                   /* parameters[] */


/***************************************************************/

   if (get_parameters(fp_cnt, parameters, NULL))  exit(-1);

   check_dbopen(1, dbname, READ_ONLY, DIR_IN_MEMORY);
   fp_out = check_fopen(out_filename,"w");

   check_error( (fscanf(fp_cnt, " time_ranges:") != 0),
         "reading time range start");
/*
---> Print the header.
*/
   printf(" ok boss\n");
   print_hed( fp_out );

/*
---> Start the loop through time ranges listed in the control file.
*/
   while (get_time_range(fp_cnt, &start, &end) == 1)
   {
      check_dbsrch(TIME_SEARCH, (char *)&start);

      print_time_range(stdout, &start, &end);
      fprintf(fp_out, "\n");

     ierr = 0;
/*
---> Start the loop through profiles within the time range.
*/
      while (!ierr && check_time(&this_time, &start, &end))
      {
         if (check_dbget(CONFIGURATION_1, (char *) &conf, sizeof(CONFIGURATION_1_TYPE),
            "getting CONF") == sizeof(CONFIGURATION_1_TYPE))
         {
            /* set bottom track output style */
            if( conf.bot_track == 0 )
                strcpy( btrac, "off" );
            else
                strcpy( btrac, " on" );

            if (!equal(conf.pls_length, save_pls_length) || !equal(conf.bin_length, save_bin_length) ||
                !equal(conf.avg_interval, save_avg_interval) || !equal(conf.tr_depth, save_tr_depth) ||
                !equal(conf.blank_length, save_blank_length) || !equal(conf.bot_track, save_bot_track) ||
                !equal(conf.top_ref_bin, save_top_ref_bin) || !equal(conf.bot_ref_bin, save_bot_ref_bin) ||
                !equal(conf.hd_offset, save_hd_offset) || !equal(conf.heading_bias, save_heading_bias) ||
                !equal(conf.num_bins, save_num_bins)   || !equal(conf.compensation, save_compensation)  )
            
            {
               write_ymdhms_time( fp_out, &this_time );
               fprintf(fp_out, " %3d   %3s   %3.0f %3hd   %2.0f %2.0f %2.0f %2.0f   %2hd %2hd   %6.2f %6.2f ",
                  db->block_dir_index,
                  btrac,
                  conf.avg_interval,
                  conf.num_bins,
		  conf.bin_length,
                  conf.pls_length,
                  conf.tr_depth,
                  conf.blank_length,
                  conf.top_ref_bin,
                  conf.bot_ref_bin,
                  conf.hd_offset,
                  conf.heading_bias);
               print_bit_short_pattern( fp_out, (SHORT *) &(conf.compensation) );
               fprintf( fp_out,"\n");
               save_avg_interval = conf.avg_interval;
               save_num_bins = conf.num_bins;
               save_pls_length = conf.pls_length;
	       save_bin_length = conf.bin_length;
               save_tr_depth  =  conf.tr_depth; 
               save_blank_length  =  conf.blank_length; 
               save_top_ref_bin  =  conf.top_ref_bin; 
               save_bot_ref_bin =  conf.bot_ref_bin;
               save_bot_track =  conf.bot_track;
               save_heading_bias =  conf.heading_bias;
               save_hd_offset =  conf.hd_offset;
               save_compensation =  conf.compensation;
            }
         }

         DBMOVE(&steps, &ierr);   /* ierr is checked at the start of the loop */

      }  /* end of loop through profiles within a time range */

   }  /* end of loop through time ranges read from control file */

   fclose(fp_out);
   DBCLOSE(&ierr);
   check_error(ierr, "DBCLOSE");
   printf("\nlst_conf completed.\n");
   return;
}
      
/************************************************************************

   function: print_hed

   print header for configuration history file

*/
#if PROTOTYPE_ALLOWED
void print_hed(FILE *fp_out)
#else
void print_hed( fp_out )
FILE *fp_out;
#endif
{
   fprintf(fp_out, "\n Configuration History File \n\n");
   fprintf(fp_out, "Columns are as follows: \n\n");
   fprintf(fp_out, " B : CODAS logical block number  \n\n");
   fprintf(fp_out, "BT : bottom track mode (on or off)  \n\n");
   fprintf(fp_out, "SI : sampling interval or averaging period for ensemble (sec)\n");
   fprintf(fp_out, "NB : number of bins\n\n");
   fprintf(fp_out, "BL : bin length (m)\n");
   fprintf(fp_out, "PL : pulse length (m)\n");
   fprintf(fp_out, "TD : transducer depth (m)\n");
   fprintf(fp_out, "BK : blanking length (m)\n\n");
   fprintf(fp_out, "TR : top reference bin (m)\n");
   fprintf(fp_out, "BR : bottom reference bin (m)\n\n");
   fprintf(fp_out, "HO : heading offset applied by DAS (deg)\n");
   fprintf(fp_out, "HB : heading bias (deg)\n");
   fprintf(fp_out, "CRPH : compensation for roll-pitch-heading, 1:on, 0:off)\n\n");

   fprintf(fp_out, "yy/mm/dd  hh:mm:ss   B    BT    SI  NB   BL PL TD BK   TR BR       HO     HB CRPH");
}

/*-----------------------------------------------------------------------------
 
   FUNCTION:  print_bit_short_pattern
 
      It prints the only 4 right most bits  pattern of a SHORT integer k in the format
 
            bbbb
 
   PARAMETERS:
 
      fp = pointer to file to print bit pattern to (usually stdout)
 
      k = pointer to SHORT integer to print as bit pattern
 
   RETURNS:  VOID
 
*/
#if PROTOTYPE_ALLOWED
unsigned int print_bit_short_pattern(FILE *fp, SHORT *k)
#else
unsigned int print_bit_short_pattern(fp, k)
FILE *fp;
SHORT *k;
#endif
{
   char b[16];
   int ibyte, i1, i;
 
   int_bit_short_pattern(*k, b);
   for (ibyte = 0;  ibyte >= 0;  ibyte--)
   {
      i1 = ibyte * 8 + 7;
      /* for (i = i1;  i > (i1 - 4);  i--)
         fprintf(fp, "%1d", b[i]);
      fprintf(fp, " "); */
      for (i = (i1 - 4);  i > (i1 - 8);  i--)
         fprintf(fp, "%1d", b[i]);
      fprintf(fp, "  ");
   }
   return(sizeof(SHORT));
}
 
/*-----------------------------------------------------------------------------

   FUNCTION: int_bit_short_pattern

      Given a SHORT integer k, it generates the binary representation of k
      in a CHAR array.

   PARAMETERS:

      k = the SHORT integer

      b = array to store binary representation in
 
   RETURNS:  VOID

*/
#if PROTOTYPE_ALLOWED
void int_bit_short_pattern(SHORT k, char *b)
#else
void int_bit_short_pattern(k, b)
SHORT k;
char *b;
#endif
{
   int i;
   for (i = 0;  i < 8 * sizeof(SHORT);  i++)
   {
      if (k & ( 1L << i))
         b[i] = '\001';
      else b[i] = '\0';
   }
   return;
}
