USGS

Isis 3.0 Application Source Code Reference

Home

hifringe.cpp

Go to the documentation of this file.
00001 /*
00002 $Id: hifringe.cpp,v 1.6 2008/05/14 21:07:24 slambright Exp $
00003 
00004 Copyright (C) 2005, 2006  Arizona Board of Regents on behalf of the
00005 Planetary Image Research Laboratory, Lunar and Planetary Laboratory at
00006 the University of Arizona.
00007 
00008 This library is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU Lesser General Public License, version 2.1,
00010 as published by the Free Software Foundation.
00011 
00012 This library is distributed in the hope that it will be useful, but
00013 WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 Lesser General Public License for more details.
00016 
00017 You should have received a copy of the GNU Lesser General Public License
00018 along with this library; if not, write to the Free Software Foundation,
00019 Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
00020 */
00021 
00022 #include "Isis.h"
00023 
00024 #include <string>
00025 
00026 #include "Process.h"
00027 #include "Statistics.h"
00028 #include "LineManager.h"
00029 #include "FileName.h"
00030 #include "IString.h"
00031 
00032 using namespace std;
00033 using namespace Isis;
00034 
00035 
00036 void pvlOut(Statistics stats1, Statistics stats2, QString name, int start,
00037             int end, PvlObject *one, PvlObject *two);
00038 
00039 static const char *const
00040 ID = "PIRL::hifringe ($Revision: 1.6 $ $Date: 2008/05/14 21:07:24 $)";
00041 
00042 void IsisMain() {
00043   UserInterface &ui = Application::GetUserInterface();
00044   Isis::FileName fromFile = ui.GetFileName("FROM");
00045 
00046   Isis::Cube inputCube;
00047   inputCube.open(fromFile.expanded());
00048 
00049   //Check to make sure we got the cube properly
00050   if(!inputCube.isOpen()) {
00051     QString msg = "Could not open FROM cube" + fromFile.expanded();
00052     throw IException(IException::User, msg, _FILEINFO_);
00053   }
00054   Process p;
00055   Cube *icube = p.SetInputCube("FROM");
00056 
00057   int totalSamples = icube->sampleCount();
00058   int totalLines   = icube->lineCount();
00059 
00060   Isis::LineManager lineManager(inputCube);
00061   lineManager.begin();
00062 
00063   int leftFringe, rightFringe;
00064   int binningMode = icube->group("Instrument")["Summing"];
00065 
00066   //determine the edges between which no statistics should be gathered
00067   leftFringe = 48 / binningMode;
00068   rightFringe = totalSamples - leftFringe;
00069 
00070 
00071   int numSections = ui.GetInteger("SECTIONS");
00072   if(numSections > 9) {
00073     QString msg = "You may have no more than 9 sections per side";
00074     throw IException(IException::User, msg, _FILEINFO_);
00075   }
00076 
00077   int sectionLength = 0;
00078   if(!ui.WasEntered("LINESIZE")) { //User didn't enter number of lines
00079     if(numSections == 0) {
00080       sectionLength = 0;
00081     }
00082     else {
00083       sectionLength = totalLines / numSections;
00084     }
00085   }
00086   else {
00087     sectionLength = ui.GetInteger("LINESIZE");
00088     if((sectionLength * numSections > totalLines) || sectionLength < 1) {
00089       sectionLength = totalLines / numSections;
00090     }
00091   }
00092 
00093   Statistics sections[numSections][2];
00094   Statistics leftTotal, rightTotal;
00095   int sectionStarts[numSections];
00096   sectionStarts[0] = 0;
00097   for(int i = 1 ; i < numSections - 1 ; i ++) {
00098     sectionStarts[i] = (totalLines / numSections) * i;
00099   }
00100   if(numSections > 0) {
00101     sectionStarts[numSections -1] = totalLines - sectionLength;
00102   }
00103 
00104   int currentSection = 0;
00105   Buffer leftFringeBuf(leftFringe, 1, 1, lineManager.PixelType());
00106   Buffer rightFringeBuf(leftFringe, 1, 1, lineManager.PixelType());
00107 
00108   //Walk down the cube
00109   for(int lineCount = 0 ; lineCount < totalLines ; lineCount++) {
00110     inputCube.read(lineManager);
00111     //Read the edges into the fringe buffers
00112     for(int i = 0 ; i < leftFringe ; i++) {
00113       leftFringeBuf[i] = lineManager[i];
00114     }
00115     for(int i = rightFringe ; i < totalSamples ; i++) {
00116       rightFringeBuf[i - rightFringe] = lineManager[i];
00117     }
00118 
00119     //No matter what, add the fringe buffers to the totals for that side
00120     leftTotal.AddData(leftFringeBuf.DoubleBuffer(), leftFringeBuf.size());
00121     rightTotal.AddData(rightFringeBuf.DoubleBuffer(), rightFringeBuf.size());
00122 
00123     if(numSections == 0) {
00124       continue;
00125     }
00126     //Index is not too large for this fringe section
00127     if(lineCount < sectionStarts[currentSection] + sectionLength) {
00128       //Index is not too small for this fringe section
00129       if(lineCount >= sectionStarts[currentSection]) {
00130         sections[currentSection][0].AddData(leftFringeBuf.DoubleBuffer(),
00131                                             leftFringeBuf.size());
00132         sections[currentSection][1].AddData(rightFringeBuf.DoubleBuffer(),
00133                                             rightFringeBuf.size());
00134       }
00135     }
00136     else {
00137       currentSection++;
00138       //Since sections may butt up against each other, it is possible that
00139       // we have to add this data to the next section.
00140       if(lineCount >= sectionStarts[currentSection]) {
00141         sections[currentSection][0].AddData(leftFringeBuf.DoubleBuffer(),
00142                                             leftFringeBuf.size());
00143         sections[currentSection][1].AddData(rightFringeBuf.DoubleBuffer(),
00144                                             rightFringeBuf.size());
00145       }
00146     }
00147   }
00148 
00149   // Write the results to the output file if the user specified one
00150   PvlObject leftSide("LeftSide"), rightSide("RightSide");
00151   for(int i = 0 ; i < numSections ; i++) {
00152     QString sectionName = "Section" + toString(i + 1);
00153     pvlOut(sections[i][0], //Stats to add to the left Object
00154            sections[i][1], //Stats to add to the right Object
00155            sectionName,    //Name for the new groups
00156            sectionStarts[i], //start line
00157            sectionStarts[i] + sectionLength, //end line
00158            &leftSide,      //Object to add left group to
00159            &rightSide      //Object to add right group to
00160           );
00161   }
00162   pvlOut(leftTotal,   //Stats to add to the left Object
00163          rightTotal,  //Stats to add to the right Object
00164          "Total",     //Name for the new groups
00165          0,           //start line
00166          totalLines,  //end line
00167          &leftSide,   //Object to add left group to
00168          &rightSide   //Object to add right group to
00169         );
00170   Pvl outputPvl;
00171   PvlGroup sourceInfo("SourceInfo");
00172 
00173   sourceInfo += PvlKeyword("From", fromFile.expanded());
00174   sourceInfo += icube->group("Archive")["ProductId"];
00175   outputPvl.AddGroup(sourceInfo);
00176   if(numSections > 0) {
00177     outputPvl.AddObject(leftSide);
00178     outputPvl.AddObject(rightSide);
00179   }
00180   else {
00181     PvlGroup leftGroup = leftSide.FindGroup("Total");
00182     PvlGroup rightGroup = rightSide.FindGroup("Total");
00183     leftGroup.SetName("LeftSide");
00184     rightGroup.SetName("RightSide");
00185     outputPvl.AddGroup(leftGroup);
00186     outputPvl.AddGroup(rightGroup);
00187   }
00188   outputPvl.Write(ui.GetFileName("TO"));
00189 }
00190 
00191 void pvlOut(Statistics stats1, Statistics stats2, QString name, int start,
00192             int end, PvlObject *one, PvlObject *two) {
00193   PvlGroup left(name);
00194   left += PvlKeyword("StartLine", toString(start + 1));
00195   left += PvlKeyword("EndLine", toString(end));
00196   left += PvlKeyword("TotalPixels", toString(stats1.TotalPixels()));
00197   left += PvlKeyword("ValidPixels", toString(stats1.ValidPixels()));
00198   if(stats1.ValidPixels() > 0) {
00199     left += PvlKeyword("Mean", toString(stats1.Average()));
00200     left += PvlKeyword("StandardDeviation", toString(stats1.StandardDeviation()));
00201     left += PvlKeyword("Minimum", toString(stats1.Minimum()));
00202     left += PvlKeyword("Maximum", toString(stats1.Maximum()));
00203   }
00204   one->AddGroup(left);
00205 
00206   PvlGroup right(name);
00207   right += PvlKeyword("StartLine", toString(start + 1));
00208   right += PvlKeyword("EndLine", toString(end));
00209   right += PvlKeyword("TotalPixels", toString(stats2.TotalPixels()));
00210   right += PvlKeyword("ValidPixels", toString(stats2.ValidPixels()));
00211   if(stats2.ValidPixels() > 0) {
00212     right += PvlKeyword("Mean", toString(stats2.Average()));
00213     right += PvlKeyword("StandardDeviation", toString(stats2.StandardDeviation()));
00214     right += PvlKeyword("Minimum", toString(stats2.Minimum()));
00215     right += PvlKeyword("Maximum", toString(stats2.Maximum()));
00216   }
00217   two->AddGroup(right);
00218 }