Unverified Commit bda79986 authored by Summer Stapleton's avatar Summer Stapleton Committed by GitHub
Browse files

Merge pull request #478 from SgStapleton/cmakeMerge

Merge dev into cmake
parents c18cf124 49450fdc
Loading
Loading
Loading
Loading
+7 −0
Original line number Original line Diff line number Diff line
ifeq ($(ISISROOT), $(BLANK))
.SILENT:
error:
	echo "Please set ISISROOT";
else
	include $(ISISROOT)/make/isismake.apps
endif
 No newline at end of file
+120 −0
Original line number Original line Diff line number Diff line
/**
 * @file
 * $Revision: 1.8 $
 * $Date: 2010/04/08 15:28:20 $
 *
 *   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 "Isis.h"

#include <QString>

#include "Cube.h"
#include "CSVReader.h"
#include "IException.h"
#include "IString.h"
#include "Pvl.h"
#include "PvlObject.h"
#include "Table.h"
#include "TableField.h"
#include "TableRecord.h"

using namespace Isis;


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

  // Read the CSV file and get the header
  QString csvFileName = ui.GetFileName("csv");
  CSVReader reader;
  try {
    reader = CSVReader(csvFileName, true);
  }
  catch(IException &e) {
    QString msg = "Failed to read CSV file [" + csvFileName + "].";
    throw IException(e, IException::Io, msg, _FILEINFO_);
  }
  int numColumns = reader.columns();
  int numRows = reader.rows();
  if (numColumns < 1 || numRows < 1) {
    QString msg = "CSV file does not have data.\nFile has [" + toString(numRows) +
                  "] rows and [" + toString(numColumns) +"] columns.";
    throw IException(IException::User, msg, _FILEINFO_);
  }
  CSVReader::CSVAxis header = reader.getHeader();

  // Construct an empty table with the CSV header as field names
  TableRecord tableRow;
  for (int columnIndex = 0; columnIndex < numColumns; columnIndex++) {
    TableField columnField(QString(header[columnIndex]), TableField::Double);
    tableRow += columnField;
  }
  QString tableName = ui.GetString("tablename");
  Table table(tableName, tableRow);

  // Fill the table
  for (int rowIndex = 0; rowIndex < numRows; rowIndex++) {
    CSVReader::CSVAxis csvRow = reader.getRow(rowIndex);
    for (int columnIndex = 0; columnIndex < numColumns; columnIndex++) {
      tableRow[columnIndex] = toDouble(csvRow[columnIndex]);
    }
    table += tableRow;
  }

  // If a set of label keywords was passed add them to the table
  if (ui.WasEntered("label")) {
    QString labelPvlFilename = ui.GetFileName("label");
    Pvl labelPvl;
    try {
      labelPvl.read(labelPvlFilename);
    }
    catch(IException &e) {
      QString msg = "Failed to read PVL label file [" + labelPvlFilename + "].";
      throw IException(e, IException::Io, msg, _FILEINFO_);
    }

    PvlObject &tableLabel = table.Label();
    for (int keyIndex = 0; keyIndex < labelPvl.keywords(); keyIndex++) {
      tableLabel.addKeyword(labelPvl[keyIndex]);
    }
  }

  // Write the table to the cube
  QString outCubeFileName(ui.GetFileName("to"));
  Cube outCube;
  try {
    outCube.open(outCubeFileName, "rw");
  }
  catch(IException &e) {
    QString msg = "Could not open output cube [" + outCubeFileName + "].";
    throw IException(e, IException::Io, msg, _FILEINFO_);
  }

  try {
    outCube.write(table);
  }
  catch(IException &e) {
    QString msg = "Could not write output table [" + tableName +
                  "] to output cube [" + outCubeFileName + "].";
    throw IException(e, IException::Io, msg, _FILEINFO_);
  }

  outCube.close();

}
+103 −0
Original line number Original line Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>

<application name="csv2table" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://isis.astrogeology.usgs.gov/Schemas/Application/application.xsd">
  <brief>
    Convert a CSV file to a table and attach it to a cube
  </brief>

  <description>
    This application converts a CSV file to a table and attaches it to a cube
    The first row of the CSV will be used as the fieldnames for the table.
    The contents of the CSV file will be converted to floating point numbers
    before they are inserted into the table.
  </description>

  <category>
    <categoryItem>Scripting</categoryItem>
  </category>

  <seeAlso>
    <applications>
      <item>tabledump</item>
    </applications>
  </seeAlso>

  <history>
    <change name="Jesse Mapel" date="2018-09-04">
      Original version
    </change>
  </history>

  <groups>
    <group name = "Files">
      <parameter name = "csv">
        <type>filename</type>
        <fileMode>input</fileMode>
        <brief>Input CSV filename</brief>
        <description>
          Input CSV filename. The first row of this file will be used as the
          table field names.
        </description>
        <filter>
          *.csv
        </filter>
      </parameter>

        <parameter name = "label">
          <type>filename</type>
          <fileMode>input</fileMode>
          <brief>Input table label PVL filename</brief>
          <description>
            Input table label PVL filename. This is expected to be a flat PVL
            file where all of the keywords and their values will be added to
            the tabel label.
          </description>
          <internalDefault>None</internalDefault>
          <filter>
            *.pvl
          </filter>
        </parameter>

      <parameter name = "to">
        <type>cube</type>
        <fileMode>output</fileMode>
        <brief>Output cube filename</brief>
        <description>
          Output cube filename that the table will be attached to
        </description>
        <filter>
          *.cub
        </filter>
      </parameter>
    </group>

    <group name = "Table">
      <parameter name = "tablename">
        <type>string</type>
        <brief>The name of the table</brief>
        <description>
          A table will be created with this name on the output cube using the
          data from the CSV file. If a table with this name already exists on
          the cube it will be overwritten.
        </description>
      </parameter>
    </group>

  </groups>

  <examples>
    <example>
      <brief>Write a simple table</brief>
      <description>Write a simple csv file to a cube.</description>
      <terminalInterface>
        <commandLine> csv=test.csv tablename=TestTable to=isisTruth.cub
        </commandLine>
        <description>
          In this example, csv2table will write a table called TestTable on
          isisTruth.cub with the contents of test.csv.
        </description>
      </terminalInterface>
    </example>
  </examples>

</application>
+14 −0
Original line number Original line Diff line number Diff line
APPNAME = csv2table

include $(ISISROOT)/make/isismake.tsts

commands:
	cp $(INPUT)/isisTruth.cub $(OUTPUT)/isisTruth.cub;
	$(APPNAME) csv=$(INPUT)/test.csv \
	label=$(INPUT)/label.pvl \
	tablename="TestTable" \
	to=$(OUTPUT)/isisTruth.cub > /dev/null;
	catlab from=$(OUTPUT)/isisTruth.cub | \
	sed -n '/Object = Table/,/End_Object/p' > \
	$(OUTPUT)/table.pvl;
	rm $(OUTPUT)/isisTruth.cub;
+4 −0
Original line number Original line Diff line number Diff line
BLANKS = "%-6s"    
LENGTH = "%-40s"

include $(ISISROOT)/make/isismake.tststree
Loading