Example #1
0
def results(response):
    info = VISinformation()

    zodi = response.getvalue('zodi')
    magnitude = float(response.getvalue('magnitude'))
    exptime = float(response.getvalue('exptime'))
    obj = response.getvalue('object')
    exposures = int(response.getvalue('exposures'))
    ETC.SNRproptoPeak(info, exptime=exptime, exposures=exposures, server=True)

    if obj == 'galaxy':
        galaxy = True
    else:
        galaxy = False

    exp = ETC.exposureTime(info, magnitude, exposures=exposures, galaxy=galaxy, fudge=1.0)
    limit = ETC.limitingMagnitude(info, exp=exptime, galaxy=galaxy, exposures=exposures, fudge=1.0)
    snr = ETC.SNR(info, magnitude=magnitude, exptime=exptime, exposures=exposures, galaxy=galaxy)

    print "<html>"
    print "<head> <center> <b>Results</b> </center></head> "
    print "<body style='background-color:Moccasin;''>"

    print '<br><b> Results </b><br>'
    print 'Exposure time required to reach SNR=10.0 given a %.2f magnitude %s is %.1f seconds ' \
          'if combining %i exposures<br><br>' % (magnitude, obj, exp, exposures)
    print 'SNR=%f for %.2fmag %s if exposure time is %.2f seconds<br><br>' % (snr, magnitude, obj, exptime*exposures)
    print 'Limiting magnitude of a %s for %.2f second exposure is %.2f<br><br>' % (obj, exptime, limit)

    print "<p><img src='../delete.png'/></p>"

    print "</body>"
def objectDetection(log,
                    magnitude=24.5,
                    exptime=565,
                    exposures=3,
                    fpeak=0.7,
                    offset=0.5,
                    covering=2550,
                    ghostlevels=(6e-7, 1e-6, 4e-6, 5e-6, 1e-5, 5e-5)):
    """
    Derive area loss in case of object detection i.e. the SNR drops below the requirement.
    Assumes that a ghost covers the covering number of pixels and that the peak pixel of a
    point source contains fpeak fraction of the total counts. An offset between the V-band
    and VIS band is applied as VIS = V + offset.

    :param log: logger instance
    :param magnitude: magnitude limit of the objects to be detected
    :param exptime: exposure time of an individual exposure in seconds
    :param exposures: number of exposures combined in the data analysis
    :param fpeak: PSF peak fraction
    :param offset: offset between the V-band and the VIS band, VIS = V + offset
    :param covering: covering fraction of a single ghost in pixels
    :param ghostlevels: a tuple of ghost levels to inspect
    :param ghostlevels: tuple

    :return: None
    """
    info = VISinformation()
    ghoste, ghoste2 = ETC.galaxyDetection(info,
                                          magnitude=magnitude,
                                          exptime=exptime,
                                          exposures=exposures)

    print '-' * 100
    print '\n\n\nObject Detection, a ghost covers %i pixels' % covering
    print '-' * 100
    res = []
    for ghostreq in ghostlevels:
        maglimit = info['zeropoint'] - 5 / 2. * np.log10(
            (ghoste) / (exptime * exposures * fpeak * ghostreq))

        txt = 'Limiting VIS magnitude for object detection = %.2f if %.2f extra electrons from ghost of ratio %.1e' % \
          (maglimit, ghoste, ghostreq)

        print txt
        log.info(txt)

        res.append(maglimit)

    _numberOfStarsAreaLoss(
        log,
        res,
        r'Object Detection Area Loss Because of Ghosts from Stars',
        offset=offset,
        covering=covering)
Example #3
0
def plotSNRfromCatalog(catalogname):
    #read in data
    catalog = np.loadtxt(catalogname, usecols=(2, 3))
    st = catalog[:, 0][catalog[:, 1] < 1]  #stars, magnitudes
    gal = catalog[:, 0][catalog[:, 1] > 7]  #galaxies, mags

    bins = np.linspace(0, 700, 31)
    df = bins[1] - bins[0]

    weight = 1. / (2048 * 2 * 2066 * 2. * 0.1 * 0.1 * 7.71604938e-8
                   ) / df  #how many square degrees one CCD is on sky
    SNRs = ETC.SNR(VISinformation(), magnitude=st, exposures=1, galaxy=False)
    SNRg = ETC.SNR(VISinformation(), magnitude=gal, exposures=1)

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.hist(SNRs,
            bins=bins,
            alpha=0.5,
            log=True,
            weights=[
                weight,
            ] * len(st),
            label='Stars')
    ax.hist(SNRg,
            bins=bins,
            alpha=0.2,
            log=True,
            weights=[
                weight,
            ] * len(gal),
            label='Galaxies')
    ax.set_xlabel('SNR [assuming 565s exposure]')
    ax.set_ylabel(r'N [deg$^{-2}$ dex$^{-1}$]')
    plt.legend(shadow=True, fancybox=True)
    plt.savefig(catalogname + 'SNRs.pdf')
    plt.close()
def objectDetection(log, magnitude=24.5, exptime=565, exposures=3, fpeak=0.7, offset=0.5, covering=2550,
                    ghostlevels=(6e-7, 1e-6, 4e-6, 5e-6, 1e-5, 5e-5)):
    """
    Derive area loss in case of object detection i.e. the SNR drops below the requirement.
    Assumes that a ghost covers the covering number of pixels and that the peak pixel of a
    point source contains fpeak fraction of the total counts. An offset between the V-band
    and VIS band is applied as VIS = V + offset.

    :param log: logger instance
    :param magnitude: magnitude limit of the objects to be detected
    :param exptime: exposure time of an individual exposure in seconds
    :param exposures: number of exposures combined in the data analysis
    :param fpeak: PSF peak fraction
    :param offset: offset between the V-band and the VIS band, VIS = V + offset
    :param covering: covering fraction of a single ghost in pixels
    :param ghostlevels: a tuple of ghost levels to inspect
    :param ghostlevels: tuple

    :return: None
    """
    info = VISinformation()
    ghoste, ghoste2 = ETC.galaxyDetection(info, magnitude=magnitude, exptime=exptime, exposures=exposures)

    print '-'*100
    print '\n\n\nObject Detection, a ghost covers %i pixels' % covering
    print '-'*100
    res = []
    for ghostreq in ghostlevels:
        maglimit = info['zeropoint'] - 5/2.*np.log10((ghoste) / (exptime*exposures*fpeak*ghostreq))

        txt = 'Limiting VIS magnitude for object detection = %.2f if %.2f extra electrons from ghost of ratio %.1e' % \
          (maglimit, ghoste, ghostreq)

        print txt
        log.info(txt)

        res.append(maglimit)

    _numberOfStarsAreaLoss(log, res, r'Object Detection Area Loss Because of Ghosts from Stars',
                           offset=offset, covering=covering)
Example #5
0
def plotSNR(deg=60, kdes=True, log=False):
    CCDs = 1000
    fudge = 47.0

    #cumulative distribution of stars for different galactic latitudes
    if deg == 30:
        tmp = 1
        sfudge = 0.79
    elif deg == 60:
        tmp = 2
        sfudge = 0.79
    else:
        tmp = 3
        sfudge = 0.78

    #stars
    d = np.loadtxt('data/stars.dat', usecols=(0, tmp))
    stmags = d[:, 0]
    stcounts = d[:, 1]

    #fit a function and generate finer sample
    z = np.polyfit(stmags, np.log10(stcounts), 4)
    p = np.poly1d(z)
    starmags = np.arange(1, 30.2, 0.2)
    starcounts = 10**p(starmags)

    cpdf = (starcounts - np.min(starcounts)) / (np.max(starcounts) -
                                                np.min(starcounts))
    starcounts /= 3600.  #convert to square arcseconds
    nstars = int(np.max(starcounts) * fudge * sfudge) * CCDs
    magStars = cr.drawFromCumulativeDistributionFunction(
        cpdf, starmags, nstars)
    SNRsStars = ETC.SNR(ETC.VISinformation(),
                        magnitude=magStars,
                        exposures=1,
                        galaxy=False)

    print 'Assuming Galactic Lattitude = %i deg' % deg
    print 'Number of stars within a pointing (36CCDs) with 70 < SNR < 700 (single 565s exposure):', \
            int((SNRsStars[(SNRsStars > 70) & (SNRsStars < 700)]).size * 36. / CCDs)
    print 'Number of stars within a pointing (36CCDs) with 60 < SNR < 80 (single 565s exposure):', \
            int((SNRsStars[(SNRsStars > 60) & (SNRsStars < 80)]).size * 36. / CCDs)
    print 'Number of stars within a pointing (36CCDs) with 690 < SNR < 710 (single 565s exposure):', \
            int((SNRsStars[(SNRsStars > 690) & (SNRsStars < 710)]).size * 36. / CCDs)
    print 'Number of stars within a pointing (36CCDs) with 18 < mag < 22 (single 565s exposure):', \
            int((SNRsStars[(magStars > 18) & (magStars < 22)]).size * 36. / CCDs)
    print 'Number of stars within a pointing (36CCDs) with 18 < mag < 23 (single 565s exposure):', \
            int((SNRsStars[(magStars > 18) & (magStars < 23)]).size * 36. / CCDs)
    print 'Number of stars within a pointing (36CCDs) with 17.9 < mag < 18.1 (single 565s exposure):', \
            int((SNRsStars[(magStars > 17.9) & (magStars < 18.1)]).size * 36. / CCDs)
    print 'Number of stars within a pointing (36CCDs) with 21 < mag < 23 (single 565s exposure):', \
            int((SNRsStars[(magStars > 21) & (magStars < 23)]).size * 36. / CCDs)

    #calculate Gaussian KDE with statsmodels package (for speed)
    if kdes:
        kn = SNRsStars[SNRsStars < 1000]
        kdeStars = KDE(kn)
        kdeStars.fit(adjust=2)
        nst = kn.size / 10. / 1.38

    #galaxies
    #cumulative distribution of galaxies
    d = np.loadtxt('data/cdf_galaxies.dat', usecols=(0, 1))
    gmags = d[:, 0]
    gcounts = d[:, 1]
    nums = int(np.max(gcounts) / 3600. * fudge * CCDs)
    z = np.polyfit(gmags, np.log10(gcounts), 4)
    p = np.poly1d(z)
    galaxymags = np.arange(10.0, 30.2, 0.2)
    galaxycounts = 10**p(galaxymags)
    cumulative = (galaxycounts - np.min(galaxycounts)) / (
        np.max(galaxycounts) - np.min(galaxycounts))
    magGalaxies = cr.drawFromCumulativeDistributionFunction(
        cumulative, galaxymags, nums)
    SNRsGalaxies = ETC.SNR(VISinformation(),
                           magnitude=magGalaxies,
                           exposures=1)

    #calculate Gaussian KDE, this time with scipy to save memory, and evaluate it
    if kdes:
        kn = SNRsGalaxies[SNRsGalaxies < 1000]
        #pos = np.linspace(1, 810, num=70)
        #kdegal = gaussian_kde(kn)
        #gals = kdegal(pos)
        #ngl = kn.size #/ df
        kdeGalaxy = KDE(kn)
        kdeGalaxy.fit(adjust=10)
        ngl = kn.size / 10. / 1.38

    #histogram binning and weighting
    bins = np.linspace(0., 1000., 101)
    df = bins[1] - bins[0]
    weight = 1. / (2048 * 2 * 2066 * 2 * 0.1 * 0.1 * 7.71604938e-8 * CCDs) / df
    weightsS = np.ones(magStars.size) * weight
    weightsG = np.ones(magGalaxies.size) * weight

    #simple magnitude distribution plot for stars
    stars = np.loadtxt('data/stars.dat')
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.hist(magStars,
            bins=30,
            cumulative=True,
            log=True,
            alpha=0.3,
            weights=weightsS * df,
            label='Random Draws')
    ax.semilogy(stars[:, 0], stars[:, 1], label='Stars (30deg)')
    ax.semilogy(stars[:, 0], stars[:, 2], label='Stars (60deg)')
    ax.semilogy(stars[:, 0], stars[:, 3], label='Stars (90deg)')
    ax.set_xlabel(r'$M_{AB}$')
    ax.set_ylabel(r'Cumulative Number of Objects [deg$^{-2}$]')
    plt.legend(shadow=True, fancybox=True, loc='upper left')
    plt.savefig('stars%ideg.pdf' % deg)
    plt.close()

    #make a plot
    txt = '%s' % datetime.datetime.isoformat(datetime.datetime.now())
    ax = host_subplot(111, axes_class=AA.Axes)

    hist1 = ax.hist(SNRsStars,
                    bins=bins,
                    alpha=0.2,
                    log=True,
                    weights=weightsS,
                    label='Stars [%i deg]' % deg,
                    color='r')
    hist2 = ax.hist(SNRsGalaxies,
                    bins=bins,
                    alpha=0.2,
                    log=True,
                    weights=weightsG,
                    label='Galaxies',
                    color='blue')

    if kdes:
        ax.plot(kdeStars.support,
                kdeStars.density * nst,
                'r-',
                label='Gaussian KDE (stars)')
        #ax.plot(pos, gals*ngl, 'b-', label='Gaussian KDE (galaxies)')
        ax.plot(kdeGalaxy.support,
                kdeGalaxy.density * ngl,
                'b-',
                label='Gaussian KDE (galaxies)')

    #calculate magnitude scale, top-axis
    if log:
        mags = np.asarray([17, 18, 19, 20, 21, 22, 23, 24])
        SNRs = ETC.SNR(VISinformation(),
                       magnitude=mags,
                       exposures=1,
                       galaxy=False)
    else:
        mags = np.asarray([17, 17.5, 18, 18.5, 19, 20, 21, 22.5])
        SNRs = ETC.SNR(VISinformation(),
                       magnitude=mags,
                       exposures=1,
                       galaxy=False)

    ax2 = ax.twin()  # ax2 is responsible for "top" axis and "right" axis
    ax2.set_xticks(SNRs)
    ax2.set_xticklabels([str(tmp) for tmp in mags])
    ax2.set_xlabel('$M(R+I)_{AB}$ [mag]')
    ax2.axis['right'].major_ticklabels.set_visible(False)

    ax.set_ylim(1e-1, 1e5)

    ax.set_ylabel('Number of Objects [deg$^{-2}$ dex$^{-1}$]')
    ax.set_xlabel('Signal-to-Noise Ratio [assuming a single 565s exposure]')

    plt.text(0.8,
             1.12,
             txt,
             ha='left',
             va='top',
             fontsize=9,
             transform=ax.transAxes,
             alpha=0.2)
    plt.legend(shadow=True, fancybox=True)

    if log:
        ax.set_xscale('log')
        plt.savefig('SNRtheoretical%ideglog.pdf' % deg)
    else:
        ax.set_xlim(1, 1e3)
        plt.savefig('SNRtheoretical%ideglin.pdf' % deg)

    plt.close()

    #write output
    if not log:
        mid = df / 2.
        #output to file
        fh = open('SNRsSTARS%ideg.txt' % deg, 'w')
        fh.write('#These values are for stars at %ideg (%s)\n' % (deg, txt))
        fh.write('#SNR number_of_stars  N\n')
        fh.write('#bin_centre per_square_degree per_pointing\n')
        for a, b in zip(hist1[0], hist1[1]):
            fh.write('%i %f %f\n' % (b + mid, a * df, a * df * 0.496))
        fh.close()
        fh = open('SNRsGALAXIES.txt', 'w')
        fh.write('#These values are for galaxies (%s)\n' % txt)
        fh.write('#SNR number_of_galaxies   N\n')
        fh.write('#bin_centre per_square_degree per_pointing\n')
        for a, b in zip(hist2[0], hist2[1]):
            fh.write('%i %f %f\n' % (b + mid, a * df, a * df * 0.496))
        fh.close()
Example #6
0
def compareSNR(catalog, max=33, noNoise=False):
    """
    Compare SExtracted SNR to radiometric model from ETC module.
    """
    txt = '%s' % datetime.datetime.isoformat(datetime.datetime.now())

    #calculate input SNRs
    info = VISinformation()
    if noNoise:
        info.update(dict(sky_background=0.0, dark=0.0, readnoise=0.0, zodiacal=0.0))
        SNRs = ETC.SNR(info, magnitude=catalog.mag_input, exposures=1, galaxy=False, background=False)
    else:
        SNRs = ETC.SNR(info, magnitude=catalog.mag_input, exposures=1, galaxy=False)
    #print SNRs
    Sextracted = catalog.flux_aper / catalog.fluxerr_aper

    fig = plt.figure(frameon=False)

    left, width = 0.1, 0.8
    rect1 = [left, 0.3, width, 0.65]
    rect2 = [left, 0.1, width, 0.2]

    ax1 = fig.add_axes(rect1, title='VIS Simulator SNR Comparison')
    ax2 = fig.add_axes(rect2)  #left, bottom, width, height

    ax1.plot([0, max], [0, max], 'k--')
    ax1.scatter(SNRs, Sextracted, c='b', marker='o', s=10, edgecolor='None', label='r=0.65 Aperture')

    ax2.plot([0, max], [1, 1], 'k--')
    ax2.plot([0, max], [0.7, 0.7], 'r:')
    ax2.scatter(SNRs, Sextracted/SNRs.copy(), c='b', marker='o', s=10, edgecolor='None')

    ax2.set_xlabel('Input SNR')
    ax1.set_ylabel('Extracted SNR')
    ax2.set_ylabel(r'$\frac{SE}{Input}$')

    ax1.set_xticklabels([])
    #ax1.set_yticks(ax1.get_yticks()[1:])
    #ax2.set_yticks(ax2.get_yticks()[::2])

    ax1.set_xlim(0, max)
    ax1.set_ylim(0, max)
    ax2.set_xlim(0, max)
    ax2.set_ylim(0.2, 1.13)

    ax1.text(0.83, 1.12, txt, ha='left', va='top', fontsize=9, transform=ax1.transAxes, alpha=0.2)
    ax1.legend(shadow=True, fancybox=True, numpoints=1, scatterpoints=1, markerscale=1.0, loc='upper left')

    plt.savefig('SNRs.pdf')
    plt.close()

    #for "best" setting
    #Sextr = catalog.flux_best / catalog.fluxerr_best
    #Sextr = 1./(catalog.magerr_auto/1.0857)
    Sextr = 1./catalog.magerr_aper

    fig = plt.figure(frameon=False)

    left, width = 0.1, 0.8
    rect1 = [left, 0.3, width, 0.65]
    rect2 = [left, 0.1, width, 0.2]

    ax1 = fig.add_axes(rect1, title='VIS Simulator SNR Comparison')
    ax2 = fig.add_axes(rect2)  #left, bottom, width, height

    ax1.plot([0, max], [0, max], 'k--')
    ax1.scatter(SNRs, Sextr, c='b', marker='o', s=10, edgecolor='None', label='r=0.65 Aperture')

    ax2.plot([0, max], [1, 1], 'k--')
    ax2.plot([0, max], [0.7, 0.7], 'r:')
    ax2.scatter(SNRs, Sextr/SNRs.copy(), c='b', marker='o', s=10, edgecolor='None')

    ax2.set_xlabel('Input SNR')
    ax1.set_ylabel('Extracted SNR')
    ax2.set_ylabel(r'$\frac{SE}{Input}$')

    ax1.set_xticklabels([])
    ax1.set_yticks(ax1.get_yticks()[1:])
    ax2.set_yticks(ax2.get_yticks()[::2])

    ax1.set_xlim(0, max)
    ax1.set_ylim(0, max)
    ax2.set_xlim(0, max)
    ax2.set_ylim(0.2, 1.2)

    ax1.text(0.83, 1.12, txt, ha='left', va='top', fontsize=9, transform=ax1.transAxes, alpha=0.2)
    ax1.legend(shadow=True, fancybox=True, numpoints=1, scatterpoints=1, markerscale=1.0, loc='upper left')

    plt.savefig('SNRs2.pdf')
    plt.close()