/* show.h
 * ======
 *
 * ver 1.00
 *
 * This is a collection of routines
 * related to showing data on RCP console in real time
 * for a generalized RCP, "Nasu"
 * 
 *				by Sessai@NIPR
 */

/* standard headers
 * ================ */

//#include <stdio.h>
//#include <stdlib.h>
//#include <sys/types.h>
//#include <sys/name.h>

/* Headers for the control library
 * =============================== */

//#include "radops.h"
//#include "fitdata.h"
//#include "log_error.h"


/* global variable(s)
 * ================== */

int fitOK;


/* ---------------------------------
 * data display routines.. by Sessai
 * ---------------------------------
 * void show_data_list
 *	// (FILE *fp,struct rawdata *raw_data,struct fitdata *fit_data,
 *	(FILE *fp,struct rawdata *raw_data,struct fitdata *fit_data,int nrang,
 *	 short int start_freq,short int freq_range,int fitOK,int verbose);
 * void show_raw_parms
 *	(FILE *fp,struct rawdata *raw_data);
 * void show_fit_parms
 *	(FILE *fp,struct fitdata *fit_data);
 *
 */

/* void show_data_list
 *	// (FILE *fp,struct rawdata *raw_data,struct fitdata *fit_data,
 *	(FILE *fp,struct rawdata *raw_data,struct fitdata *fit_data,int nrang,
 *	 short int start_freq,short int freq_range,int fitOK,int verbose);
 *
 *	This is just a routine to show data list on a console(stderr)
 *	which can be used in RCP's to check getting data in real time
 *
 *	input values
 *		FILE *fp
 *			: output file pointer, e.g. stderr
 *		struct rawdata *raw_data
 *			: put "&raw_dt_buf" in RCP...
 *		struct fitdata *fit_data
 *			: put "&fit_dt_buf" in RCP...
 *		int nrang
 *			: nrang in RCP (NRANG in default.h)
 *		short int start_freq
 *			: start_freq in RCP itself
 *		short int freq_range
 *			: freq_range in RCP itself
 *		int fitOK
 *			: fitOK in RCP
 *				which means
 *					0 : fit data is NOT ready
 *					1 : fitacf was succeeded
 *				which is defined in RCP as follows...
 *					fitOK=((f=get_fit(...))!=frame_counter);
 *		int verbose
 *			: verbose mode which you like
 *				0 : nothing done, nothing displayed...
 *				1 : only display the FCLR result
 *				2 : display FCLR and RAW max_pwr0 result
 *				3 : display FCLR, RAW max_pwr0 and FIT result
 *				4 : in addtion, show RAW pwr0 bar display!
 *				5 : in addtion, show FIT qflg-gsct bar display!
 */
void show_data_list
	// (FILE *fp,struct rawdata *raw_data,struct fitdata *fit_data,
	(FILE *fp,struct rawdata *raw_data,struct fitdata *fit_data,int nrang,
	 short int start_freq,short int freq_range,int fitOK,int verbose) {

	/* static variables to keep the last results!... */
	static short int last_max_rp0_range =  0;

	int min_power,max_rp0_range,k,n;
	double min_powerF;
	int max_fp0_range,ngood,ngsct;
	char fitOKc[2]={'X','@'};
	char qfc[3]={'.','o','g'}; /* char qfc[3]={'.','@','G'}; */
	char qfcs[90];
	char rp0c[13]={'.','0','1','2','3','4','5','6','7','8','9','!','-'};
	char rp0cs[90];
	char tmpstr[2];
	long dumL;
	static char *bmstr[3]={"Beam","SPBM","    "};

  if(verbose <= 0) return;

  if(verbose >= 3){

	min_powerF=0.0;
	max_fp0_range=0;
	ngood=0;
	ngsct=0;
		strcpy(qfcs,"");
		tmpstr[1]='\0';
	//for(k=0; k<fit_data->prms.NRANG; k++){...}
	for(k=0; k<nrang; k++){
		if(fit_data->rng[k].p_0>min_powerF){
			min_powerF = fit_data->rng[k].p_0;
			max_fp0_range = k;
		}
		if(fit_data->rng[k].qflg != 0) ngood++;
		if(fit_data->rng[k].gsct != 0) ngsct++;

			if(k%10 == 0){
				tmpstr[0]= '0' + k/10;
				strcat(qfcs,tmpstr);
			}
			tmpstr[0]=
			 qfc[ fit_data->rng[k].qflg + fit_data->rng[k].gsct ] ;
			strcat(qfcs,tmpstr);
	}
			//if(raw_data->PARMS.NRANG%10 == 0){...}
			if(nrang%10 == 0){
				//tmpstr[0]= '0' + raw_data->PARMS.NRANG/10;
				tmpstr[0]= '0' + nrang/10;
				strcat(qfcs,tmpstr);
			}

    if(verbose >= 5){
	qfcs[80]=NULL;
	fprintf(fp,"%s\n",qfcs);
    }

	if((fit_data->prms.SCAN==0)||(fit_data->prms.SCAN==1))
				strcpy(bmstr[2],bmstr[0]);
			else	strcpy(bmstr[2],bmstr[1]);
	fprintf(fp,
"FIT Last %s%2d %2d:%02d:%02d FP0 %10.3f(R%2d,Q%d)FPL%8.1f(R%2d,Q%d) fit%c%2d(g%d)\n",
		bmstr[2],fit_data->prms.BMNUM,
		fit_data->prms.HOUR,fit_data->prms.MINUT,fit_data->prms.SEC,
		fit_data->rng[last_max_rp0_range].p_0,
		last_max_rp0_range,
		fit_data->rng[last_max_rp0_range].qflg,
		fit_data->rng[max_fp0_range].p_l,
		max_fp0_range,
		fit_data->rng[max_fp0_range].qflg,
		fitOKc[fitOK],
		ngood,
		ngsct
		);
  }

	if((raw_data->PARMS.SCAN==0)||(raw_data->PARMS.SCAN==1))
				strcpy(bmstr[2],bmstr[0]);
			else	strcpy(bmstr[2],bmstr[1]);
	fprintf(fp,
"FCLR Now %s%2d %2d:%02d:%02d SF%5d dF%4d TF%5d NS%8ld<%7ld AT%2d ER%2d\n",
		bmstr[2],raw_data->PARMS.BMNUM,
		raw_data->PARMS.HOUR,raw_data->PARMS.MINUT,raw_data->PARMS.SEC,
		start_freq,freq_range,
		raw_data->PARMS.TFREQ,raw_data->PARMS.NOISE,
		raw_data->PARMS.LVMAX,
		raw_data->PARMS.ATTEN * 10,raw_data->PARMS.ERCOD);

  if(verbose >= 2){
	min_power=0L;
	max_rp0_range=0;
		strcpy(rp0cs,"");
		tmpstr[1]='\0';
	//for(k=0; k<raw_data->PARMS.NRANG; k++){...}
	for(k=0; k<nrang; k++){
		if(raw_data->pwr0[k]>min_power){
			min_power = raw_data->pwr0[k];
			max_rp0_range = k;
		}
    if(verbose >= 4){
			if(k%10 == 0){ strcat(rp0cs," "); }
			n=0;
			dumL=raw_data->pwr0[k];
			if(dumL<0L){n=12;}
			else{
				while((dumL !=0L) && (n<12)){
					dumL = dumL/10L;
					n++;
				}
			}
			tmpstr[0]=rp0c[n];
			strcat(rp0cs,tmpstr);
    }
	}

	if((raw_data->PARMS.SCAN==0)||(raw_data->PARMS.SCAN==1))
				strcpy(bmstr[2],bmstr[0]);
			else	strcpy(bmstr[2],bmstr[1]);
	fprintf(fp,
"RAW  Now %s%2d %2d:%02d:%02d txp%dk intt%d MRG  %2d P0%8ld<%ld st%2d cp%d\n",
		bmstr[2],raw_data->PARMS.BMNUM,
		raw_data->PARMS.HOUR,raw_data->PARMS.MINUT,raw_data->PARMS.SEC,
		raw_data->PARMS.TXPOW,
		raw_data->PARMS.INTT,
		max_rp0_range,
		raw_data->pwr0[max_rp0_range],
		raw_data->PARMS.MXPWR,
		raw_data->PARMS.ST_ID,
		raw_data->PARMS.CP
		);

	last_max_rp0_range = max_rp0_range;

    if(verbose >= 4){
	rp0cs[80]=NULL;
	fprintf(fp,"%s\n",rp0cs);
    }
  }

}

/* void show_raw_parms
 *	(FILE *fp,struct rawdata *raw_data);
 *
 *	This is just a modified version of print_param in support.lib.
 *						by Sessai@NIPR
 *
 */
void show_raw_parms(FILE *fp,struct rawdata *raw_data) {
   int i,j;
   fprintf(fp,"%d/%d/%d %d:%d:%d\n",
		raw_data->PARMS.YEAR,raw_data->PARMS.MONTH,raw_data->PARMS.DAY,
		raw_data->PARMS.HOUR,raw_data->PARMS.MINUT,raw_data->PARMS.SEC);
   fprintf(fp,"stid%2d,",raw_data->PARMS.ST_ID);
   fprintf(fp,"nparms%d,",raw_data->PARMS.NPARM);  
   fprintf(fp,"txpow%d,",raw_data->PARMS.TXPOW);
   fprintf(fp,"atten%d,",raw_data->PARMS.ATTEN);
   fprintf(fp,"intt%d,",raw_data->PARMS.INTT);
   fprintf(fp,"txpl%d,",raw_data->PARMS.TXPL);
   fprintf(fp,"mpinc%d,",raw_data->PARMS.MPINC);
   fprintf(fp,"mppul%d,",raw_data->PARMS.MPPUL);
   fprintf(fp,"mplgs%d\n",raw_data->PARMS.MPLGS);
// fprintf(fp,"nrang%2d,",raw_data->PARMS.NRANG);
   fprintf(fp,"frang%d,",raw_data->PARMS.FRANG);
   fprintf(fp,"rsep%d,",raw_data->PARMS.RSEP);
   fprintf(fp,"bmnum%2d,",raw_data->PARMS.BMNUM);
// fprintf(fp,"xcf%d,",raw_data->PARMS.XCF);
   fprintf(fp,"tfreq%5d,",raw_data->PARMS.TFREQ);
   fprintf(fp,"scan%d\n",raw_data->PARMS.SCAN);
   fprintf(fp,"mxpwr%d,",raw_data->PARMS.MXPWR);
   fprintf(fp,"lvmax%d,",raw_data->PARMS.LVMAX);
   fprintf(fp,"noise%d,",raw_data->PARMS.NOISE);	/* added by Sessai */
   fprintf(fp,"cp%d\n",raw_data->PARMS.CP);

   fprintf(fp,"pulse table = ");
   for (i=0;i<raw_data->PARMS.MPPUL;i++) 
     fprintf(fp,"%d ",raw_data->PULSE_PATTERN[i]);
   fprintf(fp,"\n");

  fprintf(fp,"lag table = ");
   for (i=0;i<2;i++) {
	if(i==1)fprintf(fp,"            ");
     for (j=0;j<raw_data->PARMS.MPLGS;j++) 
       fprintf(fp,"%2d ",raw_data->LAG_TABLE[i][j]);
     fprintf(fp,"\n");
  }
}

/* void show_fit_parms
 *	(FILE *fp,struct fitdata *fit_data);
 *
 *	This is just a fitdata version of show_raw_parms...
 *						by Sessai@NIPR
 *
 */
void show_fit_parms(FILE *fp,struct fitdata *fit_data) {
   int i,j;
   fprintf(fp,"%d/%d/%d %d:%d:%d\n",
		fit_data->prms.YEAR,fit_data->prms.MONTH,fit_data->prms.DAY,
		fit_data->prms.HOUR,fit_data->prms.MINUT,fit_data->prms.SEC);
   fprintf(fp,"stid%2d,",fit_data->prms.ST_ID);
   fprintf(fp,"nparms%d,",fit_data->prms.NPARM);  
   fprintf(fp,"txpow%d,",fit_data->prms.TXPOW);
   fprintf(fp,"atten%d,",fit_data->prms.ATTEN);
   fprintf(fp,"intt%d,",fit_data->prms.INTT);
   fprintf(fp,"txpl%d,",fit_data->prms.TXPL);
   fprintf(fp,"mpinc%d,",fit_data->prms.MPINC);
   fprintf(fp,"mppul%d,",fit_data->prms.MPPUL);
   fprintf(fp,"mplgs%d\n",fit_data->prms.MPLGS);
// fprintf(fp,"nrang%2d,",fit_data->prms.NRANG);
   fprintf(fp,"frang%d,",fit_data->prms.FRANG);
   fprintf(fp,"rsep%d,",fit_data->prms.RSEP);
   fprintf(fp,"bmnum%2d,",fit_data->prms.BMNUM);
// fprintf(fp,"xcf%d,",fit_data->prms.XCF);
   fprintf(fp,"tfreq%5d,",fit_data->prms.TFREQ);
   fprintf(fp,"scan%d\n",fit_data->prms.SCAN);
   fprintf(fp,"mxpwr%d,",fit_data->prms.MXPWR);
   fprintf(fp,"lvmax%d,",fit_data->prms.LVMAX);
   fprintf(fp,"noise%d,",fit_data->prms.NOISE);	/* added by Sessai */
   fprintf(fp,"cp%d\n",fit_data->prms.CP);
}