diff --git a/internal/test_unit/xml/unit_python.xml b/internal/test_unit/xml/unit_python.xml index f6e011dc0d..af782db022 100644 --- a/internal/test_unit/xml/unit_python.xml +++ b/internal/test_unit/xml/unit_python.xml @@ -115,6 +115,25 @@ + + + &MET_BIN;/plot_data_plane + + MET_PYTHON_EXE &MET_PYTHON_EXE; + + \ + PYTHON_NUMPY \ + &OUTPUT_DIR;/python/letter_numpy_0_to_missing.ps \ + 'name = "&MET_BASE;/python/examples/read_ascii_numpy.py &DATA_DIR_PYTHON;/letter.txt LETTER 0.0";' \ + -plot_range 0.0 255.0 \ + -title "Python enabled numpy plot_data_plane" \ + -v 1 + + + &OUTPUT_DIR;/python/letter_numpy_0_to_missing.ps + + + &MET_BIN;/plot_data_plane \ diff --git a/scripts/python/examples/read_ascii_numpy.py b/scripts/python/examples/read_ascii_numpy.py index 5692472b10..342121f2ff 100644 --- a/scripts/python/examples/read_ascii_numpy.py +++ b/scripts/python/examples/read_ascii_numpy.py @@ -1,5 +1,6 @@ import os import sys +import numpy from met.dataplane import dataplane ########################################### @@ -67,33 +68,47 @@ def set_dataplane_attrs(): ## load the data into the numpy array ## -if len(sys.argv) != 3: - dataplane.quit("read_ascii_numpy.py -> Must specify exactly one input file and a name for the data.") +SCRIPT_NAME = "read_ascii_numpy.py ->" +if len(sys.argv) < 3: + dataplane.quit(f"{SCRIPT_NAME} Must specify exactly one input file and a name for the data.") +elif len(sys.argv) > 4: + dataplane.quit(f"{SCRIPT_NAME} Have not supported arguments [{sys.argv[4:]}]") # Read the input file as the first argument input_file = os.path.expandvars(sys.argv[1]) data_name = sys.argv[2] try: - log("Input File:\t" + repr(input_file)) - log("Data Name:\t" + repr(data_name)) + user_fill_value = None + try: + if len(sys.argv) > 3: + 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)}") if os.path.exists(input_file): # read_2d_text_input() reads n by m text data and returns 2D numpy array met_data = dataplane.read_2d_text_input(input_file) if met_data is None: - dataplane.quit(f" Fail to build met_data from {input_file}") + dataplane.quit(f"{SCRIPT_NAME} Fail to build met_data from {input_file}") else: - log("Data Shape:\t" + repr(met_data.shape)) - log("Data Type:\t" + repr(met_data.dtype)) + log(f"{SCRIPT_NAME} Data Shape:\t{repr(met_data.shape)}") + log(f"{SCRIPT_NAME} Data Type:\t{repr(met_data.dtype)}") + if user_fill_value is not None: + met_data = numpy.ma.masked_values(met_data, user_fill_value) + log(f"{SCRIPT_NAME} Python Type:\t{type(met_data)}") else: - dataplane.quit(f"input {input_file} does exist!!!") + dataplane.quit(f"{SCRIPT_NAME} input {input_file} does exist!!!") except: import traceback traceback.print_exc() - dataplane.quit(f"Unknown error with {sys.argv[0]}: ") + dataplane.quit(f"{SCRIPT_NAME} Unknown error with {sys.argv[0]}: ") attrs = set_dataplane_attrs() -log("Attributes:\t" + repr(attrs)) +log(f"{SCRIPT_NAME} Attributes:\t{repr(attrs)}") # Sets fill_value if it exists at the dataplane #attrs['fill_value'] = 255 # for letter.txt diff --git a/scripts/python/met/logger.py b/scripts/python/met/logger.py index db58286e07..a85de36d1f 100644 --- a/scripts/python/met/logger.py +++ b/scripts/python/met/logger.py @@ -18,7 +18,7 @@ def append_error_prompt(msg): return f'{logger.ERROR_P}: {msg}' @staticmethod - def error_messageg(msg): + def error_message(msg): msgs = msg if isinstance(msg, list) else [msg] msgs.insert(0, '') msgs.append('') @@ -62,7 +62,7 @@ def get_met_fill_value(self): return met_base.MET_FILL_VALUE def error_msg(self, msg): - logger.error_messageg(msg) + logger.error_message(msg) def get_prompt(self): return met_base_tools.get_prompt() @@ -116,7 +116,7 @@ def convert_to_array(ndarray_data): for byte_data in ndarray_data: array_data.append(byte_data.decode("utf-8").rstrip()) elif isinstance(ndarray_data, (np.ma.MaskedArray, np.ma.core.MaskedArray)): - array_data = np.ma.getdata(ndarray_data, subok=False).tolist() + array_data = ndarray_data.filled(fill_value=-9999).tolist() elif isinstance(ndarray_data, np.ndarray): array_data = ndarray_data.tolist() else: @@ -126,7 +126,7 @@ def convert_to_array(ndarray_data): @staticmethod def convert_to_ndarray(array_data): if isinstance(array_data, (np.ma.MaskedArray, np.ma.core.MaskedArray)): - ndarray_data = np.ma.getdata(array_data, subok=False) + ndarray_data = array_data.filled(fill_value=-9999) elif isinstance(array_data, np.ndarray): ndarray_data = array_data else: diff --git a/scripts/python/met/point.py b/scripts/python/met/point.py index 624173c2a4..eb85c3711d 100644 --- a/scripts/python/met/point.py +++ b/scripts/python/met/point.py @@ -797,7 +797,7 @@ def convert_point_data(point_data, check_all_records=False, input_type='csv'): csv_point_data.check_csv_point_data(check_all_records) tmp_point_data = csv_point_data.get_point_data() else: - met_base.error_messageg(f'convert_point_data(() Not supported input type: {input_type}') + met_base.error_message(f'convert_point_data(() Not supported input type: {input_type}') return tmp_point_data def get_empty_point_obs():