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*)) {
157 return GslPtr<T>(p,
free);
169 static constexpr
bool isSpatiallyConstant =
true;
170 static constexpr
char const * NAME =
"SpatiallyConstantTransmissionCurve";
175 Impl1d(ndarray::Array<double,1,1>
const & throughput,
176 ndarray::Array<double,1,1>
const & wavelengths) :
177 _throughput(throughput),
178 _wavelengths(wavelengths),
179 _interp(makeGslPtr(::gsl_interp_alloc(::gsl_interp_linear, _wavelengths.size()),
182 int status = ::gsl_interp_init(_interp.get(), _wavelengths.getData(), _throughput.getData(),
183 _wavelengths.size());
191 static void setupPersistence(table::Schema &
schema, ArrayKeyVector & keys) {
193 schema.addField<table::Array<double>>(
194 "throughput",
"array of known throughput values",
"", 0
198 schema.addField<table::Array<double>>(
199 "wavelengths",
"array of known wavelength values",
"angstrom", 0
204 void persist(table::BaseRecord & record, ArrayKeyVector
const & keys)
const {
205 record.set(keys[0], _throughput);
206 record.set(keys[1], _wavelengths);
209 static Impl1d unpersist(table::BaseRecord & record, ArrayKeyVector
const & keys) {
210 return Impl1d(record[keys[0]], record[keys[1]]);
218 _accel(makeGslPtr(::gsl_interp_accel_alloc(), &gsl_interp_accel_free))
221 double operator()(Impl1d
const & parent,
double wavelength) {
223 int status = ::gsl_interp_eval_e(parent._interp.get(), parent._wavelengths.getData(),
224 parent._throughput.getData(),
225 wavelength, _accel.get(), &result);
231 GslPtr<::gsl_interp_accel> _accel;
235 ndarray::Array<double,1,1> _throughput;
236 ndarray::Array<double,1,1> _wavelengths;
237 GslPtr<::gsl_interp> _interp;
247 static constexpr
bool isSpatiallyConstant =
false;
248 static constexpr
char const * NAME =
"RadialTransmissionCurve";
250 Impl2d(ndarray::Array<double,1,1>
const & throughput,
251 ndarray::Array<double,1,1>
const & wavelengths,
252 ndarray::Array<double,1,1>
const & radii) :
253 _throughput(throughput),
254 _wavelengths(wavelengths),
256 _interp(makeGslPtr(::gsl_interp2d_alloc(::gsl_interp2d_bilinear, _wavelengths.size(),
258 &::gsl_interp2d_free))
260 int status = ::gsl_interp2d_init(_interp.get(), _wavelengths.getData(),
_radii.getData(),
261 _throughput.getData(), _wavelengths.size(),
_radii.size());
269 static void setupPersistence(table::Schema &
schema, ArrayKeyVector & keys) {
271 schema.addField<table::Array<double>>(
273 "flattenned 2-d array of known throughput values, with radius dimension fastest",
"", 0
277 schema.addField<table::Array<double>>(
278 "wavelengths",
"array of known wavelength values",
"angstrom", 0
282 schema.addField<table::Array<double>>(
283 "radii",
"array of known radius values",
"", 0
288 void persist(table::BaseRecord & record, ArrayKeyVector
const & keys)
const {
289 record.set(keys[0], _throughput);
290 record.set(keys[1], _wavelengths);
291 record.set(keys[2],
_radii);
294 static Impl2d unpersist(table::BaseRecord & record, ArrayKeyVector
const & keys) {
295 return Impl2d(record[keys[0]], record[keys[1]], record[keys[2]]);
302 Functor(Impl2d
const & parent,
geom::Point2D const & point) :
303 _radius(point.asEigen().norm()),
304 _radiusAccel(makeGslPtr(::gsl_interp_accel_alloc(), &gsl_interp_accel_free)),
305 _wavelengthAccel(makeGslPtr(::gsl_interp_accel_alloc(), &gsl_interp_accel_free))
307 _radius =
std::max(_radius, parent._radii.front());
308 _radius =
std::min(_radius, parent._radii.back());
311 double operator()(Impl2d
const & parent,
double wavelength) {
313 int status = ::gsl_interp2d_eval_e(parent._interp.get(), parent._wavelengths.getData(),
314 parent._radii.getData(), parent._throughput.getData(),
316 _radiusAccel.get(), _wavelengthAccel.get(),
324 GslPtr<::gsl_interp_accel> _radiusAccel;
325 GslPtr<::gsl_interp_accel> _wavelengthAccel;
329 ndarray::Array<double,1,1> _throughput;
330 ndarray::Array<double,1,1> _wavelengths;
331 ndarray::Array<double,1,1>
_radii;
332 GslPtr<::gsl_interp2d> _interp;
336 template <
typename Impl>
337 class InterpolatedTransmissionCurve :
public TransmissionCurve {
341 _atBounds(throughputAtBounds),
346 pex::exceptions::InvalidParameterError,
347 "Throughput values at bounds must be finite" 353 return _impl.getWavelengthBounds();
362 ndarray::Array<double const,1,1>
const & wavelengths,
363 ndarray::Array<double,1,1>
const & out
366 wavelengths.getSize<0>(), out.getSize<0>(),
367 pex::exceptions::LengthError,
368 "Length of wavelength array (%d) does not match size of output array (%d)" 370 typename Impl::Functor
functor(_impl, point);
371 auto bounds = _impl.getWavelengthBounds();
372 auto wlIter = wavelengths.begin();
373 for (
auto outIter = out.begin(); outIter != out.end(); ++outIter, ++wlIter) {
374 double & y = *outIter;
375 if (*wlIter < bounds.first) {
377 }
else if (*wlIter > bounds.second) {
378 y = _atBounds.second;
380 y = functor(_impl, *wlIter);
385 bool isPersistable()
const override {
return true; }
392 if (_impl.isSpatiallyConstant) {
393 return shared_from_this();
395 return TransmissionCurve::_transformedByImpl(transform);
399 std::string getPersistenceName()
const override {
return Impl::NAME; }
401 struct PersistenceHelper {
407 static PersistenceHelper
const &
get() {
408 static PersistenceHelper
const instance;
414 PersistenceHelper() :
417 "throughputAtMin",
"throughput below minimum wavelength")),
419 "throughputAtMax",
"throughput above minimum wavelength"))
421 Impl::setupPersistence(schema, arrays);
422 schema.getCitizen().markPersistent();
427 void write(OutputArchiveHandle& handle)
const override {
428 auto const & keys = PersistenceHelper::get();
429 auto catalog = handle.makeCatalog(keys.schema);
430 auto record = catalog.addNew();
431 record->set(keys.throughputAtMin, _atBounds.first);
432 record->set(keys.throughputAtMax, _atBounds.second);
433 _impl.persist(*record, keys.arrays);
434 handle.saveCatalog(catalog);
437 class Factory :
public table::io::PersistableFactory {
441 InputArchive
const& archive,
442 CatalogVector
const& catalogs
444 auto const& keys = PersistenceHelper::get();
447 auto & record = catalogs.front().front();
448 return std::make_shared<InterpolatedTransmissionCurve>(
449 Impl::unpersist(record, keys.arrays),
450 std::make_pair(record.get(keys.throughputAtMin), record.get(keys.throughputAtMax))
454 Factory() : table::io::PersistableFactory(Impl::NAME) {}
464 template <
typename Impl>
467 template class InterpolatedTransmissionCurve<Impl1d>;
468 template class InterpolatedTransmissionCurve<Impl2d>;
477 class ProductTransmissionCurve :
public TransmissionCurve {
480 static constexpr
char const * NAME =
"ProductTransmissionCurve";
482 ProductTransmissionCurve(
488 auto aWavelengthBounds = _a->getWavelengthBounds();
489 auto bWavelengthBounds = _b->getWavelengthBounds();
490 auto aThroughputAtBounds = _a->getThroughputAtBounds();
491 auto bThroughputAtBounds = _b->getThroughputAtBounds();
493 auto determineWavelengthBound = [](
494 double aWavelength,
double bWavelength,
495 double aThroughput,
double bThroughput,
501 if (isFirstOuter(aWavelength, bWavelength)) {
502 return (bThroughput == 0.0) ? bWavelength : aWavelength;
504 return (aThroughput == 0.0) ? aWavelength : bWavelength;
509 determineWavelengthBound(aWavelengthBounds.first, bWavelengthBounds.first,
510 aThroughputAtBounds.first, bThroughputAtBounds.first,
512 determineWavelengthBound(aWavelengthBounds.second, bWavelengthBounds.second,
513 aThroughputAtBounds.second, bThroughputAtBounds.second,
519 auto aAtBounds = _a->getThroughputAtBounds();
520 auto bAtBounds = _b->getThroughputAtBounds();
522 aAtBounds.second * bAtBounds.second);
527 ndarray::Array<double const,1,1>
const & wavelengths,
528 ndarray::Array<double,1,1>
const & out
530 _a->sampleAt(position, wavelengths, out);
531 ndarray::Array<double,1,1> tmp = ndarray::allocate(wavelengths.getSize<0>());
532 _b->sampleAt(position, wavelengths, tmp);
536 bool isPersistable()
const override {
return _a->isPersistable() && _b->isPersistable(); }
540 std::string getPersistenceName()
const override {
return NAME; }
542 struct PersistenceHelper {
547 static PersistenceHelper
const &
get() {
548 static PersistenceHelper
const instance;
554 PersistenceHelper() :
556 a(schema.addField<
int>(
"a",
"archive ID of first operand")),
557 b(schema.addField<
int>(
"b",
"archive ID of second operand"))
559 schema.getCitizen().markPersistent();
564 class Factory :
public table::io::PersistableFactory {
568 InputArchive
const& archive,
569 CatalogVector
const& catalogs
571 auto const& keys = PersistenceHelper::get();
574 auto const & record = catalogs.front().front();
575 return std::make_shared<ProductTransmissionCurve>(
576 archive.get<TransmissionCurve>(record.get(keys.a)),
577 archive.get<TransmissionCurve>(record.get(keys.b))
581 Factory() : table::io::PersistableFactory(NAME) {}
584 void write(OutputArchiveHandle & handle)
const override {
585 auto const & keys = PersistenceHelper::get();
586 auto catalog = handle.makeCatalog(keys.schema);
587 auto record = catalog.addNew();
588 record->set(keys.a, handle.put(_a));
589 record->set(keys.b, handle.put(_b));
590 handle.saveCatalog(catalog);
609 class TransformedTransmissionCurve :
public TransmissionCurve {
612 static constexpr
char const * NAME =
"TransformedTransmissionCurve";
614 TransformedTransmissionCurve(
620 return _nested->getWavelengthBounds();
624 return _nested->getThroughputAtBounds();
629 ndarray::Array<double const,1,1>
const & wavelengths,
630 ndarray::Array<double,1,1>
const & out
632 return _nested->sampleAt(_transform->applyInverse(position), wavelengths, out);
635 bool isPersistable()
const override {
return _nested->isPersistable() && _transform->isPersistable(); }
643 return std::make_shared<TransformedTransmissionCurve>(
645 transform->then(*_transform)
649 std::string getPersistenceName()
const override {
return NAME; }
651 struct PersistenceHelper {
656 static PersistenceHelper
const &
get() {
657 static PersistenceHelper
const instance;
663 PersistenceHelper() :
665 nested(schema.addField<
int>(
"nested",
"archive ID of the nested TransmissionCurve")),
666 transform(schema.addField<
int>(
"transform",
"archive ID of the coordinate transform"))
668 schema.getCitizen().markPersistent();
673 void write(OutputArchiveHandle & handle)
const override {
674 auto const & keys = PersistenceHelper::get();
675 auto catalog = handle.makeCatalog(keys.schema);
676 auto record = catalog.addNew();
677 record->set(keys.nested, handle.put(_nested));
678 record->set(keys.transform, handle.put(_transform));
679 handle.saveCatalog(catalog);
682 class Factory :
public table::io::PersistableFactory {
686 InputArchive
const& archive,
687 CatalogVector
const& catalogs
689 auto const& keys = PersistenceHelper::get();
692 auto const & record = catalogs.front().front();
693 return std::make_shared<TransformedTransmissionCurve>(
694 archive.get<TransmissionCurve>(record.get(keys.nested)),
699 Factory() : table::io::PersistableFactory(NAME) {}
720 return IdentityTransmissionCurve::get();
724 ndarray::Array<double const,1>
const & throughput,
725 ndarray::Array<double const,1>
const & wavelengths,
728 ::gsl_set_error_handler_off();
730 wavelengths.getSize<0>(), throughput.getSize<0>(),
732 "Length of wavelength array (%d) does not match size of throughput array (%d)" 734 return std::make_shared<InterpolatedTransmissionCurve<Impl1d>>(
735 Impl1d(ndarray::copy(throughput), ndarray::copy(wavelengths)),
741 ndarray::Array<double const,2>
const & throughput,
742 ndarray::Array<double const,1>
const & wavelengths,
743 ndarray::Array<double const,1>
const & radii,
746 ::gsl_set_error_handler_off();
748 wavelengths.getSize<0>(), throughput.getSize<0>(),
750 "Length of wavelength array (%d) does not match first dimension of of throughput array (%d)" 753 radii.getSize<0>(), throughput.getSize<1>(),
755 "Length of radii array (%d) does not match second dimension of of throughput array (%d)" 760 ndarray::Array<double,2,2> throughputTransposed = ndarray::allocate(throughput.getShape().reverse());
761 throughputTransposed.transpose() = throughput;
762 ndarray::Array<double,1,1> throughputFlat = ndarray::flatten<1>(throughputTransposed);
763 return std::make_shared<InterpolatedTransmissionCurve<Impl2d>>(
764 Impl2d(throughputFlat, ndarray::copy(wavelengths), ndarray::copy(radii)),
772 auto a = shared_from_this();
774 auto result =
a->_multipliedByImpl(
b);
775 if (result ==
nullptr) {
776 result =
b->_multipliedByImpl(
a);
778 if (result ==
nullptr) {
787 return _transformedByImpl(
std::move(transform));
790 ndarray::Array<double,1,1> TransmissionCurve::sampleAt(
792 ndarray::Array<double const,1,1>
const & wavelengths
794 ndarray::Array<double,1,1> out = ndarray::allocate(wavelengths.getSize<0>());
795 sampleAt(position, wavelengths, out);
802 return std::make_shared<TransformedTransmissionCurve>(shared_from_this(),
std::move(transform));
812 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,...)