number t,dx,dy,cFlag
number sizeX,sizeY,sizeZ,sx,sy,sz,z;
number mval,mx,my,firstNumber;
Image img1,img2,imgT;
string fileName,fileBase,comment;
number thickCount = 10;
number defCount   = 10;
number tdsCount   = 20;
number it,idef,itds
T_setverbose(0)

If (!OpenDialog(fileName))
Throw("No file name selected");

Result("File: "+fileName+"\n");
GetNumber("Number of TDS runs (img_[Ntds]_[t]_[def].img):",tdsCount,tdsCount);
GetNumber("Number of different thicknesses (img_[Ntds]_[t]_[def].img):",thickCount,thickCount);
GetNumber("Number of different defoci (img_[Ntds]_[t]_[def].img):",defCount,defCount);


Image img = T_readImg(filename,t,dx,dy,cFlag,comment);

fileBase = "img_"; // left(filename,len(filename)-7);
Result("File: "+fileBase+"\n");


img.GetSize(sx,sy);
realImage tgt = Realimage("Thickness_Defocus_MapTDS",4,sx*defCount,sy*thickCount);
tgt = 0;

for (it=0;it<thickCount;it++) {
	for (idef=0;idef<defCount;idef++) {
		for (itds=0;itds<tdsCount;itds++) {
			filename = fileBase+itds+"_"+it+"_"+idef+".img"
			// Result("Reading "+filename+"\n");
	
			img = T_readImg(filename,t,dx,dy,cFlag,comment);
			tgt[it*sy,idef*sx,(it+1)*sy,(idef+1)*sx] += real(img);
		}
	}
	Result("Thickness: "+(0.1*t)+" nm\n");
}
tgt.setname("Thickness_Defocus_Map_TDS");
tgt.setscale(0.1*dx,0.1*dy);
tgt.setunitstring("nm");
showimage(tgt)
	
