function sxt_deleak, index, data, lkind, lkdat,imap,xdata=xdata,$ yn=yn,debug=debug,synsfc=synsfc,timer=timer ;+ ; ; PROGRAM NAME ; sxt_deleak ; PURPOSE ; Second order straylight correction. Add/subtract enough ; of the SFC leak image to force the average signal in the ; corners of the sxt_prep'd FFI to become zero. ; SAMPLE CALLING SEQUENCE ; output = sxt_deleak(index,data,yn=yn) ; INPUT ; index, index structure of SSC datafile ; data, SSC floating point image array ; OPTIONAL KEYWORD INPUT ; /synsfc, causes use of synthetic SFCs for 2nd order correction ; /debug, stops program on each image processing cycle ; /timer, causes print to screen of time sxt_deleak took to run. ; xdata, array of raw SXT images for missing minor frame checks. ; OUTPUT ; Array of second-order leak corrected images. ; OPTIONAL INPUT/OUTPUT ; lkind, the SFC index(s) ; lkdat, the SFC leak image(s) ; imap, vector relating which SFC goes with which SSC ; OPTIONAL KEYWORD OUTPUT ; yn, array flagging images to which 2nd order leak ; correction has been applied or not needed. ; 0 if no correction ; + or - value of average corner correction ; in DN/pix if correction made. ; Correction applied if residual corner signal ; greater than .01 DN/pix. ; PROCEDURE (For images taken without the NuDen filter.) ; Add/subtract enough of the SFC leak image, after approximate ; correction for x-ray scattering, to force the average signal i ; in the periphery of the composite image to become zero. ; Details: ; 1. Compute DN/pix inside 1.1 Rsun of SSC, ds. ; 2. Approximate scattered light (DN/pix) outside of ; 1.7 Rsun from the power-law expression ; sl = 0.00657*ds^0.962. ; 3. Determine SSC signal (DN/pix) outside of 1.7 Rsun ; in SSC image, dc, in DN/pix. ; 4. Subtract sl from dc to determine residual corner ; signal, rc, in DN/pix. ; 5. Subtact or add enough SFC to SSC to force rc ==> 0. ; PROCEDURE (For images taken WITH the NuDen filter.) ; Call NuDen_2nd.pro and subtract average of rows 12/res through 42/res ; from all rows of the leak-corrected image. Sounds dumb but ; works quite well for normal pointing. ; PROGRAMS CALLED ; get_leak_image, sxt_cen, shift_res, get_rb0p, gt_pix_size, gt_res, ; blank_circle, gt_expdur, total, abs, NuDen_2nd ; HISTORY ; 16-Aug-99 LWA, Mod. of cfix.pro to correct for x-ray scatter. ; 10-Jan-2000 - S.L.Freeland - case where user supplied ; (avoid get_leak_image call) ; 07-Oct-2002 - T. Metcalf - Fixed indexing bug ; 2-Jan-2004 - L. Acton - Fixed errors in handling of QR images. ; Added capability to use SynSFCs. ; 5-Jan-2004 - LWA - Debugged synsfc keyword. ; 10-Jan-2004 - LWA - Added keyword "timer" to quiet program. ; 27-Jan-2004 - LWA - Added test for missing minor frames in data. ; 29-Jan-2004 - S.L.Freeland - fix typo (strtrm->strtrim) ; 30-Jan-2004 - LWA - Fixed case of missing minor frame at top of image. ; 9-Feb-2004 - LWA - Fixed case of missing minor frame at bottom of image. ; 24-Feb-2004 - LWA - Added protection against darks, cals, & optical filters. ; 16-Mar-2004 - LWA - Corrected bug in filt_a test that prevent 2nd order correction. ; 25-Mar-2004 - LWA - Deal with case of no SynSFC coefffile, get TermSFC. ; 1-Apr-2004 - LWA - Incorporated special correction for images using NuDen filter. ; 16-Apr-2004 - SLF - change NuDen_2nd->nuden_2nd.pro ; 3-Sep-2004 - LWA - Added step to remove Cosmic Ray spikes from second order ; background test areas beyond 1.7 Rsun. ; 1-May-2006 - LWA - Added code to discard saturated pixels in test areas ; if xdata array is available. ; 3-May-2006 - LWA - Removed old debug stop. ; 31-Jul-2009 - LWA - Installed some special leak processing for eclipses of ; 24-Oct-95, 17-Apr-96, 26-Feb-98, and 16-Feb-99. ; 3-Aug-2009 - LWA - Deal with total sun sig (eclipse) lt 0. ; Commented out ALL special eclipse processing. ; 6-Aug-2009 - LWA - Removed all special eclipse processing commands ; except for, if sun lt 0 then sun=0.0. ; 80May02013 - LWA - Added /quiet in call to get_rb0p. ;- start_time = systime(1) run_time = 0. synsfc=keyword_set(synsfc) or get_logenv('sxt_syn_sfc') ne '' ; IF NECESSARY, READ IN THE LEAK DATA case 1 of data_chk(lkind,/struct) and data_chk(lkdat,/nimage) eq 1: imap=0 n_elements(imap) eq 0 and synsfc eq 0: $ get_leak_image, index,lkind,lkdat,imap else: endcase ; SET UP THE SUNCENTER AND SOLAR RADIUS ARRAYS sunr = 1.1 ;Hardwired radius for disk intensity. refr = 1.7 ;Hardwired radius outside of which signal should be 0. xy = sxt_cen(index) xy0 = xy for i=0,1 do xy(i,*) = xy(i,*)/(2.*gt_res(index)) rr = get_rb0p(index,/radius,/quiet) rr = rr/gt_pix_size(index) out = data yn = fltarr(n_elements(index)) for i = 0,n_elements(index)-1 do begin if gt_expmode(index(i)) ne 0 then goto, JUMP if (gt_filta(index(i)) eq 2 or gt_filta(index(i)) eq 3 or $ gt_filta(index(i)) eq 4 or gt_filta(index(i)) eq 5) then goto, JUMP res = gt_res(index(i)) if res eq 0 then begin print,' *** PROGRAM ONLY GOOD FOR QR AND HR FFIs, YOU ' print,' *** HAVE INCLUDED AN FR IMAGE IN THE INPUT.' print,' *** SKIPPING THIS IMAGE.' goto,JUMP endif ; --- If filter_a is NuDen then skip down and run NuDen_2nd on this image. if gt_filta(index(i)) ne 6 then begin if res eq 2 then img=data(0:255,0:255,i) else img=data(*,*,i) iisun = blank_circle(512/res,512/res,xy(0,i),xy(1,i),sunr*rr(i),720.,/image) sun = total(img*(iisun ne 1))/n_elements(where(iisun ne 1)) ;Disk signal. if sun lt 0 then sun=0.0 ;LWA 3-Aug-09 refimg = blank_circle(512/res,512/res,xy(0,i),xy(1,i),refr*rr(i),/image) refimg(*,0:(10/res)) = 1 ;Eliminate edge effects. refimg(0,*) = 1 refimg((512/res)-1,*) = 1 iiref = where(refimg ne 1) ; ---- If raw data cube supplied check for missing minor frames ---- if n_elements(xdata) gt 0 then begin sskp=0 nrow=511/gt_res(index(i)) ngdok=8/gt_res(index(i)) datatest=total(xdata(0:nrow,0:nrow,i),1) ngd=where(datatest eq 0,nngd) case 1 of nngd eq 0 : sskp=0 nngd gt 0 and nngd le ngdok : sskp=1 ;One minor frame. nngd gt ngdok : sskp=2 endcase if sskp eq 0 then img=img if sskp eq 2 then begin print,'Image ',fmt_tim(index(i)),' has too much missing data to correct.' goto,JUMP endif if sskp eq 1 then begin zz=where(datatest eq 0) case 1 of max(zz) eq nrow : begin ;Missing minor frame at top of image. for iz=0,n_elements(zz)-1 do img(*,zz(iz))=img(*,zz(0)-1) end max(zz) lt nrow and min(zz) gt 0 : begin ;Missing minor frame within image. yy=(1./(n_elements(zz)+1))+(findgen(n_elements(zz))/(n_elements(zz)+1)) img0=img for ir=0,nrow-1 do begin col=img(ir,*) p=[col(zz(0)-1),col(zz(n_elements(zz)-1)+1)] nn=interpolate(p,yy) img(ir,zz)=nn endfor end min(zz) eq 0 : begin ;Missing minor frame at bottom of image. for iz=0,n_elements(zz)-1 do img(*,zz(iz))=img(*,max(zz)+1) end endcase endif ; ---- Discard saturated pixels in test areas ---- iicorns=where(refimg ne 1) satimg=xdata(0:nrow,0:nrow,i) sats=where(satimg(iicorns) eq 255,nsats) if nsats gt 0 then refimg(iicorns(sats))=1 endif ; ---- Remove CR spikes from corners ---- corns=img*(refimg ne 1) smoothy=big_smooth(corns,/medsmooth,/noccd,5) res=gt_res(index) case 1 of (res eq 0) : level=10^(0.5*0.75) (res eq 1) : level=10^(1.0*0.75) (res eq 2) : level=10^(1.5*0.75) endcase spykz=where(abs(corns-smoothy) gt level,np) if np gt 0 then corns(spykz)=smoothy(spykz) ; ---- Go ahead and do the second-order correction ---- ref=total(corns)/n_elements(iiref) scat = 0.00657*sun^0.962189 ;Correct corner sigs for scatter. ref = ref - scat ;Should be zero if leak_sub was perfect. if abs(ref) gt .01 then begin case 1 of synsfc eq 0 : begin limg = lkdat(*,*,imap(i)) if res eq 2 then limg=limg(0:255,0:255) lkref = total(limg*(refimg ne 1))/n_elements(iiref) ;Corner signals in SFC. nx = n_elements(limg[*,0]) & ny = n_elements(limg[0,*]) out(0,0,i) = data(0:nx-1,0:ny-1,i) - (ref/lkref)*limg ;2nd order leak corr. end synsfc eq 1 : begin mk_syn_sfc,index(i),lind,limg,synstatus=synstat if synstat eq 0 then begin ;Case of no coefficient file. get_leak_image,index(i),lind,limg endif if res eq 2 then limg=rebin(limg,256,256) lkref = total(limg*(refimg ne 1))/n_elements(iiref) nx = n_elements(limg[*,0]) & ny = n_elements(limg[0,*]) out(0,0,i) = data(0:nx-1,0:ny-1,i) - (ref/lkref)*limg end else : print,' SOMETHING wrong with synsfc keyword!' endcase endif endif else begin nx=512/res out(0,0,i) = nuden_2nd(index(i),data(0:nx-1,0:nx-1,i),row=row) ref = -average(row) endelse yn(i) = -ref ;Average correction in reference areas, DN/pix. if keyword_set(debug) then stop JUMP : ;Didn't do second_order leak correction. endfor end_time = systime(1) run_time = (end_time-start_time)/60. if keyword_set(timer) then begin print, 'SXT_DELEAK took: ', run_time, ' minutes to process your request' endif return, out end