Commit e255e9b4 authored by Jesse Mapel's avatar Jesse Mapel
Browse files

Added ProcessExportPds4 class. Fixes #5167.

git-svn-id: http://subversion.wr.usgs.gov/repos/prog/isis3/trunk@8178 41f8697f-d340-4b68-9986-7bafba869bb8
parent b2a0db0d
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
ifeq ($(ISISROOT), $(BLANK))
.SILENT:
error:
	echo "Please set ISISROOT";
else
	include $(ISISROOT)/make/isismake.objs
endif
 No newline at end of file
+217 −0
Original line number Diff line number Diff line
/**
 *   Unless noted otherwise, the portions of Isis written by the
 *   USGS are public domain. See individual third-party library
 *   and package descriptions for intellectual property
 *   information,user agreements, and related information.
 *
 *   Although Isis has been used by the USGS, no warranty, expressed or implied,
 *   is made by the USGS as to the accuracy and functioning of such software
 *   and related material nor shall the fact of distribution constitute any such
 *   warranty, and no responsibility is assumed by the USGS in connection
 *   therewith.
 *
 *   For additional information, launch
 *   $ISISROOT/doc//documents/Disclaimers/Disclaimers.html in a browser or see
 *   the Privacy & Disclaimers page on the Isis website,
 *   http://isis.astrogeology.usgs.gov, and the USGS privacy and disclaimers on
 *   http://www.usgs.gov/privacy.html.
 */
#include "ProcessExportPds4.h"

#include <cmath>
#include <iostream>
#include <sstream>

#include <QDomDocument>
#include <QString>

#include "FileName.h"
#include "Pvl.h"
#include "PvlToXmlTranslationManager.h"

using namespace std;

namespace Isis {


  /**
   * Default Constructor - Set to default the data members
   *
   */
  ProcessExportPds4::ProcessExportPds4() {
    qSetGlobalQHashSeed(1031); // hash seed to force consistent output

    m_domDoc = new QDomDocument("");

    QString xmlModel;
    xmlModel += "href=\"http://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1800.sch\" ";
    xmlModel += "schemetypens=\"http://purl.oclc.org/dsdl/schematron\"";
    QDomProcessingInstruction header =
        m_domDoc->createProcessingInstruction("xml-model", xmlModel);
    m_domDoc->appendChild(header);
  }


  /**
   * Destructor
   *
   */
  ProcessExportPds4::~ProcessExportPds4() {
    delete m_domDoc;
    m_domDoc = NULL;
  }


  QDomDocument &ProcessExportPds4::StandardPds4Label() {
    if (InputCubes.size() == 0) {
      QString msg("Must set an input cube before creating a PDS4 label.");
      throw IException(IException::Programmer, msg, _FILEINFO_);
    }
    else {
      if (m_domDoc->documentElement().isNull()) {
        QDomElement root = m_domDoc->createElement("Product_Observational");
        root.setAttribute("xmlns", "http://pds.nasa.gov/pds4/pds/v1");
        root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        root.setAttribute("xsi:schemaLocation",
                          "http://pds.nasa.gov/pds4/pds/v1 http://pds.nasa.gov/pds4/pds/v1");
        m_domDoc->appendChild(root);
      }

      CreateImageLabel();
      return *m_domDoc;
    }
  }


  /**
   * Create a standard PDS label for type IMAGE
   */
  void ProcessExportPds4::CreateImageLabel() {

    FixedImageRoot();

    StandardImageImage();

    //StandardAllMapping(mainPvl);
  }


  /**
   *
   */
  void ProcessExportPds4::FixedImageRoot() {
    //Don't know what needs to go in here yet....
    /**
     * ProcessExportPds has Pds version, record type, record bytes, file records, label records,
     * and md5 checksum
     *
     * Where does this information need to go in a Pds4 xml label?
     */
  }


  /**
   *
   */
  void ProcessExportPds4::StandardImageImage() {

    Pvl *inputLabel = InputCubes[0]->label();
    FileName transfile;
    transfile = "$base/translations/pds4Export.trn";
    PvlToXmlTranslationManager xlator(*inputLabel, transfile.expanded());
    xlator.Auto(*m_domDoc);

    QDomElement rootElement = m_domDoc->documentElement();
    QDomElement fileAreaObservationalElement =
                    rootElement.firstChildElement("File_Area_Observational");

    if (!fileAreaObservationalElement.isNull()) {

      QDomElement array2DImageElement =
                      fileAreaObservationalElement.firstChildElement("Array_2D_Image");

      if (!array2DImageElement.isNull()) {
        QDomElement elementArrayElement = m_domDoc->createElement("Element_Array");
        array2DImageElement.appendChild(elementArrayElement);

        //The next three values are assuming that the cube is Real
        QDomElement dataTypeElement = m_domDoc->createElement("data_type");
        xlator.setElementValue(dataTypeElement, "IEEE754LSBSingle");
        elementArrayElement.appendChild(dataTypeElement);

        QDomElement scalingFactorElement = m_domDoc->createElement("scaling_factor");
        xlator.setElementValue(scalingFactorElement, "1.00");
        elementArrayElement.appendChild(scalingFactorElement);

        QDomElement offsetElement = m_domDoc->createElement("offset");

        xlator.setElementValue(offsetElement, "0.00");
        elementArrayElement.appendChild(offsetElement);
      }
    }
  }


  /**
   * Write the XML label to the supplied stream.
   *
   * @param Output file stream to which the XML label will be written.
   */
  void ProcessExportPds4::OutputLabel(std::ofstream &os) {
    os << m_domDoc->toString() << endl;
  }


  /**
   * This method fills the image data of the PDS4 file using the parent class
   * ProcessExport::StartProcess.
   *
   * @param fout Output file stream to be filled with the PDS4 data.
   *
   */
  void ProcessExportPds4::StartProcess(std::ofstream &fout) {
    ProcessExport::StartProcess(fout);
  }

  
  QDomDocument &ProcessExportPds4::GetLabel() {
    if (m_domDoc->documentElement().isNull()) {
      QDomElement root = m_domDoc->createElement("Product_Observational");
      root.setAttribute("xmlns", "http://pds.nasa.gov/pds4/pds/v1");
      root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
      root.setAttribute("xsi:schemaLocation",
                        "http://pds.nasa.gov/pds4/pds/v1 http://pds.nasa.gov/pds4/pds/v1");
      m_domDoc->appendChild(root);
    }
    return *m_domDoc;
  }
  
  
  /**
   * This method write out the labels and image data to the specified output file.
   * Creates an IMG and XML file.
   *
   * @param outFile QString of the name of the output file. Will create an XML 
   *        and an IMG file with the output file name.
   *
   */
  void ProcessExportPds4::WritePds4(QString outFile) {
    
    FileName outputFile(outFile);

    QString path(FileName(outFile).originalPath());
    QString name(FileName(outFile).baseName());
    QString labelName = path + "/" + name + ".xml";
    ofstream oLabel(labelName.toLatin1().data());
    
    OutputLabel(oLabel);
    oLabel.close();
    
    ofstream oCube(outputFile.expanded().toLatin1().data());

    StartProcess(oCube);
    oCube.close();
    EndProcess();
  }

} // End of Isis namespace
+81 −0
Original line number Diff line number Diff line
#ifndef ProcessExportPds4_h
#define ProcessExportPds4_h
/*
 *   Unless noted otherwise, the portions of Isis written by the
 *   USGS are public domain. See individual third-party library
 *   and package descriptions for intellectual property
 *   information,user agreements, and related information.
 *
 *   Although Isis has been used by the USGS, no warranty, expressed or implied,
 *   is made by the USGS as to the accuracy and functioning of such software
 *   and related material nor shall the fact of distribution constitute any such
 *   warranty, and no responsibility is assumed by the USGS in connection
 *   therewith.
 *
 *   For additional information, launch
 *   $ISISROOT/doc//documents/Disclaimers/Disclaimers.html in a browser or see
 *   the Privacy &amp; Disclaimers page on the Isis website,
 *   http://isis.astrogeology.usgs.gov, and the USGS privacy and disclaimers on
 *   http://www.usgs.gov/privacy.html.
 */

#include "ProcessExport.h"
#include <vector>

#include <QDomDocument>

namespace Isis {

  /**
   * @brief Process class for exporting cubes to PDS4 standards
   *
   * This class extends the ProcessExport class to allow the user
   * to export cubes to PDS4 format.
   *
   * @author 2017-05-25 Marjorie Hahn and Makayla Shepherd
   * @internal
   *   @history 2017-05-31 Ian Humphrey - Added check in StandardPds4Label to thrown an
   *                           exception if there is no input cube set.
   *   @history 2017-06-01 Ian Humphrey - Added XML declaration and updated attributes for
   *                           Product_Observational tag.
   *   @history 2017-06-02 Marjorie Hahn - Added global hash seed to force a consistent
   *                           output (XML attribute order).
   *   @history 2017-06-02 Adam Paquette - Updated call to translation file to use a translation file
   *                           in the isis data area.
   *   @history 2017-06-02 Makayla Shepherd - Added CreateImageLabel, FixedImageRoot, and
   *                           StandardImageImage to add hardcoded values to the xml label.
   *   @history 2017-06-04 Adam Paquette - Added GetLabel function and updated StandardPds4Label.
   *   @history 2017-06-08 Marjorie Hahn - Added WritePds4 method to write out the 
   *                           .img and .xml Pds4 data.
   */

  class ProcessExportPds4: public Isis::ProcessExport {
    public:

      ProcessExportPds4();
      ~ProcessExportPds4();

      QDomDocument &StandardPds4Label();

      void CreateImageLabel();
      void FixedImageRoot();
      void StandardImageImage();

      void OutputLabel(std::ofstream &os);

      // include this using declaration to indicate that ProcessExportPds4
      // objects that call a StartProcess() method that has not been overridden
      // here should use the corresponding base class definitions
      using ProcessExport::StartProcess;
      void StartProcess(std::ofstream &fout);
      QDomDocument &GetLabel();
      void WritePds4(QString outFile);

    protected:

      QDomDocument *m_domDoc;               //!< XML label

  };
}

#endif
+50 −0
Original line number Diff line number Diff line
<?xml-model href="http://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1800.sch" schemetypens="http://purl.oclc.org/dsdl/schematron"?>
<Product_Observational xmlns="http://pds.nasa.gov/pds4/pds/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pds.nasa.gov/pds4/pds/v1 http://pds.nasa.gov/pds4/pds/v1">
 <File_Area_Observational>
  <Array_2D_Image>
   <Axis_Array>
    <axis_name>Sample</axis_name>
    <sequence_number>1</sequence_number>
    <elements>2048</elements>
   </Axis_Array>
   <Axis_Array>
    <axis_name>Lines</axis_name>
    <sequence_number>2</sequence_number>
    <elements>254</elements>
   </Axis_Array>
   <Element_Array>
    <data_type>IEEE754LSBSingle</data_type>
    <scaling_factor>1.00</scaling_factor>
    <offset>0.00</offset>
   </Element_Array>
  </Array_2D_Image>
 </File_Area_Observational>
</Product_Observational>
unittest: Exporting
0% Processed
10% Processed
20% Processed
30% Processed
40% Processed
50% Processed
60% Processed
70% Processed
80% Processed
90% Processed
100% Processed
<?xml-model href="http://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1800.sch" schemetypens="http://purl.oclc.org/dsdl/schematron"?>
<Product_Observational xmlns="http://pds.nasa.gov/pds4/pds/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pds.nasa.gov/pds4/pds/v1 http://pds.nasa.gov/pds4/pds/v1">
 <File_Area_Observational>
  <Array_2D_Image>
   <Axis_Array>
    <axis_name>Sample</axis_name>
    <sequence_number>1</sequence_number>
    <elements>2048</elements>
   </Axis_Array>
   <Axis_Array>
    <axis_name>Lines</axis_name>
    <sequence_number>2</sequence_number>
    <elements>254</elements>
   </Axis_Array>
   <Element_Array>
    <data_type>IEEE754LSBSingle</data_type>
    <scaling_factor>1.00</scaling_factor>
    <offset>0.00</offset>
   </Element_Array>
  </Array_2D_Image>
 </File_Area_Observational>
</Product_Observational>
unittest: Exporting
0% Processed
10% Processed
20% Processed
30% Processed
40% Processed
50% Processed
60% Processed
70% Processed
80% Processed
90% Processed
100% Processed
Test creating a standard Pds4Label with no input
**PROGRAMMER ERROR** Must set an input cube before creating a PDS4 label.
+54 −0
Original line number Diff line number Diff line
#include "Isis.h"

#include <QString>

#include "Cube.h"
#include "Preference.h"

#include "ProcessExportPds4.h"

using namespace std;
using namespace Isis;

/**
 * @author 2017-05-30 Marjorie Hahn
 */
void IsisMain() {
  Preference::Preferences(true);
  
  try {
    Isis::ProcessExportPds4 p;
    
    QString cubeName = "$tgo/testData/CAS-MCO-2016-11-26T22.32.39.582-BLU-03025-00.cub";
    
    Isis::Cube cub; 
    cub.open(cubeName, "r"); 
    
    p.SetInputCube(&cub);
    
    std::cout << p.StandardPds4Label().toString();
             
    std::ofstream ofs;
    p.OutputLabel(ofs);

    p.StartProcess(ofs);

    std::cout << p.GetLabel().toString();

    p.WritePds4("temp.img");
    remove("temp.img");
    remove("temp.xml");

    try {
      std::cout << "Test creating a standard Pds4Label with no input" << std::endl;
      ProcessExportPds4 emptyProcess;
      emptyProcess.StandardPds4Label();
    }
    catch(Isis::IException &e) {
      e.print();
    }
  }
  catch(Isis::IException &e) {
    e.print();
  }
}
Loading