Skip to content

Commit

Permalink
Fixing and testing UTF-8-SIG for the criteria table.
Browse files Browse the repository at this point in the history
  • Loading branch information
phargogh committed Dec 5, 2023
1 parent 329da4f commit 753bcb3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/natcap/invest/hra.py
Original file line number Diff line number Diff line change
Expand Up @@ -1830,7 +1830,7 @@ def _parse_criteria_table(criteria_table_path, target_composite_csv_path):
# This function requires that the table is read as a numpy array, so it's
# easiest to read the table directly.
table = pandas.read_csv(criteria_table_path, header=None, sep=None,
engine='python').to_numpy()
engine='python', encoding='utf-8-sig').to_numpy()

# clean up any leading or trailing whitespace.
for row_num in range(table.shape[0]):
Expand Down
30 changes: 30 additions & 0 deletions tests/test_hra.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,36 @@ def test_criteria_table_parsing(self):
pandas.testing.assert_frame_equal(
expected_composite_dataframe, composite_dataframe)

def test_criteria_table_parsing_with_bom(self):
"""HRA: criteria table - parse a BOM."""
from natcap.invest import hra

criteria_table_path = os.path.join(self.workspace_dir, 'criteria.csv')
with open(criteria_table_path, 'w') as criteria_table:
bom_char = "\uFEFF" # byte-order marker in 16-bit hex value
criteria_table.write(
textwrap.dedent(
f"""\
{bom_char}HABITAT NAME,eelgrass,,,hardbottom,,,CRITERIA TYPE
HABITAT RESILIENCE ATTRIBUTES,RATING,DQ,WEIGHT,RATING,DQ,WEIGHT,E/C
recruitment rate,2,2,2,2,2,2,C
connectivity rate,2,2,2,2,2,2,C
,,,,,,,
HABITAT STRESSOR OVERLAP PROPERTIES,,,,,,,
oil,RATING,DQ,WEIGHT,RATING,DQ,WEIGHT,E/C
frequency of disturbance,2,2,3,2,2,3,C
management effectiveness,2,2,1,2,2,1,E
,,,,,,,
fishing,RATING,DQ,WEIGHT,RATING,DQ,WEIGHT,E/C
frequency of disturbance,2,2,3,2,2,3,C
management effectiveness,2,2,1,2,2,1,E
"""
))
target_composite_csv_path = os.path.join(self.workspace_dir,
'composite.csv')
hra._parse_criteria_table(criteria_table_path,
target_composite_csv_path)

def test_criteria_table_file_not_found(self):
"""HRA: criteria table - spatial file not found."""
from natcap.invest import hra
Expand Down

0 comments on commit 753bcb3

Please sign in to comment.