Unverified Commit 3eec82d1 authored by AustinSanders's avatar AustinSanders Committed by GitHub
Browse files

Updated kaguyatc2isis to allow ingestion of data provided by JAXA online archive (#3713)

* Updated to allow ingestion of data provided by JAXA online archive

* Listed SLN-L-TC-5-MORNING-MAP-V4.0 as a supported format.

* Added tests + testing data

* Replaced several label parsing tests with cube attribute tests.
parent 3b955cd8
Loading
Loading
Loading
Loading
+292 −0
Original line number Diff line number Diff line
#include "kaguyatc2isis.h"

#include <cstdio>
#include <string>

#include "FileName.h"
#include "ProcessImportPds.h"
#include "UserInterface.h"
#include "Pvl.h"

using namespace std;

namespace Isis {
  void kaguyatc2isis(UserInterface &ui, Pvl *log) {
    ProcessImportPds importPds;
    FileName inFile = ui.GetFileName("FROM");
    QString labelFile = inFile.expanded();
    Pvl label(labelFile);

    QString dataFile = "";
    if ( inFile.extension().toLower() == "lbl" ) {
      dataFile = inFile.path() + "/" + (QString) label.findKeyword("FILE_NAME");
    }
    else {
      dataFile = labelFile;
    }

    QString id = "";
    try {
      id = (QString) label.findKeyword("DATA_SET_ID");
    }
    catch(IException &e) {
      QString msg = "Unable to read [DATA_SET_ID] from label file ["
                    + labelFile + "]";
      throw IException(e, IException::Unknown, msg, _FILEINFO_);
    }

    id = id.simplified().trimmed();
    if (id != "TC_MAP"
        && id != "TCO_MAP"
        && id != "TC1_Level2B"
        && id != "TC2_Level2B"
        && id != "SLN-L-TC-3-S-LEVEL2B0-V1.0"
        && id != "SLN-L-TC-5-MORNING-MAP-V4.0") {
      QString msg = "Input file [" + labelFile + "] does not appear to be " +
                    "a supported Kaguya Terrain Camera format. " +
                    "DATA_SET_ID is [" + id + "]" +
                    "Valid formats include [TC_MAP, TCO_MAP, TC1_Level2B, " +
                    "SLN-L-TC-3-S-LEVEL2B0-V1.0, SLN-L-TC-5-MORNING-MAP-V4.0]";
      throw IException(IException::Unknown, msg, _FILEINFO_);
    }

    if (!label.hasKeyword("TARGET_NAME")) {
      label.addKeyword(PvlKeyword("TARGET_NAME", "MOON"), Pvl::Replace);
    }

    importPds.SetPdsFile(label, dataFile);

    CubeAttributeOutput &att = ui.GetOutputAttribute("TO");
    Cube *outcube = importPds.SetOutputCube(ui.GetFileName("TO"), att);

    // Get user entered special pixel ranges
    if (ui.GetBoolean("SETNULLRANGE")) {
      importPds.SetNull(ui.GetDouble("NULLMIN"), ui.GetDouble("NULLMAX"));
    }
    if (ui.GetBoolean("SETHRSRANGE")) {
      importPds.SetHRS(ui.GetDouble("HRSMIN"), ui.GetDouble("HRSMAX"));
    }
    if (ui.GetBoolean("SETHISRANGE")) {
      importPds.SetHIS(ui.GetDouble("HISMIN"), ui.GetDouble("HISMAX"));
    }
    if (ui.GetBoolean("SETLRSRANGE")) {
      importPds.SetLRS(ui.GetDouble("LRSMIN"), ui.GetDouble("LRSMAX"));
    }
    if (ui.GetBoolean("SETLISRANGE")) {
      importPds.SetLIS(ui.GetDouble("LISMIN"), ui.GetDouble("LISMAX"));
    }

    importPds.SetOrganization(Isis::ProcessImport::BSQ);

    importPds.StartProcess();

    // Get the mapping labels
    Pvl otherLabels;
    importPds.TranslatePdsProjection(otherLabels);

    // Translate the remaining MI MAP labels
    PvlGroup dataDir(Preference::Preferences().findGroup("DataDirectory"));
    QString transDir = (QString) dataDir["Kaguya"] + "/translations/";

    FileName transFile(transDir + "kaguyaTcBandBin.trn");
    PvlToPvlTranslationManager bandBinXlater(label, transFile.expanded());
    bandBinXlater.Auto(otherLabels);

    transFile = transDir + "kaguyaTcInstrument.trn";
    PvlToPvlTranslationManager instXlater(label, transFile.expanded());
    instXlater.Auto(otherLabels);

    transFile = transDir + "kaguyaTcArchive.trn";
    PvlToPvlTranslationManager archiveXlater(label, transFile.expanded());
    archiveXlater.Auto(otherLabels);

    transFile = transDir + "kaguyaTcKernels.trn";
    PvlToPvlTranslationManager kernelsXlater(label, transFile.expanded());
    kernelsXlater.Auto(otherLabels);

    if ( otherLabels.hasGroup("Mapping")
         && otherLabels.findGroup("Mapping").keywords() > 0 ) {
      outcube->putGroup(otherLabels.findGroup("Mapping"));
    }
    if ( otherLabels.hasGroup("Instrument")
         && otherLabels.findGroup("Instrument").keywords() > 0 ) {
      PvlGroup &inst = otherLabels.findGroup("Instrument", Pvl::Traverse);
      if (inst.hasKeyword("StartTime")) {
        // Remove trailing "Z" from keyword
        PvlKeyword &startTime = inst["StartTime"];
        QString startTimeString = startTime[0];
        if (QString::compare(startTimeString.at(startTimeString.size() - 1), "Z", Qt::CaseInsensitive) == 0){
          startTimeString = startTimeString.left(startTimeString.length() - 1);
          startTime.setValue(startTimeString);
        }
      }
      if (inst.hasKeyword("StopTime")) {
        // Remove trailing "Z" from keyword
        PvlKeyword &stopTime = inst["StopTime"];
        QString stopTimeString = stopTime[0];
        if (QString::compare(stopTimeString.at(stopTimeString.size() - 1), "Z", Qt::CaseInsensitive) == 0){
          stopTimeString = stopTimeString.left(stopTimeString.length() - 1);
          stopTime.setValue(stopTimeString);
        }
      }
      outcube->putGroup(otherLabels.findGroup("Instrument"));
  /*
      // This code is not needed now, but is included here commented-out in case it becomes necessary
      // to support the swath modes by setting their NaifFrameCodes in the future. The swath mode
      // setting is currently handled entirely via the camera model.

      // add kernels group
      QString instId = inst["InstrumentId"];
      QString encoding = inst["EncodingType"];
      QString swath = inst["SwathModeId"];
      PvlGroup kern("Kernels");
      if (instId == "TC1") {
        if (swath == "Full") {
          if (productSetId == "TC_w_Level2B0") {
            if (encoding == "DCT") {
              kern += PvlKeyword("NaifFrameCode", toString(-131352));
            }
            else { // encoding == "N/A" so no compression
              kern += PvlKeyword("NaifFrameCode", toString(-131353));
            }
          }
          else if (productSetId == "TC_s_Level2B0") {
            if (encoding == "DCT") {
              kern += PvlKeyword("NaifFrameCode", toString(-131354));
            }
            else { // encoding == "N/A" so no compression
              kern += PvlKeyword("NaifFrameCode", toString(-131355));
            }
          }
        }
        else if (swath == "Nominal") {
          if (productSetId == "TC_w_Level2B0") {
            if (encoding == "DCT") {
              kern += PvlKeyword("NaifFrameCode", toString(-131356));
            }
            else { // encoding == "N/A" so no compression
              kern += PvlKeyword("NaifFrameCode", toString(-131357));
            }
          }
          else if (productSetId == "TC_s_Level2B0") {
            if (encoding == "DCT") {
              kern += PvlKeyword("NaifFrameCode", toString(-131358));
            }
            else { // encoding == "N/A" so no compression
              kern += PvlKeyword("NaifFrameCode", toString(-131359));
            }
          }
        }
        else { // swath == "Half"
          if (productSetId == "TC_w_Level2B0") {
            if (encoding == "DCT") {
              kern += PvlKeyword("NaifFrameCode", toString(-131360));
            }
            else { // encoding == "N/A" so no compression
              kern += PvlKeyword("NaifFrameCode", toString(-131361));
            }
          }
          else if (productSetId == "TC_s_Level2B0") {
            if (encoding == "DCT") {
              kern += PvlKeyword("NaifFrameCode", toString(-131362));
            }
            else { // encoding == "N/A" so no compression
              kern += PvlKeyword("NaifFrameCode", toString(-131363));
            }
          }
        }
      }
      if (instId == "TC2") {
        if (swath == "Full") {
          if (productSetId == "TC_w_Level2B0") {
            if (encoding == "DCT") {
              kern += PvlKeyword("NaifFrameCode", toString(-131372));
            }
            else { // encoding == "N/A" so no compression
              kern += PvlKeyword("NaifFrameCode", toString(-131373));
            }
          }
          else if (productSetId == "TC_s_Level2B0") {
            if (encoding == "DCT") {
              kern += PvlKeyword("NaifFrameCode", toString(-131374));
            }
            else { // encoding == "N/A" so no compression
              kern += PvlKeyword("NaifFrameCode", toString(-131375));
            }
          }
        }
        else if (swath == "Nominal") {
          if (productSetId == "TC_w_Level2B0") {
            if (encoding == "DCT") {
              kern += PvlKeyword("NaifFrameCode", toString(-131376));
            }
            else { // encoding == "N/A" so no compression
              kern += PvlKeyword("NaifFrameCode", toString(-131377));
            }
          }
          else if (productSetId == "TC_s_Level2B0") {
            if (encoding == "DCT") {
              kern += PvlKeyword("NaifFrameCode", toString(-131378));
            }
            else { // encoding == "N/A" so no compression
              kern += PvlKeyword("NaifFrameCode", toString(-131379));
            }
          }
        }
        else { // swath == "Half"
          if (productSetId == "TC_w_Level2B0") {
            if (encoding == "DCT") {
              kern += PvlKeyword("NaifFrameCode", toString(-131380));
            }
            else { // encoding == "N/A" so no compression
              kern += PvlKeyword("NaifFrameCode", toString(-131381));
            }
          }
          else if (productSetId == "TC_s_Level2B0") {
            if (encoding == "DCT") {
              kern += PvlKeyword("NaifFrameCode", toString(-131382));
            }
            else { // encoding == "N/A" so no compression
              kern += PvlKeyword("NaifFrameCode", toString(-131383));
            }
          }
        }
      }
      */
    }
    if ( otherLabels.hasGroup("BandBin")
         && otherLabels.findGroup("BandBin").keywords() > 0 ) {

      PvlGroup &bandBinGroup = otherLabels.findGroup("BandBin");
      if (!bandBinGroup.hasKeyword("FilterName")) {
        bandBinGroup += PvlKeyword("FilterName", "BroadBand");
      }
      if (!bandBinGroup.hasKeyword("Center")) {
        bandBinGroup += PvlKeyword("Center", "640", "nanometers");
      }
      if (!bandBinGroup.hasKeyword("Width")) {
        bandBinGroup += PvlKeyword("Width", "420", "nanometers");
      }
      outcube->putGroup(bandBinGroup);
    }
    else {
      // Add the BandBin group
      PvlGroup bandBinGroup("BandBin");
      bandBinGroup += PvlKeyword("FilterName", "BroadBand");
      bandBinGroup += PvlKeyword("Center", "640nm");
      bandBinGroup += PvlKeyword("Width", "420nm");
      outcube->putGroup(bandBinGroup);
    }

    if ( otherLabels.hasGroup("Archive")
         && otherLabels.findGroup("Archive").keywords() > 0 ) {
      outcube->putGroup(otherLabels.findGroup("Archive"));
    }
    if ( otherLabels.hasGroup("Kernels")
         && otherLabels.findGroup("Kernels").keywords() > 0 ) {
      outcube->putGroup(otherLabels.findGroup("Kernels", Pvl::Traverse));
    }

    importPds.EndProcess();
  }
}
+11 −0
Original line number Diff line number Diff line
#ifndef kaguyatc2isis_h
#define kaguyatc2isis_h

#include "UserInterface.h"
#include "Pvl.h"

namespace Isis {
  extern void kaguyatc2isis(UserInterface &ui, Pvl *log);
}

#endif
+11 −272
Original line number Diff line number Diff line
#include "Isis.h"

#include <cstdio>
#include <string>
#include "kaguyatc2isis.h"

#include "FileName.h"
#include "ProcessImportPds.h"
#include "UserInterface.h"
#include "Application.h"
#include "Pvl.h"

using namespace std;
using namespace Isis;

void IsisMain() {
  ProcessImportPds importPds;
  UserInterface &ui = Application::GetUserInterface();

  FileName inFile = ui.GetFileName("FROM");
  QString labelFile = inFile.expanded();
  Pvl label(labelFile);

  QString dataFile = "";
  if ( inFile.extension().toLower() == "lbl" ) {
    dataFile = inFile.path() + (QString) label.findKeyword("FILE_NAME");
  }
  else {
    dataFile = labelFile;
  }

  QString id = "";
  Pvl appLog;
  try {
    id = (QString) label.findKeyword("DATA_SET_ID");
  }
  catch(IException &e) {
    QString msg = "Unable to read [DATA_SET_ID] from label file [" 
                  + labelFile + "]";
    throw IException(e, IException::Unknown, msg, _FILEINFO_);
  }

  id = id.simplified().trimmed();
  if (id != "TC_MAP" 
      && id != "TCO_MAP"
      && id != "TC1_Level2B"
      && id != "TC2_Level2B") {
    QString msg = "Input file [" + labelFile + "] does not appear to be " +
                  "a supported Kaguya Terrain Camera format. " +
                  "DATA_SET_ID is [" + id + "]" +
                  "Valid formats include [TC_MAP, TCO_MAP, TC1_Level2B]";
    throw IException(IException::Unknown, msg, _FILEINFO_);
  }

  if (!label.hasKeyword("TARGET_NAME")) {
    label.addKeyword(PvlKeyword("TARGET_NAME", "MOON"), Pvl::Replace);
  }

  importPds.SetPdsFile(label, dataFile);

  Cube *outcube = importPds.SetOutputCube("TO");

  // Get user entered special pixel ranges
  if (ui.GetBoolean("SETNULLRANGE")) {
    importPds.SetNull(ui.GetDouble("NULLMIN"), ui.GetDouble("NULLMAX"));
  }
  if (ui.GetBoolean("SETHRSRANGE")) {
    importPds.SetHRS(ui.GetDouble("HRSMIN"), ui.GetDouble("HRSMAX"));
  }
  if (ui.GetBoolean("SETHISRANGE")) {
    importPds.SetHIS(ui.GetDouble("HISMIN"), ui.GetDouble("HISMAX"));
  }
  if (ui.GetBoolean("SETLRSRANGE")) {
    importPds.SetLRS(ui.GetDouble("LRSMIN"), ui.GetDouble("LRSMAX"));
  }
  if (ui.GetBoolean("SETLISRANGE")) {
    importPds.SetLIS(ui.GetDouble("LISMIN"), ui.GetDouble("LISMAX"));
  }

  importPds.SetOrganization(Isis::ProcessImport::BSQ);

  importPds.StartProcess();

  // Get the mapping labels
  Pvl otherLabels;
  importPds.TranslatePdsProjection(otherLabels);

  // Translate the remaining MI MAP labels
  PvlGroup dataDir(Preference::Preferences().findGroup("DataDirectory"));
  QString transDir = (QString) dataDir["Kaguya"] + "/translations/";
  
  FileName transFile(transDir + "kaguyaTcBandBin.trn");
  PvlToPvlTranslationManager bandBinXlater(label, transFile.expanded());
  bandBinXlater.Auto(otherLabels);
  
  transFile = transDir + "kaguyaTcInstrument.trn";
  PvlToPvlTranslationManager instXlater(label, transFile.expanded());
  instXlater.Auto(otherLabels);
  
  transFile = transDir + "kaguyaTcArchive.trn";
  PvlToPvlTranslationManager archiveXlater(label, transFile.expanded());
  archiveXlater.Auto(otherLabels);
  
  transFile = transDir + "kaguyaTcKernels.trn";
  PvlToPvlTranslationManager kernelsXlater(label, transFile.expanded());
  kernelsXlater.Auto(otherLabels);

  if ( otherLabels.hasGroup("Mapping") 
       && otherLabels.findGroup("Mapping").keywords() > 0 ) {
    outcube->putGroup(otherLabels.findGroup("Mapping"));
  }
  if ( otherLabels.hasGroup("Instrument") 
       && otherLabels.findGroup("Instrument").keywords() > 0 ) {
    PvlGroup &inst = otherLabels.findGroup("Instrument", Pvl::Traverse);
    if (inst.hasKeyword("StartTime")) {
      // Remove trailing "Z" from keyword
      PvlKeyword &startTime = inst["StartTime"];
      QString startTimeString = startTime[0];
      if (QString::compare(startTimeString.at(startTimeString.size() - 1), "Z", Qt::CaseInsensitive) == 0){
        startTimeString = startTimeString.left(startTimeString.length() - 1);
        startTime.setValue(startTimeString);
      }
    }
    if (inst.hasKeyword("StopTime")) {
      // Remove trailing "Z" from keyword
      PvlKeyword &stopTime = inst["StopTime"];
      QString stopTimeString = stopTime[0];
      if (QString::compare(stopTimeString.at(stopTimeString.size() - 1), "Z", Qt::CaseInsensitive) == 0){
        stopTimeString = stopTimeString.left(stopTimeString.length() - 1);
        stopTime.setValue(stopTimeString);
      }
    }
    outcube->putGroup(otherLabels.findGroup("Instrument"));
/* 
    // This code is not needed now, but is included here commented-out in case it becomes necessary
    // to support the swath modes by setting their NaifFrameCodes in the future. The swath mode
    // setting is currently handled entirely via the camera model. 
 
    // add kernels group
    QString instId = inst["InstrumentId"];
    QString encoding = inst["EncodingType"];
    QString swath = inst["SwathModeId"];
    PvlGroup kern("Kernels");
    if (instId == "TC1") {
      if (swath == "Full") {
        if (productSetId == "TC_w_Level2B0") {
          if (encoding == "DCT") {
            kern += PvlKeyword("NaifFrameCode", toString(-131352));
          }
          else { // encoding == "N/A" so no compression
            kern += PvlKeyword("NaifFrameCode", toString(-131353));
          }
        }
        else if (productSetId == "TC_s_Level2B0") { 
          if (encoding == "DCT") {
            kern += PvlKeyword("NaifFrameCode", toString(-131354));
          }
          else { // encoding == "N/A" so no compression
            kern += PvlKeyword("NaifFrameCode", toString(-131355));
          }
        }
      }
      else if (swath == "Nominal") {
        if (productSetId == "TC_w_Level2B0") {
          if (encoding == "DCT") {
            kern += PvlKeyword("NaifFrameCode", toString(-131356));
          }
          else { // encoding == "N/A" so no compression
            kern += PvlKeyword("NaifFrameCode", toString(-131357));
    kaguyatc2isis(ui, &appLog);
  }
  catch (...) {
    for (auto grpIt = appLog.beginGroup(); grpIt != appLog.endGroup(); grpIt++) {
      Application::Log(*grpIt);
    }
        else if (productSetId == "TC_s_Level2B0") { 
          if (encoding == "DCT") {
            kern += PvlKeyword("NaifFrameCode", toString(-131358));
          }
          else { // encoding == "N/A" so no compression
            kern += PvlKeyword("NaifFrameCode", toString(-131359));
          }
        }
      }
      else { // swath == "Half"
        if (productSetId == "TC_w_Level2B0") {
          if (encoding == "DCT") {
            kern += PvlKeyword("NaifFrameCode", toString(-131360));
          }
          else { // encoding == "N/A" so no compression
            kern += PvlKeyword("NaifFrameCode", toString(-131361));
          }
        }
        else if (productSetId == "TC_s_Level2B0") { 
          if (encoding == "DCT") {
            kern += PvlKeyword("NaifFrameCode", toString(-131362));
          }
          else { // encoding == "N/A" so no compression
            kern += PvlKeyword("NaifFrameCode", toString(-131363));
          }
        }
      }
    }
    if (instId == "TC2") {
      if (swath == "Full") {
        if (productSetId == "TC_w_Level2B0") {
          if (encoding == "DCT") {
            kern += PvlKeyword("NaifFrameCode", toString(-131372));
          }
          else { // encoding == "N/A" so no compression
            kern += PvlKeyword("NaifFrameCode", toString(-131373));
          }
        }
        else if (productSetId == "TC_s_Level2B0") { 
          if (encoding == "DCT") {
            kern += PvlKeyword("NaifFrameCode", toString(-131374));
          }
          else { // encoding == "N/A" so no compression
            kern += PvlKeyword("NaifFrameCode", toString(-131375));
          }
        }
      }
      else if (swath == "Nominal") {
        if (productSetId == "TC_w_Level2B0") {
          if (encoding == "DCT") {
            kern += PvlKeyword("NaifFrameCode", toString(-131376));
          }
          else { // encoding == "N/A" so no compression
            kern += PvlKeyword("NaifFrameCode", toString(-131377));
          }
        }
        else if (productSetId == "TC_s_Level2B0") { 
          if (encoding == "DCT") {
            kern += PvlKeyword("NaifFrameCode", toString(-131378));
          }
          else { // encoding == "N/A" so no compression
            kern += PvlKeyword("NaifFrameCode", toString(-131379));
          }
        }
      }
      else { // swath == "Half"
        if (productSetId == "TC_w_Level2B0") {
          if (encoding == "DCT") {
            kern += PvlKeyword("NaifFrameCode", toString(-131380));
          }
          else { // encoding == "N/A" so no compression
            kern += PvlKeyword("NaifFrameCode", toString(-131381));
          }
        }
        else if (productSetId == "TC_s_Level2B0") { 
          if (encoding == "DCT") {
            kern += PvlKeyword("NaifFrameCode", toString(-131382));
          }
          else { // encoding == "N/A" so no compression
            kern += PvlKeyword("NaifFrameCode", toString(-131383));
          }
        }
      }
    }
    */
  }
  if ( otherLabels.hasGroup("BandBin") 
       && otherLabels.findGroup("BandBin").keywords() > 0 ) {

    PvlGroup &bandBinGroup = otherLabels.findGroup("BandBin");
    if (!bandBinGroup.hasKeyword("FilterName")) {
      bandBinGroup += PvlKeyword("FilterName", "BroadBand");
    }
    if (!bandBinGroup.hasKeyword("Center")) {
      bandBinGroup += PvlKeyword("Center", "640", "nanometers");
    }
    if (!bandBinGroup.hasKeyword("Width")) {
      bandBinGroup += PvlKeyword("Width", "420", "nanometers");
    }
    outcube->putGroup(bandBinGroup);
  }
  else {
    // Add the BandBin group
    PvlGroup bandBinGroup("BandBin");
    bandBinGroup += PvlKeyword("FilterName", "BroadBand");
    bandBinGroup += PvlKeyword("Center", "640nm");
    bandBinGroup += PvlKeyword("Width", "420nm");
    outcube->putGroup(bandBinGroup);
    throw;
  }

  if ( otherLabels.hasGroup("Archive") 
       && otherLabels.findGroup("Archive").keywords() > 0 ) {
    outcube->putGroup(otherLabels.findGroup("Archive"));
  for (auto grpIt = appLog.beginGroup(); grpIt != appLog.endGroup(); grpIt++){
    Application::Log(*grpIt);
  }
  if ( otherLabels.hasGroup("Kernels") 
       && otherLabels.findGroup("Kernels").keywords() > 0 ) {
    outcube->putGroup(otherLabels.findGroup("Kernels", Pvl::Traverse));
  }

  importPds.EndProcess();
}
+88 −0

File added.

Preview size limit exceeded, changes collapsed.

+18.8 KiB

File added.

No diff preview for this file type.

Loading