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)) 54 class IdentityTransmissionCurve :
public TransmissionCurve {
57 static constexpr
char const * NAME =
"IdentityTransmissionCurve";
75 ndarray::Array<double const,1,1>
const & wavelengths,
76 ndarray::Array<double,1,1>
const & out
81 bool isPersistable()
const override {
return true; }
89 return shared_from_this();
99 std::string getPersistenceName()
const override {
return NAME; }
101 void write(OutputArchiveHandle& handle)
const override {
105 class Factory :
public table::io::PersistableFactory {
109 InputArchive
const& archive,
110 CatalogVector
const& catalogs
116 Factory() : table::io::PersistableFactory(NAME) {}
122 IdentityTransmissionCurve() =
default;
149 template <
typename T>
152 template <
typename T>
153 GslPtr<T> makeGslPtr(T* p,
void (*
free)(T*)) {
156 pex::exceptions::MemoryError,
157 "Could not allocate GSL object." 160 return GslPtr<T>(p,
free);
172 static constexpr
bool isSpatiallyConstant =
true;
173 static constexpr
char const * NAME =
"SpatiallyConstantTransmissionCurve";
178 Impl1d(ndarray::Array<double,1,1>
const & throughput,
179 ndarray::Array<double,1,1>
const & wavelengths) :
180 _throughput(throughput),
181 _wavelengths(wavelengths),
182 _interp(makeGslPtr(::gsl_interp_alloc(::gsl_interp_linear, _wavelengths.size()),
185 int status = ::gsl_interp_init(_interp.get(), _wavelengths.getData(), _throughput.getData(),
186 _wavelengths.size());
194 static void setupPersistence(table::Schema &
schema, ArrayKeyVector & keys) {
196 schema.addField<table::Array<double>>(
197 "throughput",
"array of known throughput values",
"", 0
201 schema.addField<table::Array<double>>(
202 "wavelengths",
"array of known wavelength values",
"angstrom", 0
207 void persist(table::BaseRecord & record, ArrayKeyVector
const & keys)
const {
208 record.set(keys[0], _throughput);
209 record.set(keys[1], _wavelengths);
212 static Impl1d unpersist(table::BaseRecord & record, ArrayKeyVector
const & keys) {
213 return Impl1d(record[keys[0]], record[keys[1]]);
221 _accel(makeGslPtr(::gsl_interp_accel_alloc(), &gsl_interp_accel_free))
224 double operator()(Impl1d
const & parent,
double wavelength) {
226 int status = ::gsl_interp_eval_e(parent._interp.get(), parent._wavelengths.getData(),
227 parent._throughput.getData(),
228 wavelength, _accel.get(), &result);
234 GslPtr<::gsl_interp_accel> _accel;
238 ndarray::Array<double,1,1> _throughput;
239 ndarray::Array<double,1,1> _wavelengths;
240 GslPtr<::gsl_interp> _interp;
250 static constexpr
bool isSpatiallyConstant =
false;
251 static constexpr
char const * NAME =
"RadialTransmissionCurve";
253 Impl2d(ndarray::Array<double,1,1>
const & throughput,
254 ndarray::Array<double,1,1>
const & wavelengths,
255 ndarray::Array<double,1,1>
const & radii) :
256 _throughput(throughput),
257 _wavelengths(wavelengths),
259 _interp(makeGslPtr(::gsl_interp2d_alloc(::gsl_interp2d_bilinear, _wavelengths.size(),
261 &::gsl_interp2d_free))
263 int status = ::gsl_interp2d_init(_interp.get(), _wavelengths.getData(),
_radii.getData(),
264 _throughput.getData(), _wavelengths.size(),
_radii.size());
272 static void setupPersistence(table::Schema &
schema, ArrayKeyVector & keys) {
274 schema.addField<table::Array<double>>(
276 "flattenned 2-d array of known throughput values, with radius dimension fastest",
"", 0
280 schema.addField<table::Array<double>>(
281 "wavelengths",
"array of known wavelength values",
"angstrom", 0
285 schema.addField<table::Array<double>>(
286 "radii",
"array of known radius values",
"", 0
291 void persist(table::BaseRecord & record, ArrayKeyVector
const & keys)
const {
292 record.set(keys[0], _throughput);
293 record.set(keys[1], _wavelengths);
294 record.set(keys[2],
_radii);
297 static Impl2d unpersist(table::BaseRecord & record, ArrayKeyVector
const & keys) {
298 return Impl2d(record[keys[0]], record[keys[1]], record[keys[2]]);
305 Functor(Impl2d
const & parent,
geom::Point2D const & point) :
306 _radius(point.asEigen().norm()),
307 _radiusAccel(makeGslPtr(::gsl_interp_accel_alloc(), &gsl_interp_accel_free)),
308 _wavelengthAccel(makeGslPtr(::gsl_interp_accel_alloc(), &gsl_interp_accel_free))
310 _radius =
std::max(_radius, parent._radii.front());
311 _radius =
std::min(_radius, parent._radii.back());
314 double operator()(Impl2d
const & parent,
double wavelength) {
316 int status = ::gsl_interp2d_eval_e(parent._interp.get(), parent._wavelengths.getData(),
317 parent._radii.getData(), parent._throughput.getData(),
319 _radiusAccel.get(), _wavelengthAccel.get(),
327 GslPtr<::gsl_interp_accel> _radiusAccel;
328 GslPtr<::gsl_interp_accel> _wavelengthAccel;
332 ndarray::Array<double,1,1> _throughput;
333 ndarray::Array<double,1,1> _wavelengths;
334 ndarray::Array<double,1,1>
_radii;
335 GslPtr<::gsl_interp2d> _interp;
339 template <
typename Impl>
340 class InterpolatedTransmissionCurve :
public TransmissionCurve {
344 _atBounds(throughputAtBounds),
349 pex::exceptions::InvalidParameterError,
350 "Throughput values at bounds must be finite" 356 return _impl.getWavelengthBounds();
365 ndarray::Array<double const,1,1>
const & wavelengths,
366 ndarray::Array<double,1,1>
const & out
369 wavelengths.getSize<0>(), out.getSize<0>(),
370 pex::exceptions::LengthError,
371 "Length of wavelength array (%d) does not match size of output array (%d)" 373 typename Impl::Functor
functor(_impl, point);
374 auto bounds = _impl.getWavelengthBounds();
375 auto wlIter = wavelengths.begin();
376 for (
auto outIter = out.begin(); outIter != out.end(); ++outIter, ++wlIter) {
377 double & y = *outIter;
378 if (*wlIter < bounds.first) {
380 }
else if (*wlIter > bounds.second) {
381 y = _atBounds.second;
383 y = functor(_impl, *wlIter);
388 bool isPersistable()
const override {
return true; }
395 if (_impl.isSpatiallyConstant) {
396 return shared_from_this();
398 return TransmissionCurve::_transformedByImpl(transform);
402 std::string getPersistenceName()
const override {
return Impl::NAME; }
404 struct PersistenceHelper {
410 static PersistenceHelper
const &
get() {
411 static PersistenceHelper
const instance;
417 PersistenceHelper() :
420 "throughputAtMin",
"throughput below minimum wavelength")),
422 "throughputAtMax",
"throughput above minimum wavelength"))
424 Impl::setupPersistence(schema, arrays);
425 schema.getCitizen().markPersistent();
430 void write(OutputArchiveHandle& handle)
const override {
431 auto const & keys = PersistenceHelper::get();
432 auto catalog = handle.makeCatalog(keys.schema);
433 auto record = catalog.addNew();
434 record->set(keys.throughputAtMin, _atBounds.first);
435 record->set(keys.throughputAtMax, _atBounds.second);
436 _impl.persist(*record, keys.arrays);
437 handle.saveCatalog(catalog);
440 class Factory :
public table::io::PersistableFactory {
444 InputArchive
const& archive,
445 CatalogVector
const& catalogs
447 auto const& keys = PersistenceHelper::get();
450 auto & record = catalogs.front().front();
451 return std::make_shared<InterpolatedTransmissionCurve>(
452 Impl::unpersist(record, keys.arrays),
453 std::make_pair(record.get(keys.throughputAtMin), record.get(keys.throughputAtMax))
457 Factory() : table::io::PersistableFactory(Impl::NAME) {}
467 template <
typename Impl>
470 template class InterpolatedTransmissionCurve<Impl1d>;
471 template class InterpolatedTransmissionCurve<Impl2d>;
480 class ProductTransmissionCurve :
public TransmissionCurve {
483 static constexpr
char const * NAME =
"ProductTransmissionCurve";
485 ProductTransmissionCurve(
491 auto aWavelengthBounds = _a->getWavelengthBounds();
492 auto bWavelengthBounds = _b->getWavelengthBounds();
493 auto aThroughputAtBounds = _a->getThroughputAtBounds();
494 auto bThroughputAtBounds = _b->getThroughputAtBounds();
496 auto determineWavelengthBound = [](
497 double aWavelength,
double bWavelength,
498 double aThroughput,
double bThroughput,
504 if (isFirstOuter(aWavelength, bWavelength)) {
505 return (bThroughput == 0.0) ? bWavelength : aWavelength;
507 return (aThroughput == 0.0) ? aWavelength : bWavelength;
512 determineWavelengthBound(aWavelengthBounds.first, bWavelengthBounds.first,
513 aThroughputAtBounds.first, bThroughputAtBounds.first,
515 determineWavelengthBound(aWavelengthBounds.second, bWavelengthBounds.second,
516 aThroughputAtBounds.second, bThroughputAtBounds.second,
522 auto aAtBounds = _a->getThroughputAtBounds();
523 auto bAtBounds = _b->getThroughputAtBounds();
525 aAtBounds.second * bAtBounds.second);
530 ndarray::Array<double const,1,1>
const & wavelengths,
531 ndarray::Array<double,1,1>
const & out
533 _a->sampleAt(position, wavelengths, out);
534 ndarray::Array<double,1,1> tmp = ndarray::allocate(wavelengths.getSize<0>());
535 _b->sampleAt(position, wavelengths, tmp);
539 bool isPersistable()
const override {
return _a->isPersistable() && _b->isPersistable(); }
543 std::string getPersistenceName()
const override {
return NAME; }
545 struct PersistenceHelper {
550 static PersistenceHelper
const &
get() {
551 static PersistenceHelper
const instance;
557 PersistenceHelper() :
559 a(schema.addField<
int>(
"a",
"archive ID of first operand")),
560 b(schema.addField<
int>(
"b",
"archive ID of second operand"))
562 schema.getCitizen().markPersistent();
567 class Factory :
public table::io::PersistableFactory {
571 InputArchive
const& archive,
572 CatalogVector
const& catalogs
574 auto const& keys = PersistenceHelper::get();
577 auto const & record = catalogs.front().front();
578 return std::make_shared<ProductTransmissionCurve>(
579 archive.get<TransmissionCurve>(record.get(keys.a)),
580 archive.get<TransmissionCurve>(record.get(keys.b))
584 Factory() : table::io::PersistableFactory(NAME) {}
587 void write(OutputArchiveHandle & handle)
const {
588 auto const & keys = PersistenceHelper::get();
589 auto catalog = handle.makeCatalog(keys.schema);
590 auto record = catalog.addNew();
591 record->set(keys.a, handle.put(_a));
592 record->set(keys.b, handle.put(_b));
593 handle.saveCatalog(catalog);
612 class TransformedTransmissionCurve :
public TransmissionCurve {
615 static constexpr
char const * NAME =
"TransformedTransmissionCurve";
617 TransformedTransmissionCurve(
623 return _nested->getWavelengthBounds();
627 return _nested->getThroughputAtBounds();
632 ndarray::Array<double const,1,1>
const & wavelengths,
633 ndarray::Array<double,1,1>
const & out
635 return _nested->sampleAt(_transform->applyInverse(position), wavelengths, out);
638 bool isPersistable()
const override {
return _nested->isPersistable() && _transform->isPersistable(); }
646 return std::make_shared<TransformedTransmissionCurve>(
648 transform->then(*_transform)
652 std::string getPersistenceName()
const override {
return NAME; }
654 struct PersistenceHelper {
659 static PersistenceHelper
const &
get() {
660 static PersistenceHelper
const instance;
666 PersistenceHelper() :
668 nested(schema.addField<
int>(
"nested",
"archive ID of the nested TransmissionCurve")),
669 transform(schema.addField<
int>(
"transform",
"archive ID of the coordinate transform"))
671 schema.getCitizen().markPersistent();
676 void write(OutputArchiveHandle & handle)
const {
677 auto const & keys = PersistenceHelper::get();
678 auto catalog = handle.makeCatalog(keys.schema);
679 auto record = catalog.addNew();
680 record->set(keys.nested, handle.put(_nested));
681 record->set(keys.transform, handle.put(_transform));
682 handle.saveCatalog(catalog);
685 class Factory :
public table::io::PersistableFactory {
689 InputArchive
const& archive,
690 CatalogVector
const& catalogs
692 auto const& keys = PersistenceHelper::get();
695 auto const & record = catalogs.front().front();
696 return std::make_shared<TransformedTransmissionCurve>(
697 archive.get<TransmissionCurve>(record.get(keys.nested)),
702 Factory() : table::io::PersistableFactory(NAME) {}
723 return IdentityTransmissionCurve::get();
727 ndarray::Array<double const,1>
const & throughput,
728 ndarray::Array<double const,1>
const & wavelengths,
731 ::gsl_set_error_handler_off();
733 wavelengths.getSize<0>(), throughput.getSize<0>(),
735 "Length of wavelength array (%d) does not match size of throughput array (%d)" 737 return std::make_shared<InterpolatedTransmissionCurve<Impl1d>>(
738 Impl1d(ndarray::copy(throughput), ndarray::copy(wavelengths)),
744 ndarray::Array<double const,2>
const & throughput,
745 ndarray::Array<double const,1>
const & wavelengths,
746 ndarray::Array<double const,1>
const & radii,
749 ::gsl_set_error_handler_off();
751 wavelengths.getSize<0>(), throughput.getSize<0>(),
753 "Length of wavelength array (%d) does not match first dimension of of throughput array (%d)" 756 radii.getSize<0>(), throughput.getSize<1>(),
758 "Length of radii array (%d) does not match second dimension of of throughput array (%d)" 763 ndarray::Array<double,2,2> throughputTransposed = ndarray::allocate(throughput.getShape().reverse());
764 throughputTransposed.transpose() = throughput;
765 ndarray::Array<double,1,1> throughputFlat = ndarray::flatten<1>(throughputTransposed);
766 return std::make_shared<InterpolatedTransmissionCurve<Impl2d>>(
767 Impl2d(throughputFlat, ndarray::copy(wavelengths), ndarray::copy(radii)),
775 auto a = shared_from_this();
777 auto result =
a->_multipliedByImpl(
b);
778 if (result ==
nullptr) {
779 result =
b->_multipliedByImpl(
a);
781 if (result ==
nullptr) {
790 return _transformedByImpl(
std::move(transform));
793 ndarray::Array<double,1,1> TransmissionCurve::sampleAt(
795 ndarray::Array<double const,1,1>
const & wavelengths
797 ndarray::Array<double,1,1> out = ndarray::allocate(wavelengths.getSize<0>());
798 sampleAt(position, wavelengths, out);
805 return std::make_shared<TransformedTransmissionCurve>(shared_from_this(),
std::move(transform));
815 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
table::Key< double > throughputAtMin
Transform< Point2Endpoint, Point2Endpoint > TransformPoint2ToPoint2
A base class for image defects.
Point< double, 2 > Point2D
#define LSST_CHECK_GSL(type, status)
#define LSST_EXCEPT(type,...)