Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 13 - Add SWORD version from shp.xml #111

Merged
merged 7 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Issue 75 - Update log messaging format
- Issue 60 - Encapsulate DyanmoDB under a single shared module
- Issue 60 - Improved error handling
- Issue 13 - Add SWORD version from shp.xml to DB entries
### Changed
### Deprecated
### Removed
Expand Down
38 changes: 37 additions & 1 deletion hydrocron/db/io/swot_reach_node_shp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import json
from datetime import datetime
from importlib import resources
import xml.etree.ElementTree as ET
import zipfile
import logging

import geopandas as gpd
import numpy as np
import pandas as pd


logging.getLogger().setLevel(logging.INFO)


Expand Down Expand Up @@ -44,8 +47,12 @@ def read_shapefile(filepath, obscure_data, columns, s3_resource=None):
s3_resource.Bucket(bucket_name).download_file(key, lambda_temp_file)

shp_file = gpd.read_file('zip://' + lambda_temp_file)
with zipfile.ZipFile(lambda_temp_file) as archive:
shp_xml_tree = ET.fromstring(archive.read(filename[:-4] + ".shp.xml"))
else:
shp_file = gpd.read_file('zip://' + filepath)
with zipfile.ZipFile(filepath) as archive:
shp_xml_tree = ET.fromstring(archive.read(filename[:-4] + ".shp.xml"))

numeric_columns = shp_file[columns].select_dtypes(include=[np.number]).columns
if obscure_data:
Expand All @@ -59,14 +66,43 @@ def read_shapefile(filepath, obscure_data, columns, s3_resource=None):
shp_file = shp_file.astype(str)
filename_attrs = parse_from_filename(filename)

items = assemble_attributes(shp_file, filename_attrs)
xml_attrs = parse_metadata_from_shpxml(shp_xml_tree)

attributes = filename_attrs | xml_attrs
items = assemble_attributes(shp_file, attributes)

if os.path.exists(lambda_temp_file):
os.remove(lambda_temp_file)

return items


def parse_metadata_from_shpxml(xml_etree):
"""
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

Returns
-------
metadata_attrs : dict
a dictionary of metadata attributes to add to record
"""

for globs in xml_etree.findall('global_attributes'):
prior_db_files = globs.find('xref_prior_river_db_files').text

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

return metadata_attrs


def assemble_attributes(file_as_str, attributes):
"""
Helper function to concat file attributes to records
Expand Down
5 changes: 4 additions & 1 deletion hydrocron/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"time": "739741183.129",
"time_str": "2023-06-10T19:39:43Z",
"wse": "286.2983",
"cycle_id": "548"
"cycle_id": "548",
"sword_version": "15"
}

DB_TEST_TABLE_NAME = "hydrocron-swot-test-table"
Expand All @@ -32,6 +33,7 @@
TEST_REACH_ID_VALUE = '71224100223'
TEST_TIME_VALUE = '2023-06-10T19:33:37Z'
TEST_WSE_VALUE = '286.2983'
TEST_SWORD_VERSION_VALUE = '15'

# ------------ #
# PROD CONSTANTS #
Expand All @@ -54,6 +56,7 @@
FIELDNAME_SLOPE = 'slope'
FIELDNAME_P_LON = 'p_lon'
FIELDNAME_P_LAT = 'p_lat'
FIELDNAME_SWORD_VERSION = 'sword_version'

S3_CREDS_ENDPOINT = "https://archive.swot.podaac.earthdata.nasa.gov/s3credentials"

Expand Down
1 change: 1 addition & 0 deletions tests/test_hydrocron_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ 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


def test_delete_item(hydrocron_dynamo_table):
Expand Down
Loading