/* **********************************************************************
 * *                                                                    *
 * *           ASCOPE Utility Header and Common Definitions             *
 * *                                                                    *
 * **********************************************************************
 *
 *          COPYRIGHT (c) 1998, 1999, 2000, 2002, 2003, 2005 BY
 *          SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED.
 */

#define MAGARG_REC           1
#define ASCOPE_MAGARG_REC    (ASCOPE_FACILITY + MAGARG_REC  )
#define BAD_PLAYFILE         2
#define ASCOPE_BAD_PLAYFILE  (ASCOPE_FACILITY + BAD_PLAYFILE)

#ifndef MESSAGE_ONLY

#ifndef ASCOMMON_H
#define ASCOMMON_H

#ifdef  ASCOMMON_ALLOCATE
#define EXTERN
#else
#define EXTERN extern
#endif

#define MAX_NUM_PLOTS   8       /* Maxium number of plots displayed */

/* Local macros used only within the ASCOPE utility.
 */
#define MAX_PLOT_BINS   3072	/* Maximum number of bins in a parameter plot, */
#define MAX_PLOT_LINES     8	/*   and lines plotted within each plot. */

#define MAX_TS_BINS      128	/* Maximum number of time series bins */
#define MAX_PARAMS         6	/* Maximum number of simultaneous data parameters */
#define MAX_SAMPS       1024	/* ASCOPE Upper bound on sample size */
#define NOTPLOT      0x07FFF	/* 16-bit flag for "no data" */

/* Shared version of common definition structure.
 */
EXTERN struct asc_stats stat_c ;
EXTERN struct dsp_manual_setup dspsetup_c ;

/* ------------------------------
 * DSP pointer and debugging variables, and various scalars and tables
 * that are kept out of the saved status structure.
 */
EXTERN Dsp   *pdsp_c ;		/* ==> Dsp Structure */
EXTERN UINT2 ldspused_c ;	/* Indicates real DSP is being used */

EXTERN UINT4 iDelay_c ;         /* time in millisecs between calling asloop */
EXTERN UINT2 ldebugMode_c ;	/* Some random debugging */
EXTERN UINT2 lprintTTYMsg_c ;	/* Print error messages on the TTY */
EXTERN UINT2 lShowStats_c ;	/* Include statistics printouts in plots */

EXTERN UINT2 lopch_c ;		/* Indicates operational parameters have changed */

EXTERN UINT2 lstc_wave_ok_c ;	/* STC waveform and valid flag */
EXTERN UINT2 istc_waveform_c[8192] ;

EXTERN UINT2 lgain_cal_ok_c ;   /* Gain control calibration and valid flag */
EXTERN struct dsp_gain_cal gaincal_c ;

EXTERN UINT4 lagc_table_ok_c ;	/* AGC mapping table and valid flag */
EXTERN struct dsp_agc_cal agcal_c ;

EXTERN SINT4 ioldpwnumber_c ;	/* Pulse width prior to trying to change it */

EXTERN struct data_convert convert_c ;

EXTERN struct dsp_cal Cal_c ;	/* Reflectivity calibration information */

EXTERN FLT4 fzdroffset_c ;	/* ZDR offset correction (in dB) */
EXTERN FLT4 fldroffset_c ;	/* LDR offset correction (in dB) */

EXTERN FLT4 fgas_atten_c ;	/* Gaseous attenuation (in dB/km) */

EXTERN FLT4 fprt_units_c ;	/* Multiplier to covert DSP PRT to seconds */

EXTERN UINT4 iViewspecMask_c ;	/* Mask of available spectra for viewing */
EXTERN UINT1 lAnySizeSpec_c ;	/* Power spectra not constrained to powers of two */

EXTERN SINT4 iMaxSampleSize_c ;      /* Largest sample size during normal operation */
EXTERN SINT4 iMaxSpectrumSize_c ;    /* Maximum length of internally generated spectra */
EXTERN SINT4 iMaxSampsForSpectra_c ; /* Max samps when spectra are being made from them */

/* ------------------------------
 * Noise Sampling Variables
 */
EXTERN UINT4 lsample_c ;	/* Take a single noise sample */
EXTERN UINT4 lcontsample_c ;	/* Take continuous noise samples */

/* ------------------------------
 * Other SETUP structures and data.
 */
EXTERN struct license_setup LSetup_c;

/* ------------------------------
 * Variables related to the time series simulator.
 */
#define MINSLIDEPOWER -81	/* Minimum slide pot power level */

#define SIMSIZE_LOG (14)	/* Simulated time series length */
#define SIMSIZE (1 << SIMSIZE_LOG)

#define MAXSIMBINS MAX_PLOT_BINS

EXTERN UINT2 lsim_inuse_c ;	/* Use simulated time series */
EXTERN UINT2 ltargsim_inuse_c ;	/* Simulate targets within the DSP */

/* Each realization of the (rather long) simulated timeseries are
 * stored here.  This is the ideal representation of all of the
 * targets that have been selected in the SIMULATE menu.  Effects
 * resulting from Tx/Rx modulations and errors have not yet been
 * introduced.
 *
 * Dimensioned by [ Target ][ Pulse ][ I/Q ].  The weather targets and
 * clutter targets are kept as separate timeseries because we may need
 * to combine them differently when synthesizing an overall signature.
 */
EXTERN FLT4 fSimWeatherSeries_c[2][SIMSIZE][2] ;
EXTERN FLT4 fSimClutterSeries_c[2][SIMSIZE][2] ;

EXTERN SINT4 iSimPulseStart_c ;	/* Randomized starting point per CPI */

/* Experimental Code: Each pulse can be synthesized as a sum of
 * SIMPERMS segments of the long simulated timeseries array.  By
 * averaging lots of segments we smooth the significant power
 * fluctuations that are found locally within the long data series.
 */
#if 0
#define SIMPERMS 1		/* Disable with '1' */
EXTERN SINT4 iSimPerms_c[ SIMPERMS ] ;
#endif /* #if 0 */

/* ------------------------------
 * The following variables manage the record/play menu.  The
 * LRECORDING_C and LPLAYING_C variables are private, and are
 * written only through the update routines whose prototypes are
 * given here.
 */
EXTERN UINT2 lrec_play_menu_managed_c ;

EXTERN UINT1 irec_play_mode_c ;	/* Current mode, if managed. */
#define REC_MODE   1
#define PLAY_MODE  2

EXTERN UINT2 lrecording_c ;	/* "Record" button is pressed */
void  update_record_menu( UINT4 irecord_progress_a ) ;

EXTERN FLT4 fplay_pause_c ;	/* Pause to insert at the end of each play */
EXTERN FLT4 fplay_period_c ;	/* Desired time between playback rays (sec) */

EXTERN char splay_filename_c[32] ;
EXTERN UINT2 lplaying_c ;	/* "Play" button is pressed */
EXTERN SINT4 iplay_ray_c ;	/* Current ray being played */
void  init_play_menu( void ) ;
void  update_play_menu( void ) ;

#define LPLAY_SELECTED   ( (lrec_play_menu_managed_c != 0        ) && \
			   (irec_play_mode_c         == PLAY_MODE) && \
			   (splay_filename_c[0]      != '\0'     )  )

#define LRECORD_SELECTED ( (lrec_play_menu_managed_c != 0        ) && \
			   (irec_play_mode_c         == REC_MODE )  )

/* ------------------------------
 * Boolean flags describing what needs to be displayed.
 */
EXTERN UINT2 lasdata_c ;	/* Processed data should be displayed */
EXTERN struct dsp_data_mask AsDataMask_c ; /* Selected parameters (when lasdata_c is set) */

EXTERN UINT2 lasseries_c ;	/* Time series should be displayed */
EXTERN UINT2 lasspectrum_c ;	/* Spectrum processing should be done */
EXTERN UINT2 lasatod_c ;	/* Time series vs. range is desired */
EXTERN UINT2 lMagSpecDots_c ;	/* Draw power dots on Mag(spectrum) plot */

/* ------------------------------
 * Variables that defined the geometry and graphical placement.
 */
EXTERN SINT4 iasplotwidthmax_c ;  /* Plot area width */
EXTERN SINT4 iasplotheightmax_c ; /*  height current max */

/* Gap between the plots in pixels.  Notice that the boxes are
 * drawn one pixel outside of the plots, so the number of blank
 * lines between the plots will be three less than this number.
 */
EXTERN SINT4 iasplotgap_c ;

/* Array which holds the starting points and heights for each
 * of the plots (top to bottom).
 */
EXTERN SINT4       istarts_c[ASC_PLOTTYPES_SIZE][2] ;
EXTERN SINT4      iheights_c[ASC_PLOTTYPES_SIZE] ;
EXTERN SINT4 ititleHeights_c[ASC_PLOTTYPES_SIZE] ;

/* The number of plots to draw (must be between 1 and 4)
 */
EXTERN SINT4 iasplotcount_c ;

/* Index of a plot that should be made bigger than the others
 */
EXTERN SINT4 ibig_c ;
EXTERN char  sloaded_filename_c[17];
EXTERN char  sloaded_sitename_c[17];

/*****************************************
 **                                      *
 **     Function Prototypes              *
 **                                      *
 ******************************************/

/**************************************
 * ascopeapplshell.c
 *************************************/
void ascopeSetSensitive( UINT2 lsen_a ) ;
void setupButtons(void) ;
void startMovie(void) ;
void setupButtons(void) ;
#ifdef SIGMET_DSP_H
void updateAntenna(const SINT2 itags[],
   const struct gparm *pGparm_a);
#endif  /* #ifdef SIGMET_DSP_H */
void updateTitle
( const char *setup_filename_a, const struct ymds_time *time_a, 
  const char *sTZName_a, const char *ssitename_a ) ;
void updateSensSimulatorPB(UINT2 lsens_a) ;
void updateSensTargetSimulatorPB(UINT2 lsens_a) ;
void updateTopLevelDisp(void) ;
void initValues( char *sfile_a ) ;
void fileSelected_ftn( char *file_selected ) ;
void setup_gc(void) ;
void moveRngStrobe_act(int x, int y) ;
void updateGenSetupTxt(void) ;
void setGainControl(const char *gainContStr) ;
void setOneSample(void) ;
void setContSample(UINT4 contSample) ;
void setPlotParam(void) ;
void moveRngStrobeOne(UINT2 lmove_right_a);

void asrangewindow( FLT4 fmaxrange_a, Dsp *pdsp_a ) ;
void asrangestrobe( FLT4 frange_a ) ;
void asrangerecord( void ) ;
void asrangebincnt( void ) ;

void managePlaybackLab(UINT2 lflag_a);
void updateMaxRngTxt(FLT4 fmax_rng_a);
void updateNumBinTxt(UINT4 in_bin_count_a);
void updatePrfTxt(UINT4 iasprf_a);
void updateDualPrfTxt(UINT4 iprfmode_a);
void updatePulseWidth(UINT4 ipw_number_a) ;
void updatePolarization(UINT4 ipolar_mode_a);


/**************************************
 * threshold.c
 *************************************/
void createThresholdMenu(swidget wgt_a) ;
void popupThresholdMenu(void) ;
void popdownThresholdMenu(void) ;

/**************************************
 * gaincontrol.c
 *************************************/
void createGainControlMenu(swidget wgt_a) ;
void popupGainControlMenu(void) ;
void popdownGainControlMenu(void) ;

/**************************************
 * noise.c Used for RVP7/8
 *************************************/
void createNoiseMenu(swidget wgt_a) ;
void popupNoiseMenu(void) ;
void popdownNoiseMenu(void) ;
#ifdef SIGMET_DSP_H
void updateNoise( const struct gparm *pGparm_a ) ;
#endif /* #ifdef SIGMET_DSP_H */

/**************************************
 * sampnoise.c
 *************************************/
void createSampleNoiseMenu(swidget wgt_a) ;
void popupSampleNoiseMenu(void) ;
void popdownSampleNoiseMenu(void) ;

#ifdef SIGMET_DSP_H
void updateOffsets( const struct gparm *pGparm_a ) ;
#endif /* #ifdef SIGMET_DSP_H */

/**************************************
 * gaincontrol.c
 *************************************/
void createGainControlMenu(swidget wgt_a) ;
void popupGainControlMenu(void) ;
void popdownGainControlMenu(void) ;

/**************************************
 * plotparam.c
 *************************************/
void  createPlotParamMenu(swidget wgt_a) ;
void  popupPlotParamMenu(void) ;
void  popdownPlotParamMenu(void) ;
void  setPlotParamMenu(void) ;
UINT2 lPlotTypeValid(UINT1 iplot_type_a); 

/**************************************
 * gensetup.c
 *************************************/
void createGenSetupMenu(swidget wgt_a) ;
void popupGenSetupMenu(void) ;
void popdownGenSetupMenu(void) ;
void initGenSetupMenu(void) ;
void updateMajorMode(UINT4 imajor_mode_a);
void updatePhaseMod(UINT4 iphase_seq_a);


/**************************************
 * dspparamerrors.c
 *************************************/
void createDspParamErrors(swidget wgt_a) ;
void popupDspParamErrors(void) ;
void popdownDspParamErrors(void) ;
#ifdef SIGMET_DSP_H
void checkDspParamErrors(const struct gparm *pGparm_a) ;
#endif  /* #ifdef SIGMET_DSP_H */

/**************************************
 * simulator.c
 *************************************/
void createSimulatorMenu(swidget wgt_a) ;
void popupSimulatorMenu(void) ;
void popdownSimulatorMenu(void) ;

/**************************************
 * target simulator.c
 *************************************/
void createTargetSimulatorMenu(swidget wgt_a) ;
void popupTargetSimulatorMenu(void) ;
void popdownTargetSimulatorMenu(void) ;

/**************************************
 * spectra.c
 *************************************/
void createSpectraMenu(swidget wgt_a) ;
void popupSpectraMenu(void) ;
void popdownSpectraMenu(void) ;

/**************************************
 * recplay.c
 *************************************/
void createRecPlayMenu(swidget wgt_a) ;
void popupRecPlayMenu(void) ;
void popdownRecPlayMenu(void) ;

/**************************************
 * asloop.c
 *************************************/
void asloop(void) ;

MESSAGE asloadup
( Dsp *dsp_a,
  SINT4 ibins_a, SINT4 isamps_a, FLT4 frange_a, SINT4 irangeavg_a,
  UINT2 *lchange_a, UINT2 *lempty_a, SINT4 *inoise_status_a ) ;

/* Reads the YMDS time structure from the most recent RVP8 tags. */
void GetYmdsFromTags
( struct ymds_time *pYmds_a );

/**************************************
 * asplotalloc.c
 *************************************/
void asplotalloc
(SINT4 iheightmax_a, SINT4 igap_a, SINT4 iplotcount_a,
 SINT4 istarts_a[][2], SINT4 iheights_a[], SINT4 ibig_a) ;

void aspositionlabel( void ) ;

/**************************************
 * asinitloadsave.c
 *************************************/
void initStatFromScratch( void ) ;
void loadStat( char *path_and_filename_a ) ;
void loadStatFromStat( struct asc_stats *loadstat_a ) ;
void saveStat(char *filename) ;
void initStatFromDspInfo( void ) ;
void openDspForIO( void ) ;
char *splotname( SINT4 iplottype_a ) ;
SINT4 iAscopeSpectrumSize( void ) ;

/**************************************
 * asoneplot.c
 *************************************/
void asoneplot
( SINT4 iy_a, SINT2 idata_a[], SINT4 icount_a,
  FLT4 fgain_a, SINT2 ioffset_a, SINT4 iheight_a, 
  SINT4 lplotall_a, SINT4 idistortion_a, SINT2 iarray_a[]) ;

/**************************************
 * asrealplot.c
 *************************************/
void asrealplot
( SINT4 iy_a, FLT4 fdata_a[], SINT4 icount_a, 
  FLT4 fgain_a, FLT4 foffset_a, SINT4 iheight_a,
  SINT4  lplotall_a, SINT2 iarray_a[] ) ;

/**************************************
 * asrsimdata.c
 *************************************/
void asrsimdata( SINT2 idata_a[], SINT4 icount_a, SINT4 iHeaderSize_a ) ;

/**************************************
 * asrsimseries.c
 *************************************/
void asrsimseries(SINT2 idata_a[], SINT4 icount_a, SINT4 iHeaderSize_a) ;

/**************************************
 * asrsimatod.c
 *************************************/
void asrsimatod( SINT2 idata_a[], SINT4 icount_a, SINT4 iHeaderSize_a ) ;

/**************************************
 * asrsimpram.c
 *************************************/
#ifdef SIGMET_DSP_H
void asrsimpram( struct gparm *pGparm_a) ;
#endif /* #ifdef SIGMET_DSP_H */

/**************************************
 * asspecacc.c
 *************************************/
void asspecacc( UINT2 lspec_a, void * input_a[],
		FLT4 fspectrum_a[], FLT4 fphases_a[],
		UINT2 lfirst_a, SINT4 itsbins_a ) ;

/**************************************
 * asdataplot.c
 *************************************/
void asdataplot
( SINT2 ibuf_a[], FLT4 fseries_a[], FLT4 *fspectrum_a, FLT4 *fphases_a,
  FLT4 fatod_a[], FLT4 fwinfix_a, SINT2 itags_a[] ) ;

SINT4 iDspParamOffset( SINT4 iPlotType_a ) ;

/**************************************
 * assimulate.c
 *************************************/

void tsgen_simTargetSeries( void ) ;

void tsgen_preparePulse( SINT4 nBins_a, SINT4 nPulses_a ) ;

void tsgen_makePulse
( SINT4 iSimBins_a, SINT4 iPulse_a, SINT4 iTsTerm_a,
  BIN2 *piTxPhase_a, SINT4 *piTxPower_a,
  struct dspSimPoint pSimPts_a[MAXSIMBINS] ) ;

#undef EXTERN
#endif /* #ifndef ASCOMMON_H */
#endif /* #ifndef MESSAGE_ONLY */
