Skip to content

Commit

Permalink
Feature 2673 sonarqube beta6 vector (#2991)
Browse files Browse the repository at this point in the history
* 673 Replaceddynamic allocatin method (using vector instead of new)

* 673 SonarQube findings: use vector for the dynamic memory allocation

* 673 SonarQube findings: use vector for the dynamic memory allocation

* #2673 Check None of met_data.shape

* #2673 Check None of met_data.shape

---------

Co-authored-by: Howard Soh <hsoh@seneca.rap.ucar.edu>
  • Loading branch information
hsoh-u and Howard Soh authored Oct 14, 2024
1 parent ba894e6 commit c5cd28d
Show file tree
Hide file tree
Showing 24 changed files with 239 additions and 443 deletions.
6 changes: 5 additions & 1 deletion scripts/python/met/dataplane.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ def validate_met_data(met_data, fill_value=None):
met_base.quit(f"{method_name} The met_data is None")
sys.exit(1)

nx, ny = met_data.shape
if hasattr(met_data, 'shape'):
nx, ny = met_data.shape
else:
met_base.quit(f"{method_name} The met_data does not have the shape property")
sys.exit(1)

met_fill_value = met_base.MET_FILL_VALUE
if dataplane.is_xarray_dataarray(met_data):
Expand Down
16 changes: 6 additions & 10 deletions src/libcode/vx_data2d_nc_cf/nc_cf_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ bool NcCfFile::open(const char * filepath)
int n_times = get_data_size(valid_time_var);
int tim_buf_size = n_times;
if (use_bounds_var) tim_buf_size *= 2;
auto time_values = new double[tim_buf_size];
vector<double> time_values(tim_buf_size);

if( get_nc_data(nc_time_var, time_values) ) {
if( get_nc_data(nc_time_var, time_values.data()) ) {
bool no_leap_year = get_att_no_leap_year(valid_time_var);
if( time_dim_count > 1 ) {
double latest_time = bad_data_double;
Expand Down Expand Up @@ -381,7 +381,6 @@ bool NcCfFile::open(const char * filepath)
}
}
else ValidTime.add(0); //Initialize
delete [] time_values;
}

NcVar init_time_var = get_var(_ncFile, "forecast_reference_time");
Expand Down Expand Up @@ -504,14 +503,13 @@ bool NcCfFile::open(const char * filepath)
if (IS_VALID_NC_P(z_var)) {

int z_count = get_data_size(z_var);
auto z_values = new double[z_count];
vector<double> z_values(z_count);

if( get_nc_data(z_var, z_values) ) {
if( get_nc_data(z_var, z_values.data()) ) {
for(int i=0; i<z_count; i++) {
vlevels.add(z_values[i]);
}
}
delete [] z_values;
}

// done
Expand Down Expand Up @@ -1102,7 +1100,7 @@ bool NcCfFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const

// get the data
const int plane_size = nx * ny;
double *d = new double[plane_size];
vector<double> d(plane_size);

size_t dim_size;
LongArray offsets;
Expand All @@ -1125,7 +1123,7 @@ bool NcCfFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const
offsets[y_slot] = 0;
lengths[y_slot] = ny;

get_nc_data(v, d, lengths, offsets);
get_nc_data(v, d.data(), lengths, offsets);

int offset = 0;
if( x_slot > y_slot ) {
Expand Down Expand Up @@ -1163,8 +1161,6 @@ bool NcCfFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const
} /* for x */
}

delete [] d;

// done
mlog << Debug(6) << method_name << "took "
<< get_exe_duration(start_clock) << " seconds\n";
Expand Down
5 changes: 2 additions & 3 deletions src/libcode/vx_data2d_nc_met/get_met_grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -699,10 +699,9 @@ void get_semilatlon_var(NcFile *ncfile, const char * var_name, NumArray &out_na)

// Store the requested data in the specified NumArray object
long count = get_data_size(&nc_var);
double * data_values = new double[ count ];
get_nc_data(&nc_var, data_values);
vector<double> data_values(count);
get_nc_data(&nc_var, data_values.data());
for(int i=0; i<count; i++) out_na.add(data_values[i]);
if(data_values) { delete [] data_values; data_values = (double *) nullptr; }

return;
}
Expand Down
15 changes: 6 additions & 9 deletions src/libcode/vx_data2d_nc_met/met_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -587,34 +587,31 @@ plane.set_size(Nx, Ny);
dim[x_slot] = Nx;
dim[y_slot] = Ny;

double *data_array = new double[cell_count];
double *double_array = new double[cell_count];
vector<double> data_array(cell_count);
vector<double> double_array(cell_count);

clock_time = clock();

get_nc_data(v, double_array, dim, cur);
copy_nc_data_as_double(data_array, double_array, x_slot, y_slot, Nx, Ny,
get_nc_data(v, double_array.data(), dim, cur);
copy_nc_data_as_double(data_array.data(), double_array.data(), x_slot, y_slot, Nx, Ny,
missing_value, fill_value);

nc_time = clock();
if (mlog.verbosity_level() >= 7) {
double duration_sec = (double)(nc_time - clock_time)/CLOCKS_PER_SEC;
check_nc_data_2d(data_array, Nx, Ny, missing_value);
check_nc_data_2d(data_array.data(), Nx, Ny, missing_value);
mlog << Debug(7) << method_name_short << "took " << duration_sec
<< " seconds to read NetCDF data\n";
}

plane.set_block(data_array, Nx, Ny);
plane.set_block(data_array.data(), Nx, Ny);

if (mlog.verbosity_level() >= 7) {
double duration_sec = (double)(clock() - nc_time)/CLOCKS_PER_SEC;
mlog << Debug(7) << method_name_short << "took " << duration_sec
<< " seconds to fill data plane\n";
}

if (data_array) delete[] data_array;
if (double_array) delete[] double_array;

//
// done
//
Expand Down
4 changes: 2 additions & 2 deletions src/libcode/vx_data2d_nc_wrf/wrf_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ plane.set_size(Nx, Ny);
//
// get the data
//
double d[Ny];
vector<double> d(Ny);

LongArray offsets;
LongArray lengths;
Expand All @@ -863,7 +863,7 @@ lengths[y_slot] = Ny;
int type_id = GET_NC_TYPE_ID_P(v);
for (x=0; x<Nx; ++x) {
offsets[x_slot] = x;
get_nc_data(v, (double *)&d, lengths, offsets);
get_nc_data(v, d.data(), lengths, offsets);

b[x_slot] = x;

Expand Down
29 changes: 11 additions & 18 deletions src/libcode/vx_data2d_ugrid/ugrid_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ bool UGridFile::open_metadata(const char * filepath)
int n_times = IS_VALID_NC_P(_tDim) ? get_dim_size(_tDim)
: (int) get_data_size(valid_time_var);
int tim_buf_size = n_times;
double *time_values = new double[tim_buf_size];
vector<double> time_values(tim_buf_size);
if(2 == time_dim_count) {
for(int i=0; i<n_times; i++) {
time_values[i] = get_nc_time(valid_time_var, i);
Expand All @@ -362,7 +362,7 @@ bool UGridFile::open_metadata(const char * filepath)
<< GET_NC_NAME_P(valid_time_var) << "\n";
}
}
else if( get_nc_data(valid_time_var, time_values) ) {
else if( get_nc_data(valid_time_var, time_values.data()) ) {
bool no_leap_year = get_att_no_leap_year(valid_time_var);
if( time_dim_count > 1 ) {
double latest_time = bad_data_double;
Expand Down Expand Up @@ -396,7 +396,6 @@ bool UGridFile::open_metadata(const char * filepath)
}
}
else ValidTime.add(0); //Initialize
delete [] time_values;
}

// Pull out the grid. This must be done after pulling out the dimension
Expand Down Expand Up @@ -440,14 +439,13 @@ bool UGridFile::open_metadata(const char * filepath)
if (IS_VALID_NC_P(z_var)) {

int z_count = (int) get_data_size(z_var);
double *z_values = new double[z_count];
vector<double> z_values(z_count);

if( get_nc_data(z_var, z_values) ) {
if( get_nc_data(z_var, z_values.data()) ) {
for(int i=0; i<z_count; i++) {
vlevels.add(z_values[i]);
}
}
delete [] z_values;
}

// done
Expand Down Expand Up @@ -699,7 +697,7 @@ bool UGridFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const

// get the data
const int plane_size = nx * ny;
double *d = new double[plane_size];
vector<double> d(plane_size);

int length;
size_t dim_size;
Expand All @@ -726,7 +724,7 @@ bool UGridFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const
}
}

get_nc_data(v, d, lengths, offsets);
get_nc_data(v, d.data(), lengths, offsets);

double min_value = 10e10;
double max_value = -min_value;
Expand All @@ -744,8 +742,6 @@ bool UGridFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const

} // for x

if (nullptr != d) delete [] d;

// done
ConcatString log_message;
for (int idx=0; idx<a.n_elements(); idx++) {
Expand Down Expand Up @@ -906,14 +902,14 @@ void UGridFile::read_netcdf_grid()
ConcatString units_value;
const char *method_name = "UGridFile::read_netcdf_grid() -> ";

double *_lat = new double[face_count];
double *_lon = new double[face_count];
vector<double> _lat(face_count);
vector<double> _lon(face_count);

if (IS_INVALID_NC_P(_latVar)) {
mlog << Error << "\n" << method_name << "latitude variable is missing\n\n";
exit(1);
}
else if (!get_nc_data(_latVar,_lat)) {
else if (!get_nc_data(_latVar,_lat.data())) {
mlog << Error << "\n" << method_name << "fail to read latitude values\n\n";
exit(1);
}
Expand All @@ -922,7 +918,7 @@ void UGridFile::read_netcdf_grid()
mlog << Error << "\n" << method_name << "longitude variable is missing\n\n";
exit(1);
}
else if (!get_nc_data(_lonVar,_lon)) {
else if (!get_nc_data(_lonVar,_lon.data())) {
mlog << Error << "\n" << method_name << "fail to read latitude values\n\n";
exit(1);
}
Expand All @@ -943,17 +939,14 @@ void UGridFile::read_netcdf_grid()
// Convert longitude from degrees east to west
for (int idx=0; idx<face_count; idx++) _lon[idx] = -1.0*rescale_deg(_lon[idx], -180, 180);

grid_data.set_points(face_count, _lon, _lat);
grid_data.set_points(face_count, _lon.data(), _lat.data());
grid_data.max_distance_km = max_distance_km;

grid.set(grid_data);

// Pull the grid projection from the variable information. First, look for
// a grid_mapping attribute.

if (_lat) delete [] _lat;
if (_lon) delete [] _lon;

}


Expand Down
Loading

0 comments on commit c5cd28d

Please sign in to comment.