program AD_SA2

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

unsigned short GETTRACE_A[1001];
unsigned short GETTRACE_B[1001];

float SPECTRUM_A[1001];
float SPECTRUM_B[1001];

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

int GETYSCALE;
assign GETYSCALE to "FBLAS2:GETYSCALE";

double GETREF;
assign GETREF to "FBLAS2:GETREF";

int SWTRIG;
assign SWTRIG to "FBLAS2: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] = 10.0;
		xdb[5] = 10.0;
		xdb[6] = 10.0;
		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(GETYSCALE);

		%{
		j = GETYSCALE;
		if (j>3) j=0;
		conv_fac1 = 10.0*xdb[j]/12800;
		conv_fac2 = GETREF - 10.0*xdb[j];
		}%

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