Skip to content

Commit

Permalink
Feature 2673 sonarqube beta6 vector dictionary (#2994)
Browse files Browse the repository at this point in the history
* #2673 SonarQube findings: use vector for the dynamic memory allocation

* #2673 Check if a vector is allocated

* #2673 Check if a vector is allocated

* #2673 SonarQube findings use vector

* #2673 Resolved SonarQube findingds

* #2673 SonarQube: Replaced f-strings to regular strings

* #2673 Added staticmethod  to read_mpr

* #2673 SonarQube: removed unnecessary pass

* #2673 SonarQube: use vector

* #2673 Initialize vecvgtor variables

* 673 Check if an user python command is empty

* #2673 Changed 0 to nullptr

* #2673 Rollback for s and av

* #2673 Applied the static method call

* #2673 Rollback using vector because of introducing a bug

* 673 Cleanup commented out code

---------

Co-authored-by: Howard Soh <hsoh@seneca.rap.ucar.edu>
  • Loading branch information
hsoh-u and Howard Soh authored Oct 16, 2024
1 parent e8a2cf4 commit 48f394f
Show file tree
Hide file tree
Showing 18 changed files with 239 additions and 281 deletions.
1 change: 0 additions & 1 deletion scripts/python/examples/read_ascii_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def set_dataplane_attrs():
user_fill_value = float(sys.argv[3])
except:
log(f"{SCRIPT_NAME} Ignored argument {sys.argv[3]}")
pass

log(f"{SCRIPT_NAME} Input File:\t{repr(input_file)}")
log(f"{SCRIPT_NAME} Data Name:\t{repr(data_name)}")
Expand Down
3 changes: 3 additions & 0 deletions scripts/python/met/dataplane.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def call_python(argv):
sys.exit(1)

met_base.log_message(f"User python command:\t{repr(' '.join(argv[1:]))}")
if not argv[1] or not argv[1].strip():
met_base.quit_msg(f"User python command is empty")
sys.exit(1)

# argv[1] contains the user defined python script
pyembed_module_name = argv[1]
Expand Down
33 changes: 17 additions & 16 deletions scripts/python/met/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ def error_message(msg):
for a_msg in msgs:
logger.log_message(logger.append_error_prompt(a_msg))

#@staticmethod
#def get_met_fill_value():
# return logger.MET_FILL_VALUE

@staticmethod
def info_message(msg):
print(f'{logger.PROMPT} {logger.INFO_P} {msg}')
Expand Down Expand Up @@ -78,8 +74,8 @@ def log_msg(self, msg):

@staticmethod
def get_numpy_filename(tmp_filename):
return logger.replace_extension(tmp_filename, "json", "npy") if tmp_filename.endswith(".json") else \
logger.replace_extension(tmp_filename, "nc", "npy") if tmp_filename.endswith(".nc") else f'{tmp_filename}.npy'
file_ext = os.path.splitext(tmp_filename)[1]
return logger.replace_extension(tmp_filename, file_ext, ".npy") if file_ext else f'{tmp_filename}.npy'

def is_debug_enabled(self, component_name=""):
return met_base_tools.is_debug_enabled(component_name)
Expand All @@ -99,22 +95,27 @@ class met_base_tools(object):
ENV_MET_PYTHON_DEBUG = "MET_PYTHON_DEBUG"
ENV_MET_PYTHON_TMP_FORMAT = "MET_PYTHON_TMP_FORMAT"

@staticmethod
def convert_byte_type_to_array(ndarray_data):
array_data = []
if isinstance(ndarray_data[0], (np.ma.MaskedArray, np.ma.core.MaskedArray)):
for byte_data in ndarray_data:
array_data.append(byte_data.tobytes(fill_value=' ').decode('utf-8').rstrip())
else:
for byte_data in ndarray_data:
array_data.append(byte_data.decode("utf-8").rstrip())
return array_data

@staticmethod
def convert_to_array(ndarray_data):
is_byte_type = False
if 0 < len(ndarray_data):
is_byte_type = isinstance(ndarray_data[0], (bytes, np.bytes_))
if isinstance(ndarray_data[0], np.ndarray):
if 0 < len(ndarray_data[0]):
is_byte_type = isinstance(ndarray_data[0][0], (bytes, np.bytes_))
if not is_byte_type and isinstance(ndarray_data[0], np.ndarray) \
and 0 < len(ndarray_data[0]):
is_byte_type = isinstance(ndarray_data[0][0], (bytes, np.bytes_))
if is_byte_type:
array_data = []
if isinstance(ndarray_data[0], (np.ma.MaskedArray, np.ma.core.MaskedArray)):
for byte_data in ndarray_data:
array_data.append(byte_data.tobytes(fill_value=' ').decode('utf-8').rstrip())
else:
for byte_data in ndarray_data:
array_data.append(byte_data.decode("utf-8").rstrip())
array_data = met_base_tools.convert_byte_type_to_array(ndarray_data)
elif isinstance(ndarray_data, (np.ma.MaskedArray, np.ma.core.MaskedArray)):
array_data = ndarray_data.filled(fill_value=-9999).tolist()
elif isinstance(ndarray_data, np.ndarray):
Expand Down
1 change: 1 addition & 0 deletions scripts/python/met/mprbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class mpr_data():

# Read a text file with N columns and returns the list of N column data
# Skip first "col_start" columns if col_start is not 0.
@staticmethod
def read_mpr(input_file, col_last, col_start = 0, header=None,
delim_whitespace=True, keep_default_na=False,
skiprows=1, dtype='string'):
Expand Down
73 changes: 35 additions & 38 deletions scripts/python/met/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,6 @@ def check_point_data(self):
if self.use_var_id:
self.check_data_member_string(self.obs_var_table,'obs_var_table')

#def convert_to_numpy(self, value_list):
# return met_point_tools.convert_to_ndarray(value_list)

def dump(self):
met_point_tools.print_point_data(self.get_point_data())

Expand Down Expand Up @@ -346,7 +343,7 @@ def write_point_data(self, tmp_filename):

nc_point_obs.write_nc_file(tmp_filename, self)
if met_base_tools.is_debug_enabled("point"):
met_base.log_message(f"Save to a temporary NetCDF file (point)")
met_base.log_message("Save to a temporary NetCDF file (point)")
else:
self.write_point_data_json_numpy(tmp_filename)

Expand Down Expand Up @@ -417,8 +414,8 @@ def __init__(self, point_data):
def check_csv_record(self, csv_point_data, index):
method_name = f"{self.__class__.__name__}.check_csv_record()"
error_msgs = []
# names=['typ', 'sid', 'vld', 'lat', 'lon', 'elv', 'var', 'lvl', 'hgt', 'qc', 'obs']
# dtype={'typ':'str', 'sid':'str', 'vld':'str', 'var':'str', 'qc':'str'}
# names: ['typ', 'sid', 'vld', 'lat', 'lon', 'elv', 'var', 'lvl', 'hgt', 'qc', 'obs']
# dtype: {'typ':'str', 'sid':'str', 'vld':'str', 'var':'str', 'qc':'str'}
if 11 > len(csv_point_data):
error_msgs.append(f"{method_name} {index}-th data: missing columns. should be 11 columns, not {len(csv_point_data)} columns")
elif 11 < len(csv_point_data):
Expand Down Expand Up @@ -488,7 +485,7 @@ def convert_point_data(self):
self.use_var_id = not self.is_grib_code()

index = 0
#names=['typ', 'sid', 'vld', 'lat', 'lon', 'elv', 'var', 'lvl', 'hgt', 'qc', 'obs']
#name: ['typ', 'sid', 'vld', 'lat', 'lon', 'elv', 'var', 'lvl', 'hgt', 'qc', 'obs']
for csv_point_record in self.point_data:
# Build header map.
hdr_typ_str = csv_point_record[0]
Expand Down Expand Up @@ -539,7 +536,7 @@ def convert_point_data(self):
obs_qty_map[qc_str] = qc_id
qc_cnt += 1

# names=['typ', 'sid', 'vld', 'lat', 'lon', 'elv', 'var', 'lvl', 'hgt', 'qc', 'obs']
# names: ['typ', 'sid', 'vld', 'lat', 'lon', 'elv', 'var', 'lvl', 'hgt', 'qc', 'obs']
self.obs_vid[index] = var_id
self.obs_hid[index] = hdr_idx
self.obs_lvl[index] = self.get_num_value(csv_point_record[7])
Expand Down Expand Up @@ -627,21 +624,21 @@ def read_data(self, args):
# - set self.input_name
#
# Here is a template
'''
if isinstance(args, dict):
in_filename = args.get('in_name',None)
elif isinstance(args, list):
in_filename = args[0]
else:
in_filename = args
self.input_name = in_filename
'''
#
# if isinstance(args, dict):
# in_filename = args.get('in_name',None)
# elif isinstance(args, list):
# in_filename = args[0]
# else:
# in_filename = args
# self.input_name = in_filename
pass


class dummy_point_obs(met_point_obs):

def read_data(self, args):
# Do nothing to return an empty point_obs
pass


Expand Down Expand Up @@ -689,7 +686,7 @@ def print_data(key, data_array, show_count=COUNT_SHOW):

@staticmethod
def print_point_data(met_point_data, print_subset=True):
method_name = f"met_point_tools.print_point_data()"
method_name = "met_point_tools.print_point_data()"
print(' === MET point data by python embedding ===')
if print_subset:
met_point_tools.print_data('nhdr',met_point_data['nhdr'])
Expand All @@ -714,26 +711,26 @@ def print_point_data(met_point_data, print_subset=True):
met_point_tools.print_data('obs_val',met_point_data['obs_val'])
else:
print(f'{method_name} All',met_point_data)
print(f" nhdr: met_point_data['nhdr']")
print(f" nobs: met_point_data['nobs']")
print(f" use_var_id: met_point_data['use_var_id']")
print(f" hdr_typ: met_point_data['hdr_typ']")
print(f"hdr_typ_table: met_point_data['hdr_typ_table']")
print(f" hdr_sid: met_point_data['hdr_sid']")
print(f"hdr_sid_table: met_point_data['hdr_sid_table']")
print(f" hdr_vld: met_point_data['hdr_vld']")
print(f"hdr_vld_table: met_point_data['hdr_vld_table']")
print(f" hdr_lat: met_point_data['hdr_lat']")
print(f" hdr_lon: met_point_data['hdr_lon']")
print(f" hdr_elv: met_point_data['hdr_elv']")
print(f" obs_hid: met_point_data['obs_hid']")
print(f" obs_vid: met_point_data['obs_vid']")
print(f"obs_var_table: met_point_data['obs_var_table']")
print(f" obs_qty: met_point_data['obs_qty']")
print(f"obs_qty_table: met_point_data['obs_qty_table']")
print(f" obs_lvl: met_point_data['obs_lvl']")
print(f" obs_hgt: met_point_data['obs_hgt']")
print(f" obs_val: met_point_data['obs_val']")
print(" nhdr: met_point_data['nhdr']")
print(" nobs: met_point_data['nobs']")
print(" use_var_id: met_point_data['use_var_id']")
print(" hdr_typ: met_point_data['hdr_typ']")
print("hdr_typ_table: met_point_data['hdr_typ_table']")
print(" hdr_sid: met_point_data['hdr_sid']")
print("hdr_sid_table: met_point_data['hdr_sid_table']")
print(" hdr_vld: met_point_data['hdr_vld']")
print("hdr_vld_table: met_point_data['hdr_vld_table']")
print(" hdr_lat: met_point_data['hdr_lat']")
print(" hdr_lon: met_point_data['hdr_lon']")
print(" hdr_elv: met_point_data['hdr_elv']")
print(" obs_hid: met_point_data['obs_hid']")
print(" obs_vid: met_point_data['obs_vid']")
print("obs_var_table: met_point_data['obs_var_table']")
print(" obs_qty: met_point_data['obs_qty']")
print("obs_qty_table: met_point_data['obs_qty_table']")
print(" obs_lvl: met_point_data['obs_lvl']")
print(" obs_hgt: met_point_data['obs_hgt']")
print(" obs_val: met_point_data['obs_val']")

print(' === MET point data by python embedding ===')

Expand Down
12 changes: 4 additions & 8 deletions src/basic/vx_math/hist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Histogram::~Histogram()

{

if ( Count ) { delete [] Count; Count = (int *) nullptr; }
Count.clear();

}

Expand Down Expand Up @@ -93,7 +93,7 @@ void Histogram::init_from_scratch()

{

Count = (int *) nullptr;
Count.clear();

Nbins = 0;

Expand All @@ -120,9 +120,7 @@ void Histogram::clear()

{

int j;

for (j=0; j<Nbins; ++j) Count[j] = 0;
//for (int j=0; j<Nbins; ++j) Count[j] = 0;

is_empty = 1;

Expand All @@ -142,15 +140,13 @@ void Histogram::set_nbd(int n, double b, double d)

{

if ( Count ) { delete [] Count; Count = (int *) nullptr; }

Nbins = n;

Bottom = b;
Delta = d;


Count = new int [Nbins];
Count.resize(Nbins, 0);

clear();

Expand Down
3 changes: 2 additions & 1 deletion src/basic/vx_math/hist.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
////////////////////////////////////////////////////////////////////////


#include <vector>
#include <iostream>


Expand All @@ -28,7 +29,7 @@ class Histogram {

private:

int * Count;
std::vector<int> Count;

int Nbins;

Expand Down
12 changes: 6 additions & 6 deletions src/basic/vx_math/legendre.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ void Legendre::init_from_scratch()

{

P = 0;
P.clear();

PP = 0;
PP.clear();

clear();

Expand All @@ -135,9 +135,9 @@ void Legendre::clear()

{

if ( P ) { delete [] P; P = nullptr; }
P.clear();

if ( PP ) { delete [] PP; PP = nullptr; }
PP.clear();

X = 0.0;

Expand Down Expand Up @@ -201,9 +201,9 @@ clear();

MaxDegree = N;

P = new double [N + 1];
P.resize(N + 1);

PP = new double [N + 1];
PP.resize(N + 1);

calc(0.0);

Expand Down
8 changes: 6 additions & 2 deletions src/basic/vx_math/legendre.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#define __VX_LEGENDRE_H__


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

#include <vector>

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


Expand All @@ -37,9 +41,9 @@ class Legendre {

double X; // last x value

double * P; // allocated
std::vector<double> P; // allocated

double * PP; // allocated
std::vector<double> PP; // allocated

public:

Expand Down
4 changes: 1 addition & 3 deletions src/basic/vx_math/ptile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,10 @@ if ( n <= 1 ) return 0;
int i, j, ties_current, ties_total, tie_rank_start = 0, tie_rank_end;
double tie_rank_mean;
RankInfo *rank_info = (RankInfo *) nullptr;
double *ordered_array = (double *) nullptr;
vector<double> ordered_array(n);
double prev_v, v;

rank_info = new RankInfo [n];
ordered_array = new double [n];

// Each RankInfo structure contains a index value from 0 to n-1 and a pointer
// to the data to be ranked
Expand Down Expand Up @@ -296,7 +295,6 @@ if(ties_current != 0) {
}

if(rank_info) { delete [] rank_info; rank_info = (RankInfo *) nullptr; }
if(ordered_array) { delete [] ordered_array; ordered_array = (double *) nullptr; }

return ties_total;

Expand Down
Loading

0 comments on commit 48f394f

Please sign in to comment.