LSSTApplications  11.0-22-g33de520,13.0+153,14.0+52,14.0+57,14.0-1-g013352c+36,14.0-1-g13ef843+9,14.0-1-g4b114ac+14,14.0-1-g7257b6a+12,14.0-1-g8b7e855+51,14.0-13-g7a60b79+2,14.0-14-g87d16e8+10,14.0-14-gbf7a6f8a,14.0-17-g4f4ea82+5,14.0-2-g319577b+11,14.0-2-ga5af9b6+10,14.0-22-gc48c03f+3,14.0-3-g20413be+3,14.0-46-g76222d5f+3,14.0-47-g0a51fac97,14.0-5-g744ff5f+2,14.0-5-g86eb1bd+31,14.0-6-gd5b81a9+6,14.0-6-ge2c9487+42,14.0-8-g7f6dd6b+6,14.0-8-gb81b6e9+4,14.0-9-g11010eb,14.0-9-g330837b+5
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 //
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"
43 
44 using std::int64_t;
45 namespace ex = lsst::pex::exceptions;
49 
50 namespace lsst {
51 namespace afw {
52 namespace formatters {
53 namespace {
54 
64 std::string formatFitsPropertiesImpl(std::vector<std::string> const& paramNames,
65  daf::base::PropertySet const& prop) {
66  std::ostringstream result;
67  for (auto const & fullName : paramNames) {
68  std::size_t lastPeriod = fullName.rfind(char('.'));
69  auto name = (lastPeriod == std::string::npos) ? fullName : fullName.substr(lastPeriod + 1);
70  std::type_info const& type = prop.typeOf(name);
71 
72  std::string out = "";
73  out.reserve(80);
74  if (name.size() > 8) { // Oh dear; too long for a FITS keyword
75  out += "HIERARCH = " + name;
76  } else {
77  out = (boost::format("%-8s= ") % name).str();
78  }
79 
80  if (type == typeid(bool)) {
81  out += prop.get<bool>(name) ? "1" : "0";
82  } else if (type == typeid(int)) {
83  out += (boost::format("%20d") % prop.get<int>(name)).str();
84  } else if (type == typeid(double)) {
85  out += (boost::format("%20.15g") % prop.get<double>(name)).str();
86  } else if (type == typeid(float)) {
87  out += (boost::format("%20.15g") % prop.get<float>(name)).str();
88  } else if (type == typeid(std::string)) {
89  out += (boost::format("'%-67s' ") % prop.get<std::string>(name)).str();
90  }
91 
92  int const len = out.size();
93  if (len < 80) {
94  out += std::string(80 - len, ' ');
95  } else {
96  out = out.substr(0, 80);
97  }
98 
99  result << out;
100  }
101 
102  return result.str();
103 }
104 
105 } // namespace
106 
108  if (properties->isArray("sliceId")) {
109  throw LSST_EXCEPT(ex::RuntimeError, "\"sliceId\" property has multiple values");
110  }
111  int sliceId = properties->getAsInt("sliceId");
112  if (sliceId < 0) {
113  throw LSST_EXCEPT(ex::RangeError, "negative \"sliceId\"");
114  }
115  if (properties->exists("universeSize") && !properties->isArray("universeSize")) {
116  int universeSize = properties->getAsInt("universeSize");
117  if (sliceId >= universeSize) {
118  throw LSST_EXCEPT(ex::RangeError, "\"sliceId\" must be less than \"universeSize \"");
119  }
120  }
121  return sliceId;
122 }
123 
125  if (properties->isArray("visitId")) {
126  throw LSST_EXCEPT(ex::RuntimeError, "\"visitId\" property has multiple values");
127  }
128  int visitId = properties->getAsInt("visitId");
129  if (visitId < 0) {
130  throw LSST_EXCEPT(ex::RangeError, "negative \"visitId\"");
131  }
132  return visitId;
133 }
134 
136  if (properties->isArray("fpaExposureId")) {
137  throw LSST_EXCEPT(ex::RuntimeError, "\"fpaExposureId\" property has multiple values");
138  }
139  int64_t fpaExposureId = properties->getAsInt64("fpaExposureId");
140  if (fpaExposureId < 0) {
141  throw LSST_EXCEPT(ex::RangeError, "negative \"fpaExposureId\"");
142  }
143  if ((fpaExposureId & 0xfffffffe00000000LL) != 0LL) {
144  throw LSST_EXCEPT(ex::RangeError, "\"fpaExposureId\" is too large");
145  }
146  return fpaExposureId;
147 }
148 
150  if (properties->isArray("ccdId")) {
151  throw LSST_EXCEPT(ex::RuntimeError, "\"ccdId\" property has multiple values");
152  }
153  int ccdId = properties->getAsInt("ccdId");
154  if (ccdId < 0) {
155  throw LSST_EXCEPT(ex::RangeError, "negative \"ccdId\"");
156  }
157  if (ccdId > 255) {
158  throw LSST_EXCEPT(ex::RangeError, "\"ccdId\" is too large");
159  }
160  return static_cast<int>(ccdId);
161 }
162 
164  if (properties->isArray("ampId")) {
165  throw LSST_EXCEPT(ex::RuntimeError, "\"ampId\" property has multiple values");
166  }
167  int ampId = properties->getAsInt("ampId");
168  if (ampId < 0) {
169  throw LSST_EXCEPT(ex::RangeError, "negative \"ampId\"");
170  }
171  if (ampId > 63) {
172  throw LSST_EXCEPT(ex::RangeError, "\"ampId\" is too large");
173  }
174  return (extractCcdId(properties) << 6) + ampId;
175 }
176 
178  if (properties->isArray("ccdExposureId")) {
179  throw LSST_EXCEPT(ex::RuntimeError, "\"ccdExposureId\" property has multiple values");
180  }
181  int64_t ccdExposureId = properties->getAsInt64("ccdExposureId");
182  if (ccdExposureId < 0) {
183  throw LSST_EXCEPT(ex::RangeError, "negative \"ccdExposureId\"");
184  }
185  return ccdExposureId;
186 }
187 
189  if (properties->isArray("ampExposureId")) {
190  throw LSST_EXCEPT(ex::RuntimeError, "\"ampExposureId\" property has multiple values");
191  }
192  int64_t ampExposureId = properties->getAsInt64("ampExposureId");
193  if (ampExposureId < 0) {
194  throw LSST_EXCEPT(ex::RangeError, "negative \"ampExposureId\"");
195  }
196  return ampExposureId;
197 }
198 
200  if (!properties) {
201  throw LSST_EXCEPT(ex::InvalidParameterError, "Null std::shared_ptr<PropertySet>");
202  }
203  if (properties->isArray("itemName")) {
204  throw LSST_EXCEPT(ex::InvalidParameterError, "\"itemName\" property has multiple values");
205  }
206  return properties->getAsString("itemName");
207 }
208 
210  if (properties && properties->exists(name)) {
211  return properties->getAsBool(name);
212  }
213  return false;
214 }
215 
217  std::shared_ptr<PropertySet const> const& properties) {
218  std::string itemName(getItemName(properties));
219  return LogicalLocation(policy->getString(itemName + ".tableNamePattern"), properties).locString();
220 }
221 
223  std::shared_ptr<PropertySet const> const& properties) {
224  std::string itemName(getItemName(properties));
225  std::string pattern(policy->getString(itemName + ".tableNamePattern"));
226  int numSlices = 1;
227  if (properties->exists(itemName + ".numSlices")) {
228  numSlices = properties->getAsInt(itemName + ".numSlices");
229  }
230  if (numSlices <= 0) {
231  throw LSST_EXCEPT(ex::RuntimeError, itemName + " \".numSlices\" property value must be positive");
232  }
234  names.reserve(numSlices);
235  std::shared_ptr<PropertySet> props = properties->deepCopy();
236  for (int i = 0; i < numSlices; ++i) {
237  props->set("sliceId", i);
238  names.push_back(LogicalLocation(pattern, props).locString());
239  }
240  return names;
241 }
242 
245  std::shared_ptr<PropertySet const> const& properties) {
246  std::string itemName(getItemName(properties));
247  std::string name(getTableName(policy, properties));
248  std::string model(policy->getString(itemName + ".templateTableName"));
249 
251  db.setPersistLocation(location);
252  db.createTableFromTemplate(name, model);
253 }
254 
257  std::shared_ptr<PropertySet const> const& properties) {
258  std::vector<std::string> names = getAllSliceTableNames(policy, properties);
259 
261  db.setPersistLocation(location);
262  for (std::vector<std::string>::const_iterator i(names.begin()), end(names.end()); i != end; ++i) {
263  db.dropTable(*i);
264  }
265 }
266 
268  std::set<std::string> const& excludeNames) {
269  auto const allParamNames = prop.paramNames(false);
270  std::vector<std::string> desiredParamNames;
271  for (auto const & name: allParamNames) {
272  if (excludeNames.count(name) == 0) {
273  desiredParamNames.push_back(name);
274  }
275  }
276  return formatFitsPropertiesImpl(desiredParamNames, prop);
277 }
278 
280  return prop.paramNames(false).size();
281 }
282 
283 ndarray::Array<std::uint8_t, 1, 1> stringToBytes(std::string const& str) {
284  auto nbytes = str.size() * sizeof(char) / sizeof(std::uint8_t);
285  std::uint8_t const* byteCArr = reinterpret_cast<std::uint8_t const*>(str.data());
286  auto shape = ndarray::makeVector(nbytes);
287  auto strides = ndarray::makeVector(1);
288  // Make an Array that shares memory with `str` (and does not free that memory when destroyed),
289  // then return a copy; this is simpler than manually copying the data into a newly allocated array
290  ndarray::Array<std::uint8_t const, 1, 1> localArray = ndarray::external(byteCArr, shape, strides);
291  return ndarray::copy(localArray);
292 }
293 
294 std::string bytesToString(ndarray::Array<std::uint8_t const, 1, 1> const& bytes) {
295  auto nchars = bytes.size() * sizeof(std::uint8_t) / sizeof(char);
296  char const* charCArr = reinterpret_cast<char const*>(bytes.getData());
297  return std::string(charCArr, nchars);
298 }
299 
300 }
301 }
302 } // 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...
Definition: Utils.cc:199
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...
Definition: Utils.cc:209
table::Key< std::string > name
Definition: ApCorrMap.cc:71
virtual void setPersistLocation(LogicalLocation const &location)
Set the database location to persist to.
Definition: DbTsvStorage.cc:91
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...
Definition: Utils.cc:216
Class for logical location of a persisted Persistable instance.
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:169
std::vector< std::string > paramNames(bool topLevelOnly=true) const
A variant of names that excludes the names of subproperties.
Definition: PropertySet.cc:106
std::string formatFitsProperties(lsst::daf::base::PropertySet const &prop, std::set< std::string > const &excludeNames={})
Format a PropertySet into a single FITS header string.
Definition: Utils.cc:267
int extractAmpId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:163
T end(T... args)
int extractSliceId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:107
int64_t extractCcdExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:177
std::string bytesToString(ndarray::Array< std::uint8_t const, 1, 1 > const &bytes)
Decode a std::string from a vector of uint8 returned by stringToBytes.
Definition: Utils.cc:294
STL class.
int64_t extractFpaExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:135
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:55
T push_back(T... args)
int extractVisitId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:124
T data(T... args)
int64_t extractAmpExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:188
A base class for image defects.
Definition: cameraGeom.dox:3
int extractCcdId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:149
Interface for DbTsvStorage class.
int end
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:134
Include files required for standard LSST Exception handling.
int countFitsHeaderCards(lsst::daf::base::PropertySet const &prop)
Definition: Utils.cc:279
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().
Definition: Utils.cc:255
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.
Definition: Utils.cc:243
T str(T... args)
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...
Definition: Utils.cc:222
T count(T... args)
Class for database storage with data loading from TSV files.
Definition: DbTsvStorage.h:66
T size(T... args)
STL class.
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
T begin(T... args)
Class for storing generic metadata.
Definition: PropertySet.h:73
T substr(T... args)
ndarray::Array< std::uint8_t, 1, 1 > stringToBytes(std::string const &str)
Encode a std::string as a vector of uint8.
Definition: Utils.cc:283
Interface for LogicalLocation class.
virtual void dropTable(std::string const &tableName)
Drop a table.
T reserve(T... args)