/*
  NewPwr1.C                              16 Feb '98  TP

	 GAC POWER LOOKUP TABLE GENERATOR PROGRAM

    -  This is a modified version of "gpwrs3b.c" used for generating
       the Power LT values for the simulation studies.
    -  generates Six bit Power values in text format (ie. the power
       values vary from 0 to 63).
    -  reads the configuration from SimList.cfg
    -  screen prints are restricted.     24feb97tp
    -  works with genABCP1b,m data file. 24feb97tp
    -  can be used to watch the Look up computation steps.
    -  0.003 power maintained as the (middle) Table-7 (See Ooty Log books !).
 = ORIGINAL source from
	  "pwr-r2b.c"  of   24/1/97, which is again a GMRT version of
	   the  "PWR-1H.C" logic used in Ooty GAC.

 _______________________________________________________about PWR-R2B.C ___:
 PWR-R2B.C  was last modified on 20 October 96
    This version is aimed for generating the Power Lookup tables which can be
    used for masking one or both the Pols of any given Dish. Since Pols are
    coming time multiplexed from FFT cards, if they have to be "Masked", it
    will have to be done at real time, using these table.

 How Table-0 is generated?
     TABLE-0: (newly introduced this time 20 Oct 96).
     Table 0 has all zeros. This is generated by making the
     PwrU[0] >= (98*2)*64; in (98 is Power max; 2 -accounts
     for round-off; 64 is #of levels) the main() program

 How to POL MASKING carried out with this table?:
      Pol masking can be achieved by choosing always Table-0
      in the GAC RAM for that corresponding Dish.

 Make every EVEN locations Zero in the RAM for masking Pol-1.
	-do-     ODD  locations    -do-          -do-       Pol-2.

 TABLE-1 to TABLE-15: (same as before-15mar96 but a mistake
    is identified and removed in the table generation routine )

    In this set of tables Table 1 to 15 are the Root-2 tables with 0.003
    (or P003) located at Table 7 . Any table below table 7 (ie 6,5,4,..1)
    are at Root-2 step below its previous table. (or at 1.5 dB down in power
    level ). Any table above 7 (ie. 8,9,10,..15) are in Root-2 step above
    its previous table (or at 1.5 dB up in power level).

 How to produce the Final PROM files ?

    After the above file is generated
    concatanate this ALL16U16.BIN  file with this file. This will make
    the Power Proms second half as Diagnostics bank.

	command    r2table2.bin > P003-r2b.bin
		   ALL16U16.BIn >> P003-r2b.bin
						  :   GK and TP  20 Oct 96

 Some notes from Pulsar Receiver log note book p# 69 dtd. Nov 20/92

   Peak value (PWR_SAT) of 0.001 is required to SEE  -2.5 dBm Noise level
   PWR_SAT =1.25 will work well for  0 dBm CW
   This module computes the Power Lookup Values for a Window
   between PWR_SAT_L and PWR_SAT_U
*/

#include<stdio.h>
#include<math.h>
#include<fcntl.h>   /* check availabilty in UNIX */

#define  stdprn NULL
#define  getch() ;

/* ---------------------------------------------Definie CONSTANTS Here---*/
#define  Q_LEVELS 64  /* Power 6 bits; 64 Quantization levels */
#define  P003 0.003
#define  TABELS 16
#define  OUTFILE "PwrTabl1.txt"    /* main data file in TXT format*/
#define  OUTFILE2 "PwrTabl1.tx2"   /* doc file contains S# T#,a,b,c, and P */
/* ----------------------------------------------------------------------*/
#define NOTE_1 " New GAC POWER LOOKUP TABLE Generator program  :TP-17feb98"


/* ----------------------------------------------GLOBAL  Declarations---*/
void  generate();

	double x,y,e,bl,P[Q_LEVELS],
		  actual_x,
		  PWR_SAT_U,
		  PWR_SAT_L,
		  old_level,
		  step_size, PwrU[16] ;
	int lines,a,b,c,d,Adr,i,TableNo,op[4096] ;
	int ma,mb ; /* magnitudes */
	FILE  *out,*out2;
	int r1,i1,e1,tno1,I1;
	unsigned long int samp;

/* ----------------------------------------------function find_power()---*/
void find_power(int r1,int i1,int e1, int Tno, long int sn)
{

	if(fmod(sn,1024)==0)
	 {   printf("%6ld processing ",sn);
	     printf(" (R=%2d I=%2d  E=%2d) Table=%3d ]\n",r1,i1,e1,Tno);
	 }
	/* Finding the Upper Saturation values for all the 16 tables */
	 e=1.0 ;
	 for(i=8;i<=15;i++)
	 {    e = e*sqrt(2.0) ;
	      PwrU[i] = (float)(P003)*e ;
	      /* printf("gtop %d:   %f  e: %f\n",i,PwrU[i],e); */
	 }

	 PwrU[7] =P003 ;    /* 7th Table operates at the P003 power level */
			    /* Ooty table */
	 /*  printf("gtop %d:   %f\n",7,PwrU[7]); */

	 e= 1.0 ;
	 for(i=6;i>=1;i--)
	 {    e = (float)e*sqrt(2.0) ;
	      PwrU[i] = ((float)P003)/(float)e ;
	      /* printf("gtop %d:   %f  e:%f\n",i,PwrU[i],e); */

	 }

	 PwrU[0] =15000 ;/* This will force all values as zeroes for the
			    0th Table; And this table can be used for masking
			    one or both Pols of a specific Dish.
			    Oct 20-96 GK,TP  */

	/* Generate the table contents */

	TableNo= Tno ;

	    PWR_SAT_U =  PwrU[TableNo]    ; /*  15 Mr 96    */
	    PWR_SAT_L =  0.0000                  ;


/*	printf("\nEnter Power Upper Saturation Value :  "); */
/*	scanf("%f ",&PWR_SAT_U ) ;                          */
/*	printf("\nEnter Power Lower saturation Value :  "); */
/*	scanf("%f ",&PWR_SAT_L ) ;                          */

	    generate();

} /*  end of function find_power()  */

/* --------------------------------------------- function generate() ----- */
void generate()
{       int q_pwr, nc ;

	bl = (PWR_SAT_U - PWR_SAT_L) /(float)Q_LEVELS ; /* Find the STEP Size */

/*	fprintf(stdprn,"POWER Lookup Table \nDiff. Quantization   Levels\n\n"); */

	/* Computing the Quantization step values */
	old_level = PWR_SAT_L ;
	step_size = 0 ;
	x=0 ;
	for(c=0; c<(Q_LEVELS); c++)
	{
		P[c] = PWR_SAT_L + (c+1)*bl ;

	/*	printf("%d  %x  	%4.15f\n",c,c,P[c]); */
		x = P[c] ;
	}
	getch();

	lines =0;

	/* Computing the Power Values & the Lookup values     */

 /* Address generation  is not    required in this routine 24/02/97tp  */
    a=abs(r1); b=abs(i1); c=abs(e1); /* Instead this line is required
					to assign the values of a,b,c */

				Adr =((256*c)+(16*b)+a);
				op[Adr]=0;
				q_pwr=0;
				ma = a&0x7 ;   /* sign bits removed */
				mb = b&0x7 ;   /*       -do-       */

	   actual_x = ((float)((ma*ma)+(mb*mb)))/pow(2.0,(float)(2.0*c)) ;
	   nc=2*c ;

				/* Half step size  is added for rounding-off    */
				x = actual_x + (bl/(float)2.0000000000000) ;

				for(d=0; d<( Q_LEVELS-1 ); d++)
				{ if( (float)x >= (float)P[d])
				  { op[Adr]=d+1;
				    q_pwr =d+1;
				   }
				 }
				 fprintf(out,"%d\n",q_pwr);
fprintf(out2,"Adr:%5ld  T:%2d (E:%2d I:%2d R:%2d) P:%d\n",samp-1,TableNo,e1,i1,r1,q_pwr);
} /* end of fn. generate() */

/* ------------------------------------------- Main Program starts here ----*/
void main(int argc, char *argv[])
{
  int tno2;

  fprintf(stdout,"\nRunning : %s\n\n%s \n\n\n", argv[0], NOTE_1);

  out=fopen(OUTFILE,"wt");
  out2=fopen(OUTFILE2,"wt");
  fprintf(out2,"\nRunning : %s\n\n%s \n", argv[0], NOTE_1);
  fprintf(out2,"This doc. file Name: %s \n", OUTFILE2);
  fprintf(out2,"Corresp. TXT format PROM file Name: %s\n\n", OUTFILE) ;

  samp=1;                      /* Generate the LT values for */
  for(tno2=0;tno2<=15;tno2++)  /* all 16 Tables, for all combination of the */
    for(e1=0;e1<=15;e1++)      /*   EXP part, */
      for(i1=0;i1<=15;i1++)    /*   Imaginary part, */
	for(r1=0;r1<=15;r1++)  /*  and Real parts. */
	{  find_power(r1,i1,e1,tno2,samp);
	   samp++;
	}

  printf("\nSaved all values\nExiting\n\n");
  fcloseall();
} /* ---------------------------------------- End of main Program -------- */








