LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
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 //
30 //
31 //##====---------------- ----------------====##/
32 
33 #include "boost/cstdint.hpp"
34 #include "boost/format.hpp"
35 
36 #include "lsst/pex/exceptions.h"
41 
42 using boost::int64_t;
43 namespace ex = lsst::pex::exceptions;
47 
48 namespace lsst {
49 namespace afw {
50 namespace formatters {
51 
52 int extractSliceId(CONST_PTR(PropertySet) 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(CONST_PTR(PropertySet) 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(CONST_PTR(PropertySet) 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(CONST_PTR(PropertySet) 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(CONST_PTR(PropertySet) 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(CONST_PTR(PropertySet) 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(CONST_PTR(PropertySet) 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 
151 std::string const getItemName(CONST_PTR(PropertySet) const& properties) {
152  if (!properties) {
153  throw LSST_EXCEPT(ex::InvalidParameterError, "Null PropertySet::Ptr");
154  }
155  if (properties->isArray("itemName")) {
156  throw LSST_EXCEPT(ex::InvalidParameterError, "\"itemName\" property has multiple values");
157  }
158  return properties->getAsString("itemName");
159 }
160 
161 
167  CONST_PTR(PropertySet) const& properties,
168  std::string const & name
169 ) {
170  if (properties && properties->exists(name)) {
171  return properties->getAsBool(name);
172  }
173  return false;
174 }
175 
176 
192 std::string const getTableName(
193  CONST_PTR(Policy) const& policy,
194  CONST_PTR(PropertySet) const& properties
195 ) {
196  std::string itemName(getItemName(properties));
197  return LogicalLocation(policy->getString(itemName + ".tableNamePattern"), properties).locString();
198 }
199 
200 
223 std::vector<std::string> getAllSliceTableNames(
224  CONST_PTR(Policy) const& policy,
225  CONST_PTR(PropertySet) const& properties
226 ) {
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,
235  itemName + " \".numSlices\" property value must be positive");
236  }
237  std::vector<std::string> names;
238  names.reserve(numSlices);
239  PTR(PropertySet) props = properties->deepCopy();
240  for (int i = 0; i < numSlices; ++i) {
241  props->set("sliceId", i);
242  names.push_back(LogicalLocation(pattern, props).locString());
243  }
244  return names;
245 }
246 
247 
259  CONST_PTR(lsst::pex::policy::Policy) const& policy,
260  CONST_PTR(PropertySet) const& properties
261 ) {
262  std::string itemName(getItemName(properties));
263  std::string name(getTableName(policy, properties));
264  std::string model(policy->getString(itemName + ".templateTableName"));
265 
267  db.setPersistLocation(location);
268  db.createTableFromTemplate(name, model);
269 }
270 
271 
275  CONST_PTR(lsst::pex::policy::Policy) const& policy,
276  CONST_PTR(PropertySet) const& properties
277 ) {
278  std::vector<std::string> names = getAllSliceTableNames(policy, properties);
279 
281  db.setPersistLocation(location);
282  for (std::vector<std::string>::const_iterator i(names.begin()), end(names.end()); i != end; ++i) {
283  db.dropTable(*i);
284  }
285 }
286 
287 
289  typedef std::vector<std::string> NameList;
290  std::string sout;
291 
292  NameList paramNames = prop->paramNames(false);
293 
294  for (NameList::const_iterator i = paramNames.begin(), end = paramNames.end(); i != end; ++i) {
295  std::size_t lastPeriod = i->rfind(char('.'));
296  std::string name = (lastPeriod == std::string::npos) ? *i : i->substr(lastPeriod + 1);
297  std::type_info const & type = prop->typeOf(*i);
298 
299  std::string out = "";
300  if (name.size() > 8) { // Oh dear; too long for a FITS keyword
301  out += "HIERARCH = " + name;
302  } else {
303  out = (boost::format("%-8s= ") % name).str();
304  }
305 
306  if (type == typeid(int)) {
307  out += (boost::format("%20d") % prop->get<int>(*i)).str();
308  } else if (type == typeid(double)) {
309  out += (boost::format("%20.15g") % prop->get<double>(*i)).str();
310  } else if (type == typeid(std::string)) {
311  out += (boost::format("'%-67s' ") % prop->get<std::string>(*i)).str();
312  }
313 
314  int const len = out.size();
315  if (len < 80) {
316  out += std::string(80 - len, ' ');
317  } else {
318  out = out.substr(0, 80);
319  }
320 
321  sout += out;
322  }
323 
324  return sout.c_str();
325 }
326 
327 
329  return prop->paramNames(false).size();
330 }
331 
332 
333 }}} // namespace lsst::afw::formatters
bool getAsBool(std::string const &name) const
Definition: PropertySet.cc:321
std::vector< std::string > paramNames(bool topLevelOnly=true) const
Definition: PropertySet.cc:142
int64_t extractAmpExposureId(boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
int extractCcdId(boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
int extractAmpId(boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
std::string const getItemName(boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
table::Key< std::string > name
Definition: ApCorrMap.cc:71
virtual void setPersistLocation(LogicalLocation const &location)
Definition: DbTsvStorage.cc:91
int64_t getAsInt64(std::string const &name) const
Definition: PropertySet.cc:372
Include files required for standard LSST Exception handling.
Class for logical location of a persisted Persistable instance.
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:169
const std::string getString(const std::string &name) const
Definition: Policy.h:631
#define PTR(...)
Definition: base.h:41
bool extractOptionalFlag(boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties, std::string const &name)
Formatting utilities.
int64_t extractFpaExposureId(boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
bool isArray(std::string const &name) const
Definition: PropertySet.cc:198
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:58
int64_t extractCcdExposureId(boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
int getAsInt(std::string const &name) const
Definition: PropertySet.cc:333
Interface for DbTsvStorage class.
T get(std::string const &name) const
Definition: PropertySet.cc:246
void dropAllSliceTables(lsst::daf::persistence::LogicalLocation const &location, boost::shared_ptr< lsst::pex::policy::Policy const > const &policy, boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Class for database storage with data loading from TSV files.
Definition: DbTsvStorage.h:66
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
std::type_info const & typeOf(std::string const &name) const
Definition: PropertySet.cc:227
void createTable(lsst::daf::persistence::LogicalLocation const &location, boost::shared_ptr< lsst::pex::policy::Policy const > const &policy, boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
int extractVisitId(boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Class for storing generic metadata.
Definition: PropertySet.h:82
virtual Ptr deepCopy(void) const
Definition: PropertySet.cc:70
std::string getAsString(std::string const &name) const
Definition: PropertySet.cc:444
#define CONST_PTR(...)
Definition: base.h:47
int extractSliceId(boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Interface for PropertySet class.
std::string formatFitsProperties(boost::shared_ptr< lsst::daf::base::PropertySet const > const &prop)
std::vector< std::string > getAllSliceTableNames(boost::shared_ptr< lsst::pex::policy::Policy const > const &policy, boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Interface for LogicalLocation class.
std::string const getTableName(boost::shared_ptr< lsst::pex::policy::Policy const > const &policy, boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
bool exists(std::string const &name) const
Definition: PropertySet.cc:190
int countFitsHeaderCards(boost::shared_ptr< lsst::daf::base::PropertySet const > const &prop)
virtual void dropTable(std::string const &tableName)