USGS

Isis 3.0 Application Source Code Reference

Home

reduce.cpp

Go to the documentation of this file.
00001 #include "Isis.h"
00002 #include "IException.h"
00003 #include "IString.h"
00004 #include "ProcessByLine.h"
00005 #include "Reduce.h"
00006 
00007 #include <cmath>
00008 
00009 using namespace std;
00010 using namespace Isis;
00011 
00012 void IsisMain() {
00013   try {
00014     // We will be processing by line
00015     ProcessByLine p;
00016     double sscale, lscale;
00017     int ins, inl, inb;
00018     int ons, onl;
00019     vector<QString> bands;
00020     Cube inCube;
00021 
00022     // To propogate labels, set input cube,
00023     // this cube will be cleared after output cube is set.
00024     p.SetInputCube("FROM");
00025 
00026     // Setup the input and output cubes
00027     UserInterface &ui = Application::GetUserInterface();
00028     QString replaceMode = ui.GetAsString("VPER_REPLACE");
00029     CubeAttributeInput cai(ui.GetAsString("FROM"));
00030     bands = cai.bands();
00031 
00032     inCube.setVirtualBands(bands);
00033 
00034     QString from = ui.GetFileName("FROM");
00035     inCube.open(from);
00036 
00037     ins = inCube.sampleCount();
00038     inl = inCube.lineCount();
00039     inb = inCube.bandCount();
00040 
00041     QString alg  = ui.GetString("ALGORITHM");
00042     double vper = ui.GetDouble("VALIDPER") / 100.;
00043 
00044     if(ui.GetString("MODE") == "TOTAL") {
00045       ons = ui.GetInteger("ONS");
00046       onl = ui.GetInteger("ONL");
00047       sscale = (double)ins / (double)ons;
00048       lscale = (double)inl / (double)onl;
00049     }
00050     else {
00051       sscale = ui.GetDouble("SSCALE");
00052       lscale = ui.GetDouble("LSCALE");
00053       ons = (int)((double)ins / sscale + 0.5);
00054       onl = (int)((double)inl / lscale + 0.5);
00055     }
00056 
00057     if(ons > ins || onl > inl) {
00058       QString msg = "Number of output samples/lines must be less than or equal";
00059       msg = msg + " to the input samples/lines.";
00060       throw IException(IException::User, msg, _FILEINFO_);
00061     }
00062 
00063     //  Allocate output file
00064     Cube *ocube = p.SetOutputCube("TO", ons, onl, inb);
00065     // Our processing routine only needs 1
00066     // the original set was for info about the cube only
00067     p.ClearInputCubes();
00068 
00069     // Start the processing
00070     PvlGroup results;
00071     if(alg == "AVERAGE"){
00072       Average average(&inCube, sscale, lscale, vper, replaceMode);
00073       p.ProcessCubeInPlace(average, false);
00074       results = average.UpdateOutputLabel(ocube);
00075     }
00076     else if(alg == "NEAREST") {
00077       Nearest near(&inCube, sscale, lscale);
00078       p.ProcessCubeInPlace(near, false);
00079       results = near.UpdateOutputLabel(ocube);
00080     }
00081 
00082     // Cleanup
00083     inCube.close();
00084     p.EndProcess();
00085 
00086     // Write the results to the log
00087     Application::Log(results);
00088   } // REFORMAT THESE ERRORS INTO ISIS TYPES AND RETHROW
00089   catch (IException &) {
00090     throw;
00091   }
00092   catch (std::exception const &se) {
00093     QString message = "std::exception: " + (QString)se.what();
00094     throw IException(IException::User, message, _FILEINFO_);
00095   }
00096   catch (...) {
00097     QString message = "Other Error";
00098     throw IException(IException::User, message, _FILEINFO_);
00099   }
00100 }