/********************************************
Copyright Pavel Potapov, 2013-2016
distributed under GNU General Public License
********************************************/


/***********************CHECK********************************/

if(doesfunctionexist("setcamerabinning112"))
	{
	result("\nfunctions "+"InLine112"+" already installed\n");
	throw("")
	}	
result("\ninstall functions "+"InLine112")


/************************************************
Function: Camera Manager path
************************************************/
string Path112_CM() return "CameraManager"
string Path112_CCL() return Path112_CM()+":CameraConfigurationList"
string Path112_HLP(number CameraID,string Mode,string SubMode)
{
string Path = Path112_CCL()
Path += (":["+CameraID+"]")
Path += ":Acquisition Parameter Sets"
Path += ":Imaging"
Path += (":"+Mode)
Path += (":"+SubMode)
Path += ":High Level"
return Path
}


/******************************************************************
function : Set binning /exposure in Camera Manager Tags
******************************************************************/
void setCameraBinning112(string Mode,string SubMode,number Binning)
	{
	number CameraID
	if(GetPersistentNumberNote(Path112_CM()+":Current Camera",CameraID))
		{
		SetPersistentPointNote(Path112_HLP(CameraID,Mode,SubMode)+":Binning",Binning,Binning)
		}
	}
void setCameraExposure112(string Mode,string SubMode,number exp)
	{
	number CameraID
	if(GetPersistentNumberNote(Path112_CM()+":Current Camera",CameraID))
		{
		SetPersistentNumberNote(Path112_HLP(CameraID,Mode,SubMode)+":Exposure (s)",exp)
		}
	}

/******************************************************************
function : TimeOut
******************************************************************/
void tOut112(number t0,number TimeOut,string Message)
{
if(CalcOSSecondsBetween(t0,GetOSTickCount( ))>TimeOut)
	{
	throw("Time out with "+Message)
	}
}

/******************************************************************
function : Ensure valid live window in in-line DM
******************************************************************/
documentWindow GetWindowByTitle112(string name)
{
documentwindow win
string CurrName,fullname
number N = CountDocumentWindowsofType(5)
//result("\nN "+N)
for(number i=0;i<N;i++) 
	{
	win = GetNthDocumentWindowOfType(5,i)
	if(win.WindowGetType()==5) 
		{
		fullname = Win.WindowGetTitle()
		number pos = find(fullname,":")
//result("\n#"+fullname+"# "+pos)
		CurrName = right(fullname,len(fullname)-pos-2)
		if(CurrName==Name) return win
//result("\n#"+name+"#")
		}	
	}
	
return NULL
}

/******************************************************************
function : Ensure valid live ImageDocument in in-line DM
******************************************************************/
imagedocument getImageDocumentByTitle112(string name)
{
number TOt =10
number t0=getOStickCount()

documentwindow win //= NULL
while(!win.WindowIsValid())
	{
	try{
	win =GetWindowByTitle112(name);
		} 
	catch{break;}
	
	tOut112(t0,Tot,name)
	}
//result("\ngot window")
imagedocument imdoc = win.ImageWindowGetImageDOcument()

if(imdoc.ImageDocumentIsValid()) return imdoc

else {result("\ninvalid doc");return NULL;}
}


/******************************************************************
function : Record
******************************************************************/
image Record112(number tR,number bR,number CCDwidth)
{
	number w = CCDwidth/bR
	number h = CCDwidth/bR
	
	if(!doesfunctionexist("sscgainnormalizedbinnedacquire")) throw("Function 'sscgainnormalizedbinnedacquire' not found")
	image im := sscgainnormalizedBinnedAcquire(tR,bR,0,0,h,w);
	while(!im.ImageIsValid()) yield()
	
return im
}


/******************************************************************
function : Name in Menu to restart liveView
******************************************************************/
string RestartMenu112() return "Search"


/******************************************************************
function : start live FFT from  image document
******************************************************************/
imageDocument CreateFFTfromImageDocument112(ImageDocument imdoc)
	{
	if(!imdoc.ImageDocumentIsvalid()) throw("No FrontImage to build FFT !!")
	documentWindow imwin = imdoc.ImageDocumentGetWindow()	//To examine: Is window always valid when imageDocument valid ?
	
	imagedisplay imdisp = imdoc.ImageDocumentGetImageModeDisplay()
	ROI sel = imdisp.ImageDisplayGetRoi(0)
	image FFTim := NewLiveFFT(imdisp,sel,0)
	FFTim.showimage()
	imageDocument FFTdoc = FFTim.ImageGetOrCreateImageDocument()
	
	return FFTdoc
	}
