LSSTApplications  11.0-24-g0a022a1,14.0+77,15.0,15.0+1
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 
55  if (properties->isArray("sliceId")) {
56  throw LSST_EXCEPT(ex::RuntimeError, "\"sliceId\" property has multiple values");
57  }
58  int sliceId = properties->getAsInt("sliceId");
59  if (sliceId < 0) {
60  throw LSST_EXCEPT(ex::RangeError, "negative \"sliceId\"");
61  }
62  if (properties->exists("universeSize") && !properties->isArray("universeSize")) {
63  int universeSize = properties->getAsInt("universeSize");
64  if (sliceId >= universeSize) {
65  throw LSST_EXCEPT(ex::RangeError, "\"sliceId\" must be less than \"universeSize \"");
66  }
67  }
68  return sliceId;
69 }
70 
72  if (properties->isArray("visitId")) {
73  throw LSST_EXCEPT(ex::RuntimeError, "\"visitId\" property has multiple values");
74  }
75  int visitId = properties->getAsInt("visitId");
76  if (visitId < 0) {
77  throw LSST_EXCEPT(ex::RangeError, "negative \"visitId\"");
78  }
79  return visitId;
80 }
81 
83  if (properties->isArray("fpaExposureId")) {
84  throw LSST_EXCEPT(ex::RuntimeError, "\"fpaExposureId\" property has multiple values");
85  }
86  int64_t fpaExposureId = properties->getAsInt64("fpaExposureId");
87  if (fpaExposureId < 0) {
88  throw LSST_EXCEPT(ex::RangeError, "negative \"fpaExposureId\"");
89  }
90  if ((fpaExposureId & 0xfffffffe00000000LL) != 0LL) {
91  throw LSST_EXCEPT(ex::RangeError, "\"fpaExposureId\" is too large");
92  }
93  return fpaExposureId;
94 }
95 
97  if (properties->isArray("ccdId")) {
98  throw LSST_EXCEPT(ex::RuntimeError, "\"ccdId\" property has multiple values");
99  }
100  int ccdId = properties->getAsInt("ccdId");
101  if (ccdId < 0) {
102  throw LSST_EXCEPT(ex::RangeError, "negative \"ccdId\"");
103  }
104  if (ccdId > 255) {
105  throw LSST_EXCEPT(ex::RangeError, "\"ccdId\" is too large");
106  }
107  return static_cast<int>(ccdId);
108 }
109 
111  if (properties->isArray("ampId")) {
112  throw LSST_EXCEPT(ex::RuntimeError, "\"ampId\" property has multiple values");
113  }
114  int ampId = properties->getAsInt("ampId");
115  if (ampId < 0) {
116  throw LSST_EXCEPT(ex::RangeError, "negative \"ampId\"");
117  }
118  if (ampId > 63) {
119  throw LSST_EXCEPT(ex::RangeError, "\"ampId\" is too large");
120  }
121  return (extractCcdId(properties) << 6) + ampId;
122 }
123 
125  if (properties->isArray("ccdExposureId")) {
126  throw LSST_EXCEPT(ex::RuntimeError, "\"ccdExposureId\" property has multiple values");
127  }
128  int64_t ccdExposureId = properties->getAsInt64("ccdExposureId");
129  if (ccdExposureId < 0) {
130  throw LSST_EXCEPT(ex::RangeError, "negative \"ccdExposureId\"");
131  }
132  return ccdExposureId;
133 }
134 
136  if (properties->isArray("ampExposureId")) {
137  throw LSST_EXCEPT(ex::RuntimeError, "\"ampExposureId\" property has multiple values");
138  }
139  int64_t ampExposureId = properties->getAsInt64("ampExposureId");
140  if (ampExposureId < 0) {
141  throw LSST_EXCEPT(ex::RangeError, "negative \"ampExposureId\"");
142  }
143  return ampExposureId;
144 }
145 
147  if (!properties) {
148  throw LSST_EXCEPT(ex::InvalidParameterError, "Null std::shared_ptr<PropertySet>");
149  }
150  if (properties->isArray("itemName")) {
151  throw LSST_EXCEPT(ex::InvalidParameterError, "\"itemName\" property has multiple values");
152  }
153  return properties->getAsString("itemName");
154 }
155 
157  if (properties && properties->exists(name)) {
158  return properties->getAsBool(name);
159  }
160  return false;
161 }
162 
164  std::shared_ptr<PropertySet const> const& properties) {
165  std::string itemName(getItemName(properties));
166  return LogicalLocation(policy->getString(itemName + ".tableNamePattern"), properties).locString();
167 }
168 
170  std::shared_ptr<PropertySet const> const& properties) {
171  std::string itemName(getItemName(properties));
172  std::string pattern(policy->getString(itemName + ".tableNamePattern"));
173  int numSlices = 1;
174  if (properties->exists(itemName + ".numSlices")) {
175  numSlices = properties->getAsInt(itemName + ".numSlices");
176  }
177  if (numSlices <= 0) {
178  throw LSST_EXCEPT(ex::RuntimeError, itemName + " \".numSlices\" property value must be positive");
179  }
181  names.reserve(numSlices);
182  std::shared_ptr<PropertySet> props = properties->deepCopy();
183  for (int i = 0; i < numSlices; ++i) {
184  props->set("sliceId", i);
185  names.push_back(LogicalLocation(pattern, props).locString());
186  }
187  return names;
188 }
189 
192  std::shared_ptr<PropertySet const> const& properties) {
193  std::string itemName(getItemName(properties));
194  std::string name(getTableName(policy, properties));
195  std::string model(policy->getString(itemName + ".templateTableName"));
196 
198  db.setPersistLocation(location);
199  db.createTableFromTemplate(name, model);
200 }
201 
204  std::shared_ptr<PropertySet const> const& properties) {
205  std::vector<std::string> names = getAllSliceTableNames(policy, properties);
206 
208  db.setPersistLocation(location);
209  for (std::vector<std::string>::const_iterator i(names.begin()), end(names.end()); i != end; ++i) {
210  db.dropTable(*i);
211  }
212 }
213 
215  return prop.paramNames(false).size();
216 }
217 
218 ndarray::Array<std::uint8_t, 1, 1> stringToBytes(std::string const& str) {
219  auto nbytes = str.size() * sizeof(char) / sizeof(std::uint8_t);
220  std::uint8_t const* byteCArr = reinterpret_cast<std::uint8_t const*>(str.data());
221  auto shape = ndarray::makeVector(nbytes);
222  auto strides = ndarray::makeVector(1);
223  // Make an Array that shares memory with `str` (and does not free that memory when destroyed),
224  // then return a copy; this is simpler than manually copying the data into a newly allocated array
225  ndarray::Array<std::uint8_t const, 1, 1> localArray = ndarray::external(byteCArr, shape, strides);
226  return ndarray::copy(localArray);
227 }
228 
229 std::string bytesToString(ndarray::Array<std::uint8_t const, 1, 1> const& bytes) {
230  auto nchars = bytes.size() * sizeof(std::uint8_t) / sizeof(char);
231  char const* charCArr = reinterpret_cast<char const*>(bytes.getData());
232  return std::string(charCArr, nchars);
233 }
234 
235 }
236 }
237 } // 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:146
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:156
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:163
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
int extractAmpId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:110
T end(T... args)
int extractSliceId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:54
int64_t extractCcdExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:124
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:229
STL class.
int64_t extractFpaExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:82
Include files required for standard LSST Exception handling.
T push_back(T... args)
int extractVisitId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:71
T data(T... args)
int64_t extractAmpExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:135
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:96
Interface for DbTsvStorage class.
int end
int countFitsHeaderCards(lsst::daf::base::PropertySet const &prop)
Definition: Utils.cc:214
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:202
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:190
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:169
Class for database storage with data loading from TSV files.
Definition: DbTsvStorage.h:66
T size(T... args)
#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
ndarray::Array< std::uint8_t, 1, 1 > stringToBytes(std::string const &str)
Encode a std::string as a vector of uint8.
Definition: Utils.cc:218
Interface for LogicalLocation class.
virtual void dropTable(std::string const &tableName)
Drop a table.
T reserve(T... args)