def __getOpSimMjd(self, opsim, ra, dec, fil):
        colmn = 'observationStartMJD'
        opsdb = db.OpsimDatabase(opsim)

        # Directory where tmp files are going to be stored TODO eliminate - this
        outDir = 'TmpDir'
        resultsDb = db.ResultsDb(outDir=outDir)

        metric = metrics.PassMetric(cols=[colmn, 'fiveSigmaDepth', 'filter'])
        slicer = slicers.UserPointsSlicer(ra=ra, dec=dec)
        sqlconstraint = 'filter = \'' + fil + '\''

        bundle = mb.MetricBundle(metric, slicer, sqlconstraint, runName='name')
        bgroup = mb.MetricBundleGroup({0: bundle},
                                      opsdb,
                                      outDir=outDir,
                                      resultsDb=resultsDb)
        bgroup.runAll()

        filters = np.unique(bundle.metricValues[0]['filter'])
        mv = bundle.metricValues[0]

        # Get dates
        mjd = mv[colmn]
        mjd = np.sort(mjd)
        print('Num of visits ' + str(len(mjd)) + ' ' + opsim)
        return mjd
示例#2
0
def get_cadence(ra, dec, b, snrLimit, nPtsLimit, filters, outDir, opsimdb,
                resultsDb):

    # The pass metric just passes data straight through.
    metric = metrics.PassMetric(cols=['filter', 'fiveSigmaDepth', 'expMJD'])
    slicer = slicers.UserPointsSlicer(ra,
                                      dec,
                                      lonCol='ditheredRA',
                                      latCol='ditheredDec')
    sql = ''
    bundle = metricBundles.MetricBundle(metric, slicer, sql)
    bg = metricBundles.MetricBundleGroup({0: bundle},
                                         opsimdb,
                                         outDir=outDir,
                                         resultsDb=resultsDb)

    bg.runAll()
    bundle.metricValues.data[0]['filter']

    print("Plotting...")
    colors = {'u': 'cyan', 'g': 'g', 'r': 'y', 'i': 'r', 'z': 'm', 'y': 'k'}
    dayZero = bundle.metricValues.data[0]['expMJD'].min()
    times = []
    depths = []
    plt.clf()
    for fname in filters:
        good = np.where(bundle.metricValues.data[0]['filter'] == fname)
        times.append(bundle.metricValues.data[0]['expMJD'][good] - dayZero)
        depths.append(bundle.metricValues.data[0]['fiveSigmaDepth'][good])

        plt.scatter(bundle.metricValues.data[0]['expMJD'][good] - dayZero,
                    bundle.metricValues.data[0]['fiveSigmaDepth'][good],
                    c=colors[fname],
                    label=fname)

    plt.xlabel('Day')
    plt.ylabel('5$\sigma$ depth')
    plt.legend(scatterpoints=1, loc="upper left", bbox_to_anchor=(1, 1))
    plt.savefig("l45b{0}_cadence.pdf".format(int(b)))

    return times, depths
示例#3
0
def makeBundleList(dbFile, runName=None, nside=64, benchmark='design',
                   lonCol='fieldRA', latCol='fieldDec', seeingCol='seeingFwhmGeom'):
    """
    make a list of metricBundle objects to look at the scientific performance
    of an opsim run.
    """

    # List to hold everything we're going to make
    bundleList = []

    # List to hold metrics that shouldn't be saved
    noSaveBundleList = []

    # Connect to the databse
    opsimdb = db.OpsimDatabaseV4(dbFile)
    if runName is None:
        runName = os.path.basename(dbFile).replace('_sqlite.db', '')

    # Fetch the proposal ID values from the database
    propids, propTags = opsimdb.fetchPropInfo()

    # Fetch the telescope location from config
    lat, lon, height = opsimdb.fetchLatLonHeight()

    # Add metadata regarding dithering/non-dithered.
    commonname = ''.join([a for a in lonCol if a in latCol])
    if commonname == 'field':
        slicermetadata = ' (non-dithered)'
    else:
        slicermetadata = ' (%s)' % (commonname)

    # Construct a WFD SQL where clause so multiple propIDs can query by WFD:
    wfdWhere = opsimdb.createSQLWhere('WFD', propTags)
    print('#FYI: WFD "where" clause: %s' % (wfdWhere))
    ddWhere = opsimdb.createSQLWhere('DD', propTags)
    print('#FYI: DD "where" clause: %s' % (ddWhere))

    # Set up benchmark values, scaled to length of opsim run.
    runLength = opsimdb.fetchRunLength()
    if benchmark == 'requested':
        # Fetch design values for seeing/skybrightness/single visit depth.
        benchmarkVals = utils.scaleBenchmarks(runLength, benchmark='design')
        # Update nvisits with requested visits from config files.
        benchmarkVals['nvisits'] = opsimdb.fetchRequestedNvisits(propId=propTags['WFD'])
        # Calculate expected coadded depth.
        benchmarkVals['coaddedDepth'] = utils.calcCoaddedDepth(benchmarkVals['nvisits'],
                                                               benchmarkVals['singleVisitDepth'])
    elif (benchmark == 'stretch') or (benchmark == 'design'):
        # Calculate benchmarks for stretch or design.
        benchmarkVals = utils.scaleBenchmarks(runLength, benchmark=benchmark)
        benchmarkVals['coaddedDepth'] = utils.calcCoaddedDepth(benchmarkVals['nvisits'],
                                                               benchmarkVals['singleVisitDepth'])
    else:
        raise ValueError('Could not recognize benchmark value %s, use design, stretch or requested.'
                         % (benchmark))
    # Check that nvisits is not set to zero (for very short run length).
    for f in benchmarkVals['nvisits']:
        if benchmarkVals['nvisits'][f] == 0:
            print('Updating benchmark nvisits value in %s to be nonzero' % (f))
            benchmarkVals['nvisits'][f] = 1

    # Set values for min/max range of nvisits for All/WFD and DD plots. These are somewhat arbitrary.
    nvisitsRange = {}
    nvisitsRange['all'] = {'u': [20, 80], 'g': [50, 150], 'r': [100, 250],
                           'i': [100, 250], 'z': [100, 300], 'y': [100, 300]}
    nvisitsRange['DD'] = {'u': [6000, 10000], 'g': [2500, 5000], 'r': [5000, 8000],
                          'i': [5000, 8000], 'z': [7000, 10000], 'y': [5000, 8000]}
    # Scale these ranges for the runLength.
    scale = runLength / 10.0
    for prop in nvisitsRange:
        for f in nvisitsRange[prop]:
            for i in [0, 1]:
                nvisitsRange[prop][f][i] = int(np.floor(nvisitsRange[prop][f][i] * scale))

    # Filter list, and map of colors (for plots) to filters.
    filters = ['u', 'g', 'r', 'i', 'z', 'y']
    colors = {'u': 'cyan', 'g': 'g', 'r': 'y', 'i': 'r', 'z': 'm', 'y': 'k'}
    filtorder = {'u': 1, 'g': 2, 'r': 3, 'i': 4, 'z': 5, 'y': 6}

    # Easy way to run through all fi

    # Set up a list of common summary stats
    commonSummary = [metrics.MeanMetric(), metrics.RobustRmsMetric(), metrics.MedianMetric(),
                     metrics.PercentileMetric(metricName='25th%ile', percentile=25),
                     metrics.PercentileMetric(metricName='75th%ile', percentile=75),
                     metrics.MinMetric(), metrics.MaxMetric()]
    allStats = commonSummary

    # Set up some 'group' labels
    reqgroup = 'A: Required SRD metrics'
    depthgroup = 'B: Depth per filter'
    uniformitygroup = 'C: Uniformity'
    airmassgroup = 'D: Airmass distribution'
    seeinggroup = 'E: Seeing distribution'
    transgroup = 'F: Transients'
    sngroup = 'G: SN Ia'
    altAzGroup = 'H: Alt Az'
    rangeGroup = 'I: Range of Dates'
    intergroup = 'J: Inter-Night'
    phaseGroup = 'K: Max Phase Gap'
    NEOGroup = 'L: NEO Detection'

    # Set up an object to track the metricBundles that we want to combine into merged plots.
    mergedHistDict = {}

    # Set the histogram merge function.
    mergeFunc = plots.HealpixHistogram()

    keys = ['NVisits', 'coaddm5', 'NormEffTime', 'Minseeing', 'seeingAboveLimit', 'minAirmass',
            'fracAboveAirmass']

    for key in keys:
        mergedHistDict[key] = plots.PlotBundle(plotFunc=mergeFunc)

    ##
    # Calculate the fO metrics for all proposals and WFD only.
    order = 0
    for prop in ('All prop', 'WFD only'):
        if prop == 'All prop':
            metadata = 'All Visits' + slicermetadata
            sqlconstraint = ''
        if prop == 'WFD only':
            metadata = 'WFD only' + slicermetadata
            sqlconstraint = '%s' % (wfdWhere)
        # Configure the count metric which is what is used for f0 slicer.
        m1 = metrics.CountMetric(col='observationStartMJD', metricName='fO')
        plotDict = {'xlabel': 'Number of Visits', 'Asky': benchmarkVals['Area'],
                    'Nvisit': benchmarkVals['nvisitsTotal'], 'xMin': 0, 'xMax': 1500}
        summaryMetrics = [metrics.fOArea(nside=nside, norm=False, metricName='fOArea: Nvisits (#)',
                                         Asky=benchmarkVals['Area'], Nvisit=benchmarkVals['nvisitsTotal']),
                          metrics.fOArea(nside=nside, norm=True, metricName='fOArea: Nvisits/benchmark',
                                         Asky=benchmarkVals['Area'], Nvisit=benchmarkVals['nvisitsTotal']),
                          metrics.fONv(nside=nside, norm=False, metricName='fONv: Area (sqdeg)',
                                       Asky=benchmarkVals['Area'], Nvisit=benchmarkVals['nvisitsTotal']),
                          metrics.fONv(nside=nside, norm=True, metricName='fONv: Area/benchmark',
                                       Asky=benchmarkVals['Area'], Nvisit=benchmarkVals['nvisitsTotal'])]
        caption = 'The FO metric evaluates the overall efficiency of observing. '
        caption += ('fOArea: Nvisits = %.1f sq degrees receive at least this many visits out of %d. '
                    % (benchmarkVals['Area'], benchmarkVals['nvisitsTotal']))
        caption += ('fONv: Area = this many square degrees out of %.1f receive at least %d visits.'
                    % (benchmarkVals['Area'], benchmarkVals['nvisitsTotal']))
        displayDict = {'group': reqgroup, 'subgroup': 'F0', 'displayOrder': order, 'caption': caption}
        order += 1
        slicer = slicers.HealpixSlicer(nside=nside, lonCol=lonCol, latCol=latCol)

        bundle = metricBundles.MetricBundle(m1, slicer, sqlconstraint, plotDict=plotDict,
                                            displayDict=displayDict, summaryMetrics=summaryMetrics,
                                            plotFuncs=[plots.FOPlot()],
                                            runName=runName, metadata=metadata)
        bundleList.append(bundle)

    ###
    # Calculate the Rapid Revisit Metrics.
    order = 0
    metadata = 'All Visits' + slicermetadata
    sqlconstraint = ''
    dTmin = 40.0  # seconds
    dTmax = 30.0*60. # seconds
    minNvisit = 100
    pixArea = float(hp.nside2pixarea(nside, degrees=True))
    scale = pixArea * hp.nside2npix(nside)
    cutoff1 = 0.15
    extraStats1 = [metrics.FracBelowMetric(cutoff=cutoff1, scale=scale, metricName='Area (sq deg)')]
    extraStats1.extend(commonSummary)
    slicer = slicers.HealpixSlicer(nside=nside, lonCol=lonCol, latCol=latCol)
    m1 = metrics.RapidRevisitMetric(metricName='RapidRevisitUniformity',
                                    dTmin=dTmin / 60.0 / 60.0 / 24.0, dTmax=dTmax / 60.0 / 60.0 / 24.0,
                                    minNvisits=minNvisit)

    plotDict = {'xMin': 0, 'xMax': 1}
    summaryStats = extraStats1
    caption = 'Deviation from uniformity for short revisit timescales, between %s and %s seconds, ' % (
        dTmin, dTmax)
    caption += 'for pointings with at least %d visits in this time range. ' % (minNvisit)
    caption += 'Summary statistic "Area" below indicates the area on the sky which has a '
    caption += 'deviation from uniformity of < %.2f.' % (cutoff1)
    displayDict = {'group': reqgroup, 'subgroup': 'Rapid Revisit', 'displayOrder': order,
                   'caption': caption}
    bundle = metricBundles.MetricBundle(m1, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, summaryMetrics=summaryStats,
                                        runName=runName, metadata=metadata)
    bundleList.append(bundle)
    order += 1

    dTmax = dTmax/60.0 # need time in minutes for Nrevisits metric
    m2 = metrics.NRevisitsMetric(dT=dTmax)
    plotDict = {'xMin': 0.1, 'xMax': 2000, 'logScale': True}
    cutoff2 = 800
    extraStats2 = [metrics.FracAboveMetric(cutoff=cutoff2, scale=scale, metricName='Area (sq deg)')]
    extraStats2.extend(commonSummary)
    caption = 'Number of consecutive visits with return times faster than %.1f minutes, ' % (dTmax)
    caption += 'in any filter, all proposals. '
    caption += 'Summary statistic "Area" below indicates the area on the sky which has more than '
    caption += '%d revisits within this time window.' % (cutoff2)
    summaryStats = extraStats2
    displayDict = {'group': reqgroup, 'subgroup': 'Rapid Revisit', 'displayOrder': order,
                   'caption': caption}
    bundle = metricBundles.MetricBundle(m2, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, summaryMetrics=summaryStats,
                                        runName=runName, metadata=metadata)
    bundleList.append(bundle)
    order += 1
    m3 = metrics.NRevisitsMetric(dT=dTmax, normed=True)
    plotDict = {'xMin': 0, 'xMax': 1, 'cbarFormat': '%.1f'}
    cutoff3 = 0.6
    extraStats3 = [metrics.FracAboveMetric(cutoff=cutoff3, scale=scale, metricName='Area (sq deg)')]
    extraStats3.extend(commonSummary)
    summaryStats = extraStats3
    caption = 'Fraction of total visits where consecutive visits have return times faster '
    caption += 'than %.1f minutes, in any filter, all proposals. ' % (dTmax)
    caption += 'Summary statistic "Area" below indicates the area on the sky which has more '
    caption += 'than %d revisits within this time window.' % (cutoff3)
    displayDict = {'group': reqgroup, 'subgroup': 'Rapid Revisit', 'displayOrder': order,
                   'caption': caption}
    bundle = metricBundles.MetricBundle(m3, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, summaryMetrics=summaryStats,
                                        runName=runName, metadata=metadata)
    bundleList.append(bundle)
    order += 1

    # And add a histogram of the time between quick revisits.
    binMin = 0
    binMax = 120.
    binsize = 3.
    bins_metric = np.arange(binMin / 60.0 / 24.0, (binMax + binsize) / 60. / 24., binsize / 60. / 24.)
    bins_plot = bins_metric * 24.0 * 60.0
    m1 = metrics.TgapsMetric(bins=bins_metric, metricName='dT visits')
    plotDict = {'bins': bins_plot, 'xlabel': 'dT (minutes)'}
    caption = ('Histogram of the time between consecutive revisits (<%.1f minutes), over entire sky.'
               % (binMax))
    displayDict = {'group': reqgroup, 'subgroup': 'Rapid Revisit', 'order': order,
                   'caption': caption}
    slicer = slicers.HealpixSlicer(nside=nside, lonCol=lonCol, latCol=latCol)
    plotFunc = plots.SummaryHistogram()
    bundle = metricBundles.MetricBundle(m1, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, runName=runName,
                                        metadata=metadata, plotFuncs=[plotFunc])
    bundleList.append(bundle)
    order += 1

    ##
    # Trigonometric parallax and proper motion @ r=20 and r=24
    slicer = slicers.HealpixSlicer(nside=nside, lonCol=lonCol, latCol=latCol)
    sqlconstraint = ''
    order = 0
    metric = metrics.ParallaxMetric(metricName='Parallax 20', rmag=20, seeingCol=seeingCol)
    summaryStats = allStats
    plotDict = {'cbarFormat': '%.1f', 'xMin': 0, 'xMax': 3}
    displayDict = {'group': reqgroup, 'subgroup': 'Parallax', 'order': order,
                   'caption': 'Parallax precision at r=20. (without refraction).'}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, summaryMetrics=summaryStats,
                                        runName=runName, metadata=metadata)
    bundleList.append(bundle)
    order += 1
    metric = metrics.ParallaxMetric(metricName='Parallax 24', rmag=24, seeingCol=seeingCol)
    plotDict = {'cbarFormat': '%.1f', 'xMin': 0, 'xMax': 10}
    displayDict = {'group': reqgroup, 'subgroup': 'Parallax', 'order': order,
                   'caption': 'Parallax precision at r=24. (without refraction).'}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, summaryMetrics=summaryStats,
                                        runName=runName, metadata=metadata)
    bundleList.append(bundle)
    order += 1
    metric = metrics.ParallaxMetric(metricName='Parallax Normed', rmag=24, normalize=True,
                                    seeingCol=seeingCol)
    plotDict = {'xMin': 0.5, 'xMax': 1.0}
    displayDict = {'group': reqgroup, 'subgroup': 'Parallax', 'order': order,
                   'caption':
                   'Normalized parallax (normalized to optimum observation cadence, 1=optimal).'}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, summaryMetrics=summaryStats,
                                        runName=runName, metadata=metadata)
    bundleList.append(bundle)
    order += 1
    metric = metrics.ParallaxCoverageMetric(metricName='Parallax Coverage 20', rmag=20, seeingCol=seeingCol)
    plotDict = {}
    caption = "Parallax factor coverage for an r=20 star (0 is bad, 0.5-1 is good). "
    caption += "One expects the parallax factor coverage to vary because stars on the ecliptic "
    caption += "can be observed when they have no parallax offset while stars at the pole are always "
    caption += "offset by the full parallax offset."""
    displayDict = {'group': reqgroup, 'subgroup': 'Parallax', 'order': order,
                   'caption': caption}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, summaryMetrics=summaryStats,
                                        runName=runName, metadata=metadata)
    bundleList.append(bundle)
    order += 1
    metric = metrics.ParallaxCoverageMetric(metricName='Parallax Coverage 24', rmag=24, seeingCol=seeingCol)
    plotDict = {}
    caption = "Parallax factor coverage for an r=24 star (0 is bad, 0.5-1 is good). "
    caption += "One expects the parallax factor coverage to vary because stars on the ecliptic "
    caption += "can be observed when they have no parallax offset while stars at the pole are always "
    caption += "offset by the full parallax offset."""
    displayDict = {'group': reqgroup, 'subgroup': 'Parallax', 'order': order,
                   'caption': caption}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, summaryMetrics=summaryStats,
                                        runName=runName, metadata=metadata)
    bundleList.append(bundle)
    order += 1
    metric = metrics.ParallaxDcrDegenMetric(metricName='Parallax-DCR degeneracy 20', rmag=20,
                                            seeingCol=seeingCol)
    plotDict = {}
    caption = 'Correlation between parallax offset magnitude and hour angle an r=20 star.'
    caption += ' (0 is good, near -1 or 1 is bad).'
    displayDict = {'group': reqgroup, 'subgroup': 'Parallax', 'order': order,
                   'caption': caption}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, summaryMetrics=summaryStats,
                                        runName=runName, metadata=metadata)
    bundleList.append(bundle)
    order += 1
    metric = metrics.ParallaxDcrDegenMetric(metricName='Parallax-DCR degeneracy 24', rmag=24,
                                            seeingCol=seeingCol)
    plotDict = {}
    caption = 'Correlation between parallax offset magnitude and hour angle an r=24 star.'
    caption += ' (0 is good, near -1 or 1 is bad).'
    displayDict = {'group': reqgroup, 'subgroup': 'Parallax', 'order': order,
                   'caption': caption}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, summaryMetrics=summaryStats,
                                        runName=runName, metadata=metadata)
    bundleList.append(bundle)
    order += 1

    metric = metrics.ProperMotionMetric(metricName='Proper Motion 20', rmag=20, seeingCol=seeingCol)

    summaryStats = allStats
    plotDict = {'xMin': 0, 'xMax': 3}
    displayDict = {'group': reqgroup, 'subgroup': 'Proper Motion', 'order': order,
                   'caption': 'Proper Motion precision at r=20.'}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, summaryMetrics=summaryStats,
                                        runName=runName, metadata=metadata)
    bundleList.append(bundle)
    order += 1
    metric = metrics.ProperMotionMetric(rmag=24, metricName='Proper Motion 24', seeingCol=seeingCol)
    summaryStats = allStats
    plotDict = {'xMin': 0, 'xMax': 10}
    displayDict = {'group': reqgroup, 'subgroup': 'Proper Motion', 'order': order,
                   'caption': 'Proper Motion precision at r=24.'}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, summaryMetrics=summaryStats,
                                        runName=runName, metadata=metadata)
    bundleList.append(bundle)
    order += 1
    metric = metrics.ProperMotionMetric(rmag=24, normalize=True, metricName='Proper Motion Normed',
                                        seeingCol=seeingCol)
    plotDict = {'xMin': 0.2, 'xMax': 0.7}
    caption = 'Normalized proper motion at r=24. '
    caption += '(normalized to optimum observation cadence - start/end. 1=optimal).'
    displayDict = {'group': reqgroup, 'subgroup': 'Proper Motion', 'order': order,
                   'caption': caption}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, summaryMetrics=summaryStats,
                                        runName=runName, metadata=metadata)
    bundleList.append(bundle)
    order += 1

    ##
    # Calculate the time uniformity in each filter, for each year.
    order = 0

    slicer = slicers.HealpixSlicer(nside=nside, lonCol=lonCol, latCol=latCol)
    plotFuncs = [plots.TwoDMap()]
    step = 0.5
    bins = np.arange(0, 365.25 * 10 + 40, 40) - step
    metric = metrics.AccumulateUniformityMetric(bins=bins)
    plotDict = {'xlabel': 'Night (days)', 'xextent': [bins.min(
    ) + step, bins.max() + step], 'cbarTitle': 'Uniformity'}
    for f in filters:
        sqlconstraint = 'filter = "%s"' % (f)
        caption = 'Deviation from uniformity in %s band. ' % f
        caption += 'Northern Healpixels are at the top of the image.'
        caption += '(0=perfectly uniform, 1=perfectly nonuniform).'
        displayDict = {'group': uniformitygroup, 'subgroup': 'per night',
                       'order': filtorder[f], 'caption': caption}
        metadata = '%s band' % (f) + slicermetadata
        bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                            displayDict=displayDict, runName=runName, metadata=metadata,
                                            plotFuncs=plotFuncs)
        noSaveBundleList.append(bundle)

    ##
    # Depth metrics.
    slicer = slicers.HealpixSlicer(nside=nside, lonCol=lonCol, latCol=latCol)
    for f in filters:
        propCaption = '%s band, all proposals %s' % (f, slicermetadata)
        sqlconstraint = 'filter = "%s"' % (f)
        metadata = '%s band' % (f) + slicermetadata
        # Number of visits.
        metric = metrics.CountMetric(col='observationStartMJD', metricName='NVisits')
        plotDict = {'xlabel': 'Number of visits',
                    'xMin': nvisitsRange['all'][f][0],
                    'xMax': nvisitsRange['all'][f][1],
                    'colorMin': nvisitsRange['all'][f][0],
                    'colorMax': nvisitsRange['all'][f][1],
                    'binsize': 5,
                    'logScale': True, 'nTicks': 4, 'colorMin': 1}
        summaryStats = allStats
        displayDict = {'group': depthgroup, 'subgroup': 'Nvisits', 'order': filtorder[f],
                       'caption': 'Number of visits in filter %s, %s.' % (f, propCaption)}
        histMerge = {'color': colors[f], 'label': '%s' % (f),
                     'binsize': 5,
                     'xMin': nvisitsRange['all'][f][0], 'xMax': nvisitsRange['all'][f][1],
                     'legendloc': 'upper right'}
        bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                            displayDict=displayDict, runName=runName, metadata=metadata,
                                            summaryMetrics=summaryStats)
        mergedHistDict['NVisits'].addBundle(bundle, plotDict=histMerge)
        bundleList.append(bundle)
        # Coadded depth.
        metric = metrics.Coaddm5Metric()
        plotDict = {'zp': benchmarkVals['coaddedDepth'][f], 'xMin': -0.8, 'xMax': 0.8,
                    'xlabel': 'coadded m5 - %.1f' % benchmarkVals['coaddedDepth'][f]}
        summaryStats = allStats
        histMerge = {'legendloc': 'upper right', 'color': colors[f], 'label': '%s' % f, 'binsize': .02,
                     'xlabel': 'coadded m5 - benchmark value'}
        caption = ('Coadded depth in filter %s, with %s value subtracted (%.1f), %s. '
                   % (f, benchmark, benchmarkVals['coaddedDepth'][f], propCaption))
        caption += 'More positive numbers indicate fainter limiting magnitudes.'
        displayDict = {'group': depthgroup, 'subgroup': 'Coadded Depth',
                       'order': filtorder[f], 'caption': caption}
        bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                            displayDict=displayDict, runName=runName, metadata=metadata,
                                            summaryMetrics=summaryStats)
        mergedHistDict['coaddm5'].addBundle(bundle, plotDict=histMerge)
        bundleList.append(bundle)
        # Effective time.
        metric = metrics.TeffMetric(metricName='Normalized Effective Time', normed=True,
                                    fiducialDepth=benchmarkVals['singleVisitDepth'])
        plotDict = {'xMin': 0.1, 'xMax': 1.1}
        summaryStats = allStats
        histMerge = {'legendLoc': 'upper right', 'color': colors[f], 'label': '%s' % f, 'binsize': 0.02}
        caption = ('"Time Effective" in filter %s, calculated with fiducial single-visit depth of %s mag. '
                   % (f, benchmarkVals['singleVisitDepth'][f]))
        caption += 'Normalized by the fiducial time effective, if every observation was at '
        caption += 'the fiducial depth.'
        displayDict = {'group': depthgroup, 'subgroup': 'Time Eff.',
                       'order': filtorder[f], 'caption': caption}
        bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                            displayDict=displayDict, runName=runName, metadata=metadata,
                                            summaryMetrics=summaryStats)
        mergedHistDict['NormEffTime'].addBundle(bundle, plotDict=histMerge)
        bundleList.append(bundle)

    # Put in a z=0.5 Type Ia SN, based on Cambridge 2015 workshop notebook.
    # Check for 1) detection in any band, 2) detection on the rise in any band,
    # 3) good characterization
    peaks = {'uPeak': 25.9, 'gPeak': 23.6, 'rPeak': 22.6, 'iPeak': 22.7, 'zPeak': 22.7, 'yPeak': 22.8}
    peakTime = 15.
    transDuration = peakTime + 30.  # Days
    metric = metrics.TransientMetric(riseSlope=-2. / peakTime, declineSlope=1.4 / 30.0,
                                     transDuration=transDuration, peakTime=peakTime,
                                     surveyDuration=runLength,
                                     metricName='SNDetection', **peaks)
    caption = 'Fraction of z=0.5 type Ia SN that are detected in any filter'
    displayDict = {'group': transgroup, 'subgroup': 'Detected', 'caption': caption}
    sqlconstraint = ''
    metadata = '' + slicermetadata
    plotDict = {}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, runName=runName, metadata=metadata)
    bundleList.append(bundle)

    metric = metrics.TransientMetric(riseSlope=-2. / peakTime, declineSlope=1.4 / 30.0,
                                     transDuration=transDuration, peakTime=peakTime,
                                     surveyDuration=runLength,
                                     nPrePeak=1, metricName='SNAlert', **peaks)
    caption = 'Fraction of z=0.5 type Ia SN that are detected pre-peak in any filter'
    displayDict = {'group': transgroup, 'subgroup': 'Detected on the rise', 'caption': caption}
    plotDict = {}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, runName=runName, metadata=metadata)
    bundleList.append(bundle)

    metric = metrics.TransientMetric(riseSlope=-2. / peakTime, declineSlope=1.4 / 30.,
                                     transDuration=transDuration, peakTime=peakTime,
                                     surveyDuration=runLength, metricName='SNLots',
                                     nFilters=3, nPrePeak=3, nPerLC=2, **peaks)
    caption = 'Fraction of z=0.5 type Ia SN that are observed 6 times, 3 pre-peak, '
    caption += '3 post-peak, with observations in 3 filters'
    displayDict = {'group': transgroup, 'subgroup': 'Well observed', 'caption': caption}
    sqlconstraint = 'filter="r" or filter="g" or filter="i" or filter="z" '
    plotDict = {}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, runName=runName, metadata=metadata)
    bundleList.append(bundle)

    # Good seeing in r/i band metrics, including in first/second years.
    order = 0
    for tcolor, tlabel, timespan in zip(['k', 'g', 'r'], ['10 years', '1 year', '2 years'],
                                        ['', ' and night<=365', ' and night<=730']):
        order += 1
        for f in (['r', 'i']):
            sqlconstraint = 'filter = "%s" %s' % (f, timespan)
            propCaption = '%s band, all proposals %s, over %s.' % (f, slicermetadata, tlabel)
            metadata = '%s band, %s' % (f, tlabel) + slicermetadata
            seeing_limit = 0.7
            airmass_limit = 1.2
            metric = metrics.MinMetric(col=seeingCol)
            summaryStats = allStats
            plotDict = {'xMin': 0.35, 'xMax': 1.5, 'color': tcolor}
            displayDict = {'group': seeinggroup, 'subgroup': 'Best Seeing',
                           'order': filtorder[f] * 100 + order,
                           'caption': 'Minimum FWHMgeom values in %s.' % (propCaption)}
            histMerge = {'label': '%s %s' % (f, tlabel), 'color': tcolor,
                         'binsize': 0.03, 'xMin': 0.35, 'xMax': 1.5, 'legendloc': 'upper right'}
            bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                                displayDict=displayDict, runName=runName, metadata=metadata,
                                                summaryMetrics=summaryStats)
            mergedHistDict['Minseeing'].addBundle(bundle, plotDict=histMerge)
            bundleList.append(bundle)

            metric = metrics.FracAboveMetric(col=seeingCol, cutoff=seeing_limit)
            summaryStats = allStats
            plotDict = {'xMin': 0, 'xMax': 1.1, 'color': tcolor}
            displayDict = {'group': seeinggroup, 'subgroup': 'Good seeing fraction',
                           'order': filtorder[f] * 100 + order,
                           'caption': 'Fraction of total images with FWHMgeom worse than %.1f, in %s'
                           % (seeing_limit, propCaption)}
            histMerge = {'color': tcolor, 'label': '%s %s' % (f, tlabel),
                         'binsize': 0.05, 'legendloc': 'upper right'}
            bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                                displayDict=displayDict, runName=runName, metadata=metadata,
                                                summaryMetrics=summaryStats)
            mergedHistDict['seeingAboveLimit'].addBundle(bundle, plotDict=histMerge)
            bundleList.append(bundle)

            metric = metrics.MinMetric(col='airmass')
            plotDict = {'xMin': 1, 'xMax': 1.5, 'color': tcolor}
            summaryStats = allStats
            displayDict = {'group': airmassgroup, 'subgroup': 'Best Airmass',
                           'order': filtorder[f] * 100 + order, 'caption':
                           'Minimum airmass in %s.' % (propCaption)}
            histMerge = {'color': tcolor, 'label': '%s %s' % (f, tlabel),
                         'binsize': 0.03, 'legendloc': 'upper right'}
            bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                                displayDict=displayDict, runName=runName, metadata=metadata,
                                                summaryMetrics=summaryStats)
            mergedHistDict['minAirmass'].addBundle(bundle, plotDict=histMerge)
            bundleList.append(bundle)

            metric = metrics.FracAboveMetric(col='airmass', cutoff=airmass_limit)
            plotDict = {'xMin': 0, 'xMax': 1, 'color': tcolor}
            summaryStats = allStats
            displayDict = {'group': airmassgroup, 'subgroup': 'Low airmass fraction',
                           'order': filtorder[f] * 100 + order, 'caption':
                           'Fraction of total images with airmass higher than %.2f, in %s'
                           % (airmass_limit, propCaption)}
            histMerge = {'color': tcolor, 'label': '%s %s' % (
                f, tlabel), 'binsize': 0.05, 'legendloc': 'upper right'}

            bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                                displayDict=displayDict, runName=runName, metadata=metadata,
                                                summaryMetrics=summaryStats)
            mergedHistDict['fracAboveAirmass'].addBundle(bundle, plotDict=histMerge)
            bundleList.append(bundle)

# SNe metrics from UK workshop.


    peaks = {'uPeak': 25.9, 'gPeak': 23.6, 'rPeak': 22.6, 'iPeak': 22.7, 'zPeak': 22.7, 'yPeak': 22.8}
    peakTime = 15.
    transDuration = peakTime + 30.  # Days
    metric = metrics.TransientMetric(riseSlope=-2. / peakTime, declineSlope=1.4 / 30.0,
                                     transDuration=transDuration, peakTime=peakTime,
                                     surveyDuration=runLength,
                                     metricName='SNDetection', **peaks)
    caption = 'Fraction of z=0.5 type Ia SN that are detected at any point in their light curve in any filter'
    displayDict = {'group': sngroup, 'subgroup': 'Detected', 'caption': caption}
    sqlconstraint = ''
    plotDict = {}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, runName=runName)
    bundleList.append(bundle)

    metric = metrics.TransientMetric(riseSlope=-2. / peakTime, declineSlope=1.4 / 30.0,
                                     transDuration=transDuration, peakTime=peakTime,
                                     surveyDuration=runLength,
                                     nPrePeak=1, metricName='SNAlert', **peaks)
    caption = 'Fraction of z=0.5 type Ia SN that are detected pre-peak in any filter'
    displayDict = {'group': sngroup, 'subgroup': 'Detected on the rise', 'caption': caption}
    plotDict = {}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, runName=runName)
    bundleList.append(bundle)

    metric = metrics.TransientMetric(riseSlope=-2. / peakTime, declineSlope=1.4 / 30.,
                                     transDuration=transDuration, peakTime=peakTime,
                                     surveyDuration=runLength, metricName='SNLots',
                                     nFilters=3, nPrePeak=3, nPerLC=2, **peaks)
    caption = 'Fraction of z=0.5 type Ia SN that are observed 6 times, 3 pre-peak, '
    caption += '3 post-peak, with observations in 3 filters'
    displayDict = {'group': sngroup, 'subgroup': 'Well observed', 'caption': caption}
    sqlconstraint = 'filter="r" or filter="g" or filter="i" or filter="z" '
    plotDict = {}
    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, runName=runName)
    bundleList.append(bundle)

    propIDOrderDict = {}
    orderVal = 100
    for propID in propids:
        propIDOrderDict[propID] = orderVal
        orderVal += 100

    # Full range of dates:
    metric = metrics.FullRangeMetric(col='observationStartMJD')
    plotFuncs = [plots.HealpixSkyMap(), plots.HealpixHistogram()]
    caption = 'Time span of survey.'
    sqlconstraint = ''
    plotDict = {}
    displayDict = {'group': rangeGroup, 'caption': caption}

    bundle = metricBundles.MetricBundle(metric, slicer, sqlconstraint, plotDict=plotDict,
                                        displayDict=displayDict, runName=runName)
    bundleList.append(bundle)
    for f in filters:
        for propid in propids:
            displayDict = {'group': rangeGroup, 'subgroup': propids[propid], 'caption': caption,
                           'order': filtorder[f]}
            md = '%s, %s' % (f, propids[propid])
            sql = 'filter="%s" and proposalId=%i' % (f, propid)
            bundle = metricBundles.MetricBundle(metric, slicer, sql, plotDict=plotDict,
                                                metadata=md, plotFuncs=plotFuncs,
                                                displayDict=displayDict, runName=runName)
            bundleList.append(bundle)

    # Alt az plots
    slicer = slicers.HealpixSlicer(nside=64, latCol='zenithDistance', lonCol='azimuth', useCache=False)
    metric = metrics.CountMetric('observationStartMJD', metricName='Nvisits as function of Alt/Az')
    plotDict = {}
    plotFuncs = [plots.LambertSkyMap()]
    displayDict = {'group': altAzGroup, 'caption': 'Alt Az pointing distribution'}
    for f in filters:
        for propid in propids:
            displayDict = {'group': altAzGroup, 'subgroup': propids[propid],
                           'caption': 'Alt Az pointing distribution',
                           'order': filtorder[f]}
            md = '%s, %s' % (f, propids[propid])
            sql = 'filter="%s" and proposalId=%i' % (f, propid)
            bundle = metricBundles.MetricBundle(metric, slicer, sql, plotDict=plotDict,
                                                plotFuncs=plotFuncs, metadata=md,
                                                displayDict=displayDict, runName=runName)
            bundleList.append(bundle)

    sql = ''
    md = 'all observations'
    displayDict = {'group': altAzGroup, 'subgroup': 'All Observations',
                   'caption': 'Alt Az pointing distribution'}
    bundle = metricBundles.MetricBundle(metric, slicer, sql, plotDict=plotDict,
                                        plotFuncs=plotFuncs, metadata=md,
                                        displayDict=displayDict, runName=runName)
    bundleList.append(bundle)

    # Median inter-night gap (each and all filters)
    slicer = slicers.HealpixSlicer(nside=nside, lonCol=lonCol, latCol=latCol)
    metric = metrics.InterNightGapsMetric(metricName='Median Inter-Night Gap')
    sqls = ['filter = "%s"' % f for f in filters]
    orders = [filtorder[f] for f in filters]
    orders.append(0)
    sqls.append('')
    for sql, order in zip(sqls, orders):
        displayDict = {'group': intergroup, 'subgroup': 'Median Gap', 'caption': 'Median gap between days',
                       'order': order}
        bundle = metricBundles.MetricBundle(metric, slicer, sql, displayDict=displayDict, runName=runName)
        bundleList.append(bundle)

    # Max inter-night gap in r and all bands
    dslicer = slicers.HealpixSlicer(nside=nside, lonCol='ditheredRA', latCol='ditheredDec')
    metric = metrics.InterNightGapsMetric(metricName='Max Inter-Night Gap', reduceFunc=np.max)

    plotDict = {'percentileClip': 95.}
    for sql, order in zip(sqls, orders):
        displayDict = {'group': intergroup, 'subgroup': 'Max Gap', 'caption': 'Max gap between nights',
                       'order': order}
        bundle = metricBundles.MetricBundle(metric, dslicer, sql, displayDict=displayDict,
                                            plotDict=plotDict, runName=runName)
        bundleList.append(bundle)

    # largest phase gap for periods
    periods = [0.1, 1.0, 10., 100.]
    sqls = {'u': 'filter = "u"', 'r': 'filter="r"',
            'g,r,i,z': 'filter="g" or filter="r" or filter="i" or filter="z"',
            'all': ''}

    for sql in sqls:
        for period in periods:
            displayDict = {'group': phaseGroup,
                           'subgroup': 'period=%.2f days, filter=%s' % (period, sql),
                           'caption': 'Maximum phase gaps'}
            metric = metrics.PhaseGapMetric(nPeriods=1, periodMin=period, periodMax=period,
                                            metricName='PhaseGap, %.1f' % period)
            bundle = metricBundles.MetricBundle(metric, slicer, sqls[sql],
                                                displayDict=displayDict, runName=runName)
            bundleList.append(bundle)

    # NEO XY plots
    slicer = slicers.UniSlicer()
    metric = metrics.PassMetric(metricName='NEODistances')
    stacker = stackers.NEODistStacker()
    stacker2 = stackers.EclipticStacker()
    for f in filters:
        plotFunc = plots.NeoDistancePlotter(eclipMax=10., eclipMin=-10.)
        caption = 'Observations within 10 degrees of the ecliptic. Distance an H=22 NEO would be detected'
        displayDict = {'group': NEOGroup, 'subgroup': 'xy', 'order': filtorder[f],
                       'caption': caption}
        plotDict = {}
        sqlconstraint = 'filter = "%s"' % (f)
        bundle = metricBundles.MetricBundle(metric, slicer,
                                            sqlconstraint, displayDict=displayDict,
                                            stackerList=[stacker, stacker2],
                                            plotDict=plotDict,
                                            plotFuncs=[plotFunc])
        noSaveBundleList.append(bundle)

    # Solar elongation
    sqls = ['filter = "%s"' % f for f in filters]
    orders = [filtorder[f] for f in filters]
    sqls.append('')
    orders.append(0)
    for sql, order in zip(sqls, orders):
        plotFuncs = [plots.HealpixSkyMap(), plots.HealpixHistogram()]
        displayDict = {'group': NEOGroup, 'subgroup': 'Solar Elongation',
                       'caption': 'Median solar elongation in degrees', 'order': order}
        metric = metrics.MedianMetric('solarElong')
        slicer = slicers.HealpixSlicer(nside=nside, lonCol=lonCol, latCol=latCol)
        bundle = metricBundles.MetricBundle(metric, slicer, sql, displayDict=displayDict,
                                            plotFuncs=plotFuncs)
        bundleList.append(bundle)

        plotFuncs = [plots.HealpixSkyMap(), plots.HealpixHistogram()]
        displayDict = {'group': NEOGroup, 'subgroup': 'Solar Elongation',
                       'caption': 'Minimum solar elongation in degrees', 'order': order}
        metric = metrics.MinMetric('solarElong')
        slicer = slicers.HealpixSlicer(nside=nside, lonCol=lonCol, latCol=latCol)
        bundle = metricBundles.MetricBundle(metric, slicer, sql, displayDict=displayDict,
                                            plotFuncs=plotFuncs)
        bundleList.append(bundle)

    return (metricBundles.makeBundlesDictFromList(bundleList), mergedHistDict,
            metricBundles.makeBundlesDictFromList(noSaveBundleList))
示例#4
0
out_file = sys.argv[2]
out_dir = sys.argv[2]

gal_l_min = 0.
gal_l_max = 360.
gal_b_min = -89.
gal_b_max = 89.
diameter = 3.5
step = diameter / np.sqrt(2) # This would be enough on a 2D plane.
step *= 0.85

gal_l_all = np.linspace(gal_l_min, gal_l_max, (gal_l_max-gal_l_min)/step+1)
gal_b_all = np.linspace(gal_b_min, gal_b_max, (gal_b_max-gal_b_min)/step+1)
(gal_l, gal_b) = np.meshgrid(gal_l_all, gal_b_all)

c = SkyCoord(gal_l.flatten(), gal_b.flatten(), unit=u.deg, frame='galactic')
userRA = c.fk5.ra.value
userDec = c.fk5.dec.value

columns = ['observationStartMJD', 'filter', 'fiveSigmaDepth']

metric = metrics.PassMetric(cols=columns) 
slicer = slicers.UserPointsSlicer(userRA, userDec)
sqlconstraint = ''
MJDmetric = metricBundles.MetricBundle(metric, slicer, sqlconstraint, 
                                                            fileRoot=out_file)
bundleDict = {'MJDmetric': MJDmetric}
opsdb = db.OpsimDatabase(database)
group = metricBundles.MetricBundleGroup(bundleDict, opsdb, outDir=out_dir)
group.runAll()
示例#5
0
filterNames = ['u', 'g', 'r', 'i', 'z']
colors = {'u': 'purple', 'g': 'g', 'r': 'r', 'i': 'blue', 'z': 'm'}
#create list of peak days and magnitudes to iterate over
day_of_peak = np.arange(59580, 63232, 30)
mag_of_peak = np.arange(17, 25, 1)

# Set the database and query
runName = 'minion_1018'
opsdb = db.OpsimDatabase(runName + '_sqlite.db')

# Set the output directory
outDir = 'Observations Dictionary'
resultsDb = db.ResultsDb(outDir)

# This creates our database of observations. The pass metric just passes data straight through.
metric = metrics.PassMetric(cols=['expMJD', 'filter', 'fiveSigmaDepth'])
"""use slicer to restrict the ra and decs, use np.random.uniform to get random points, 
	first coordinate represents ra and second dec. Or, give a list of specific
	ra and decs - the second slicer is for the deep drilling fields. One must be commented out."""
#slicer = slicers.UserPointsSlicer(np.random.uniform(0,360,1000), np.random.uniform(-80,0,1000))
slicer = slicers.UserPointsSlicer([349.4, 0.00, 53.0, 34.4, 150.4],
                                  [-63.3, -45.5, -27.4, -5.1, 2.8])
#sql is empty as there are no restrictions currently
sql = ''
bundle = metricBundles.MetricBundle(metric, slicer, sql)
bg = metricBundles.MetricBundleGroup({0: bundle},
                                     opsdb,
                                     outDir=outDir,
                                     resultsDb=resultsDb)
bg.runAll()
    def getMetrics(self): 
        
        colmn = 'observationStartMJD';
        opsdb = db.OpsimDatabase(self.opsim)
        
        # Directory where tmp files are going to be stored TODO eliminate - this
        outDir = 'TmpDir'
        resultsDb = db.ResultsDb(outDir=outDir)

        
        metric=metrics.PassMetric(cols=[colmn,'fiveSigmaDepth', 'filter'])
        slicer = slicers.UserPointsSlicer(ra=self.ra,dec=self.dec)
        sqlconstraint = 'filter = \'' + self.fil + '\''

        bundle = mb.MetricBundle(metric, slicer, sqlconstraint, runName=self.name)
        bgroup = mb.MetricBundleGroup({0: bundle}, opsdb, outDir=outDir, resultsDb=resultsDb)
        bgroup.runAll();
        
        filters = np.unique(bundle.metricValues[0]['filter'])
        mv = bundle.metricValues[0]


        # Get dates
        self.mjd = mv[colmn]
        self.mjd = np.sort(self.mjd)


        # Define redshift bins
        zbin = np.linspace(0.5,7.5,8)
        zbin = np.insert(zbin,0,0)

        # Converting MJD to survey days
        T=np.int(self.mjd.max()-self.mjd.min()+1)
        swop=[]
        wedgeop=[]
        scop=[]
        edgecop=[]
        i=0

        total = len(zbin)*(self.nlc);
        progress = 0;

        # We generate a number (nlc) of light curves for each redshift bin
        for z in zbin:
            for w in range(self.nlc):
                # Generating continuous light curve (cadence=1d)
                tt, yy = drw_artificial_lc(T, z=z, frame=self.frame)
         
                sn, edgesn = self.sf(tt,yy,z=z)
                # Calculating SF for the current continuous light curve
                scop.append(sn)
                edgecop.append(edgesn)
                self.edgesn = edgesn
                # Generating OpSim light curve evaluated on the current continuous light curve
                top,yop=self.__opsim_lc(tt,yy)
                # Calculating SF for the current OpSim light curve
                srol,edgesrol=self.sf(top,yop,z=z)
                swop.append(srol)
                wedgeop.append(edgesrol)

                #progressBar(progress, total);
                progress = progress + 1;
            i=i+1  # counter


        swop=np.asarray(swop)
        swop=swop.reshape(9,self.nlc,99)
        scop=np.asarray(scop)
        scop=scop.reshape(9,self.nlc,99)
        razrol=[]
        for z in range(9):
            for r in range(self.nlc):
                # Calculating the SF metric
                razrol.append((np.nan_to_num(np.sqrt(scop[z,r,:]))-np.nan_to_num(np.sqrt(swop[z,r,:]))))

        razrol9=np.asarray(razrol)
        razrol9=razrol9.reshape(9,self.nlc,99)
        # We take the mean of generated light curves for each redshift bin.
        self.raz2=np.nanmean(razrol9[:,:,:],axis=1)
示例#7
0
Ds = cosmo.angular_diameter_distance(myinput['system']['zs']).value  # in Mpc
Dl = cosmo.angular_diameter_distance(myinput['system']['zl']).value  # in Mpc
Dls = cosmo.angular_diameter_distance_z1z2(
    myinput['system']['zl'], myinput['system']['zs']).value  # in Mpc

print(">>>>>>>>>>>>>>>>>> Reading LSST dates and depths <<<<<<<<<<<<<<<<<<<<<")
outDir = outdir + '/output'
dbFile = path_to_dbfile + runName + '.db'
opsimdb = db.opsimDatabase.OpsimDatabase(dbFile)
resultsDb = db.ResultsDb(outDir=outDir)

#ra=ra*15.0
# SNR limit (Don't use points below this limit)
snrLimit = 5.0
# The pass metric just passes data straight through.
metric = metrics.PassMetric(
    cols=['filter', 'fiveSigmaDepth', 'observationStartMJD'])
slicer = slicers.UserPointsSlicer(ra, dec, lonCol='fieldRA', latCol='fieldDec')
sql = ''
bundle = metricBundles.MetricBundle(metric, slicer, sql)
bg = metricBundles.MetricBundleGroup({0: bundle},
                                     opsimdb,
                                     outDir=outDir,
                                     resultsDb=resultsDb)
bg.runAll()
bundle.metricValues.data[0]['filter']

filters = myinput['filters']
print('%i Observations total at this point (All SNR levels)' %
      bundle.metricValues.data[0].size)
for fname in filters:
    good = numpy.where(bundle.metricValues.data[0]['filter'] == fname)
示例#8
0
def save_csv_dithers(dbs_path,
                     outDir,
                     db_files_only=None,
                     rot_rand_seed=42,
                     trans_rand_seed=42,
                     print_progress=True,
                     show_diagnostic_plots=False,
                     save_plots=False):
    """
    
    The goal here is to calculate the translational and rotational dithers for
    various cadences and save the output as a csv file.  These dithers are largely
    the same as in DC1/DC2: 
        - Translational dithers:
            - WFD: large random offsets (as large as 1.75 deg) applied after every visit.
            - DD: small random offsets (as large as 7 arcmin) applied after every visit.
            - Else: no dithers, so `fieldRA`, `fieldDec` are returned.
        - Rotational dithers:
            - All surveys (WFD, DD, else): random between -90, 90 degrees applied after
                                           every filter change. (Break from DC2: Some visits
                                           dont get dithered since they are forced outside
                                           the rotator range.
                                           See RotStacker info for details.)
             
    Supports OpSim V3/V4 outputs.
    
    Required Inputs
    ---------------
    * dbs_path: str: path to the directory that contains the .db files; could have non-.db files.
    * outDir: str: path to the directory where the output should be saved.
    
    Optional Inputs
    ---------------
    * db_files_only: list of str: list of names of the db files to run.
                                  Default: None. Runs over all the files in db_path.
    * rot_rand_seed: int: seed for random number generator for rotational dithers.
                          Default: 42
    * trans_rand_seed: int: seed for random number generator for translational dithers.
                            Default: 42
    * print_progress: bool: set to False to not print progress.
                            Default: True
    * show_diagnostic_plots: bool: set to True to show histogram of added dithers.
                                   Default: False
    * save_plots: bool: set to True to save the histogram for descDithers in outDir.
                        Default: False
                                   
    Saved file format
    -----------------
    .csv file with four columns:
        obsIDcol, 'descDitheredRA', 'descDitheredDec', 'descDitheredRotTelPos'
    where
        obsIDcol = 'observationId' for V4 outputs and 'obsHistID' for V3 outputs.
    
    Saved filename = descDithers_<database name>.csv
    
    """
    startTime_0 = time.time()
    readme = '##############################\n%s' % (datetime.date.isoformat(
        datetime.date.today()))
    readme += '\nRunning with lsst.sims.maf.__version__: %s' % lsst.sims.maf.__version__
    readme += '\n\nsave_csv_dithers run:\ndbs_path= %s\n' % dbs_path
    readme += 'outDir: %s' % outDir
    readme += 'db_files_only: %s' % db_files_only
    readme += 'rot_rand_seed=%s\ntrans_rand_seed=%s' % (rot_rand_seed,
                                                        trans_rand_seed)
    readme += 'print_progress=%s\show_diagnostic_plots=%s\n' % (
        print_progress, show_diagnostic_plots)

    dbfiles = [f for f in os.listdir(dbs_path)
               if f.endswith('db')]  # select db files
    if print_progress: print('Found files: %s\n' % dbfiles)

    if db_files_only is not None:
        dbfiles = [f for f in dbfiles if f in db_files_only]  # select db files

    readme += '\nReading for files: %s\n\n' % dbfiles
    if print_progress and db_files_only is not None:
        print('Running over: %s\n' % dbfiles)

    for i, dbfile in enumerate(dbfiles):  # loop over all the db files
        startTime = time.time()
        if (i != 0): readme = ''
        readme += '%s' % dbfile

        if print_progress: print('Starting: %s\n' % dbfile)

        opsdb = db.OpsimDatabase('%s/%s' %
                                 (dbs_path, dbfile))  # connect to the database

        # specify the column names to get from the db file
        colnames = [
            'proposalId', 'observationId', 'fieldRA', 'fieldDec', 'rotTelPos'
        ]
        propIDcol, obsIDcol = 'proposalId', 'observationId'

        if (opsdb.opsimVersion == 'V3'):
            # V3 outputs have somewhat different column names
            colnames = [
                'propID', 'obsHistID', 'fieldRA', 'fieldDec', 'rotTelPos'
            ]
            propIDcol, obsIDcol = 'propID', 'obsHistID'

        # get the data
        simdata = opsdb.fetchMetricData(colnames=colnames, sqlconstraint=None)

        # set up to run the stackers that add columns for translational and rotational dithers.
        metric = metrics.PassMetric(
        )  # want to access the database; no analysis needed
        slicer = slicers.OneDSlicer(
            sliceColName='night', binsize=1,
            verbose=print_progress)  # essentially accessing all nights
        sqlconstraint = None

        resultsDb = db.ResultsDb(outDir=outDir)
        ################################################################################################
        # set up metric bundle to run stackers for large translational dithers + rotational dithers
        if print_progress:
            print('Setting up for WFD translational dithers + rot dithers.')
        bgroup = {}
        stackerList = [
            stackers.RandomDitherFieldPerVisitStacker(
                degrees=opsdb.raDecInDeg, randomSeed=trans_rand_seed),
            stackers.RandomRotDitherPerFilterChangeStacker(
                degrees=opsdb.raDecInDeg, randomSeed=rot_rand_seed)
        ]

        bundle = metricBundles.MetricBundle(metric,
                                            slicer,
                                            sqlconstraint=sqlconstraint,
                                            stackerList=stackerList)
        bgroup['WFD'] = metricBundles.MetricBundleGroup({0: bundle},
                                                        opsdb,
                                                        outDir=outDir,
                                                        resultsDb=resultsDb,
                                                        saveEarly=False,
                                                        verbose=print_progress)
        # run the bundle
        bgroup['WFD'].runAll()

        # set up the bundle for small translational dithers
        if print_progress: print('\nSetting up for DD translational dithers.')
        chipSize = 1.75 * 2 / 15
        chipMaxDither = chipSize / 2.
        stackerList = [
            stackers.RandomDitherFieldPerVisitStacker(
                maxDither=chipMaxDither,
                degrees=opsdb.raDecInDeg,
                randomSeed=trans_rand_seed)
        ]
        bundle = metricBundles.MetricBundle(metric,
                                            slicer,
                                            sqlconstraint=sqlconstraint,
                                            stackerList=stackerList)

        bgroup['DD'] = metricBundles.MetricBundleGroup({0: bundle},
                                                       opsdb,
                                                       outDir=outDir,
                                                       resultsDb=resultsDb,
                                                       saveEarly=False,
                                                       verbose=print_progress)
        # run the bundle
        bgroup['DD'].runAll()

        ################################################################################################
        # access the relevant columns
        dithered_RA, dithered_Dec = {}, {}
        for key in bgroup:
            dithered_RA[key] = bgroup[key].simData[
                'randomDitherFieldPerVisitRa']
            dithered_Dec[key] = bgroup[key].simData[
                'randomDitherFieldPerVisitDec']

        dithered_rotTelPos = bgroup['WFD'].simData[
            'randomDitherPerFilterChangeRotTelPos']

        ################################################################################################
        # diagnostic plots
        if show_diagnostic_plots:
            # histograms of dithers
            fig, axes = plt.subplots(nrows=1, ncols=3)

            for key in bgroup:
                # ra
                axes[0].hist(dithered_RA[key] - simdata['fieldRA'],
                             label='%s dithers: delRA' % key,
                             histtype='step',
                             lw=2,
                             bins=30)

                # dec
                axes[1].hist(dithered_Dec[key] - simdata['fieldDec'],
                             label='%s dithers: delDec' % key,
                             histtype='step',
                             lw=2)

            # tel pos
            axes[2].hist(dithered_rotTelPos - simdata['rotTelPos'],
                         label='rot dithers: rotTelPos',
                         histtype='step',
                         lw=2)
            for ax in axes:
                ax.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
                ax.set_ylabel('Counts')

            axes[0].legend()
            axes[1].legend()

            if opsdb.raDecInDeg: unitlabel = 'degrees'
            else: unitlabel = 'radians'

            axes[0].set_xlabel('delRA (%s)' % unitlabel)
            axes[1].set_xlabel('delDec (%s)' % unitlabel)
            axes[2].set_xlabel('delRotTelPos (%s)' % unitlabel)

            plt.title(dbfile)
            fig.set_size_inches(20, 5)

        ################################################################################################
        # initiate the final arrays as undithered fieldRA, fieldDec as nonWFD, nonDDF should remain unchanged
        descDitheredRA = simdata['fieldRA'].copy()
        descDitheredDec = simdata['fieldDec'].copy()
        descDitheredRot = simdata['rotTelPos'].copy()

        # need to find the indices for WFD vs. DD observations since we are adding different
        # translational dithers for WFD/DDF visits + none for other surveys
        propIds, propTags = opsdb.fetchPropInfo()
        # ok work with WFD visits now
        ind_WFD = np.where(simdata[propIDcol] == propTags['WFD'])[0]
        if print_progress:
            tot = len(simdata)
            print('Total visits: ', tot)
            print('propTags: ', propTags)
            print('%s WFD visits out of total %s' % (len(ind_WFD), tot))

        descDitheredRA[ind_WFD] = dithered_RA['WFD'][ind_WFD]
        descDitheredDec[ind_WFD] = dithered_Dec['WFD'][ind_WFD]

        # work with DD visits now
        ind_DD = np.where(simdata[propIDcol] == propTags['DD'])[0]
        if print_progress:
            print('%s DD visits out of total %s' % (len(ind_DD), tot))

        descDitheredRA[ind_DD] = dithered_RA['DD'][ind_DD]
        descDitheredDec[ind_DD] = dithered_Dec['DD'][ind_DD]

        # add rotational dithers to everything
        descDitheredRot = dithered_rotTelPos

        ###############################################################
        # diagnostic plots
        if show_diagnostic_plots or save_plots:
            # histograms of desc dithered positions
            fig, axes = plt.subplots(nrows=1, ncols=3)

            _, bins, _ = axes[0].hist(descDitheredRA,
                                      label='descDitheredRA',
                                      histtype='step',
                                      lw=2)
            axes[0].hist(simdata['fieldRA'],
                         label='fieldRA',
                         histtype='step',
                         lw=2,
                         bins=bins)

            _, bins, _ = axes[1].hist(descDitheredDec,
                                      label='descDitheredDec',
                                      histtype='step',
                                      lw=2)
            axes[1].hist(simdata['fieldDec'],
                         label='fieldDec',
                         histtype='step',
                         lw=2,
                         bins=bins)

            _, bins, _ = axes[2].hist(descDitheredRot,
                                      label='descDitheredRot',
                                      histtype='step',
                                      lw=2)
            axes[2].hist(simdata['rotTelPos'],
                         label='rotTelPos',
                         histtype='step',
                         lw=2,
                         bins=bins)

            if opsdb.raDecInDeg: xlabel = 'degrees'
            else: xlabel = 'radians'

            for ax in axes:
                ax.legend()
                ax.set_xlabel(xlabel)
                ax.set_ylabel('Counts')

            plt.suptitle(dbfile)
            fig.set_size_inches(20, 5)

            if save_plots:
                filename = 'hist_descDithers_%s.png' % (dbfile.split('.db')[0])
                plt.savefig('%s/%s' % (outDir, filename),
                            format='png',
                            bbox_inches='tight')
                readme += '\nSaved hist for descDithers in %s.' % filename

                if print_progress:
                    print('\nSaved hist plot in %s' % filename)

            if show_diagnostic_plots:
                plt.show()
            else:
                plt.close('all')

        ###############################################################
        # save the columns as a csv file.
        d = {
            obsIDcol: simdata[obsIDcol],
            'descDitheredRA': descDitheredRA,
            'descDitheredDec': descDitheredDec,
            'descDitheredRotTelPos': descDitheredRot
        }

        filename = 'descDithers_%s.csv' % (dbfile.split('.db')[0])
        pd.DataFrame(d).to_csv('%s/%s' % (outDir, filename), index=False)

        readme += '\nSaved the dithers in %s' % filename
        readme += '\nTime taken: %.2f (min)\n\n' % (
            (time.time() - startTime) / 60.)

        if print_progress:
            print('\nSaved the dithers in %s' % filename)
            print('Time taken: %.2f (min)\n\n' %
                  ((time.time() - startTime) / 60.))

        readme_file = open('%s/readme.txt' % (outDir), 'a')
        readme_file.write(readme)
        readme_file.close()

    # mark the end in the readme.
    readme_file = open('%s/readme.txt' % (outDir), 'a')
    readme_file.write('All done. Total time taken: %.2f (min)\n\n' %
                      ((time.time() - startTime_0) / 60.))
    readme_file.close()
示例#9
0
def spot_inspect(filename,
                 ra,
                 dec,
                 year_max=8.5,
                 outDir='temp',
                 season_pad=80):

    resultsDb = db.ResultsDb(outDir=outDir)

    f2c = {
        'u': 'purple',
        'g': 'blue',
        'r': 'green',
        'i': 'cyan',
        'z': 'orange',
        'y': 'red'
    }

    name = filename.replace('_v1.7_10yrs.db', '')

    conn = db.OpsimDatabase(filename)
    bundleList = []
    sql = ''  #'night > 250 and night < %i' % (365*year_max)
    metric = metrics.PassMetric(
        ['filter', 'observationStartMJD', 'fiveSigmaDepth', 'night'])
    slicer = slicers.UserPointsSlicer(ra=ra, dec=dec)
    summaryStats = []
    plotDict = {}
    bundleList.append(
        metricBundles.MetricBundle(metric,
                                   slicer,
                                   sql,
                                   plotDict=plotDict,
                                   summaryMetrics=summaryStats,
                                   runName=name))
    bd = metricBundles.makeBundlesDictFromList(bundleList)
    bg = metricBundles.MetricBundleGroup(bd,
                                         conn,
                                         outDir=outDir,
                                         resultsDb=resultsDb)
    bg.runAll()
    #bg.plotAll(closefigs=False)
    mv = bundleList[0].metricValues[0]
    mv.sort(order='observationStartMJD')

    all_mjd = np.arange(mv['observationStartMJD'].min() - 1,
                        mv['observationStartMJD'].max() + 2, 1)

    breaks_indx = season_breaks(all_mjd, ra)
    breaks1 = all_mjd[breaks_indx] - all_mjd.min() + mv['night'].min()
    breaks = [mv['night'].min() - 1]
    breaks.extend(breaks1.tolist())
    breaks.append(mv['night'].max() + 3)

    #breaks = np.array([mv['night'].min()-season_pad] + breaks.tolist() + [mv['night'].max()+season_pad])

    fig = plt.figure()
    ax1 = fig.add_subplot(1, 1, 1)

    di = np.diff(breaks)
    mps = breaks[0:-1] + di / 2
    counts, med_gaps, unights = gap_stats(mv['night'], bins=breaks)

    for fn in f2c:
        in_filt = np.where(mv['filter'] == fn)[0]
        ax1.plot(mv['night'][in_filt],
                 mv['fiveSigmaDepth'][in_filt],
                 'o',
                 color=f2c[fn],
                 label=fn,
                 alpha=0.5)
    ax1.set_xlabel('Night')
    ax1.set_ylabel(r'5$\sigma$ depth (mags)')

    for i in np.arange(mps.size):
        plt.annotate('%i\n %.1f \n %i' % (counts[i], med_gaps[i], unights[i]),
                     [mps[i], 20])

    #plt.legend(loc=(1.04,0))
    for br in breaks:
        ax1.axvline(br)
    ax1.set_ylim([19.5, 25.5])
    #plt.xlim([1340, 1560])
    ax1.set_title(name + '\nra=%.2f, dec=%.2f' % (ra, dec))

    return fig, ax1