# Copyright(c) 2002-2003 Association of Universities for Research in Astronomy, Inc. procedure gnsskysub(inimages) # Generate a sky subtracted nod & shuffled image. # # Version Sept 20, 2002 RA,IJ v1.4 release # Feb 25, 2003 MB: generalized the loop over sciext for 6amp mode # May 09, 2003 IJ, removed all the old code # Jun 03, 2003 IJ check sci,1 for binning char inimages {"",prompt="Input GMOS images or list"} char outimages {"",prompt="Output images or list"} char outpref {"n",prompt="Prefix for output images"} bool fl_fixnc {no,prompt="Auto-correct for nod count mismatch?"} char logfile {"",prompt="Logfile"} bool verbose {yes,prompt="Verbose?"} int status {0,prompt="Exit status (0=good)"} struct* scanfile {"",prompt="Internal use only"} begin char l_inimages, l_outimages, l_logfile, l_dir, l_outpref bool l_verbose, l_fl_fixnc struct l_struct int l_test, i, k, ninp, nout, nbad #*# added k as a variable char img, filelist, newfilelist, temp1, temp2, temp3, temp4, tmpfile, l_temp int shuffle, binning, anodcnt, nsci, bnodcnt,dummy, maxfiles char inimg[200], outimg[200] # Initialize status = 0 maxfiles = 200 # Localize global variables l_inimages=inimages; l_outimages=outimages; l_logfile=logfile l_verbose=verbose; l_outpref=outpref l_fl_fixnc=fl_fixnc # Keep task parameters from changing from the outside cache("imgets") # Create any needed temporary file names filelist=mktemp("tmpfile2") newfilelist=mktemp("tmpfile3") tmpfile=mktemp("tmpfile4") # Test the logfile: if (l_logfile == "" || stridx(" ",l_logfile)>0) { l_logfile = gmos.logfile if (l_logfile == "" || stridx(" ",l_logfile)>0) { l_logfile = "gmos.log" printlog("WARNING - GNSSKYSUB: Both gnsskysub.logfile and gmos.logfile fields are empty",l_logfile,yes) printlog(" Using default file gmos.log",l_logfile,yes) } } # Start logging date | scan(l_struct) printlog("----------------------------------------------------------------------------",l_logfile,l_verbose) printlog("GNSSKYSUB -- "//l_struct,l_logfile,l_verbose) printlog("",l_logfile,l_verbose) printlog("Input list = "//l_inimages,l_logfile,l_verbose) printlog("Output list = "//l_outimages,l_logfile,l_verbose) printlog("",l_logfile,l_verbose) # Load up input name list: @list, * and ?, comma separated if (l_inimages == "" || stridx(" ",l_inimages)>0) { printlog("ERROR - GNSSKYSUB: Input file not specified",l_logfile,yes) goto crash } # Test for @filelist if (substr(l_inimages,1,1) == "@" && !access(substr(l_inimages,2,strlen(l_inimages))) ) { printlog("ERROR - GNSSKYSUB: Input list "//substr(l_inimages,2,strlen(l_inimages))//" does not exist",l_logfile,yes) goto crash } # parse wildcard and comma-separated lists if(substr(l_inimages,1,1)=="@") { scanfile=substr(l_inimages,2,strlen(l_inimages)) while(fscan(scanfile,l_temp) != EOF) { files(l_temp, >> filelist) } } else { if(stridx(",",l_inimages)==0) files(l_inimages, > filelist) else { l_test=9999 while(l_test!=0) { l_test=stridx(",",l_inimages) if(l_test>0) files(substr(l_inimages,1,l_test-1), >> filelist) else files(l_inimages, >> filelist) l_inimages=substr(l_inimages,l_test+1,strlen(l_inimages)) } } } scanfile="" ; delete(tmpfile,verify-, >>& "dev$null") scanfile=filelist # Define the input name list ninp = 0 nbad=0 while(fscan(scanfile, img) != EOF) { # split off directory path fparse(img,verbose-) img=fparse.root l_dir=fparse.directory # Must be there, but cannot be GEIS or OIF gimverify(l_dir//img) if(gimverify.status==0) { ninp = ninp+1 if (ninp > maxfiles) { printlog("ERROR - GNSSKYSUB: Maximum number of input images exceeded", l_logfile,verbose+) goto crash } inimg[ninp] = gimverify.outname//".fits" # now has path if relevant } else if (gimverify.status==1) { printlog("ERROR - GNSSKYSUB: Input image "//l_dir//img//" does not exist", l_logfile,yes) nbad+=1 } else { printlog("ERROR - GNSSKYSUB Input image "//l_dir//img//" is not MEF", l_logfile,yes) nbad+=1 } } # end while if(nbad!=0) { printlog("ERROR - GNSSKYSUB: "//nbad//" input files do not exist, or are the wrong type",l_logfile,verbose+) goto crash } if(ninp==0) { printlog("ERROR - GNSSKYSUB: No input images meet wildcard criteria", l_logfile,verbose+) goto crash } # At the end of all this inimg[ninp] now contains the input images incl. the directory path # Output name list: can be empty if prefix is defined, @list or comma-separated list if (stridx(" ",l_outimages)>0) l_outimages = "" if (substr(l_outimages,1,1) == "@" && !access(substr(l_outimages,2,strlen(l_outimages))) ) { printlog("ERROR - GNSSKYSUB: File "//substr(l_outimages,2,strlen(l_outimages))//" does not exist",l_logfile,yes) goto crash } files(l_outimages,sort-, > newfilelist) scanfile=newfilelist nout = 0 # If empty string, prefix must be defined if (l_outimages == "") { if (l_outpref == "" || stridx(" ",l_outpref)>0 ) { printlog("ERROR - GNSSKYSUB: Neither output name nor output prefix is defined", l_logfile,verbose+) goto crash } nout = ninp i = 1 while(i<=nout) { fparse(inimg[i],verbose=no) outimg[i] = l_outpref//fparse.root//".fits" gimverify(outimg[i]) if (gimverify.status!=1) { printlog("ERROR - GNSSKYSUB: Output image "//outimg[i]//" already exists", l_logfile, verbose+) nbad+=1 } i = i+1 } # end of while(i) loop } else { while(fscan(scanfile, img) != EOF) { gimverify(img) if (gimverify.status!=1) { printlog("ERROR - GNSSKYSUB: Output image "//img//" already exists", l_logfile,verbose+) nbad+=1 } nout = nout+1 if (nout > maxfiles) { printlog("ERROR - GNSSKYSUB: Maximum number of output images exceeded", l_logfile,verbose+) goto crash } outimg[nout] = gimverify.outname//".fits" } # end of while (img) loop } # end else # scanfile="" delete(newfilelist,ver-, >>& "dev$null") if(nbad>0) { goto crash } # At the end of all this outimg[nout] now contains the output images # Input and output number must be the same, if output names are defined if (ninp!=nout && l_outimages!="") { printlog("ERROR - GNSSKYSUB: Number of input and output images are not the same", l_logfile,yes) goto crash } # Run a quick check to see if the input images have been gprepared i = 1 while (i<=ninp){ imgets(inimg[i]//"[0]","NSCIEXT", >>& "dev$null") nsci=int(imgets.value) if (nsci==0) { printlog("ERROR - GNSSKYSUB: Keyword NSCIEXT not found in image",l_logfile,l_verbose) printlog("ERROR - GNSSKYSUB: File "//inimg[i]//" is not gprepared",l_logfile,l_verbose) printlog("ERROR - GNSSKYSUB: Please run gprepare on all input images",l_logfile,l_verbose) goto crash } i=i+1 } # Start making the output images. This is where all the action really is! i = 1 while (i<=ninp) { printlog("Operating on "//inimg[i]//" to create "//outimg[i],l_logfile,l_verbose) #get header information imgets(inimg[i]//"[0]","NODPIX") shuffle=int(imgets.value) imgets(inimg[i]//"[SCI,1]","CCDSUM") print(imgets.value) | scan(dummy,binning) shuffle = shuffle/binning imgets(inimg[i]//"[0]","ANODCNT") anodcnt=int(imgets.value) imgets(inimg[i]//"[0]","BNODCNT") bnodcnt=int(imgets.value) #check whether the nod counts match. if not we may want renormalize the input later if (anodcnt != bnodcnt){ printlog("WARNING - GNSSKYSUB: Nod counts for A and B positions do not match.",l_logfile,yes) if (l_fl_fixnc){ printlog("WARNING - GNSSKYSUB: Normalizing images to fix nod count mismatch.",l_logfile,yes) } else{ printlog("WARNING - GNSSKYSUB: Ignoring the nod count mismatch.",l_logfile,yes) } } if (!l_fl_fixnc){ #the nod counts match or the user has chosen not to fix a mismatch, so simply #clone the input images and use them as a starting point for the output image #create temporary file temp1=mktemp("tmpfile1") imdel(images=outimg[i],verify-,>& "dev$null") copy(input=inimg[i],output=outimg[i],verb-) ##------------------------------------------------------------ for(k=1;k<=nsci;k+=1){ #process extension k imdel(temp1,verify-,>& "dev$null") imshift(input=inimg[i]//"[SCI,"//str(k)//"]",output=temp1,xshift=0,yshift=shuffle) imarith(operand1=inimg[i]//"[SCI,"//str(k)//"]",op="-",operand2=temp1,result=outimg[i]//"[SCI,"//str(k)//",overwrite]") } #end k loop #delete temporary files imdel(temp1,verify-,>& "dev$null") } else{ # this is the more complicated version since we're going to try # to fix a mismatch in the nod counts. #create temporary files temp1=mktemp("tmpfile1") temp2=mktemp("tmpfile2") temp3=mktemp("tmpfile3") temp4=mktemp("tmpfile4") imdel(images=outimg[i],verify-,>& "dev$null") copy(input=inimg[i],output=outimg[i],verb-) for(k=1;k<=nsci;k+=1){ #process extension k imdel(temp1,verify-,>& "dev$null") imdel(temp2,verify-,>& "dev$null") imdel(temp3,verify-,>& "dev$null") imdel(temp4,verify-,>& "dev$null") imarith(operand1=inimg[i]//"[SCI,"//str(k)//"]",op="/",operand2=bnodcnt,result=temp1) imshift(input=temp1,output=temp2,xshift=0,yshift=shuffle) imarith(operand1=inimg[i]//"[SCI,"//str(k)//"]",op="/",operand2=anodcnt,result=temp3) imarith(operand1=temp3,op="-",operand2=temp2,result=temp4) imarith(operand1=temp4,op="*",operand2=(max(anodcnt,bnodcnt)),result=outimg[i]//"[SCI,"//str(k)//",overwrite]") } #end k loop #delete temporary files imdel(temp1,verify-,>& "dev$null") imdel(temp2,verify-,>& "dev$null") imdel(temp3,verify-,>& "dev$null") imdel(temp4,verify-,>& "dev$null") } printlog("",l_logfile,yes) i=i+1 } # exit status good goto clean # Exit with error subroutine crash: status=1 goto clean # clean up clean: delete(filelist//","//tmpfile,ver-, >>& "dev$null") delete(newfilelist//","//tmpfile,ver-, >>& "dev$null") scanfile="" #close log file if (status == 0) printlog("GNSSKYSUB exit status: good.",l_logfile,l_verbose) else printlog("GNSSKYSUB exit status: error.",l_logfile,l_verbose) printlog("----------------------------------------------------------------------------",l_logfile,l_verbose) end