function xconvolve_make_mask, image1, image2, satcnts1, satcnts2, cobj1, cobj2,$ fwhm1, fwhm2, zeropoint1, zeropoint2, n_convolve=n_convolve, $ match=match, minmag=minmag, maxmag=maxmag, pixscale=pixscale, $ scalenew=scalenew, subsize=subsize, nobright=nobright, $ brightmag=brightmag, satmask=satmask ;+ ;NAME: ; XCONVOLVE_MAKE_MASK() ; ;PURPOSE: ; Make a mask to be used in xconvolve_subtract_images. ; ;EXPLANATION: ; Only nonsatuated pixels will be used in cross-convolution ; process. User can also choose to use just regions around matching ; stars in a selected range of magnitudes. For sparse field, this ; will greatly improve the computational speed for finding the ; proper convolution kernel. When the object lists are matched, the ; new image can also be scaled to match the flux of the reference. ; ;CALLING SEQUENCE: ; result = xconvolve_make_mask(image1, image2, satcnts1, satcnts2, ; cobj1, cobj2, fwhm1, fwhm2, zeropoint1, zeropoint2, ; [n_convolve=n_convolve, match=match, minmag=minmag, maxmag=maxmag, ; pixscale=pixscale, scalenew=scalenew, subsize=subsize, ; nobright=nobright, brightmag=brightmag, satmask=satmask]) ; ;INPUTS: ; image1 - a two-dimension reference image image array ; image2 - a new image with the same dimension as image1 ; satcnts1 - satuation level in image1 ; satcnts2 - satuation level in image2 ; cobj1 - an object list for the reference. Must be defined if ; /match, /scalenew or /nobright is set. Must an array of ; structures with the following tags defined: ra, dec, x, ; y (physical coordinates in the image), flags (objects ; are considered good if this value is less than 2), fwhm, ; m (magnitude). ; cobj2 - an object list for image2. Must be defined if ; /match or /scalenew is set. An array of structres with the ; same tags defined as cobj1. ; fwhm1 - typical fwhm of image1. Must be defined when /match, ; /scalenew or /nobright is set. This value is used to ; determine the size of mask around matching objects. ; fwhm2 - typical fwhm of image2. Same as above. ; zeropoint1 - magnitude zeropoint for image1. Must be defined when ; /scalenew is set. This is used to translate ; magnitude of objects to flux. ; zeropoint2 - magnitude zeropoint for image2. Same as above. ; ;OPTIONAL INPUT KEYWORDS: ; n_convolve - size of the convolution kernel. Default value is 9. ; /match - mark regions around matched objects. Most of the ; background will not be considered when fitting the ; convolution kernel. ; minmag - minimum magnitude for matching objects. Used when /match ; or /scalenew is set. Default is 12.5. ; maxmag - maximum magnitude for matching objects. Used when /match ; or /scalenew is set. Default is 16.5. ; pixscale - scale of the pixel size in unit of degree. Used when ; /match or /scalenew is set to match the detections ; between images. The default value if 0.0009d for ; ROTSE-III. ; /scalenew - scale the new image(image2) to match the flux level ; of the reference(image1). ; subsize - size of the subregion. Used when /scalenew is ; set. Default is 400. ; /nobright - mask out the regions around bright stars where the ; response might be nonlinear. ; brightmag - objects brighter than this magnitude will be rejected ; when /nobright is set. Default is 12. ; ;OUTPUTS: ; result - a 2-dimension mask that is to be used in ; xconvolve_subtract_images. Pixels marked as 1 will be used. ; ;OPTIONAL OUTPUTS: ; satmask - a 2-dimension bit mask with satuated pixels set to 0. ; ;REVISION HISTORY: ; written by Fang Yuan, Dec 2007 ;- str0='syntax - mask = ' str1='xconvolve_make_mask(image1, image2, satcnts1,satcnts2, cobj1,' str2=' cobj2, fwhm1, fwhm2, zeropoint1, zeropoint2, n_convolve=n_convolve,' str3=' match=match, minmag=minmag, maxmag=maxmag, pixscale=pixscale,' str4=' scalenew=scalenew, subsize=subsize, nobright=nobright, brightmag=brightmag,' str5=' satmask=satmask)' if n_params() eq 0 then begin print, str0,str1,str2,str3,str4,str5, format='(/a,a/a/a/a/a)' return,'' endif if (keyword_set(match) or keyword_set(scalenew) or keyword_set(nobright)) $ and (n_elements(cobj1) lt 1 or n_elements(fwhm1) ne 1 or n_elements(fwhm2) ne 1) then begin print, str0,str1,str2,str3,str4,str5, format='(/a,a/a/a/a/a)' print,'cobj1, fwhm1 and fwhm2 must be defined when /match, /scalenew or /nobright is set.' return,'' endif if (keyword_set(match) or keyword_set(scalenew)) and n_elements(cobj2) lt 1 then begin print, str0,str1,str2,str3,str4,str5, format='(/a,a/a/a/a/a)' print,'cobj2 must be defined when /match, /scalenew is set.' return,'' endif if keyword_set(scalenew) and (n_elements(zeropoint1) eq 0 or n_elements(zeropoint2) eq 0) then begin print, str0,str1,str2,str3,str4,str5, format='(/a,a/a/a/a/a)' print,'zeropoint1 and zeropoint2 must be defined when /scalenew is set.' return,'' endif if n_elements(n_convolve) eq 0 then n_convolve=9L nch=n_convolve/2 w_image=(size(image1))[1] h_image=(size(image1))[2] ;make mask mask=replicate(1b,w_image,h_image) mask[0:nch-1,0:h_image-1]=0B mask[w_image-nch:w_image-1,0:h_image-1]=0B mask[0:w_image-1,0:nch-1]=0B mask[0:w_image-1,h_image-nch:h_image-1]=0B ;mask out the satuated pixels tst=((image1 ge satcnts1) or (image2 ge satcnts2)) and mask if (max(tst)) then begin indx=where(tst,ni) print,'number of saturated pixels',ni for i=0,ni-1 do begin ix=indx[i] mod w_image iy=indx[i]/w_image ixl=(ix-nch*2-1)>0 ixh=(ix+nch*2+1)<(w_image-1) iyl=(iy-nch*2-1)>0 iyh=(iy+nch*2+1)<(h_image-1) mask[ixl:ixh,iyl:iyh]=0b endfor endif else begin print,'no saturated pixels.' endelse satmask=mask ;mask the matched area if keyword_set(match) or keyword_set(scalenew) then begin if n_elements(minmag) eq 0 then minmag=12.5 if n_elements(maxmag) eq 0 then maxmag=16.5 if n_elements(pixscale) eq 0 then pixscale=0.0009d gd1=where(cobj1.flags le 2 and cobj1.m ge minmag and cobj1.m le maxmag,ngd1) if (ngd1 lt 100) then begin print,'WARNING: Not enough good ref stars. Using them all.',ngd1 gd1=lindgen(n_elements(cobj1)) endif gd2=where(cobj2.flags le 2 and cobj2.m ge minmag and cobj2.m le maxmag,ngd2) if (ngd2 lt 100) then begin print,'WARNING: Not enough good new stars. Using them all.',ngd2 gd2=lindgen(n_elements(cobj2)) endif close_match_radec,cobj1[gd1].ra,cobj1[gd1].dec,cobj2[gd2].ra,cobj2[gd2].dec,match1,match2,pixscale,1 if keyword_set(match) then begin matchmask=replicate(0b,w_image,h_image) nmatch=n_elements(match1) if nmatch gt 0 then begin for indm=0,nmatch-1 do begin fwhm=fwhm10 xh=(ceil(cobj1[gd1[match1[indm]]].x+fwhm*2.))<(w_image-1) yl=(floor(cobj1[gd1[match1[indm]]].y-fwhm*2.))>0 yh=(ceil(cobj1[gd1[match1[indm]]].y+fwhm*2.))<(h_image-1) matchmask[xl:xh,yl:yh]=1b endfor mask=mask and matchmask endif else begin print,'no match...' endelse endif if keyword_set(scalenew) then begin if n_elements(subsize) eq 0 then subsize=400l nxbg=floor(w_image/subsize) nybg=floor(h_image/subsize) bgx=ceil(w_image/nxbg) bgy=ceil(h_image/nybg) scales=fltarr(nxbg,nybg) for nx=0,nxbg-1 do begin for ny=0,nybg-1 do begin xl=nx*bgx xh=(nx*bgx+bgx-1)<(w_image-1) yl=ny*bgy yh=(ny*bgy+bgy-1)<(h_image-1) ind=where(cobj1[gd1[match1]].x ge xl and $ cobj1[gd1[match1]].x le xh and $ cobj1[gd1[match1]].y ge yl and $ cobj1[gd1[match1]].y le yh, nind) while nind lt 5 do begin xl=(xl-subsize/8.)>0 xh=(xh+subsize/8.)<(w_image-1) yl=(yl-subsize/8.)>0 yh=(yh+subsize/8.)<(h_image-1) ind=where(cobj1[gd1[match1]].x ge xl and $ cobj1[gd1[match1]].x le xh and $ cobj1[gd1[match1]].y ge yl and $ cobj1[gd1[match1]].y le yh, nind) endwhile refflux=10^((zeropoint1-cobj1[gd1[match1[ind]]].m)/2.5) newflux=10^((zeropoint2-cobj2[gd2[match2[ind]]].m)/2.5) scales[nx,ny]=median(refflux/newflux,/even) endfor endfor if nxbg gt 1 or nybg gt 1 then begin x1a=lindgen(nxbg)*bgx+floor(bgx/2) x2a=lindgen(nybg)*bgy+floor(bgy/2) xconvolve_splie2, x1a, x2a, scales, nxbg, nybg, y2a1 xconvolve_splin2, x1a, x2a, scales, y2a1, nxbg, nybg, $ findgen(w_image), findgen(h_image), scalemap image2=temporary(image2)*scalemap endif else begin image2=temporary(image2)*scales[0,0] endelse endif endif else begin ;mask out the bright stars if keyword_set(nobright) then begin if n_elements(brightmag) eq 0 then brightmag=12. bright=where(cobj1.m le brightmag,nbright) if nbright gt 0 then begin for nb=0,nbright-1 do begin bfwhm=(cobj1[bright[nb]].fwhm*((fwhm2/fwhm1)>1))<50. ixl=(cobj1[bright[nb]].x-bfwhm*3.d0-nch-1)>0 ixh=(cobj1[bright[nb]].x+bfwhm*3.d0+nch+1)<(w_image-1) iyl=(cobj1[bright[nb]].y-bfwhm*3.d0-nch-10)>0 iyh=(cobj1[bright[nb]].y+bfwhm*3.d0+nch+1)<(h_image-1) mask[ixl:ixh,iyl:iyh]=0b endfor endif endif endelse return,mask end