コード例 #1
0
    def plot(self, bmap, ax_to_plot, unpacked_healpix, **kwargs):

        if len(self.polygon_coords) > 0:

            cra_deg, cdec_deg = zip(*[(coord_deg[0], coord_deg[1])
                                      for coord_deg in self.polygon_coords])

            cx, cy = bmap(cra_deg, cdec_deg)
            clat_lons = np.vstack([cx, cy]).transpose()
            ax_to_plot.add_patch(Polygon(clat_lons, **kwargs))

        else:

            if self.relative_prob < 1e-8:
                fc = 'k'
                ec = 'None'
                alph = 0.1
            else:
                fc = 'None'
                ec = 'k'
                alph = 1.0

            c = coord.SkyCoord(self.RA, self.Dec, unit=(u.deg, u.deg))
            pe = Pixel_Element(self.pixel_index, unpacked_healpix.nside,
                               unpacked_healpix.prob[self.pixel_index])
            pe.plot(bmap,
                    ax_to_plot,
                    facecolor=fc,
                    edgecolor=ec,
                    linewidth=0.1,
                    alpha=alph,
                    zorder=9500)
コード例 #2
0
    def main(self):

        is_error = False
        has_candidates = False

        # Parameter checks
        if self.options.gw_id == "":
            is_error = True
            print("GWID is required.")

        formatted_healpix_dir = self.options.healpix_dir
        if "{GWID}" in formatted_healpix_dir:
            formatted_healpix_dir = formatted_healpix_dir.replace(
                "{GWID}", self.options.gw_id)

        hpx_path = "%s/%s" % (formatted_healpix_dir, self.options.healpix_file)
        candidate_file_path = "%s/Candidates/%s" % (
            formatted_healpix_dir, self.options.candidate_file)

        if self.options.healpix_file == "":
            is_error = True
            print("You must specify which healpix file to process.")

        if os.path.exists(candidate_file_path):
            has_candidates = True
        else:
            print("Skipping plot candidates.")

        if is_error:
            print("Exiting...")
            return 1

        # Get Map ID
        healpix_map_select = "SELECT id, NSIDE FROM HealpixMap WHERE GWID = '%s' and Filename = '%s'"
        healpix_map_id = query_db([
            healpix_map_select %
            (self.options.gw_id, self.options.healpix_file)
        ])[0][0][0]
        healpix_map_nside = query_db([
            healpix_map_select %
            (self.options.gw_id, self.options.healpix_file)
        ])[0][0][1]

        # Get configured Detectors
        detector_select = "SELECT id, Name, Deg_width, Deg_width, Deg_radius, Area, MinDec, MaxDec FROM Detector"
        detector_result = query_db([detector_select])[0]
        detectors = [
            Detector(dr[1], float(dr[2]), float(dr[2]), detector_id=int(dr[0]))
            for dr in detector_result
        ]

        # Get all observed tiles for all configured detectors
        observed_tile_select = '''
			SELECT 
				id,
				Detector_id, 
				FieldName, 
				RA, 
				_Dec, 
				EBV, 
				N128_SkyPixel_id, 
				Band_id, 
				MJD, 
				Exp_Time, 
				Mag_Lim, 
				HealpixMap_id 
			FROM
				ObservedTile 
			WHERE
				HealpixMap_id = %s and 
				Detector_id = %s 
		'''
        observed_tiles = {}
        for d in detectors:

            ot_result = query_db(
                [observed_tile_select % (healpix_map_id, d.id)])[0]
            observed_tiles[d.name] = [
                Tile(float(ot[3]),
                     float(ot[4]),
                     d.deg_width,
                     d.deg_height,
                     healpix_map_nside,
                     tile_id=int(ot[0])) for ot in ot_result
            ]

            print("Loaded %s %s tiles..." %
                  (len(observed_tiles[d.name]), d.name))

        # load candidates if they exist
        candidates = []
        if has_candidates:
            with open(candidate_file_path, 'r') as csvfile:

                csvreader = csv.reader(csvfile, delimiter=',')
                next(csvreader)  # skip header

                for row in csvreader:
                    name = row[0]
                    ra = float(row[1])
                    dec = float(row[2])
                    flag1 = bool(int(row[3]))
                    flag2 = bool(int(row[4]))
                    is_keck = bool(int(row[5]))

                    # append tuple
                    candidates.append(
                        (name, coord.SkyCoord(ra, dec, unit=(u.deg, u.deg)),
                         flag1, flag2, is_keck))

        select_pix = '''
			SELECT 
				running_prob.id, 
				running_prob.HealpixMap_id, 
				running_prob.Pixel_Index, 
				running_prob.Prob, 
				running_prob.Distmu, 
				running_prob.Distsigma, 
				running_prob.Mean, 
				running_prob.Stddev, 
				running_prob.Norm, 
				running_prob.N128_SkyPixel_id, 
				running_prob.cum_prob 
			FROM 
				(SELECT 
					hp_prob.id, 
					hp_prob.HealpixMap_id, 
					hp_prob.Pixel_Index,
					hp_prob.Prob, 
					hp_prob.Distmu, 
					hp_prob.Distsigma, 
					hp_prob.Mean, 
					hp_prob.Stddev, 
					hp_prob.Norm, 
					hp_prob.N128_SkyPixel_id, 
					SUM(hp_prob.Prob) OVER(ORDER BY hp_prob.Prob DESC) AS cum_prob 
				FROM 
					(SELECT 
						hp.id, 
						hp.HealpixMap_id, 
						hp.Pixel_Index,
						hp.Prob, 
						hp.Distmu, 
						hp.Distsigma, 
						hp.Mean, 
						hp.Stddev, 
						hp.Norm, 
						hp.N128_SkyPixel_id 
					FROM HealpixPixel hp 
					WHERE hp.HealpixMap_id = %s 
					ORDER BY
						hp.Prob DESC) hp_prob
					GROUP BY
						hp_prob.id, 
						hp_prob.HealpixMap_id, 
						hp_prob.Pixel_Index,
						hp_prob.Prob, 
						hp_prob.Distmu, 
						hp_prob.Distsigma, 
						hp_prob.Mean, 
						hp_prob.Stddev, 
						hp_prob.Norm, 
						hp_prob.N128_SkyPixel_id 
					) running_prob 
			WHERE 
				running_prob.cum_prob <= 0.9 
		'''

        print("Selecting map pixels...")
        map_pix_result = query_db([select_pix % healpix_map_id])[0]
        print("...done")

        print("Building pixel elements...")
        map_pix = [
            Pixel_Element(int(mp[2]),
                          healpix_map_nside,
                          float(mp[3]),
                          pixel_id=int(mp[0])) for mp in map_pix_result
        ]
        map_pix_sorted = sorted(map_pix, key=lambda x: x.prob, reverse=True)
        print("...done")

        cutoff_50th = 0.5
        cutoff_90th = 0.9
        index_50th = 0
        index_90th = 0

        print("Find index for 50th...")
        cum_prob = 0.0
        for i in range(len(map_pix_sorted)):
            cum_prob += map_pix_sorted[i].prob
            index_50th = i

            if (cum_prob >= cutoff_50th):
                break

        print("... %s" % index_50th)

        print("Find index for 90th...")
        cum_prob = 0.0
        for i in range(len(map_pix_sorted)):
            cum_prob += map_pix_sorted[i].prob
            index_90th = i

            if (cum_prob >= cutoff_90th):
                break
        print("... %s" % index_90th)

        # print("Build multipolygons...")
        # net_50_polygon = []
        # for p in map_pix_sorted[0:index_50th]:
        # 	net_50_polygon += p.query_polygon
        # joined_50_poly = unary_union(net_50_polygon)

        # # Fix any seams
        # eps = 0.00001
        # merged_50_poly = []
        # smoothed_50_poly = joined_50_poly.buffer(eps, 1, join_style=JOIN_STYLE.mitre).buffer(-eps, 1, join_style=JOIN_STYLE.mitre)

        # try:
        # 	test_iter = iter(smoothed_50_poly)
        # 	merged_50_poly = smoothed_50_poly
        # except TypeError as te:
        # 	merged_50_poly.append(smoothed_50_poly)

        # print("Number of sub-polygons in `merged_50_poly`: %s" % len(merged_50_poly))
        # sql_50_poly = SQL_Polygon(merged_50_poly, detectors[0])

        # net_90_polygon = []
        # for p in map_pix_sorted[0:index_90th]:
        # 	net_90_polygon += p.query_polygon
        # joined_90_poly = unary_union(net_90_polygon)

        # # Fix any seams
        # merged_90_poly = []
        # smoothed_90_poly = joined_90_poly.buffer(eps, 1, join_style=JOIN_STYLE.mitre).buffer(-eps, 1, join_style=JOIN_STYLE.mitre)

        # try:
        # 	test_iter = iter(smoothed_90_poly)
        # 	merged_90_poly = smoothed_90_poly
        # except TypeError as te:
        # 	merged_90_poly.append(smoothed_90_poly)

        # print("Number of sub-polygons in `merged_90_poly`: %s" % len(merged_90_poly))
        # sql_90_poly = SQL_Polygon(merged_90_poly, detectors[0])
        # print("... done.")

        sql_50_poly = None
        with open('sql50.pkl', 'rb') as handle:
            sql_50_poly = pickle.load(handle)

        sql_90_poly = None
        with open('sql90.pkl', 'rb') as handle:
            sql_90_poly = pickle.load(handle)

        fig = plt.figure(figsize=(10, 10), dpi=1000)
        ax = fig.add_subplot(111)

        m = Basemap(projection='stere',
                    lon_0=15.0,
                    lat_0=-20.0,
                    llcrnrlat=-35.0,
                    urcrnrlat=-19.5,
                    llcrnrlon=8.0,
                    urcrnrlon=24.5)
        # # m = Basemap(projection='moll',lon_0=180.0)

        # print("Plotting `pixels_filled`...")

        # Scale colormap
        pix_90 = map_pix_sorted[0:index_90th]
        pixel_probs = [p.prob for p in pix_90]
        min_prob = np.min(pixel_probs)
        max_prob = np.max(pixel_probs)
        # min_prob = 1.341511144989834e-06
        # max_prob = 6.890843958619067e-05

        print("min prob: %s" % min_prob)
        print("max prob: %s" % max_prob)

        # fracs = pixels_probs/max_prob
        # norm = colors.Normalize(fracs.min(), fracs.max())
        # norm = colors.LogNorm(min_prob, max_prob)
        norm = colors.Normalize(min_prob, max_prob)
        # norm = colors.LogNorm(1e-18, max_prob)
        # log_lower_limit = 1e-8
        # norm = colors.LogNorm(log_lower_limit, max_prob)

        # clrs = {
        # 	"SWOPE":"yellow",
        # 	"NICKEL":"brown",
        # 	"THACHER":"blue",
        # 	"ANDICAM":"red",
        # 	"MOSFIRE":"green",
        # }

        clrs = {
            "SWOPE": (230.0 / 256.0, 159 / 256.0, 0),
            "NICKEL": (0, 114.0 / 256.0, 178.0 / 256.0),
            "THACHER": (0, 158.0 / 256.0, 115.0 / 256.0),
            "MOSFIRE": (204.0 / 256.0, 121.0 / 256.0, 167.0 / 256.0),
            # "ANDICAM":"red",
            # "MOSFIRE":"green"
        }

        # Plot SWOPE, then THACHER, then NICKEL, then MOSFIRE
        x1, y1 = m(0, 0)
        m.plot(x1,
               y1,
               marker='s',
               markeredgecolor="k",
               markerfacecolor=clrs["SWOPE"],
               markersize=20,
               label="Swope",
               linestyle='None')
        m.plot(x1,
               y1,
               marker='s',
               markeredgecolor="k",
               markerfacecolor=clrs["THACHER"],
               markersize=16,
               label="Thacher",
               linestyle='None')
        m.plot(x1,
               y1,
               marker='s',
               markeredgecolor="k",
               markerfacecolor=clrs["NICKEL"],
               markersize=14,
               label="Nickel",
               linestyle='None')
        m.plot(x1,
               y1,
               marker='s',
               markeredgecolor="k",
               markerfacecolor=clrs["MOSFIRE"],
               markersize=10,
               label="MOSFIRE",
               linestyle='None')

        tile_opacity = 0.1
        print("Plotting Tiles for: %s (%s)" % ("SWOPE", clrs["SWOPE"]))
        for i, t in enumerate(observed_tiles["SWOPE"]):
            t.plot(m,
                   ax,
                   edgecolor=clrs["SWOPE"],
                   facecolor=clrs["SWOPE"],
                   linewidth=0.25,
                   alpha=tile_opacity)
            t.plot(m,
                   ax,
                   edgecolor='k',
                   facecolor="None",
                   linewidth=0.25,
                   alpha=1.0)

        print("Plotting Tiles for: %s (%s)" % ("THACHER", clrs["THACHER"]))
        for i, t in enumerate(observed_tiles["THACHER"]):
            t.plot(m,
                   ax,
                   edgecolor=clrs["THACHER"],
                   facecolor=clrs["THACHER"],
                   linewidth=0.25,
                   alpha=tile_opacity)
            t.plot(m,
                   ax,
                   edgecolor='k',
                   facecolor="None",
                   linewidth=0.25,
                   alpha=1.0)

        print("Plotting Tiles for: %s (%s)" % ("NICKEL", clrs["NICKEL"]))
        for i, t in enumerate(observed_tiles["NICKEL"]):
            t.plot(m,
                   ax,
                   edgecolor=clrs["NICKEL"],
                   facecolor=clrs["NICKEL"],
                   linewidth=0.25,
                   alpha=tile_opacity)
            t.plot(m,
                   ax,
                   edgecolor='k',
                   facecolor="None",
                   linewidth=0.25,
                   alpha=1.0)

        print("Plotting Tiles for: %s (%s)" % ("MOSFIRE", clrs["MOSFIRE"]))
        for i, t in enumerate(observed_tiles["MOSFIRE"]):
            t.plot(m,
                   ax,
                   edgecolor=clrs["MOSFIRE"],
                   facecolor=clrs["MOSFIRE"],
                   linewidth=0.25,
                   alpha=1.0,
                   zorder=9999)
            t.plot(m,
                   ax,
                   edgecolor='k',
                   facecolor="None",
                   linewidth=0.25,
                   alpha=1.0,
                   zorder=9999)

        print("Plotting (%s) `pixels`..." % len(pix_90))
        for i, p in enumerate(pix_90):
            p.plot(m,
                   ax,
                   facecolor=plt.cm.Greys(norm(p.prob)),
                   edgecolor='None',
                   linewidth=0.5,
                   alpha=norm(p.prob) * 0.8)

        print("Plotting SQL Multipolygons")

        # with open('sql50.pkl', 'wb') as handle:
        # 	pickle.dump(sql_50_poly, handle, protocol=pickle.HIGHEST_PROTOCOL)

        # with open('sql90.pkl', 'wb') as handle:
        # 	pickle.dump(sql_90_poly, handle, protocol=pickle.HIGHEST_PROTOCOL)
        sql_50_poly.plot(m,
                         ax,
                         edgecolor='black',
                         linewidth=1.0,
                         facecolor='None')
        sql_90_poly.plot(m,
                         ax,
                         edgecolor='gray',
                         linewidth=0.75,
                         facecolor='None')

        # Plotted off the map so that the legend will have a line item
        if has_candidates:
            for c in candidates:
                x, y = m(c[1].ra.degree, c[1].dec.degree)

                if c[4]:
                    m.plot(x,
                           y,
                           marker='*',
                           markeredgecolor='k',
                           markerfacecolor='gray',
                           markersize=20.0,
                           zorder=9999)
                else:
                    mkrclr = 'gray'
                    if c[2] and c[3]:  # passes both Charlie and Ryan cuts
                        mkrclr = 'red'
                    m.plot(x,
                           y,
                           marker='o',
                           markeredgecolor='k',
                           markerfacecolor=mkrclr,
                           markersize=5.0)

            x1, y1 = m(0, 0)
            m.plot(x1,
                   y1,
                   marker='.',
                   linestyle="None",
                   markeredgecolor="k",
                   markerfacecolor="red",
                   markersize=20,
                   label="Viable candidate")

            x2, y2 = m(0, 0)
            m.plot(x2,
                   y2,
                   marker='.',
                   linestyle="None",
                   markeredgecolor="k",
                   markerfacecolor="grey",
                   markersize=20,
                   label="Excluded")

            x3, y3 = m(0, 0)
            m.plot(x3,
                   y3,
                   marker='*',
                   linestyle="None",
                   markeredgecolor="k",
                   markerfacecolor="grey",
                   markersize=20,
                   label="Keck Observed")

        # # # -------------- Use this for mollweide projections --------------
        # # meridians = np.arange(0.,360.,60.)
        # # m.drawparallels(np.arange(-90.,91.,30.),fontsize=14,labels=[True,True,False,False],dashes=[2,2],linewidth=0.5) # , xoffset=2500000
        # # m.drawmeridians(meridians,labels=[False,False,False,False],dashes=[2,2],linewidth=0.5)
        # # # ----------------------------------------------------------------

        # # # -------------- Use this for stereographic projections ----------
        # # # draw meridians
        # # meridians = np.arange(0.,360.,10.)
        # # par = m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=18,zorder=-1,color='gray', linewidth=0.5)

        # # # draw parallels
        # # parallels = np.arange(-90.,90.,10.)
        # # par = m.drawparallels(parallels,labels=[0,1,0,0],fontsize=18,zorder=-1,color='gray', linewidth=0.5, xoffset=230000)
        # # ## ----------------------------------------------------------------

        # # for mer in meridians[1:]:
        # # 	plt.annotate("%0.0f" % mer,xy=m(mer,0),xycoords='data', fontsize=14, zorder=9999)

        # draw parallels.
        sm_label_size = 18
        label_size = 28
        title_size = 36

        _90_x1 = 0.77
        _90_y1 = 0.558

        _90_x2 = 0.77
        _90_y2 = 0.40

        _90_text_y = 0.37
        _90_text_x = 0.32

        _50_x1 = 0.60
        _50_y1 = 0.51

        _50_x2 = 0.48
        _50_y2 = 0.40

        _50_text_y = 0.37
        _50_text_x = 0.64

        ax.annotate('',
                    xy=(_90_x1, _90_y1),
                    xytext=(_90_x2, _90_y2),
                    xycoords='axes fraction',
                    arrowprops={
                        'arrowstyle': '-',
                        'lw': 2.0
                    })
        ax.annotate('',
                    xy=(_50_x1, _50_y1),
                    xytext=(_50_x2, _50_y2),
                    xycoords='axes fraction',
                    arrowprops={
                        'arrowstyle': '-',
                        'lw': 2.0
                    })

        ax.annotate('90th percentile',
                    xy=(_50_text_x, _50_text_y),
                    xycoords='axes fraction',
                    fontsize=sm_label_size)
        ax.annotate('50th percentile',
                    xy=(_90_text_x, _90_text_y),
                    xycoords='axes fraction',
                    fontsize=sm_label_size)

        parallels = np.arange(-90., 90., 10.)
        dec_ticks = m.drawparallels(parallels, labels=[0, 1, 0, 0])
        for i, tick_obj in enumerate(dec_ticks):
            a = coord.Angle(tick_obj, unit=u.deg)

            for text_obj in dec_ticks[tick_obj][1]:
                direction = '+' if a.dms[0] > 0.0 else '-'
                text_obj.set_text(r'${0}{1:0g}^{{\degree}}$'.format(
                    direction, np.abs(a.dms[0])))
                text_obj.set_size(sm_label_size)
                x = text_obj.get_position()[0]

                new_x = x * (1.0 + 0.08)
                text_obj.set_x(new_x)

        # draw meridians
        meridians = np.arange(0., 360., 7.5)
        ra_ticks = m.drawmeridians(meridians, labels=[0, 0, 0, 1])

        RA_label_dict = {
            7.5: r'$00^{\mathrm{h}}30^{\mathrm{m}}$',
            15.0: r'$01^{\mathrm{h}}00^{\mathrm{m}}$',
            22.5: r'$01^{\mathrm{h}}30^{\mathrm{m}}$',
        }

        for i, tick_obj in enumerate(ra_ticks):
            # a = coord.Angle(tick_obj, unit=u.deg)
            # print(a.hms)
            print(tick_obj)
            for text_obj in ra_ticks[tick_obj][1]:
                if tick_obj in RA_label_dict:
                    text_obj.set_text(RA_label_dict[tick_obj])
                    text_obj.set_size(sm_label_size)

        sm = plt.cm.ScalarMappable(norm=norm, cmap=plt.cm.Greys)
        sm.set_array([])  # can be an empty list

        tks = np.linspace(min_prob, max_prob, 6)
        # tks = np.logspace(np.log10(min_prob), np.log10(max_prob), 11)
        tks_strings = []

        for t in tks:
            tks_strings.append('%0.2f' % (t * 100))

        cb = fig.colorbar(sm,
                          ax=ax,
                          ticks=tks,
                          orientation='vertical',
                          fraction=0.04875,
                          pad=0.02,
                          alpha=0.80)  # 0.08951
        cb.ax.set_yticklabels(tks_strings, fontsize=16)
        cb.set_label("2D Pixel Probability", fontsize=label_size, labelpad=9.0)

        cb.ax.tick_params(width=2.0, length=6.0)

        cb.outline.set_linewidth(2.0)

        for axis in ['top', 'bottom', 'left', 'right']:
            ax.spines[axis].set_linewidth(2.0)

        ax.invert_xaxis()
        ax.legend(loc='upper left',
                  fontsize=sm_label_size,
                  borderpad=0.35,
                  handletextpad=0.0,
                  labelspacing=0.4)
        # ax.legend(fontsize=14)

        plt.ylabel(r'$\mathrm{Declination}$', fontsize=label_size, labelpad=36)
        plt.xlabel(r'$\mathrm{Right\;Ascension}$',
                   fontsize=label_size,
                   labelpad=30)

        fig.savefig('Keck.png', bbox_inches='tight')  #,dpi=840
        plt.close('all')
        print("... Done.")
コード例 #3
0
    def main(self):

        ### UNPACKING THE HEALPIX FILES ###

        hpx_path = "%s/%s" % (self.options.healpix_dir,
                              self.options.healpix_file)

        # If you specify a telescope that's not in the default list, you must provide the rest of the information
        detector = None
        is_error = False
        is_custom_percentile = False
        custom_percentile = None

        # Check the inputs for errors...
        if self.options.telescope_abbreviation not in self.telescope_mapping.keys(
        ):
            print(
                "Running for custom telescope. Checking required parameters..."
            )

            if self.options.telescope_abbreviation == "":
                is_error = True
                print(
                    "For custom telescope, `telescope_abbreviation` is required!"
                )

            if self.options.telescope_name == "":
                is_error = True
                print("For custom telescope, `telescope_name` is required!")

            if self.options.detector_width_deg <= 0.0:
                is_error = True
                print(
                    "For custom telescope, `detector_width_deg` is required, and must be > 0!"
                )

            if self.options.detector_height_deg <= 0.0:
                is_error = True
                print(
                    "For custom telescope, `detector_height_deg` is required, and must be > 0!"
                )

            if not is_error:
                detector = Detector(self.options.telescope_name,
                                    self.options.detector_width_deg,
                                    self.options.detector_height_deg)
        else:
            detector = self.telescope_mapping[
                self.options.telescope_abbreviation]

        if self.options.percentile > 0.9:
            is_error = True
            print("User-defined percentile must be <= 0.90")
        elif self.options.percentile > 0.0:
            is_custom_percentile = True
            custom_percentile = self.options.percentile

        if is_error:
            print("Exiting...")
            return 1

        print("\n\nTelescope: `%s -- %s`, width: %s [deg]; height %s [deg]" %
              (self.options.telescope_abbreviation, detector.name,
               detector.deg_width, detector.deg_height))
        print("%s FOV area: %s" % (detector.name,
                                   (detector.deg_width * detector.deg_height)))

        if is_custom_percentile:
            print("\nTiling to %s percentile." % self.options.percentile)

        t1 = time.time()

        print("Unpacking '%s':%s..." % (self.options.gw_id, hpx_path))
        prob, distmu, distsigma, distnorm, header_gen = hp.read_map(
            hpx_path, field=(0, 1, 2, 3), h=True)
        header = dict(header_gen)

        t2 = time.time()

        print("\n********* start DEBUG ***********")
        print("`Unpacking healpix` execution time: %s" % (t2 - t1))
        print("********* end DEBUG ***********\n")

        npix = len(prob)
        print("\nNumber of pix in '%s': %s" % (hpx_path, len(prob)))

        sky_area = 4 * 180**2 / np.pi
        area_per_px = sky_area / npix
        print("Sky Area per pix in '%s': %s [sq deg]" %
              (hpx_path, area_per_px))

        sky_area_radians = 4 * np.pi
        steradian_per_pix = sky_area_radians / npix
        pixel_radius_radian = np.sqrt(steradian_per_pix / np.pi)
        print("Steradian per px for '%s': %s" % (hpx_path, steradian_per_pix))

        # nside = the lateral resolution of the HEALPix map
        nside = hp.npix2nside(npix)
        print("Resolution (nside) of '%s': %s\n" % (hpx_path, nside))

        print("Processing for %s..." % detector.name)
        num_px_per_field = (detector.deg_height *
                            detector.deg_width) / area_per_px
        print("Pix per (%s) field for '%s': %s" %
              (detector.name, hpx_path, num_px_per_field))

        percentile = self.options.percentile
        unpacked_healpix = Unpacked_Healpix(
            hpx_path,
            prob,
            distmu,
            distsigma,
            distnorm,
            header,
            nside,
            npix,
            area_per_px,
            linestyle="-",
            compute_contours=False,
            custom_percentile=custom_percentile)

        num_50 = len(unpacked_healpix.indices_of_50)
        num_90 = len(unpacked_healpix.indices_of_90)

        area_50 = num_50 * area_per_px
        area_90 = num_90 * area_per_px

        # Debug -- should match Grace DB statistics
        print("\nArea of 50th: %s" % area_50)
        print("Area of 90th: %s\n" % area_90)

        if is_custom_percentile:
            num_custom = len(unpacked_healpix.pixels_custom)
            area_custom = num_custom * area_per_px
            print("\n[User defined] Area of %s: %s\n" %
                  (custom_percentile, area_custom))

        ### GENERATE THE ALL-SKY TILING FOR A DETECTOR & GET THE ENCLOSED GALAXIES ###

        print("Generating all sky coords for %s" % detector.name)
        detector_all_sky_coords = Cartographer.generate_all_sky_coords(
            detector)
        base_cartography = Cartographer(self.options.gw_id,
                                        unpacked_healpix,
                                        detector,
                                        detector_all_sky_coords,
                                        generate_tiles=True)

        # Save cartograpy
        with open(
                '%s/%s_base_cartography.pkl' %
            (self.options.working_dir, self.options.gw_id), 'wb') as handle:
            pickle.dump(base_cartography,
                        handle,
                        protocol=pickle.HIGHEST_PROTOCOL)

        if not self.options.skip_completeness:
            # Build the spatial query
            t1 = time.time()

            # Join all tiles together into a composite polygon to simplify the SQL query
            net_polygon = []
            for t in base_cartography.tiles:
                net_polygon += t.query_polygon
            joined_poly = unary_union(net_polygon)

            # Fix any seams
            eps = 0.00001
            merged_poly = joined_poly.buffer(
                eps, 1, join_style=JOIN_STYLE.mitre).buffer(
                    -eps, 1, join_style=JOIN_STYLE.mitre)

            try:
                print("Number of sub-polygons in query: %s" % len(merged_poly))
            except TypeError as e:
                print("Warning...")
                print(e)
                print(
                    "\nOnly one polygons in query! Wrapping `merged_poly` and resuming..."
                )

                merged_poly = [merged_poly]

            # Create and save sql_poly
            sql_poly = SQL_Polygon(merged_poly, detector)
            with open(
                    '%s/%s_sql_poly.pkl' %
                (self.options.working_dir, self.options.gw_id),
                    'wb') as handle:
                pickle.dump(sql_poly, handle, protocol=pickle.HIGHEST_PROTOCOL)

            # # Use the sql_poly string to create the WHERE clause
            mp_where = "ST_WITHIN(Coord, ST_GEOMFROMTEXT('"
            mp_where += sql_poly.query_polygon_string
            mp_where += "', 4326));"

            t2 = time.time()

            print("\n********* start DEBUG ***********")
            print("`Generating multipolygon` execution time: %s" % (t2 - t1))
            print("********* end DEBUG ***********\n")

            # Database I/O
            t1 = time.time()

            query = "SELECT * from GalaxyDistance2 WHERE z_dist IS NOT NULL AND z_dist_err IS NOT NULL AND B IS NOT NULL AND "
            query += mp_where

            print(query)
            print("\n*****************************\n")

            # Save cartograpy
            with open(
                    '%s/%s_query.pkl' %
                (self.options.working_dir, self.options.gw_id),
                    'wb') as handle:
                pickle.dump(query, handle, protocol=pickle.HIGHEST_PROTOCOL)

            result = QueryDB(query, port=LOCAL_PORT)

            t2 = time.time()

            print("\n********* start DEBUG ***********")
            print("`Query database` execution time: %s" % (t2 - t1))
            print("********* end DEBUG ***********\n")

            # Instantiate galaxies
            contained_galaxies = []

            # What is the angular radius for our given map?
            cosmo = LambdaCDM(H0=70, Om0=0.3, Ode0=0.7)
            pixel_diameter_arcsec = np.degrees(
                pixel_radius_radian) * 2.0 * 3600.0
            proper_radius = 20.0  # kpc

            z = np.linspace(1e-18, 0.3, 1000)
            arcsec_of_proper_diam = cosmo.arcsec_per_kpc_proper(
                z) * proper_radius * 2.0
            z_index = find_nearest(arcsec_of_proper_diam,
                                   pixel_diameter_arcsec)
            target_z = z[z_index]
            dist_cuttoff = cosmo.luminosity_distance(target_z).value  # Mpc

            print("Redshift cutoff: %s" % target_z)
            print("Distance cutoff: %s" % dist_cuttoff)

            for row in result:
                g = glade_galaxy(row, base_cartography.unpacked_healpix, cosmo,
                                 dist_cuttoff, proper_radius)
                contained_galaxies.append(g)

            print("Query returned %s galaxies" % len(contained_galaxies))

            avg_dist = average_distance_prior(
                base_cartography.unpacked_healpix)
            catalog_completeness = GLADE_completeness(avg_dist)
            print("Completeness: %s" % catalog_completeness)

            ### REDISTRIBUTE THE PROBABILITY IN THE MAP TO THE GALAXIES ###

            print("Assigning relative prob...")
            Cartographer.assign_galaxy_relative_prob(
                base_cartography.unpacked_healpix, contained_galaxies,
                base_cartography.cumlative_prob_in_tiles, catalog_completeness)

            # Save contained galaxies
            with open(
                    '%s/%s_contained_galaxies.pkl' %
                (self.options.working_dir, self.options.gw_id),
                    'wb') as handle:
                pickle.dump(contained_galaxies,
                            handle,
                            protocol=pickle.HIGHEST_PROTOCOL)

            print("Redistribute prob...")
            # redistributed_map
            redistributed_prob, enclosed_indices = Cartographer.redistribute_probability(
                base_cartography.unpacked_healpix, contained_galaxies,
                base_cartography.tiles, catalog_completeness)

            # Copy base cartography (have to do this?) and update the prob of the 90th perecentil
            redistributed_cartography = copy.deepcopy(base_cartography)
            redistributed_cartography.unpacked_healpix.prob = redistributed_prob
            redistributed_cartography.unpacked_healpix.pixels_90 = [
                Pixel_Element(
                    ei, redistributed_cartography.unpacked_healpix.nside,
                    redistributed_cartography.unpacked_healpix.prob[ei])
                for ei in enclosed_indices
            ]

            # Update the original tiles with the new probability
            redistributed_cartography.assign_tiles(base_cartography.tiles)
            # Save cartograpy
            with open(
                    '%s/%s_redstributed_cartography.pkl' %
                (self.options.working_dir, self.options.gw_id),
                    'wb') as handle:
                pickle.dump(redistributed_cartography,
                            handle,
                            protocol=pickle.HIGHEST_PROTOCOL)

        else:
            print("\n\nPure tiling...")

        ### SAVE THE RESULTS ###
        def GetSexigesimalString(c):
            ra = c.ra.hms
            dec = c.dec.dms

            ra_string = "%02d:%02d:%05.2f" % (ra[0], ra[1], ra[2])
            if dec[0] >= 0:
                dec_string = "+%02d:%02d:%05.2f" % (dec[0], np.abs(
                    dec[1]), np.abs(dec[2]))
            else:
                dec_string = "%03d:%02d:%05.2f" % (dec[0], np.abs(
                    dec[1]), np.abs(dec[2]))

            # Python has a -0.0 object. If the deg is this (because object lies < 60 min south), the string formatter will drop the negative sign
            if c.dec < 0.0 and dec[0] == 0.0:
                dec_string = "-00:%02d:%05.2f" % (np.abs(dec[1]), np.abs(
                    dec[2]))
            return (ra_string, dec_string)

        tiles_to_serialize = None
        if not self.options.skip_completeness:
            tiles_to_serialize = redistributed_cartography.tiles
        else:
            tiles_to_serialize = base_cartography.tiles

        t1 = time.time()
        sorted_tiles = sorted(tiles_to_serialize,
                              key=lambda x: x.net_prob,
                              reverse=True)

        with open(
                '%s/%s_%s_%s_Tiles.txt' %
            (self.options.working_dir, base_cartography.gwid,
             self.options.schedule_designation, detector.name),
                'w') as csvfile:

            csvwriter = csv.writer(csvfile)

            cols = []
            cols.append('# FieldName')
            cols.append('FieldRA')
            cols.append('FieldDec')
            cols.append('Telscope')
            cols.append('Filter')
            cols.append('ExpTime')
            cols.append('Priority')
            cols.append('Status')
            csvwriter.writerow(cols)

            for i, st in enumerate(sorted_tiles):

                c = coord.SkyCoord(st.ra_deg, st.dec_deg, unit=(u.deg, u.deg))
                coord_str = GetSexigesimalString(c)

                cols = []
                field_name = "%s%s%sE%s" % (
                    self.options.telescope_abbreviation,
                    str(self.options.event_number).zfill(3),
                    self.options.schedule_designation, str(i + 1).zfill(5))

                cols.append(field_name)
                cols.append(coord_str[0])
                cols.append(coord_str[1])
                cols.append(detector.name)
                cols.append(self.options.filter)
                cols.append(self.options.exp_time)
                cols.append(st.net_prob)
                cols.append('False')
                csvwriter.writerow(cols)

            print("Done")

        t2 = time.time()

        print("\n********* start DEBUG ***********")
        print("`Serialize tiles` execution time: %s" % (t2 - t1))
        print("********* end DEBUG ***********\n")
コード例 #4
0
    def initialize(self, compute_contours):

        t1 = time.time()

        sorted_prob = np.sort(self.prob)
        sorted_prob = sorted_prob[::-1]  # Reverse sort (highest first)
        max_prob = np.max(sorted_prob)

        # 99th percentile
        _99_cut, index_99 = self.get_prob_val_and_index(sorted_prob, 0.99)
        # 95th percentile
        _95_cut, index_95 = self.get_prob_val_and_index(sorted_prob, 0.95)
        # 90th percentile
        _90_cut, index_90 = self.get_prob_val_and_index(sorted_prob, 0.90)
        # 70th percentile
        _70_cut, index_70 = self.get_prob_val_and_index(sorted_prob, 0.70)
        # 50th percentile
        _50_cut, index_50 = self.get_prob_val_and_index(sorted_prob, 0.50)

        lvls = [_90_cut, _70_cut, _50_cut, max_prob]

        indices_of_50 = np.where(self.prob >= _50_cut)[0]
        indices_of_70 = np.where(self.prob >= _70_cut)[0]
        indices_of_90 = np.where(self.prob >= _90_cut)[0]
        indices_of_95 = np.where(self.prob >= _95_cut)[0]
        indices_of_99 = np.where(self.prob >= _99_cut)[0]

        self.indices_of_50 = indices_of_50
        self.indices_of_70 = indices_of_70
        self.indices_of_90 = indices_of_90
        self.indices_of_95 = indices_of_95
        self.indices_of_99 = indices_of_99

        self.levels = lvls
        self.pixels_90 = [
            Pixel_Element(i90, self.nside, self.prob[i90])
            for i90 in self.indices_of_90
        ]

        self.indices_of_custom = None

        # Custom percentile
        _custom_cut = None
        index_custom = None
        indices_of_custom = None
        if self.custom_percentile is not None:
            _custom_cut, index_custom = self.get_prob_val_and_index(
                sorted_prob, self.custom_percentile)
            indices_of_custom = np.where(self.prob >= _custom_cut)[0]
            self.indices_of_custom = indices_of_custom
            self.pixels_custom = [
                Pixel_Element(ic, self.nside, self.prob[ic])
                for ic in self.indices_of_custom
            ]

        if compute_contours:
            print("Computing contours for '%s'...\n" % self.file_name)
            self.compute_contours()

        t2 = time.time()

        print("\n********* start DEBUG ***********")
        print("`Unpacked_Healpix.initialize` execution time: %s" % (t2 - t1))
        print("********* end DEBUG ***********\n")
コード例 #5
0
    def main(self):

        detector_mapping = {
            "s": "SWOPE",
            "t": "THACHER",
            "n": "NICKEL",
            "k": "KAIT"
        }

        band_mapping = {
            "g": "SDSS g",
            "r": "SDSS r",
            "i": "SDSS i",
            "Clear": "Clear"
        }

        # Valid prob types
        _4D = "4D"
        _2D = "2D"

        is_error = False

        # Parameter checks
        if self.options.gw_id == "":
            is_error = True
            print("GWID is required.")

        formatted_healpix_dir = self.options.healpix_dir
        if "{GWID}" in formatted_healpix_dir:
            formatted_healpix_dir = formatted_healpix_dir.replace(
                "{GWID}", self.options.gw_id)

        hpx_path = "%s/%s" % (formatted_healpix_dir, self.options.healpix_file)
        tile_file_path = "%s/%s" % (formatted_healpix_dir,
                                    self.options.tile_file)
        galaxies_file_path = "%s/%s" % (formatted_healpix_dir,
                                        self.options.galaxies_file)

        if self.options.healpix_file == "":
            is_error = True
            print("You must specify which healpix file to process.")

        if self.options.band not in band_mapping:
            is_error = True
            print("Invalid band selection. Available bands: %s" %
                  band_mapping.keys())

        if self.options.tele not in detector_mapping:
            is_error = True
            print("Invalid telescope selection. Available telescopes: %s" %
                  detector_mapping.keys())

        if not self.options.get_tiles_from_db:
            if not os.path.exists(tile_file_path):
                is_error = True
                print("You must specify which tile file to plot.")

        plotGalaxies = False
        if os.path.exists(galaxies_file_path):
            plotGalaxies = True

        if self.options.extinct <= 0.0:
            is_error = True
            print("Extinction must be a valid float > 0.0")

        if not (self.options.prob_type == _4D
                or self.options.prob_type == _2D):
            is_error = True
            print("Prob type must either be `4D` or `2D`")

        if self.options.cum_prob_outer > 0.95 or \
                self.options.cum_prob_outer < 0.20 or \
                self.options.cum_prob_outer < self.options.cum_prob_inner:
            is_error = True
            print(
                "Cum prob outer must be between 0.2 and 0.95 and > Cum prob inner"
            )

        if self.options.cum_prob_inner > 0.95 or \
                self.options.cum_prob_inner < 0.20 or \
                self.options.cum_prob_inner > self.options.cum_prob_outer:
            is_error = True
            print(
                "Cum prob inner must be between 0.2 and 0.95 and < Cum prob outer"
            )

        if is_error:
            print("Exiting...")
            return 1

        # Get Map ID
        healpix_map_select = "SELECT id, RescaledNSIDE FROM HealpixMap WHERE GWID = '%s' and Filename = '%s'"
        healpix_map_result = query_db([
            healpix_map_select %
            (self.options.gw_id, self.options.healpix_file)
        ])[0][0]
        healpix_map_id = int(healpix_map_result[0])
        healpix_map_nside = int(healpix_map_result[1])

        band_name = band_mapping[self.options.band]
        band_select = "SELECT id, Name, F99_Coefficient FROM Band WHERE `Name`='%s'"
        band_result = query_db([band_select % band_name])[0][0]
        band_id = band_result[0]
        band_F99 = float(band_result[2])

        telescope_name = detector_mapping[self.options.tele]
        detector_select_by_name = "SELECT id, Name, Deg_width, Deg_height, Deg_radius, Area, MinDec, MaxDec FROM Detector WHERE Name='%s'"
        detector_result = query_db([detector_select_by_name % telescope_name
                                    ])[0][0]
        detector_id = int(detector_result[0])
        detector = Detector(detector_result[1],
                            float(detector_result[2]),
                            float(detector_result[3]),
                            detector_id=detector_id)

        galaxies_to_plot = []
        gal_ra = []
        gal_dec = []
        if plotGalaxies:
            with open('%s' % galaxies_file_path, 'r') as csvfile:
                csvreader = csv.reader(csvfile,
                                       delimiter=',',
                                       skipinitialspace=True)
                # Skip Header
                next(csvreader)

                for row in csvreader:
                    gal_ra.append(row[1])
                    gal_dec.append(row[2])

                galaxies_to_plot = coord.SkyCoord(gal_ra,
                                                  gal_dec,
                                                  unit=(u.hour, u.deg))

        tiles_to_plot = []
        if not self.options.get_tiles_from_db:
            # Load tile_file
            with open('%s' % tile_file_path, 'r') as csvfile:
                csvreader = csv.reader(csvfile,
                                       delimiter=',',
                                       skipinitialspace=True)
                # Skip Header
                next(csvreader)

                for row in csvreader:
                    name = row[0]
                    c = coord.SkyCoord(row[1], row[2], unit=(u.hour, u.deg))
                    t = Tile(c.ra.degree, c.dec.degree, detector.deg_width,
                             detector.deg_height, healpix_map_nside)
                    t.field_name = name
                    t.net_prob = float(row[6])
                    tiles_to_plot.append(t)
        else:
            select_tiles_per_map = '''
                SELECT 
                    ot.FieldName, 
                    ot.RA, 
                    ot._Dec, 
                    ot.MJD, 
                    ot.Exp_Time, 
                    ot.Mag_Lim, 
                    d.`Name` as DetectorName, 
                    d.Deg_width, 
                    d.Deg_height
                FROM ObservedTile ot
                JOIN Detector d on d.id = ot.Detector_id
                WHERE ot.HealpixMap_id = %s;
            '''

            map_tiles = query_db([select_tiles_per_map % healpix_map_id])[0]
            print("Map tiles: %s" % len(map_tiles))
            for mt in map_tiles:
                c = coord.SkyCoord(mt[1], mt[2], unit=(u.deg, u.deg))
                t = Tile(c.ra.degree, c.dec.degree, float(mt[7]), float(mt[8]),
                         healpix_map_nside)
                t.field_name = mt[6]
                tiles_to_plot.append(t)

        tile_colors = {
            'KAIT': "seagreen",
            'NICKEL': "mediumpurple",
            'SWOPE': "red",
            'THACHER': "darkorange"
        }

        select_2D_pix = '''
            SELECT 
                running_prob.id, 
                running_prob.HealpixMap_id, 
                running_prob.Pixel_Index, 
                running_prob.Prob, 
                running_prob.Distmu, 
                running_prob.Distsigma, 
                running_prob.Mean, 
                running_prob.Stddev, 
                running_prob.Norm, 
                running_prob.N128_SkyPixel_id, 
                running_prob.cum_prob 
            FROM 
                (SELECT 
                    hp_prob.id, 
                    hp_prob.HealpixMap_id, 
                    hp_prob.Pixel_Index,
                    hp_prob.Prob, 
                    hp_prob.Distmu, 
                    hp_prob.Distsigma, 
                    hp_prob.Mean, 
                    hp_prob.Stddev, 
                    hp_prob.Norm, 
                    hp_prob.N128_SkyPixel_id, 
                    SUM(hp_prob.Prob) OVER(ORDER BY hp_prob.Prob DESC) AS cum_prob 
                FROM 
                    (SELECT 
                        hp.id, 
                        hp.HealpixMap_id, 
                        hp.Pixel_Index,
                        hp.Prob, 
                        hp.Distmu, 
                        hp.Distsigma, 
                        hp.Mean, 
                        hp.Stddev, 
                        hp.Norm, 
                        hp.N128_SkyPixel_id 
                    FROM HealpixPixel hp 
                    WHERE hp.HealpixMap_id = %s 
                    ORDER BY
                        hp.Prob DESC) hp_prob
                    GROUP BY
                        hp_prob.id, 
                        hp_prob.HealpixMap_id, 
                        hp_prob.Pixel_Index,
                        hp_prob.Prob, 
                        hp_prob.Distmu, 
                        hp_prob.Distsigma, 
                        hp_prob.Mean, 
                        hp_prob.Stddev, 
                        hp_prob.Norm, 
                        hp_prob.N128_SkyPixel_id 
                    ) running_prob 
            WHERE 
                running_prob.cum_prob <= %s
        '''

        select_4D_pix = '''
            SELECT 
                running_prob.id, 
                running_prob.HealpixMap_id, 
                running_prob.Pixel_Index, 
                running_prob.Prob, 
                running_prob.NetPixelProb, 
                running_prob.Distmu, 
                running_prob.Distsigma, 
                running_prob.Mean, 
                running_prob.Stddev, 
                running_prob.Norm, 
                running_prob.N128_SkyPixel_id, 
                running_prob.cum_net_pixel_prob 
            FROM 
                (SELECT 
                    hp_prob.id, 
                    hp_prob.HealpixMap_id, 
                    hp_prob.Pixel_Index,
                    hp_prob.Prob, 
                    hp_prob.NetPixelProb, 
                    hp_prob.Distmu, 
                    hp_prob.Distsigma, 
                    hp_prob.Mean, 
                    hp_prob.Stddev, 
                    hp_prob.Norm, 
                    hp_prob.N128_SkyPixel_id, 
                    SUM(hp_prob.NetPixelProb) OVER(ORDER BY hp_prob.NetPixelProb DESC) AS cum_net_pixel_prob 
                FROM 
                    (SELECT 
                        hp.id, 
                        hp.HealpixMap_id, 
                        hp.Pixel_Index,
                        hp.Prob, 
                        hpc.NetPixelProb, 
                        hp.Distmu, 
                        hp.Distsigma, 
                        hp.Mean, 
                        hp.Stddev, 
                        hp.Norm, 
                        hp.N128_SkyPixel_id 
                    FROM HealpixPixel hp 
                    JOIN HealpixPixel_Completeness hpc on hpc.HealpixPixel_id = hp.id
                    WHERE hp.HealpixMap_id = %s 
                    ORDER BY
                        hpc.NetPixelProb DESC) hp_prob 
                    GROUP BY
                        hp_prob.id, 
                        hp_prob.HealpixMap_id, 
                        hp_prob.Pixel_Index,
                        hp_prob.Prob, 
                        hp_prob.NetPixelProb, 
                        hp_prob.Distmu, 
                        hp_prob.Distsigma, 
                        hp_prob.Mean, 
                        hp_prob.Stddev, 
                        hp_prob.Norm, 
                        hp_prob.N128_SkyPixel_id 
                    ) running_prob 
            WHERE 
                running_prob.cum_net_pixel_prob <= %s 
        '''

        select_mwe_pix = '''
            SELECT sp.Pixel_Index
            FROM SkyPixel sp
            WHERE sp.id IN
            (
                SELECT sp.Parent_Pixel_id
                FROM SkyPixel sp
                WHERE sp.id IN
                (
                    SELECT sp.Parent_Pixel_id
                    FROM SkyPixel sp
                    JOIN SkyPixel_EBV sp_ebv ON sp_ebv.N128_SkyPixel_id = sp.id
                    WHERE sp_ebv.EBV*%s > %s and NSIDE = 128
                ) and NSIDE = 64
            ) and NSIDE = 32
        '''

        print("Selecting map pixels...")
        pixels_to_select = ""
        if self.options.prob_type == _2D:
            pixels_to_select = select_2D_pix % (healpix_map_id,
                                                self.options.cum_prob_outer)
        else:
            pixels_to_select = select_4D_pix % (healpix_map_id,
                                                self.options.cum_prob_outer)

        map_pix_result = query_db([pixels_to_select])[0]

        mwe_pix_result = query_db(
            [select_mwe_pix % (band_F99, self.options.extinct)])[0]
        print("...done")

        print("Building pixel elements...")
        map_pix = [
            Pixel_Element(int(mp[2]),
                          healpix_map_nside,
                          float(mp[3]),
                          pixel_id=int(mp[0])) for mp in map_pix_result
        ]
        map_pix_sorted = sorted(map_pix, key=lambda p: p.prob, reverse=True)

        mwe_pix = [Pixel_Element(int(mp[0]), 32, 0.0) for mp in mwe_pix_result]
        print("...done")

        cutoff_inner = self.options.cum_prob_inner
        cutoff_outer = self.options.cum_prob_outer
        index_inner = 0
        index_outer = 0

        print("Find index for inner contour...")
        cum_prob = 0.0
        for i in range(len(map_pix_sorted)):
            cum_prob += map_pix_sorted[i].prob
            index_inner = i
            if cum_prob >= cutoff_inner:
                break
        print("... %s" % index_inner)

        print("Find index for outer contour...")
        cum_prob = 0.0
        for i in range(len(map_pix_sorted)):
            cum_prob += map_pix_sorted[i].prob
            index_outer = i
            if cum_prob >= cutoff_outer:
                break
        print("... %s" % index_outer)

        print("Build multipolygons...")
        net_inner_polygon = []
        for p in map_pix_sorted[0:index_inner]:
            net_inner_polygon += p.query_polygon
        joined_inner_poly = unary_union(net_inner_polygon)

        # Fix any seams
        eps = 0.00001
        merged_inner_poly = []
        smoothed_inner_poly = joined_inner_poly.buffer(
            eps, 1,
            join_style=JOIN_STYLE.mitre).buffer(-eps,
                                                1,
                                                join_style=JOIN_STYLE.mitre)

        try:
            test_iter = iter(smoothed_inner_poly)
            merged_inner_poly = smoothed_inner_poly
        except TypeError as te:
            merged_inner_poly.append(smoothed_inner_poly)

        print("Number of sub-polygons in `merged_inner_poly`: %s" %
              len(merged_inner_poly))
        sql_inner_poly = SQL_Polygon(merged_inner_poly, detector)

        net_outer_polygon = []
        for p in map_pix_sorted[0:index_outer]:
            net_outer_polygon += p.query_polygon
        joined_outer_poly = unary_union(net_outer_polygon)

        # Fix any seams
        merged_outer_poly = []
        smoothed_outer_poly = joined_outer_poly.buffer(
            eps, 1,
            join_style=JOIN_STYLE.mitre).buffer(-eps,
                                                1,
                                                join_style=JOIN_STYLE.mitre)

        try:
            test_iter = iter(smoothed_outer_poly)
            merged_outer_poly = smoothed_outer_poly
        except TypeError as te:
            merged_outer_poly.append(smoothed_outer_poly)

        print("Number of sub-polygons in `merged_outer_poly`: %s" %
              len(merged_outer_poly))
        sql_outer_poly = SQL_Polygon(merged_outer_poly, detector)
        print("... done.")

        # Plot!!
        fig = plt.figure(figsize=(10, 10), dpi=800)
        ax = fig.add_subplot(111)
        m = Basemap(projection='moll', lon_0=180.0)

        sql_inner_poly.plot(m,
                            ax,
                            edgecolor='black',
                            linewidth=0.5,
                            facecolor='None')
        sql_outer_poly.plot(m,
                            ax,
                            edgecolor='gray',
                            linewidth=0.5,
                            facecolor='None')

        for p in mwe_pix:
            p.plot(m,
                   ax,
                   edgecolor='None',
                   linewidth=0.5,
                   facecolor='cornflowerblue',
                   alpha=0.5)

        print("Plotting (%s) Tiles..." % len(tiles_to_plot))
        if not self.options.get_tiles_from_db:
            for i, t in enumerate(tiles_to_plot):
                t.plot(m,
                       ax,
                       edgecolor='r',
                       facecolor='None',
                       linewidth=0.25,
                       alpha=1.0,
                       zorder=9900)
        else:
            for i, t in enumerate(tiles_to_plot):
                t.plot(m,
                       ax,
                       edgecolor=tile_colors[t.field_name],
                       facecolor='None',
                       linewidth=0.25,
                       alpha=1.0,
                       zorder=9900)

        print("Plotting (%s) Galaxies..." % len(galaxies_to_plot))
        if plotGalaxies:
            for g in galaxies_to_plot:
                x, y = m(g.ra.degree, g.dec.degree)
                m.plot(x,
                       y,
                       'ko',
                       markersize=0.2,
                       linewidth=0.25,
                       alpha=0.3,
                       zorder=9900)

        # # # -------------- Use this for mollweide projections --------------
        meridians = np.arange(0., 360., 60.)
        m.drawparallels(np.arange(-90., 91., 30.),
                        fontsize=14,
                        labels=[True, True, False, False],
                        dashes=[2, 2],
                        linewidth=0.5)  # , xoffset=2500000
        m.drawmeridians(meridians,
                        labels=[False, False, False, False],
                        dashes=[2, 2],
                        linewidth=0.5)
        # # # ----------------------------------------------------------------

        ax.invert_xaxis()

        plt.ylabel(r'$\mathrm{Declination}$', fontsize=16, labelpad=36)
        plt.xlabel(r'$\mathrm{Right\;Ascension}$', fontsize=16, labelpad=30)

        output_path = "%s/%s" % (formatted_healpix_dir,
                                 self.options.tile_file.replace(
                                     ".csv", ".png"))
        fig.savefig(output_path, bbox_inches='tight')  # ,dpi=840
        plt.close('all')
        print("... Done.")
コード例 #6
0
    print("\tLoading existing pixels...")
    with open('sky_pixels.pkl', 'rb') as handle:
        sky_pixels = pickle.load(handle)
else:
    ## Build Sky Pixel Objects ##
    print("\n\nBuilding Sky Pixels...")
    sky_pixels = OrderedDict(
    )  # Ordered Dict because it will matter the order once we start iterating during the INSERT

    for i, nside in enumerate(nsides):

        print("Initializing NSIDE=%s pix..." % nside)
        sky_pixels[nside] = {}

        for j in range(nside_npix[i]):
            pe = Pixel_Element(j, nside, 0.0)
            pe.query_polygon_string  # initialize
            sky_pixels[nside][j] = pe

        if i > 0:
            for j, (pi, pe) in enumerate(sky_pixels[nside].items()):
                # Get Parent Pixel
                theta, phi = hp.pix2ang(pe.nside, pe.index)
                parent_index = hp.ang2pix(nsides[i - 1], theta, phi)

                if not parent_index in sky_pixels[nsides[i - 1]]:
                    raise (
                        "Orphaned NSIDE=%s pixel! Orphaned index, theta, phi: (%s, %s, %s)"
                        % (nside, pe.index, theta, phi))
                else:
                    parent_pixel = sky_pixels[nsides[i - 1]][parent_index]
コード例 #7
0
    def main(self):

        hpx_path = "%s/%s" % (self.options.healpix_dir, self.options.healpix_file)

        # If you specify a telescope that's not in the default list, you must provide the rest of the information
        detector = None
        is_error = False
        is_custom_percentile = False
        custom_percentile = None

        # Check the inputs for errors...
        if self.options.telescope_abbreviation not in self.telescope_mapping.keys():
            print("Running for custom telescope. Checking required parameters...")

            if self.options.telescope_abbreviation == "":
                is_error = True
                print("For custom telescope, `telescope_abbreviation` is required!")

            if self.options.telescope_name == "":
                is_error = True
                print("For custom telescope, `telescope_name` is required!")

            if self.options.detector_width_deg <= 0.0:
                is_error = True
                print("For custom telescope, `detector_width_deg` is required, and must be > 0!")

            if self.options.detector_height_deg <= 0.0:
                is_error = True
                print("For custom telescope, `detector_height_deg` is required, and must be > 0!")

            if not is_error:
                detector = Detector(self.options.telescope_name, self.options.detector_width_deg,
                                    self.options.detector_height_deg)
        else:
            detector = self.telescope_mapping[self.options.telescope_abbreviation]

        if self.options.percentile > 0.9:
            is_error = True
            print("User-defined percentile must be <= 0.90")
        elif self.options.percentile > 0.0:
            is_custom_percentile = True
            custom_percentile = self.options.percentile

        if is_error:
            print("Exiting...")
            return 1

        print("\n\nTelescope: `%s -- %s`, width: %s [deg]; height %s [deg]" % (self.options.telescope_abbreviation,
                                                                               detector.name, detector.deg_width,
                                                                               detector.deg_height))
        fov_area = (detector.deg_width * detector.deg_height)
        print("%s FOV area: %s" % (detector.name, fov_area))

        print("Loading base cartography...")
        base_cartography = None
        with open('%s/%s_base_cartography.pkl' % (self.options.working_dir, self.options.gw_id), 'rb') as handle:
            base_cartography = pickle.load(handle)

        # print("Loading sql pixel map...")
        # sql_pixel_map = None
        # with open('%s/%s_sql_pixel_map.pkl' % (self.options.working_dir, self.options.gw_id), 'rb') as handle:
        # 	sql_pixel_map = pickle.load(handle)

        # print("Loading sql cartography...")
        # sql_tile_cartography = None
        # with open('%s/%s_sql_cartography.pkl' % (self.options.working_dir, self.options.gw_id), 'rb') as handle:
        # 	sql_tile_cartography = pickle.load(handle)

        # print("Loading galaxy query...")
        # query = None
        # with open('%s/%s_query.pkl' % (self.options.working_dir, self.options.gw_id), 'rb') as handle:
        # 	query = pickle.load(handle)

        if not self.options.skip_completeness:
            print("Loading sql multipolygon...")
            sql_poly = None
            with open('%s/%s_sql_poly.pkl' % (self.options.working_dir, self.options.gw_id), 'rb') as handle:
                sql_poly = pickle.load(handle)

            # print("Loading contained galaxies...")
            # contained_galaxies = None
            # with open('%s/%s_contained_galaxies.pkl' % (self.options.working_dir, self.options.gw_id), 'rb') as handle:
            # 	contained_galaxies = pickle.load(handle)

            # for g in (sorted(contained_galaxies, key=lambda x: x.relative_prob, reverse=True))[:99]:
            # 	print(g.relative_prob)

            print("Loading redistributed cartography...")
            redistributed_cartography = None
            with open('%s/%s_redstributed_cartography.pkl' % (self.options.working_dir, self.options.gw_id),
                      'rb') as handle:
                redistributed_cartography = pickle.load(handle)

        # print("Loading observed tiles file...")
        # observed_tiles = {
        # 	"S190728q":{"Thacher":{"ut190728":[]},
        # 				"Swope":{"ut190728":[]}
        # 			   }
        # }

        # running_prob = 0
        # unique_pixels = []
        # # Thacher
        # thacher_width = 2048*0.609/3600. #arcseconds -> deg
        # thacher_height = 2048*0.609/3600.
        # with open('%s/ut190727_28_Thacher_observed.txt' % self.options.working_dir,'r') as csvfile:
        # 	csvreader = csv.reader(csvfile, delimiter=',',skipinitialspace=True)

        # 	for row in csvreader:
        # 		name = row[0]
        # 		t = Tile(coord.SkyCoord(row[1], row[2], unit=(u.hour, u.deg)), thacher_width, thacher_height, redistributed_cartography.unpacked_healpix.nside)
        # 		t.field_name = name

        # 		t_prob = t.enclosed_pixel_indices
        # 		for ti in t_prob:
        # 			if ti not in unique_pixels:
        # 				unique_pixels.append(ti)

        # 		t.net_prob = np.sum(redistributed_cartography.unpacked_healpix.prob[t_prob])
        # 		running_prob += t.net_prob
        # 		print("%s - net prob: %s" % (name, t.net_prob))
        # 		observed_tiles["S190728q"]["Thacher"]["ut190728"].append(t)

        # print("\n")
        # swope_width = 4096*0.435/3600. #arcseconds -> deg
        # swope_height = 4112*0.435/3600.
        # with open('%s/ut190727_28_Swope_observed.txt' % self.options.working_dir,'r') as csvfile:
        # 	csvreader = csv.reader(csvfile, delimiter=',',skipinitialspace=True)

        # 	for row in csvreader:
        # 		name = row[0]
        # 		t = Tile(coord.SkyCoord(row[1], row[2], unit=(u.hour, u.deg)), swope_width, swope_height, redistributed_cartography.unpacked_healpix.nside)
        # 		t.field_name = name

        # 		t_prob = t.enclosed_pixel_indices

        # 		for ti in t_prob:
        # 			if ti not in unique_pixels:
        # 				unique_pixels.append(ti)

        # 		t.net_prob = np.sum(redistributed_cartography.unpacked_healpix.prob[t_prob])
        # 		running_prob += t.net_prob
        # 		print("%s - net prob: %s" % (name, t.net_prob))
        # 		observed_tiles["S190728q"]["Swope"]["ut190728"].append(t)

        # tile_set = [
        # 	("Thacher_0728",observed_tiles["S190728q"]["Thacher"]["ut190728"],('royalblue','None')),
        # 	("Swope_0728",observed_tiles["S190728q"]["Swope"]["ut190728"],('green','None')),
        # ]
        # print("Total non-corrected prob captured by observed Thacher+Swope tiles: %s" % running_prob)
        # print("Total corrected prob captured by observed Thacher+Swope tiles: %s" % np.sum(redistributed_cartography.unpacked_healpix.prob[np.asarray(unique_pixels)]))

        print("\n\nBuilding MWE...")
        config["data_dir"] = "./DataIngestion"

        print("Initializing MWE n128 pix...")
        f99_r = 2.285
        nside128 = 128
        nside128_npix = hp.nside2npix(nside128)
        nside128_pixels = []
        nside128_RA = []
        nside128_DEC = []

        mw_pixels = []
        for i in range(nside128_npix):
            pe = Pixel_Element(i, nside128, 0.0)
            nside128_pixels.append(pe)

            theta, phi = hp.pix2ang(pe.nside, pe.index)
            dec = np.degrees(0.5 * np.pi - theta)
            ra = np.degrees(phi)

            nside128_RA.append(ra)
            nside128_DEC.append(dec)

        c1 = coord.SkyCoord(nside128_RA, nside128_DEC, unit=(u.deg, u.deg))
        sfd = SFDQuery()
        ebv1 = sfd(c1)
        for i, e in enumerate(ebv1):
            if e * f99_r >= 0.5:
                mw_pixels.append(nside128_pixels[i])

        tiles_to_plot = None
        pixels_to_plot = None
        sql_poly_to_plot = None
        if not self.options.skip_completeness:
            tiles_to_plot = redistributed_cartography.tiles
            pixels_to_plot = redistributed_cartography.unpacked_healpix.pixels_90
            sql_poly_to_plot = sql_poly
        else:
            tiles_to_plot = base_cartography.tiles
            pixels_to_plot = base_cartography.unpacked_healpix.pixels_90

        ra_tiles = []
        dec_tiles = []
        for i, t in enumerate(tiles_to_plot):
            field_name = "%s%s%sE%s" % (self.options.telescope_abbreviation,
                                        str(self.options.event_number).zfill(3),
                                        self.options.schedule_designation,
                                        str(i + 1).zfill(5))

            t.field_name = field_name
            # ra_tiles.append(t.coord.ra.degree)
            # dec_tiles.append(t.coord.dec.degree)
            ra_tiles.append(t.ra_deg)
            dec_tiles.append(t.dec_deg)

        c = coord.SkyCoord(ra_tiles, dec_tiles, unit=(u.deg, u.deg))
        sfd = SFDQuery()
        ebv = sfd(c)

        new_tiles = []
        for i, e in enumerate(ebv):
            t = tiles_to_plot[i]
            # if e*f99_r < 0.5 and t.coord.dec.degree > -30.0:
            if e * f99_r < 0.5 and t.dec_deg < 30.0:
                new_tiles.append(t)
        # if t.coord.dec.degree > -30.0:
        # 	t.mwe = e
        # 	new_tiles.append(t)

        print(len(new_tiles))
        prob = 0.0
        for t in new_tiles:
            prob += t.net_prob

        print("Enclosed prob = %s" % prob)

        def GetSexigesimalString(c):
            ra = c.ra.hms
            dec = c.dec.dms

            ra_string = "%02d:%02d:%05.2f" % (ra[0], ra[1], ra[2])
            if dec[0] >= 0:
                dec_string = "+%02d:%02d:%05.2f" % (dec[0], np.abs(dec[1]), np.abs(dec[2]))
            else:
                dec_string = "%03d:%02d:%05.2f" % (dec[0], np.abs(dec[1]), np.abs(dec[2]))

            # Python has a -0.0 object. If the deg is this (because object lies < 60 min south), the string formatter will drop the negative sign
            if c.dec < 0.0 and dec[0] == 0.0:
                dec_string = "-00:%02d:%05.2f" % (np.abs(dec[1]), np.abs(dec[2]))
            return (ra_string, dec_string)

        with open('%s/%s_%s_MWE_below_30_AA.txt' % (self.options.working_dir, self.options.gw_id, detector.name),
                  'w') as csvfile:

            csvwriter = csv.writer(csvfile)

            cols = []
            cols.append('# FieldName')
            cols.append('FieldRA')
            cols.append('FieldDec')
            cols.append('Telscope')
            cols.append('Filter')
            cols.append('ExpTime')
            cols.append('Priority')
            cols.append('Status')
            # cols.append('A_R')
            csvwriter.writerow(cols)

            for i, st in enumerate(new_tiles):
                # coord_str = GetSexigesimalString(st.coord)
                c = coord.SkyCoord(st.ra_deg, st.dec_deg, unit=(u.deg, u.deg))
                coord_str = GetSexigesimalString(c)

                cols = []

                cols.append(st.field_name)
                cols.append(coord_str[0])
                cols.append(coord_str[1])
                cols.append(detector.name)
                cols.append(self.options.filter)
                cols.append(self.options.exp_time)
                cols.append(st.net_prob)
                cols.append('False')
                # cols.append(st.mwe)
                csvwriter.writerow(cols)

            print("Done")

        # return 0;
        # raise("Stop!")

        # swope_width = 4096*0.435/3600. #arcseconds -> deg
        # swope_height = 4112*0.435/3600.

        # thacher_width = 2048*0.609/3600. #arcseconds -> deg
        # thacher_height = 2048*0.609/3600.

        # nickel_width = 2048*0.368/3600. #arcseconds -> deg
        # nickel_height = 2048*0.368/3600.

        # nickel_width = 2048*0.368/3600. #arcseconds -> deg
        # nickel_height = 2048*0.368/3600.

        # andicam_ccd_width = 1024*0.371/3600. #arcseconds -> deg
        # andicam_ccd_height = 1024*0.371/3600.

        # swift_uvot_width = 2048*0.502/3600. #arcseconds -> deg
        # swift_uvot_height = 2048*0.502/3600.

        # saguaro_width = 2.26 #deg
        # saguaro_height = 2.26

        # observed_tiles = {
        # 	"S190425z":{"Swope":{"ut190425":[], "ut190428":[], "ut190429":[]},
        # 				"Thacher":{"ut190425":[]},
        # 				"ANDICAM":{"ut190425":[], "ut190428":[]},
        # 				"Nickel":{"ut190425":[], "ut190429":[]},
        # 				"Swift":{"ut190425":[]},
        # 				"SAGUARO":{"ut190425":[]}
        # 			   }
        # }

        # # Just do S190425z for now...
        # # SWOPE
        # with open('../O3_Alerts/GW190408/GW190425_2/S190425z_ut190425_Swope_Tiles_Observed.csv','r') as csvfile:

        # 	csvreader = csv.reader(csvfile, delimiter=' ',skipinitialspace=True)
        # 	next(csvreader)

        # 	for row in csvreader:
        # 		t = Tile(coord.SkyCoord(row[1], row[2], unit=(u.hour, u.deg)), swope_width, swope_height, redistributed_cartography.unpacked_healpix.nside)
        # 		(observed_tiles["S190425z"]["Swope"]["ut190425"]).append(t)

        # with open('../O3_Alerts/GW190408/GW190425_2/S190425z_ut190428_Swope_Tiles_Observed.csv','r') as csvfile:

        # 	csvreader = csv.reader(csvfile, delimiter=' ',skipinitialspace=True)
        # 	next(csvreader)

        # 	for row in csvreader:
        # 		t = Tile(coord.SkyCoord(row[1], row[2], unit=(u.hour, u.deg)), swope_width, swope_height, redistributed_cartography.unpacked_healpix.nside)
        # 		(observed_tiles["S190425z"]["Swope"]["ut190428"]).append(t)

        # with open('../O3_Alerts/GW190408/GW190425_2/S190425z_ut190429_Swope_Tiles_Observed.csv','r') as csvfile:

        # 	csvreader = csv.reader(csvfile, delimiter=' ',skipinitialspace=True)
        # 	next(csvreader)

        # 	for row in csvreader:
        # 		t = Tile(coord.SkyCoord(row[1], row[2], unit=(u.hour, u.deg)), swope_width, swope_height, redistributed_cartography.unpacked_healpix.nside)
        # 		(observed_tiles["S190425z"]["Swope"]["ut190429"]).append(t)

        # # THACHER
        # with open('../O3_Alerts/GW190408/GW190425_2/S190425z_ut190425_Thacher_Tiles_Observed.csv','r') as csvfile:

        # 	csvreader = csv.reader(csvfile, delimiter=' ',skipinitialspace=True)
        # 	next(csvreader)

        # 	for row in csvreader:
        # 		t = Tile(coord.SkyCoord(row[1], row[2], unit=(u.hour, u.deg)), thacher_width, thacher_height, redistributed_cartography.unpacked_healpix.nside)
        # 		(observed_tiles["S190425z"]["Thacher"]["ut190425"]).append(t)

        # # ANDICAM
        # with open('../O3_Alerts/GW190408/GW190425_2/S190425z_ut190425_ANDICAM_Tiles_Observed.csv','r') as csvfile:

        # 	csvreader = csv.reader(csvfile, delimiter=' ',skipinitialspace=True)
        # 	next(csvreader)

        # 	for row in csvreader:
        # 		t = Tile(coord.SkyCoord(row[1], row[2], unit=(u.hour, u.deg)), andicam_ccd_width, andicam_ccd_height, redistributed_cartography.unpacked_healpix.nside)
        # 		(observed_tiles["S190425z"]["ANDICAM"]["ut190425"]).append(t)

        # with open('../O3_Alerts/GW190408/GW190425_2/S190425z_ut190428_ANDICAM_Tiles_Observed.csv','r') as csvfile:

        # 	csvreader = csv.reader(csvfile, delimiter=' ',skipinitialspace=True)
        # 	next(csvreader)

        # 	for row in csvreader:
        # 		t = Tile(coord.SkyCoord(row[1], row[2], unit=(u.hour, u.deg)), andicam_ccd_width, andicam_ccd_height, redistributed_cartography.unpacked_healpix.nside)
        # 		(observed_tiles["S190425z"]["ANDICAM"]["ut190428"]).append(t)

        # # NICKEL
        # with open('../O3_Alerts/GW190408/GW190425_2/S190425z_ut190425_Nickel_Tiles_Observed.csv','r') as csvfile:

        # 	csvreader = csv.reader(csvfile, delimiter=' ',skipinitialspace=True)
        # 	next(csvreader)

        # 	for row in csvreader:
        # 		t = Tile(coord.SkyCoord(row[1], row[2], unit=(u.hour, u.deg)), nickel_width, nickel_height, redistributed_cartography.unpacked_healpix.nside)
        # 		(observed_tiles["S190425z"]["Nickel"]["ut190425"]).append(t)

        # with open('../O3_Alerts/GW190408/GW190425_2/S190425z_ut190429_Nickel_Tiles_Observed.csv','r') as csvfile:

        # 	csvreader = csv.reader(csvfile, delimiter=' ',skipinitialspace=True)
        # 	next(csvreader)

        # 	for row in csvreader:
        # 		t = Tile(coord.SkyCoord(row[1], row[2], unit=(u.hour, u.deg)), nickel_width, nickel_height, redistributed_cartography.unpacked_healpix.nside)
        # 		(observed_tiles["S190425z"]["Nickel"]["ut190429"]).append(t)

        # # Swift
        # with open('../O3_Alerts/GW190408/GW190425_2/S190425z_ut190425_Swift_Tiles_Observed.csv','r') as csvfile:

        # 	csvreader = csv.reader(csvfile, delimiter=' ',skipinitialspace=True)
        # 	next(csvreader)

        # 	for row in csvreader:
        # 		t = Tile(coord.SkyCoord(row[1], row[2], unit=(u.deg, u.deg)), swift_uvot_width, swift_uvot_height, redistributed_cartography.unpacked_healpix.nside)
        # 		(observed_tiles["S190425z"]["Swift"]["ut190425"]).append(t)

        # for k1,v1 in observed_tiles.items():
        # 	print("%s" % k1)

        # 	for k2,v2 in observed_tiles[k1].items():
        # 		print("\t%s" % k2)

        # 		for k3,v3 in observed_tiles[k1][k2].items():
        # 			print("\t\t%s - # of tiles: %s" % (k3, len(observed_tiles[k1][k2][k3])))

        # 	print("\n")

        # tile_set = [
        # 	("Swope_0425",observed_tiles["S190425z"]["Swope"]["ut190425"],('r','None')),
        # 	("Swope_0428",observed_tiles["S190425z"]["Swope"]["ut190428"],('r','None')),
        # 	("Swope_0429",observed_tiles["S190425z"]["Swope"]["ut190429"],('r','None')),

        # 	("Thacher_0425",observed_tiles["S190425z"]["Thacher"]["ut190425"],('royalblue','None')),

        # 	("ANDICAM_0425",observed_tiles["S190425z"]["ANDICAM"]["ut190425"],('forestgreen','None')),
        # 	("ANDICAM_0428",observed_tiles["S190425z"]["ANDICAM"]["ut190428"],('forestgreen','None')),

        # 	("Nickel_0425",observed_tiles["S190425z"]["Nickel"]["ut190425"],('mediumorchid','None')),
        # 	("Nickel_0429",observed_tiles["S190425z"]["Nickel"]["ut190429"],('mediumorchid','None')),

        # 	("Swift_0425",observed_tiles["S190425z"]["Swift"]["ut190425"],('k','None'))
        # ]

        # # In case you want to plot contours...
        # print("Computing contours for '%s'...\n" % base_cartography.unpacked_healpix.file_name)
        # base_cartography.unpacked_healpix.compute_contours()

        # print("Computing contours for '%s'...\n" % redistributed_cartography.unpacked_healpix.file_name)
        # redistributed_cartography.unpacked_healpix.compute_contours()

        # # Thacher
        # thacher_tiles = []
        # with open('%s/S190521g_AA_THACHER_Tiles.txt' % self.options.working_dir,'r') as csvfile:

        #     csvreader = csv.reader(csvfile, delimiter=',')
        #     next(csvreader)

        #     for row in csvreader:
        #         t = Tile(coord.SkyCoord(row[1], row[2], unit=(u.hour, u.deg)), detector.deg_width, detector.deg_height,
        #                  redistributed_cartography.unpacked_healpix.nside)
        #         t.field_name = row[0]
        #         t.net_prob = np.sum(redistributed_cartography.unpacked_healpix.prob[t.enclosed_pixel_indices])

        #       		print("Tile #: %s; Net prob: %0.4f" % (t.field_name, t.net_prob))

        #         thacher_tiles.append(t)

        # above_30 = []
        # for t in thacher_tiles:
        # 	if t.coord.dec.degree > -30.0:
        # 		above_30.append(t)

        # sorted_tiles = sorted(above_30, key=lambda t: t.net_prob, reverse=True)
        # print("Number of northern tiles: %s" % len(sorted_tiles))

        # top_200 = sorted_tiles[0:199]
        # top_200_prob = np.sum([t.net_prob for t in top_200])
        # print("Prob of northern, top 200: %s" % top_200_prob)

        # top_200_area = fov_area*200
        # print("Area of northern, top 200: %s" % top_200_area)

        # with open('%s/S190521g_AA_THACHER_Tiles_Northern_200.txt' % self.options.working_dir,'w') as csvfile:

        # 	csvwriter = csv.writer(csvfile)

        # 	cols = []
        # 	cols.append('# FieldName')
        # 	cols.append('FieldRA')
        # 	cols.append('FieldDec')
        # 	cols.append('Telscope')
        # 	cols.append('Filter')
        # 	cols.append('ExpTime')
        # 	cols.append('Priority')
        # 	cols.append('Status')
        # 	csvwriter.writerow(cols)

        # 	for i, st in enumerate(top_200):

        # 		coord_str = GetSexigesimalString(st.coord)

        # 		cols = []

        # 		cols.append(st.field_name)
        # 		cols.append(coord_str[0])
        # 		cols.append(coord_str[1])
        # 		cols.append(detector.name)
        # 		cols.append(self.options.filter)
        # 		cols.append(self.options.exp_time)
        # 		print(st.net_prob)
        # 		cols.append(st.net_prob)
        # 		cols.append('False')
        # 		csvwriter.writerow(cols)

        # 	print("Done")

        # test = []
        # for t in base_cartography.tiles:
        # 	test.append(t.polygon)

        # # print("here")
        # # print(list(test[0].exterior.coords))
        # # print("\n\n")

        # test2 = unary_union(test)
        # # print(type(test2))

        # eps = 0.00001
        # test3 = test2.buffer(eps, 1, join_style=JOIN_STYLE.mitre).buffer(-eps, 1, join_style=JOIN_STYLE.mitre)

        # plot_probability_map("%s/%s_SQL_Pixels" % (self.options.working_dir, self.options.gw_id),
        # 			 pixels_filled=base_cartography.unpacked_healpix.pixels_90,
        # 			 # tiles=base_cartography.tiles,
        # 			 # pixels_empty=base_cartography.unpacked_healpix.pixels_90,
        # 			 colormap=plt.cm.viridis,
        # 			 linear_rings=test3)

        # pixels_90 = [Pixel_Element(i90, redistributed_cartography.unpacked_healpix.nside,
        # 							redistributed_cartography.unpacked_healpix.prob[i90])
        # 							for i90 in redistributed_cartography.unpacked_healpix.indices_of_90]

        plot_probability_map(
            "%s/%s_%s_Redistributed_90th_Tiles" % (self.options.working_dir, self.options.gw_id, detector.name),
            # pixels_filled=redistributed_cartography.unpacked_healpix.pixels_90,
            pixels_filled=pixels_to_plot,
            # galaxies=contained_galaxies,
            galaxies=mw_pixels,
            tiles=new_tiles,
            # tiles=redistributed_cartography.tiles,
            # tiles=base_cartography.tiles,
            # sql_poly=sql_poly,
            # sql_poly=sql_poly_to_plot,
            # linear_rings=sql_poly.polygon,
            # healpix_obj_for_contours=base_cartography.unpacked_healpix,
            # tile_set=tile_set
            )