Beispiel #1
0
def credge(arr, thresh, gain, rdnoise, b_factor, fthresh):
    """Identify cosmic rays following the van Dokkem (2001) prescription
    """

    min_limit=0.01

    #set up the convolution kernel
    f_conv=np.array([[0,-1,0],[-1,4,-1],[0,-1,0]])

    #set up the array
    shape=arr.shape
    sdata=arr

    #rebin the data
    newshape=(b_factor*shape[0],b_factor*shape[1])
    ldata=salttran.rebin_factor(sdata,newshape)

    #convolve with f_conv
    #ldata=conv2d(ldata,f_conv,mode='same')
    ldata=conv2d(ldata,f_conv)
    mask = (ldata >= 0)
    ldata = ldata * mask

    #return to the original binning
    ldata = salttran.blockave(ldata,shape)

    #create noise model
    meddata =saltstat.median_image(sdata,5)
    noise = abs(meddata)
    noise = (gain*noise+rdnoise**2)**0.5/gain

    #create S/N image
    odata = ldata / noise / b_factor

    #remove extended objects
    odata = odata - saltstat.median_image(odata,5)

    #select objects
    masks = (odata>thresh)

    #remove compact bright sources
    mdata = saltstat.median_image(sdata,5)
    fdata = mdata - saltstat.median_image(mdata,7)
    fdata = fdata / noise

    # set a minimum value for all pixels so no divide by zero problems
    indf = np.where(fdata < min_limit)
    fdata[indf]=min_limit

    fdata = odata * masks/fdata
    maskf = (fdata > fthresh)

    #make the list of cosmic rays
    mask =  masks*maskf

    #identify the cosmic rays
    crarr=mdata*mask

    return crarr
Beispiel #2
0
def credge(arr, thresh, gain, rdnoise, b_factor, fthresh):
    """Identify cosmic rays following the van Dokkem (2001) prescription
    """

    min_limit = 0.01

    #set up the convolution kernel
    f_conv = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]])

    #set up the array
    shape = arr.shape
    sdata = arr

    #rebin the data
    newshape = (b_factor * shape[0], b_factor * shape[1])
    ldata = salttran.rebin_factor(sdata, newshape)

    #convolve with f_conv
    #ldata=conv2d(ldata,f_conv,mode='same')
    ldata = conv2d(ldata, f_conv)
    mask = (ldata >= 0)
    ldata = ldata * mask

    #return to the original binning
    ldata = salttran.blockave(ldata, shape)

    #create noise model
    meddata = saltstat.median_image(sdata, 5)
    noise = abs(meddata)
    noise = (gain * noise + rdnoise**2)**0.5 / gain

    #create S/N image
    odata = ldata / noise / b_factor

    #remove extended objects
    odata = odata - saltstat.median_image(odata, 5)

    #select objects
    masks = (odata > thresh)

    #remove compact bright sources
    mdata = saltstat.median_image(sdata, 5)
    fdata = mdata - saltstat.median_image(mdata, 7)
    fdata = fdata / noise

    # set a minimum value for all pixels so no divide by zero problems
    indf = np.where(fdata < min_limit)
    fdata[indf] = min_limit

    fdata = odata * masks / fdata
    maskf = (fdata > fthresh)

    #make the list of cosmic rays
    mask = masks * maskf

    #identify the cosmic rays
    crarr = mdata * mask

    return crarr
Beispiel #3
0
def crmedian(arr, thresh, mbox, bbox):
    """Identify cosmic rays through median technique.  Similar implimentation to
       crmedian in iraf.imred.crutil.crmedian
    """
    #Check to make sure the background box is an appropriate size
    #If it is too small, then insufficient statistics are generated
    if bbox <  10: bbox=10
    cbox=int(0.5*bbox)

    #make the background image
    #barr=generic_filter(arr,sigma_func, size=(bbox,bbox))
    barr=arr*0.0+arr.std()
    xlen=len(arr[0])
    ylen=len(arr)
    for i in range(mbox,xlen,bbox):
     for j in range(mbox,ylen,bbox):
         x1,x2,y1,y2=setbox(i,j,cbox, xlen, ylen)
         barr[y1:y2,x1:x2]=sigma_func(arr[y1:y2,x1:x2])

    #Median smooth the image
    marr=saltstat.median_image(arr, mbox)

    #Find the residual image
    rarr=(arr-marr)/barr

    #identify all sources
    crarr=marr*(rarr > thresh)

    return crarr
Beispiel #4
0
def crmedian(arr, thresh, mbox, bbox):
    """Identify cosmic rays through median technique.  Similar implimentation to
       crmedian in iraf.imred.crutil.crmedian
    """
    #Check to make sure the background box is an appropriate size
    #If it is too small, then insufficient statistics are generated
    if bbox < 10: bbox = 10
    cbox = int(0.5 * bbox)

    #make the background image
    #barr=generic_filter(arr,sigma_func, size=(bbox,bbox))
    barr = arr * 0.0 + arr.std()
    xlen = len(arr[0])
    ylen = len(arr)
    for i in range(mbox, xlen, bbox):
        for j in range(mbox, ylen, bbox):
            x1, x2, y1, y2 = setbox(i, j, cbox, xlen, ylen)
            barr[y1:y2, x1:x2] = sigma_func(arr[y1:y2, x1:x2])

    #Median smooth the image
    marr = saltstat.median_image(arr, mbox)

    #Find the residual image
    rarr = (arr - marr) / barr

    #identify all sources
    crarr = marr * (rarr > thresh)

    return crarr
Beispiel #5
0
def subbackground(image, sig, nbin, order, iter, type):
    """Calculate and subtract a global background from an image
    using a number of different methods.  This is assuming that
    slotmode data is being used.

    return data array
    """

    # return the same image if no background subtraction is required
    if type=='none': return image

    # calculate the image statistics
    if np.size(image)>0:
        mean, median, stddev=saltstat.iterstat(image,sig,iter)
    if stddev==0:
        raise SaltError('Standard deviation = 0')

    # Create a row median image. This will be useful for a number of purposes
    image_mrow=med_row_image(image)

    # Replace bright objects in the frame with the median value of that row
    mask=((image-mean) < sig*stddev)*(image-mean > -sig*stddev)
    image_sig=image*mask+(1-mask)*image_mrow

    if type == 'median-row':
        image_back=image*0.0+image_mrow

    # Median smooth the image
    if type == 'median':
        image_back=median_image(image_sig,nbin)

    # Make a fit to the surface
    if type=='surf':
        image_back=image.copy()

    if type=='both':
        image_surf=image.copy()
        for x in range(len(image[0])):
            y=np.arange(len(image))
            my=((image[:,x]-mean) < sig*stddev)*(image[:,x] -mean > -sig*stddev)
            cy=np.compress(my,y)
            cim=np.compress(my,image_med[:,x])
            coef=np.polyfit(cy,cim,order)
            image_surf[:,x]=np.polyval(coef,y)

    # subtract the fit
    image=image-image_back

    # return the image
    return image