-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from nbiederbeck/skymap-dl3
Skymap dl3
- Loading branch information
Showing
6 changed files
with
224 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import json | ||
from argparse import ArgumentParser | ||
|
||
import numpy as np | ||
from astropy import units as u | ||
from astropy.coordinates import SkyCoord | ||
from gammapy.data import DataStore | ||
from gammapy.maps import MapAxis, WcsGeom, WcsNDMap | ||
|
||
parser = ArgumentParser() | ||
parser.add_argument("-i", "--input-path", required=True) | ||
parser.add_argument("-o", "--output-path", required=True) | ||
parser.add_argument("-c", "--config", required=True) | ||
parser.add_argument("--obs-id", required=True) | ||
parser.add_argument( | ||
"--width", | ||
help="Width of skymap", | ||
default="3 deg", | ||
type=u.Quantity, | ||
) | ||
parser.add_argument("--n-bins", default=100) | ||
args = parser.parse_args() | ||
|
||
|
||
@u.quantity_input(width=u.deg) | ||
def main(input_path, config, output_path, obs_id, width, n_bins): # noqa: PLR0913 | ||
with open(config, "r") as f: | ||
config = json.load(f) | ||
|
||
ds = DataStore.from_dir(input_path) | ||
|
||
obs = ds.obs(int(obs_id), ["aeff"]) | ||
|
||
events = obs.events | ||
|
||
evts = events.radec | ||
|
||
source = SkyCoord( | ||
config["DataReductionFITSWriter"]["source_ra"], | ||
config["DataReductionFITSWriter"]["source_dec"], | ||
frame="icrs", | ||
) | ||
|
||
r = width / 2 | ||
bins = u.Quantity( | ||
[ | ||
np.linspace(source.ra - r, source.ra + r, n_bins + 1), | ||
np.linspace(source.dec - r, source.dec + r, n_bins + 1), | ||
], | ||
) | ||
|
||
hist, *edges = np.histogram2d( | ||
evts.ra.to_value(u.deg), | ||
evts.dec.to_value(u.deg), | ||
bins.to_value(u.deg), | ||
) | ||
|
||
energy_axis = MapAxis.from_edges( | ||
u.Quantity([1e-5, 1e5], u.TeV), # basically [-inf, inf], but finite | ||
name="energy", | ||
) | ||
geom = WcsGeom.create( | ||
(n_bins, n_bins), | ||
skydir=source, | ||
binsz=width / n_bins, | ||
width=width, | ||
axes=[energy_axis], | ||
) | ||
|
||
skymap = WcsNDMap(geom, hist) | ||
|
||
skymap.write(output_path, overwrite=True) | ||
|
||
|
||
if __name__ == "__main__": | ||
main(**vars(args)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from argparse import ArgumentParser | ||
|
||
import numpy as np | ||
from astropy import units as u | ||
from gammapy.maps import WcsNDMap | ||
from matplotlib import pyplot as plt | ||
from matplotlib.collections import PatchCollection | ||
from matplotlib.patches import Circle | ||
|
||
parser = ArgumentParser() | ||
parser.add_argument("-i", "--input-path", required=True) | ||
parser.add_argument("-o", "--output-path", required=True) | ||
parser.add_argument( | ||
"--width", | ||
help="Width of skymap", | ||
default="4 deg", | ||
type=u.Quantity, | ||
) | ||
parser.add_argument("--n-bins", default=100) | ||
args = parser.parse_args() | ||
|
||
|
||
@u.quantity_input(width=u.deg) | ||
def main(input_path, output_path, width, n_bins): | ||
skymap = WcsNDMap.read(input_path) | ||
|
||
geom = skymap.geom | ||
source = geom.center_skydir | ||
|
||
edges = u.Quantity( | ||
[ | ||
u.Quantity(geom.pix_to_coord([i, j])) | ||
for i, j in zip(range(int(geom.npix[0])), range(int(geom.npix[1]))) | ||
], | ||
).T | ||
|
||
patches = PatchCollection( | ||
[ | ||
Circle( | ||
(source.ra.to_value(u.deg), source.dec.to_value(u.deg)), | ||
radius=0.3, | ||
color="k", | ||
ec="k", | ||
fc="#0000", | ||
fill=False, | ||
), | ||
] | ||
+ [ | ||
Circle( | ||
( | ||
source.ra.to_value(u.deg) + 0.8 * np.sin(phi), | ||
source.dec.to_value(u.deg) + 0.8 * np.cos(phi), | ||
), | ||
radius=0.3, | ||
color="w", | ||
ec="w", | ||
fc="#0000", | ||
fill=False, | ||
) | ||
for phi in np.linspace(0, 2 * np.pi, 7) | ||
], | ||
match_original=True, | ||
) | ||
|
||
fig, ax = plt.subplots() | ||
|
||
mesh = ax.pcolormesh( | ||
*edges.to_value(u.deg), | ||
skymap.data[0, ...], | ||
rasterized=True, | ||
) | ||
|
||
fig.colorbar(mesh, ax=ax) | ||
|
||
ax.set_xlabel("RA / deg") | ||
ax.set_ylabel("Dec / deg") | ||
ax.set_aspect(1) | ||
|
||
ax.scatter( | ||
source.ra.to_value(u.deg), | ||
source.dec.to_value(u.deg), | ||
color="w", | ||
edgecolor="k", | ||
label="Source", | ||
) | ||
|
||
ax.add_collection(patches) | ||
|
||
ax.legend() | ||
|
||
fig.savefig(output_path) | ||
|
||
|
||
if __name__ == "__main__": | ||
main(**vars(args)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters