28 #include "gsl/gsl_interp.h" 29 #include "gsl/gsl_interp2d.h" 30 #include "gsl/gsl_errno.h" 39 #define LSST_CHECK_GSL(type, status) \ 40 if (status) throw LSST_EXCEPT(type, "GSL error: "s + ::gsl_strerror(status)) 53 class IdentityTransmissionCurve :
public TransmissionCurve {
55 static constexpr
char const* NAME =
"IdentityTransmissionCurve";
69 void sampleAt(
lsst::geom::Point2D const&, ndarray::Array<double const, 1, 1>
const& wavelengths,
70 ndarray::Array<double, 1, 1>
const& out)
const override {
74 bool isPersistable()
const override {
return true; }
80 return shared_from_this();
89 std::string getPersistenceName()
const override {
return NAME; }
91 void write(OutputArchiveHandle& handle)
const override { handle.saveEmpty(); }
93 class Factory :
public table::io::PersistableFactory {
96 CatalogVector
const& catalogs)
const override {
101 Factory() : table::io::PersistableFactory(NAME) {}
107 IdentityTransmissionCurve() =
default;
133 template <
typename T>
136 template <
typename T>
137 GslPtr<T> makeGslPtr(T* p,
void (*
free)(T*)) {
141 return GslPtr<T>(p,
free);
152 static constexpr
bool isSpatiallyConstant =
true;
153 static constexpr
char const* NAME =
"SpatiallyConstantTransmissionCurve";
158 Impl1d(ndarray::Array<double, 1, 1>
const& throughput, ndarray::Array<double, 1, 1>
const& wavelengths)
159 : _throughput(throughput),
160 _wavelengths(wavelengths),
161 _interp(makeGslPtr(::gsl_interp_alloc(::gsl_interp_linear, _wavelengths.size()),
162 &::gsl_interp_free)) {
163 int status = ::gsl_interp_init(_interp.get(), _wavelengths.getData(), _throughput.getData(),
164 _wavelengths.size());
172 static void setupPersistence(table::Schema&
schema, ArrayKeyVector&
keys) {
173 keys.push_back(schema.addField<table::Array<double>>(
"throughput",
"array of known throughput values",
175 keys.push_back(schema.addField<table::Array<double>>(
176 "wavelengths",
"array of known wavelength values",
"angstrom", 0));
179 void persist(table::BaseRecord& record, ArrayKeyVector
const& keys)
const {
180 record.set(keys[0], _throughput);
181 record.set(keys[1], _wavelengths);
184 static Impl1d unpersist(table::BaseRecord& record, ArrayKeyVector
const& keys) {
185 return Impl1d(record[keys[0]], record[keys[1]]);
192 : _accel(makeGslPtr(::gsl_interp_accel_alloc(), &gsl_interp_accel_free)) {}
194 double operator()(Impl1d
const& parent,
double wavelength) {
196 int status = ::gsl_interp_eval_e(parent._interp.get(), parent._wavelengths.getData(),
197 parent._throughput.getData(), wavelength, _accel.get(), &
result);
203 GslPtr<::gsl_interp_accel> _accel;
207 ndarray::Array<double, 1, 1> _throughput;
208 ndarray::Array<double, 1, 1> _wavelengths;
209 GslPtr<::gsl_interp> _interp;
218 static constexpr
bool isSpatiallyConstant =
false;
219 static constexpr
char const* NAME =
"RadialTransmissionCurve";
221 Impl2d(ndarray::Array<double, 1, 1>
const& throughput, ndarray::Array<double, 1, 1>
const& wavelengths,
222 ndarray::Array<double, 1, 1>
const& radii)
223 : _throughput(throughput),
224 _wavelengths(wavelengths),
227 ::gsl_interp2d_alloc(::gsl_interp2d_bilinear, _wavelengths.size(),
_radii.size()),
228 &::gsl_interp2d_free)) {
229 int status = ::gsl_interp2d_init(_interp.get(), _wavelengths.getData(),
_radii.getData(),
230 _throughput.getData(), _wavelengths.size(),
_radii.size());
238 static void setupPersistence(table::Schema&
schema, ArrayKeyVector&
keys) {
239 keys.push_back(schema.addField<table::Array<double>>(
241 "flattenned 2-d array of known throughput values, with radius dimension fastest",
"", 0));
242 keys.push_back(schema.addField<table::Array<double>>(
243 "wavelengths",
"array of known wavelength values",
"angstrom", 0));
244 keys.push_back(schema.addField<table::Array<double>>(
"radii",
"array of known radius values",
"", 0));
247 void persist(table::BaseRecord& record, ArrayKeyVector
const& keys)
const {
248 record.set(keys[0], _throughput);
249 record.set(keys[1], _wavelengths);
250 record.set(keys[2],
_radii);
253 static Impl2d unpersist(table::BaseRecord& record, ArrayKeyVector
const& keys) {
254 return Impl2d(record[keys[0]], record[keys[1]], record[keys[2]]);
261 : _radius(point.
asEigen().norm()),
262 _radiusAccel(makeGslPtr(::gsl_interp_accel_alloc(), &gsl_interp_accel_free)),
263 _wavelengthAccel(makeGslPtr(::gsl_interp_accel_alloc(), &gsl_interp_accel_free)) {
264 _radius =
std::max(_radius, parent._radii.front());
265 _radius =
std::min(_radius, parent._radii.back());
268 double operator()(Impl2d
const& parent,
double wavelength) {
271 ::gsl_interp2d_eval_e(parent._interp.get(), parent._wavelengths.getData(),
272 parent._radii.getData(), parent._throughput.getData(), wavelength,
273 _radius, _radiusAccel.get(), _wavelengthAccel.get(), &
result);
280 GslPtr<::gsl_interp_accel> _radiusAccel;
281 GslPtr<::gsl_interp_accel> _wavelengthAccel;
285 ndarray::Array<double, 1, 1> _throughput;
286 ndarray::Array<double, 1, 1> _wavelengths;
287 ndarray::Array<double, 1, 1>
_radii;
288 GslPtr<::gsl_interp2d> _interp;
291 template <
typename Impl>
292 class InterpolatedTransmissionCurve :
public TransmissionCurve {
295 : _atBounds(throughputAtBounds), _impl(
std::move(impl)) {
297 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
298 "Throughput values at bounds must be finite");
306 void sampleAt(
lsst::geom::Point2D const& point, ndarray::Array<double const, 1, 1>
const& wavelengths,
307 ndarray::Array<double, 1, 1>
const& out)
const override {
308 LSST_THROW_IF_NE(wavelengths.getSize<0>(), out.getSize<0>(), pex::exceptions::LengthError,
309 "Length of wavelength array (%d) does not match size of output array (%d)");
310 typename Impl::Functor functor(_impl, point);
311 auto bounds = _impl.getWavelengthBounds();
312 auto wlIter = wavelengths.begin();
313 for (
auto outIter = out.begin(); outIter != out.end(); ++outIter, ++wlIter) {
314 double&
y = *outIter;
315 if (*wlIter < bounds.first) {
317 }
else if (*wlIter > bounds.second) {
318 y = _atBounds.second;
320 y = functor(_impl, *wlIter);
325 bool isPersistable()
const override {
return true; }
330 if (_impl.isSpatiallyConstant) {
331 return shared_from_this();
333 return TransmissionCurve::_transformedByImpl(transform);
337 std::string getPersistenceName()
const override {
return Impl::NAME; }
339 struct PersistenceHelper {
345 static PersistenceHelper
const&
get() {
346 static PersistenceHelper
const instance;
354 schema.addField<
double>(
"throughputAtMin",
"throughput below minimum wavelength")),
356 schema.addField<
double>(
"throughputAtMax",
"throughput above minimum wavelength")) {
357 Impl::setupPersistence(schema, arrays);
358 schema.getCitizen().markPersistent();
362 void write(OutputArchiveHandle& handle)
const override {
363 auto const&
keys = PersistenceHelper::get();
364 auto catalog = handle.makeCatalog(
keys.schema);
365 auto record = catalog.addNew();
366 record->set(
keys.throughputAtMin, _atBounds.first);
367 record->set(
keys.throughputAtMax, _atBounds.second);
368 _impl.persist(*record,
keys.arrays);
369 handle.saveCatalog(catalog);
372 class Factory :
public table::io::PersistableFactory {
375 CatalogVector
const& catalogs)
const override {
376 auto const&
keys = PersistenceHelper::get();
379 auto& record = catalogs.front().front();
380 return std::make_shared<InterpolatedTransmissionCurve>(
381 Impl::unpersist(record,
keys.arrays),
385 Factory() : table::io::PersistableFactory(Impl::NAME) {}
395 template <
typename Impl>
398 template class InterpolatedTransmissionCurve<Impl1d>;
399 template class InterpolatedTransmissionCurve<Impl2d>;
407 class ProductTransmissionCurve :
public TransmissionCurve {
409 static constexpr
char const* NAME =
"ProductTransmissionCurve";
416 auto aWavelengthBounds = _a->getWavelengthBounds();
417 auto bWavelengthBounds = _b->getWavelengthBounds();
418 auto aThroughputAtBounds = _a->getThroughputAtBounds();
419 auto bThroughputAtBounds = _b->getThroughputAtBounds();
421 auto determineWavelengthBound = [](
double aWavelength,
double bWavelength,
double aThroughput,
422 double bThroughput,
auto isFirstOuter) ->
double {
426 if (isFirstOuter(aWavelength, bWavelength)) {
427 return (bThroughput == 0.0) ? bWavelength : aWavelength;
429 return (aThroughput == 0.0) ? aWavelength : bWavelength;
433 return std::make_pair(determineWavelengthBound(aWavelengthBounds.first, bWavelengthBounds.first,
434 aThroughputAtBounds.first, bThroughputAtBounds.first,
436 determineWavelengthBound(aWavelengthBounds.second, bWavelengthBounds.second,
437 aThroughputAtBounds.second, bThroughputAtBounds.second,
442 auto aAtBounds = _a->getThroughputAtBounds();
443 auto bAtBounds = _b->getThroughputAtBounds();
444 return std::make_pair(aAtBounds.first * bAtBounds.first, aAtBounds.second * bAtBounds.second);
447 void sampleAt(
lsst::geom::Point2D const& position, ndarray::Array<double const, 1, 1>
const& wavelengths,
448 ndarray::Array<double, 1, 1>
const& out)
const override {
449 _a->sampleAt(position, wavelengths, out);
450 ndarray::Array<double, 1, 1> tmp = ndarray::allocate(wavelengths.getSize<0>());
451 _b->sampleAt(position, wavelengths, tmp);
455 bool isPersistable()
const override {
return _a->isPersistable() && _b->isPersistable(); }
458 std::string getPersistenceName()
const override {
return NAME; }
460 struct PersistenceHelper {
465 static PersistenceHelper
const&
get() {
466 static PersistenceHelper
const instance;
473 a(schema.addField<
int>(
"a",
"archive ID of first operand")),
474 b(schema.addField<
int>(
"b",
"archive ID of second operand")) {
475 schema.getCitizen().markPersistent();
479 class Factory :
public table::io::PersistableFactory {
482 CatalogVector
const& catalogs)
const override {
483 auto const&
keys = PersistenceHelper::get();
486 auto const& record = catalogs.front().front();
487 return std::make_shared<ProductTransmissionCurve>(
488 archive.get<TransmissionCurve>(record.get(
keys.a)),
489 archive.get<TransmissionCurve>(record.get(
keys.b)));
492 Factory() : table::io::PersistableFactory(NAME) {}
495 void write(OutputArchiveHandle& handle)
const override {
496 auto const&
keys = PersistenceHelper::get();
497 auto catalog = handle.makeCatalog(
keys.schema);
498 auto record = catalog.addNew();
499 record->set(
keys.a, handle.put(_a));
500 record->set(
keys.b, handle.put(_b));
501 handle.saveCatalog(catalog);
519 class TransformedTransmissionCurve :
public TransmissionCurve {
521 static constexpr
char const* NAME =
"TransformedTransmissionCurve";
530 return _nested->getThroughputAtBounds();
533 void sampleAt(
lsst::geom::Point2D const& position, ndarray::Array<double const, 1, 1>
const& wavelengths,
534 ndarray::Array<double, 1, 1>
const& out)
const override {
535 return _nested->sampleAt(_transform->applyInverse(position), wavelengths, out);
538 bool isPersistable()
const override {
return _nested->isPersistable() && _transform->isPersistable(); }
544 return std::make_shared<TransformedTransmissionCurve>(_nested, transform->then(*_transform));
547 std::string getPersistenceName()
const override {
return NAME; }
549 struct PersistenceHelper {
554 static PersistenceHelper
const&
get() {
555 static PersistenceHelper
const instance;
562 nested(schema.addField<
int>(
"nested",
"archive ID of the nested TransmissionCurve")),
563 transform(schema.addField<
int>(
"transform",
"archive ID of the coordinate transform")) {
564 schema.getCitizen().markPersistent();
568 void write(OutputArchiveHandle& handle)
const override {
569 auto const&
keys = PersistenceHelper::get();
570 auto catalog = handle.makeCatalog(
keys.schema);
571 auto record = catalog.addNew();
572 record->set(
keys.nested, handle.put(_nested));
573 record->set(
keys.transform, handle.put(_transform));
574 handle.saveCatalog(catalog);
577 class Factory :
public table::io::PersistableFactory {
580 CatalogVector
const& catalogs)
const override {
581 auto const&
keys = PersistenceHelper::get();
584 auto const& record = catalogs.front().front();
585 return std::make_shared<TransformedTransmissionCurve>(
586 archive.get<TransmissionCurve>(record.get(
keys.nested)),
590 Factory() : table::io::PersistableFactory(NAME) {}
609 return IdentityTransmissionCurve::get();
613 ndarray::Array<double const, 1>
const& throughput, ndarray::Array<double const, 1>
const& wavelengths,
615 ::gsl_set_error_handler_off();
617 "Length of wavelength array (%d) does not match size of throughput array (%d)");
618 return std::make_shared<InterpolatedTransmissionCurve<Impl1d>>(
619 Impl1d(ndarray::copy(throughput), ndarray::copy(wavelengths)),
624 ndarray::Array<double const, 2>
const& throughput, ndarray::Array<double const, 1>
const& wavelengths,
626 ::gsl_set_error_handler_off();
629 "Length of wavelength array (%d) does not match first dimension of of throughput array (%d)");
632 "Length of radii array (%d) does not match second dimension of of throughput array (%d)");
636 ndarray::Array<double, 2, 2> throughputTransposed = ndarray::allocate(throughput.getShape().reverse());
637 throughputTransposed.transpose() = throughput;
638 ndarray::Array<double, 1, 1> throughputFlat = ndarray::flatten<1>(throughputTransposed);
639 return std::make_shared<InterpolatedTransmissionCurve<Impl2d>>(
640 Impl2d(throughputFlat, ndarray::copy(wavelengths), ndarray::copy(radii)),
646 auto a = shared_from_this();
648 auto result =
a->_multipliedByImpl(
b);
660 return _transformedByImpl(
std::move(transform));
663 ndarray::Array<double, 1, 1> TransmissionCurve::sampleAt(
664 lsst::geom::Point2D const& position, ndarray::Array<double const, 1, 1>
const& wavelengths)
const {
665 ndarray::Array<double, 1, 1> out = ndarray::allocate(wavelengths.getSize<0>());
666 sampleAt(position, wavelengths, out);
672 return std::make_shared<TransformedTransmissionCurve>(shared_from_this(),
std::move(transform));
680 std::string TransmissionCurve::getPythonModule()
const {
return "lsst.afw.image"; }
A spatially-varying transmission curve as a function of wavelength.
T shared_from_this(T... args)
#define LSST_THROW_IF_NE(N1, N2, EXC_CLASS, MSG)
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
table::Key< double > throughputAtMax
ItemVariant const * other
table::Key< double > throughputAtMin
Transform< Point2Endpoint, Point2Endpoint > TransformPoint2ToPoint2
A base class for image defects.
#define LSST_CHECK_GSL(type, status)
std::unique_ptr< SchemaItem< U > > result
#define LSST_EXCEPT(type,...)
EigenVector const & asEigen() const