LSST Applications  21.0.0+c4f5df5339,21.0.0+e70536a077,21.0.0-1-ga51b5d4+7c60f8a6ea,21.0.0-10-gcf60f90+74aa0801fd,21.0.0-12-g63909ac9+643a1044a5,21.0.0-15-gedb9d5423+1041c3824f,21.0.0-2-g103fe59+a356b2badb,21.0.0-2-g1367e85+6d3f3f41db,21.0.0-2-g45278ab+e70536a077,21.0.0-2-g5242d73+6d3f3f41db,21.0.0-2-g7f82c8f+8d7c04eab9,21.0.0-2-g8f08a60+9c9a9cfcc8,21.0.0-2-ga326454+8d7c04eab9,21.0.0-2-gde069b7+bedfc5e1fb,21.0.0-2-gecfae73+6cb6558258,21.0.0-2-gfc62afb+6d3f3f41db,21.0.0-3-g21c7a62+f6e98b25aa,21.0.0-3-g357aad2+bd62456bea,21.0.0-3-g4be5c26+6d3f3f41db,21.0.0-3-g65f322c+03a4076c01,21.0.0-3-g7d9da8d+c4f5df5339,21.0.0-3-gaa929c8+c6b98066dc,21.0.0-3-gc44e71e+a26d5c1aea,21.0.0-3-ge02ed75+04b527a9d5,21.0.0-35-g64f566ff+b27e5ef93e,21.0.0-4-g591bb35+04b527a9d5,21.0.0-4-g88306b8+8773047b2e,21.0.0-4-gc004bbf+80a0b7acb7,21.0.0-4-gccdca77+a5c54364a0,21.0.0-4-ge8fba5a+ccfc328107,21.0.0-5-gdf36809+87b8d260e6,21.0.0-6-g00874e7+7eeda2b6ba,21.0.0-6-g2d4f3f3+e70536a077,21.0.0-6-g4e60332+04b527a9d5,21.0.0-6-g5ef7dad+f53629abd8,21.0.0-7-gc8ca178+b63e69433b,21.0.0-8-gfbe0b4b+c6b98066dc,w.2021.06
LSST Data Management Base Package
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
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
double z
Definition: Match.cc:44
A BoundedField that gives the amount a pixel is distorted at each point.
std::ostream * os
Definition: Schema.cc:746
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
An abstract base class for 2-d functions defined on an integer bounding boxes.
Definition: BoundedField.h:55
lsst::geom::Box2I getBBox() const
Return the bounding box that defines the region where the field is valid.
Definition: BoundedField.h:112
A BoundedField that evaluate the pixel area of a SkyWcs in angular units.
double evaluate(lsst::geom::Point2D const &position) const override
Evaluate the field at the given point.
std::shared_ptr< BoundedField > operator*(double const scale) const override
Return a scaled BoundedField.
bool isPersistable() const noexcept override
PixelAreaBoundedField is persistable if and only if the nested SkyWcs is.
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
bool operator==(BoundedField const &rhs) const override
BoundedFields (of the same sublcass) are equal if their bounding boxes and parameters are equal.
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
An object passed to Persistable::write to allow it to persist itself.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
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...
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
Definition: Persistable.cc:18
A class used to convert scalar POD types such as double to Angle.
Definition: Angle.h:70
An integer coordinate rectangle.
Definition: Box.h:55
Reports invalid arguments.
Definition: Runtime.h:66
T emplace_back(T... args)
def scale(algorithm, min, max=None, frame=None)
Definition: ds9.py:109
FilterProperty & operator=(FilterProperty const &)=default
BoxKey< lsst::geom::Box2I > Box2IKey
Definition: aggregates.h:201
constexpr AngleUnit radians
constant with units of radians
Definition: Angle.h:108
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
A base class for image defects.
T pow(T... args)
T reserve(T... args)
T sqrt(T... args)
int y
Definition: SpanSet.cc:49
double x
table::Key< double > scaling
table::Box2IKey bbox
table::Schema schema
table::Key< int > wcs