{
This program produce radial distribution of the ED image
from Ratio images 
Before start of this program need to run General_RATany4.v

Output files: 2D arrays (coloumn corresponding to delay, and row to pixel number)
<Path\Radial_1D\TRED> - combined files of intensities at set of delays <FULL(LEFT,RIGHT)>.tif
<Path\Radial_1D\TRED> - corresponding combined files of 3x standard of average 
					    <sigmaFULL(sigmaLEFT,sigmaRIGHT)>.tif

Output files: 1D arrays
<Path\Radial_1D\RAW\> - files <F(L,R)><delay>.txt
<Path\Radial_1D\TRED> - combined files of intensities at set of delays <FULL(LEFT,RIGHT)>.txt
<Path\Radial_1D\TRED> - corresponding combined files of 3x standard of average 
					    <sigmaFULL(sigmaLEFT,sigmaRIGHT)>.txt

created 12/02/2001 by V.Lobastov Ver. 01.
}

const
	 P ='D:\DATA\Utilities\Delays\*.ini'; 		 	  //default directory for INI file
//  	 P = 'D:\DATA\04072001\*.ini';	 
	 	 
//------------> Global variables
var
	Data;
    SamName;									//name of the file header   
    Size_x, Size_y; 		  	 	  		 	//image size
	
	NS,NF, im, curIm;
	Sel, delay, FL;
	Sub, Out, InIMG;
	Path, PathU, PathR, PathOUT;
	FL0;
	RefIm, DifIm, Movie; 
	REF, REF1,REF2, Mov;
	M512, II_Mask;
{
******************************************************************
********************** function Select ***************************
******************************************************************
This function return a full file name for a file 
}	 	 
function SELECT;											
		var
   		   nName; Name, ix;
   		   Choice, nDIR;
		begin
	 		 Name:=FindFirstFile(P, fa_Archive);
	 		 nName:=ExtractFileName(Name);		
	 		 nDIR:= ExtractFilePath(Name);			//extract the path
			 ix:=0;
	  		 
			 while StrLen(Name) > 0 do

	  		 	   begin
	  	   		   		Name:=ExtractFileName(FindNextFile) ;
		   				nName:=nName+';'+Name;
					 if ix>100 then halt('Too many Files in Directory');   
					 	ix:=ix+1;
	 				end; 

	 			 if SelectString( 'Select File',nName , Choice ) >= 0 then
	 			 	begin
			 			 SELECT:=nDIR+Choice;
	 				end	
	 			else
	 				begin 
	 	 	  			  WriteInfo('No Selection made! Restart program! ');	
		 	  			  halt;
	 				end; 
		end;
//----------------------> end of function SELECT <-----------------------------------			


{
 ************************************************************************
 ************************************************************************
 ***                                                                  ***
 ***	                      Main program							  ***
 ***																  ***
 ************************************************************************ 
 ************************************************************************
}

begin

    {-------> Get Input Data Images}
//-----------------------> Initialization parameters<---------------------------------------

	 		Sel:=SELECT;												 // selector for initialization file

	 			 Path:= ReadPrivateINIString( Sel, 'Paths', 'Path');	 		//read a base path				 					 				 			 				 				 		 			 	 			 
	 			 PathU:= ReadPrivateINIString( Sel, 'Paths', 'PathU');	  		//read a path	for utilities Masks
				 M512:=ReadPrivateINIString( Sel, 'Images', 'Mask');  	  		 //read a mask file name				 				 
				 InIMG:=ReadPrivateINIString( Sel, 'Paths', 'OutIMG');				//subdirectory for Input Ratio Images					 				 

				 OUT:=ReadPrivateINIString( Sel, 'Paths', 'DIF');		 
				 if (StrLen(FindFirstFile( Path+InIMG+OUT+'*.txt', fa_Archive)) = 0) then
				 CreateDir(Path+InIMG+OUT);			 		  				  	   	   //create subdirectory for output
				 
				 NS:=Val(ReadPrivateINIString( Sel, 'TotalDelays', 'First'));	   //first frame to process
				 NF:=Val(ReadPrivateINIString( Sel, 'TotalDelays', 'Last'));	   //last frame to process
				 Mov:=ReadPrivateINIString( Sel, 'Images', 'Movie');	   //first frame to process

				 
//-----------------------> end of Initialization <------------------------------------------


//				GetNumber('Enter the REFERENCE image', REF);		   
//	   		   	FL0:=ReadPrivateINIString( Sel, 'FileNames', 'delay'+Str(REF));				
//				Open(Path + InIMG + 'NR' + FL0 +'.tif',RefIm); 			  //								

   		    	 Open(PathU + M512, II_Mask);					
              if IsNull( II_Mask ) then halt( 'No Reference image was selected' ) ;
                   Size_x:=GetXSize(II_Mask);
                   Size_y:=GetYSize(II_Mask);					  
				   Delete(II_Mask);
			  
				REF1:=1;
				REF2:=1;
				GetNumber('Enter the FIRST reference ', REF1);
				GetNumber('Enter the LAST  reference', REF2);				
	   			RefIm:=CreateImage(Single,Size_x,Size_y);
  
   for im:=REF1 to REF2 do
   	   begin   
			   		   delay:='delay'+Str(im);   
	   		   		   FL:=ReadPrivateINIString( Sel, 'FileNames', delay);		 	   		

					   Open(Path + InIMG + 'NR' + FL +'.tif',curIm); 			  //
					   SamName:=FL;
					   RefIm:=RefIm+curIm;
		end;			
						RefIm:=RefIm/(REF2-REF1+1);				   

	   //------> Browse trough files in directory defined in Path+InIMG
	   			Movie:=CreateImage(Single,Size_x,Size_y,NF); 
   for im:=NS to NF do
   	   begin   
			   		   delay:='delay'+Str(im);   
	   		   		   FL:=ReadPrivateINIString( Sel, 'FileNames', delay);
		 	   		
					   Open(Path + InIMG + 'NR' + FL +'.tif',curIm); 			  //
					   SamName:=FL;
					   DifIm:=curIm - RefIm;
					   Save(DifIm,Path+InIMG+OUT+'D'+FL+'.tif');
					   Show(DifIm, 'D'+FL);	
					   Movie[..,..,im-1]:=DifIm;		
		end;			
							Delete(curIm);
							Delete(RefIm);
							Delete(DifIm);

			if (REF1 = REF2) then DeleteFrame(Movie,0);						

					Save(Movie,Path+InIMG+OUT+Mov);
					Show(Movie, Mov);
					
							Free(Movie);	
			        PlaySound( 'C:\Lib_V++\Mac.wav' ) ;							
					WriteInfo('Calculation completed!');

//------> free memory					
end
