LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
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 #include "lsst/pex/exceptions.h"
41 
42 using std::int64_t;
43 namespace ex = lsst::pex::exceptions;
46 
47 namespace lsst {
48 namespace afw {
49 namespace formatters {
50 
52  if (properties->isArray("sliceId")) {
53  throw LSST_EXCEPT(ex::RuntimeError, "\"sliceId\" property has multiple values");
54  }
55  int sliceId = properties->getAsInt("sliceId");
56  if (sliceId < 0) {
57  throw LSST_EXCEPT(ex::RangeError, "negative \"sliceId\"");
58  }
59  if (properties->exists("universeSize") && !properties->isArray("universeSize")) {
60  int universeSize = properties->getAsInt("universeSize");
61  if (sliceId >= universeSize) {
62  throw LSST_EXCEPT(ex::RangeError, "\"sliceId\" must be less than \"universeSize \"");
63  }
64  }
65  return sliceId;
66 }
67 
69  if (properties->isArray("visitId")) {
70  throw LSST_EXCEPT(ex::RuntimeError, "\"visitId\" property has multiple values");
71  }
72  int visitId = properties->getAsInt("visitId");
73  if (visitId < 0) {
74  throw LSST_EXCEPT(ex::RangeError, "negative \"visitId\"");
75  }
76  return visitId;
77 }
78 
80  if (properties->isArray("fpaExposureId")) {
81  throw LSST_EXCEPT(ex::RuntimeError, "\"fpaExposureId\" property has multiple values");
82  }
83  int64_t fpaExposureId = properties->getAsInt64("fpaExposureId");
84  if (fpaExposureId < 0) {
85  throw LSST_EXCEPT(ex::RangeError, "negative \"fpaExposureId\"");
86  }
87  if ((fpaExposureId & 0xfffffffe00000000LL) != 0LL) {
88  throw LSST_EXCEPT(ex::RangeError, "\"fpaExposureId\" is too large");
89  }
90  return fpaExposureId;
91 }
92 
94  if (properties->isArray("ccdId")) {
95  throw LSST_EXCEPT(ex::RuntimeError, "\"ccdId\" property has multiple values");
96  }
97  int ccdId = properties->getAsInt("ccdId");
98  if (ccdId < 0) {
99  throw LSST_EXCEPT(ex::RangeError, "negative \"ccdId\"");
100  }
101  if (ccdId > 255) {
102  throw LSST_EXCEPT(ex::RangeError, "\"ccdId\" is too large");
103  }
104  return static_cast<int>(ccdId);
105 }
106 
108  if (properties->isArray("ampId")) {
109  throw LSST_EXCEPT(ex::RuntimeError, "\"ampId\" property has multiple values");
110  }
111  int ampId = properties->getAsInt("ampId");
112  if (ampId < 0) {
113  throw LSST_EXCEPT(ex::RangeError, "negative \"ampId\"");
114  }
115  if (ampId > 63) {
116  throw LSST_EXCEPT(ex::RangeError, "\"ampId\" is too large");
117  }
118  return (extractCcdId(properties) << 6) + ampId;
119 }
120 
122  if (properties->isArray("ccdExposureId")) {
123  throw LSST_EXCEPT(ex::RuntimeError, "\"ccdExposureId\" property has multiple values");
124  }
125  int64_t ccdExposureId = properties->getAsInt64("ccdExposureId");
126  if (ccdExposureId < 0) {
127  throw LSST_EXCEPT(ex::RangeError, "negative \"ccdExposureId\"");
128  }
129  return ccdExposureId;
130 }
131 
133  if (properties->isArray("ampExposureId")) {
134  throw LSST_EXCEPT(ex::RuntimeError, "\"ampExposureId\" property has multiple values");
135  }
136  int64_t ampExposureId = properties->getAsInt64("ampExposureId");
137  if (ampExposureId < 0) {
138  throw LSST_EXCEPT(ex::RangeError, "negative \"ampExposureId\"");
139  }
140  return ampExposureId;
141 }
142 
144  if (!properties) {
145  throw LSST_EXCEPT(ex::InvalidParameterError, "Null std::shared_ptr<PropertySet>");
146  }
147  if (properties->isArray("itemName")) {
148  throw LSST_EXCEPT(ex::InvalidParameterError, "\"itemName\" property has multiple values");
149  }
150  return properties->getAsString("itemName");
151 }
152 
154  if (properties && properties->exists(name)) {
155  return properties->getAsBool(name);
156  }
157  return false;
158 }
159 
160 int countFitsHeaderCards(lsst::daf::base::PropertySet const& prop) { return prop.paramNames(false).size(); }
161 
162 ndarray::Array<std::uint8_t, 1, 1> stringToBytes(std::string const& str) {
163  auto nbytes = str.size() * sizeof(char) / sizeof(std::uint8_t);
164  std::uint8_t const* byteCArr = reinterpret_cast<std::uint8_t const*>(str.data());
165  auto shape = ndarray::makeVector(nbytes);
166  auto strides = ndarray::makeVector(1);
167  // Make an Array that shares memory with `str` (and does not free that memory when destroyed),
168  // then return a copy; this is simpler than manually copying the data into a newly allocated array
169  ndarray::Array<std::uint8_t const, 1, 1> localArray = ndarray::external(byteCArr, shape, strides);
170  return ndarray::copy(localArray);
171 }
172 
173 std::string bytesToString(ndarray::Array<std::uint8_t const, 1, 1> const& bytes) {
174  auto nchars = bytes.size() * sizeof(std::uint8_t) / sizeof(char);
175  char const* charCArr = reinterpret_cast<char const*>(bytes.getData());
176  return std::string(charCArr, nchars);
177 }
178 
179 } // namespace formatters
180 } // namespace afw
181 } // namespace lsst
lsst::daf::base::PropertySet::getAsInt
int getAsInt(std::string const &name) const
Get the last value for a bool/char/short/int property name (possibly hierarchical).
lsst::daf::base::PropertySet::getAsBool
bool getAsBool(std::string const &name) const
Get the last value for a bool property name (possibly hierarchical).
std::string
STL class.
std::shared_ptr
STL class.
lsst::afw::formatters::extractOptionalFlag
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:153
lsst::afw::formatters::extractSliceId
int extractSliceId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:51
std::vector::size
T size(T... args)
lsst.pex::exceptions::RangeError
Reports when the result of an operation cannot be represented by the destination type.
Definition: Runtime.h:115
lsst::afw
Definition: imageAlgorithm.dox:1
lsst::daf::base::PropertySet::isArray
bool isArray(std::string const &name) const
Determine if a name (possibly hierarchical) has multiple values.
lsst::afw::formatters::extractAmpId
int extractAmpId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:107
lsst::daf::base::PropertySet::exists
bool exists(std::string const &name) const
Determine if a name (possibly hierarchical) exists.
lsst::afw::formatters::extractCcdExposureId
int64_t extractCcdExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:121
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
lsst::daf::persistence::LogicalLocation
Class for logical location of a persisted Persistable instance.
Definition: LogicalLocation.h:59
lsst::afw::formatters::extractCcdId
int extractCcdId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:93
lsst::afw::formatters::bytesToString
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:173
lsst::afw::formatters::extractVisitId
int extractVisitId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:68
lsst::daf::base::PropertySet::getAsInt64
int64_t getAsInt64(std::string const &name) const
Get the last value for a bool/char/short/int/int64_t property name (possibly hierarchical).
lsst::daf::base::PropertySet::getAsString
std::string getAsString(std::string const &name) const
Get the last value for a string property name (possibly hierarchical).
PropertySet.h
lsst::afw::formatters::extractAmpExposureId
int64_t extractAmpExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:132
lsst::afw::formatters::extractFpaExposureId
int64_t extractFpaExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:79
std::int64_t
lsst::afw::formatters::countFitsHeaderCards
int countFitsHeaderCards(lsst::daf::base::PropertySet const &prop)
Definition: Utils.cc:160
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::daf::base::PropertySet::paramNames
std::vector< std::string > paramNames(bool topLevelOnly=true) const
A variant of names that excludes the names of subproperties.
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::afw::formatters::stringToBytes
ndarray::Array< std::uint8_t, 1, 1 > stringToBytes(std::string const &str)
Encode a std::string as a vector of uint8.
Definition: Utils.cc:162
bytes
table::Key< table::Array< std::uint8_t > > bytes
Definition: Transform.cc:199
LogicalLocation.h
Interface for LogicalLocation class.
lsst.pex::exceptions::InvalidParameterError
Reports invalid arguments.
Definition: Runtime.h:66
Utils.h
lsst::daf::base::PropertySet
Class for storing generic metadata.
Definition: PropertySet.h:67
lsst.pex::exceptions
Definition: Exception.h:37
std::string::data
T data(T... args)
lsst::afw::formatters::getItemName
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:143
lsst.pex::exceptions::RuntimeError
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104
exceptions.h