Skip to content

Commit

Permalink
Feature/issue 85 - Add units to db records (#113)
Browse files Browse the repository at this point in the history
* add skeleton xml parsing function

* add function to read shp.xml file, update unit tests

* update changelog

* fix filename reference

* parse xml file for field units, update tests

* update changelog

---------

Co-authored-by: Frank Greguska <[email protected]>
  • Loading branch information
torimcd and frankinspace authored Mar 18, 2024
1 parent 9d6914a commit 274b220
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- Issue 85 - Add variable units to API response
- Issue 79 - Generate data for use by benchmarks
- Issue 88 - There are no CloudWatch logs for the API Gateway
- Issue 13 - Add SWORD version from shp.xml to DB entries
Expand Down
24 changes: 19 additions & 5 deletions hydrocron/db/io/swot_reach_node_shp.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,43 @@ def read_shapefile(filepath, obscure_data, columns, s3_resource=None):
return items


def parse_metadata_from_shpxml(xml_etree):
def parse_metadata_from_shpxml(xml_elem):
"""
Read the SWORD version number from the shp.xml file
and add to the database fields
Parameters
----------
xml_etree : xml.etree.ElementTree
an Element Tree representation of the shp.xml metadata file
xml_elem : xml.etree.ElementTree.Element
an Element representation of the shp.xml metadata file
Returns
-------
metadata_attrs : dict
a dictionary of metadata attributes to add to record
"""

for globs in xml_etree.findall('global_attributes'):
# get SWORD version
for globs in xml_elem.findall('global_attributes'):
prior_db_files = globs.find('xref_prior_river_db_files').text

metadata_attrs = {
'sword_version': prior_db_files[-5:-3]
}

# get units on fields that have them
for child in xml_elem:
if child.tag == 'attributes':
for field in child:
try:
units = field.find('units').text
except AttributeError:
units = ""
logging.info('No units on field %s', field.tag)

if units != "":
unit_field_name = field.tag + "_units"
metadata_attrs[unit_field_name] = units

return metadata_attrs


Expand Down
6 changes: 4 additions & 2 deletions hydrocron/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"time_str": "2023-06-10T19:39:43Z",
"wse": "286.2983",
"cycle_id": "548",
"sword_version": "15"
}
"sword_version": "15",
"p_lat_units": "degrees_north"

DB_TEST_TABLE_NAME = "hydrocron-swot-test-table"
API_TEST_TABLE_NAME = "hydrocron-swot-reach-table"
Expand All @@ -34,6 +34,8 @@
TEST_TIME_VALUE = '2023-06-10T19:33:37Z'
TEST_WSE_VALUE = '286.2983'
TEST_SWORD_VERSION_VALUE = '15'
TEST_UNITS_FIELD = 'p_lat_units'
TEST_UNITS = 'degrees_north'

# ------------ #
# PROD CONSTANTS #
Expand Down
2 changes: 2 additions & 0 deletions tests/test_hydrocron_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ def test_query(hydrocron_dynamo_table):

items = hydrocron_dynamo_table.run_query(
partition_key=constants.TEST_REACH_ID_VALUE)

assert items[0][constants.FIELDNAME_WSE] == constants.TEST_WSE_VALUE
assert items[0][constants.FIELDNAME_SWORD_VERSION] == constants.TEST_SWORD_VERSION_VALUE
assert items[0][constants.TEST_UNITS_FIELD] == constants.TEST_UNITS


def test_delete_item(hydrocron_dynamo_table):
Expand Down

0 comments on commit 274b220

Please sign in to comment.