Isis 3 Programmer Reference
HiCalConf.cpp
Go to the documentation of this file.
1 
25 #include <iostream>
26 #include <numeric>
27 #include <sstream>
28 #include <vector>
29 
30 #include <QString>
31 
32 #include <SpiceUsr.h>
33 
34 #include "Brick.h"
35 #include "Cube.h"
36 #include "FileName.h"
37 #include "HiCalConf.h"
38 #include "HiCalUtil.h"
39 #include "IString.h"
40 #include "IException.h"
41 #include "Pvl.h"
42 #include "SpecialPixel.h"
43 #include "NaifStatus.h"
44 
45 using namespace std;
46 
47 namespace Isis {
48 
49 bool HiCalConf::_naifLoaded = false;
50 
54  HiCalConf::HiCalConf() : DbAccess() {
55  _profName.clear();
56  init();
57  }
58 
59 
64  HiCalConf::HiCalConf(Pvl &label) : DbAccess() {
65  _profName.clear();
66  init(label);
67  }
68 
75  HiCalConf::HiCalConf(Pvl &label, const QString &conf) :
76  DbAccess(Pvl(filepath(conf)).findObject("Hical", PvlObject::Traverse)) {
77  _profName.clear();
78  init(label);
79  }
80 
86  void HiCalConf::setLabel(Pvl &label) {
87  init(label);
88  return;
89  }
90 
103  QString HiCalConf::filepath(const QString &fname) const {
104  FileName efile(fname);
105  if (efile.isVersioned()) {
106  QString path(efile.originalPath());
107  if (!path.isEmpty()) path += "/";
108 
109  efile = efile.highestVersion();
110 
111  return (path + efile.name());
112  }
113  return (fname);
114  }
115 
116 
126  void HiCalConf::setConf(const QString &conf) {
127  load(Pvl(filepath(conf)).findObject("Hical", PvlObject::Traverse));
128  }
129 
139  void HiCalConf::selectProfile(const QString &profile) {
140  _profName = profile;
141  return;
142  }
143 
151  QString HiCalConf::getProfileName() const {
152  return (_profName);
153  }
154 
168  QString HiCalConf::getMatrixSource(const QString &name) const {
169  return (getMatrixSource(name, getMatrixProfile()));
170  }
171 
186  QString HiCalConf::getMatrixSource(const QString &name,
187  const DbProfile &matconf) const {
188 
189  QString mfile = parser(matconf.value(name),
190  getList(matconf,"OptionKeywords"),
191  matconf);
192 
193 // Translate and return
194  return (filepath(mfile));
195  }
196 
197  HiVector HiCalConf::getMatrix(const QString &name,
198  int expected_size) const {
199  return (getMatrix(name,getMatrixProfile(), expected_size));
200  }
201 
202 
203  QString HiCalConf::resolve(const QString &composite,
204  const DbProfile &matconf) const {
205  return (parser(composite,getList(matconf,"OptionKeywords"), matconf));
206  }
207 
208 
209 
233  HiVector HiCalConf::getMatrix(const QString &name,
234  const DbProfile &profile,
235  int expected_size) const {
236 
237  QString mfile = getMatrixSource(name, profile);
238 
239 // Crack the file and read the appropriate band
240  Cube cube;
241  cube.open(mfile);
242 
243 // Check for proper size if specifeid
244  if (expected_size != 0) {
245  if (cube.sampleCount() != expected_size) {
246  ostringstream mess;
247  mess << "Specifed matrix (" << name
248  << ") from file \"" << mfile
249  << "\" does not have expected samples (" << expected_size
250  << ") but has " << cube.sampleCount();
251  cube.close();
252  throw IException(IException::User, mess.str(), _FILEINFO_);
253  }
254  }
255 
256 // Read the specifed region
257  Brick bandio(cube.sampleCount(), 1, 1, Real);
258  bandio.SetBasePosition(1,1,getMatrixBand(profile));
259  cube.read(bandio);
260 
261 // Now create the output buffer with some TNT funny business
262  HiVector temp(cube.sampleCount(), bandio.DoubleBuffer());
263  HiVector out(cube.sampleCount());
264  out.inject(temp);
265  cube.close();
266  return (out);
267  }
268 
283  HiVector HiCalConf::getScalar(const QString &name,
284  const DbProfile &profile,
285  int expected_size) const {
286  int nvals = profile.count(name);
287 
288  // Check for proper size if specifeid
289  if (expected_size != 0) {
290  if (nvals != expected_size) {
291  ostringstream mess;
292  mess << "Specifed scalar (" << name
293  << ") does not have expected size (" << expected_size
294  << ") but has " << nvals;
295  throw IException(IException::User, mess.str(), _FILEINFO_);
296  }
297  }
298 // All is OK
299  HiVector mtx(nvals, Null);
300  for (int i = 0 ; i < nvals ; i++) {
301  mtx[i] = ToDouble(profile(name, i));
302  }
303  return (mtx);
304  }
305 
314  double HiCalConf::sunDistanceAU() {
316  loadNaifTiming();
317 
318  QString scStartTime = getKey("SpacecraftClockStartCount", "Instrument");
319  double obsStartTime;
320  scs2e_c (-74999,scStartTime.toLatin1().data(),&obsStartTime);
321 
322  QString targetName = getKey("TargetName", "Instrument");
323  if (targetName.toLower() == "sky" ||
324  targetName.toLower() == "cal" ||
325  targetName.toLower() == "phobos" ||
326  targetName.toLower() == "deimos") {
327  targetName = "Mars";
328  }
329  double sunv[3];
330  double lt;
331  (void) spkpos_c(targetName.toLatin1().data(), obsStartTime, "J2000", "LT+S", "sun",
332  sunv, &lt);
333  double sunkm = vnorm_c(sunv);
335  // Return in AU units
336  return (sunkm/1.49597870691E8);
337  }
338 
339 
354  int HiCalConf::getMatrixBand() const {
355  Pvl label = _label;
356  DbProfile parms = makeParameters(label);
357  return (getMatrixBand(parms));
358  }
359 
373  int HiCalConf::getMatrixBand(const DbProfile &p) const {
374  return (getChannelIndex(ToInteger(p("CCD")), ToInteger(p("CHANNEL"))));
375  }
376 
389  HiCalConf::ValueList HiCalConf::getList(const DbProfile &profile,
390  const QString &key) const {
391  ValueList slist;
392 
393 // Get keyword parameters
394  if ( profile.exists(key) ) {
395  int nvals = profile.count(key);
396  for (int i = 0 ; i < nvals ; i++) {
397  slist.push_back(profile.value(key, i));
398  }
399  }
400  return (slist);
401  }
402 
409 void HiCalConf::loadNaifTiming( ) {
411  if (!_naifLoaded) {
412 // Load the NAIF kernels to determine timing data
413  Isis::FileName leapseconds("$base/kernels/lsk/naif????.tls");
414  leapseconds = leapseconds.highestVersion();
415 
416  Isis::FileName sclk("$mro/kernels/sclk/MRO_SCLKSCET.?????.65536.tsc");
417  sclk = sclk.highestVersion();
418 
419  Isis::FileName pck("$base/kernels/spk/de???.bsp");
420  pck = pck.highestVersion();
421 
422  Isis::FileName sat("$base/kernels/spk/mar???.bsp");
423  sat = sat.highestVersion();
424 
425 // Load the kernels
426  QString lsk = leapseconds.expanded();
427  QString sClock = sclk.expanded();
428  QString pConstants = pck.expanded();
429  QString satConstants = sat.expanded();
430  furnsh_c(lsk.toLatin1().data());
431  furnsh_c(sClock.toLatin1().data());
432  furnsh_c(pConstants.toLatin1().data());
433  furnsh_c(satConstants.toLatin1().data());
435 
436 // Ensure it is loaded only once
437  _naifLoaded = true;
438  }
439  return;
440 }
441 
445 void HiCalConf::init() {
446  _label.clear();
447  return;
448 }
449 
459 void HiCalConf::init(Pvl &label) {
460  init();
461  _label = label;
462  return;
463 }
464 
476 PvlKeyword &HiCalConf::getKey(const QString &key,
477  const QString &group) {
478  if (!group.isEmpty()) {
479  PvlGroup &grp = _label.findGroup(group, Pvl::Traverse);
480  return (grp[key]);
481  }
482  else {
483  return (_label.findKeyword(key));
484  }
485 }
486 
505 DbProfile HiCalConf::getMatrixProfile(const QString &profile) const {
506  QString myprof = (!profile.isEmpty()) ? profile : _profName;
507  DbProfile matconf = getProfile(myprof);
508  if (!matconf.isValid()) {
509  ostringstream mess;
510  mess << "Specifed matrix profile (" << matconf.Name()
511  << ") does not exist or is invalid!";
512  throw IException(IException::User, mess.str(), _FILEINFO_);
513  }
514 
515  // Profile the label and merge them. Order is important.
516  matconf = DbProfile(getLabelProfile(matconf), matconf, matconf.Name());
517 
518  // Add special parameters. Again, order is important.
519  matconf = DbProfile(matconf, makeParameters(matconf), matconf.Name());
520 
521 // Load any optional ones
522  ValueList profkeys = getList(matconf,"OptionKeywords");
523  ValueList proforder = getList(matconf,"ProfileOptions");
524  QString pName(matconf.Name());
525  for (unsigned int i = 0 ; i < proforder.size() ; i++) {
526  QString profile = parser(proforder[i], profkeys, matconf);
527  if (profileExists(profile)) {
528  pName += "+[" + profile + "]";
529  matconf = DbProfile(matconf,getProfile(profile), pName);
530  }
531  }
532  return (matconf);
533 }
534 
535 
536 DbProfile HiCalConf::getLabelProfile(const DbProfile &profile) const {
537  DbProfile lblprof("Label");
538  if ( profile.exists("LabelGroups") ) {
539  int ngroups = profile.count("LabelGroups");
540  Pvl label = _label;
541  for ( int g = 0 ; g < ngroups ; g++ ) {
542  QString group = profile("LabelGroups", g);
543  PvlGroup grp = label.findGroup(group, Pvl::Traverse);
544  lblprof = DbProfile(lblprof,DbProfile(grp));
545  }
546  }
547  return (lblprof);
548 }
549 
550 int HiCalConf::getChannelIndex(const int &ccd, const int &channel) const {
551  return(1 + (ccd*2) + channel);
552 }
553 
554 DbProfile HiCalConf::makeParameters(Pvl &label) const {
555  PvlGroup inst = label.findGroup("Instrument", Pvl::Traverse);
556  DbProfile parms("Parameters");
557 
558  int ccd = CpmmToCcd((int) inst["CpmmNumber"]);
559  int channel = inst["ChannelNumber"];
560  parms.add("CCD",ToString(ccd));
561  parms.add("CHANNEL", ToString(channel));
562  parms.add("TDI", inst["Tdi"]);
563  parms.add("BIN", inst["Summing"]);
564  parms.add("FILTER", CcdToFilter(ccd));
565  parms.add("CCDCHANNELINDEX", ToString(getChannelIndex(ccd, channel)));
566  return (parms);
567 }
568 
569 DbProfile HiCalConf::makeParameters(const DbProfile &profile) const {
570  DbProfile parms("Parameters");
571  int ccd = CpmmToCcd(ToInteger(profile("CpmmNumber")));
572  int channel = ToInteger(profile("ChannelNumber"));
573  parms.add("CCD",ToString(ccd));
574  parms.add("CHANNEL", ToString(channel));
575  parms.add("TDI", profile("Tdi"));
576  parms.add("BIN", profile("Summing"));
577  parms.add("FILTER", CcdToFilter(ccd));
578  parms.add("CCDCHANNELINDEX", ToString(getChannelIndex(ccd, channel)));
579  return (parms);
580 }
581 
582 QString HiCalConf::makePattern(const QString &str) const {
583  return (QString("{" + str + "}"));
584 }
585 
597 QString HiCalConf::parser(const QString &s, const ValueList &vlist,
598  const DbProfile &prof) const {
599  QString sout(s);
600 
601  ValueList::const_iterator ciVlist;
602  for ( ciVlist = vlist.begin() ; ciVlist != vlist.end() ; ++ciVlist ) {
603  QString str(*ciVlist);
604  if ( prof.exists(str) ) {
605  sout = sout.replace(makePattern(str), prof(str));
606  }
607  }
608 
609  return (sout);
610 }
611 
612 } // namespace Isis
const double Null
Value for an Isis Null pixel.
Definition: SpecialPixel.h:110
int CpmmToCcd(int cpmm)
Convert HiRISE Cpmm number to Ccd number.
Definition: HiCalUtil.h:85
File name manipulation and expansion.
Definition: FileName.h:116
Namespace for the standard library.
Search child objects.
Definition: PvlObject.h:170
int ToInteger(const T &value)
Helper function to convert values to Integers.
Definition: HiCalUtil.h:236
QString CcdToFilter(int ccd)
Convert HiRISE Ccd number to string filter name.
Definition: HiCalUtil.h:100
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
A type of error that could only have occurred due to a mistake on the user&#39;s part (e...
Definition: IException.h:142
double ToDouble(const T &value)
Helper function to convert values to doubles.
Definition: HiCalUtil.h:248
static void CheckErrors(bool resetNaif=true)
This method looks for any naif errors that might have occurred.
Definition: NaifStatus.cpp:43
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
QString ToString(const T &value)
Helper function to convert values to strings.
Definition: HiCalUtil.h:260
TNT::Array1D< double > HiVector
1-D Buffer
Definition: HiCalTypes.h:40