lsst.afw  13.0-39-g3deb52d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
34 #include "boost/format.hpp"
35 
36 #include "lsst/pex/exceptions.h"
37 #include "lsst/daf/base/PropertySet.h"
38 #include "lsst/daf/persistence/LogicalLocation.h"
39 #include "lsst/daf/persistence/DbTsvStorage.h"
41 
42 using std::int64_t;
43 namespace ex = lsst::pex::exceptions;
45 using lsst::pex::policy::Policy;
46 using lsst::daf::persistence::LogicalLocation;
47 
48 namespace lsst {
49 namespace afw {
50 namespace formatters {
51 
52 int extractSliceId(std::shared_ptr<PropertySet const> const& properties) {
53  if (properties->isArray("sliceId")) {
54  throw LSST_EXCEPT(ex::RuntimeError, "\"sliceId\" property has multiple values");
55  }
56  int sliceId = properties->getAsInt("sliceId");
57  if (sliceId < 0) {
58  throw LSST_EXCEPT(ex::RangeError, "negative \"sliceId\"");
59  }
60  if (properties->exists("universeSize") && !properties->isArray("universeSize")) {
61  int universeSize = properties->getAsInt("universeSize");
62  if (sliceId >= universeSize) {
63  throw LSST_EXCEPT(ex::RangeError, "\"sliceId\" must be less than \"universeSize \"");
64  }
65  }
66  return sliceId;
67 }
68 
69 int extractVisitId(std::shared_ptr<PropertySet const> const& properties) {
70  if (properties->isArray("visitId")) {
71  throw LSST_EXCEPT(ex::RuntimeError, "\"visitId\" property has multiple values");
72  }
73  int visitId = properties->getAsInt("visitId");
74  if (visitId < 0) {
75  throw LSST_EXCEPT(ex::RangeError, "negative \"visitId\"");
76  }
77  return visitId;
78 }
79 
80 int64_t extractFpaExposureId(std::shared_ptr<PropertySet const> const& properties) {
81  if (properties->isArray("fpaExposureId")) {
82  throw LSST_EXCEPT(ex::RuntimeError, "\"fpaExposureId\" property has multiple values");
83  }
84  int64_t fpaExposureId = properties->getAsInt64("fpaExposureId");
85  if (fpaExposureId < 0) {
86  throw LSST_EXCEPT(ex::RangeError, "negative \"fpaExposureId\"");
87  }
88  if ((fpaExposureId & 0xfffffffe00000000LL) != 0LL) {
89  throw LSST_EXCEPT(ex::RangeError, "\"fpaExposureId\" is too large");
90  }
91  return fpaExposureId;
92 }
93 
94 int extractCcdId(std::shared_ptr<PropertySet const> const& properties) {
95  if (properties->isArray("ccdId")) {
96  throw LSST_EXCEPT(ex::RuntimeError, "\"ccdId\" property has multiple values");
97  }
98  int ccdId = properties->getAsInt("ccdId");
99  if (ccdId < 0) {
100  throw LSST_EXCEPT(ex::RangeError, "negative \"ccdId\"");
101  }
102  if (ccdId > 255) {
103  throw LSST_EXCEPT(ex::RangeError, "\"ccdId\" is too large");
104  }
105  return static_cast<int>(ccdId);
106 }
107 
108 int extractAmpId(std::shared_ptr<PropertySet const> const& properties) {
109  if (properties->isArray("ampId")) {
110  throw LSST_EXCEPT(ex::RuntimeError, "\"ampId\" property has multiple values");
111  }
112  int ampId = properties->getAsInt("ampId");
113  if (ampId < 0) {
114  throw LSST_EXCEPT(ex::RangeError, "negative \"ampId\"");
115  }
116  if (ampId > 63) {
117  throw LSST_EXCEPT(ex::RangeError, "\"ampId\" is too large");
118  }
119  return (extractCcdId(properties) << 6) + ampId;
120 }
121 
122 int64_t extractCcdExposureId(std::shared_ptr<PropertySet const> const& properties) {
123  if (properties->isArray("ccdExposureId")) {
124  throw LSST_EXCEPT(ex::RuntimeError, "\"ccdExposureId\" property has multiple values");
125  }
126  int64_t ccdExposureId = properties->getAsInt64("ccdExposureId");
127  if (ccdExposureId < 0) {
128  throw LSST_EXCEPT(ex::RangeError, "negative \"ccdExposureId\"");
129  }
130  return ccdExposureId;
131 }
132 
133 int64_t extractAmpExposureId(std::shared_ptr<PropertySet const> const& properties) {
134  if (properties->isArray("ampExposureId")) {
135  throw LSST_EXCEPT(ex::RuntimeError, "\"ampExposureId\" property has multiple values");
136  }
137  int64_t ampExposureId = properties->getAsInt64("ampExposureId");
138  if (ampExposureId < 0) {
139  throw LSST_EXCEPT(ex::RangeError, "negative \"ampExposureId\"");
140  }
141  return ampExposureId;
142 }
143 
144 std::string const getItemName(std::shared_ptr<PropertySet const> const& properties) {
145  if (!properties) {
146  throw LSST_EXCEPT(ex::InvalidParameterError, "Null std::shared_ptr<PropertySet>");
147  }
148  if (properties->isArray("itemName")) {
149  throw LSST_EXCEPT(ex::InvalidParameterError, "\"itemName\" property has multiple values");
150  }
151  return properties->getAsString("itemName");
152 }
153 
154 bool extractOptionalFlag(std::shared_ptr<PropertySet const> const& properties, std::string const& name) {
155  if (properties && properties->exists(name)) {
156  return properties->getAsBool(name);
157  }
158  return false;
159 }
160 
161 std::string const getTableName(std::shared_ptr<Policy const> const& policy,
162  std::shared_ptr<PropertySet const> const& properties) {
163  std::string itemName(getItemName(properties));
164  return LogicalLocation(policy->getString(itemName + ".tableNamePattern"), properties).locString();
165 }
166 
167 std::vector<std::string> getAllSliceTableNames(std::shared_ptr<Policy const> const& policy,
168  std::shared_ptr<PropertySet const> const& properties) {
169  std::string itemName(getItemName(properties));
170  std::string pattern(policy->getString(itemName + ".tableNamePattern"));
171  int numSlices = 1;
172  if (properties->exists(itemName + ".numSlices")) {
173  numSlices = properties->getAsInt(itemName + ".numSlices");
174  }
175  if (numSlices <= 0) {
176  throw LSST_EXCEPT(ex::RuntimeError, itemName + " \".numSlices\" property value must be positive");
177  }
178  std::vector<std::string> names;
179  names.reserve(numSlices);
180  std::shared_ptr<PropertySet> props = properties->deepCopy();
181  for (int i = 0; i < numSlices; ++i) {
182  props->set("sliceId", i);
183  names.push_back(LogicalLocation(pattern, props).locString());
184  }
185  return names;
186 }
187 
188 void createTable(lsst::daf::persistence::LogicalLocation const& location,
189  std::shared_ptr<lsst::pex::policy::Policy const> const& policy,
190  std::shared_ptr<PropertySet const> const& properties) {
191  std::string itemName(getItemName(properties));
192  std::string name(getTableName(policy, properties));
193  std::string model(policy->getString(itemName + ".templateTableName"));
194 
195  lsst::daf::persistence::DbTsvStorage db;
196  db.setPersistLocation(location);
197  db.createTableFromTemplate(name, model);
198 }
199 
200 void dropAllSliceTables(lsst::daf::persistence::LogicalLocation const& location,
201  std::shared_ptr<lsst::pex::policy::Policy const> const& policy,
202  std::shared_ptr<PropertySet const> const& properties) {
203  std::vector<std::string> names = getAllSliceTableNames(policy, properties);
204 
205  lsst::daf::persistence::DbTsvStorage db;
206  db.setPersistLocation(location);
207  for (std::vector<std::string>::const_iterator i(names.begin()), end(names.end()); i != end; ++i) {
208  db.dropTable(*i);
209  }
210 }
211 
212 std::string formatFitsProperties(std::shared_ptr<lsst::daf::base::PropertySet const> const& prop) {
213  typedef std::vector<std::string> NameList;
214  std::string sout;
215 
216  NameList paramNames = prop->paramNames(false);
217 
218  for (NameList::const_iterator i = paramNames.begin(), end = paramNames.end(); i != end; ++i) {
219  std::size_t lastPeriod = i->rfind(char('.'));
220  std::string name = (lastPeriod == std::string::npos) ? *i : i->substr(lastPeriod + 1);
221  std::type_info const& type = prop->typeOf(*i);
222 
223  std::string out = "";
224  if (name.size() > 8) { // Oh dear; too long for a FITS keyword
225  out += "HIERARCH = " + name;
226  } else {
227  out = (boost::format("%-8s= ") % name).str();
228  }
229 
230  if (type == typeid(int)) {
231  out += (boost::format("%20d") % prop->get<int>(*i)).str();
232  } else if (type == typeid(double)) {
233  out += (boost::format("%20.15g") % prop->get<double>(*i)).str();
234  } else if (type == typeid(std::string)) {
235  out += (boost::format("'%-67s' ") % prop->get<std::string>(*i)).str();
236  }
237 
238  int const len = out.size();
239  if (len < 80) {
240  out += std::string(80 - len, ' ');
241  } else {
242  out = out.substr(0, 80);
243  }
244 
245  sout += out;
246  }
247 
248  return sout.c_str();
249 }
250 
251 int countFitsHeaderCards(std::shared_ptr<lsst::daf::base::PropertySet const> const& prop) {
252  return prop->paramNames(false).size();
253 }
254 }
255 }
256 } // namespace lsst::afw::formatters
int64_t extractFpaExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
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...
int64_t extractAmpExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
int64_t extractCcdExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
int extractAmpId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
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().
std::string const getItemName(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Extracts and returns the string-valued &quot;itemName&quot; property from the given data property object...
int countFitsHeaderCards(std::shared_ptr< lsst::daf::base::PropertySet const > const &prop)
Definition: Utils.cc:251
int extractCcdId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
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.
table::Key< std::string > name
Definition: ApCorrMap.cc:71
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:55
std::string formatFitsProperties(std::shared_ptr< lsst::daf::base::PropertySet const > const &prop)
Definition: Utils.cc:212
int extractSliceId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
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 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...