Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve jointcal plotting backend Tickets/dm 7444 #19

Merged
merged 3 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 98 additions & 0 deletions bin.src/plot_jointcal_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env python
# See COPYRIGHT file at the top of the source tree.
"""
Generate plots from the results of a jointcal run.

Requires a butler repository that has had jointcal successfully run on it, which
produces a new "wcs" component of the repository.
"""

import os

import lsst.daf.persistence
from lsst.meas.astrom import LoadAstrometryNetObjectsTask, LoadAstrometryNetObjectsConfig
import lsst.afw.image
from lsst.afw.geom import degrees

from lsst.jointcal import utils


def get_valid_dataIds(butler, dataset_type='wcs'):
"""Return a list of all dataIds that exist in this butler."""
data_ids = []
for data_ref in butler.subset(dataset_type):
if data_ref.datasetExists():
data_ids.append(data_ref.dataId)
return data_ids


def prep_reference_loader(center, radius):
"""
Return an astrometry.net reference loader.

Parameters
----------
center: afw.coord
The center of the field you're testing on.
radius: afw.geom.angle
The radius to load objects around center.
"""
refLoader = LoadAstrometryNetObjectsTask(LoadAstrometryNetObjectsConfig())
# Make a copy of the reference catalog for in-memory contiguity.
return refLoader.loadSkyCircle(center, radius, filterName='r').refCat.copy()


def get_old_wcs_list(data_refs):
"""Return a list of the original WCSs of the data_refs."""
result = []
for data_ref in data_refs:
md = data_ref.get("calexp_md", immediate=True)
tanwcs = lsst.afw.image.TanWcs.cast(lsst.afw.image.makeWcs(md))
result.append(tanwcs)
return result


def main():
import argparse
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("repo", metavar="repo", help="Directory containing butler repository.")
parser.add_argument("refcat", metavar="refcat", help="Directory of reference catalog to match against.")
parser.add_argument("ra", default=None, type=float, nargs='?',
help="Decimal degrees center RA of catalog (default uses center of first visit).")
parser.add_argument("dec", default=None, type=float, nargs='?',
help="Decimal degrees center Dec of catalog (default uses center of first visit).")
parser.add_argument("radius", default=6., type=float, nargs='?',
help="Radius (degrees) of sources to load from reference catalog.")
parser.add_argument("-i", "--interactive", action="store_true",
help="Use interactive matplotlib backend and set ion(), in addition to saving files.")
parser.add_argument("-o", "--outdir", default=".plots",
help="output directory for plots (default: $(default)s)")
args = parser.parse_args()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be useful here to include an argument that allows the user to specify an output directory

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion. Done.


butler = lsst.daf.persistence.Butler(inputs=args.repo)
dataIds = get_valid_dataIds(butler)

data_refs = [butler.dataRef('wcs', dataId=dataId) for dataId in dataIds]
visits = [data_ref.dataId['visit'] for data_ref in data_refs]
old_wcs_list = get_old_wcs_list(data_refs)

os.environ['ASTROMETRY_NET_DATA_DIR'] = args.refcat
if args.ra is None or args.dec is None:
calexp = data_refs[0].get('calexp')
center = calexp.getInfo().getVisitInfo().getBoresightRaDec()
else:
center = (args.ra, args.dec)

reference = prep_reference_loader(center, args.radius*degrees)

jointcalStatistics = utils.JointcalStatistics()
jointcalStatistics.compute_rms(data_refs, visits, reference)

name = os.path.basename(os.path.normpath(args.repo))
jointcalStatistics.make_plots(data_refs, visits, old_wcs_list, name=name,
interactive=args.interactive, outdir=args.outdir)


if __name__ == "__main__":
main()