LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Peak.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2014 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
26 
27 namespace lsst {
28 namespace afw {
29 namespace detection {
30 
31 //-----------------------------------------------------------------------------------------------------------
32 //----- PeakFitsWriter ---------------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------------------------------------
34 
35 // A custom FitsWriter for Peak - this just sets the AFW_TYPE key to PEAK, which should ensure
36 // we use PeakFitsReader to read it.
37 
38 namespace {
39 
40 class PeakFitsWriter : public afw::table::io::FitsWriter {
41 public:
42  explicit PeakFitsWriter(Fits* fits, int flags) : afw::table::io::FitsWriter(fits, flags) {}
43 
44 protected:
45  void _writeTable(std::shared_ptr<afw::table::BaseTable const> const& table, std::size_t nRows) override;
46 };
47 
48 void PeakFitsWriter::_writeTable(std::shared_ptr<afw::table::BaseTable const> const& t, std::size_t nRows) {
49  std::shared_ptr<PeakTable const> table = std::dynamic_pointer_cast<PeakTable const>(t);
50  if (!table) {
52  "Cannot use a PeakFitsWriter on a non-Peak table.");
53  }
55  _fits->writeKey("AFW_TYPE", "PEAK", "Tells lsst::afw to load this as a Peak table.");
56 }
57 
58 } // namespace
59 
60 //-----------------------------------------------------------------------------------------------------------
61 //----- PeakFitsReader ---------------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------------------------------------
63 
64 // A custom FitsReader for PeakTable/Record - this gets registered with name SIMPLE, so it should get used
65 // whenever we read a table with AFW_TYPE set to that value.
66 
67 namespace {
68 
69 class PeakFitsReader : public afw::table::io::FitsReader {
70 public:
71  PeakFitsReader() : afw::table::io::FitsReader("PEAK") {}
72 
73  std::shared_ptr<afw::table::BaseTable> makeTable(afw::table::io::FitsSchemaInputMapper& mapper,
75  int ioFlags, bool stripMetadata) const override {
77  table->setMetadata(metadata);
78  return table;
79  }
80 };
81 
82 // registers the reader so FitsReader::make can use it.
83 static PeakFitsReader const peakFitsReader;
84 
85 } // namespace
86 
87 //-----------------------------------------------------------------------------------------------------------
88 //----- PeakTable/Record member function implementations -----------------------------------------------
89 //-----------------------------------------------------------------------------------------------------------
90 
92  return os << (boost::format("%d: (%d,%d) (%.3f,%.3f)") % record.getId() % record.getIx() %
93  record.getIy() % record.getFx() % record.getFy());
94 }
95 
97  using CachedTableList = std::list<std::weak_ptr<PeakTable>>;
98  static CachedTableList cache;
99  if (!checkSchema(schema)) {
101  "Schema for Peak must contain at least the keys defined by makeMinimalSchema().");
102  }
103  if (forceNewTable) {
105  }
106  CachedTableList::iterator iter = cache.begin();
107  while (iter != cache.end()) {
108  std::shared_ptr<PeakTable> p = iter->lock();
109  if (!p) {
110  iter = cache.erase(iter);
111  } else {
112  if (p->getSchema().compare(schema, afw::table::Schema::IDENTICAL) ==
114  // Move the one we found to the front of the list, so it's easier to find
115  // the same thing repeatedly
116  if (iter != cache.begin()) {
117  cache.splice(cache.begin(), cache, iter);
118  }
119  return p;
120  }
121  ++iter;
122  }
123  }
124  // No match: we create a new table and put it in the cache
126  cache.push_front(newTable);
127  return newTable;
128 }
129 
132  : afw::table::BaseTable(schema), _idFactory(idFactory) {}
133 
135  : afw::table::BaseTable(other),
136  _idFactory(other._idFactory ? other._idFactory->clone() : other._idFactory) {}
137 // Delegate to copy-constructor for backwards-compatibility
139 
140 PeakTable::~PeakTable() = default;
141 
142 PeakTable::MinimalSchema::MinimalSchema() {
143  id = schema.addField<afw::table::RecordId>("id", "unique ID");
144  fx = schema.addField<float>("f_x", "subpixel column position", "pixel");
145  fy = schema.addField<float>("f_y", "subpixel row position", "pixel");
146  ix = schema.addField<int>("i_x", "column position of highest pixel", "pixel");
147  iy = schema.addField<int>("i_y", "row position of highest pixel", "pixel");
148  peakValue = schema.addField<float>("peakValue", "value of [smoothed] image at peak position", "count");
149 }
150 
151 PeakTable::MinimalSchema& PeakTable::getMinimalSchema() {
152  static MinimalSchema it;
153  return it;
154 }
155 
156 std::shared_ptr<afw::table::io::FitsWriter> PeakTable::makeFitsWriter(fits::Fits* fitsfile, int flags) const {
157  return std::make_shared<PeakFitsWriter>(fitsfile, flags);
158 }
159 
161  return std::shared_ptr<PeakTable>(new PeakTable(*this));
162 }
163 
165  auto record = constructRecord<PeakRecord>();
166  if (getIdFactory()) record->setId((*getIdFactory())());
167  return record;
168 }
169 
170 } // namespace detection
171 
172 namespace table {
173 
174 template class CatalogT<afw::detection::PeakRecord>;
175 template class CatalogT<afw::detection::PeakRecord const>;
176 } // namespace table
177 } // namespace afw
178 } // namespace lsst
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Fits * fits
Definition: FitsWriter.cc:90
std::ostream * os
Definition: Schema.cc:557
SchemaMapper * mapper
Definition: SchemaMapper.cc:71
table::Schema schema
Definition: python.h:134
Record class that represents a peak in a Footprint.
Definition: Peak.h:42
afw::table::RecordId getId() const
Convenience accessors for the keys in the minimal schema.
Definition: Peak.h:227
Table class for Peaks in Footprints.
Definition: Peak.h:102
std::shared_ptr< afw::table::BaseTable > _clone() const override
Clone implementation with noncovariant return types.
Definition: Peak.cc:160
PeakTable(afw::table::Schema const &schema, std::shared_ptr< afw::table::IdFactory > const &idFactory)
Definition: Peak.cc:130
std::shared_ptr< afw::table::BaseRecord > _makeRecord() override
Default-construct an associated record (protected implementation).
Definition: Peak.cc:164
static std::shared_ptr< PeakTable > make(afw::table::Schema const &schema, bool forceNew=false)
Obtain a table that can be used to create records with given schema.
Definition: Peak.cc:96
static bool checkSchema(afw::table::Schema const &other)
Return true if the given schema is a valid PeakTable schema.
Definition: Peak.h:145
std::shared_ptr< afw::table::IdFactory > getIdFactory()
Return the object that generates IDs for the table (may be null).
Definition: Peak.h:150
static std::shared_ptr< IdFactory > makeSimple()
Return a simple IdFactory that simply counts from 1.
Definition: IdFactory.cc:70
Defines the fields and offsets for a table.
Definition: Schema.h:51
@ IDENTICAL
Everything is the same.
Definition: Schema.h:72
virtual void _writeTable(std::shared_ptr< BaseTable const > const &table, std::size_t nRows)
Write a table and its schema.
Definition: FitsWriter.cc:103
Reports invalid arguments.
Definition: Runtime.h:66
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
std::ostream & operator<<(std::ostream &os, PeakRecord const &record)
Definition: Peak.cc:91
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
A base class for image defects.