lsst.afw  13.0-100-ge0459d59f+2
Utils.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 //
26 //##====---------------- ----------------====##/
27 //
28 // Support for formatters
29 //
30 //##====---------------- ----------------====##/
31 
32 #include <cstdint>
33 #include <iostream>
34 #include <vector>
35 
36 #include "boost/format.hpp"
37 
38 #include "lsst/pex/exceptions.h"
39 #include "lsst/daf/base/PropertySet.h"
40 #include "lsst/daf/persistence/LogicalLocation.h"
41 #include "lsst/daf/persistence/DbTsvStorage.h"
43 
44 using std::int64_t;
45 namespace ex = lsst::pex::exceptions;
47 using lsst::pex::policy::Policy;
48 using lsst::daf::persistence::LogicalLocation;
49 
50 namespace lsst {
51 namespace afw {
52 namespace formatters {
53 namespace {
54 
67 std::string formatFitsPropertiesImpl(std::vector<std::string> const& paramNames,
68  daf::base::PropertySet const& prop) {
69  std::ostringstream result;
70  for (auto const & fullName : paramNames) {
71  std::size_t lastPeriod = fullName.rfind(char('.'));
72  auto name = (lastPeriod == std::string::npos) ? fullName : fullName.substr(lastPeriod + 1);
73  std::type_info const& type = prop.typeOf(name);
74 
75  std::string out = "";
76  out.reserve(80);
77  if (name.size() > 8) { // Oh dear; too long for a FITS keyword
78  out += "HIERARCH = " + name;
79  } else {
80  out = (boost::format("%-8s= ") % name).str();
81  }
82 
83  if (type == typeid(bool)) {
84  out += prop.get<bool>(name) ? "1" : "0";
85  } else if (type == typeid(int)) {
86  out += (boost::format("%20d") % prop.get<int>(name)).str();
87  } else if (type == typeid(double)) {
88  out += (boost::format("%20.15g") % prop.get<double>(name)).str();
89  } else if (type == typeid(float)) {
90  out += (boost::format("%20.15g") % prop.get<float>(name)).str();
91  } else if (type == typeid(std::string)) {
92  out += (boost::format("'%-67s' ") % prop.get<std::string>(name)).str();
93  }
94 
95  int const len = out.size();
96  if (len < 80) {
97  out += std::string(80 - len, ' ');
98  } else {
99  out = out.substr(0, 80);
100  }
101 
102  result << out;
103  }
104 
105  return result.str();
106 }
107 
108 } // namespace
109 
110 int extractSliceId(std::shared_ptr<PropertySet const> const& properties) {
111  if (properties->isArray("sliceId")) {
112  throw LSST_EXCEPT(ex::RuntimeError, "\"sliceId\" property has multiple values");
113  }
114  int sliceId = properties->getAsInt("sliceId");
115  if (sliceId < 0) {
116  throw LSST_EXCEPT(ex::RangeError, "negative \"sliceId\"");
117  }
118  if (properties->exists("universeSize") && !properties->isArray("universeSize")) {
119  int universeSize = properties->getAsInt("universeSize");
120  if (sliceId >= universeSize) {
121  throw LSST_EXCEPT(ex::RangeError, "\"sliceId\" must be less than \"universeSize \"");
122  }
123  }
124  return sliceId;
125 }
126 
127 int extractVisitId(std::shared_ptr<PropertySet const> const& properties) {
128  if (properties->isArray("visitId")) {
129  throw LSST_EXCEPT(ex::RuntimeError, "\"visitId\" property has multiple values");
130  }
131  int visitId = properties->getAsInt("visitId");
132  if (visitId < 0) {
133  throw LSST_EXCEPT(ex::RangeError, "negative \"visitId\"");
134  }
135  return visitId;
136 }
137 
138 int64_t extractFpaExposureId(std::shared_ptr<PropertySet const> const& properties) {
139  if (properties->isArray("fpaExposureId")) {
140  throw LSST_EXCEPT(ex::RuntimeError, "\"fpaExposureId\" property has multiple values");
141  }
142  int64_t fpaExposureId = properties->getAsInt64("fpaExposureId");
143  if (fpaExposureId < 0) {
144  throw LSST_EXCEPT(ex::RangeError, "negative \"fpaExposureId\"");
145  }
146  if ((fpaExposureId & 0xfffffffe00000000LL) != 0LL) {
147  throw LSST_EXCEPT(ex::RangeError, "\"fpaExposureId\" is too large");
148  }
149  return fpaExposureId;
150 }
151 
152 int extractCcdId(std::shared_ptr<PropertySet const> const& properties) {
153  if (properties->isArray("ccdId")) {
154  throw LSST_EXCEPT(ex::RuntimeError, "\"ccdId\" property has multiple values");
155  }
156  int ccdId = properties->getAsInt("ccdId");
157  if (ccdId < 0) {
158  throw LSST_EXCEPT(ex::RangeError, "negative \"ccdId\"");
159  }
160  if (ccdId > 255) {
161  throw LSST_EXCEPT(ex::RangeError, "\"ccdId\" is too large");
162  }
163  return static_cast<int>(ccdId);
164 }
165 
166 int extractAmpId(std::shared_ptr<PropertySet const> const& properties) {
167  if (properties->isArray("ampId")) {
168  throw LSST_EXCEPT(ex::RuntimeError, "\"ampId\" property has multiple values");
169  }
170  int ampId = properties->getAsInt("ampId");
171  if (ampId < 0) {
172  throw LSST_EXCEPT(ex::RangeError, "negative \"ampId\"");
173  }
174  if (ampId > 63) {
175  throw LSST_EXCEPT(ex::RangeError, "\"ampId\" is too large");
176  }
177  return (extractCcdId(properties) << 6) + ampId;
178 }
179 
180 int64_t extractCcdExposureId(std::shared_ptr<PropertySet const> const& properties) {
181  if (properties->isArray("ccdExposureId")) {
182  throw LSST_EXCEPT(ex::RuntimeError, "\"ccdExposureId\" property has multiple values");
183  }
184  int64_t ccdExposureId = properties->getAsInt64("ccdExposureId");
185  if (ccdExposureId < 0) {
186  throw LSST_EXCEPT(ex::RangeError, "negative \"ccdExposureId\"");
187  }
188  return ccdExposureId;
189 }
190 
191 int64_t extractAmpExposureId(std::shared_ptr<PropertySet const> const& properties) {
192  if (properties->isArray("ampExposureId")) {
193  throw LSST_EXCEPT(ex::RuntimeError, "\"ampExposureId\" property has multiple values");
194  }
195  int64_t ampExposureId = properties->getAsInt64("ampExposureId");
196  if (ampExposureId < 0) {
197  throw LSST_EXCEPT(ex::RangeError, "negative \"ampExposureId\"");
198  }
199  return ampExposureId;
200 }
201 
202 std::string const getItemName(std::shared_ptr<PropertySet const> const& properties) {
203  if (!properties) {
204  throw LSST_EXCEPT(ex::InvalidParameterError, "Null std::shared_ptr<PropertySet>");
205  }
206  if (properties->isArray("itemName")) {
207  throw LSST_EXCEPT(ex::InvalidParameterError, "\"itemName\" property has multiple values");
208  }
209  return properties->getAsString("itemName");
210 }
211 
212 bool extractOptionalFlag(std::shared_ptr<PropertySet const> const& properties, std::string const& name) {
213  if (properties && properties->exists(name)) {
214  return properties->getAsBool(name);
215  }
216  return false;
217 }
218 
219 std::string const getTableName(std::shared_ptr<Policy const> const& policy,
220  std::shared_ptr<PropertySet const> const& properties) {
221  std::string itemName(getItemName(properties));
222  return LogicalLocation(policy->getString(itemName + ".tableNamePattern"), properties).locString();
223 }
224 
225 std::vector<std::string> getAllSliceTableNames(std::shared_ptr<Policy const> const& policy,
226  std::shared_ptr<PropertySet const> const& properties) {
227  std::string itemName(getItemName(properties));
228  std::string pattern(policy->getString(itemName + ".tableNamePattern"));
229  int numSlices = 1;
230  if (properties->exists(itemName + ".numSlices")) {
231  numSlices = properties->getAsInt(itemName + ".numSlices");
232  }
233  if (numSlices <= 0) {
234  throw LSST_EXCEPT(ex::RuntimeError, itemName + " \".numSlices\" property value must be positive");
235  }
236  std::vector<std::string> names;
237  names.reserve(numSlices);
238  std::shared_ptr<PropertySet> props = properties->deepCopy();
239  for (int i = 0; i < numSlices; ++i) {
240  props->set("sliceId", i);
241  names.push_back(LogicalLocation(pattern, props).locString());
242  }
243  return names;
244 }
245 
246 void createTable(lsst::daf::persistence::LogicalLocation const& location,
247  std::shared_ptr<lsst::pex::policy::Policy const> const& policy,
248  std::shared_ptr<PropertySet const> const& properties) {
249  std::string itemName(getItemName(properties));
250  std::string name(getTableName(policy, properties));
251  std::string model(policy->getString(itemName + ".templateTableName"));
252 
253  lsst::daf::persistence::DbTsvStorage db;
254  db.setPersistLocation(location);
255  db.createTableFromTemplate(name, model);
256 }
257 
258 void dropAllSliceTables(lsst::daf::persistence::LogicalLocation const& location,
259  std::shared_ptr<lsst::pex::policy::Policy const> const& policy,
260  std::shared_ptr<PropertySet const> const& properties) {
261  std::vector<std::string> names = getAllSliceTableNames(policy, properties);
262 
263  lsst::daf::persistence::DbTsvStorage db;
264  db.setPersistLocation(location);
265  for (std::vector<std::string>::const_iterator i(names.begin()), end(names.end()); i != end; ++i) {
266  db.dropTable(*i);
267  }
268 }
269 
271  auto paramNames = prop.paramNames(false);
272  return formatFitsPropertiesImpl(paramNames, prop);
273 }
274 
276  std::set<std::string> const& excludeNames) {
277  std::vector<std::string> paramNames;
278  for (auto const & name: prop) {
279  if (excludeNames.count(name) == 0) {
280  paramNames.push_back(name);
281  }
282  }
283  return formatFitsPropertiesImpl(paramNames, prop);
284 }
285 
287  return prop.paramNames(false).size();
288 }
289 }
290 }
291 } // namespace lsst::afw::formatters
std::string const getItemName(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Extracts and returns the string-valued "itemName" property from the given data property object...
bool extractOptionalFlag(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties, std::string const &name)
Returns true if and only if properties is non-null and contains a unique property with the given name...
table::Key< std::string > name
Definition: ApCorrMap.cc:71
std::string const getTableName(std::shared_ptr< lsst::pex::policy::Policy const > const &policy, std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Returns the name of the table that a single slice of a pipeline involved in the processing of a singl...
int extractAmpId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
int extractSliceId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
lsst::daf::base::PropertyList PropertyList
Definition: Wcs.cc:56
int64_t extractCcdExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
int64_t extractFpaExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:55
int extractVisitId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
std::vector< std::string > getAllSliceTableNames(std::shared_ptr< lsst::pex::policy::Policy const > const &policy, std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Stores the name of the table that each slice of a pipeline involved in processing a visit used for pe...
int64_t extractAmpExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
A base class for image defects.
Definition: cameraGeom.dox:3
int extractCcdId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
int end
std::string formatFitsProperties(lsst::daf::base::PropertySet const &prop)
Format a PropertySet into a single FITS header string.
Definition: Utils.cc:270
int countFitsHeaderCards(lsst::daf::base::PropertySet const &prop)
Definition: Utils.cc:286
void dropAllSliceTables(lsst::daf::persistence::LogicalLocation const &location, std::shared_ptr< lsst::pex::policy::Policy const > const &policy, std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Drops the database table(s) identified by getAllSliceTables().
void createTable(lsst::daf::persistence::LogicalLocation const &location, std::shared_ptr< lsst::pex::policy::Policy const > const &policy, std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Creates the table identified by calling getTableName() with the given policy and properties.