LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
psfImpl.cc
Go to the documentation of this file.
1 // -*- lsst-C++ -*-
2 #include "boost/format.hpp"
3 #include "lsst/pex/exceptions.h"
4 #include "lsst/meas/extensions/psfex/psf.hh"
5 #include "ndarray.h"
6 
7 #define RETURN_IMAGE_FIELD(NAME, CNAME, SIZE) \
8  ndarray::Array<float,2,2> \
9  NAME() const \
10  { \
11  ndarray::Array<float,2,2>::Index shape = ndarray::makeVector(SIZE[0], SIZE[1]); \
12  ndarray::Array<float,2,2>::Index strides = ndarray::makeVector(1, SIZE[0]); \
13  \
14  return ndarray::external(impl->CNAME, shape, strides); \
15  }
16 
17 namespace lsst { namespace meas { namespace extensions { namespace psfex {
18 
19 Context::Context(std::vector<std::string> const& names,
20  std::vector<int> const& group,
21  std::vector<int> const& degree,
22  bool pcexflag
23  )
24 {
25  _names = names;
26  int const ndim = names.size();
27  int const ngroup = group.size();
28 
30  for (int i = 0; i != ndim; ++i) {
31  cnames[i] = names[i].c_str();
32  }
33 
34  impl = context_init(const_cast<char **>(&cnames[0]),
35  const_cast<int *>(&group[0]),
36  ndim,
37  const_cast<int *>(&degree[0]),
38  ngroup, (pcexflag ? 1 : 0));
39  _pc_vectors.resize(impl->npc);
40 }
41 
42 Context::~Context() {
43  context_end(impl);
44 }
45 
46 std::vector<double> & Context::getPc(int const i) const
47 {
48  if (i < 0 || i >= impl->npc) {
50  s1 << "Index " << i << " is out of range 0.." << impl->npc - 1;
51  throw std::out_of_range(s1.str());
52  }
53  if (_pc_vectors[i].empty()) {
54  _pc_vectors[i].reserve(impl->npc);
55  for (int j = 0; j != impl->npc; ++j) {
56  _pc_vectors[i][j] = impl->pc[i*impl->npc + j];
57  }
58  }
59  return _pc_vectors[i];
60 }
61 
62 /************************************************************************************************************/
63 
64 void Sample::setVig(ndarray::Array<float,2,2> const& img)
65 {
66  int const w = img.getSize<0>(), h = img.getSize<1>();
67 
68  if (_vigsize[0]*_vigsize[1] != w*h) {
70  str(boost::format("Expected %dx%d array, but was passed %dx%d")
71  % _vigsize[0] % _vigsize[1] % w % h));
72  }
73 
74  ndarray::Array<float,1,1> flattened = ndarray::flatten<1>(img);
75  std::copy(flattened.begin(), flattened.end(), impl->vig);
76 }
77 
78 RETURN_IMAGE_FIELD(Sample::getVig, vig, _vigsize)
79 RETURN_IMAGE_FIELD(Sample::getVigResi, vigresi, _vigsize)
80 RETURN_IMAGE_FIELD(Sample::getVigChi, vigchi, _vigsize)
81 RETURN_IMAGE_FIELD(Sample::getVigWeight, vigweight, _vigsize)
82 
83 /************************************************************************************************************/
84 
85 Set::Set(Context &c) {
86  impl = init_set(c.impl);
87  impl->nsample = impl->nsamplemax = 0;
88 }
89 Set::~Set() {
90  end_set(impl);
91 }
92 
93 Sample
94 Set::newSample()
95 {
96  if (impl->nsample >= impl->nsamplemax) {
97  if (impl->nsamplemax == 0) {
98  malloc_samples(impl, LSAMPLE_DEFSIZE);
99  } else {
100  realloc_samples(impl, (int)(1.62*impl->nsamplemax + 1));
101  }
102  }
103  return Sample(impl->sample[impl->nsample], _recentroid, impl->vigsize);
104 }
105 
106 void
107 Set::trimMemory() const
108 {
109  setstruct *mutable_impl = const_cast<setstruct *>(impl);
110  realloc_samples(mutable_impl, impl->nsample);
111 }
112 
113 void
114 Set::finiSample(Sample const& sample)
115 {
116  ++impl->nsample;
117  make_weights(impl, sample.impl);
118  if (sample._recentroid) {
119  recenter_sample(sample.impl, impl, sample._fluxrad);
120  }
121 }
122 
123 /************************************************************************************************************/
124 
125 Psf::~Psf() {
126 #if 0 // we don't own impl
127  psf_end(impl);
128 #endif
129 }
130 
131 void
132 Psf::build(double x, double y,
133  std::vector<double> const& other)
134 {
135  if (!impl->contextoffset) { // we never set context{offset,scale}
136  throw std::out_of_range("psf.contextoffset not set (probably no valid stars) so I can't index it");
137  }
138 
139  std::vector<double> pos(2);
140  pos[0] = x; pos[1] = y;
141 
142  pos.insert(pos.end(), other.begin(), other.end());
143 
144  for (unsigned int i = 0; i != pos.size(); ++i) {
145  pos[i] = (pos[i] - impl->contextoffset[i])/impl->contextscale[i];
146  }
147 
148  psf_build(impl, &pos[0]);
149 }
150 
151 RETURN_IMAGE_FIELD(Psf::getLoc, loc, impl->size)
152 RETURN_IMAGE_FIELD(Psf::getResi, resi, impl->size)
153 
154 }}}}
y
int y
Definition: SpanSet.cc:49
std::vector< std::string >
std::vector::size
T size(T... args)
lsst.pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
degree
table::Key< table::Array< int > > degree
Definition: PsfexPsf.cc:360
RETURN_IMAGE_FIELD
#define RETURN_IMAGE_FIELD(NAME, CNAME, SIZE)
Definition: psfImpl.cc:7
x
double x
Definition: ChebyshevBoundedField.cc:277
lsst.pex::exceptions::LengthError
Reports attempts to exceed implementation-defined length limits for some classes.
Definition: Runtime.h:76
other
ItemVariant const * other
Definition: Schema.cc:56
std::copy
T copy(T... args)
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
group
table::Key< table::Array< int > > group
Definition: PsfexPsf.cc:359
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
std::ostringstream
STL class.
ndim
table::Key< int > ndim
Definition: PsfexPsf.cc:325
std::out_of_range
STL class.
std::ostringstream::str
T str(T... args)
w
double w
Definition: CoaddPsf.cc:69
ngroup
table::Key< int > ngroup
Definition: PsfexPsf.cc:326
exceptions.h