Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
36 changes: 28 additions & 8 deletions src/xopr/opr_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,38 @@ def load_frame_url(self, url: str) -> xr.Dataset:
warnings.warn(f"Warning: Unexpected result from ops_api: {result['data']}", UserWarning)
else:
result_data = {}
for key, value in result['data'].items():
if len(value) == 1:
result_data = result['data']
if 'metadata' in result_data:
result_data = result_data['metadata'] # New style response from OPS API
new_api = True
else:
new_api = False

# Flatten lists with single items to just the item, and convert lists with multiple items to sets
for key, value in result_data.items():
if len(value) == 0:
result_data[key] = None
elif len(value) == 1:
result_data[key] = value[0]
elif len(value) > 1:
result_data[key] = set(value)

if 'dois' in result_data:
ds.attrs['doi'] = result_data['dois']
if 'rors' in result_data:
ds.attrs['ror'] = result_data['rors']
if 'funding_sources' in result_data:
ds.attrs['funder_text'] = result_data['funding_sources']
# These match on the old style keys from the OPS API for backwards compatibility
if new_api:
for k, v in result_data.items():
if k in ds.attrs:
if ds.attrs[k] != v:
warnings.warn(f"Warning: Conflicting value for attribute '{k}': existing value '{ds.attrs[k]}', new value '{v}'", UserWarning)
ds.attrs[k] = v
else:
if 'dois' in result_data:
ds.attrs['DOI'] = result_data['dois']
if 'rors' in result_data:
ds.attrs['Organization'] = result_data['rors']
if 'funding_sources' in result_data:
ds.attrs['funder_text'] = result_data['funding_sources']



# Add the rest of the Matlab parameters
if filetype == 'hdf5':
Expand Down
32 changes: 25 additions & 7 deletions src/xopr/opr_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,40 @@ def generate_citation(ds : xr.Dataset) -> str:

citation_string += "== Data Citation ==\n"

if 'ror' in ds.attrs and ds.attrs['ror']:
# Find the right key for ROR
ror_key = None
if 'ror' in ds.attrs:
ror_key = 'ror'
elif 'Organization' in ds.attrs:
ror_key = 'Organization'

if ror_key and ds.attrs[ror_key]:
any_citation_info = True
if isinstance(ds.attrs['ror'], (set, list)):
institution_name = ', '.join([get_ror_display_name(ror) for ror in ds.attrs['ror']])
if isinstance(ds.attrs[ror_key], (set, list)):
institution_name = ', '.join([get_ror_display_name(ror) for ror in ds.attrs[ror_key]])
else:
institution_name = get_ror_display_name(ds.attrs['ror'])
institution_name = get_ror_display_name(ds.attrs[ror_key])

citation_string += f"This data was collected by {institution_name}.\n"

if 'doi' in ds.attrs and ds.attrs['doi']:
# Find the right key for DOI
doi_key = None
if 'doi' in ds.attrs:
doi_key = 'doi'
elif 'DOI' in ds.attrs:
doi_key = 'DOI'

if doi_key and ds.attrs[doi_key]:
any_citation_info = True
citation_string += f"Please cite the dataset DOI: https://doi.org/{ds.attrs['doi']}\n"
citation_string += f"Please cite the dataset DOI: https://doi.org/{ds.attrs[doi_key]}\n"

if 'funder_text' in ds.attrs and ds.attrs['funder_text']:
if 'funder_text' in ds.attrs and ds.attrs['funder_text']: # Old style funding acknowledgement
any_citation_info = True
citation_string += f"Please include the following funder acknowledgment:\n{ds.attrs['funder_text']}\n"

if 'Funding' in ds.attrs and ds.attrs['Funding']: # New style funding acknowledgement
any_citation_info = True
citation_string += f"Data collection was funded by: {ds.attrs['Funding']}\n"

if not any_citation_info:
citation_string += "No specific citation information was retrieved for this dataset. By default, please cite:\n"
Expand Down
6 changes: 5 additions & 1 deletion src/xopr/stac/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ def extract_item_metadata(
center_freq = float((low_freq + high_freq) / 2)

# Extract science metadata
doi = ds.attrs.get('doi', None)
if 'DOI' in ds.attrs:
doi = ds.attrs['DOI']
else:
doi = ds.attrs.get('doi', None)

cite = ds.attrs.get('funder_text', None)
mime = ds.attrs['mimetype']

Expand Down
7 changes: 6 additions & 1 deletion src/xopr/tests/test_ops_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ def test_get_segment_metadata_valid_flights(season, flight_id):
data = result['data']
assert data is not None, f"Expected non-null data for {season}/{flight_id}"

expected_keys = ['dois', 'funding_sources', 'rors']
if 'metadata' in data:
# New style response from OPS API
data = data['metadata']
expected_keys = ['DOI', 'Funding', 'Organization']
else:
expected_keys = ['dois', 'funding_sources', 'rors']

for key in expected_keys:
assert key in data, f"Expected key '{key}' in data for {season}/{flight_id}"
Expand Down