|
| 1 | +from irf import estimate_exposure_time, build_exposure_map |
| 2 | +import click |
| 3 | +import fact.io as fio |
| 4 | +import pandas as pd |
| 5 | +from astroquery.skyview import SkyView |
| 6 | +from astropy.wcs import WCS |
| 7 | +from astropy.visualization import ImageNormalize, ZScaleInterval, AsinhStretch |
| 8 | +import astropy.units as u |
| 9 | +from astropy.coordinates import SkyCoord |
| 10 | + |
| 11 | +import matplotlib.pyplot as plt |
| 12 | + |
| 13 | + |
| 14 | +@u.quantity_input(fov=u.deg) |
| 15 | +def get_sdss_sky_image(img_center, fov=9 * u.deg, n_pix=1000): |
| 16 | + ''' |
| 17 | + A small helper method which uses astroquery to get an image from the sdss. |
| 18 | + This requires internet access and fails pretty often due to http timeouts. |
| 19 | + ''' |
| 20 | + hdu = SkyView.get_images( |
| 21 | + position=img_center, |
| 22 | + pixels=n_pix, |
| 23 | + survey=['DSS'], |
| 24 | + width=fov, |
| 25 | + height=fov)[0][0] |
| 26 | + |
| 27 | + img = hdu.data |
| 28 | + wcs = WCS(hdu.header) |
| 29 | + return img, wcs |
| 30 | + |
| 31 | + |
| 32 | +@click.command() |
| 33 | +@click.argument( |
| 34 | + 'dl3_path', |
| 35 | + type=click.Path(file_okay=True, dir_okay=False), |
| 36 | +) |
| 37 | +@click.argument( |
| 38 | + 'output_path', |
| 39 | + type=click.Path(file_okay=True, dir_okay=False, exists=False), |
| 40 | +) |
| 41 | +@click.option( |
| 42 | + '-n', |
| 43 | + '--n_pix', |
| 44 | + default=1000, |
| 45 | + help='number of pixels along the edge of the produced image', |
| 46 | +) |
| 47 | +@click.option( |
| 48 | + '-s', |
| 49 | + '--source_name', |
| 50 | + default=None, |
| 51 | + help= |
| 52 | + 'If supplied, e.g. "Crab Nebula" will draw the position of that source into the image', |
| 53 | +) |
| 54 | +@click.option( |
| 55 | + '--background/--no-background', |
| 56 | + default=True, |
| 57 | + help='If true, downloads SDSS image for backgrund image in the plot') |
| 58 | +def main(dl3_path, output_path, n_pix, source_name, background): |
| 59 | + ''' |
| 60 | + Takes FACT dl3 output and plots a skymap which is being saved to the output_path. |
| 61 | + ''' |
| 62 | + runs = fio.read_data(dl3_path, key='runs') |
| 63 | + dl3 = fio.read_data(dl3_path, key='events') |
| 64 | + |
| 65 | + data = pd.merge(runs, dl3, on=['run_id', 'night']) |
| 66 | + |
| 67 | + timestamps = pd.to_datetime(data.timestamp).values |
| 68 | + total_ontime = estimate_exposure_time(timestamps) |
| 69 | + print('Total estimated exposure time: {}'.format(total_ontime.to(u.h))) |
| 70 | + |
| 71 | + ra_pointing = data.right_ascension.values * u.hourangle |
| 72 | + dec_pointing = data.declination.values * u.deg |
| 73 | + pointing = SkyCoord(ra=ra_pointing, dec=dec_pointing) |
| 74 | + |
| 75 | + img = None |
| 76 | + wcs = None |
| 77 | + if background: |
| 78 | + img_center = SkyCoord(ra=pointing.ra.mean(), dec=pointing.dec.mean()) |
| 79 | + img, wcs = get_sdss_sky_image( |
| 80 | + img_center=img_center, n_pix=n_pix, fov=9 * u.deg) |
| 81 | + |
| 82 | + mask, wcs = build_exposure_map(pointing, timestamps, shape=(n_pix, n_pix)) |
| 83 | + |
| 84 | + ax = plot_exposure(mask, wcs, image=img) |
| 85 | + if source_name: |
| 86 | + source = SkyCoord.from_name('Crab Nebula') |
| 87 | + ax.scatter( |
| 88 | + source.ra.deg, |
| 89 | + source.dec.deg, |
| 90 | + transform=ax.get_transform('icrs'), |
| 91 | + label=source_name, |
| 92 | + s=10**2, |
| 93 | + facecolors='none', |
| 94 | + edgecolors='r', |
| 95 | + ) |
| 96 | + ax.legend() |
| 97 | + |
| 98 | + plt.savefig(output_path, dpi=200) |
| 99 | + |
| 100 | + |
| 101 | +def plot_exposure(mask, wcs, image=None): |
| 102 | + plt.figure(figsize=(10, 10)) |
| 103 | + ax = plt.subplot(projection=wcs) |
| 104 | + |
| 105 | + if image is not None: |
| 106 | + norm = ImageNormalize( |
| 107 | + image, |
| 108 | + interval=ZScaleInterval(contrast=0.05), |
| 109 | + stretch=AsinhStretch(a=0.2)) |
| 110 | + ax.imshow(image, cmap='gray', norm=norm, interpolation='nearest') |
| 111 | + |
| 112 | + d = ax.imshow(mask, alpha=0.7) |
| 113 | + cb = plt.colorbar(d) |
| 114 | + cb.set_label('Live Time / Hours') |
| 115 | + ax.set_xlabel('Galactic Longitude') |
| 116 | + ax.set_ylabel('Galactic Latitude') |
| 117 | + |
| 118 | + return ax |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + main() |
0 commit comments