Ejemplo n.º 1
0
 def fancy_plot(self):
     """Make a fancier looking sky map of the footprint.
     """
     if not (fancyplot):
         print(
             'Cannot make this fancy plot; MAF plotting utilities unavailable.'
         )
         return None
     # fieldRA / fieldDec are dictionaries - key=prop
     slicer = slicers.OpsimFieldSlicer()
     fignum = None
     colorlist = [[1, 1, 0], [.5, 0, .5], [0, .25, .5], [0, 1, 0],
                  [0, 0, 0], [1, 0, 0], [.5, .5, 1]]
     ci = 0
     colors = {}
     add_planes = True
     for name in self.regions:
         print(name)
         # Modify slicer so we can use it for plotting.
         slicer.slicePoints['ra'] = np.radians(self.regions[name]['ra'])
         slicer.slicePoints['dec'] = np.radians(self.regions[name]['dec'])
         fieldLocs = ma.MaskedArray(data=np.empty(len(self.regions[name]),
                                                  object),
                                    mask=np.zeros(len(self.regions[name]),
                                                  bool),
                                    fill_value=-99)
         colors[name] = [
             colorlist[ci][0], colorlist[ci][1], colorlist[ci][2], 0.4
         ]
         ci += 1
         if ci == len(colorlist):
             ci = 0
         for i in range(len(self.regions[name])):
             fieldLocs.data[i] = colors[name]
         skymap = plots.BaseSkyMap()
         fignum = skymap(fieldLocs,
                         slicer, {
                             'metricIsColor': True,
                             'bgcolor': 'lightgray',
                             'raCen': 0,
                             'figsize': (10, 8),
                             'ecPlane': add_planes,
                             'mwZone': add_planes
                         },
                         fignum=fignum)
         add_planes = False
     fig = plt.figure(fignum)
     labelcolors = []
     labeltext = []
     for name in self.regions:
         el = Ellipse(
             (0, 0),
             0.03,
             0.03,
             fc=(colors[name][0], colors[name][1], colors[name][2]),
             alpha=colors[name][3])
         labelcolors.append(el)
         labeltext.append(name)
     plt.legend(labelcolors, labeltext, loc=(0.85, 0.9), fontsize='smaller')
     return fig
Ejemplo n.º 2
0
 def fancy_plot_Nvisits(self, cmap='viridis'):
     """Make a fancier looking sky map of the footprint.
     """
     if not (fancyplot):
         print(
             'Cannot make this fancy plot; MAF plotting utilities unavailable.'
         )
         return None
     # fieldRA / fieldDec are dictionaries - key=prop
     slicer = slicers.OpsimFieldSlicer()
     fignum = None
     # Add a 'survey' that covers the whole sky, and then we put nvisits per survey into it.
     regionlist = list(self.regions.keys())
     self.define_survey_region('_all_nvisits', limits={'dec': [-90, 90]})
     self.regions['_all_nvisits'] = self.regions['_all_nvisits'].assign(
         Nvis=0)
     for name in regionlist:
         print(name)
         tmp = self.regions['_all_nvisits']['Nvis'] + self.regions[name][
             'Nvis']
         tmp = np.where(np.isnan(tmp), 0, tmp)
         self.regions['_all_nvisits']['Nvis'] += tmp
     nvisits = ma.MaskedArray(data=self.regions['_all_nvisits']['Nvis'],
                              mask=np.zeros(
                                  len(self.regions['_all_nvisits']), bool),
                              fill_value=-99)
     nvisits.mask = np.where(nvisits == 0, True, False)
     # Modify slicer so we can use it for plotting.
     slicer.slicePoints['ra'] = np.radians(
         self.regions['_all_nvisits']['ra'])
     slicer.slicePoints['dec'] = np.radians(
         self.regions['_all_nvisits']['dec'])
     skymap = plots.BaseSkyMap()
     fignum = skymap(nvisits,
                     slicer, {
                         'xlabel': 'Nvisits',
                         'cmap': cmap,
                         'raCen': 0,
                         'figsize': (10, 8),
                         'colorMin': 0,
                         'colorMax': 1000,
                         'ecPlane': True,
                         'mwZone': True
                     },
                     fignum=fignum)
     del self.regions['_all_nvisits']
     fig = plt.figure(fignum)
     return fig
Ejemplo n.º 3
0
def plotFields(fields, match):
    slicer = slicers.OpsimFieldSlicer()
    # Modify slicer so we can use it for plotting.
    slicer.slicePoints['ra'] = fields['ra']
    slicer.slicePoints['dec'] = fields['dec']
    fieldLocs = ma.MaskedArray(data=np.zeros(len(fields['ra']), float),
                               mask=np.ones(len(fields['ra']), int),
                               fill_value=-99)
    fieldLocs.data[match] = 1.0
    fieldLocs.mask[match] = 0
    skymap = plots.BaseSkyMap()
    skymap(fieldLocs, slicer, {
        'colorMin': 0,
        'colorMax': 1,
        'xlabel': 'Field Locations'
    })
    plt.show()
Ejemplo n.º 4
0
def plotFields(fieldRA, fieldDec):
    slicer = slicers.OpsimFieldSlicer()
    fignum = None
    colorlist = [[1, 0, 0], [1, 1, 0], [0, 1, 0], [0, .25, .5], [.5, 0, .5],
                 [0, 0, 0], [.5, .5, 1]]
    ci = 0
    colors = {}
    for prop in fieldRA:
        # Modify slicer so we can use it for plotting.
        slicer.slicePoints['ra'] = np.radians(fieldRA[prop])
        slicer.slicePoints['dec'] = np.radians(fieldDec[prop])
        fieldLocs = ma.MaskedArray(data=np.empty(len(fieldRA[prop]), object),
                                   mask=np.zeros(len(fieldRA[prop]), bool),
                                   fill_value=-99)
        colors[prop] = [
            colorlist[ci][0], colorlist[ci][1], colorlist[ci][2], 0.4
        ]
        ci += 1
        if ci == len(colorlist):
            ci = 0
        for i in xrange(len(fieldRA[prop])):
            fieldLocs.data[i] = colors[prop]
        skymap = plots.BaseSkyMap()
        fignum = skymap(fieldLocs,
                        slicer, {
                            'metricIsColor': True,
                            'bgcolor': 'lightgray',
                            'raCen': 0
                        },
                        fignum=fignum)
    plt.figure(fignum)
    labelcolors = []
    labeltext = []
    for prop in fieldRA:
        el = Ellipse((0, 0),
                     0.03,
                     0.03,
                     fc=(colors[prop][0], colors[prop][1], colors[prop][2]),
                     alpha=colors[prop][3])
        labelcolors.append(el)
        labeltext.append(prop.rstrip('.conf'))
    plt.legend(labelcolors, labeltext, loc=(0.85, 0.9), fontsize='smaller')
    plt.show()
Ejemplo n.º 5
0
def runSlices(opsimName,
              metadata,
              simdata,
              fields,
              bins,
              args,
              opsDb,
              verbose=False):
    # Set up the movie slicer.
    movieslicer = setupMovieSlicer(simdata, bins)
    # Set up formatting for output suffix.
    sliceformat = '%s0%dd' % ('%', int(np.log10(len(movieslicer))) + 1)
    # Get the telescope latitude info.
    lat_tele = Site(name='LSST').latitude_rad
    # Run through the movie slicer slicePoints and generate plots at each point.
    for i, ms in enumerate(movieslicer):
        t = time.time()
        slicenumber = sliceformat % (i)
        if verbose:
            print(slicenumber)
        # Set up metrics.
        if args.movieStepsize != 0:
            tstep = args.movieStepsize
        else:
            tstep = ms['slicePoint']['binRight'] - bins[i]
            if tstep > 1:
                tstep = 40. / 24. / 60. / 60.
        # Add simple view of time to plot label.
        times_from_start = ms['slicePoint']['binRight'] - (int(bins[0]) +
                                                           0.16 - 0.5)
        # Opsim years are 365 days (not 365.25)
        years = int(times_from_start / 365)
        days = times_from_start - years * 365
        plotlabel = 'Year %d Day %.4f' % (years, days)
        # Set up metrics.
        metricList, plotDictList = setupMetrics(
            opsimName,
            metadata,
            plotlabel=plotlabel,
            t0=ms['slicePoint']['binRight'],
            tStep=tstep,
            years=years,
            verbose=verbose)
        # Identify the subset of simdata in the movieslicer 'data slice'
        simdatasubset = simdata[ms['idxs']]
        # Set up opsim slicer on subset of simdata provided by movieslicer
        opslicer = slicers.OpsimFieldSlicer(
            simDataFieldIdColName='opsimFieldId',
            fieldIdColName='opsimFieldId')
        # Set up metricBundles to combine metrics, plotdicts and slicer.
        bundles = []
        sqlconstraint = ''
        for metric, plotDict in zip(metricList, plotDictList):
            bundles.append(
                metricBundles.MetricBundle(metric,
                                           opslicer,
                                           constraint=sqlconstraint,
                                           metadata=metadata,
                                           runName=opsimName,
                                           plotDict=plotDict))
        # Remove (default) stackers from bundles, because we've already run them above on the original data.
        for mb in bundles:
            mb.stackerList = []
        bundledict = metricBundles.makeBundlesDictFromList(bundles)
        # Set up metricBundleGroup to handle metrics calculation + plotting
        bg = metricBundles.MetricBundleGroup(bundledict,
                                             opsDb,
                                             outDir=args.outDir,
                                             resultsDb=None,
                                             saveEarly=False)
        # 'Hack' bundleGroup to just go ahead and run the metrics, without querying the database.
        simData = simdatasubset
        bg.fieldData = fields
        bg.setCurrent(sqlconstraint)
        bg.runCurrent(constraint=sqlconstraint, simData=simData)
        # Plot data each metric, for this slice of the movie, adding slicenumber as a suffix for output plots.
        # Plotting here, rather than automatically via sliceMetric method because we're going to rotate the sky,
        #  and add extra legend info and figure text (for FilterColors metric).
        ph = plots.PlotHandler(outDir=args.outDir,
                               figformat='png',
                               dpi=72,
                               thumbnail=False,
                               savefig=False)
        obsnow = np.where(simdatasubset['observationStartMJD'] ==
                          simdatasubset['observationStartMJD'].max())[0]
        raCen = np.radians(
            np.mean(simdatasubset[obsnow]['observationStartLST']))
        # Calculate horizon location.
        horizonlon, horizonlat = addHorizon(lat_telescope=lat_tele)
        # Create the plot for each metric and save it (after some additional manipulation).
        for mb in bundles:
            ph.setMetricBundles([mb])
            fignum = ph.plot(plotFunc=plots.BaseSkyMap(),
                             plotDicts={'raCen': raCen})
            fig = plt.figure(fignum)
            ax = plt.gca()
            # Add horizon and zenith.
            plt.plot(horizonlon, horizonlat, 'k.', alpha=0.3, markersize=1.8)
            plt.plot(0, lat_tele, 'k+')
            # For the FilterColors metric, add some extra items.
            if mb.metric.name == 'FilterColors':
                # Add the time stamp info (plotlabel) with a fancybox.
                plt.figtext(0.75,
                            0.9,
                            '%s' % (plotlabel),
                            bbox=dict(boxstyle='Round, pad=0.7',
                                      fc='w',
                                      ec='k',
                                      alpha=0.5))
                # Add a legend for the filters.
                filterstacker = stackers.FilterColorStacker()
                for i, f in enumerate(['u', 'g', 'r', 'i', 'z', 'y']):
                    plt.figtext(0.92,
                                0.55 - i * 0.035,
                                f,
                                color=filterstacker.filter_rgb_map[f])
                # Add a moon.
                moonRA = np.radians(np.mean(simdatasubset[obsnow]['moonRA']))
                lon = -(moonRA - raCen - np.pi) % (np.pi * 2) - np.pi
                moonDec = np.radians(np.mean(simdatasubset[obsnow]['moonDec']))
                # Note that moonphase is 0-100 (translate to 0-1). 0=new.
                moonPhase = np.mean(simdatasubset[obsnow]['moonPhase']) / 100.
                alpha = np.max([moonPhase, 0.15])
                circle = Circle((lon, moonDec),
                                radius=0.05,
                                color='k',
                                alpha=alpha)
                ax.add_patch(circle)
                # Add some explanatory text.
                ecliptic = Line2D([], [], color='r', label="Ecliptic plane")
                galaxy = Line2D([], [], color='b', label="Galactic plane")
                horizon = Line2D([], [],
                                 color='k',
                                 alpha=0.3,
                                 label="20 deg elevation limit")
                moon = Line2D([], [],
                              color='k',
                              linestyle='',
                              marker='o',
                              markersize=8,
                              alpha=alpha,
                              label="\nMoon (Dark=Full)\n         (Light=New)")
                zenith = Line2D([], [],
                                color='k',
                                linestyle='',
                                marker='+',
                                markersize=5,
                                label="Zenith")
                plt.legend(
                    handles=[horizon, zenith, galaxy, ecliptic, moon],
                    loc=[0.1, -0.35],
                    ncol=3,
                    frameon=False,
                    title=
                    'Aitoff plot showing HA/Dec of simulated survey pointings',
                    numpoints=1,
                    fontsize='small')
            # Save figure.
            plt.savefig(os.path.join(
                args.outDir,
                mb.metric.name + '_' + slicenumber + '_SkyMap.png'),
                        format='png',
                        dpi=72)
            plt.close('all')
            dt, t = dtime(t)
            if verbose:
                print('Ran and plotted slice %s of movieslicer in %f s' %
                      (slicenumber, dt))