Subject: precor

From: Andrew Fruchter <[email-masked]>
Date: Mon, 08 Sep 1997 13:07:22 -0400

procedure precor(inlist)

string	inlist 	{prompt="Image(s) for cosmic ray cleaning"}
string  kern_siz  {"5",prompt="Convolution kernel size -- 3 or 5 pixels" }
int     min_pix  {16,prompt="required number of significant pixels"} 
real    min_val  {3.0,prompt="minimum value considered significant"}
int     NGROUP   {4,prompt="number of groups in image"}
bool    do_sky    {yes,prompt="run cgsky on the input image?"}
bool    do_sqrt   {no,prompt="use sqrt image?"}
bool	verbose  {yes,prompt="Verbose output?"}
struct	*inimglist

begin

	string	ilist,infile,img,hold,hold2,outimg,imcalc_in,outname
        string in_name,out_name,holdfile,hold1,imstr,find_kern,save_kern
	string imcalf,imcals
	int ii,jj

# Get query parameters.

	ilist = inlist

# Expand file lists into temporary files.

	infile =  mktemp("tmp$pre_cor")
	sections (ilist,option="fullname",>infile)
	inimglist = infile

# set up kernels
	if (kern_siz == "3") {
            if (min_pix > 9) {
               error (0,"if kern_siz = 3 then min_pix must be <= 9")
               }
	    find_kern="1 1 1"
	    save_kern = "1 1 1 1 1"
            }
	else if (kern_siz == "5") {
            if (min_pix > 25) {
               error (0,"if kern_siz = 5 then min_pix must be <= 9")
               }
	    find_kern="1 1 1 1 1" 
	    save_kern = "1 1 1 1 1 1 1"
            }
	else {
	    error (0,"kernel size must be 3 or 5")  
	    }

# Loop through input files:

	while (fscan(inimglist,img) != EOF) {

# Strip extension off input file name.

		fileroot (img,validim+)
		img = fileroot.root

		if (verbose) print ("Working on ",img )

# Main deriv loop:

		convolve.bilinear=yes
		outimg = img//"_obj"
#		print (outimg)
                gcopy(img,outimg,groups="ALL",verbose=no)
                hedit(outimg,"ORG_TITL",img,add=yes,verify=no)
                if (do_sky) cgsky(outimg,subsky=yes)
	 	for (ii=1; ii<=NGROUP; ii+=1) {
		   holdfile=mktemp("tmp$pre_cor")
		   hold = mktemp("tmp$pre_cor")
		   hold1 = mktemp("tmp$pre_cor")
		   hold2 = mktemp("tmp$pre_cor")
		   imcals = "if (im1 .lt. 70) then im1 else sqrt(abs(im1 - 70))+70"
                   in_name = outimg//"["//ii//"]"
		   out_name=outimg//"["//ii//"]"
#		   print (out_name)
                   imcopy(in_name,holdfile)
#		   print("about to imcalc")
#		   print (holdfile)
                   imstr = "if (im1 .ge. "//min_val//") then 1 else 0"
		   imcalc(holdfile,hold,imstr,verbose=no)
#       set up kernel for finding convolution
		   convolve.xkernel=find_kern
		   convolve.ykernel=find_kern
		   convolve(hold,hold)
#		   print (hold)
                   imstr = "if (im1 .ge. "//min_pix//") then 1 else 0"
		   imcalc(hold,hold1,imstr)
#       set up kernel for save convolution
                   convolve.xkernel=save_kern
                   convolve.ykernel=save_kern
		   convolve(hold1,hold1)
#		   print (hold1)
		   imcalc(hold1,hold2,"if (im1 .gt. 0) then 1 else 0")
#		   print(hold2)
		   imarith(hold2,"*",holdfile,holdfile)
                   if (do_sqrt) {
                      imcalc(holdfile,out_name,imcals)
		   }
		   else {
		   gcopy(holdfile,out_name)
		   }
		   imdel (holdfile)
		   imdel (hold)
		   imdel (hold1)
		   imdel (hold2)
		}  
	}

end