25#include "IException.h"
26#include "Preference.h"
29#include "SpecialPixel.h"
30#include "NaifStatus.h"
36bool HiCalConf::_naifLoaded =
false;
41 HiCalConf::HiCalConf() :
DbAccess() {
51 HiCalConf::HiCalConf(Pvl &label) : DbAccess() {
62 HiCalConf::HiCalConf(Pvl &label,
const QString &conf) :
63 DbAccess(Pvl(filepath(conf)).findObject(
"Hical", PvlObject::Traverse)) {
73 void HiCalConf::setLabel(Pvl &label) {
90 QString HiCalConf::filepath(
const QString &fname)
const {
91 FileName efile(fname);
92 if (efile.isVersioned()) {
93 QString path(efile.originalPath());
94 if (!path.isEmpty()) path +=
"/";
96 efile = efile.highestVersion();
98 return (path + efile.name());
113 void HiCalConf::setConf(
const QString &conf) {
114 load(Pvl(filepath(conf)).findObject(
"Hical", PvlObject::Traverse));
126 void HiCalConf::selectProfile(
const QString &profile) {
138 QString HiCalConf::getProfileName()
const {
155 QString HiCalConf::getMatrixSource(
const QString &name)
const {
156 return (getMatrixSource(name, getMatrixProfile()));
173 QString HiCalConf::getMatrixSource(
const QString &name,
174 const DbProfile &matconf)
const {
176 QString mfile = parser(matconf.value(name),
177 getList(matconf,
"OptionKeywords"),
181 return (filepath(mfile));
184 HiVector HiCalConf::getMatrix(
const QString &name,
185 int expected_size)
const {
186 return (getMatrix(name,getMatrixProfile(), expected_size));
190 QString HiCalConf::resolve(
const QString &composite,
191 const DbProfile &matconf)
const {
192 return (parser(composite,getList(matconf,
"OptionKeywords"), matconf));
220 HiVector HiCalConf::getMatrix(
const QString &name,
221 const DbProfile &profile,
222 int expected_size)
const {
224 QString mfile = getMatrixSource(name, profile);
231 if (expected_size != 0) {
232 if (cube.sampleCount() != expected_size) {
234 mess <<
"Specifed matrix (" << name
235 <<
") from file \"" << mfile
236 <<
"\" does not have expected samples (" << expected_size
237 <<
") but has " << cube.sampleCount();
239 throw IException(IException::User, mess.str(), _FILEINFO_);
244 Brick bandio(cube.sampleCount(), 1, 1, Real);
245 bandio.SetBasePosition(1,1,getMatrixBand(profile));
249 HiVector temp(cube.sampleCount(), bandio.DoubleBuffer());
270 HiVector HiCalConf::getScalar(
const QString &name,
271 const DbProfile &profile,
272 int expected_size)
const {
273 int nvals = profile.count(name);
276 if (expected_size != 0) {
277 if (nvals != expected_size) {
279 mess <<
"Specifed scalar (" << name
280 <<
") does not have expected size (" << expected_size
281 <<
") but has " << nvals;
282 throw IException(IException::User, mess.str(), _FILEINFO_);
287 for (
int i = 0 ; i < nvals ; i++) {
288 mtx[i] =
ToDouble(profile(name, i));
301 double HiCalConf::sunDistanceAU(Cube *cube) {
305 cam = cube->camera();
306 cam->SetImage(0.5, 0.5);
307 sunkm = cam->sunToBodyDist();
308 NaifStatus::CheckErrors();
310 catch (IException &e) {
312 QString scStartTime = getKey(
"SpacecraftClockStartCount",
"Instrument");
313 NaifStatus::CheckErrors();
314 bool useWeb = QString(Preference::Preferences().findGroup(
"SpiceQL")[
"UseSpiceQL"]).toUpper() ==
"TRUE";
315 double obsStartTime = SpiceQL::strSclkToEt(-74999, scStartTime.toLatin1().data(),
"hirise", useWeb).first;
317 QString targetName = getKey(
"TargetName",
"Instrument");
318 if (targetName.toLower() ==
"sky" ||
319 targetName.toLower() ==
"cal" ||
320 targetName.toLower() ==
"phobos" ||
321 targetName.toLower() ==
"deimos") {
326 std::vector<double> etStart = {obsStartTime};
327 vector<vector<double>> sunLt = SpiceQL::getTargetStates(etStart, targetName.toLatin1().data(),
"sun",
"J2000",
"LT+S",
"hirise", {
"reconstructed"}, {
"reconstructed"}, useWeb).first;
328 std::copy(sunLt[0].begin(), sunLt[0].begin()+3, sunv);
330 sunkm = vnorm_c(sunv);
332 NaifStatus::CheckErrors();
334 catch(IException &e) {
335 QString msg =
"Unable to determine the distance from the target to the sun";
336 throw IException(e, IException::User, msg, _FILEINFO_);
341 return (sunkm/1.49597870691E8);
359 int HiCalConf::getMatrixBand()
const {
361 DbProfile parms = makeParameters(label);
362 return (getMatrixBand(parms));
378 int HiCalConf::getMatrixBand(
const DbProfile &p)
const {
394 HiCalConf::ValueList HiCalConf::getList(
const DbProfile &profile,
395 const QString &key)
const {
399 if ( profile.exists(key) ) {
400 int nvals = profile.count(key);
401 for (
int i = 0 ; i < nvals ; i++) {
402 slist.push_back(profile.value(key, i));
411void HiCalConf::init() {
425void HiCalConf::init(Pvl &label) {
442PvlKeyword &HiCalConf::getKey(
const QString &key,
443 const QString &group) {
444 if (!group.isEmpty()) {
445 PvlGroup &grp = _label.findGroup(group, Pvl::Traverse);
449 return (_label.findKeyword(key));
471DbProfile HiCalConf::getMatrixProfile(
const QString &profile)
const {
472 QString myprof = (!profile.isEmpty()) ? profile : _profName;
473 DbProfile matconf = getProfile(myprof);
474 if (!matconf.isValid()) {
476 mess <<
"Specifed matrix profile (" << matconf.Name()
477 <<
") does not exist or is invalid!";
478 throw IException(IException::User, mess.str(), _FILEINFO_);
482 matconf = DbProfile(getLabelProfile(matconf), matconf, matconf.Name());
485 matconf = DbProfile(matconf, makeParameters(matconf), matconf.Name());
488 ValueList profkeys = getList(matconf,
"OptionKeywords");
489 ValueList proforder = getList(matconf,
"ProfileOptions");
490 QString pName(matconf.Name());
491 for (
unsigned int i = 0 ; i < proforder.size() ; i++) {
492 QString profile = parser(proforder[i], profkeys, matconf);
493 if (profileExists(profile)) {
494 pName +=
"+[" + profile +
"]";
495 matconf = DbProfile(matconf,getProfile(profile), pName);
502DbProfile HiCalConf::getLabelProfile(
const DbProfile &profile)
const {
503 DbProfile lblprof(
"Label");
504 if ( profile.exists(
"LabelGroups") ) {
505 int ngroups = profile.count(
"LabelGroups");
507 for (
int g = 0 ; g < ngroups ; g++ ) {
508 QString group = profile(
"LabelGroups", g);
509 PvlGroup grp = label.findGroup(group, Pvl::Traverse);
510 lblprof = DbProfile(lblprof,DbProfile(grp));
516int HiCalConf::getChannelIndex(
const int &ccd,
const int &channel)
const {
517 return(1 + (ccd*2) + channel);
520DbProfile HiCalConf::makeParameters(Pvl &label)
const {
521 PvlGroup inst = label.findGroup(
"Instrument", Pvl::Traverse);
522 DbProfile parms(
"Parameters");
524 int ccd =
CpmmToCcd((
int) inst[
"CpmmNumber"]);
525 int channel = inst[
"ChannelNumber"];
527 parms.add(
"CHANNEL",
ToString(channel));
528 parms.add(
"TDI", inst[
"Tdi"]);
529 parms.add(
"BIN", inst[
"Summing"]);
531 parms.add(
"CCDCHANNELINDEX",
ToString(getChannelIndex(ccd, channel)));
535DbProfile HiCalConf::makeParameters(
const DbProfile &profile)
const {
536 DbProfile parms(
"Parameters");
538 int channel =
ToInteger(profile(
"ChannelNumber"));
540 parms.add(
"CHANNEL",
ToString(channel));
541 parms.add(
"TDI", profile(
"Tdi"));
542 parms.add(
"BIN", profile(
"Summing"));
544 parms.add(
"CCDCHANNELINDEX",
ToString(getChannelIndex(ccd, channel)));
548QString HiCalConf::makePattern(
const QString &str)
const {
549 return (QString(
"{" + str +
"}"));
563QString HiCalConf::parser(
const QString &s,
const ValueList &vlist,
564 const DbProfile &prof)
const {
567 ValueList::const_iterator ciVlist;
568 for ( ciVlist = vlist.begin() ; ciVlist != vlist.end() ; ++ciVlist ) {
569 QString str(*ciVlist);
570 if ( prof.exists(str) ) {
571 sout = sout.replace(makePattern(str), prof(str));
DbAccess manages programatic access to a database through profiles.
This is free and unencumbered software released into the public domain.
QString CcdToFilter(int ccd)
Convert HiRISE Ccd number to string filter name.
int CpmmToCcd(int cpmm)
Convert HiRISE Cpmm number to Ccd number.
TNT::Array1D< double > HiVector
1-D Buffer
QString ToString(const T &value)
Helper function to convert values to strings.
double ToDouble(const T &value)
Helper function to convert values to doubles.
int ToInteger(const T &value)
Helper function to convert values to Integers.
Namespace for the standard library.