// $BACKGROUND$
// Acquisition parameters
Number nEDSCh      = 4096;
Number nEDSDisp    = 10;
Number nEDSAcqTime = 10;
Number nDelay = 0.1; //Sec
Number nMaxDataSet = 1000;

//main
Number nW,nH;
Image imgP
if(!GetOneImage("QED Tilting Parameter",imgP)) exit(0);
imgP.GetSize(nW,nH);
if(nW != 2) Exit(0);
nH = min(nH,nMaxDataSet);

Object oQED = alloc(QED_OpenClass).init();
oQED.QEDC_setFrags(1,0,0);	//IllShift, Descan, Non-Liner Descan
oQED.QEDC_SaveOriginalBeamSettings();

Number nTiltXmrad,nTiltYmrad;
Image imgOut  = RealImage("ALCHEMI Stack",4,nEDSCh,nH);
Image imgDisp = RealImage("Current EDS",4,nEDSCh);
imgDisp := EDSAcquireSpectrum( nEDSCh, nEDSDisp, nEDSAcqTime );
showimage(imgDisp);

try
{
	for(number i;i<nH;i++){
		nTiltXmrad = imgP.GetPixel(0,i);
		nTiltYmrad = imgP.GetPixel(1,i);
		oQED.QEDC_BeamTilt(nTiltXmrad,nTiltYmrad) // Set tilting angle with mrad.
		sleep(nDelay);
		imgDisp = EDSAcquireSpectrum( nEDSCh, nEDSDisp, nEDSAcqTime );
		imgOut[i,0,i+1,nEDSCh] = imgDisp;
		if(ShiftDown()){
			if(OkCancelDialog( "Stop Acquiring?" )){
				break;
			}
		}
	}
}
recover
{
	Number origin,scale,calibrationFormat;
	String units;
	imgDisp.ImageGetDimensionCalibration( 0, origin, scale, units, calibrationFormat);
	imgOut.ImageSetDimensionCalibration(  0, origin, scale, units, calibrationFormat);
	showimage(imgOut);
	oQED.QEDC_ResetBeamSettings();
}

