Skip to content

Commit

Permalink
Per #1019, more changes to address SonarQube code smells and reduce t…
Browse files Browse the repository at this point in the history
…he overall number of them lower than what's in the develop branch.
  • Loading branch information
JohnHalleyGotway committed Jan 14, 2025
1 parent c02f1c9 commit 8a9b02a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 104 deletions.
2 changes: 1 addition & 1 deletion src/basic/vx_util/data_line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ while (it != end) {

if ( Delimiter.find(Line.back()) != string::npos ) Items.emplace_back("");

N_items = Items.size();
N_items = (int) Items.size();

LineNumber = ldf->last_line_number() + 1;

Expand Down
42 changes: 1 addition & 41 deletions src/basic/vx_util/num_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ using namespace std;
////////////////////////////////////////////////////////////////////////


NumArray::NumArray()

{

init_from_scratch();

}
NumArray::NumArray() {}


////////////////////////////////////////////////////////////////////////
Expand All @@ -67,8 +61,6 @@ NumArray::NumArray(const NumArray & a)

{

init_from_scratch();

assign(a);

}
Expand All @@ -77,24 +69,6 @@ NumArray::NumArray(const NumArray & a)
////////////////////////////////////////////////////////////////////////


NumArray::NumArray(const vector<double> & a)

{

clear();

e = a;

Sorted = false;

return;

}


////////////////////////////////////////////////////////////////////////


NumArray & NumArray::operator=(const NumArray & a)

{
Expand Down Expand Up @@ -136,20 +110,6 @@ bool NumArray::operator==(const NumArray & a) const
////////////////////////////////////////////////////////////////////////


void NumArray::init_from_scratch()

{

clear();

return;

}


////////////////////////////////////////////////////////////////////////


void NumArray::clear()

{
Expand Down
7 changes: 3 additions & 4 deletions src/basic/vx_util/num_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,19 @@ class NumArray {

private:

void init_from_scratch();

void assign(const NumArray &);

std::vector<double> e;

bool Sorted;
bool Sorted = false;

public:

NumArray();
~NumArray();
NumArray(const NumArray &);
NumArray(const std::vector<double> &);
explicit NumArray(const std::vector<double> &a) : e(a) {};

NumArray & operator=(const NumArray &);
bool operator==(const NumArray &) const;

Expand Down
10 changes: 5 additions & 5 deletions src/libcode/vx_grid/unstructured_grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ void UnstructuredData::build_tree() {
for (int i=0; i<n_face; i++) {
llh_to_ecef(points_XYZ[i].y(), points_XYZ[i].x(),
points_XYZ[i].z(), &x_km, &y_km, &z_km);
points_XYZ_km.emplace_back({x_km, y_km, z_km});
points_XYZ_km.push_back({x_km, y_km, z_km});
kdtree->insert(points_XYZ_km[i], n);
n++;
lat_checksum += (i+1) * y_km;
Expand Down Expand Up @@ -483,7 +483,7 @@ void UnstructuredData::set_points(int count, const double *_lon, const double *_
n_face = count;
points_lonlat.reserve(count);
for (int i=0; i<count; i++) {
points_lonlat.emplace_back({_lon[i], _lat[i]});
points_lonlat.push_back({_lon[i], _lat[i]});
}
if(mlog.verbosity_level() >= UGRID_DEBUG_LEVEL) mlog
<< Debug(UGRID_DEBUG_LEVEL) << "UnstructuredData::set_points(int, double *, double *) first ("
Expand All @@ -505,7 +505,7 @@ void UnstructuredData::set_points(int count, const double *_lon, const double *_
n_face = count;
points_XYZ.reserve(count);
for (int i=0; i<count; i++) {
points_XYZ.emplace_back({_lon[i], _lat[i], _alt[i]});
points_XYZ.push_back({_lon[i], _lat[i], _alt[i]});
}
if(mlog.verbosity_level() >= UGRID_DEBUG_LEVEL) mlog
<< Debug(UGRID_DEBUG_LEVEL) << "UnstructuredData::set_points(int, double *lon, double *lat, double *alt) first ("
Expand Down Expand Up @@ -535,7 +535,7 @@ void UnstructuredData::set_points(int count, const std::vector<PointLonLat> &poi
n_face = count;
points_lonlat.reserve(count);
for (int i=0; i<count; i++) {
points_lonlat.emplace_back({pointsLL[i].x(), pointsLL[i].y()});
points_lonlat.push_back({pointsLL[i].x(), pointsLL[i].y()});
}
if(mlog.verbosity_level() >= UGRID_DEBUG_LEVEL) mlog
<< Debug(UGRID_DEBUG_LEVEL) << method_name << " first: ("
Expand Down Expand Up @@ -565,7 +565,7 @@ void UnstructuredData::set_points(int count, const std::vector<PointXYZ> &points
n_face = count;
points_XYZ.reserve(count);
for (int i=0; i<count; i++) {
points_XYZ.emplace_back({pointsXYZ[i].x(), pointsXYZ[i].y(), pointsXYZ[i].z()});
points_XYZ.push_back({pointsXYZ[i].x(), pointsXYZ[i].y(), pointsXYZ[i].z()});
}
if(mlog.verbosity_level() >= UGRID_DEBUG_LEVEL) {
int last_i = count - 1;
Expand Down
6 changes: 3 additions & 3 deletions src/libcode/vx_summary/summary_obs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ bool SummaryObs::summarizeObs(const TimeSummaryInfo &summary_info)
int summaryCount = 0;
int summaryKeyCount = 0;
// Save the summary information
const char *var_name = 0;
const char *var_units = 0;
const char *var_desc = 0;
const char *var_name = nullptr;
const char *var_units = nullptr;
const char *var_desc = nullptr;

//_dataSummarized = true;
TimeSummaryInfo inputSummaryInfo = summary_info;
Expand Down
12 changes: 6 additions & 6 deletions src/libcode/vx_summary/summary_obs.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class SummaryObs
time_t getValidTime(const std::string &time_string) const;
TimeSummaryInfo getSummaryInfo();
void setSummaryInfo(const TimeSummaryInfo &summary_info);
StringArray getObsNames();
StringArray getObsUnits();
StringArray getObsDescs();
StringArray getObsNames() const;
StringArray getObsUnits() const;
StringArray getObsDescs() const;

protected:

Expand Down Expand Up @@ -258,9 +258,9 @@ class SummaryObs

inline std::vector< Observation > SummaryObs::getObservations() { return observations; }
inline std::vector< Observation > SummaryObs::getSummaries() { return summaries; }
inline StringArray SummaryObs::getObsNames() { return obs_names; }
inline StringArray SummaryObs::getObsUnits() { return obs_units; }
inline StringArray SummaryObs::getObsDescs() { return obs_descs; }
inline StringArray SummaryObs::getObsNames() const { return obs_names; }
inline StringArray SummaryObs::getObsUnits() const { return obs_units; }
inline StringArray SummaryObs::getObsDescs() const { return obs_descs; }
inline void SummaryObs::setSummaryInfo(const TimeSummaryInfo &summary_info)
{ summaryInfo = summary_info; }
inline TimeSummaryInfo SummaryObs::getSummaryInfo() { return summaryInfo; }
Expand Down
46 changes: 21 additions & 25 deletions src/tools/other/ascii2nc/uscrn_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ using namespace std;
// - Contains 15 columns defined by:
// https://www.ncei.noaa.gov/pub/data/uscrn/products/monthly01/readme.txt
//
USCRNFormatInfo uscrn_monthly_info = {
static const USCRNFormatInfo uscrn_monthly_info = {
"USCRN-Monthly", "CRNM0102", ".txt", 15, 0, 1, -1, 3, 4,
{
{ 5, "T_MONTHLY_MAX", "Celsius",
Expand Down Expand Up @@ -62,7 +62,7 @@ USCRNFormatInfo uscrn_monthly_info = {
// - Contains 28 columns defined by:
// https://www.ncei.noaa.gov/pub/data/uscrn/products/daily01/readme.txt
//
USCRNFormatInfo uscrn_daily_info = {
static const USCRNFormatInfo uscrn_daily_info = {
"USCRN-Daily", "CRND0103", ".txt", 28, 0, 1, -1, 3, 4,
{
{ 5, "T_DAILY_MAX", "Celsius",
Expand Down Expand Up @@ -118,7 +118,7 @@ USCRNFormatInfo uscrn_daily_info = {
// - Contains 38 columns defined by:
// https://www.ncei.noaa.gov/pub/data/uscrn/products/hourly02/readme.txt
//
USCRNFormatInfo uscrn_hourly_info = {
static const USCRNFormatInfo uscrn_hourly_info = {
"USCRN-Hourly", "CRNH0203", ".txt", 38, 0, 1, 2, 6, 7,
{
{ 8, "T_CALC", "Celsius",
Expand Down Expand Up @@ -174,7 +174,7 @@ USCRNFormatInfo uscrn_hourly_info = {
// - Contains 23 columns defined by:
// https://www.ncei.noaa.gov/pub/data/uscrn/products/subhourly01/readme.txt
//
USCRNFormatInfo uscrn_subhourly_info = {
static const USCRNFormatInfo uscrn_subhourly_info = {
"USCRN-SubHourly", "CRNS0101", ".txt", 23, 0, 1, 2, 6, 7,
{
{ 8, "AIR_TEMPERATURE", "Celsius",
Expand Down Expand Up @@ -206,7 +206,7 @@ USCRNFormatInfo uscrn_subhourly_info = {
// Note that "soil/soilclim01" files named "CRNSMC0101-{Location}.csv"
// are not supported directly here.
//
USCRNFormatInfo uscrn_soilanom_info = {
static const USCRNFormatInfo uscrn_soilanom_info = {
"USCRN-SoilAnom", "CRNSSM0101", ".csv", 30, 0, 1, -1, 2, 3,
{
{ 4, "SMVWC_5_CM", "m^3/m^3",
Expand Down Expand Up @@ -256,7 +256,7 @@ USCRNFormatInfo uscrn_soilanom_info = {
// - Contains 16 NAMED columns described by:
// https://www.ncei.noaa.gov/pub/data/uscrn/products/heat01/readme.txt
//
USCRNFormatInfo uscrn_heat_info = {
static const USCRNFormatInfo uscrn_heat_info = {
"USCRN-Heat", "CRNHE0101", ".csv", 16, 0, 1, -1, 2, 3,
{
{ 4, "RELATIVE_HUMIDITY", "%",
Expand Down Expand Up @@ -292,7 +292,7 @@ USCRNFormatInfo uscrn_heat_info = {
// - Contains 32 NAMED columns described by:
// https://www.ncei.noaa.gov/pub/data/uscrn/products/drought01/readme.txt
//
USCRNFormatInfo uscrn_drought_info = {
static const USCRNFormatInfo uscrn_drought_info = {
"USCRN-Drought", "CRNDI0101", ".csv", 32, 0, 1, -1, 2, 3,
{
{ 4, "SMVWC_5_CM_MEAN", "m^3/m^3",
Expand Down Expand Up @@ -329,7 +329,7 @@ USCRNFormatInfo uscrn_drought_info = {
//
// Mapping of USCRN format variants to metadata.
//
std::map<USCRNFormat,USCRNFormatInfo> USCRNFormatMap = {
static const std::map<USCRNFormat,USCRNFormatInfo> USCRNFormatMap = {
{ USCRNFormat::Monthly, uscrn_monthly_info },
{ USCRNFormat::Daily, uscrn_daily_info },
{ USCRNFormat::Hourly, uscrn_hourly_info },
Expand All @@ -356,10 +356,6 @@ UscrnHandler::UscrnHandler(const string &program_name) :

////////////////////////////////////////////////////////////////////////

UscrnHandler::~UscrnHandler() { }

////////////////////////////////////////////////////////////////////////

bool UscrnHandler::isFileType(LineDataFile &ascii_file) const {
return _getFileFormat(ascii_file) != USCRNFormat::None;
}
Expand All @@ -374,7 +370,7 @@ USCRNFormat UscrnHandler::_getFileFormat(const LineDataFile &ascii_file) const {
// USCRN files are identified by their prefix and suffix.

// Loop over supported USCRN formats
for(auto &x : USCRNFormatMap) {
for(const auto &x : USCRNFormatMap) {

// Check for expected prefix and suffix
if(check_prefix_suffix(ascii_file.short_filename(),
Expand Down Expand Up @@ -422,30 +418,30 @@ bool UscrnHandler::_readObservations(LineDataFile &ascii_file) {
while(ascii_file >> dl) {

// Check the expected number of columns
if(dl.n_items() != USCRNFormatMap[_format]._nCols) {
if(dl.n_items() != USCRNFormatMap.at(_format)._nCols) {
mlog << Error << "\nUscrnHandler::_readObservations() -> "
<< "unexpected number of columns ("
<< dl.n_items() << " != " << USCRNFormatMap[_format]._nCols
<< dl.n_items() << " != " << USCRNFormatMap.at(_format)._nCols
<< ") on line number " << dl.line_number()
<< " of USCRN file \"" << ascii_file.filename()
<< "\"!\n\n";
return false;
}

// Store the header information
_stationId = dl[USCRNFormatMap[_format]._sidOffset];
_stationLon = atof(dl[USCRNFormatMap[_format]._lonOffset]);
_stationLat = atof(dl[USCRNFormatMap[_format]._latOffset]);
_stationId = dl[USCRNFormatMap.at(_format)._sidOffset];
_stationLon = atof(dl[USCRNFormatMap.at(_format)._lonOffset]);
_stationLat = atof(dl[USCRNFormatMap.at(_format)._latOffset]);

// Skip header lines where station ID begins with "WBAN"
if(_stationId.substr(0,4) == "WBAN") continue;

// Extract the valid time from the data line
time_t valid_time = _getValidTime(dl);
time_t valid_time = _getUscrnValidTime(dl);
if(valid_time == 0) return false;

// Process all observations from the line
for(auto &col : USCRNFormatMap[_format]._obsInfo) {
for(const auto &col : USCRNFormatMap.at(_format)._obsInfo) {

// Get the observation
string obs_str(dl[col._offset]);
Expand All @@ -463,7 +459,7 @@ bool UscrnHandler::_readObservations(LineDataFile &ascii_file) {

// Store the observation
_addObservations(Observation(
USCRNFormatMap[_format]._formatName,
USCRNFormatMap.at(_format)._formatName,
_stationId, valid_time,
_stationLat, _stationLon, bad_data_double,
qc_str, -1, bad_data_double,
Expand All @@ -477,16 +473,16 @@ bool UscrnHandler::_readObservations(LineDataFile &ascii_file) {

////////////////////////////////////////////////////////////////////////

time_t UscrnHandler::_getValidTime(const DataLine &dl) const {
time_t UscrnHandler::_getUscrnValidTime(const DataLine &dl) const {
struct tm time_struct;
memset(&time_struct, 0, sizeof(time_struct));

// Store date string (required)
string date_str(dl[USCRNFormatMap[_format]._ymdOffset]);
string date_str(dl[USCRNFormatMap.at(_format)._ymdOffset]);

// Store time string (optional)
string time_str;
int time_offset = USCRNFormatMap[_format]._hmOffset;
int time_offset = USCRNFormatMap.at(_format)._hmOffset;
if(time_offset > 0) time_str = dl[time_offset];
else time_str = "0000";

Expand All @@ -505,7 +501,7 @@ time_t UscrnHandler::_getValidTime(const DataLine &dl) const {
date_str = date_str.substr(0, 8);
}
else {
mlog << Error << "\nUscrnHandler::_getValidTime() -> "
mlog << Error << "\nUscrnHandler::_getUscrnValidTime() -> "
<< "unexpected date format (" << date_str
<< ") on line number " << dl.line_number() << "!\n\n";
exit(1);
Expand Down
Loading

0 comments on commit 8a9b02a

Please sign in to comment.