Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
FloCiaglia committed Sep 19, 2019
2 parents 9891190 + 1c86435 commit 3994935
Show file tree
Hide file tree
Showing 19 changed files with 587 additions and 140 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ $(BIN)/LidarDriver_unittests: $(OBJ)/LidarDriver_unittests.o \
$(OBJ)/CmdLine.o \
$(OBJ)/FlightLineData.o $(OBJ)/LidarVolume.o \
$(OBJ)/LidarDriver.o $(OBJ)/WaveGPSInformation.o\
$(OBJ)/PulseData.o \
$(OBJ)/PulseData.o $(OBJ)/TxtWaveReader.o\
$(OBJ)/Peak.o $(OBJ)/GaussianFitter.o\
$(LIB)/gtest_main.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@ -L \
Expand Down Expand Up @@ -205,7 +205,8 @@ $(BIN)/geotiff-driver: $(OBJ)/pls_to_geotiff.o $(OBJ)/CmdLine.o \
$(OBJ)/FlightLineData.o $(OBJ)/LidarVolume.o \
$(OBJ)/LidarDriver.o $(OBJ)/WaveGPSInformation.o\
$(OBJ)/WaveGPSInformation.o $(OBJ)/PulseData.o \
$(OBJ)/Peak.o $(OBJ)/GaussianFitter.o
$(OBJ)/Peak.o $(OBJ)/GaussianFitter.o \
$(OBJ)/TxtWaveReader.o
$(CXX) $(PFLAG) $(CPPFLAGS) $(CXXFLAGS) -g -lpthread $^ -o $@ -L \
$(PULSE_DIR)/lib -lpulsewaves -lgdal -lm -lgsl \
-lgslcblas
Expand All @@ -221,7 +222,7 @@ $(BIN)/csv-driver: $(OBJ)/pls_to_csv.o $(OBJ)/csv_CmdLine.o \
$(OBJ)/FlightLineData.o $(OBJ)/LidarVolume.o \
$(OBJ)/LidarDriver.o $(OBJ)/WaveGPSInformation.o \
$(OBJ)/PulseData.o $(OBJ)/Peak.o $(OBJ)/GaussianFitter.o \
$(OBJ)/CsvWriter.o
$(OBJ)/CsvWriter.o $(OBJ)/TxtWaveReader.o
$(CXX) $(PFLAG) $(CPPFLAGS) $(CXXFLAGS) -g -lpthread $^ -o $@ -L \
$(PULSE_DIR)/lib -lpulsewaves -lgdal -lm -lgsl \
-lgslcblas
Expand Down
33 changes: 30 additions & 3 deletions src/CmdLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ const static std::string prod_peaks[3] = {"first", "last", "all"};
const static std::string prod_vars[7] = {"elev", "amp", "width",
"riseTime", "backscatter", "heightAtEnergy", "energyAtHeight"};

//Verbosities
const static int num_verbs = 6;
const static std::string verbs[num_verbs] = {"trace", "debug", "info", "warn",
"error", "critical"};

const static std::vector<int> start = {0,18,36,54,72,90};//,96,102};

/****************************************************************************
Expand Down Expand Up @@ -52,7 +57,7 @@ void CmdLine::setUsageMessage()
<< " :Disables gaussian fitter, using first diff method instead" << std::endl;
buffer << " -h"
<< " :Prints this help message" << std::endl;
buffer << std::endl;
buffer << std::endl;
buffer << "Product Type Options:" << std::endl;
buffer << " -e <list of products>"
<< " :Generates Elevation products" << std::endl;
Expand All @@ -68,6 +73,10 @@ void CmdLine::setUsageMessage()
buffer << " Scientific notation allowed for calibration constant"
<< " (e.g. 0.78 = 7.8e-1 = 7.8E-1)"
<< std::endl;
buffer << " -v <verbosity level>"
<< " :Sets the level of verbosity for the logger to use" << std::endl;
buffer << " Options are 'trace', 'debug', 'info', 'warn', 'error'"
<< ", and 'critical'" << std::endl;
buffer << " --all <calibration constant>"
<< " :Generates all products for every variable. calibration constant"
<< " is used for backscatter coefficient calculations" << std::endl;
Expand Down Expand Up @@ -124,6 +133,7 @@ CmdLine::CmdLine(){
printUsageMessage = false;
useGaussianFitting = true;
calcBackscatter = false;
verb = (verbosity) NULL;
exeName = "";
setUsageMessage();
}
Expand All @@ -138,7 +148,18 @@ std::string CmdLine::getInputFileName(bool pls){
return pls ? plsFileName : wvsFileName;
}


/**
* Tries to match option given with verbosity flag to a known value,
* and if so, sets verbosity instance variable to match it.
*/
void CmdLine::set_verbosity (char* new_verb) {
int i;
for (i = 0; i < num_verbs; i++) {
if (!std::strncmp (new_verb, verbs[i].c_str(), 9)) {
verb = (verbosity) i;
}
}
}

/**
* Function that parses the command line arguments
Expand Down Expand Up @@ -171,6 +192,7 @@ bool CmdLine::parse_args(int argc,char *argv[]){
{"file", required_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"firstdiff", no_argument, NULL, 'd'},
{"verbosity", required_argument, NULL, 'v'},
{"elevation", required_argument,NULL,'e'},
{"amplitude", required_argument,NULL,'a'},
{"width", required_argument,NULL,'w'},
Expand All @@ -188,7 +210,7 @@ bool CmdLine::parse_args(int argc,char *argv[]){
* ":hf:s:" indicate that option 'h' is without arguments while
* option 'f' and 's' require arguments
*/
while((optionChar = getopt_long (argc, argv, "-:hdf:e:a:w:r:b:l:",
while((optionChar = getopt_long (argc, argv, "-:hdf:e:a:w:r:b:l:v:",
long_options, &option_index))!= -1){
if (optionChar == 'f') { //Set the filename to parse
fArg = optarg;
Expand All @@ -197,6 +219,11 @@ bool CmdLine::parse_args(int argc,char *argv[]){
printUsageMessage = true;
} else if (optionChar == 'd') { //Sets analysis method
useGaussianFitting = false;
} else if (optionChar == 'v') {
set_verbosity(optarg);
if (verb == (verbosity) NULL) {
printUsageMessage = true;
}
} else if (optionChar == 'e' || optionChar == 'a' || optionChar == 'w'
|| optionChar == 'r' || optionChar == 'b'){
//Sets which pruducts to create and for which variable
Expand Down
6 changes: 5 additions & 1 deletion src/CmdLine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CmdLine{
// else, use smooth second difference
bool max_elev_flag;


void set_verbosity(char* new_verb);

public:
//calibration constant (for backscatter option)
Expand All @@ -49,6 +49,10 @@ class CmdLine{
//True stifles all output statements
bool quiet;

// For conveying verbosity to main function in a readable way
enum verbosity { trace, debug, info, warn, error, critical };
verbosity verb;

CmdLine();


Expand Down
2 changes: 2 additions & 0 deletions src/CmdLine_unittests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ TEST_F(CmdLineTest, invalidNonProductOption){
ASSERT_TRUE(cmd.printUsageMessage);
}

//Tests that verbosity is set to the correct value by the -v option

/****************************************************************************
*
* Long Option Tests
Expand Down
50 changes: 28 additions & 22 deletions src/GaussianFitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ GaussianFitter::GaussianFitter(){
pass = 0;
total = 0;

// Set fitter params to default values
max_iter = MAX_ITER;

//Set instance variables to default values, as defined in header.
tolerance_scales = TOL_SCALES;
x_tolerance = X_TOL;
g_tolerance = G_TOL;
Expand All @@ -28,6 +26,12 @@ GaussianFitter::GaussianFitter(){

amp_upper_bound = AMP_UPPER_BOUND;
amp_lower_bound = AMP_LOWER_BOUND;

log_diagnostics = false;
}

void GaussianFitter::setDiagnostics(bool newval) {
log_diagnostics = newval;
}


Expand Down Expand Up @@ -237,27 +241,27 @@ void callback(const size_t iter, void *params,
double avratio = gsl_multifit_nlinear_avratio(w);
double rcond;

(void) params; /* not used */

/* compute reciprocal condition number of J(x) */
gsl_multifit_nlinear_rcond(&rcond, w);

size_t npeaks = x->size/3;
spdlog::trace("iter {}:",iter);
size_t j;
for(j=0; j<npeaks; j++){
spdlog::trace(
if (*((bool*) params)) { //solve system passes log_diagnostics over params.
size_t npeaks = x->size/3;
spdlog::trace("iter {}:",iter);
size_t j;
for(j=0; j<npeaks; j++){
spdlog::trace(
"peak {}: amp = {:#.6g}, t = {:#.6g}, width = {:#.6g}",
j,
gsl_vector_get(x,3*j+ 0),
gsl_vector_get(x,3*j+ 1),
gsl_vector_get(x,3*j+ 2));
}
spdlog::trace(
}
spdlog::trace(
"Also, |a|/|v| = {:#.6g} cond(J) = {:#.6g}, |f(x)| = {:#.6g}",
avratio,
1.0 / rcond,
gsl_blas_dnrm2(f));
}
}


Expand Down Expand Up @@ -287,9 +291,8 @@ void handler (const char * reason,
* @return
*/
int GaussianFitter::solve_system(gsl_vector *x, gsl_multifit_nlinear_fdf *fdf,
gsl_multifit_nlinear_parameters *params, int max) {
const size_t max_iter = this->max_iter;

gsl_multifit_nlinear_parameters *params, int max,
const size_t max_iter) {
const gsl_multifit_nlinear_type *T = gsl_multifit_nlinear_trust;

const double xtol = tolerance_scales ? max / x_tolerance : x_tolerance;
Expand Down Expand Up @@ -318,7 +321,8 @@ int GaussianFitter::solve_system(gsl_vector *x, gsl_multifit_nlinear_fdf *fdf,

/* iterate until convergence */
status = gsl_multifit_nlinear_driver(max_iter, xtol, gtol, ftol,
callback, NULL, &info, work);
callback, (void*) &log_diagnostics
, &info, work);
if (status) {
// std::cerr << "There was an error: " << gsl_strerror (status)
// << "\n" << std::endl;
Expand Down Expand Up @@ -364,11 +368,13 @@ int GaussianFitter::solve_system(gsl_vector *x, gsl_multifit_nlinear_fdf *fdf,
* @param results pointer to vector to store peaks
* @param ampData
* @param idxData
* @param max_iter
* @return count of found peaks
*/
int GaussianFitter::find_peaks(std::vector<Peak*>* results,
std::vector<int> ampData,
std::vector<int> idxData) {
std::vector<int> idxData,
const size_t max_iter) {
incr_total();

//Error handling
Expand Down Expand Up @@ -457,7 +463,7 @@ int GaussianFitter::find_peaks(std::vector<Peak*>* results,

spdlog::error("peakCount = {}",peakCount);

if(!solve_system(x, &fdf, &fdf_params, max)){
if(!solve_system(x, &fdf, &fdf_params, max, max_iter)){
incr_pass();

//this loop is going through every peak
Expand Down Expand Up @@ -624,7 +630,6 @@ int GaussianFitter::guess_peaks(std::vector<Peak*>* results,
//are pointing to space used in LidarVolume
results->clear();


//UPDATE: We are only using a noise level of 6 because we want all peaks
//with an amplitude >= 10
//Level up to and including which peaks will be excluded
Expand All @@ -643,8 +648,7 @@ int GaussianFitter::guess_peaks(std::vector<Peak*>* results,
noise_level = ((float)max)*.09;

spdlog::error("Max = {} Noise = {}", max, ((float)max)*.09);



if (noise_level < 6){
noise_level = 6;
}
Expand Down Expand Up @@ -728,10 +732,12 @@ int GaussianFitter::guess_peaks(std::vector<Peak*>* results,
guess = guess_lt0_default;
}

spdlog::debug(
if (log_diagnostics) {
spdlog::debug(
"Guess for peak {}: amp {}; time: {}; width: {}",
i, ampData[peak_guesses_loc[i]], idxData[peak_guesses_loc[i]],
guess);
}

if(guess > 20) {guess = 10;}
peaks_found++;
Expand Down
11 changes: 7 additions & 4 deletions src/GaussianFitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class GaussianFitter{

public:
int find_peaks(std::vector<Peak*>* results,std::vector<int> ampData,
std::vector<int> idxData);
std::vector<int> idxData, const size_t max_iter);
int noise_level;
int guess_peaks(std::vector<Peak*>* results,
std::vector<int> ampData,
Expand All @@ -47,6 +47,8 @@ class GaussianFitter{
int get_pass();
int get_total();

void setDiagnostics(bool newval);

std::vector<std::string> equations; //Fitted equations

int max; //The max peak amplitude of each set of returning waves
Expand All @@ -56,8 +58,6 @@ class GaussianFitter{

// *** Fitter parameters (that were magic numbers once) ***

size_t max_iter;

bool tolerance_scales;
double x_tolerance;
double g_tolerance;
Expand All @@ -72,9 +72,12 @@ class GaussianFitter{


private:
bool log_diagnostics;

int solve_system (gsl_vector *x,
gsl_multifit_nlinear_fdf *fdf,
gsl_multifit_nlinear_parameters *params, int max);
gsl_multifit_nlinear_parameters *params, int max,
const size_t max_iter);

std::vector<int> calculateFirstDifferences(std::vector<int>ampData);
void incr_fail();
Expand Down
Loading

0 comments on commit 3994935

Please sign in to comment.