program AD_SA1

int TRACE_SEL;
assign TRACE_SEL to "FBLAS:TRACE:SEL";

unsigned short GETTRACE_A[701];
unsigned short GETTRACE_B[701];

float SPECTRUM_A[701];
float SPECTRUM_B[701];

assign GETTRACE_A to "FBLAS:GETTRACE:A";
assign GETTRACE_B to "FBLAS:GETTRACE:B";
assign SPECTRUM_A to "FBLAS:SPECTRUM:A";
assign SPECTRUM_B to "FBLAS:SPECTRUM:B";

int GETYSCALE;
assign GETYSCALE to "FBLAS:GETYSCALE";

int GETDIV;
assign GETDIV to "FBLAS:GETDIV";

double GETREF;
assign GETREF to "FBLAS:GETREF";

int SWTRIG;
assign SWTRIG to "FBLAS:SWTRIG";
monitor SWTRIG;

int SWTRIG_OLD;

%{
static int i;
static int j;
static double conv_fac1;
static double xdb[8];
static double conv_fac2;
}%

ss state_all
{
	state init
	{
		when()
		{
		pvGet(SWTRIG);
		SWTRIG_OLD = SWTRIG;
		xdb[0] = 10.0;
		xdb[1] = 5.0;
		xdb[2] = 2.0;
		xdb[3] = 1.0;
		xdb[4] = 0.5;
		xdb[5] = 0.2;
		xdb[6] = 0.1;
		xdb[7] = 10.0;
		}state trace_monitor
	}
	state trace_monitor
	{
		when(SWTRIG_OLD != SWTRIG)
		{
		SWTRIG_OLD = SWTRIG;
		pvGet(TRACE_SEL);
		pvGet(GETTRACE_A);
		pvGet(GETREF);
		pvGet(GETDIV);
		pvGet(GETYSCALE);

		%{
		j = GETYSCALE;
		if (j>6) j=0;
		if (GETDIV ==0) 
		{
			conv_fac1 = 8.0*xdb[j]/400.0;
			conv_fac2 = GETREF - 8.0*xdb[j];
		}
		else
		{
			conv_fac1 = 12.0*xdb[j]/400.0;
			conv_fac2 = GETREF - 12.0*xdb[j];
		}
		

		}%

		if (TRACE_SEL == 0) 
		{
			for (i=0; i<701;i++)
			{
			SPECTRUM_A[i]=GETTRACE_A[i]*conv_fac1+conv_fac2;
			}
			pvPut(SPECTRUM_A);
		}
		else
		{
			for (i=0; i<701;i++)
			{
			SPECTRUM_B[i]=GETTRACE_A[i]*conv_fac1+conv_fac2;
			}
			pvPut(SPECTRUM_B);
		}
		}state trace_monitor
	}
}