LSST Applications  21.0.0-1-g8760c09+64c1bc5aa5,21.0.0-1-ga51b5d4+9915f4c1f0,21.0.0-111-gf06e6c5c+fee76ed8f6,21.0.0-14-g3bd782b+25c619aa25,21.0.0-17-gd94932e+0bf297642c,21.0.0-2-g103fe59+ccc08e5a44,21.0.0-2-g45278ab+64c1bc5aa5,21.0.0-2-g5242d73+7a03d65b13,21.0.0-2-g7f82c8f+cb30300d2e,21.0.0-2-gde069b7+4f46bdaea8,21.0.0-2-gfc62afb+7a03d65b13,21.0.0-20-g1e553c2+8d8bef21d9,21.0.0-20-g3b2d1f0+b95b9e7887,21.0.0-21-g51ee50f4+27a29155c2,21.0.0-21-gc8894c7+552aca6dca,21.0.0-27-gcbf119a+71dcc0c312,21.0.0-3-g1b65d06+c212a16cbd,21.0.0-3-g357aad2+6b2f77ef8c,21.0.0-3-g4a4ce7f+7a03d65b13,21.0.0-3-g4be5c26+7a03d65b13,21.0.0-3-ge02ed75+552aca6dca,21.0.0-3-gf9d8b05+cb30300d2e,21.0.0-30-g727f315e+692e5d6c44,21.0.0-4-g7dab645+80ff539b09,21.0.0-45-g119a6608a+532d44eec1,21.0.0-5-g5f8d297+a12ef938c8,21.0.0-5-g8c1d971+7e5b4c34a6,21.0.0-5-gcc89fd6+b18c5f6b76,21.0.0-5-gd00fb1e+cabc0fa2dc,21.0.0-6-g1930a60+b18c5f6b76,21.0.0-6-gc54ca17+f4e773f57d,21.0.0-6-gc675373+7a03d65b13,21.0.0-7-g0503b2e+18535a8d22,21.0.0-7-g7aa11f2+552aca6dca,21.0.0-7-gdf92d54+64c1bc5aa5,21.0.0-9-g7058be7+2d726afa20,master-ga86695c5c8+552aca6dca,master-gcc5351303a+ccc08e5a44,w.2021.25
LSST Data Management Base Package
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
table::Key< std::string > name
Definition: Amplifier.cc:116
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Interface for LogicalLocation class.
table::Key< table::Array< std::uint8_t > > bytes
Definition: python.h:135
Class for storing generic metadata.
Definition: PropertySet.h:66
std::vector< std::string > paramNames(bool topLevelOnly=true) const
A variant of names that excludes the names of subproperties.
Class for logical location of a persisted Persistable instance.
Reports invalid arguments.
Definition: Runtime.h:66
Reports when the result of an operation cannot be represented by the destination type.
Definition: Runtime.h:115
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104
T data(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:162
int extractSliceId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:51
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
int64_t extractAmpExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:132
int extractCcdId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:93
int extractAmpId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:107
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
int64_t extractCcdExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:121
int extractVisitId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:68
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
int64_t extractFpaExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Definition: Utils.cc:79
int countFitsHeaderCards(lsst::daf::base::PropertySet const &prop)
Definition: Utils.cc:160
A base class for image defects.
T size(T... args)