#
#     IRAF Task to correct for the interquadrant row cross 
#     talk effect in SOFI.
#     NOTA BENE: The task must be run in the ctio package!
#
#     For first time IRAF users: 
#     -- to define the task use
#          task crosstalk=crosstalk.cl
#     -- to starts the task: 
#          crosstalk @input_list @output_list
#     where input@list and output_list are the lists with 
#     input and output file names, one file name per line.
#
#     -- Created: Leo Vanzi December 2000
#     -- Modified: V.D. Ivanov, 01-02-2005: added ability to 
#     handle image lists and a check if the ctio package is 
#     loaded.
#

procedure crosstalk(input,output)

string    input    {prompt="list of input images"}
string    output   {prompt="list of output images"}
struct   *ilist
struct   *olist

begin

string inname,outname,inlist,outlist

# check to see if onedspec is loaded
if (!defpac("ctio")) {
  print ("Please load the ctio package and try again.  Thank you.")
  bye
  }

# The following lines allow wildcards, @lists and comma-delimited 
# image lists to be used - consistent with regular IRAF tasks
   inlist=mktemp('tmp$tmp')
   files(input,sort+,>inlist)
   ilist=inlist
   outlist=mktemp('tmp$tmp')
   files(output,sort+,>outlist)
   olist=outlist

# Loop through image list and correct for the corsstalk
   while(fscan(ilist,inname)!=EOF)
      if(fscan(olist,outname)!=EOF){

        #  calculate the first part of the correction
        blkavg (input=inname,output="temp_sum.fits",option="sum",b1=1024,b2=1)
        imarith ("temp_sum.fits","*",1.4e-5,"temp_alpha.fits")
        blkrep ("temp_alpha.fits","temp_corr_A.fits",1024,1)
        
        #  calculate the second part of the correction
        imcreate("temp_corr_B.fits",2,1024,1024)
        imcopy("temp_corr_A.fits[*,1:512]","temp_corr_B.fits[*,513:1024]")
        imcopy("temp_corr_A.fits[*,513:1024]","temp_corr_B.fits[*,1:512]")
        
        #  combine the 2 corrections
        imarith("temp_corr_A.fits","+","temp_corr_B.fits","temp_corr.fits")
        
        #  apply the correction
        imarith(inname,"-","temp_corr.fits",outname)
        
        #   delete the temporary images and files
        imdelete("temp*.fits",yes,ver-)

      }

end
