Skip to content

Commit

Permalink
Bandnorm tests (#4189)
Browse files Browse the repository at this point in the history
* init things

* updated tests

* removed old tests

* address comments
  • Loading branch information
Kelvin Rodriguez authored Dec 15, 2020
1 parent 1fc23d0 commit 2b4c1fa
Show file tree
Hide file tree
Showing 8 changed files with 431 additions and 198 deletions.
173 changes: 173 additions & 0 deletions isis/src/base/apps/bandnorm/bandnorm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// system include files go first
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>

// Isis specific include files go next
#include "ProcessByLine.h"
#include "SpecialPixel.h"
#include "IException.h"
#include "Pvl.h"
#include "TextFile.h"
#include "Statistics.h"

#include "bandnorm.h"

using namespace std;

namespace Isis {

static vector<int> band;
static vector<double> average;
static vector<double> normalizer;

// function prototypes
void getStats(Buffer &in);
void normalize(Buffer &in, Buffer &out);
void Tokenize(const QString &str,
vector<QString> & tokens,
const QString &delimiters = " ");

void bandnorm(UserInterface &ui) {
Cube icube(ui.GetFileName("FROM"), "r");
bandnorm(&icube, ui);
}


void bandnorm(Cube *icube, UserInterface &ui) {
// We will be processing by line
ProcessByLine p;

// Now get the statistics for each band or the entire cube
QString avg = ui.GetString("AVERAGE");
p.SetInputCube(icube);
p.StartProcess(getStats);
if(avg == "BAND") {
int b = 0;
Statistics stats;
for(int i = 0; i < (int)average.size(); i++) {
if(b == band[i]) {
stats.AddData(&average[i], (unsigned int)1);
}
else {
normalizer.push_back(stats.Average());
b++;
stats.Reset();
}
}
normalizer.push_back(stats.Average());
}
else if(avg == "PENCIL") {
TextFile pencil;
pencil.Open(ui.GetFileName("SPECTRUM"));
std::cout << pencil.LineCount() << " " << icube->bandCount() << std::endl;
if(pencil.LineCount() - 1 < icube->bandCount()) {
QString msg = "The spectral pencil file [" + ui.GetAsString("SPECTRUM") +
"] does not contain enough data for all bands.";
throw IException(IException::User, msg, _FILEINFO_);
}
QString st;
int column = -1;
vector<QString> tokens;
pencil.GetLine(st); //Takes care of title line
Tokenize(st, tokens, ", \"-+");
if(ui.GetAsString("METHOD") == "number") {
column = ui.GetInteger("NUMBER");
}
else {
for(unsigned i = 0; i < tokens.size(); i++) {
if(tokens[i] == ui.GetString("NAME")) {
column = i;
break;
}
}
}
if(column < 0 || (unsigned)column > tokens.size()) {
QString msg = "The column specified in file [" + ui.GetFileName("SPECTRUM")
+ "] was not found.";
throw IException(IException::User, msg, _FILEINFO_);
}
// Add the correct column of data to normalizer
for(int i = 0; i < icube->bandCount(); i++) {
tokens.clear();
pencil.GetLine(st);
Tokenize(st, tokens, ", \"");
std::cout << "col: " << column << std::endl;
std::cout << Isis::IString(tokens[column]).ToDouble() << std::endl;
normalizer.push_back(Isis::IString(tokens[column]).ToDouble());
}
}
else { // avg == "CUBE"
Statistics stats;
for(int i = 0; i < (int)average.size(); i++) {
stats.AddData(&average[i], (unsigned int)1);
}
for(int b = 0; b < icube->bandCount(); b++) {
normalizer.push_back(stats.Average());
}
}

// Setup the output file and apply the correction
p.SetOutputCube(ui.GetFileName("TO"), ui.GetOutputAttribute("TO"), icube->sampleCount(), icube->lineCount(), icube->bandCount());
p.StartProcess(normalize);

// Cleanup
p.EndProcess();
normalizer.clear();
band.clear();
average.clear();
}

//**********************************************************
// Get statistics on a band or entire cube
//**********************************************************
void getStats(Buffer &in) {
Statistics stats;
stats.AddData(in.DoubleBuffer(), in.size());
average.push_back(stats.Average());
band.push_back(in.Band() - 1);
}

// Apply coefficients
void normalize(Buffer &in, Buffer &out) {
int index = in.Band() - 1;
double coeff = normalizer[index];

// Now loop and apply the coefficents
for(int i = 0; i < in.size(); i++) {
if(IsSpecial(in[i])) {
out[i] = in[i];
}
else {
out[i] = Null;
if(coeff != 0.0 && IsValidPixel(coeff)) {
out[i] = in[i] / coeff;
}
}
}
}

// Tokenizer
void Tokenize(const QString &strQStr,
vector<QString> & tokens,
const QString &delimitersQStr) {
IString str = strQStr;
IString delimiters = delimitersQStr;

//Skip delimiters at the beginning
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);

while(string::npos != pos || string::npos != lastPos) {
// Found a token, add it to the vector
tokens.push_back(str.substr(lastPos, pos - lastPos).c_str());
// Skip delimiters
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
}
12 changes: 12 additions & 0 deletions isis/src/base/apps/bandnorm/bandnorm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef std2isis_h
#define std2isis_h

#include "UserInterface.h"
#include "Cube.h"

namespace Isis {
extern void bandnorm(Cube *icube, UserInterface &ui);
extern void bandnorm(UserInterface &ui);
}

#endif
162 changes: 4 additions & 158 deletions isis/src/base/apps/bandnorm/main.cpp
Original file line number Diff line number Diff line change
@@ -1,165 +1,11 @@
#include "Isis.h"

// system include files go first
#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include "Application.h"
#include "bandnorm.h"

// Isis specific include files go next
#include "ProcessByLine.h"
#include "SpecialPixel.h"
#include "IException.h"
#include "Pvl.h"
#include "TextFile.h"
#include "Statistics.h"

using namespace std;
using namespace Isis;

static vector<int> band;
static vector<double> average;
static vector<double> normalizer;

// function prototypes
void getStats(Buffer &in);
void normalize(Buffer &in, Buffer &out);
void Tokenize(const QString &str,
vector<QString> & tokens,
const QString &delimiters = " ");

void IsisMain() {
// We will be processing by line
ProcessByLine p;

// Setup the input cube
UserInterface &ui = Application::GetUserInterface();
Cube *icube = p.SetInputCube("FROM");

// Now get the statistics for each band or the entire cube
QString avg = ui.GetString("AVERAGE");
p.StartProcess(getStats);
if(avg == "BAND") {
int b = 0;
Statistics stats;
for(int i = 0; i < (int)average.size(); i++) {
if(b == band[i]) {
stats.AddData(&average[i], (unsigned int)1);
}
else {
normalizer.push_back(stats.Average());
b++;
stats.Reset();
}
}
normalizer.push_back(stats.Average());
}
else if(avg == "PENCIL") {
TextFile pencil;
pencil.Open(ui.GetFileName("SPECTRUM"));
if(pencil.LineCount() - 1 < icube->bandCount()) {
QString msg = "The spectral pencil file [" + ui.GetAsString("SPECTRUM") +
"] does not contain enough data for all bands.";
throw IException(IException::User, msg, _FILEINFO_);
}
QString st;
int column = -1;
vector<QString> tokens;
pencil.GetLine(st); //Takes care of title line
Tokenize(st, tokens, ", \"-+");
if(ui.GetAsString("METHOD") == "number") {
column = ui.GetInteger("NUMBER");
}
else {
for(unsigned i = 0; i < tokens.size(); i++) {
if(tokens[i] == ui.GetString("NAME")) {
column = i;
break;
}
}
}
if(column < 0 || (unsigned)column > tokens.size()) {
QString msg = "The column specified in file [" + ui.GetFileName("SPECTRUM")
+ "] was not found.";
throw IException(IException::User, msg, _FILEINFO_);
}
// Add the correct column of data to normalizer
for(int i = 0; i < icube->bandCount(); i++) {
tokens.clear();
pencil.GetLine(st);
Tokenize(st, tokens, ", \"");
normalizer.push_back(Isis::IString(tokens[column]).ToDouble());
}
}
else { // avg == "CUBE"
Statistics stats;
for(int i = 0; i < (int)average.size(); i++) {
stats.AddData(&average[i], (unsigned int)1);
}
for(int b = 0; b < icube->bandCount(); b++) {
normalizer.push_back(stats.Average());
}
}

// Setup the output file and apply the correction
p.SetOutputCube("TO");
p.StartProcess(normalize);

// Cleanup
p.EndProcess();
normalizer.clear();
band.clear();
average.clear();
}

//**********************************************************
// Get statistics on a band or entire cube
//**********************************************************
void getStats(Buffer &in) {
Statistics stats;
stats.AddData(in.DoubleBuffer(), in.size());
average.push_back(stats.Average());
band.push_back(in.Band() - 1);
}

// Apply coefficients
void normalize(Buffer &in, Buffer &out) {
int index = in.Band() - 1;
double coeff = normalizer[index];

// Now loop and apply the coefficents
for(int i = 0; i < in.size(); i++) {
if(IsSpecial(in[i])) {
out[i] = in[i];
}
else {
out[i] = Null;
if(coeff != 0.0 && IsValidPixel(coeff)) {
out[i] = in[i] / coeff;
}
}
}
}

// Tokenizer
void Tokenize(const QString &strQStr,
vector<QString> & tokens,
const QString &delimitersQStr) {
IString str = strQStr;
IString delimiters = delimitersQStr;

//Skip delimiters at the beginning
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);

while(string::npos != pos || string::npos != lastPos) {
// Found a token, add it to the vector
tokens.push_back(str.substr(lastPos, pos - lastPos).c_str());
// Skip delimiters
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
bandnorm(ui);
}
4 changes: 0 additions & 4 deletions isis/src/base/apps/bandnorm/tsts/Makefile

This file was deleted.

7 changes: 0 additions & 7 deletions isis/src/base/apps/bandnorm/tsts/batchlist/Makefile

This file was deleted.

7 changes: 0 additions & 7 deletions isis/src/base/apps/bandnorm/tsts/default/Makefile

This file was deleted.

Loading

0 comments on commit 2b4c1fa

Please sign in to comment.