USGS

Isis 3.0 Application Source Code Reference

Home

mdisedrinfo.cpp

Go to the documentation of this file.
00001 //  $Id: mdisedrinfo.cpp,v 1.8 2008/07/11 22:38:41 nhilt Exp $
00002 #include "Isis.h"
00003 
00004 #include <cstdio>
00005 #include <string>
00006 #include <vector>
00007 #include <algorithm>
00008 #include <sstream>
00009 #include <iostream>
00010 
00011 #include "FileName.h"
00012 #include "UserInterface.h"
00013 #include "Cube.h"
00014 #include "OriginalLabel.h"
00015 #include "ProgramLauncher.h"
00016 #include "Pvl.h"
00017 #include "PvlGroup.h"
00018 #include "IString.h"
00019 
00020 #include "MdisGeometry.h"
00021 #include "MdisEdrKeys.h"
00022 
00023 using namespace Isis;
00024 using namespace std;
00025 
00026 
00027 void IsisMain() {
00028 
00029 //  Input parameters
00030   UserInterface &ui = Application::GetUserInterface();
00031   QString sourceFile = ui.GetAsString("FROM");    //  Save off source filename
00032   FileName from(sourceFile);
00033 
00034   QString to;
00035   if(ui.WasEntered("TO")) to = ui.GetAsString("TO");
00036   bool delete_from(false);
00037 
00038 //  Is there a separate file containing keywords to report?
00039   QString keylist;
00040   if(ui.WasEntered("KEYLIST")) keylist = ui.GetAsString("KEYLIST");
00041   else keylist = to;
00042 
00043 //  Get PVL parameter
00044   QString pvl;
00045   if(ui.WasEntered("PVL")) pvl = ui.GetAsString("PVL");
00046 
00047 // Run mdis2isis if necessary.  If this step must be done, we must also
00048 //  run spiceinit to initialize it with spice.
00049   if(from.extension().toUpper() != "CUB") {
00050     FileName temp = FileName::createTempFile("$TEMPORARY/" + from.baseName() + ".cub");
00051     QString params = "from=" + from.expanded()  + " to=" + temp.expanded();
00052 
00053     try {
00054       ProgramLauncher::RunIsisProgram("mdis2isis", params);
00055 
00056 // Ensure a proper target before initialization
00057       Cube cube;
00058       cube.open(temp.expanded(), "rw");
00059       Pvl *label = cube.label();
00060       MdisGeometry::validateTarget(*label, true);
00061       cube.close();
00062 
00063       //  Run spiceinit on it
00064       ProgramLauncher::RunIsisProgram("spiceinit", "from=" + temp.expanded());
00065     }
00066     catch(IException &ie) {
00067       QString tempName(temp.expanded());
00068       remove(tempName.toAscii().data());
00069       throw IException(ie, IException::User,
00070                        "Failed to execute mdis2isis/spiceinit", _FILEINFO_);
00071     }
00072 
00073     //  FROM file is now the ISIS cube.  Stage cube file for deletion as well.
00074     from = temp;
00075     delete_from = true;
00076   }
00077 
00078 //  Now process the label and instantiate the camera model(s)
00079   try {
00080 
00081 //  Get the orginal PDS EDR labels and initialize the keyword map
00082     Pvl edrlab = OriginalLabel(from.expanded()).ReturnLabels();
00083     MdisEdrKeys edrkeys(edrlab);
00084 
00085 //  Compute the Geometry
00086     MdisGeometry geom(from.expanded());
00087     Pvl geomkeys = geom.getGeometry(sourceFile);
00088     edrkeys.updateKeys(geomkeys);
00089 
00090     PvlGroup mdiskeys("MdisPdsKeys");
00091 
00092 //  Only process the PDS EDR keyword mapping if KEYMAP (or TO) is entered.
00093     if(!keylist.isEmpty()) {
00094       FileName kmap(keylist);
00095       if(!kmap.fileExists()) {
00096         QString mess = "EDR keyword map source file, " + kmap.expanded()
00097                        + ", does not exist!";
00098         throw IException(IException::User, mess, _FILEINFO_);
00099       }
00100 
00101       // Get the keylist source line
00102       QString kmapName(kmap.expanded());
00103       ifstream ifile(kmapName.toAscii().data(), ios::in);
00104       if(!ifile) {
00105         QString mess = "Unable to open key map source file " + kmap.expanded();
00106         throw IException(IException::User, mess, _FILEINFO_);
00107       }
00108 
00109       string keystring;
00110       if(!getline(ifile, keystring)) {
00111         QString mess = "I/O error reading key map line from  " + kmap.expanded();
00112         throw IException(IException::User, mess, _FILEINFO_);
00113       }
00114 
00115       ifile.close();
00116 
00117       //  Split the line using semi-colons as the delimiter
00118       QStringList keys = QString(keystring.c_str()).split(";");
00119       QString keyvalues = edrkeys.extract(keys, geom.getNull(), &mdiskeys);
00120 
00121       if(!to.isEmpty()) {
00122         //  Now open the output file and write the result
00123         FileName tomap(to);
00124         QString tomapName(tomap.expanded());
00125         bool toExists = tomap.fileExists();
00126         ofstream ofile;
00127         if(toExists) {
00128           ofile.open(tomapName.toAscii().data(), std::ios::out | std::ios::app);
00129         }
00130         else {
00131           ofile.open(tomapName.toAscii().data(), std::ios::out);
00132         }
00133 
00134         if(!ofile) {
00135           QString mess = "Could not open or create output TO file " +
00136                          tomapName;
00137           throw IException(IException::User, mess, _FILEINFO_);
00138         }
00139 
00140         //  Write the header if requested by the user
00141         if(!toExists) ofile << keystring << endl;
00142         ofile << keyvalues << endl;
00143         ofile.close();
00144       }
00145     }
00146     else {
00147       PvlContainer::PvlKeywordIterator keyIter = geomkeys.Begin();
00148       for(; keyIter != geomkeys.End() ;  ++keyIter) {
00149         mdiskeys.AddKeyword(*keyIter);
00150       }
00151     }
00152 
00153 //  See if the user wants to write out the PVL keywords
00154     if(!pvl.isEmpty()) {
00155       Pvl pout;
00156       pout.AddGroup(mdiskeys);
00157       pout.Write(pvl);
00158     }
00159 
00160 //  Log the results to the log/terminal/gui
00161     Application::Log(mdiskeys);
00162   }
00163   catch(IException &) {
00164     QString fromName(from.expanded());
00165     if(delete_from) remove(fromName.toAscii().data());
00166     throw;
00167   }
00168 
00169 // Delete the from file if it was temporarily created from an EDR
00170   QString fromName(from.expanded());
00171   if(delete_from) remove(fromName.toAscii().data());
00172 }
00173