|
1 | 1 | import astropy.units as u
|
2 | 2 | from astropy.coordinates import Angle, SkyCoord
|
| 3 | +from astropy import wcs |
3 | 4 | from regions import CircleSkyRegion
|
4 |
| -from astroquery.skyview import SkyView |
5 |
| -from astropy.wcs import WCS |
6 |
| -import matplotlib.pyplot as plt |
7 |
| -import fact.io as fio |
8 | 5 | import numpy as np
|
9 |
| -import pandas as pd |
10 | 6 | from scipy.stats import expon
|
11 | 7 |
|
12 | 8 |
|
13 | 9 | def estimate_exposure_time(timestamps):
|
14 | 10 | '''
|
15 |
| - Takes numpy datetime64[ns] timestamps and returns an estimates of the exposure time in seconds |
| 11 | + Takes numpy datetime64[ns] timestamps and returns an estimates of the exposure time in seconds. |
16 | 12 | '''
|
17 | 13 | delta_s = np.diff(timestamps).astype(int) * float(1e-9)
|
| 14 | + |
18 | 15 | # take only events that came within 30 seconds or so
|
19 | 16 | delta_s = delta_s[delta_s < 30]
|
20 | 17 | scale = delta_s.mean()
|
@@ -43,71 +40,38 @@ def build_exposure_regions(pointing_coords, fov=4.5 * u.deg):
|
43 | 40 | return unique_pointing_positions, regions
|
44 | 41 |
|
45 | 42 |
|
| 43 | +def _build_standard_wcs(image_center, shape, naxis=2, fov=9 * u.deg): |
| 44 | + width, height = shape |
| 45 | + |
| 46 | + w = wcs.WCS(naxis=2) |
| 47 | + w.wcs.crpix = [width / 2 + 0.5, height / 2 + 0.5] |
| 48 | + w.wcs.cdelt = np.array([-fov.value / width, fov.value / height]) |
| 49 | + w.wcs.crval = [image_center.ra.deg, image_center.dec.deg] |
| 50 | + w.wcs.ctype = ["RA---TAN", "DEC--TAN"] |
| 51 | + w.wcs.radesys = 'FK5' |
| 52 | + w.wcs.equinox = 2000.0 |
| 53 | + w.wcs.cunit = ['deg', 'deg'] |
| 54 | + w._naxis = [width, height] |
| 55 | + return w |
| 56 | + |
| 57 | + |
46 | 58 | @u.quantity_input(event_ra=u.hourangle, event_dec=u.deg, fov=u.deg)
|
47 |
| -def build_exposure_map(pointing_coords, event_time, fov=4.5 * u.deg): |
48 |
| - unique_pointing_positions, regions = build_exposure_regions(pointing_coords) |
| 59 | +def build_exposure_map(pointing_coords, event_time, fov=4.5 * u.deg, wcs=None, shape=(1000, 1000)): |
49 | 60 |
|
50 |
| - img_center = SkyCoord(ra=pointing_coords.ra.mean(), dec=pointing_coords.dec.mean()) |
51 |
| - print('getting image') |
52 |
| - hdu = SkyView.get_images(position=img_center, pixels=1000, survey=['DSS'], width=2 * fov, height=2 * fov)[0][0] |
53 |
| - img = hdu.data |
54 |
| - wcs = WCS(hdu.header) |
| 61 | + if not wcs: |
| 62 | + image_center = SkyCoord(ra=pointing_coords.ra.mean(), dec=pointing_coords.dec.mean()) |
| 63 | + wcs = _build_standard_wcs(image_center, shape, fov=2 * fov) |
| 64 | + |
| 65 | + unique_pointing_positions, regions = build_exposure_regions(pointing_coords) |
55 | 66 |
|
56 |
| - print('starting loop') |
57 | 67 | times = []
|
58 | 68 | for p in unique_pointing_positions:
|
59 | 69 | m = (pointing_coords.ra == p.ra) & (pointing_coords.dec == p.dec)
|
60 | 70 | exposure_time = estimate_exposure_time(event_time[m])
|
61 |
| - print(exposure_time.to('h')) |
62 | 71 | times.append(exposure_time)
|
63 | 72 |
|
64 |
| - print(sum(times).to('h')) |
65 |
| - |
66 |
| - # import IPython; IPython.embed() |
67 |
| - masks = [r.to_pixel(wcs).to_mask().to_image(img.shape) for r in regions] |
68 |
| - # |
| 73 | + masks = [r.to_pixel(wcs).to_mask().to_image(shape) for r in regions] |
69 | 74 | cutout = sum(masks).astype(bool)
|
70 |
| - mask = sum([w * m for w, m in zip(times, masks)]) |
71 |
| - |
72 |
| - return np.ma.masked_array(mask.to('h'), mask=~cutout), wcs, img |
73 |
| - |
74 |
| - |
75 |
| -def plot_exposure(img, mask, wcs): |
76 |
| - ax = plt.subplot(projection=wcs) |
77 |
| - |
78 |
| - ax.imshow(img, cmap='gray') |
79 |
| - d = ax.imshow(mask, alpha=0.7) |
80 |
| - cb = plt.colorbar(d) |
81 |
| - cb.set_label('Live Time / Hours') |
82 |
| - ax.set_xlabel('Galactic Longitude') |
83 |
| - ax.set_ylabel('Galactic Latitude') |
84 |
| - |
85 |
| - crab = SkyCoord.from_name('Crab Nebula') |
86 |
| - ax.scatter(crab.ra.deg, crab.dec.deg, transform=ax.get_transform('icrs'), label='Crab Nebula') |
87 |
| - ax.legend() |
88 |
| - |
89 |
| - |
90 |
| -if __name__ == '__main__': |
91 |
| - runs = fio.read_data('crab_dl3.hdf5', key='runs') |
92 |
| - dl3 = fio.read_data('crab_dl3.hdf5', key='events') |
93 |
| - |
94 |
| - data = pd.merge(runs, dl3, on=['run_id', 'night'] ) |
95 |
| - |
96 |
| - timestamps = pd.to_datetime(data.timestamp).values |
97 |
| - total_ontime = estimate_exposure_time(timestamps) |
98 |
| - print('total exposure time: {}'.format(total_ontime.to(u.h))) |
99 |
| - |
100 |
| - ra = data.ra_prediction.values * u.hourangle |
101 |
| - dec = data.dec_prediction.values * u.deg |
102 |
| - |
103 |
| - ra_pointing = data.right_ascension.values * u.hourangle |
104 |
| - dec_pointing = data.declination.values * u.deg |
105 |
| - |
106 |
| - event_coords = SkyCoord(ra=ra, dec=dec) |
107 |
| - pointing_coords = SkyCoord(ra=ra_pointing, dec=dec_pointing) |
108 |
| - |
109 |
| - mask, wcs, img = build_exposure_map(pointing_coords, timestamps) |
| 75 | + mask = sum([w.to('h').value * m for w, m in zip(times, masks)]) |
110 | 76 |
|
111 |
| - ax = plot_exposure(img, mask, wcs) |
112 |
| - plt.savefig('exposure.pdf') |
113 |
| - # plt.show() |
| 77 | + return np.ma.masked_array(mask, mask=~cutout), wcs |
0 commit comments