lsst.afw  19.0.0-14-g706b86db4+3
PixelAreaBoundedField.cc
Go to the documentation of this file.
1 /*
2  * This file is part of afw.
3  *
4  * Developed for the LSST Data Management System.
5  * This product includes software developed by the LSST Project
6  * (https://www.lsst.org).
7  * See the COPYRIGHT file at the top-level directory of this distribution
8  * for details of code ownership.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
29 
31 
32 namespace lsst {
33 namespace afw {
34 namespace table {
35 namespace io {
36 
40  );
41 
42 } // namespace io
43 } // namespace table
44 
45 namespace math {
46 
47 PixelAreaBoundedField::PixelAreaBoundedField(
48  lsst::geom::Box2I const &bbox,
50  lsst::geom::AngleUnit const & unit,
51  double scaling
52 ) : BoundedField(bbox),
53  _skyWcs(skyWcs),
54  _scaling(scaling)
55 {
56  if (_skyWcs == nullptr) {
57  throw LSST_EXCEPT(
59  "SkyWcs passed to PixelAreaBoundedField is null."
60  );
61  }
62  _scaling /= std::pow((1.0*unit).asRadians(), 2);
63 }
64 
66  return std::pow(_skyWcs->getPixelScale(position).asRadians(), 2) * _scaling;
67 }
68 
69 ndarray::Array<double, 1, 1> PixelAreaBoundedField::evaluate(
70  ndarray::Array<double const, 1> const & x,
71  ndarray::Array<double const, 1> const & y
72 ) const {
73  if (x.getShape() != y.getShape()) {
74  throw LSST_EXCEPT(
76  (boost::format("Inconsistent shapes in evaluate; %s != %s.") % x.getShape() % y.getShape()).str()
77  );
78  }
79  // Compute _skyWcs->pixelToSky at all of the given points in a single
80  // vectorized call, along with points one pixel away in x and y.
81  double constexpr side = 1.0;
82  std::size_t const n = x.size();
84  pixPoints.reserve(n*3);
85  for (std::size_t i = 0; i < n; ++i) {
86  pixPoints.emplace_back(x[i], y[i]);
87  pixPoints.emplace_back(x[i] + side, y[i]);
88  pixPoints.emplace_back(x[i], y[i] + side);
89  }
90  auto skyPoints = _skyWcs->pixelToSky(pixPoints);
91  // Work in 3-space to avoid RA wrapping and pole issues.
92  ndarray::Array<double, 1, 1> z = ndarray::allocate(x.getShape());
93  for (std::size_t i = 0; i < n; ++i) {
94  std::size_t j = i*3;
95  auto skyLL = skyPoints[j].getVector();
96  auto skyDx = skyPoints[j + 1].getVector() - skyLL;
97  auto skyDy = skyPoints[j + 2].getVector() - skyLL;
98  double skyAreaSq = skyDx.cross(skyDy).getSquaredNorm();
99  z[i] = _scaling * std::sqrt(skyAreaSq) / (side*side);
100  }
101  return z;
102 }
103 
105  return _skyWcs->isPersistable();
106 }
107 
109  return std::make_shared<PixelAreaBoundedField>(getBBox(), _skyWcs, lsst::geom::radians, _scaling*scale);
110 }
111 
113  auto rhsCasted = dynamic_cast<PixelAreaBoundedField const *>(&rhs);
114  if (!rhsCasted) return false;
115 
116  return getBBox() == rhsCasted->getBBox() && *_skyWcs == *rhsCasted->_skyWcs &&
117  _scaling == rhsCasted->_scaling;
118 }
119 
120 std::string PixelAreaBoundedField::toString() const {
122  os << "PixelAreaBoundedField(" << (*_skyWcs) << ", scaling=" << _scaling << ")";
123  return os.str();
124 }
125 
126 
127 namespace {
128 
129 struct PersistenceHelper {
130  table::Schema schema;
132  table::Key<int> wcs;
133  table::Key<double> scaling;
134 
135  static PersistenceHelper const & get() {
136  static PersistenceHelper const instance;
137  return instance;
138  }
139 
140 private:
141  PersistenceHelper() :
142  schema(),
143  bbox(table::Box2IKey::addFields(schema, "bbox", "Bounding box for field.", "pixel")),
144  wcs(schema.addField<int>("wcs", "Archive ID for SkyWcs instance.")),
145  scaling(schema.addField<double>("scaling",
146  "Scaling factor (including any transformation from rad^2."))
147  {}
148  PersistenceHelper(PersistenceHelper const &) = delete;
149  PersistenceHelper(PersistenceHelper &&) = delete;
150  PersistenceHelper & operator=(PersistenceHelper const &) = delete;
151  PersistenceHelper & operator=(PersistenceHelper &&) = delete;
152  ~PersistenceHelper() noexcept = default;
153 };
154 
155 class PixelAreaBoundedFieldFactory : public table::io::PersistableFactory {
156 public:
157  explicit PixelAreaBoundedFieldFactory(std::string const& name)
158  : afw::table::io::PersistableFactory(name) {}
159 
160  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
161  CatalogVector const& catalogs) const override {
162  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
163  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
164  table::BaseRecord const& record = catalogs.front().front();
165  auto const & keys = PersistenceHelper::get();
166  LSST_ARCHIVE_ASSERT(record.getSchema() == keys.schema);
167  lsst::geom::Box2I bbox(record.get(keys.bbox));
168  auto wcs = archive.get<afw::geom::SkyWcs>(record.get(keys.wcs));
169  double scaling = record.get(keys.scaling);
170  return std::make_shared<PixelAreaBoundedField>(bbox, wcs, lsst::geom::radians, scaling);
171  }
172 };
173 
174 std::string getPixelAreaBoundedFieldPersistenceName() { return "PixelAreaBoundedField"; }
175 
176 PixelAreaBoundedFieldFactory registration(getPixelAreaBoundedFieldPersistenceName());
177 
178 } // namespace
179 
181  return getPixelAreaBoundedFieldPersistenceName();
182 }
183 
184 std::string PixelAreaBoundedField::getPythonModule() const { return "lsst.afw.math"; }
185 
187  auto const & keys = PersistenceHelper::get();
188  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
189  std::shared_ptr<table::BaseRecord> record = catalog.addNew();
190  record->set(keys.bbox, getBBox());
191  record->set(keys.wcs, handle.put(_skyWcs));
192  record->set(keys.scaling, _scaling);
193  handle.saveCatalog(catalog);
194 }
195 
196 } // namespace math
197 } // namespace afw
198 } // namespace lsst
def keys(self)
int put(Persistable const *obj, bool permissive=false)
Save an object to the archive and return a unique ID that can be used to retrieve it from an InputArc...
double evaluate(lsst::geom::Point2D const &position) const override
Evaluate the field at the given point.
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
An object passed to Persistable::write to allow it to persist itself.
BoxKey< lsst::geom::Box2I > Box2IKey
Definition: aggregates.h:201
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
def scale(algorithm, min, max=None, frame=None)
Definition: ds9.py:109
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
Definition: Persistable.cc:18
double z
Definition: Match.cc:44
AngleUnit constexpr radians
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
STL class.
bool isPersistable() const noexcept override
PixelAreaBoundedField is persistable if and only if the nested SkyWcs is.
A base class for image defects.
A BoundedField that gives the amount a pixel is distorted at each point.
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
lsst::geom::Box2I getBBox() const
Return the bounding box that defines the region where the field is valid.
Definition: BoundedField.h:112
T str(T... args)
double x
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
#define LSST_EXCEPT(type,...)
STL class.
T pow(T... args)
bool operator==(BoundedField const &rhs) const override
BoundedFields (of the same sublcass) are equal if their bounding boxes and parameters are equal...
An abstract base class for 2-d functions defined on an integer bounding boxes.
Definition: BoundedField.h:55
table::Key< int > wcs
table::Schema schema
table::Box2IKey bbox
T sqrt(T... args)
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
std::shared_ptr< BoundedField > operator*(double const scale) const override
Return a scaled BoundedField.
A BoundedField that evaluate the pixel area of a SkyWcs in angular units.
table::Key< double > scaling
std::ostream * os
Definition: Schema.cc:746
int y
Definition: SpanSet.cc:49
static BoxKey addFields(Schema &schema, std::string const &name, std::string const &doc, std::string const &unit)
Add _min_x, _min_y, _max_x, _max_y fields to a Schema, and return a BoxKey that points to them...
Definition: aggregates.cc:60
PixelAreaBoundedField & operator=(PixelAreaBoundedField const &)=delete
T reserve(T... args)
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
Definition: Catalog.h:485
T emplace_back(T... args)