34 #include "boost/format.hpp"
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"
43 namespace ex = lsst::pex::exceptions;
45 using lsst::pex::policy::Policy;
46 using lsst::daf::persistence::LogicalLocation;
50 namespace formatters {
53 if (properties->isArray(
"sliceId")) {
54 throw LSST_EXCEPT(ex::RuntimeError,
"\"sliceId\" property has multiple values");
56 int sliceId = properties->getAsInt(
"sliceId");
58 throw LSST_EXCEPT(ex::RangeError,
"negative \"sliceId\"");
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 \"");
70 if (properties->isArray(
"visitId")) {
71 throw LSST_EXCEPT(ex::RuntimeError,
"\"visitId\" property has multiple values");
73 int visitId = properties->getAsInt(
"visitId");
75 throw LSST_EXCEPT(ex::RangeError,
"negative \"visitId\"");
81 if (properties->isArray(
"fpaExposureId")) {
82 throw LSST_EXCEPT(ex::RuntimeError,
"\"fpaExposureId\" property has multiple values");
84 int64_t fpaExposureId = properties->getAsInt64(
"fpaExposureId");
85 if (fpaExposureId < 0) {
86 throw LSST_EXCEPT(ex::RangeError,
"negative \"fpaExposureId\"");
88 if ((fpaExposureId & 0xfffffffe00000000LL) != 0
LL) {
89 throw LSST_EXCEPT(ex::RangeError,
"\"fpaExposureId\" is too large");
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");
98 int ccdId = properties->getAsInt(
"ccdId");
100 throw LSST_EXCEPT(ex::RangeError,
"negative \"ccdId\"");
103 throw LSST_EXCEPT(ex::RangeError,
"\"ccdId\" is too large");
105 return static_cast<int>(ccdId);
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");
112 int ampId = properties->getAsInt(
"ampId");
114 throw LSST_EXCEPT(ex::RangeError,
"negative \"ampId\"");
117 throw LSST_EXCEPT(ex::RangeError,
"\"ampId\" is too large");
123 if (properties->isArray(
"ccdExposureId")) {
124 throw LSST_EXCEPT(ex::RuntimeError,
"\"ccdExposureId\" property has multiple values");
126 int64_t ccdExposureId = properties->getAsInt64(
"ccdExposureId");
127 if (ccdExposureId < 0) {
128 throw LSST_EXCEPT(ex::RangeError,
"negative \"ccdExposureId\"");
130 return ccdExposureId;
134 if (properties->isArray(
"ampExposureId")) {
135 throw LSST_EXCEPT(ex::RuntimeError,
"\"ampExposureId\" property has multiple values");
137 int64_t ampExposureId = properties->getAsInt64(
"ampExposureId");
138 if (ampExposureId < 0) {
139 throw LSST_EXCEPT(ex::RangeError,
"negative \"ampExposureId\"");
141 return ampExposureId;
144 std::string
const getItemName(std::shared_ptr<PropertySet const>
const& properties) {
146 throw LSST_EXCEPT(ex::InvalidParameterError,
"Null std::shared_ptr<PropertySet>");
148 if (properties->isArray(
"itemName")) {
149 throw LSST_EXCEPT(ex::InvalidParameterError,
"\"itemName\" property has multiple values");
151 return properties->getAsString(
"itemName");
155 if (properties && properties->exists(name)) {
156 return properties->getAsBool(name);
161 std::string
const getTableName(std::shared_ptr<Policy const>
const& policy,
162 std::shared_ptr<PropertySet const>
const& properties) {
164 return LogicalLocation(policy->getString(itemName +
".tableNamePattern"), properties).locString();
168 std::shared_ptr<PropertySet const>
const& properties) {
170 std::string pattern(policy->getString(itemName +
".tableNamePattern"));
172 if (properties->exists(itemName +
".numSlices")) {
173 numSlices = properties->getAsInt(itemName +
".numSlices");
175 if (numSlices <= 0) {
176 throw LSST_EXCEPT(ex::RuntimeError, itemName +
" \".numSlices\" property value must be positive");
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());
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) {
193 std::string model(policy->getString(itemName +
".templateTableName"));
195 lsst::daf::persistence::DbTsvStorage db;
196 db.setPersistLocation(location);
197 db.createTableFromTemplate(name, model);
201 std::shared_ptr<lsst::pex::policy::Policy const>
const& policy,
202 std::shared_ptr<PropertySet const>
const& properties) {
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) {
213 typedef std::vector<std::string> NameList;
216 NameList paramNames = prop->paramNames(
false);
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);
223 std::string out =
"";
224 if (name.size() > 8) {
225 out +=
"HIERARCH = " +
name;
227 out = (boost::format(
"%-8s= ") %
name).str();
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();
238 int const len = out.size();
240 out += std::string(80 - len,
' ');
242 out = out.substr(0, 80);
252 return prop->paramNames(
false).size();
table::Key< std::string > name
lsst::daf::base::PropertySet PropertySet