USGS

Isis 3.0 Application Source Code Reference

Home

cubeavg.cpp

Go to the documentation of this file.
00001 #include "Isis.h"
00002 #include "ProcessBySpectra.h"
00003 #include "Statistics.h"
00004 #include "Application.h"
00005 #include "PvlGroup.h"
00006 #include "PvlSequence.h"
00007 
00008 using namespace std; 
00009 using namespace Isis;
00010 
00011 void cubeavg (vector<Buffer *> &in,
00012               vector<Buffer *> &out);
00013 
00014 void removekeywords ( PvlGroup & pvlg );
00015 
00016 void compute (vector<double> centers,
00017               vector<double> widths,
00018               Cube *ocube );
00019 
00020 void IsisMain() {
00021   ProcessBySpectra p;
00022   p.SetType( ProcessBySpectra::PerPixel );
00023   Cube *icube = p.SetInputCube("FROM");
00024   Cube *ocube = p.SetOutputCube("TO", icube->Samples(), icube->Lines(), 1);
00025 
00026   //Get user parameters and sets outputcube's BandBin
00027   UserInterface &ui = Application::GetUserInterface();
00028   if(ui.GetString("BANDBIN") == "COMPUTE") {
00029     if( icube->HasGroup("BandBin") ) {
00030       PvlGroup &pvlg = icube->GetGroup("BandBin");
00031       removekeywords( pvlg );
00032       if(pvlg.HasKeyword("Center")) {
00033         bool hasWidth = pvlg.HasKeyword("Width");
00034         PvlKeyword &pvlCenter = pvlg.FindKeyword("Center");
00035         PvlKeyword * pvlWidth = NULL;
00036         if(hasWidth) {
00037           pvlWidth = & pvlg.FindKeyword("Width");
00038         }
00039         std::vector<double> centers;
00040         centers.resize(icube->Bands());
00041         std::vector<double> widths;
00042         widths.resize(icube->Bands());
00043         for( int i=0; i<pvlCenter.Size(); i++ ) {
00044           centers[i] = pvlCenter[i];
00045           if( hasWidth )
00046             widths[i] = (*pvlWidth)[i];
00047           else
00048             widths[i] = 0.0;
00049         }
00050         compute(centers, widths, ocube);
00051       }
00052       else {
00053         string message = "The BandBin in your input cube does not have a Center value.";
00054         throw Isis::iException::Message(Isis::iException::User,message,_FILEINFO_);
00055       }
00056     }
00057     else{
00058       string message = "There is not a BandBin Group in the input cube.";
00059       throw Isis::iException::Message(Isis::iException::User,message,_FILEINFO_);
00060     }
00061   }
00062 
00063   else if(ui.GetString("BANDBIN") == "USER") {
00064     PvlGroup pvlg;
00065     if(!icube->HasGroup("BandBin")) {
00066       pvlg = PvlGroup("BandBin");
00067       icube->PutGroup(pvlg);
00068     }
00069     else {
00070       pvlg = ocube->GetGroup("BandBin");
00071       removekeywords(pvlg);
00072     }
00073     string Units = "";
00074     PvlKeyword pvlCenter;
00075     if(pvlg.HasKeyword("Center")) {
00076       pvlCenter = pvlg.FindKeyword("Center");
00077       Units = pvlCenter.Unit();
00078       pvlg.DeleteKeyword("Center");
00079     }
00080 
00081     pvlCenter = PvlKeyword("Center");
00082     pvlCenter.SetValue(ui.GetAsString("CENTER"), Units);
00083     pvlg.AddKeyword(pvlCenter);
00084     PvlKeyword pvlWidth;
00085     if( pvlg.HasKeyword("Width") ) {
00086       pvlWidth = pvlg.FindKeyword("Width");
00087       Units = pvlWidth.Unit();
00088       pvlg.DeleteKeyword("Width");
00089     }
00090 
00091     pvlWidth = PvlKeyword("Width");
00092     pvlWidth.SetValue(ui.GetAsString("WIDTH"), Units);
00093     pvlg.AddKeyword(pvlWidth);
00094     //Destroys the old and adds the new BandBin Group
00095     if( ocube->HasGroup("BandBin") ) {
00096       ocube->DeleteGroup("BandBin");
00097     }
00098     ocube->PutGroup(pvlg);
00099   }
00100 
00101   else if(ui.GetString("BANDBIN") == "DELETE") {
00102     if(ocube->HasGroup("BandBin")) {
00103       ocube->DeleteGroup("BandBin");
00104     }
00105   }
00106 
00107   p.StartProcess(cubeavg);
00108   p.EndProcess();
00109 }
00110 
00111 // Band processing routine
00112 void cubeavg ( vector<Buffer *> &in, vector<Buffer *> &out ) {
00113   Statistics sts;
00114   sts.AddData( (*in[0]).DoubleBuffer() , (*in[0]).size() );
00115   (*out[0]) = sts.Average();
00116 }
00117 
00118 /** 
00119  * Removes the PvlKeywords that can't be processed
00120  * 
00121  * @param pvlg the group from which the keywords are removed
00122  */
00123 void removekeywords ( PvlGroup & pvlg ) {
00124   if( pvlg.HasKeyword("OriginalBand") ) {
00125     pvlg.DeleteKeyword("OriginalBand");
00126   }
00127   if( pvlg.HasKeyword("Name") ) {
00128     pvlg.DeleteKeyword("Name");
00129   }
00130 }
00131 
00132 //BandBin Computeing
00133 void compute( vector<double> centers, vector<double> widths,
00134               Cube *ocube ) {
00135   PvlGroup &pvlg = ocube->GetGroup("BandBin");
00136   PvlKeyword &pvlCenter = pvlg.FindKeyword("Center");
00137   string centerUnit = pvlCenter.Unit();
00138   bool hasWidth  = pvlg.HasKeyword("Width");
00139   double large = centers[0] + widths[0]/2;
00140   double small = centers[0] - widths[0]/2;
00141   for( int i=1; i<pvlCenter.Size(); i++ ) {
00142     if( large < (double)centers[i] + (double)widths[i]/2.0 ) {
00143       large = (double)centers[i] + (double)widths[i]/2.0;
00144     }
00145     if( small > (double)centers[i] - (double)widths[i]/2.0 ) {
00146       small = (double)centers[i] - (double)widths[i]/2.0;
00147     }
00148   }
00149   pvlCenter.SetValue( iString( (large-small)/2 + small ), centerUnit );
00150   if( hasWidth ) {
00151     PvlKeyword &pvlWidth  = pvlg.FindKeyword("Width");
00152     pvlWidth.SetValue( large-small, pvlWidth.Unit() );
00153   }
00154   else {
00155     PvlKeyword pvlWidth = PvlKeyword("Width");
00156     pvlWidth.SetValue( large-small, centerUnit );
00157     pvlg.AddKeyword(pvlWidth);
00158   }
00159   
00160 }