#include        <math.h>
#include        <stdio.h>
#include        <time.h>
#include        <string.h>

#include        "..\include\globals.h"

#define FIFOINTDEPTH    6000
static  float   Beamdelay,Timedelay;
void filldwell(DWELL *dwell,PIRAQ *piraq,CONFIG *config)
   {
   Beamdelay = (double)FIFOINTDEPTH / (double)(config->hits * 2 * config->gates);
   Timedelay = Beamdelay * config->hits * config->prt / 8000000.0;
   fillheader(dwell,piraq,config);
   readdata(dwell,piraq,config);
   }
   
/* don't fillheader until the DSP has been initialized */
void fillheader(DWELL *dwell,PIRAQ *piraq,CONFIG *config)
   {
   readmisc(dwell,piraq,config);
   readtime(dwell);
   readangles(dwell);
//   readposition(dwell);
   readvelocity(dwell);
   }
   
/* don't readmisc until the DSP has been initialized */
void readmisc(DWELL *dwell,PIRAQ *piraq,CONFIG *config)
   {
   int  size;
   
   dwell->header.desc[0] = 'D';
   dwell->header.desc[1] = 'W';
   dwell->header.desc[2] = 'E';
   dwell->header.desc[3] = 'L';
   dwell->header.gates = config->gates;
   dwell->header.hits = config->hits;
   dwell->header.rcvr_pulsewidth = config->pulsewidth * 1.25E-7;
   dwell->header.prt = config->prt * 1.25E-7;
   dwell->header.clutterfilter = config->clutterfilter;
   dwell->header.timeseries = config->timeseries;
   dwell->header.tsgate = config->tsgate;
   dwell->header.delay = config->delay * 1.25E-7;
   dwell->header.dataformat = *piraq->dataformat;
   if(dwell->header.dataformat == 0) dwell->header.dataformat=6; /* integer compressed binet format */
   dwell->header.prt2 = config->prt2 * 1.25E-7;
   dwell->header.ray_count = *piraq->flag;

   size = sizeof(HEADER) + 8 * (config->timeseries ? config->hits : 0);
   switch(dwell->header.dataformat)
      {
      case   DATA_POLYPP:  size += 5 * sizeof(float) * config->gates;   break;     
      case   DATA_DUALPP:  size += 6 * sizeof(float) * config->gates;   break;
      case     DATA_POL1:  size += 6 * sizeof(float) * config->gates;   break;
      case DATA_SIMPLEPP:  size += 3 * sizeof(float) * config->gates;   break;
      case 6:              size += 3 * sizeof(short) * config->gates;   break;
      }
   dwell->header.recordlen = size; 
   }

double az=0.0;
int stupid=0,ttest,subsec,otime;
void readtime(DWELL *dwell)
   {
   double       newtime;

//   gpstime(&dwell->header.time,&dwell->header.subsec);

   /* fake the GPS time (with subsec) for now */
   ttest = time(0);
   subsec += (dwell->header.prt * dwell->header.hits) / 800;
   if(otime != ttest) 
      {
      if(subsec < 10000) subsec = 0;
      subsec %= 10000;
      }
   else if(subsec >= 10000) subsec = 9999;
   dwell->header.time = otime = ttest;
   dwell->header.subsec = subsec;
   //printf("%d.%04d\n",dwell->header.time,(int)dwell->header.subsec);
   
   /* correct for the FIFO delay time */
   newtime = dwell->header.time + dwell->header.subsec / 10000.0 - Timedelay;
   dwell->header.time = newtime;
   dwell->header.subsec = (newtime - dwell->header.time) * 10000.0;
   }


void readangles(DWELL *dwell)
   {
   dwell->header.az = AZ;
   dwell->header.el = EL;
   }

/*
void readposition(DWELL *dwell)
   {
   dwell->header.radar_longitude = 0.0;
   dwell->header.radar_lattitude = 0.0;
   dwell->header.radar_altitude = 0.0;
   }
*/

void readvelocity(DWELL *dwell)
   {
   dwell->header.ew_velocity = 0.0;
   dwell->header.ns_velocity = 0.0;
   dwell->header.vert_velocity = 0.0;
   }

/* read the abp and time series data (if applicable) into the DWELL structure */
void readdata(DWELL *dwell,PIRAQ *piraq,CONFIG *config)
   {
   double      scale,hscale,offset,a,b;
   int                  i,end,*dpram;
   short                *abp;   
   float                *fabp;

   scale =  10.0 / log(10.0) / .004;  /* approx = 1085.7 */
   hscale = 0.5 * scale;
   offset = - 2.0 * scale * log(512.0 * dwell->header.rcvr_pulsewidth / 1.25e-7)
	    -       scale * log((double)dwell->header.hits);
   
   dpram = piraq->bufptr[*(piraq->flag) & 1];
   abp = (short *)dwell->abp;
   
   /* convert raw data from the dual port ram to log integers */
   for(i=0; i<config->gates; i++)   
      {
      a = toieee(*dpram++);
      b = toieee(*dpram++);

if(a != 0.0 || b != 0.0)      
{      
      *abp++ = hscale * log(a*a+b*b) + offset;
      *abp++ = -atan2(b,a) * 65536.0 / TWOPI;
}
else      
{      
*abp++ = 0;
*abp++ = 0;
}
      *abp++ =  scale * log((double)toieee(*dpram++)) + offset;
      }

   /* if there is time series, put it right after the abp data */
   if(config->timeseries)
      {
      fabp = (float *)abp;
      dpram = piraq->tsptr[*(piraq->flag) & 1];
      end = 2 * config->hits;
      for(i=0; i<end; i++)
	 *fabp++ = toieee(*dpram--);
      }
   }

