LSSTApplications  16.0-11-g09ed895+2,16.0-11-g12e47bd,16.0-11-g9bb73b2+6,16.0-12-g5c924a4+6,16.0-14-g9a974b3+1,16.0-15-g1417920+1,16.0-15-gdd5ca33+1,16.0-16-gf0259e2,16.0-17-g31abd91+7,16.0-17-g7d7456e+7,16.0-17-ga3d2e9f+13,16.0-18-ga4d4bcb+1,16.0-18-gd06566c+1,16.0-2-g0febb12+21,16.0-2-g9d5294e+69,16.0-2-ga8830df+6,16.0-20-g21842373+7,16.0-24-g3eae5ec,16.0-28-gfc9ea6c+4,16.0-29-ge8801f9,16.0-3-ge00e371+34,16.0-4-g18f3627+13,16.0-4-g5f3a788+20,16.0-4-ga3eb747+10,16.0-4-gabf74b7+29,16.0-4-gb13d127+6,16.0-49-g42e581f7+6,16.0-5-g27fb78a+7,16.0-5-g6a53317+34,16.0-5-gb3f8a4b+87,16.0-6-g9321be7+4,16.0-6-gcbc7b31+42,16.0-6-gf49912c+29,16.0-7-gd2eeba5+51,16.0-71-ge89f8615e,16.0-8-g21fd5fe+29,16.0-8-g3a9f023+20,16.0-8-g4734f7a+1,16.0-8-g5858431+3,16.0-9-gf5c1f43+8,master-gd73dc1d098+1,w.2019.01
LSSTDataManagementBasePackage
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) {
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 {
78  std::shared_ptr<PeakTable> table = PeakTable::make(mapper.finalize());
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 
93 PeakRecord::PeakRecord(std::shared_ptr<PeakTable> const& table) : BaseRecord(table) {}
94 
96  return os << (boost::format("%d: (%d,%d) (%.3f,%.3f)") % record.getId() % record.getIx() %
97  record.getIy() % record.getFx() % record.getFy());
98 }
99 
101  typedef std::list<std::weak_ptr<PeakTable> > CachedTableList;
102  static CachedTableList cache;
103  if (!checkSchema(schema)) {
105  "Schema for Peak must contain at least the keys defined by makeMinimalSchema().");
106  }
107  if (forceNewTable) {
109  }
110  CachedTableList::iterator iter = cache.begin();
111  while (iter != cache.end()) {
112  std::shared_ptr<PeakTable> p = iter->lock();
113  if (!p) {
114  iter = cache.erase(iter);
115  } else {
116  if (p->getSchema().compare(schema, afw::table::Schema::IDENTICAL) ==
118  // Move the one we found to the front of the list, so it's easier to find
119  // the same thing repeatedly
120  if (iter != cache.begin()) {
121  cache.splice(cache.begin(), cache, iter);
122  }
123  return p;
124  }
125  ++iter;
126  }
127  }
128  // No match: we create a new table and put it in the cache
130  cache.push_front(newTable);
131  return newTable;
132 }
133 
136  : afw::table::BaseTable(schema), _idFactory(idFactory) {}
137 
139  : afw::table::BaseTable(other),
140  _idFactory(other._idFactory ? other._idFactory->clone() : other._idFactory) {}
141 // Delegate to copy-constructor for backwards-compatibility
143 
144 PeakTable::~PeakTable() = default;
145 
146 PeakTable::MinimalSchema::MinimalSchema() {
147  id = schema.addField<afw::table::RecordId>("id", "unique ID");
148  fx = schema.addField<float>("f_x", "subpixel column position", "pixel");
149  fy = schema.addField<float>("f_y", "subpixel row position", "pixel");
150  ix = schema.addField<int>("i_x", "column position of highest pixel", "pixel");
151  iy = schema.addField<int>("i_y", "row position of highest pixel", "pixel");
152  peakValue = schema.addField<float>("peakValue", "value of [smoothed] image at peak position", "count");
153  schema.getCitizen().markPersistent();
154 }
155 
156 PeakTable::MinimalSchema& PeakTable::getMinimalSchema() {
157  static MinimalSchema it;
158  return it;
159 }
160 
161 std::shared_ptr<afw::table::io::FitsWriter> PeakTable::makeFitsWriter(fits::Fits* fitsfile, int flags) const {
162  return std::make_shared<PeakFitsWriter>(fitsfile, flags);
163 }
164 
166  return std::shared_ptr<PeakTable>(new PeakTable(*this));
167 }
168 
170  std::shared_ptr<PeakRecord> record(new PeakRecord(getSelf<PeakTable>()));
171  if (getIdFactory()) record->setId((*getIdFactory())());
172  return record;
173 }
174 
175 } // namespace detection
176 
177 namespace table {
178 
179 template class CatalogT<afw::detection::PeakRecord>;
180 template class CatalogT<afw::detection::PeakRecord const>;
181 } // namespace table
182 } // namespace afw
183 } // namespace lsst
Defines the fields and offsets for a table.
Definition: Schema.h:50
int getIy() const
Convenience accessors for the keys in the minimal schema.
Definition: Peak.h:221
friend std::ostream & operator<<(std::ostream &os, BaseRecord const &record)
Write the record&#39;s content out, one field on each line.
Definition: BaseRecord.cc:101
std::shared_ptr< afw::table::IdFactory > getIdFactory()
Return the object that generates IDs for the table (may be null).
Definition: Peak.h:140
std::shared_ptr< PeakTable > clone() const
Return a polymorphic deep copy of the table.
Definition: Peak.h:163
PeakRecord(PeakRecord const &)=delete
Table class for Peaks in Footprints.
Definition: Peak.h:92
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:100
std::shared_ptr< afw::table::BaseRecord > _makeRecord() override
Default-construct an associated record (protected implementation).
Definition: Peak.cc:169
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:296
Fits * fits
Definition: FitsWriter.cc:90
afw::table::RecordId getId() const
Convenience accessors for the keys in the minimal schema.
Definition: Peak.h:217
PeakTable(afw::table::Schema const &schema, std::shared_ptr< afw::table::IdFactory > const &idFactory)
Definition: Peak.cc:134
A base class for image defects.
Definition: cameraGeom.dox:3
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:129
table::Schema schema
Definition: Camera.cc:161
BaseTable(Schema const &schema)
Construct from a schema.
Definition: BaseTable.cc:149
T dynamic_pointer_cast(T... args)
int getIx() const
Convenience accessors for the keys in the minimal schema.
Definition: Peak.h:220
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
STL class.
virtual void _writeTable(std::shared_ptr< BaseTable const > const &table, std::size_t nRows)
Write a table and its schema.
Definition: FitsWriter.cc:103
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Reports invalid arguments.
Definition: Runtime.h:66
ItemVariant const * other
Definition: Schema.cc:56
static std::shared_ptr< IdFactory > makeSimple()
Return a simple IdFactory that simply counts from 1.
Definition: IdFactory.cc:70
std::shared_ptr< afw::table::BaseTable > _clone() const override
Clone implementation with noncovariant return types.
Definition: Peak.cc:165
Record class that represents a peak in a Footprint.
Definition: Peak.h:42
STL class.
float getFy() const
Convenience accessors for the keys in the minimal schema.
Definition: Peak.h:226
std::ostream * os
Definition: Schema.cc:746
SchemaMapper * mapper
Definition: SchemaMapper.cc:78
Everything is the same.
Definition: Schema.h:71
float getFx() const
Convenience accessors for the keys in the minimal schema.
Definition: Peak.h:225