diff --git a/pvlib/iotools/__init__.py b/pvlib/iotools/__init__.py index ecc70cab2b..adb4733a68 100644 --- a/pvlib/iotools/__init__.py +++ b/pvlib/iotools/__init__.py @@ -22,4 +22,4 @@ from pvlib.iotools.sodapro import get_cams # noqa: F401 from pvlib.iotools.sodapro import read_cams # noqa: F401 from pvlib.iotools.sodapro import parse_cams # noqa: F401 -from pvlib.iotools.pvsyst import read_panond, parse_panond \ No newline at end of file +from pvlib.iotools.panond import read_panond, parse_panond diff --git a/pvlib/iotools/pvsyst.py b/pvlib/iotools/panond.py similarity index 75% rename from pvlib/iotools/pvsyst.py rename to pvlib/iotools/panond.py index bcb1e95b06..87ad2764c8 100644 --- a/pvlib/iotools/pvsyst.py +++ b/pvlib/iotools/panond.py @@ -5,27 +5,28 @@ import io def num_type(value): - # Determine if a value is float, int or leave as string + + # Determine if a value is float, int or leave as string if '.' in value: try: # Detect float value_out = float(value) return value_out - + except ValueError: # Otherwise leave as string value_out = value return value_out - + else: try: # Detect int value_out = int(value) return value_out - + except ValueError: # Otherwise leave as string value_out = value return value_out - + def element_type(element): # Determine if an element is a list then pass to num_type() @@ -35,7 +36,7 @@ def element_type(element): element_out = [] for val in values: # Determine datatype of each value element_out.append(num_type(val)) - + return element_out else: @@ -50,13 +51,13 @@ def parse_panond(fbuf): ---------- fbuf : File-like object Buffer of a .pan or .ond file - + Returns ------- comp : Nested Dictionary - Contents of the .pan or .ond file following the indentation of the file. - The value of datatypes are assumed during reading. The value units are - the default used by PVsyst. + Contents of the .pan or .ond file following the indentation of the + file. The value of datatypes are assumed during reading. The value + units are the default used by PVsyst. Raises ------ @@ -72,19 +73,27 @@ def parse_panond(fbuf): """ comp = {} # Component dict_levels = [comp] - + fbuf.seek(0) lines = fbuf.getvalue().splitlines() - for i in range(0, len(lines) - 1): # Reading blank lines. Stopping one short to avoid index error. Last line never contains important data. - if lines[i] == '': # Skipping blank lines - continue - indent_lvl_1 = (len(lines[i]) - len(lines[i].lstrip(' '))) // 2 - indent_lvl_2 = (len(lines[i + 1]) - len(lines[i + 1].lstrip(' '))) // 2 - line_data = lines[i].split('=') + for i in range(0, len(lines) - 1): + if lines[i] == '': # Skipping blank lines + continue + # Reading blank lines. Stopping one short to avoid index error. + # Last line never contains important data. + indent_lvl_1 = (len(lines[i]) - len(lines[i].lstrip(' '))) // 2 + indent_lvl_2 = (len(lines[i + 1]) - len(lines[i + 1].lstrip(' '))) // 2 + line_data = lines[i].split('=') key = line_data[0].strip() - value = element_type(line_data[1].strip()) if len(line_data) > 1 else None - if indent_lvl_2 > indent_lvl_1: # add a level to the dict. The key here will be ignored. Not vital to file function. + # value = element_type(line_data[1].strip()) if len(line_data) > 1 else None + if len(line_data) > 1: + value = element_type(line_data[1].strip()) + else: + value = element_type = None + # add a level to the dict. The key here will be ignored. + # Not vital to file function. + if indent_lvl_2 > indent_lvl_1: current_level = dict_levels[indent_lvl_1] new_level = {} current_level[key] = new_level @@ -101,19 +110,20 @@ def parse_panond(fbuf): def read_panond(file): """ - Retrieve Module or Inverter data from a .pan or .ond text file, respectively. + Retrieve Module or Inverter data from a .pan or .ond text file, + respectively. Parameters ---------- file : string or path object Name or path of a .pan/.ond file - + Returns ------- content : Nested Dictionary - Contents of the .pan or .ond file following the indentation of the file. - The value of datatypes are assumed during reading. The value units are - the default used by PVsyst. + Contents of the .pan or .ond file following the indentation of the + file. The value of datatypes are assumed during reading. The value + units are the default used by PVsyst. Raises ------ @@ -131,7 +141,7 @@ def read_panond(file): with open(file, "r", encoding='utf-8-sig') as file: f_content = file.read() fbuf = io.StringIO(f_content) - + content = parse_panond(fbuf) return content diff --git a/pvlib/tests/iotools/test_pvsyst.py b/pvlib/tests/iotools/test_panond.py similarity index 100% rename from pvlib/tests/iotools/test_pvsyst.py rename to pvlib/tests/iotools/test_panond.py