LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
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  */
23 #include <typeinfo>
24 
28 
29 namespace lsst {
30 namespace afw {
31 namespace detection {
32 
33 //-----------------------------------------------------------------------------------------------------------
34 //----- PeakFitsWriter ---------------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------------------------------------
36 
37 // A custom FitsWriter for Peak - this just sets the AFW_TYPE key to PEAK, which should ensure
38 // we use PeakFitsReader to read it.
39 
40 namespace {
41 
42 class PeakFitsWriter : public afw::table::io::FitsWriter {
43 public:
44  explicit PeakFitsWriter(Fits* fits, int flags) : afw::table::io::FitsWriter(fits, flags) {}
45 
46 protected:
47  void _writeTable(std::shared_ptr<afw::table::BaseTable const> const& table, std::size_t nRows) override;
48 };
49 
50 void PeakFitsWriter::_writeTable(std::shared_ptr<afw::table::BaseTable const> const& t, std::size_t nRows) {
51  std::shared_ptr<PeakTable const> table = std::dynamic_pointer_cast<PeakTable const>(t);
52  if (!table) {
54  "Cannot use a PeakFitsWriter on a non-Peak table.");
55  }
57  _fits->writeKey("AFW_TYPE", "PEAK", "Tells lsst::afw to load this as a Peak table.");
58 }
59 
60 } // namespace
61 
62 //-----------------------------------------------------------------------------------------------------------
63 //----- PeakFitsReader ---------------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------------------------------------
65 
66 // A custom FitsReader for PeakTable/Record - this gets registered with name SIMPLE, so it should get used
67 // whenever we read a table with AFW_TYPE set to that value.
68 
69 namespace {
70 
71 class PeakFitsReader : public afw::table::io::FitsReader {
72 public:
73  PeakFitsReader() : afw::table::io::FitsReader("PEAK") {}
74 
75  std::shared_ptr<afw::table::BaseTable> makeTable(afw::table::io::FitsSchemaInputMapper& mapper,
77  int ioFlags, bool stripMetadata) const override {
79  table->setMetadata(metadata);
80  return table;
81  }
82 };
83 
84 // registers the reader so FitsReader::make can use it.
85 static PeakFitsReader const peakFitsReader;
86 
87 } // namespace
88 
89 //-----------------------------------------------------------------------------------------------------------
90 //----- PeakTable/Record member function implementations -----------------------------------------------
91 //-----------------------------------------------------------------------------------------------------------
92 
94  return os << (boost::format("%d: (%d,%d) (%.3f,%.3f)") % record.getId() % record.getIx() %
95  record.getIy() % record.getFx() % record.getFy());
96 }
97 
99  typedef std::list<std::weak_ptr<PeakTable> > CachedTableList;
100  static CachedTableList cache;
101  if (!checkSchema(schema)) {
103  "Schema for Peak must contain at least the keys defined by makeMinimalSchema().");
104  }
105  if (forceNewTable) {
107  }
108  CachedTableList::iterator iter = cache.begin();
109  while (iter != cache.end()) {
110  std::shared_ptr<PeakTable> p = iter->lock();
111  if (!p) {
112  iter = cache.erase(iter);
113  } else {
114  if (p->getSchema().compare(schema, afw::table::Schema::IDENTICAL) ==
116  // Move the one we found to the front of the list, so it's easier to find
117  // the same thing repeatedly
118  if (iter != cache.begin()) {
119  cache.splice(cache.begin(), cache, iter);
120  }
121  return p;
122  }
123  ++iter;
124  }
125  }
126  // No match: we create a new table and put it in the cache
128  cache.push_front(newTable);
129  return newTable;
130 }
131 
134  : afw::table::BaseTable(schema), _idFactory(idFactory) {}
135 
137  : afw::table::BaseTable(other),
138  _idFactory(other._idFactory ? other._idFactory->clone() : other._idFactory) {}
139 // Delegate to copy-constructor for backwards-compatibility
141 
142 PeakTable::~PeakTable() = default;
143 
144 PeakTable::MinimalSchema::MinimalSchema() {
145  id = schema.addField<afw::table::RecordId>("id", "unique ID");
146  fx = schema.addField<float>("f_x", "subpixel column position", "pixel");
147  fy = schema.addField<float>("f_y", "subpixel row position", "pixel");
148  ix = schema.addField<int>("i_x", "column position of highest pixel", "pixel");
149  iy = schema.addField<int>("i_y", "row position of highest pixel", "pixel");
150  peakValue = schema.addField<float>("peakValue", "value of [smoothed] image at peak position", "count");
151 }
152 
153 PeakTable::MinimalSchema& PeakTable::getMinimalSchema() {
154  static MinimalSchema it;
155  return it;
156 }
157 
158 std::shared_ptr<afw::table::io::FitsWriter> PeakTable::makeFitsWriter(fits::Fits* fitsfile, int flags) const {
159  return std::make_shared<PeakFitsWriter>(fitsfile, flags);
160 }
161 
163  return std::shared_ptr<PeakTable>(new PeakTable(*this));
164 }
165 
167  auto record = constructRecord<PeakRecord>();
168  if (getIdFactory()) record->setId((*getIdFactory())());
169  return record;
170 }
171 
172 } // namespace detection
173 
174 namespace table {
175 
176 template class CatalogT<afw::detection::PeakRecord>;
177 template class CatalogT<afw::detection::PeakRecord const>;
178 } // namespace table
179 } // namespace afw
180 } // namespace lsst
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Fits * fits
Definition: FitsWriter.cc:90
ItemVariant const * other
Definition: Schema.cc:56
std::ostream * os
Definition: Schema.cc:746
SchemaMapper * mapper
Definition: SchemaMapper.cc:78
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:162
PeakTable(afw::table::Schema const &schema, std::shared_ptr< afw::table::IdFactory > const &idFactory)
Definition: Peak.cc:132
std::shared_ptr< afw::table::BaseRecord > _makeRecord() override
Default-construct an associated record (protected implementation).
Definition: Peak.cc:166
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:98
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:50
@ IDENTICAL
Everything is the same.
Definition: Schema.h:71
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:93
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
A base class for image defects.