Skip to content

Commit

Permalink
add image::bboxFromMetadata
Browse files Browse the repository at this point in the history
This function provides a bounding box, given the FITS header.
  • Loading branch information
PaulPrice committed Jul 24, 2017
1 parent 8be4659 commit be2696a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/lsst/afw/image/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,13 @@ class DecoratedImage : public lsst::daf::base::Persistable, public lsst::daf::ba

template <typename PixelT>
void swap(DecoratedImage<PixelT>& a, DecoratedImage<PixelT>& b);

/// Determine the image bounding box from its metadata (FITS header)
///
/// Note that this modifies the metadata, stripping the WCS headers that
/// provide the xy0.
geom::Box2I bboxFromMetadata(daf::base::PropertySet & metadata);

}
}
} // lsst::afw::image
Expand Down
2 changes: 2 additions & 0 deletions python/lsst/afw/image/image/image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ PYBIND11_PLUGIN(image) {
declareCastConstructor<std::uint64_t, float>(clsImageF);
declareCastConstructor<std::uint64_t, double>(clsImageD);

mod.def("bboxFromMetadata", &bboxFromMetadata);

return mod.ptr();
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/image/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,13 @@ Image<LhsPixelT>& operator/=(Image<LhsPixelT>& lhs, Image<RhsPixelT> const& rhs)
return lhs;
}

geom::Box2I bboxFromMetadata(daf::base::PropertySet & metadata)
{
geom::Extent2I dims{metadata.getAsInt("NAXIS1"), metadata.getAsInt("NAXIS2")};
geom::Point2I xy0 = detail::getImageXY0FromMetadata(detail::wcsNameForXY0, &metadata);
return geom::Box2I(xy0, dims);
}

//
// Explicit instantiations
//
Expand Down
16 changes: 16 additions & 0 deletions tests/test_imageIo1.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,22 @@ def testLongStrings(self):
expNew = afwImage.ExposureF(tmpFile)
self.assertEqual(expNew.getMetadata().get(keyWord), longString)

def checkBBoxFromMetadata(self, filename, expected, hdu=0):
metadata = afwImage.readMetadata(filename, hdu)
bbox = afwImage.bboxFromMetadata(metadata)
self.assertEqual(bbox, expected)

def testBBoxFromMetadata(self):
self.checkBBoxFromMetadata(os.path.join(dataDir, "871034p_1_img.fits"),
afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(2112, 4644)))
for hdu in range(1, 4):
self.checkBBoxFromMetadata(os.path.join(dataDir, "871034p_1_MI.fits"),
afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(2112, 4644)),
hdu=hdu)
self.checkBBoxFromMetadata(os.path.join(dataDir, "medsub.fits"),
afwGeom.Box2I(afwGeom.Point2I(40, 150), afwGeom.Extent2I(145, 200)),
hdu=hdu)


class TestMemory(lsst.utils.tests.MemoryTestCase):
pass
Expand Down

0 comments on commit be2696a

Please sign in to comment.