Skip to content

Commit

Permalink
Per #2880, many changes to the vx_nc_obs library and point2grid appli…
Browse files Browse the repository at this point in the history
…cation to replace dynamically allocated memory with STL vectors to satisfy SonarQube code smells.
  • Loading branch information
JohnHalleyGotway committed Oct 1, 2024
1 parent b082e7d commit 72d9653
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 230 deletions.
117 changes: 34 additions & 83 deletions src/libcode/vx_nc_obs/met_point_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ void MetPointData::init_from_scratch() {
use_arr_vars = false;
}


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

void MetPointData::clear() {
Expand Down Expand Up @@ -127,7 +126,6 @@ bool MetPointData::get_lons(float *hdr_lons) {
return true;
}


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

bool MetPointData::is_same_obs_values(const float obs_arr1[OBS_ARRAY_LEN],
Expand All @@ -150,17 +148,10 @@ void MetPointData::set_obs_cnt(int obs_cnt) {
obs_data->obs_cnt = obs_cnt;
}




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


//
// Code for class MetPointDataPython
//


//
// Code for class MetPointDataPython
//
////////////////////////////////////////////////////////////////////////

MetPointDataPython::MetPointDataPython() {
Expand All @@ -178,76 +169,58 @@ MetPointDataPython::MetPointDataPython(MetPointDataPython &d) {
header_data.assign(*d.get_header_data());
}


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

MetPointDataPython::~MetPointDataPython() {
clear();
}


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

void MetPointDataPython::allocate(int obs_cnt) {
set_obs_cnt(obs_cnt);
obs_data->allocate();
}

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



///////////////////////////////////////////////////////////////////////////////
// struct MetPointObsData

MetPointObsData::MetPointObsData():
obs_cnt(0),
obs_ids((int *)0),
obs_hids((int *)0),
obs_qids((int *)0),
obs_lvls((float *)0),
obs_hgts((float *)0),
obs_vals((float *)0),
obs_arr((float *)0),
is_obs_array(false)
{
}
//
// Code for struct MetPointObsData
//
///////////////////////////////////////////////////////////////////////////////

MetPointObsData::MetPointObsData() {
clear();
}

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

void MetPointObsData::allocate() {
if (is_obs_array) obs_arr = new float[obs_cnt*OBS_ARRAY_LEN]; // nobs * 5
if (is_obs_array) {
obs_arr.resize(obs_cnt*OBS_ARRAY_LEN, bad_data_float); // nobs * 5
}
else {
obs_ids = new int[obs_cnt]; // grib_code or var_id
obs_hids = new int[obs_cnt];
obs_qids = new int[obs_cnt];
obs_lvls = new float[obs_cnt];
obs_hgts = new float[obs_cnt];
obs_vals = new float[obs_cnt];
obs_ids.resize(obs_cnt, bad_data_int); // grib_code or var_id
obs_hids.resize(obs_cnt, bad_data_int);
obs_qids.resize(obs_cnt, bad_data_int);
obs_lvls.resize(obs_cnt, bad_data_float);
obs_hgts.resize(obs_cnt, bad_data_float);
obs_vals.resize(obs_cnt, bad_data_float);
}
}

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

void MetPointObsData::assign(MetPointObsData &o) {
clear();
obs_cnt = o.obs_cnt;
is_obs_array = o.is_obs_array;

clear();
allocate();
if (is_obs_array)
for (int idx=0; idx<obs_cnt*OBS_ARRAY_LEN; idx++) obs_arr[idx] = o.obs_arr[idx];
else {
for (int idx=0; idx<obs_cnt; idx++) {
obs_ids[idx] = o.obs_ids[idx];
obs_hids[idx] = o.obs_hids[idx];
obs_qids[idx] = o.obs_qids[idx];
obs_lvls[idx] = o.obs_lvls[idx];
obs_hgts[idx] = o.obs_hgts[idx];
obs_vals[idx] = o.obs_vals[idx];
}
}
obs_arr = o.obs_arr;
obs_ids = o.obs_ids;
obs_hids = o.obs_hids;
obs_qids = o.obs_qids;
obs_lvls = o.obs_lvls;
obs_hgts = o.obs_hgts;
obs_vals = o.obs_vals;
var_names = o.var_names;
qty_names = o.qty_names;
}
Expand All @@ -257,42 +230,20 @@ void MetPointObsData::assign(MetPointObsData &o) {
void MetPointObsData::clear() {
obs_cnt = 0;
is_obs_array = false;

clear_numbers();
clear_strings();
}

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

void MetPointObsData::clear_numbers() {
if (0 != obs_ids) {
delete [] obs_ids;
obs_ids = (int *) nullptr;
}
if (0 != obs_hids) {
delete [] obs_hids;
obs_hids = (int *) nullptr;
}
if (0 != obs_qids) {
delete [] obs_qids;
obs_qids = (int *) nullptr;
}
if (0 != obs_lvls) {
delete [] obs_lvls;
obs_lvls = (float *) nullptr;
}
if (0 != obs_hgts) {
delete [] obs_hgts;
obs_hgts = (float *) nullptr;
}
if (0 != obs_vals) {
delete [] obs_vals;
obs_vals = (float *) nullptr;
}
if (0 != obs_arr) {
delete [] obs_arr;
obs_arr = (float *) nullptr;
}
obs_arr.clear();
obs_ids.clear();
obs_hids.clear();
obs_qids.clear();
obs_lvls.clear();
obs_hgts.clear();
obs_vals.clear();
}

///////////////////////////////////////////////////////////////////////////////
Expand Down
15 changes: 8 additions & 7 deletions src/libcode/vx_nc_obs/met_point_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


#include <ostream>
#include <vector>

#include "nc_utils.h"

Expand Down Expand Up @@ -61,13 +62,13 @@ struct MetPointObsData {
int obs_cnt;
bool is_obs_array;

int *obs_ids; // grib_code or var_id
int *obs_hids;
int *obs_qids;
float *obs_lvls;
float *obs_hgts;
float *obs_vals;
float *obs_arr; // nobs * 5
std::vector<int> obs_ids; // grib_code or var_id
std::vector<int> obs_hids;
std::vector<int> obs_qids;
std::vector<float> obs_lvls;
std::vector<float> obs_hgts;
std::vector<float> obs_vals;
std::vector<float> obs_arr; // nobs * 5
StringArray var_names;
StringArray qty_names;

Expand Down
32 changes: 14 additions & 18 deletions src/libcode/vx_nc_obs/nc_obs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,15 @@ bool NcPointObsData::read_obs_data_numbers(NetcdfObsVars obs_vars, bool stop) {
clear_numbers();
obs_cnt = obs_vars.obs_cnt;

if (!IS_INVALID_NC(obs_vars.obs_arr_var)) is_obs_array = true;

// Resize arrays for input data
allocate();

StringArray missing_vars;
StringArray failed_vars;
if (!IS_INVALID_NC(obs_vars.obs_arr_var)) {
is_obs_array = true;
obs_arr = new float[obs_cnt*OBS_ARRAY_LEN];
if (!get_nc_data(&obs_vars.obs_arr_var, obs_arr)) {
if(is_obs_array) {
if (!get_nc_data(&obs_vars.obs_arr_var, obs_arr.data())) {
succeed = false;
failed_vars.add(nc_var_obs_arr);
}
Expand All @@ -103,8 +106,7 @@ bool NcPointObsData::read_obs_data_numbers(NetcdfObsVars obs_vars, bool stop) {
missing_vars.add(nc_var_obs_hid);
}
else {
obs_hids = new int[obs_cnt];
if (!get_nc_data(&obs_vars.obs_hid_var, obs_hids)) {
if (!get_nc_data(&obs_vars.obs_hid_var, obs_hids.data())) {
succeed = false;
failed_vars.add(nc_var_obs_hid);
}
Expand All @@ -114,8 +116,7 @@ bool NcPointObsData::read_obs_data_numbers(NetcdfObsVars obs_vars, bool stop) {
missing_vars.add(nc_var_obs_lvl);
}
else {
obs_lvls = new float[obs_cnt];
if (!get_nc_data(&obs_vars.obs_lvl_var, obs_lvls)) {
if (!get_nc_data(&obs_vars.obs_lvl_var, obs_lvls.data())) {
succeed = false;
failed_vars.add(nc_var_obs_lvl);
}
Expand All @@ -125,8 +126,7 @@ bool NcPointObsData::read_obs_data_numbers(NetcdfObsVars obs_vars, bool stop) {
missing_vars.add(nc_var_obs_hgt);
}
else {
obs_hgts = new float[obs_cnt];
if (!get_nc_data(&obs_vars.obs_hgt_var, obs_hgts)) {
if (!get_nc_data(&obs_vars.obs_hgt_var, obs_hgts.data())) {
succeed = false;
failed_vars.add(nc_var_obs_hgt);
}
Expand All @@ -136,22 +136,19 @@ bool NcPointObsData::read_obs_data_numbers(NetcdfObsVars obs_vars, bool stop) {
missing_vars.add(nc_var_obs_val);
}
else {
obs_vals = new float[obs_cnt];
if (!get_nc_data(&obs_vars.obs_val_var, obs_vals)) {
if (!get_nc_data(&obs_vars.obs_val_var, obs_vals.data())) {
succeed = false;
failed_vars.add(nc_var_obs_val);
}
}
if (IS_VALID_NC(obs_vars.obs_gc_var)) {
obs_ids = new int[obs_cnt];
if (!get_nc_data(&obs_vars.obs_gc_var, obs_ids)) {
if (!get_nc_data(&obs_vars.obs_gc_var, obs_ids.data())) {
succeed = false;
failed_vars.add(nc_var_obs_gc);
}
}
else if (IS_VALID_NC(obs_vars.obs_vid_var)) {
obs_ids = new int[obs_cnt];
if (!get_nc_data(&obs_vars.obs_vid_var, obs_ids)) {
if (!get_nc_data(&obs_vars.obs_vid_var, obs_ids.data())) {
succeed = false;
failed_vars.add(nc_var_obs_vid);
}
Expand All @@ -162,8 +159,7 @@ bool NcPointObsData::read_obs_data_numbers(NetcdfObsVars obs_vars, bool stop) {
missing_vars.add(nc_var_obs_qty);
}
else {
obs_qids = new int[obs_cnt];
if (!get_nc_data(&obs_vars.obs_qty_var, obs_qids)) {
if (!get_nc_data(&obs_vars.obs_qty_var, obs_qids.data())) {
succeed = false;
failed_vars.add(nc_var_obs_qty);
}
Expand Down
Loading

0 comments on commit 72d9653

Please sign in to comment.