Skip to content

Commit

Permalink
Merge pull request natcap#1461 from phargogh/bugfix/1460-hra-csv-bom-…
Browse files Browse the repository at this point in the history
…loading-issue

Handle BOMs when loading HRA criteria tables
  • Loading branch information
dcdenu4 authored Dec 11, 2023
2 parents f7597fa + 0bc66be commit 9ec52a8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Unreleased Changes
table columns: ``cur_path``, ``fut_path``, and ``base_path`` are meant
to be file system path strings.
https://github.com/natcap/invest/issues/1455
* HRA
* Fixed an issue preventing the HRA criteria table from loading when the
table was UTF-8 encoded with a Byte-Order Marker.
https://github.com/natcap/invest/issues/1460
* NDR
* Fixing an issue where minor geometric issues in the watersheds input
(such as a ring self-intersection) would raise an error in the model.
Expand Down
35 changes: 35 additions & 0 deletions tests/test_hra.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,41 @@ 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', encoding='utf-8-sig') as criteria_table:
criteria_table.write(
textwrap.dedent(
"""\
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
"""
))

# Sanity check: make sure the file has the expected BOM
# Gotta use binary mode so that python doesn't silently strip the BOM
with open(criteria_table_path, 'rb') as criteria_table:
self.assertTrue(criteria_table.read().startswith(b"\xef\xbb\xbf"))

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 9ec52a8

Please sign in to comment.