#include "raw_proto.h"

/* compare_hdrs *****************************************************
* INPUT:    fh  pointer to first header record of current averaging *
*           ch  pointer to current header record.                   *
* RETURNS:  0   if no change was detected.                          *
*           1   if a change was detected.                           *
* PURPOSE:                                                          *
* determine if a change has taken place in header that warrants     *
* restarting averaging sequence.                                    *
********************************************************************/
int compare_hdrs(HarrisHeader *fh, HarrisHeader *ch)
{
    /****************************************
    * parameters in HarrisHeader to compare *
    * tu_nippsf, tu_ipp, tu_pw, tu_ng,      *
    * tu_sd, tu_tau, nippstw, nbaud,        *
    * n_tx_smp, n_nx_smp, nwords            *
    ****************************************/

  if(fh->tu_nippsf != 1)
    printf_exit("raw_main: compare_hdrs: Can't handle %ld ipps per frame\n",
		fh->tu_nippsf);
  if(ch->tu_nippsf != 1)
    printf_exit("raw_main: compare_hdrs: Can't handle %ld ipps per frame\n",
		ch->tu_nippsf);
    
  if(ch->tu_ipp != fh->tu_ipp) return(1);    /* did the ipp changed? */
  if(ch->tu_pw != fh->tu_pw) return(1);      /* did the pulse width changed? */
  if(ch->tu_ng != fh->tu_ng) return(1);      /* did total gates change?      */
  if(ch->tu_sd != fh->tu_sd) return(1);      /* did sample delay change?     */
  if(ch->tu_tau != fh->tu_tau) return(1);    /* did double pulse change?     */
  if(ch->nippstw != fh->nippstw) return(1);  /* did # ipps saved change?     */
  if(ch->nbaudp != fh->nbaudp) return(1);    /* did barker code change?      */
  if(ch->n_tx_smp != fh->n_tx_smp) return(1);/* did transmitter smps change? */
  if(ch->n_nx_smp != fh->n_nx_smp) return(1);/* did noise smps change?       */
  if(ch->nwords != fh->nwords) return(1);    /* did total data words change? */
  
  return(0);
}

/* main *****************************************************************
* INPUT:    argc    count of inputs on command line                     *
*           argv    array of input strings the name of file with        *
*                   the processing parameters should be specified.      *
*                   mumparms is the original file.  The 'u' specifies   *
*                   that a filename is provided at the end of the file  *
*                   defining the data file/tape from which to read data *
* PURPOSE:                                                              *
* This particular CUPRI program checks the bits in all the receivers    *
************************************************************************/
void main(int argc, char **argv)
{
  char spfile[30],dpfile[30];	  /* output file moms[mode].[ymd].[hms]   */
  char *buf;                      /* raw data input buffer                */
  char *data, *pdata;             /* pointer to beginning of data in buf  */
  InputParms ip;                  /* input parameters structure           */
  RawHeader ch;                   /* current raw record header            */
  DateTime irig, harris;          /* irig and harris times                */
  DateTime startdt, stopdt;       /* start and stop processing times      */
  long int start, stop, curr;     /* start,stop,current seconds in yr     */
  int i,frp;                      /* input/output tape/file               */
  long int *bit_count;            /* bit counters for each receiver chan  */

  /* allocation of memory */
  buf = (char *) malloc(MAX_CUPRI_BYTES*sizeof(char));
  bit_count = (long int *) malloc(6*8*sizeof(long int));
    
  /* initializing variables */
  pdata = data = &buf[CUPRI_VOS_HEADER_SIZE];

#ifdef THINK_C
  /* the following call is needed for the Mac to provide an I/O window to input
   * parameters that would normally be entered on the command line in
   * other systems.
   */

  argc = ccommand(&argv);
#endif /* THINK_C */
    
  ip = get_input_parms(argv[1]);
  
  if((frp = open(ip.file,O_RDONLY)) == -1)
    printf_exit("raw_main: main: can't open read file\n",(long int)ip.file);
  
  /* convert input times */
  startdt = get_input_time(ip.start_str);
  stopdt = get_input_time(ip.end_str);
  
  /* convert start and stop times to seconds to use in search
   * and processing loops.
   */
  
  start = seconds(&startdt);
  stop = seconds(&stopdt);
  
  /* clear bit array */
  for(i=0;i<48;i++) bit_count[i] = 0;
  
  /* loop through raw records until arrive at start time */
  printf("raw_main: main: Finding start location.\n");
  curr = 0;
  while(curr<start)
  {
    static long int last_dift = 0,bit_counter=0;
    
    long int li_dift;
    int ix,iy,j;
    
    /* get current time from raw record */
    ch = get_raw_record(frp,buf,data);
    if(ch.h.totnwrds == -1)
      printf_exit("raw_main: main: Record Read Error, EOF detected\n",0);
    harris = decode_harris_time(ch.hdr);
    curr = seconds(&harris);
    
    li_dift = start - curr;
    if(!(li_dift%100) && li_dift != last_dift)
    {
      printf("%ld seconds remaining.\n",li_dift);
      last_dift = li_dift;
    }
    
    pdata = data;
    for(i=0;i<ch.h.tu_ng;i++)
    {
      for(ix=0;ix<6;ix++)
      {
	for(iy=0;iy<8;iy++)
	{
	  bit_count[ix*8 + iy] += ((*pdata) & (1 << iy)) >> iy;
	}
	pdata++;
      }
      if(!((bit_counter++)%1000))
      {
	printf("# summed %ld\n",bit_counter);
	for(j=0;j<48;j+=8)
	  printf("bit[%2d] : %6ld %6ld %6ld %6ld %6ld %6ld %6ld %6ld\n",
		 j,bit_count[j],bit_count[j+1],bit_count[j+2],bit_count[j+3],
		 bit_count[j+4],bit_count[j+5],bit_count[j+6],bit_count[j+7]);
      }
    }
  }
  close(frp);
}
