original_docstring: Given the file with the clipped pixels (in the frame of the
      combined image) created by SWarp [clip_logname] and a list of
      reduced images [imagelist], this function will create a mask from
      the clipped pixels in the frame of the input images, where the
      clipped pixels are filtered using boxes of shapes (3,3), (10,10)
      and (50,50). Based on Gruen et al. 2014
      (https://ui.adsabs.harvard.edu/abs/2014PASP..126..158G/abstract)
      and their MaskMap program.
  
      
docstring: Given the file with the clipped pixels (in the frame of the
  combined image) created by SWarp [clip_logname] and a list of
  reduced images [imagelist], this function will create a mask from
  the clipped pixels in the frame of the input images, where the
  clipped pixels are filtered using boxes of shapes (3,3), (10,10)
  and (50,50). Based on Gruen et al. 2014
  
  and their MaskMap program.
docstring_tokens: Given the file with the clipped pixels ( in the frame of the combined image ) created by SWarp [ clip_logname ] and a list of reduced images [ imagelist ] this function will create a mask from the clipped pixels in the frame of the input images where the clipped pixels are filtered using boxes of shapes ( 3 3 ) ( 10 10 ) and ( 50 50 ) . Based on Gruen et al . 2014 and their MaskMap program .
code: def clipped2mask (clip_logname, imagelist, nsigma_clip, fits_ref):
  
      """Given the file with the clipped pixels (in the frame of the
      combined image) created by SWarp [clip_logname] and a list of
      reduced images [imagelist], this function will create a mask from
      the clipped pixels in the frame of the input images, where the
      clipped pixels are filtered using boxes of shapes (3,3), (10,10)
      and (50,50). Based on Gruen et al. 2014
      (https://ui.adsabs.harvard.edu/abs/2014PASP..126..158G/abstract)
      and their MaskMap program.
  
      """
  
      header = read_hdulist(imagelist[0], get_data=False, get_header=True)
      xsize, ysize = header['NAXIS1'], header['NAXIS2']
      
      # filter definitions
      fsize = [            5, 1 ]
      fsigma = [ nsigma_clip, 4 ]
      fmax = [             4, 1 ]
  
      
      # read clip_logname file created by SWarp
      table = ascii.read(clip_logname, format='fast_no_header', data_start=0,
                         names=['nfile', 'x', 'y', 'nsigma'])
      
      # keep only the entries above minimum sigma
      mask_keep = np.abs(table['nsigma']) > min(fsigma)
      table = table[mask_keep]
  
      # read ref image header
      hdr_ref = read_hdulist(fits_ref, get_data=False, get_header=True)
  
  
      # convert pixel coordinates to RA,DEC
      wcs_ref = WCS(hdr_ref)
      ra_ref, dec_ref = wcs_ref.all_pix2world(table['x'], table['y'], 1)
  
      
      # loop imagelist; that list is assumed to correspond to the
      # integers in the first column of [clip_logname]
      nimages = len(set(table['nfile']))
      if nimages != len(imagelist):
          # log error if these numbers are not consistent
          log.error ('#images in imagelist in [clipped2mask]: {} is not '
                     'consistent with #images in first column of {}: {}'
                     .format(len(imagelist), clip_logname, nimages))
  
  
      for nimage, image in enumerate(imagelist):
          
          # part of table relevant for this image
          mask_im = (table['nfile']==nimage)
          table_im = table[mask_im]
  
          # if empty, continue with next image
          if len(table_im)==0:
              continue
  
          # convert ra, dec coordinates from the reference frame (the
          # subset relevant for the current image) to pixel coordinates
          # in the frame of the current image
          hdr_im = read_hdulist(image, get_data=False, get_header=True)
  
  
          wcs_im = WCS(hdr_im)
          x_im, y_im = wcs_im.all_world2pix(ra_ref[mask_im], dec_ref[mask_im], 1)
          # update table_im coordinates with integers of x_im and y_im
          table_im['x'] = (x_im+0.5).astype(int)
          table_im['y'] = (y_im+0.5).astype(int)
  
          # discard objects beyond edges, if any
          mask_keep = ((table_im['x'] >= 1) & (table_im['x'] <= xsize) &
                       (table_im['y'] >= 1) & (table_im['y'] <= ysize))
          table_im = table_im[mask_keep]
  
  
          # use [pass_filters] to convert [table_im] with x, y, nsigma
          # into a boolean mask in which pixels to be masked are True
          t0 = time.time()
          mask_im = pass_filters (table_im, fsize, fsigma, fmax, (ysize, xsize))
  
  
          # make sure not to include pixels near saturated stars
          data_mask = (read_hdulist(image.replace('_red.fits', '_mask.fits'))
                       .astype('uint8'))
          mask_dict = get_par(set_zogy.mask_value,tel)
          # iterate over all mask values
          mask_sat = np.zeros(data_mask.shape, dtype=bool)
          for key in mask_dict.keys():
              # add saturated and saturated-connected pixels to [mask_sat]
              if 'saturated' in key:
                  val = mask_dict[key]
                  mask_sat |= (data_mask & val == val)
  
          # indices of [mask_sat]=True pixels
          (y_sat, x_sat) = np.nonzero(mask_sat)
          # indices of [mask_im]=True pixels
          (y_im, x_im) = np.nonzero(mask_im)
          dist2_limit = (5*header['PSF-FWHM'])**2
          for i in range(np.sum(mask_sat)):
              dist2 = (x_im - x_sat[i])**2 + (y_im - y_sat[i])**2
              mask_dist = (dist2 <= dist2_limit)
              if np.sum(mask_dist) > 0:
                  mask_im[y_im[mask_dist], x_im[mask_dist]] = False
  
  
          log.info ('created mask for {} in {:.2f}s'.format(image, time.time()-t0))
  
  
          if False:
              # create image with nsigmas
              im_sigmas = np.zeros((ysize,xsize), dtype='float32')
              im_sigmas[table_im['y']-1,table_im['x']-1] = table_im['nsigma']
  
              data = read_hdulist(image)
              ds9_arrays (data_c2m=data, im_sigmas_c2m=im_sigmas,
                          mask_im_c2m=mask_im.astype(int))
  
              
          # update corresponding weights image, i.e. set weights value
          # at [mask_im] to zero
          weights_tmp = image.replace('.fits','_weights.fits')
          with fits.open(weights_tmp, 'update') as hdulist:
              hdulist[-1].data[mask_im] = 0
  
  
      return
code_tokens: def clipped2mask ( clip_logname , imagelist , nsigma_clip , fits_ref ) : header = read_hdulist ( imagelist [ 0 ] , get_data = False , get_header = True ) xsize , ysize = header [ 'NAXIS1' ] , header [ 'NAXIS2' ] fsize = [ 5 , 1 ] fsigma = [ nsigma_clip , 4 ] fmax = [ 4 , 1 ] table = ascii . read ( clip_logname , format = 'fast_no_header' , data_start = 0 , names = [ 'nfile' , 'x' , 'y' , 'nsigma' ] ) mask_keep = np . abs ( table [ 'nsigma' ] ) > min ( fsigma ) table = table [ mask_keep ] hdr_ref = read_hdulist ( fits_ref , get_data = False , get_header = True ) wcs_ref = WCS ( hdr_ref ) ra_ref , dec_ref = wcs_ref . all_pix2world ( table [ 'x' ] , table [ 'y' ] , 1 ) nimages = len ( set ( table [ 'nfile' ] ) ) if nimages != len ( imagelist ) : log . error ( '#images in imagelist in [clipped2mask]: {} is not ' 'consistent with #images in first column of {}: {}' . format ( len ( imagelist ) , clip_logname , nimages ) ) for nimage , image in enumerate ( imagelist ) : mask_im = ( table [ 'nfile' ] == nimage ) table_im = table [ mask_im ] if len ( table_im ) == 0 : continue hdr_im = read_hdulist ( image , get_data = False , get_header = True ) wcs_im = WCS ( hdr_im ) x_im , y_im = wcs_im . all_world2pix ( ra_ref [ mask_im ] , dec_ref [ mask_im ] , 1 ) table_im [ 'x' ] = ( x_im + 0.5 ) . astype ( int ) table_im [ 'y' ] = ( y_im + 0.5 ) . astype ( int ) mask_keep = ( ( table_im [ 'x' ] >= 1 ) & ( table_im [ 'x' ] <= xsize ) & ( table_im [ 'y' ] >= 1 ) & ( table_im [ 'y' ] <= ysize ) ) table_im = table_im [ mask_keep ] t0 = time . time ( ) mask_im = pass_filters ( table_im , fsize , fsigma , fmax , ( ysize , xsize ) ) data_mask = ( read_hdulist ( image . replace ( '_red.fits' , '_mask.fits' ) ) . astype ( 'uint8' ) ) mask_dict = get_par ( set_zogy . mask_value , tel ) mask_sat = np . zeros ( data_mask . shape , dtype = bool ) for key in mask_dict . keys ( ) : if 'saturated' in key : val = mask_dict [ key ] mask_sat |= ( data_mask & val == val ) ( y_sat , x_sat ) = np . nonzero ( mask_sat ) ( y_im , x_im ) = np . nonzero ( mask_im ) dist2_limit = ( 5 * header [ 'PSF-FWHM' ] ) ** 2 for i in range ( np . sum ( mask_sat ) ) : dist2 = ( x_im - x_sat [ i ] ) ** 2 + ( y_im - y_sat [ i ] ) ** 2 mask_dist = ( dist2 <= dist2_limit ) if np . sum ( mask_dist ) > 0 : mask_im [ y_im [ mask_dist ] , x_im [ mask_dist ] ] = False log . info ( 'created mask for {} in {:.2f}s' . format ( image , time . time ( ) - t0 ) ) if False : im_sigmas = np . zeros ( ( ysize , xsize ) , dtype = 'float32' ) im_sigmas [ table_im [ 'y' ] - 1 , table_im [ 'x' ] - 1 ] = table_im [ 'nsigma' ] data = read_hdulist ( image ) ds9_arrays ( data_c2m = data , im_sigmas_c2m = im_sigmas , mask_im_c2m = mask_im . astype ( int ) ) weights_tmp = image . replace ( '.fits' , '_weights.fits' ) with fits . open ( weights_tmp , 'update' ) as hdulist : hdulist [ - 1 ] . data [ mask_im ] = 0 return
short_docstring: Given the file with the clipped pixels (in the frame of the
  combined image) created by SWarp [clip_logname] and a list of
  reduced images [imagelist], this function will create a mask from
  the clipped pixels in the frame of the input images, where the
  clipped pixels are filtered using boxes of shapes (3,3), (10,10)
  and (50,50).
short_docstring_tokens: Given the file with the clipped pixels ( in the frame of the combined image ) created by SWarp [ clip_logname ] and a list of reduced images [ imagelist ] this function will create a mask from the clipped pixels in the frame of the input images where the clipped pixels are filtered using boxes of shapes ( 3 3 ) ( 10 10 ) and ( 50 50 ) .
comment: """Given the file with the clipped pixels (in the frame of the
      combined image) created by SWarp [clip_logname] and a list of
      reduced images [imagelist], this function will create a mask from
      the clipped pixels in the frame of the input images, where the
      clipped pixels are filtered using boxes of shapes (3,3), (10,10)
      and (50,50). Based on Gruen et al. 2014
      (https://ui.adsabs.harvard.edu/abs/2014PASP..126..158G/abstract)
      and their MaskMap program.
  
      """$SEP$# filter definitions$SEP$# read clip_logname file created by SWarp$SEP$# keep only the entries above minimum sigma$SEP$# read ref image header$SEP$# convert pixel coordinates to RA,DEC$SEP$# loop imagelist; that list is assumed to correspond to the$SEP$# integers in the first column of [clip_logname]$SEP$# log error if these numbers are not consistent$SEP$# part of table relevant for this image$SEP$# if empty, continue with next image$SEP$# convert ra, dec coordinates from the reference frame (the$SEP$# subset relevant for the current image) to pixel coordinates$SEP$# in the frame of the current image$SEP$# update table_im coordinates with integers of x_im and y_im$SEP$# discard objects beyond edges, if any$SEP$# use [pass_filters] to convert [table_im] with x, y, nsigma$SEP$# into a boolean mask in which pixels to be masked are True$SEP$# make sure not to include pixels near saturated stars$SEP$# iterate over all mask values$SEP$# add saturated and saturated-connected pixels to [mask_sat]$SEP$# indices of [mask_sat]=True pixels$SEP$# indices of [mask_im]=True pixels$SEP$# create image with nsigmas$SEP$# update corresponding weights image, i.e. set weights value$SEP$# at [mask_im] to zero