Ejemplo n.º 1
0
def generic(filenames,
            keys=[],
            normalize=False,
            method=None,
            mbias=None,
            mdark=None,
            mflat=None,
            product=None,
            new_header=False,
            min_val=0,
            max_val=65535):

    log.info(f'fitsort {len(filenames)} filenames per {keys}')

    df = Dfits(filenames)
    sortlist = df.fitsort(keys)
    heads = df.heads

    if new_header:
        instrument = init_observatory(new_header)
        o = Observatory(**instrument)

    for value in sortlist.unique_values:
        filenames = sortlist.unique_names_for(value)
        log.info(f'getting {len(filenames)} filenames for {value}')

        # Combine (and save) data per data.
        if method == "slice" or method == "individual":
            for i, filename in enumerate(filenames):

                data = get_fits_data(filename)
                output = combine(data,
                                 normalize=normalize,
                                 min_val=min_val,
                                 max_val=max_val,
                                 mbias=mbias,
                                 mdark=mdark,
                                 mflat=mflat)

                header = o.newhead(heads[i]) if new_header else heads[i]

                closing(keys, value, product, output, counter=i, header=header)

        # Combine and save acting on a data cube
        else:
            datas = np.array([get_fits_data(f) for f in filenames])
            output = combine(datas,
                             normalize=normalize,
                             min_val=min_val,
                             max_val=max_val,
                             method=method,
                             mbias=mbias,
                             mdark=mdark,
                             mflat=mflat)

            header = o.newhead(header=heads[0]) if new_header else heads[0]

            closing(keys, value, product, output, header=header)
Ejemplo n.º 2
0
def detect_donuts(filename, template):
    '''
    Use opencv to find centroids of highly defocused images template matching.
    '''

    img = rescale(get_fits_data(filename))
    tpl = rescale(get_fits_data(template))

    res = cv2.matchTemplate(img, tpl, cv2.TM_CCOEFF_NORMED)
    threshold = 0.6

    loc = np.where(res >= threshold)
    x, y = loc
    p = np.repeat("point ", y.size)
    t = [p, (y + tpl.shape[0] / 2), (x + tpl.shape[1] / 2)]
    table = Table(t, names=['# ', '## ', '###'])  # bleah
    ascii.write(table, "donuts.reg", overwrite=True)

    return res
Ejemplo n.º 3
0
def signal_noise(filenames, my_instr="Mexman"):
    for filename in filenames:
        catalog = load_catalog(filename)
        apers = set_apertures(catalog)
        data = get_fits_data(filename)
        header = get_fits_header(filename)
        wcs = WCS(header)
        phot_table = do_photometry(data, apers=apers, wcs=wcs)
        pixar = apers[0].to_pixel(wcs)
        pixan = apers[1].to_pixel(wcs)
        bkg_mean = phot_table['aperture_sum_1'] / pixan.area
        
        R_star = phot_table['residual_aperture_sum']     
        R_sky = bkg_mean * pixar.area
        n_pix_aperture = pixan.area
        
        with open('instruments.json') as jfile:
            instrument_dict = json.load(jfile)

        instrument = instrument_dict[my_instr]

        # if header['INSTRUME'] == 'Mexman':
        #     key = instrument_dict['Mexman']
        # elif header['INSTRUME'] == 'DFOSC_FASU':
        #     key = instrument_dict['DFOSC_FASU']
        # elif header['INSTRUME'] == 'STL-11000 3 CCD Camera':
        #     key = instrument_dict['STL-11000 3 CCD Camera']
        # else:
        #     log.warning('INSTRUMENT NOT FOUND ')
            
        gain = instrument['gain']
        RON = instrument['ron']
        if gain is None:
            log.warning(f'Gain not found for {instrument}')
        if ron is None:
            log.warning(f'Ron not found for {instrument}')
            
        signal_noise = (R_star)/[(R_star + R_sky + RON + (gain/2)**2)**1/2]
        # for now I'm neglecting Dark Current effects 
        
    return(signal_noise)
         
Ejemplo n.º 4
0
def detect_sources(image):
    '''
    By Anna Marini
    Extract the light sources from the image
    '''
    # threshold = detect_threshold(image, nsigma=2.)
    # sigma = 3.0 * gaussian_fwhm_to_sigma  # FWHM = 3.
    # kernel = Gaussian2DKernel(sigma, x_size=3, y_size=3)
    # kernel.normalize()

    if isinstance(image, str):
        image = get_fits_data(image)

    mask = make_source_mask(image, nsigma=2, npixels=5, dilate_size=11)
    mean, median, std = sigma_clipped_stats(image, sigma=3, mask=mask)
    daofind = DAOStarFinder(fwhm=3.0, threshold=5. * std)
    sources = daofind(image - median)
    # Pixel coordinates of the sources
    x = np.array(sources['xcentroid'])
    y = np.array(sources['ycentroid'])
    return x, y
Ejemplo n.º 5
0
def combine(images,
            normalize=False,
            method=None,
            precision='float32',
            mbias=None,
            mdark=None,
            mflat=None,
            mask=False,
            min_val=0,
            max_val=65535):
    #a = Time.now()

    # Datas from pattern
    if isinstance(images, str):
        images = [images]
    if isinstance(images[0], str):
        datas = np.array([get_fits_data(i) for i in images])
    else:
        datas = images
    # Check counts
    datas = np.array(
        [d for d in datas if counts_ok(d, min_val=min_val, max_val=max_val)])

    # Master datas from filename
    if isinstance(mbias, str):
        mbias = get_fits_data(mbias)
    if isinstance(mdark, str):
        mdark = get_fits_data(mdark)
    if isinstance(mflat, str):
        mflat = get_fits_data(mflat)

    # Cannot cast type
    if mbias is not None and len(mbias):
        datas = (datas - mbias).astype(precision)
    if mdark is not None and len(mdark):
        datas = (datas - mdark).astype(precision)
    if mflat is not None and len(mflat):
        datas = (datas / mflat).astype(precision)

    del mbias, mdark, mflat

    # Did not find a faster method to save memory.
    if normalize:
        bottle = np.zeros(shape=datas.shape).astype(precision)
        for i, d in enumerate(datas):
            bottle[i] = d / np.mean(d).astype(precision)
        datas = bottle
        del bottle

    if method == 'average':
        combined = np.average(datas, axis=0).astype(precision)
    elif method == 'median':
        combined = np.median(datas, axis=0).astype(precision)
    else:  # cube or 1-slice cube.
        combined = np.squeeze(datas)

    log.info(
        f'{method}: {datas.shape}{datas.dtype} -> {combined.shape}{combined.dtype}'
    )
    #log.info(f'Done in {Time.now().unix - a.unix :.1f}s')
    del datas  # Saving memory

    return combined
Ejemplo n.º 6
0
def counts(filename,
           bias1='1_CCD_Image_34.fits',
           bias2='1_CCD_Image_47.fits',
           bias3='1_CCD_Image_75.fits',
           bias4='2_CCD_Image_191.fits',
           bias5='2_CCD_Image_244.fits',
           bias6='2_CCD_Image_277.fits',
           bias7='3_CCD_Image_52.fits',
           bias8='3_CCD_Image_103.fits',
           bias9='3_CCD_Image_152.fits'):

    header = get_fits_header(filename)

    camera = header['INSTRUME']
    temp = header['CCD-TEMP']

    if camera == 'Atik Cameras':
        if temp <= 0.3:
            mean_sub = np.mean(
                get_fits_data(filename)[1007:3007, 631:2040]) - np.mean(
                    get_fits_data(bias1))
        elif 0.3 < temp <= 1.3:
            mean_sub = np.mean(
                get_fits_data(filename)[1007:3007, 631:2040]) - np.mean(
                    get_fits_data(bias2))
        else:
            mean_sub = np.mean(
                get_fits_data(filename)[1007:3007, 631:2040]) - np.mean(
                    get_fits_data(bias3))  #intorno a 3
    elif camera == 'SBIG STL-11000 3 CCD Camera w/ AO':
        if -5.5 <= temp <= -3.7:
            mean_sub = np.mean(
                get_fits_data(filename)[500:3508, 500:2172]) - np.mean(
                    get_fits_data(bias4))
        elif temp < -5.5:
            mean_sub = np.mean(
                get_fits_data(filename)[500:3508, 500:2172]) - np.mean(
                    get_fits_data(bias5))
        else:
            mean_sub = np.mean(
                get_fits_data(filename)[500:3508, 500:2172]) - np.mean(
                    get_fits_data(bias6))
    elif camera == 'SBIG STX-16801 3 CCD Camera w/ AO':
        if temp <= -19.3:
            mean_sub = np.mean(
                get_fits_data(filename)[500:3500, 500:3500]) - np.mean(
                    get_fits_data(bias7))
        elif -19.2 < temp <= -14.3:
            mean_sub = np.mean(
                get_fits_data(filename)[500:3500, 500:3500]) - np.mean(
                    get_fits_data(bias8))
        else:
            mean_sub = np.mean(
                get_fits_data(filename)[500:3500, 500:3500]) - np.mean(
                    get_fits_data(bias9))

    return (mean_sub)
Ejemplo n.º 7
0
def apphot(filenames,
           reference=0,
           display=DISPLAY,
           r=False,
           r_in=False,
           r_out=False):
    '''
    Perform the aperture photometry
    '''

    filenames = sorted(filenames)

    header0 = get_fits_header(filenames[reference])
    wcs0 = WCS(header0)

    catalog = load_catalog(wcs=wcs0)
    if r and r_in and r_out:
        apers = set_apertures(catalog, r=r, r_in=r_in, r_out=r_out)
    else:
        apers = set_apertures(catalog)

    tables = Table()
    err_table = Table()

    if display:
        d = pyds9.DS9("ds9")

    for filename in filenames:
        header = get_fits_header(filename)
        data = get_fits_data(filename)
        wcs = WCS(header)

        #catalog = load_catalog(wcs=wcs)
        #apers = set_apertures(catalog, r=r, r_in=r_in, r_out=r_out)

        ron, gain, dark_current = ron_gain_dark()

        phot_table = do_photometry(data,
                                   apers,
                                   wcs,
                                   ron,
                                   gain,
                                   dark_current,
                                   obstime=header['MJD-OBS'])
        # phot_table = do_photometry(data, apers, wcs, obstime=header['MJD-OBS'],
        #                            flux=False, zero_point_flux=1)

        # positions = SkyCoord(catalog['ra'], catalog['dec'],
        #                      frame='icrs',
        #                      unit=(u.deg, u.deg))

        if display:
            d.set(f"file {filename}")

            # for i,pos in enumerate(positions):
            #     p = pos.to_pixel(wcs)
            #     circ = f'circle({p[0]}, {p[1]}, {10})'
            #     d.set("regions", circ)
            #     d.set("region", f"text {p[0]} {p[1]} "+"{"+str(i)+"}")

            for i, aper in enumerate(apers[0].to_pixel(wcs)):
                circ = f'circle({aper.positions[0]}, {aper.positions[1]}, {aper.r})'
                d.set("regions", circ)
                d.set(
                    "region",
                    f"text {aper.positions[0]}, {aper.positions[1]} " + "{" +
                    str(i) + "}")

            for aper in apers[1].to_pixel(wcs):
                circ = f'circle({aper.positions[0]}, {aper.positions[1]}, {aper.r_in})'
                d.set("regions", circ)
                circ = f'circle({aper.positions[0]}, {aper.positions[1]}, {aper.r_out})'
                d.set("regions", circ)

        tables.add_column(phot_table["residual_aperture_sum"],
                          rename_duplicate=True)
        err_table.add_column(phot_table["error"], rename_duplicate=True)
        log.info(f"Done {filename}")

    return tables, err_table