/********************************************
* stcr_proto.h
* general prototype file and include file
********************************************/

/****************
* include files
****************/
#include <stdlib.h>        /* used for malloc declaration              */

#ifdef THINK_C
                           /* MacIntosh THINK C specific include files */
#include <unix.h>          /* used for open, read, write calls         */
#include <console.h>       /* for command line                         */
#include <fcntl.h>         /* FILE handling                            */

#else                      /* Ultrix Specific include files            */
#include <sys/file.h>
#endif                     /* THINK_C */

#include <stdio.h>
#include <string.h>        /* strncpy defined                          */
#include <math.h>

#ifndef O_RDONLY
#define O_RDONLY 0
#endif

/*****************
* useful defines
*****************/

#define NUMBER_OF_INPUT_PARMS       11

        /* 250ranges * 256pnt * 6 channels + 336 header = 384336 */
#define MAX_RANGES				   250
#define MAX_FFT_SIZE                64
#define MAX_RECEIVERS				 3
#define MAX_NCHANLS                  6 /* MAX_RECEIVERS * 2 */
#define VOS_WORD_SIZE                3
#define CUPRI_HEADER_SIZE          112
#define CUPRI_VOS_HEADER_SIZE	   336 /* CUPRI_HEADER_SIZE * VOS_WORD_SIZE */
#define MAX_PROC_DATA			 48000 /* MAX_RANGES * MAX_FFT_SIZE * MAX_RECEIVERS */
#define MAX_CUPRI_BYTES          96336 /* MAX_PROC_DATA * 2 + CUPRI_VOS_HEADER_SIZE */
#define MAX_BARKER_SIZE             13
#define MAX_RECEIVERS                3
#define PERMS                     0644
#define PERCENTILE_NOISE             0.10
#define PI							 3.141592654
#define PI2							 6.283185307
#ifdef DEBUG_GLOB
#define DEBUG_BARK
#define DEBUG_DT
#define DEBUG_ENC
#define DEBUG_FIO
#define DEBUG_FFT
#define DEBUG_MAIN
#define DEBUG_MOM
#define DEBUG_SPE
#endif /* DEBUG_GLOB */

/*********************************
* type definitions used in files
* struct InputParms
* struct DateTime
* struct HarrisHeader (header.h)
*  union RawHeader
* struct ComplexFloat
* struct ProcHeader
*********************************/
typedef struct InputParms
{
    char start_str[30];    /* start time in yyyy/mm/dd,hh:mm:ss format */
    char end_str[30];      /* end time in yyyy/mm/dd,hh:mm:ss format   */
    long int maxrecs;      /* maximum processed data records to write  */
    long int nrecs;        /* Number of FFT records to average         */
    long int rx;           /* Which receiver to use for moments        */
    long int first_r;      /* first range of interest                  */
    long int last_r;       /* last range of interest                   */
    long int pm;           /* processing mode:                         */
                           /* 1 - three moments of receiver rx.        */
                           /* 2 - nippstw point spectra for rx         */
                           /* 3 - spectra of rx and cross coherences   */
    long int fftwdth;      /* desired fft width.  Must be a multiple   */
                           /* of 2, no greater than 128 currently.     */
    long int inc_tx;       /* include tx samples?                      */
    long int inc_nx;       /* include nx samples?                      */
    char file[30];         /* raw data filename of file/tape           */
}InputParms;

typedef struct AOInputParms
{
    char start_str[30];    /* start time in yyyy/mm/dd,hh:mm:ss format */
    char end_str[30];      /* end time in yyyy/mm/dd,hh:mm:ss format   */
    long int pm;           /* processing mode:                         */
                           /* 1 - powrd and powre.                     */
                           /* 2 - powrd only.                          */
                           /* 3 - powre only                           */
                           /* 4 - mracf only.                          */
    char file[30];         /* raw data filename of file/tape           */
}AOInputParms;

typedef struct DateTime
{
    long int yr;        /* year             */
    int leap;           /* leap year? 1y,0n */
    long int mon;       /* month            */
    long int mday;      /* day of month     */
    long int yday;      /* day of year      */
    long int hr;        /* hours            */
    long int min;       /* minutes          */
    long int sec;       /* seconds          */
    long int tsec;      /* tenths of second */
}DateTime;

typedef struct HarrisAOHeader
{
    char ident1[12];                 /* CUPRI ident code (9bytes)        */
    long int harris_date_time[2];    /* Harris date and time    (2)      */
    char progname[12];               /* program and record type          */
    long int rec_number;             /* Record number of seqence         */
    long int notused10;
    long int scan_number;            /* scan number                      */
    long int start_time;             /* In some versions                 */
    long int stop_time;              /* In some versions                 */
    long int notused14[3];
    float start_zen;                 /* zenith angle at beginning        */
    long int spacer18;
    float start_az;                  /* azimuth angle at beginning       */
    long int spacer20;
    float end_zen;                   /* zenith angle at end              */
    long int spacer22;
    float end_az;                    /* azimuth angle at end             */
    long int spacer24;
    long int notused25[5];
    long int ngates;                 /* Total Sample Gates               */
    long int nlags;                  /* number of lags                   */
    long int notused32[3];
    long int n_tx_smp;               /* # transmitter samples            */
    long int n_sig_smp;              /* # signal samples                 */
    long int n_nx_smp;               /* # noise samples                  */
    long int notused38[9];
    long int npulses;                /* number of pulses                 */
    long int spacing;                /* spacing between multiple pulses  */
    char msa_txmod[8];               /* sampling control, tx mode        */
    long int ipp;                    /* TX Inter-pulse Period (us)       */
    long int bw;                     /* beam on (us)                     */
    long int rf;                     /* Transmitter Pulse Width (us)     */
    long int sd1;                    /* Delay to first sample gate (us)  */
    long int si1;                    /* Sample Interval (us)             */
    char txcode[4];                  /* tx coding, eg: 'BK ' for barker  */
    long int sd2;                    /* Delay to second sample gate (us) */
                                     /* from end of first sample window  */
    long int si2;                    /* Sample Interval #2 (us)          */
    long int ncodestat;              /* # of phase coding states         */
    long int notused60[10];
    long int nippsave;               /* number of ipps averaged          */
    long int notused71[21];
    long int gatewidth;              /* baud/gate width (us)             */
    long int nbauds;                 /* number of bauds                  */
    long int notused94[9];
    long int caltemp;                /* calibration pulse temp (K)       */
    long int notused104;
    long int nippsperbuf;            /* IPPs per tape write              */
    long int notused106[4];
    long int nwords;                 /* Number of data words in record   */
    long int notused111;
    long int nwords2;                /* Total number of words in record  */
}HarrisAOHeader;

typedef struct HarrisHeader
{
    char ident1[12];                 /* CUPRI ident code (9bytes)        */
    long int harris_date_time[2];    /* Harris date and time    (2)      */
    long int notused6[2];
    long int seq_number;             /* Sequence number                  */
    long int rec_number;             /* Record number of seqence         */
    long int notused10[2];
    long int start_time;             /* In some versions                 */
    long int stop_time;              /* In some versions                 */
    long int notused14;
    long int irig_words[2];          /* IRIG time words (2)              */
    long int notused17[22];
    long int tu_nippsf;              /* # of IPPs per frame              */
    long int tu_ipp;                 /* Transmitter Inter-pulse Period   */
    long int tu_pw;                  /* Transmitter Pulse Width (us)     */
    long int tu_ng;                  /* Number of Sample Gates per IPP   */
    long int tu_si;                  /* Sample Interval (us)             */
    long int tu_sd;                  /* Delay to first sample gate (us)  */
    long int tu_tau;                 /* Pulse separation (us)            */
    long int nchanls;                /* # ADC channels=2*Rx              */
    long int nsbits;                 /* # of bits in each sample         */
    long int irig;                   /* # of IRIG clock words (0 or 2)   */
    long int nippstw;                /* IPPs per tape write              */
    long int nbaudp;                 /* # bauds in TX pulse (0 no code)  */
    long int n_tx_smp;               /* # transmitter samples            */
    long int n_nx_smp;               /* # noise samples                  */
    long int mcd_ipp;                /* MCD IPP Thumbwheel               */
    long int mcd_pw;                 /* MCD PW Thumbwheel                */
    long int mcd_ng;                 /* MCD # Sample Gates Thumbwheel    */
    long int mcd_si;                 /* MCD Sample Interval Thumbwheel   */
    long int mcd_sd;                 /* MCD Sample Delay Thumbwheel      */
    long int notused58[3];
    char ident2[80];                 /* CUPRI ID                         */
    char ident3[80];                 /* User generated ID                */
    long int notused101[7];
    long int rt_rec;                 /* Real-Time record number          */
    long int mtu_rec;                /* MTU record number                */
    long int nwords;                 /* Number of data words in record   */
    long int totnwrds;               /* Total number of words in record  */
    long int start_seg;              /* what segment did we start with?  */
    long int stop_seg;               /* what segment did we end with?    */
}HarrisHeader;

typedef union RawHeader
{
    long int hdr[112];
    HarrisHeader h;
}RawHeader;

typedef union RawAOHeader
{
    long int hdr[112];
    HarrisAOHeader h;
}RawAOHeader;

typedef struct ComplexFloat
{
    float r;
    float i;
}ComplexFloat;

typedef struct AOProcHeader
{
    long int pd_rec;               /* Proc data record number          */
    long int mode;                 /* Mode 1 signal power mode         */
    long int size;                 /* size of this header in bytes;    */
    long int date;                 /* DDMMYY                           */
    long int start_time;           /* HHMMSSs                          */
    long int stop_time;            /* HHMMSSs                          */
    long int ipp;                  /* Transmitter Inter-pulse Period   */
    long int ngates;               /* Number of Sample Gates per IPP   */
    long int gatewidth;            /* gatewidth (us)                   */
    long int sd;                   /* Delay to first sample gate (us)  */
    float noise;                   /* noise for SNR calculation        */
    float start_zen;               /* starting zenith angle            */
    float end_zen;                 /* ending zenith angle              */
    float start_az;                /* start azimuth angle              */
    float end_az;                  /* ending zenith angle              */
    long int nave;                 /* # of ipps averaged               */
    long int nbytes;               /* Number of data bytes in record   */
    long int totnbyts;             /* Total number of bytes in record  */
}AOProcHeader;
    
typedef struct ProcHeader
{
    long int pd_rec;               /* Proc data record number          */
    long int mode;                 /* Mode 1,2 or 3                    */
                                   /* 1 is 3 moments of rx             */
                                   /* 2 is spectra of rx               */
                                   /* 3 is spec and cross coh          */
    long int size;                 /* size of this header in bytes.    */
    long int fh_date;              /* YYMMDD                           */
    long int fh_time;              /* HHMMSSs                          */
    long int fh_rec_number;        /* Record number of seqence on tape */
    long int fh_seg;               /* segment of first header for time */
    long int lh_date;              /* YYMMDD                           */
    long int lh_time;              /* HHMMSSs                          */
    long int lh_rec_number;        /* Record number of seqence on tape */
    long int lh_seg;               /* segment of last header for time  */
    long int tu_ipp;               /* Transmitter Inter-pulse Period   */
    long int tu_ng;                /* Number of Sample Gates per IPP   */
    long int tu_si;                /* Sample Interval (us)             */
    long int tu_sd;                /* Delay to first sample gate (us)  */
    long int tu_tau;               /* Double pulse separation (us)     */
    long int nippstw;              /* IPPs per tape write on original  */
    long int fftwdth;              /* actual fft width used            */
    long int nbaudp;               /* # bauds in TX pulse (0 no code)  */
    long int n_tx_smp;             /* # tx samps written to tape       */
    long int inc_tx;               /* included tx smps in output?      */
    long int n_nx_smp;             /* # nx samps written to tape       */
    long int inc_nx;               /* included rx smps in output?      */
    long int n_rx_smp;             /* # useful rx samps to write       */
    long int first_w;              /* first range to save              */
    long int last_w;               /* last+1 range to save  (for loop) */
    long int nrx;                  /* # of receivers                   */
    long int nave;                 /* # of records averaged            */
    long int nbytes;               /* Number of data bytes in record   */
    long int totnbyts;             /* Total number of bytes in record  */
    float bin_noise;               /* bin noise for SNR calculation    */
}ProcHeader;

/******************************
* prototypes from date_time.c
******************************/
long int    decimal_date(DateTime *dt);

long int    decimal_time(DateTime *dt);

DateTime    decode_ao_stop_time(long int *phdr);

DateTime    decode_ao_time(long int *phdr);

DateTime    decode_harris_time(long int *pdata);

DateTime    decode_irig_time(long int *pd);

void        get_day_in_month(DateTime *t);

void        get_day_in_year(DateTime *t);

DateTime    get_input_time(char *str);

int         is_leap(DateTime *dt);

void        printf_time(DateTime *t, char *label);

long int    seconds(DateTime *t);

/***************************
* prototypes from df_fft.c *
***************************/
void            bitrev(int *r, long int npts);

ComplexFloat    cadd(ComplexFloat *cf1, ComplexFloat *cf2);

ComplexFloat    cmag(ComplexFloat *cf1, ComplexFloat *cf2);

ComplexFloat    cphase(ComplexFloat *cf1, ComplexFloat *cf2);

ComplexFloat    cmult(ComplexFloat *cf1, ComplexFloat *cf2);

ComplexFloat    csub(ComplexFloat *cf1, ComplexFloat *cf2);

void            df_fft(ComplexFloat *n1, int npts, int e, ComplexFloat *wi, int *r, int phase);

void            df_fft_cell(ComplexFloat *a1, ComplexFloat *a2, ComplexFloat *cm);

void            df_fft_data(ComplexFloat *pvrx, HarrisHeader *ch, InputParms *ip, ComplexFloat *wi, int *br, int phase);

float			unwrap(ComplexFloat *c,float *last_angle);

void            wings(ComplexFloat *w, long int npts);

/***************************
* prototypes from encode.c *
***************************/
long int    convert_ao_data(char *data, HarrisAOHeader *ah, float *pns);

void        convert_data(char *data, HarrisHeader *ch, InputParms *ip, ComplexFloat *pVrx);

RawAOHeader convert_ao_header(char *buf);

RawHeader   convert_header(char *buf);

void        dump_ao_header(RawAOHeader ah);

void        dump_header(RawHeader ch);

void        dump_proc_hdr(ProcHeader *ph);

void        fix_procAOHdr(RawAOHeader *ah, AOProcHeader *ph, long int wrec, float noise, long int mode);

void        fix_procHdr(RawHeader *fh, RawHeader *lh, ProcHeader *ph, InputParms *ip, long int wrec, int rrec);

void        get_range_bins(ProcHeader *ph, InputParms *ip);

/****************************
* prototypes from file_io.c *
****************************/
void        dump_ao_moments(char *pmdata, int fwp);

void        dump_moments(float *pmdata, int fwp);

AOInputParms get_ao_input_parms(char *file);

InputParms  get_input_parms(char *file);

RawAOHeader get_rawao_record(int frp, char *buf, char *data);

RawHeader   get_raw_record(int frp, char *buf, char *data);

long int    get_raw_data(int frp, char *buf, char *data, InputParms *ip, RawHeader *ch);

void        printf_exit(char *s, long int err);

/*********************************
* prototypes from mac_graphics.c *
*********************************/
void initialize_graph_window(void);

void show_noise_spectra(ComplexFloat *ps, ProcHeader *ph);

void show_moments(float *ps, float *pv, float *pw, ProcHeader *ph);

/*************************
* prototypes from main.c *
*************************/
int compare_hdrs(HarrisHeader *fh, HarrisHeader *ch);

/****************************
* prototypes from moments.c *
****************************/
float   *compute_bin_doppler(ProcHeader *ph, float *data, ComplexFloat *spec);

float    compute_bin_width(ComplexFloat *spec, ProcHeader *ph);

float    compute_dp_doppler(ComplexFloat *spec, ProcHeader *ph);

float    compute_dp_power(ComplexFloat *cf, ProcHeader *ph);

void     compute_moments(ProcHeader *ph, float *pd, ComplexFloat *spec);

float   *compute_power(ProcHeader *ph, float *data, ComplexFloat *spec);

float    compute_sp_doppler(ComplexFloat *spec, ProcHeader *ph);

float    compute_sp_power(ComplexFloat *cf, ProcHeader *ph, long int noise_flag);

void     compute_spectral_width(ProcHeader *ph, float *pdev, ComplexFloat *spec);

void     RecToPol(ComplexFloat *cf);

float    sort_spectrum(ComplexFloat *cf, ProcHeader *ph);

/*****************************
* prototypes from spectrum.c
*****************************/
void accum_coherence_spectrum(ComplexFloat *coh, ComplexFloat *pvrx, HarrisHeader *ch, InputParms *ip, float *angle);

void accum_power_spectrum(ComplexFloat *spec, ComplexFloat *pvrx, HarrisHeader *ch, InputParms *ip);

void barker(long int bauds, int *bk);

void do_barker(ComplexFloat *pvrx, HarrisHeader *ch, InputParms *ip, int *bk);

void zero_array(ComplexFloat *spec, long int count);

void compute_spectra(ProcHeader *ph, float *pmdata, ComplexFloat *pspec);

void compute_coherence(ProcHeader *ph, float *pd, ComplexFloat *spec0, ComplexFloat *coh02);
