Exemplo n.º 1
0
    def evaluate_sex(
        self,
        params=["X_IMAGE", "Y_IMAGE", "FLUX_AUTO", "FLUX_ISO", "FLUX_ISOCOR", "FLAGS"],
        config={"DETECT_MINAREA": 4, "PHOT_FLUXFRAC": "0.3, 0.5, 0.8"},
        sexpath="/afs/slac/g/ki/software/local/bin/sex",
        **kwargs
    ):
        """
        Run sextractor on a numpy array (or weak_sauce source) to do photometry.
        Results returned in an astropy table.

        params: list of sextractor outputs you want to write out
        config: dict of input sextractor params (FILTER_NAME for non-default filter)
        sexpath: path to sextractor executable (default is location on ki-ls)

        """
        import sewpy, os

        sew = sewpy.SEW(params=params, config=config, sexpath=sexpath, **kwargs)
        from astropy.io import fits

        hdu = fits.PrimaryHDU(self.fluxes)
        hdulist = fits.HDUList([hdu])
        try:
            hdulist.writeto("/scratch/iamlame.fits", clobber=False)
        except IOError:
            print(
                "you already have a file called iamlame.fits in this directory--you should rename it and then seek help..."
            )
            raise
        out = sew("/scratch/iamlame.fits")
        os.remove("/scratch/iamlame.fits")
        return out["table"]  # this is an astropy table.
Exemplo n.º 2
0
def make_asc_runsex(img):
    #img='/home/yu/da/2019_work/cg_search/H2019-09-11T14:14:51_GW_S190910d_1819+2521.fits'
    write_key = [
        'NUMBER', 'ALPHA_J2000', 'DELTA_J2000', 'MAG_AUTO', 'MAGERR_AUTO',
        'MAG_BEST', 'MAGERR_BEST', 'ELLIPTICITY'
    ]
    sew = sewpy.SEW(params=write_key, )
    print('sex' + img)
    try:
        try:
            out = sew(img + '.fits')
        except:
            out = sew(img)
    except:
        logfile = open('temp.log', 'a')
        logfile.write(img + '\n')
        logfile.close()
        return
    sextab = out['table']
    with open('GaiaCatalog0.ASC', 'w') as asv:
        sumlog = 1
        for i in write_key:
            asv.write('# ' + str(sumlog) + ' ' + i + '\n')
            sumlog = sumlog + 1
        for i in sextab:
            line = ''
            for ii in i:
                line = line + ' ' + str(ii)
            asv.write(line + '\n')
    return sextab
Exemplo n.º 3
0
def extractSept():
    sew = sewpy.SEW(params=[
        "NUMBER", "X_IMAGE", "Y_IMAGE", "FLAGS", "CLASS_STAR", "ALPHA_SKY",
        "DELTA_SKY", "FWHM_IMAGE",
        "FLUX_APER(%s)" % (len(aperture_list_pix))
    ],
                    config={
                        "PIXEL_SCALE": scale,
                        "SEEING_FWHM": seeing_fwhm_sept,
                        "FILTER_NAME": (sex + "gauss_5.0_9x9.conv"),
                        "CHECKIMAGE_TYPE": 'SEGMENTATION',
                        "PHOT_APERTURES": (aperture_list_pix),
                        "CHECKIMAGE_NAME": (userdata + 'sept_check.fits')
                    })
    for num in range(len(septlist)):
        out = sew(sept + septlist[num])
        sexname = sept + septlist[num].split(".fits")[0] + "_sexout.txt"
        cleanname = sept + septlist[num].split(".fits")[0] + "_clean.txt"
        septextracted.append(sexname)
        septclean.append(cleanname)
        ascii.write(out["table"], sexname, overwrite=True)
        copyfile(sexname, cleanname)
        for line in fileinput.input(cleanname, inplace=1):
            x = line.split(' ')
            if (not line.startswith('N')) and (float(x[26]) > 0) and int(
                    x[3]) == 0 and float(x[4]) > .1:
                sys.stdout.write(line)
Exemplo n.º 4
0
    def compute_sextractor_catalog(self,
                                   image_path,
                                   image_wht_path,
                                   detect_minarea=9,
                                   phot_fluxfrac=0.5,
                                   detect_thresh=1.0,
                                   analysis_thresh=1.0,
                                   deblend_nthresh=32,
                                   deblend_mincont=0.0015,
                                   pixel_scale=0.065,
                                   weight_thresh=100,
                                   save_check_images=False,
                                   use_image_wht=True):
        import sewpy

        sexpath = expanduser(config['sextractor_path'])
        params = [
            "X_IMAGE", "Y_IMAGE", "FLUX_RADIUS", "FLUX_AUTO", "MAG_AUTO",
            "MU_MAX", "FWHM_IMAGE", "FLAGS"
        ]

        config_se = {
            "DETECT_MINAREA": detect_minarea,
            "PHOT_FLUXFRAC": phot_fluxfrac,
            "DETECT_THRESH": detect_thresh,
            "ANALYSIS_THRESH": analysis_thresh,
            "DEBLEND_NTHRESH": deblend_nthresh,
            "DEBLEND_MINCONT": deblend_mincont,
            "PIXEL_SCALE": pixel_scale
        }

        if save_check_images:
            config_se.update({
                "CHECKIMAGE_TYPE":
                "BACKGROUND,BACKGROUND_RMS,OBJECTS,-BACKGROUND,-OBJECTS,APERTURES,SEGMENTATION"
            })
        if use_image_wht:
            config_se.update({
                "WEIGHT_IMAGE": image_wht_path,
                "WEIGHT_TYPE": "MAP_WEIGHT",
                "WEIGHT_THRESH": weight_thresh
            })

        sew = sewpy.SEW(sexpath=sexpath, params=params, config=config_se)
        catalog_orig = sew(image_path)

        catalog = {}
        catalog['xy'] = np.vstack((catalog_orig['table']['X_IMAGE'],
                                   catalog_orig['table']['Y_IMAGE'])).T - 1.0
        catalog['flux_radius'] = catalog_orig['table']['FLUX_RADIUS']
        catalog['flux_auto'] = catalog_orig['table']['FLUX_AUTO']
        catalog['mag_auto'] = catalog_orig['table']['MAG_AUTO']
        catalog['mu_max'] = catalog_orig['table']['MU_MAX']
        catalog['fwhm_image'] = catalog_orig['table']['FWHM_IMAGE']

        return catalog, catalog_orig
Exemplo n.º 5
0
def sourceExtract(galaxyname):
    sew = sewpy.SEW(params=["NUMBER"],
                    config={
                        "DETECT_MINAREA": 50,
                        "DETECT_THRESH": 3.0,
                        "CHECKIMAGE_NAME": galaxyname + "_seg.fits",
                        "CHECKIMAGE_TYPE": "SEGMENTATION",
                        "WEIGHT_TYPE": "MAP_RMS",
                        "WEIGHT_IMAGE": galaxyname + "_sig.fits"
                    })
    sew(galaxyname + "_modsub.fits")
    iraf.imcopy(galaxyname + "_seg.fits", galaxyname + ".fits.pl")
Exemplo n.º 6
0
def sextractor(image_name):
    sew = sewpy.SEW(params=[
        'X_IMAGE', 'Y_IMAGE', 'FLUX_RADIUS(3)', 'THETA_IMAGE', 'ELLIPTICITY',
        'A_IMAGE', 'B_IMAGE', 'DISK_THETA_IMAGE'
    ],
                    config={
                        'DETECT_MINAREA': 5,
                        'PHOT_FLUXFRAC': '0.5, 1.0, 1.0'
                    })
    str_out = sew(image_name)
    obj_tab = str_out['table']
    return obj_tab
Exemplo n.º 7
0
    def get_fwhm(self, force=False, nominal=5):
        '''gets fwhm from header or runs SExtractor to determine it if not in header'''

        if 'FWHM' in self.header:
            self.fwhm = self.header['FWHM']

        # skip if already done unless forced to re-calculate
        if (self.fwhm != 0) and (force is False):
            return

        # get paths to needed files
        sxcp = os.path.join(os.path.dirname(inspect.getfile(LOSSPhotPypeline)),
                            'conf', 'sextractor_config')
        filt = os.path.join(sxcp, 'gauss_2.0_5x5.conv')
        par = os.path.join(sxcp, 'fwhm.par')
        star = os.path.join(sxcp, 'default.nnw')

        # set up configuration dictionary to override SExtractor defaults
        cf = {
            'PARAMETERS_NAME': par,
            'DETECT_MINAREA': 10,
            'DETECT_THRESH': 3,
            'ANALYSIS_THRESH': 3.5,
            'FILTER_NAME': filt,
            'DEBLEND_MINCOUNT': 0.0001,
            'MASK_TYPE': 'NONE',
            'SATUR_LEVEL': 36000.0,
            'MAG_ZEROPOINT': 25.0,
            'GAIN': 3,
            'PIXEL_SCALE': 0.7965,
            'SEEING_FWHM': 4.17,
            'STARNNW_NAME': star,
            'BACK_SIZE': 8,
            'BACKPHOTO_THICK': 24,
            'WEIGHT_TYPE': 'BACKGROUND',
            'WEIGHT_GAIN': 'Y'
        }

        # run SExtractor and get results, write nominal val if failed
        sew = sewpy.SEW(config=cf, configfilepath=sxcp)
        res = sew(self.cimg)["table"]
        if len(res) == 0:
            self.fwhm = nominal
        else:
            self.fwhm = np.median(res["FWHM_IMAGE"])

        # if somehow still zero, write nominal value
        if self.fwhm == 0:
            self.fwhm = nominal

        # set in the fits image
        self.write_header('FWHM', self.fwhm)
Exemplo n.º 8
0
def sextractor_setup(var_fname, segm_fname, bg_fname, aper=5, workdir=None):
# sextractor config parameters
    sexconfig = {'CHECKIMAGE_TYPE': 'SEGMENTATION,BACKGROUND',
                 'CHECKIMAGE_NAME': ','.join([segm_fname,bg_fname]),
                 'CATALOG_TYPE': 'FITS_1.0',
                 'WEIGHT_TYPE': 'MAP_VAR',
                 'WEIGHT_IMAGE': var_fname,
                 'PHOT_APERTURES': '%f'%(aper)
                 #'CATALOG_NAME': cat_fname
}
    print (sexconfig)
    # set up sextractor
    sexpath = os.environ.get('SEX_PATH') or '/usr/bin/sextractor'
    sex = sew.SEW(workdir=workdir or getcwd(),
                  sexpath=sexpath,
                  params=sexparams,
                  config=sexconfig)
    sex._clean_workdir()
    return sex
Exemplo n.º 9
0
def extractOct():
    for num in range(len(oct_list)):
        sew = sewpy.SEW(params=[
            "NUMBER", "X_IMAGE", "Y_IMAGE", "FLAGS", "CLASS_STAR",
            "FWHM_IMAGE",
            "FLUX_APER(%s)" % (len(aperture_list_pix))
        ],
                        config={
                            "PIXEL_SCALE":
                            scale,
                            "SEEING_FWHM":
                            seeing_fwhm_oct,
                            "FILTER_NAME": (sex + "gauss_3.0_7x7.conv"),
                            "CHECKIMAGE_TYPE":
                            'SEGMENTATION',
                            "CHECKIMAGE_NAME":
                            (userdata + 'c%s_check.fits' % (num + 1)),
                            "PHOT_APERTURES": (aperture_list_pix)
                        })
        out = sew(oct + oct_list[num])
        name = oct + oct_list[num].split(".fits")[0]
        cleanname = name + "_clean.txt"
        sexname = name + "_sexout.txt"
        plotname = name + '_plot.txt'
        octclean.append(cleanname)
        oct_extracted.append(sexname)
        octplot.append(plotname)
        ascii.write(out["table"], cleanname, overwrite=True)
        copyfile(cleanname, sexname)
        temp1, temp2 = [], []
        for line in fileinput.input(cleanname, inplace=1):
            x = line.split(' ')
            if (not line.startswith('N')) and (int(x[3]) == 0) and (
                    loweroct < float(x[1]) < upperoct) and (loweroct < float(
                        x[2]) < upperoct) and (float(x[24]) > 0):
                temp1.append(x[1])
                temp2.append(x[2])
                sys.stdout.write(line)
        copyfile(cleanname, plotname)
        with open((userdata + '%soct.xml') % (num + 1), 'w') as f:
            for y in range(len(temp1)):
                f.write('circle(%s,%s,%s)' % (temp1[y], temp2[y], 30) + '\n')
Exemplo n.º 10
0
def sext_find(dataFile,
              rmsFile,
              regFile,
              outputName,
              plotImage=False,
              excludeReg=True):
    '''Using SEXtractor to identify cores in an image.
    Then filter out some cores based on ) RMS map, input as rmsFile.
    2) Regions input as regFile.
    3) Eccentricity of the fitted ellipses.

    Args:
        1) dataFile: the input FITS file.
        2) rmsFile: the RMS FITS file for filtering, should in same physical
          shape as the image FITS file.
        3) regFile: the regions files created in DS9, which contains lists ot
          2n floats that depict n points of a polygon.
        4) outputName: the root name of the output files. <outputName>.txt,
           the table of the params of the identified cores; <outputName.pdf>
           a plot of the image overlaid with the cores, in ellipses.
        5) plotImage: if you want to plot the image with cores overlaid
        6) excludeReg: if exclude the cores in large Hii regions defined by
            regFile.

    Returns:
        identified cores num and total cores num (before filtering)
    '''
    header = fits.open(dataFile)[0].header
    data = fits.open(dataFile)[0].data.T  # to make ra as x and dec as y
    rmshdu = fits.open(rmsFile)[0]
    rms = rmshdu.data.T  # to make ra as x and dec as y

    '''set up SEXtractor. The outputParameters dic will be used to store the
        filtered in cores.
    '''
    configParameters = ['X_IMAGE',
                        'Y_IMAGE',
                        'FLUX_RADIUS',
                        # 'FLUX_MAX',
                        'FLUX_ISO',
                        'FLUXERR_ISO',
                        'FLAGS',
                        'A_IMAGE',
                        'B_IMAGE',
                        'THETA_IMAGE',
                        'XPEAK_IMAGE',
                        'YPEAK_IMAGE',
                        'ALPHAPEAK_J2000',
                        'DELTAPEAK_J2000']
    sew = sewpy.SEW(params=configParameters,
                    config={'DETECT_MINAREA': 60,
                            'DETECT_MAXAREA': 2500 * 4,
                            'DETECT_THRESH': 5,
                            'BACK_SIZE': 32,
                            'BACKPHOTO_TYPE': 'LOCAL',
                            'BACKPHOTO_THICK': 24,
                            'THRESH_TYPE': 'RELATIVE',
                            'DEBLEND_MINCONT': 0.00001,
                            'DEBLEND_NTHRESH': 64,
                            'CHECKIMAGE_NAME': '../data/%s_check_rms.fits' % (outputName),
                            'CHECKIMAGE_TYPE': 'BACKGROUND_RMS'})
    ot = sew(dataFile)['table']

    '''SEXtractor returns half of maj/min-axis, plot uses full. sizeFactor
    adjust it in plot.
    '''
    sizeFactor = 2.0

    '''For a finite-size core, if you sum up all the pixels' flux density
    in unit of Jy/beam, then multiply this will give you the flux of the
    whole core. header['CDELTA#'] are the pixel intervels (sizes), which
    can be negative. header['BMAJ'] and header['BMIN'] are the beam sizes.
    '''
    fluxFactor = 4. * np.log(2.0) *\
        header['CDELT2'] * header['CDELT1'] /\
        (np.pi * header['BMAJ'] * header['BMIN'])
    fluxFactor = np.abs(fluxFactor * 1e3)  # convert Jy to mJy.

    '''create masks to filter out seudocores. All the masks are indicating
    the GOOD cores.
    '''
    maskDic = {}
    ot['FLUX_PEAK'] = np.ones_like(ot['FLUX_ISO'])
    ot['FLUXERR_PEAK'] = np.ones_like(ot['FLUX_ISO'])
    for ind in range(len(ot['FLUXERR_PEAK'])):
        ot['FLUX_PEAK'][ind] = data[
            ot['XPEAK_IMAGE'][ind],
            ot['YPEAK_IMAGE'][ind]]
        ot['FLUXERR_PEAK'][ind] = rms[
            ot['XPEAK_IMAGE'][ind],
            ot['YPEAK_IMAGE'][ind]]

    maskDic['size'] = ot["A_IMAGE"] < 0.1 / 2 * 50 / 2 / 0.05  # D/2/pixsize
    maskDic['rms'] = ot['FLUX_PEAK'] > 10. * ot['FLUXERR_PEAK']
    if excludeReg:
        maskDic['reg'] = np.ones_like(ot['A_IMAGE'])
        polygonSet = polygon_reader('large_hii_regions.reg')
        for ind in range(len(ot['A_IMAGE'])):
            point = geo.Point(ot['XPEAK_IMAGE'][ind],
                              ot['YPEAK_IMAGE'][ind])
            for poly in polygonSet:
                if point.within(poly):
                    maskDic['reg'][ind] = 0
                    ind = ind + 1

    maskIntersect = np.array(list(maskDic.values()))
    maskIntersect = np.prod(maskIntersect, axis=0)
    for maskItem in maskDic:
        print(maskItem, maskDic[maskItem].sum())

    ''' assign masked (positively) item of ot to outputParameters
    '''
    outputParameters = {}
    for parameter in list(ot.keys()):
        outputParameters[parameter] = ot[parameter].copy()[maskIntersect == 1]

    '''convert the outputPrameters dictionary to a Pandas DataFrame.
    '''
    outputDf = pd.DataFrame.from_dict(outputParameters, orient='columns')
    outputDf['FLUX_ISO'] = outputDf['FLUX_ISO'] * fluxFactor
    outputDf['FLUXERR_ISO'] = outputDf['FLUXERR_ISO'] * fluxFactor
    outputDf.to_csv('%s.csv' % outputName, index=False)

    if plotImage:
        fig = plt.figure()
        gc3 = aplpy.FITSFigure(dataFile, figure=fig, subplot=(1, 1, 1))
        gc3.recenter(x=266.8340, y=-28.3850, radius=3. / 60.)
        gc3.show_colorscale(cmap='gray', vmin=0.0, vmax=0.01, stretch='asinh')
        gc3.add_colorbar()
        gc3.colorbar.set_axis_label_text('Intensity (Jy/beam)')
        gc3.show_ellipses(ot['X_IMAGE'],
                          ot['Y_IMAGE'],
                          width=ot['A_IMAGE'] * sizeFactor,
                          height=ot['B_IMAGE'] * sizeFactor,
                          angle=ot['THETA_IMAGE'],
                          lw=0.1,
                          facecolor='none',
                          edgecolor=(0.5, 0.5, 1, 1),
                          coords_frame='pixel')
        gc3.show_ellipses(outputDf['X_IMAGE'],
                          outputDf['Y_IMAGE'],
                          width=outputDf['A_IMAGE'] * sizeFactor,
                          height=outputDf['B_IMAGE'] * sizeFactor,
                          angle=outputDf['THETA_IMAGE'],
                          lw=0.1,
                          facecolor='none',
                          edgecolor=(1, 0.5, 0.5, 1),
                          coords_frame='pixel')
        gc3.add_label(0.8, 0.92,
                      '%i / %i' % (len(outputDf['X_IMAGE']),
                                   len(ot['X_IMAGE'])),
                      relative='True',
                      color='gray',
                      size='small')

        fig.savefig('../plot/%s.pdf' % outputName, dpi=900)

    return len(ot['X_IMAGE']), len(outputDf['X_IMAGE'])
Exemplo n.º 11
0
# The following two lines let this script find sewpy, even if sewpy has not yet been installed.
import sys, os
sys.path.insert(0, os.path.abspath('../'))

# Once sewpy is installed, you can start your script from here...

# The next two lines are temporary and will be moved from the minimal demo to the second minimal demo...
import logging
logging.basicConfig(
    format='%(levelname)s: %(name)s(%(funcName)s): %(message)s',
    level=logging.DEBUG)

import sewpy

sew = sewpy.SEW(params=["X_IMAGE", "Y_IMAGE", "FLUX_APER(3)", "FLAGS"],
                config={
                    "DETECT_MINAREA": 10,
                    "PHOT_APERTURES": "5, 10, 20"
                },
                sexpath="sex")
# By default, this assumes that SExtractor can be called as "sex"
# If this is not the case, or if the executable is not in your path,
# specify the path by adding the argument sexpath="/path/to/sextractor"
# to the above instantiation.

out = sew("image.fits")

print out["table"]  # This is an astropy table.
Exemplo n.º 12
0
import unwise_psf
import crowdsource
import psf as cs_psf

#import logging
#logging.basicConfig(format='%(levelname)s: %(name)s(%(funcName)s): %(message)s', level=logging.DEBUG)
sew = sewpy.SEW(
    params=[
        "XPSF_IMAGE", "YPSF_IMAGE", "XPSF_WORLD", "YPSF_WORLD", "MAG_PSF",
        "MAGERR_PSF", "CHI2_PSF", "XWIN_IMAGE", "YWIN_IMAGE", "XWIN_WORLD",
        "YWIN_WORLD", "AWIN_IMAGE", "ERRAWIN_IMAGE", "BWIN_IMAGE",
        "ERRBWIN_IMAGE", "THETAWIN_IMAGE", "ERRTHETAWIN_IMAGE", "X2WIN_IMAGE",
        "ERRX2WIN_IMAGE", "Y2WIN_IMAGE", "ERRY2WIN_IMAGE", "XYWIN_IMAGE",
        "ERRXYWIN_IMAGE", "MAG_AUTO", "MAGERR_AUTO", "MAG_BEST", "MAGERR_BEST",
        "FLUX_BEST", "FLUXERR_BEST", "FLUX_APER(1)", "FLUXERR_APER(1)",
        "BACKGROUND", "MU_MAX", "FWHM_IMAGE", "FLAGS"
    ],
    config={
        "DETECT_MINAREA": 3,
        "PIXEL_SCALE": 2.75,
        "DETECT_THRESH": 0.8,
        "MAG_ZEROPOINT": 22.5,
        #"FILTER_NAME":"/home/ubuntu/backyard-worlds-scripts-dev/byw/post_hunt/sub.conv"
    },
    sexpath="sex")
sew2 = sewpy.SEW(
    params=[
        "MAG_PSF", "MAGERR_PSF", "CHI2_PSF", "MAG_AUTO", "MAGERR_AUTO",
        "MAG_BEST", "MAGERR_BEST", "FLUX_BEST", "FLUXERR_BEST", "FLUX_APER(1)",
        "FLUXERR_APER(1)", "BACKGROUND", "MU_MAX", "FWHM_IMAGE", "FLAGS"
    ],
Exemplo n.º 13
0
def trim_frame(_path,
               _fits_name,
               _win=100,
               _method='sextractor',
               _x=None,
               _y=None,
               _drizzled=True):
    """

    :param _path: path
    :param _fits_name: fits-file name
    :param _win: window width
    :param _method: from 'frames.txt', using 'sextractor', a simple 'max', or 'manual'
    :param _x: source x position -- if known in advance
    :param _y: source y position -- if known in advance
    :param _drizzled: was it drizzled?

    :return: cropped image
    """
    scidata = fits.open(os.path.join(_path, _fits_name))[0].data

    if _method == 'sextractor':
        # extract sources
        sew = sewpy.SEW(params=[
            "X_IMAGE", "Y_IMAGE", "XPEAK_IMAGE", "YPEAK_IMAGE", "A_IMAGE",
            "B_IMAGE", "FWHM_IMAGE", "FLAGS"
        ],
                        config={
                            "DETECT_MINAREA": 10,
                            "PHOT_APERTURES": "10",
                            'DETECT_THRESH': '5.0'
                        },
                        sexpath="sex")

        out = sew(os.path.join(_path, _fits_name))
        # sort by FWHM
        out['table'].sort('FWHM_IMAGE')
        # descending order
        out['table'].reverse()

        print(out['table'])  # This is an astropy table.

        # get first 10 and score them:
        scores = []
        # maximum width of a fix Gaussian. Real sources usually have larger 'errors'
        gauss_error_max = [
            np.max([sou['A_IMAGE'] for sou in out['table'][0:10]]),
            np.max([sou['B_IMAGE'] for sou in out['table'][0:10]])
        ]
        for sou in out['table'][0:10]:
            if sou['FWHM_IMAGE'] > 1:
                score = (log_gauss_score(sou['FWHM_IMAGE']) + gauss_score(
                    rho(sou['X_IMAGE'], sou['Y_IMAGE'])) + np.mean([
                        sou['A_IMAGE'] / gauss_error_max[0],
                        sou['B_IMAGE'] / gauss_error_max[1]
                    ])) / 3.0
            else:
                score = 0  # it could so happen that reported FWHM is 0
            scores.append(score)

        print('scores: ', scores)

        N_sou = len(out['table'])
        # do not crop large planets and crowded fields
        if N_sou != 0 and N_sou < 30:
            # sou_xy = [out['table']['X_IMAGE'][0], out['table']['Y_IMAGE'][0]]
            best_score = np.argmax(scores) if len(scores) > 0 else 0
            # sou_size = np.max((int(out['table']['FWHM_IMAGE'][best_score] * 3), 90))
            # print(out['table']['XPEAK_IMAGE'][best_score], out['table']['YPEAK_IMAGE'][best_score])
            # print(get_xy_from_frames_txt(_path))
            x = out['table']['YPEAK_IMAGE'][best_score]
            y = out['table']['XPEAK_IMAGE'][best_score]
            scidata_cropped = scidata[x - _win:x + _win + 1,
                                      y - _win:y + _win + 1]
        else:
            # use a simple max instead:
            x, y = np.unravel_index(scidata.argmax(), scidata.shape)
            scidata_cropped = scidata[x - _win:x + _win + 1,
                                      y - _win:y + _win + 1]
    elif _method == 'max':
        x, y = np.unravel_index(scidata.argmax(), scidata.shape)
        scidata_cropped = scidata[x - _win:x + _win + 1, y - _win:y + _win + 1]
    elif _method == 'frames.txt':
        y, x = get_xy_from_frames_txt(_path)
        if _drizzled:
            x *= 2.0
            y *= 2.0
        scidata_cropped = scidata[x - _win:x + _win + 1, y - _win:y + _win + 1]
    elif _method == 'manual' and _x is not None and _y is not None:
        x, y = _x, _y
        scidata_cropped = scidata[x - _win:x + _win + 1, y - _win:y + _win + 1]
    else:
        raise Exception('unrecognized trimming method.')

    return scidata_cropped, x, y
Exemplo n.º 14
0
        pyraf.iraftask.gettask("geompar")[0].setParam("x0", center_x)
        pyraf.iraftask.gettask("geompar")[0].setParam("y0", center_y)
        pyraf.iraftask.gettask("geompar")[0].setParam("maxsma", sma)

        iraf.ellipse(filename, galaxy_name + ".tab")
        iraf.bmodel(galaxy_name + ".tab", galaxy_name + "_mod.fits", backgr=bg)
        iraf.imarith(operand1=filename,
                     op="-",
                     operand2=galaxy_name + "_mod.fits",
                     result=galaxy_name + "_modsub.fits")

        for i in range(se_iters):  #TODO
            sew = sewpy.SEW(params=["NUMBER"],
                            config={
                                "DETECT_MINAREA": 150,
                                "DETECT_THRESH": 3,
                                "CHECKIMAGE_NAME": galaxy_name + "_seg.fits",
                                "CATALOG_NAME": galaxy_name + "_modsub.cat",
                                "CHECKIMAGE_TYPE": "SEGMENTATION"
                            })
            sew(galaxy_name + "_modsub.fits")

            iraf.imcopy(galaxy_name + "_seg.fits", filename + ".pl")

            iraf.ellipse(filename, galaxy_name + "_SEx.tab")
            iraf.bmodel(galaxy_name + "_SEx.tab",
                        galaxy_name + "_mod_SEx.fits",
                        backgr=bg)
            iraf.imarith(operand1=filename,
                         op="-",
                         operand2=galaxy_name + "_mod_SEx.fits",
                         result=galaxy_name + "_modsub_SEx.fits")
Exemplo n.º 15
0
if len(sys.argv) < 3:
    print 'Usage:', sys.argv[0], '--path directory path'
    print 'Usage:', sys.argv[0], '--image image.fits'
    sys.exit(1)

print '=============== Source Detection Started ==============='
# Setup sewpy parameters for SExtractor
# Use default files from local directory
sew = sewpy.SEW(
    params=[
        'NUMBER', 'X_IMAGE', 'X2_IMAGE', 'Y_IMAGE', 'Y2_IMAGE', 'FLUX_AUTO',
        'FLUXERR_AUTO', 'MAG_AUTO', 'MAGERR_AUTO', 'CLASS_STAR'
    ],
    configfilepath=
    '/media/data/scripts/bahtinov/default_sextractor/configuration.sex',
    config={
        'FILTER_NAME':
        '/media/data/scripts/bahtinov/default_sextractor/default.conv',
        'STARNNW_NAME':
        '/media/data/scripts/bahtinov/default_sextractor/default.nnw'
    },
    sexpath='/usr/bin/sextractor',
    workdir='/media/data/scripts/bahtinov/SExtractor')

# Determine positions of sources for one image
if args.image is not None:
    file = args.image
    data_reduced = {}
    candidates = {}
    name = (file.split('/')[-1]).split('.')[0]
    candidates = sew(file)
Exemplo n.º 16
0
try:
    img, hdr = fits.getdata("Fits/" + sys.argv[1], header=True)
    science.append(img)
    header.append(hdr)
    img, hdr = fits.getdata("Fits/" + sys.argv[2], header=True)
    science.append(img)
    header.append(hdr)
except:
    print "Files could not be opened!"

cat = list()
i = 0
while (i < 2):
    ret = configuration(header[i])
    sew = sewpy.SEW(params=[
        "NUMBER", "ALPHA_SKY", "DELTA_SKY", "X_IMAGE", "Y_IMAGE", "MAG_AUTO",
        "MAG_MODEL", "MAGERR_AUTO", "MAGERR_MODEL"
    ],
                    config={
                        "GAIN": ret[0],
                        "SATUR_LEVEL": ret[3],
                        "MAG_ZEROPOINT": float(sys.argv[i + 3]),
                        "PIXEL_SCALE": ret[1],
                        "SEEING_FWHM": ret[2]
                    })
    out = sew("Fits/" + sys.argv[i + 1])
    print out["table"]
    cat.append(out["catfilepath"])
    print cat[i]
    i += 1
Exemplo n.º 17
0
        iraf.ellipse(filename, galaxyname + ".tab")

        iraf.bmodel(galaxyname + ".tab",
                    galaxyname + "_mod.fits",
                    backgr=background)
        iraf.imarith(operand1=filename,
                     op="-",
                     operand2=galaxyname + "_mod.fits",
                     result=galaxyname + "_modsub.fits")

        sew = sewpy.SEW(params=["NUMBER"],
                        config={
                            "DETECT_MINAREA": 2,
                            "DETECT_MAXAREA": 700,
                            "DETECT_THRESH": 3,
                            "ANALYSIS_THRESH": 5.0,
                            "DEBLEND_MINCOUNT": 0.002,
                            "CHECKIMAGE_NAME": galaxyname + "_seg.fits",
                            "CATALOG_NAME": galaxyname + "_modsub.cat",
                            "CHECKIMAGE_TYPE": "SEGMENTATION"
                        })

        sew(galaxyname + "_modsub.fits")

        iraf.imcopy(galaxyname + "_seg.fits", filename + ".pl")

        iraf.ellipse(filename, galaxyname + "_SEx.tab")

        iraf.bmodel(galaxyname + "_SEx.tab",
                    galaxyname + "_modSEx.fits",
                    backgr=background)
Exemplo n.º 18
0
# # Try `sewpy`
# working_path = '/Users/mwilde/Dropbox/COS-Gemini/gemini_data.GN-2014A-Q-1/reduced_data/J1555+3628/mask_11/Imaging/'
working_path = './'

import sewpy

config = {
    "CATALOG_NAME": cat_name,
    "SATUR_LEVEL": 50000,
    "MAG_ZEROPOINT": mag_zpt,
    "GAIN": gain,
    "PIXEL_SCALE": pixel_scale,
    "SEEING_FWHM": seeing
}

# instatiate SE wrapper object
sew = sewpy.SEW(params=params,
                config=config,
                sexpath="sex",
                workdir=working_path)

sew.tmp = False
# set teh working dir ... gets weird otherwise
sew.workdir = working_path

# run sextractor on the image
out = sew('mrgN20140327S0155_add.fits')

print(out["table"])
Exemplo n.º 19
0
def run(imagefilename,weightfilename,survey='DES',index='',bigreturn=False):
    print 'inside getsky and skyerr'
    if survey == 'DES':
        sexpath = "sex"
    if survey == 'PS1':
        sexpath = "/export/scratch0/ps1sn1/pipe/v10.0gpc1/photpipe/Cfiles/bin/linux/sex"

    newfilename = '/global/cscratch1/sd/dbrout/sewpy_logs/'+imagefilename.split('/')[-1]

    # im = pf.getdata(imagefilename)
    # dt.save_fits_image(im, newfilename,go=True)
    logging.basicConfig(format='%(levelname)s: %(name)s(%(funcName)s): %(message)s', level=logging.DEBUG)
    sew = sewpy.SEW(
            workdir='/global/cscratch1/sd/dbrout/sewpy_logs/'
            , sexpath=sexpath
            , loglevel="CRITICAL"
            , params = ["X_IMAGE", "Y_IMAGE","ISOAREA_IMAGE"]
            , config={"WEIGHT_TYPE":"NONE,MAP_WEIGHT","WEIGHT_IMAGE":weightfilename
                      ,"back_size":"256"
                      ,"catalog":"test.cat"
                      ,"DETECT MINAREA":"4"
                      ,"THRESH_TYPE":"RELATIVE"
                      ,"DETECT_THRESH":"1.1"
                      }

        )
    out = sew(imagefilename)
    path = out['logfilepath']
    log = open(path, 'r')
    background = -9
    rms = -9
    print 'running S-Extractor'
    for line in log.readlines():
        print line
    print '-'*100
    print out["table"]

    im = pf.getdata(imagefilename)
    wgt = pf.getdata(weightfilename)
    wgt[wgt<1e-5] = np.nan
    wgt[wgt>0] = 1.
    fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
    ax.imshow(np.log10(im*wgt), cmap="Greys")
    from matplotlib.patches import Ellipse
    ells = [Ellipse(xy=(x,y),
                    width=xa ,height=ya,
                    angle=ang)
            for x,y,xa,ya,ang in zip(out["table"]['XWIN_IMAGE'],out["table"]['YWIN_IMAGE'],
                                 out["table"]['AWIN_IMAGE']*np.log10(out["table"]['FLUX_AUTO']+10)*4.5+2,
                                 out["table"]['BWIN_IMAGE']*np.log10(out["table"]['FLUX_AUTO']+10)*4.5+2,
                                 out["table"]['THETAWIN_IMAGE'])]

    for e in ells:
        ax.add_artist(e)
        e.set_clip_box(ax.bbox)
        #e.set_alpha(np.random.rand())
        e.set_facecolor('none')
        e.set_edgecolor('red')
        e.set_linewidth(.2)

    #ax.set_ylim(1000,4000)
    #asdf

    plt.savefig('testext.png',dpi=1000)
    #os.popen('upload testext.png')

    import skimage.draw
    for x, y, xa, ya, ang in zip(out["table"]['XWIN_IMAGE'], out["table"]['YWIN_IMAGE'],
                                         out["table"]['AWIN_IMAGE'] * np.log10(out["table"]['FLUX_AUTO']+10) * 3. + 2,
                                         out["table"]['BWIN_IMAGE'] * np.log10(out["table"]['FLUX_AUTO']+10) * 3. + 2,
                                         out["table"]['THETAWIN_IMAGE']):

        rr, cc = skimage.draw.ellipse(y, x, ya,xa,shape=im.shape, rotation=2*3.14-np.deg2rad(ang))
        im[rr, cc] = np.nan
    plt.clf()
    fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
    ax.imshow(np.log10(im * wgt), cmap="Greys")
    plt.savefig('testextmask.png',dpi=1000)
    #os.popen('upload testextmask.png')



    # from scipy import fftpack
    # F1 = fftpack.fft(im[np.isfinite(im)].ravel().astype(float))
    # F2 = fftpack.fftshift(F1)
    # #psd2D = np.abs(F2) ** 2
    # #psd1D = azimuthalAverage(psd2D)
    # #print psd2D
    # #print psd1D
    # print F1
    # print F2
    # plt.clf()
    # plt.semilogy(F2)
    # plt.xlabel('Spatial Frequency')
    # plt.ylabel('Power')
    # #plt.ylim(-.2e8,.2e8)
    # plt.savefig('plots/noisepower.png', dpi=100)
    # print os.popen('source ~/.bash_profile.ext; upload plots/noisepower.png').read()
    # sys.exit()
    nx,ny = im.shape[0],im.shape[1]

    groupings = [1.,2.,3.,4.,6.,8.,16.,32.,64.]
    resultsdict = {}
    for g in groupings:
        print 'calculating grouping',g
        resultsdict[g] = []
        for x in np.arange(0,nx-64,g):
            if x%100 == 0: print x
        #for x in np.arange(0, 512, g):
            #for y in np.arange(0, 512, g):
            for y in np.arange(0,ny-64,g):
                resultsdict[g].append(np.mean(im[int(x):int(x+g),int(y):int(y+g)]))

        resultsdict[g] = np.array(resultsdict[g])
        print ''
    plt.clf()
    for g in groupings:
        hist, bin_edges = np.histogram(resultsdict[g][np.isfinite(resultsdict[g])], bins=np.arange(-705.,700,10), normed=True)
        bin_centers = (bin_edges[1:] + bin_edges[:-1])/2.
        #print bin_centers
        #raw_input()
        plt.plot(bin_centers,hist,label='Group %d'%g,linewidth=3.,alpha=.75)
        #plt.hist(resultsdict[g][np.isfinite(resultsdict[g])], bins=np.arange(-505,500,10),
        #         type='step', label='Group %d'%g)
    plt.legend()
    plt.xlim(-300,300)
    plt.xlabel('Grouped Pixel Values')
    plt.savefig('plots/correlatednoise.png',dpi=100)
    print os.popen('source ~/.bash_profile.ext; upload plots/correlatednoise.png').read()

    plt.clf()
    stds = []
    for g in groupings:
        stds.append(np.std(resultsdict[g][np.isfinite(resultsdict[g])]))
    stds = np.array(stds)
    groupings = np.array(groupings)
    #plt.plot(np.log(groupings),stds,label='DES IMAGE',linewidth=3)
    #plt.plot(np.log(groupings),stds[0]/np.sqrt(groupings**2),label='1/sqrt(N^2)',linewidth=3)
    plt.plot(np.log(groupings),stds/(stds[0]/np.sqrt(groupings**2)),label='Ratio',linewidth=3)
    plt.grid(True)
    plt.legend()
    plt.xticks(np.log(groupings),groupings)
    plt.xlabel('Pixel Groupings')
    #lt.xlim(-1,7)
    plt.ylabel('Ratio of DES STD / ROOT(Nside^2)')
    plt.savefig('plots/correlatednoisestds.png', dpi=100)
    print os.popen('source ~/.bash_profile.ext; upload plots/correlatednoisestds.png').read()

    print stds
    print stds[0]/np.sqrt(groupings**2)
    return
Exemplo n.º 20
0
def getsky_and_skyerr(imagefilename,weightfilename,im,xlow,xhi,ylow,yhi,survey='DES',index='',bigreturn=False,readin=False):
    print 'inside getsky and skyerr'
    if survey == 'DES':
        sexpath = "sex"
        #sexpath = "/global/common/cori/contrib/hpcosmo/hpcports_gnu-4.0/sextractor-2.19.5_b1614e12-4.0/bin/sex"
        fermigrid  = True
    if survey == 'PS1':
        sexpath = "/export/scratch0/ps1sn1/pipe/v10.0gpc1/photpipe/Cfiles/bin/linux/sex"
        fermigrid = False

    #if not os.path.exists('/global/cscratch1/sd/dbrout/sewpy_logs/'):
    #    os.makedirs('/global/cscratch1/sd/dbrout/sewpy_logs/')
    #newfilename = '/global/cscratch1/sd/dbrout/sewpy_logs/'+index+'trimmed_'+imagefilename.split('/')[-1]
    #print newfilename
    #dt.savefits(im, newfilename,fermigrid=fermigrid)
    #dt.save_fits_image(im, newfilename,go=True)
    #print 'saved trimmed file'
    gotdata = False
    if readin:
        if os.path.exists(imagefilename+'.background'):
            if os.path.exists(imagefilename+'.background_rms'):
                #if os.path.exists(imagefilename+'.bkgrnd.npz'):
                #    grabdata

    if not gotdata:
        logging.basicConfig(format='%(levelname)s: %(name)s(%(funcName)s): %(message)s', level=logging.DEBUG)
        sew = sewpy.SEW(
                workdir='/global/cscratch1/sd/dbrout/sewpy_logs/'
                , sexpath=sexpath
                , loglevel="CRITICAL"
                , config={"WEIGHT_TYPE":"NONE,MAP_WEIGHT","WEIGHT_IMAGE":weightfilename,"checkimage_type":"BACKGROUND,BACKGROUND_RMS","checkimage_name":'/global/cscratch1/sd/dbrout/sewpy_logs/'+index+'_'+imagefilename.split('/')[-1]+
                                                                                          '.background, '+
                                                                                          '/global/cscratch1/sd/dbrout/sewpy_logs/'+index+'_'+imagefilename.split('/')[-1]+
                                                                                          '.background_rms'
                          ,"back_size":"256"}

            )
        out = sew(imagefilename)
        path = out['logfilepath']
        log = open(path, 'r')
        background = -9
        rms = -9
        print 'getting background'
        for line in log.readlines():
            if 'Background:' in line.split(' '):
                background = line.split('Background: ')[1].split(' ')[0]
                rms = line.split('RMS: ')[1].split(' ')[0]

    if bigreturn:
        bkgrnd = pf.getdata(
            '/global/cscratch1/sd/dbrout/sewpy_logs/' + index + '_' + imagefilename.split('/')[-1] + '.background')
        bkgrndrms = pf.getdata(
            '/global/cscratch1/sd/dbrout/sewpy_logs/' + index + '_' + imagefilename.split('/')[-1] + '.background_rms')


    print 'done'*100

    if bigreturn:
        return float(background), float(rms), bkgrnd, bkgrndrms
    #raw_input('sextractor stop')
    return float(background), float(rms)

#im = '/global/cscratch1/sd/dbrout/v3/20130902_SN-S2/r_21/SNp1_230168_SN-S2_tile20_r_21.fits'
#background, rms = getsky_and_skyerr(im)
#print 'bbb', b, 'rms', r
Exemplo n.º 21
0
def make_img(_sou_name, _time, _filter, _prog_num, _camera, _marker,
             _path_sou, _path_data, pipe_out_type, _program_num_planets=24,
             _sr='none'):
    """

    :param _sou_name:
    :param _time:
    :param _filter:
    :param _prog_num:
    :param _camera:
    :param _marker:
    :param _path_sou:
    :param _path_data:
    :param pipe_out_type: 'high_flux' or 'faint'
    :param _program_num_planets:
    :param _sr:
    :return:
    """
    # read, process and display fits:
    hdulist = fits.open(os.path.join(_path_sou, '100p.fits'))
    scidata = hdulist[0].data
    # header:
    header = OrderedDict()
    for entry in hdulist[0].header.cards:
        header[entry[0]] = entry[1:]
    # print(header)

    # extract sources:
    sew = sewpy.SEW(
            # params=["X_IMAGE", "Y_IMAGE", "X2_IMAGE", "Y2_IMAGE",
            #         "A_IMAGE", "B_IMAGE", "FLUX_APER(3)", "FLAGS",
            #         "FWHM_IMAGE", "VIGNET"],
            params=["X_IMAGE", "Y_IMAGE", "XPEAK_IMAGE", "YPEAK_IMAGE",
                    "A_IMAGE", "B_IMAGE",
                    "FWHM_IMAGE", "FLAGS"],
            config={"DETECT_MINAREA": 10, "PHOT_APERTURES": "10", 'DETECT_THRESH': '5.0'},
            sexpath="sex")
    # By default, this assumes that SExtractor can be called as "sex"
    # If this is not the case, or if the executable is not in your path,
    # specify the path by adding the argument sexpath="/path/to/sextractor"
    # to the above instantiation.

    out = sew(os.path.join(_path_sou, '100p.fits'))
    # sort according to FWHM
    out['table'].sort('FWHM_IMAGE')
    # descending order
    out['table'].reverse()

    # print(out['table'])  # This is an astropy table.

    # get first 10 and score them:
    scores = []
    # maximum width of a fix Gaussian. Real sources usually have larger 'errors'
    gauss_error_max = [np.max([sou['A_IMAGE'] for sou in out['table'][0:10]]),
                       np.max([sou['B_IMAGE'] for sou in out['table'][0:10]])]
    for sou in out['table'][0:10]:
        if sou['FWHM_IMAGE'] > 1:
            score = (log_gauss_score(sou['FWHM_IMAGE']) +
                     gauss_score(rho(sou['X_IMAGE'], sou['Y_IMAGE'])) +
                     np.mean([sou['A_IMAGE'] / gauss_error_max[0],
                              sou['B_IMAGE'] / gauss_error_max[1]])) / 3.0
        else:
            score = 0  # it could so happen that reported FWHM is 0
        scores.append(score)

    # print(scores)

    # create a plot

    # bokeh
    # s = figure(x_range=(0, 10), y_range=(0, 10), width=700, plot_height=700, title=None)
    # s.image(image=[scidata], x=0, y=0, dw=10, dh=10, palette="Spectral11")
    # show(s)
    # raise Exception('hola!')

    # seaborn
    norm = np.max(np.max(scidata))
    mask = scidata <= 0
    scidata[mask] = 0
    scidata = np.uint16(scidata/norm*65535)
    # logarithmic_corrected = exposure.adjust_log(scidata, 1)
    # scidata_corrected = logarithmic_corrected
    # print(np.min(np.min(scidata)), np.max(np.max(scidata)))

    # don't do histogram equalization for planets:
    if _sou_name.lower() in ('mars', 'venus', 'jupiter', 'saturn', 'neptune', 'uranus',
                             'callisto', 'ganymede', 'europa', 'io'):
        p_1, p_2 = np.percentile(scidata, (8, 100))
        scidata_corrected = exposure.rescale_intensity(scidata, in_range=(p_1, p_2))
    #
    else:
        # Equalization
        # selem = disk(30)
        # scidata_corrected = rank.equalize(scidata, selem=selem)
        scidata_corrected = exposure.equalize_adapthist(scidata, clip_limit=0.03)

    ''' plot full image '''
    plt.close('all')
    fig = plt.figure(_sou_name+'__full')
    fig.set_size_inches(4, 4, forward=False)
    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.set_axis_off()
    fig.add_axes(ax)
    # plot detected objects:
    # ax.plot(out['table']['X_IMAGE']-1, out['table']['Y_IMAGE']-1, 'o',
    #         markeredgewidth=1, markerfacecolor='None', markeredgecolor=plt.cm.Oranges(0.8))
    # ax.imshow(scidata, cmap='gray', origin='lower', interpolation='nearest')
    ax.imshow(scidata_corrected, cmap='gray', origin='lower', interpolation='nearest')
    # ax.imshow(scidata, cmap='gist_heat', origin='lower', interpolation='nearest')
    # plt.axis('off')
    plt.grid('off')

    # save full figure
    fname_full = '{:d}_{:s}_{:s}_{:s}_{:s}_{:s}_full.png'.format(_prog_num, _sou_name, _camera, _filter, _marker,
                                                  datetime.datetime.strftime(_time, '%Y%m%d_%H%M%S.%f'))
    plt.savefig(os.path.join(_path_data, pipe_out_type, fname_full), dpi=300)

    ''' crop the brightest detected source: '''
    N_sou = len(out['table'])
    # do not crop large planets and crowded fields
    if (_prog_num != _program_num_planets) and (N_sou != 0 and N_sou < 30):
        # sou_xy = [out['table']['X_IMAGE'][0], out['table']['Y_IMAGE'][0]]
        best_score = np.argmax(scores) if len(scores) > 0 else 0
        sou_size = np.max((int(out['table']['FWHM_IMAGE'][best_score] * 3), 90))
        scidata_corrected_cropped = scidata_corrected[out['table']['YPEAK_IMAGE'][best_score] - sou_size/2:
                                                      out['table']['YPEAK_IMAGE'][best_score] + sou_size/2,
                                                      out['table']['XPEAK_IMAGE'][best_score] - sou_size/2:
                                                      out['table']['XPEAK_IMAGE'][best_score] + sou_size/2]
    else:
        scidata_corrected_cropped = scidata_corrected
    # save cropped image
    fig = plt.figure(_sou_name)
    fig.set_size_inches(3, 3, forward=False)
    # ax = fig.add_subplot(111)
    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.set_axis_off()
    fig.add_axes(ax)
    ax.imshow(scidata_corrected_cropped, cmap='gray', origin='lower', interpolation='nearest')
    # add scale bar:
    # draw a horizontal bar with length of 0.1*x_size
    # (ax.transData) with a label underneath.
    bar_len = scidata_corrected_cropped.shape[0]*0.1
    bar_len_str = '{:.1f}'.format(bar_len*36/1024/2)
    asb = AnchoredSizeBar(ax.transData,
                          bar_len,
                          bar_len_str[0] + r"$^{\prime\prime}\!\!\!.$" + bar_len_str[-1],
                          loc=4, pad=0.3, borderpad=0.5, sep=10, frameon=False)
    ax.add_artist(asb)
    # add Strehl ratio
    if _sr != 'none':
        asb2 = AnchoredSizeBar(ax.transData,
                               0,
                               'Strehl: {:.2f}%'.format(float(_sr)),
                               loc=2, pad=0.3, borderpad=0.4, sep=5, frameon=False)
        ax.add_artist(asb2)
        # asb3 = AnchoredSizeBar(ax.transData,
        #                        0,
        #                        'SR: {:.2f}%'.format(float(_sr)),
        #                        loc=3, pad=0.3, borderpad=0.5, sep=10, frameon=False)
        # ax.add_artist(asb3)

    # save cropped figure
    fname_cropped = '{:d}_{:s}_{:s}_{:s}_{:s}_{:s}_cropped.png'.format(_prog_num, _sou_name, _camera, _filter,
                                                                            _marker, datetime.datetime.strftime(_time,
                                                                                             '%Y%m%d_%H%M%S.%f'))
    fig.savefig(os.path.join(_path_data, pipe_out_type, fname_cropped), dpi=300)

    return header
Exemplo n.º 22
0
    def star_coords(self):
        """ Read from coords_file the coordinates of the stars.
        The file must have two columns, with one star per row. If the coordinates are in (RA,DEC) we will transform
        them into image pixels.
        """
        # x, y = np.genfromtxt(self.coords_file, unpack=True)
        # if self._wcs:  # if x,y are not pixels but RA,DEC
        #     with fits.open(self.im_name, 'readonly') as im:
        #         w = wcs.WCS(im[0].header, im)
        #     y, x = w.all_world2pix(x, y, 1)
        # try:
        #     len(x)
        #     return zip(x, y)
        # except:
        #     return [[x, y]]

        try:
            # try sextractor:
            # extract sources:
            sew = sewpy.SEW(params=[
                "X_IMAGE", "Y_IMAGE", "XPEAK_IMAGE", "YPEAK_IMAGE",
                "FWHM_IMAGE", "FLAGS"
            ],
                            config={
                                "DETECT_MINAREA": 10,
                                "PHOT_APERTURES": "10",
                                'DETECT_THRESH': '5.0'
                            },
                            sexpath="sex")
            # By default, this assumes that SExtractor can be called as "sex"
            # If this is not the case, or if the executable is not in your path,
            # specify the path by adding the argument sexpath="/path/to/sextractor"
            # to the above instantiation.

            # create a tmp stacked seeing image, corrected for darks/flats:
            fits_stacked = self.im_name + '.stacked'
            path_calib = os.path.join(
                self.pipe_path,
                datetime.datetime.strftime(self.t_stamp, '%Y%m%d'), 'calib')
            # flat field:
            flat_fits = os.path.join(path_calib,
                                     'flat_{:s}.fits'.format(self.filter))
            # dark current:
            dark_fits = os.path.join(path_calib,
                                     'dark_{:s}.fits'.format(self.mode))
            if not os.path.isfile(fits_stacked):
                hdulist_dark = fits.open(dark_fits)
                hdulist_flat = fits.open(flat_fits)

                dark = hdulist_dark[0].data
                flat = hdulist_flat[0].data

                hdu = fits.PrimaryHDU(
                    (self.im_data / self.nframes - dark) / flat)
                hdu.writeto(fits_stacked)

            # for the above approach to work, the image must first be corrected for flats/darks

            out = sew(fits_stacked)
            # out = sew(self.im_name)
            # sort according to FWHM
            out['table'].sort('FWHM_IMAGE')
            # descending order
            out['table'].reverse()

            # print(out['table'][0:5])
            # print(len(out['table']), self.nframes)
            # print(out['table']['YPEAK_IMAGE'][0], out['table']['XPEAK_IMAGE'][0])
            # print(np.unravel_index(self.im_data.argmax(), self.im_data.shape))

            # get first 5 and score them:
            scores = []
            for sou in out['table'][0:5]:
                if sou['FWHM_IMAGE'] > 1:
                    score = log_gauss_score(sou['FWHM_IMAGE']) + gauss_score(
                        rho(sou['X_IMAGE'], sou['Y_IMAGE']))
                else:
                    score = 0  # it could so happen that reported FWHM is 0
                scores.append(score)
            # print(scores)

            # remove the tmp fits:
            p = subprocess.Popen(['rm', '-f', fits_stacked])
            p.wait()

            # for ii in range(len(out['table'])):
            #     if out['table']['FLAGS'][ii] == 0:
            #         x, y = out['table']['YPEAK_IMAGE'][ii], out['table']['XPEAK_IMAGE'][ii]

            best_score = np.argmax(scores) if len(scores) > 0 else 0
            x, y = out['table']['YPEAK_IMAGE'][best_score], out['table'][
                'XPEAK_IMAGE'][best_score]

        except Exception as err:
            print(str(err))
            # use a simple max instead:
            x, y = np.unravel_index(self.im_data.argmax(), self.im_data.shape)

        return [[x, y]]
Exemplo n.º 23
0
def getsky_and_skyerr(imagefilename,
                      weightfilename,
                      im,
                      xlow,
                      xhi,
                      ylow,
                      yhi,
                      survey='DES',
                      index='',
                      bigreturn=False):
    print 'inside getsky and skyerr'
    if survey == 'DES':
        sexpath = "/global/common/cori/contrib/hpcosmo/hpcports_gnu-4.0/sextractor-2.19.5_b1614e12-4.0/bin/sex"
        #sexpath = "/global/common/cori/contrib/hpcosmo/hpcports_gnu-4.0/sextractor-2.19.5_b1614e12-4.0/bin/sex"
        fermigrid = True
    if survey == 'PS1':
        sexpath = "/export/scratch0/ps1sn1/pipe/v10.0gpc1/photpipe/Cfiles/bin/linux/sex"
        fermigrid = False

    #im = pf.getdata(imagefilename)
    #hdr = pf.getheader(imagefilename)
    #im = im[ylow:yhi,xlow:xhi]
    logpath = '/global/cscratch1/sd/dbrout//sewpy_logs/'
    if not os.path.exists(logpath):
        os.makedirs(logpath)
    newfilename = logpath + index + 'trimmed_' + imagefilename.split('/')[-1]
    print newfilename
    #dt.savefits(im, newfilename,fermigrid=fermigrid)
    dt.save_fits_image(im, newfilename, go=True)
    print 'saved trimmed file'
    logging.basicConfig(
        format='%(levelname)s: %(name)s(%(funcName)s): %(message)s',
        level=logging.DEBUG)
    sew = sewpy.SEW(workdir=logpath,
                    sexpath=sexpath,
                    loglevel="CRITICAL",
                    config={
                        "WEIGHT_TYPE":
                        "NONE,MAP_WEIGHT",
                        "WEIGHT_IMAGE":
                        weightfilename,
                        "checkimage_type":
                        "BACKGROUND,BACKGROUND_RMS",
                        "checkimage_name":
                        logpath + index + '_' + imagefilename.split('/')[-1] +
                        '.background, ' + logpath + index + '_' +
                        imagefilename.split('/')[-1] + '.background_rms',
                        "back_size":
                        "256"
                    })
    out = sew(newfilename)
    path = out['logfilepath']
    log = open(path, 'r')
    background = -9
    rms = -9
    print 'getting background'
    for line in log.readlines():
        if 'Background:' in line.split(' '):
            background = line.split('Background: ')[1].split(' ')[0]
            rms = line.split('RMS: ')[1].split(' ')[0]

    if bigreturn:
        bkgrnd = pf.getdata(logpath + index + '_' +
                            imagefilename.split('/')[-1] + '.background')
        bkgrndrms = pf.getdata(logpath + index + '_' +
                               imagefilename.split('/')[-1] +
                               '.background_rms')

    print 'removing files'
    try:
        os.remove(newfilename)
        os.remove(newfilename.split('.fits')[0] + '.cat.txt')
        os.remove(newfilename.split('.fits')[0] + '.log.txt')

        os.remove(logpath + index + '_' + imagefilename.split('/')[-1] +
                  '.background')
        os.remove(logpath + index + '_' + imagefilename.split('/')[-1] +
                  '.background_rms')
    except:
        pass

    print 'done' * 100

    if bigreturn:
        return float(background), float(rms), bkgrnd, bkgrndrms
    #raw_input('sextractor stop')
    return float(background), float(rms)
Exemplo n.º 24
0
# The following two lines let this script find sewpy, even if sewpy has not yet been installed.
import sys, os

sys.path.insert(0, os.path.abspath('../'))

# Once sewpy is installed, you can start your script from here...

# The next two lines are temporary and will be moved from the minimal demo to the second minimal demo...
import logging

logging.basicConfig(
    format='%(levelname)s: %(name)s(%(funcName)s): %(message)s',
    level=logging.DEBUG)

import sewpy

sew = sewpy.SEW(params=["X_IMAGE", "Y_IMAGE", "FLUX_APER(3)", "FLAGS"],
                config={
                    "DETECT_MINAREA": 10,
                    "PHOT_APERTURES": "5, 10, 20"
                },
                sexpath="/data/marvels/billzhu/sextractor-2.19.5/")
# By default, this assumes that SExtractor can be called as "sex"
# If this is not the case, or if the executable is not in your path,
# specify the path by adding the argument sexpath="/path/to/sextractor"
# to the above instantiation.

out = sew("image.fits")

print(out["table"])  # This is an astropy table.
Exemplo n.º 25
0
params = [
    "X_IMAGE", "Y_IMAGE", 'X_WORLD', 'Y_WORLD', "FLUX_RADIUS(3)", "FLAGS",
    'FWHM_IMAGE', 'FWHM_WORLD', 'KRON_RADIUS', 'PETRO_RADIUS', 'THETA_IMAGE',
    'A_IMAGE', 'B_IMAGE', 'A_WORLD', 'B_WORLD', 'ELLIPTICITY', 'CLASS_STAR'
]

# Sextractor short cut
sextractor_name = 'sextractor'
TEMP_PANSTARRS_PATH = './temp/'

try:
    sewpy.SEW(params=params,
              config={
                  "DETECT_MINAREA": 10,
                  "PHOT_FLUXFRAC": "0.3, 0.5, 0.8",
                  'SATUR_LEVEL': 1111060,
                  'SEEING_FWHM': 1.31,
                  'GAIN': 1.030748,
                  'PIXEL_SCALE': 0.25
              })
except RuntimeError:
    sextractor_name = 'sex'

# SExtractor properties for the different bands
se = {
    'g':
    sewpy.SEW(params=params,
              config={
                  "DETECT_MINAREA": 10,
                  "PHOT_FLUXFRAC": "0.3, 0.5, 0.8",
                  'SATUR_LEVEL': 1111060,