36 #include "boost/format.hpp" 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" 45 namespace ex = lsst::pex::exceptions;
47 using lsst::pex::policy::Policy;
48 using lsst::daf::persistence::LogicalLocation;
52 namespace formatters {
67 std::string formatFitsPropertiesImpl(std::vector<std::string>
const& paramNames,
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);
77 if (
name.size() > 8) {
78 out +=
"HIERARCH = " +
name;
80 out = (boost::format(
"%-8s= ") %
name).str();
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();
95 int const len = out.size();
97 out += std::string(80 - len,
' ');
99 out = out.substr(0, 80);
111 if (properties->isArray(
"sliceId")) {
112 throw LSST_EXCEPT(ex::RuntimeError,
"\"sliceId\" property has multiple values");
114 int sliceId = properties->getAsInt(
"sliceId");
116 throw LSST_EXCEPT(ex::RangeError,
"negative \"sliceId\"");
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 \"");
128 if (properties->isArray(
"visitId")) {
129 throw LSST_EXCEPT(ex::RuntimeError,
"\"visitId\" property has multiple values");
131 int visitId = properties->getAsInt(
"visitId");
133 throw LSST_EXCEPT(ex::RangeError,
"negative \"visitId\"");
139 if (properties->isArray(
"fpaExposureId")) {
140 throw LSST_EXCEPT(ex::RuntimeError,
"\"fpaExposureId\" property has multiple values");
142 int64_t fpaExposureId = properties->getAsInt64(
"fpaExposureId");
143 if (fpaExposureId < 0) {
144 throw LSST_EXCEPT(ex::RangeError,
"negative \"fpaExposureId\"");
146 if ((fpaExposureId & 0xfffffffe00000000LL) != 0
LL) {
147 throw LSST_EXCEPT(ex::RangeError,
"\"fpaExposureId\" is too large");
149 return fpaExposureId;
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");
156 int ccdId = properties->getAsInt(
"ccdId");
158 throw LSST_EXCEPT(ex::RangeError,
"negative \"ccdId\"");
161 throw LSST_EXCEPT(ex::RangeError,
"\"ccdId\" is too large");
163 return static_cast<int>(ccdId);
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");
170 int ampId = properties->getAsInt(
"ampId");
172 throw LSST_EXCEPT(ex::RangeError,
"negative \"ampId\"");
175 throw LSST_EXCEPT(ex::RangeError,
"\"ampId\" is too large");
181 if (properties->isArray(
"ccdExposureId")) {
182 throw LSST_EXCEPT(ex::RuntimeError,
"\"ccdExposureId\" property has multiple values");
184 int64_t ccdExposureId = properties->getAsInt64(
"ccdExposureId");
185 if (ccdExposureId < 0) {
186 throw LSST_EXCEPT(ex::RangeError,
"negative \"ccdExposureId\"");
188 return ccdExposureId;
192 if (properties->isArray(
"ampExposureId")) {
193 throw LSST_EXCEPT(ex::RuntimeError,
"\"ampExposureId\" property has multiple values");
195 int64_t ampExposureId = properties->getAsInt64(
"ampExposureId");
196 if (ampExposureId < 0) {
197 throw LSST_EXCEPT(ex::RangeError,
"negative \"ampExposureId\"");
199 return ampExposureId;
202 std::string
const getItemName(std::shared_ptr<PropertySet const>
const& properties) {
204 throw LSST_EXCEPT(ex::InvalidParameterError,
"Null std::shared_ptr<PropertySet>");
206 if (properties->isArray(
"itemName")) {
207 throw LSST_EXCEPT(ex::InvalidParameterError,
"\"itemName\" property has multiple values");
209 return properties->getAsString(
"itemName");
213 if (properties && properties->exists(name)) {
214 return properties->getAsBool(name);
219 std::string
const getTableName(std::shared_ptr<Policy const>
const& policy,
220 std::shared_ptr<PropertySet const>
const& properties) {
222 return LogicalLocation(policy->getString(itemName +
".tableNamePattern"), properties).locString();
226 std::shared_ptr<PropertySet const>
const& properties) {
228 std::string pattern(policy->getString(itemName +
".tableNamePattern"));
230 if (properties->exists(itemName +
".numSlices")) {
231 numSlices = properties->getAsInt(itemName +
".numSlices");
233 if (numSlices <= 0) {
234 throw LSST_EXCEPT(ex::RuntimeError, itemName +
" \".numSlices\" property value must be positive");
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());
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) {
251 std::string model(policy->getString(itemName +
".templateTableName"));
253 lsst::daf::persistence::DbTsvStorage db;
254 db.setPersistLocation(location);
255 db.createTableFromTemplate(name, model);
259 std::shared_ptr<lsst::pex::policy::Policy const>
const& policy,
260 std::shared_ptr<PropertySet const>
const& properties) {
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) {
271 auto paramNames = prop.paramNames(
false);
272 return formatFitsPropertiesImpl(paramNames, prop);
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);
283 return formatFitsPropertiesImpl(paramNames, prop);
287 return prop.paramNames(
false).size();
291 auto nbytes = str.size() *
sizeof(char) /
sizeof(std::uint8_t);
292 std::uint8_t
const* byteCArr =
reinterpret_cast<std::uint8_t const*
>(str.data());
293 auto shape = ndarray::makeVector(nbytes);
294 auto strides = ndarray::makeVector(1);
297 ndarray::Array<std::uint8_t const, 1, 1> localArray = ndarray::external(byteCArr, shape, strides);
298 return ndarray::copy(localArray);
301 std::string
bytesToString(ndarray::Array<std::uint8_t const, 1, 1>
const& bytes) {
302 auto nchars = bytes.size() *
sizeof(std::uint8_t) /
sizeof(
char);
303 char const* charCArr =
reinterpret_cast<char const*
>(bytes.getData());
304 return std::string(charCArr, nchars);
table::Key< std::string > name
lsst::daf::base::PropertyList PropertyList
lsst::daf::base::PropertySet PropertySet
A base class for image defects.