Skip to content

Commit 8f3e7dc

Browse files
committed
add cache to function that will eventually query CMR
1 parent 91f46df commit 8f3e7dc

File tree

7 files changed

+41
-43
lines changed

7 files changed

+41
-43
lines changed

docs/source/conf.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,34 @@
22

33
# -- Project information
44

5-
project = 'MetGenC'
6-
copyright = '2024, NSIDC'
7-
author = 'National Snow and Ice Data Center'
5+
project = "MetGenC"
6+
copyright = "2024, NSIDC"
7+
author = "National Snow and Ice Data Center"
88

9-
release = '0.6'
10-
version = '0.6.0'
9+
release = "0.6"
10+
version = "0.6.0"
1111

1212
# -- General configuration
1313

1414
extensions = [
15-
'sphinx.ext.duration',
16-
'sphinx.ext.doctest',
17-
'sphinx.ext.autodoc',
18-
'sphinx.ext.autosummary',
19-
'sphinx.ext.intersphinx',
15+
"sphinx.ext.duration",
16+
"sphinx.ext.doctest",
17+
"sphinx.ext.autodoc",
18+
"sphinx.ext.autosummary",
19+
"sphinx.ext.intersphinx",
2020
]
2121

2222
intersphinx_mapping = {
23-
'python': ('https://docs.python.org/3/', None),
24-
'sphinx': ('https://www.sphinx-doc.org/en/master/', None),
23+
"python": ("https://docs.python.org/3/", None),
24+
"sphinx": ("https://www.sphinx-doc.org/en/master/", None),
2525
}
26-
intersphinx_disabled_domains = ['std']
26+
intersphinx_disabled_domains = ["std"]
2727

28-
templates_path = ['_templates']
28+
templates_path = ["_templates"]
2929

3030
# -- Options for HTML output
3131

32-
html_theme = 'sphinx_rtd_theme'
32+
html_theme = "sphinx_rtd_theme"
3333

3434
# -- Options for EPUB output
35-
epub_show_urls = 'footnote'
35+
epub_show_urls = "footnote"

src/nsidc/metgen/cli.py

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def validate(config_filename, content_type):
6969
metgen.init_logging(configuration)
7070
metgen.validate(configuration, content_type)
7171

72+
7273
@cli.command()
7374
@click.option(
7475
"-f",
@@ -81,6 +82,7 @@ def assess():
8182
"""Examine a sample data file for metadata completeness"""
8283
return True
8384

85+
8486
@cli.command()
8587
@click.option(
8688
"-c",

src/nsidc/metgen/config.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os.path
55
from pathlib import Path
66

7-
from nsidc.metgen import aws, constants
7+
from nsidc.metgen import aws, constants, netcdf_reader
88

99

1010
class ValidationError(Exception):
@@ -32,7 +32,8 @@ class Config:
3232
dry_run: bool
3333

3434
def __post_init__(self):
35-
self.collection = None
35+
# data_reader: Callable[[str], dict]
36+
self.data_reader = netcdf_reader
3637

3738
def show(self):
3839
# TODO: add section headings in the right spot

src/nsidc/metgen/metgen.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99
import uuid
1010
from collections.abc import Callable
11+
from functools import cache
1112
from importlib.resources import open_text
1213
from pathlib import Path
1314
from string import Template
@@ -18,7 +19,7 @@
1819
from returns.maybe import Maybe
1920
from rich.prompt import Confirm, Prompt
2021

21-
from nsidc.metgen import aws, config, constants, netcdf_reader
22+
from nsidc.metgen import aws, config, constants
2223

2324
# -------------------------------------------------------------------
2425
CONSOLE_FORMAT = "%(message)s"
@@ -186,7 +187,6 @@ class Collection:
186187

187188
auth_id: str
188189
version: int
189-
data_reader: Callable[[str], dict]
190190

191191

192192
@dataclasses.dataclass
@@ -231,15 +231,7 @@ def process(configuration: config.Config) -> None:
231231
"""
232232
Process all Granules and record the results and summary.
233233
"""
234-
# TODO:
235-
# Do any prep actions, like mkdir, etc
236-
# Get real collection information from CMR
237-
# Determine data reader based on actual data file characteristics (e.g. suffix)
238-
configuration.collection = Collection(
239-
configuration.auth_id,
240-
configuration.version,
241-
netcdf_reader.extract_metadata
242-
)
234+
# TODO: Do any prep actions, like mkdir, etc
243235

244236
# Ordered list of operations to perform on each granule
245237
operations = [
@@ -343,12 +335,21 @@ def null_operation(configuration: config.Config, granule: Granule) -> Granule:
343335
return granule
344336

345337

338+
@cache
339+
def retrieve_collection(auth_id: str, version: int):
340+
# ummc_from_cmr = talk_to_cmr(configuration.auth_id, configuration.version)
341+
# pull out fields from UMM-C response and use to create collection object
342+
# with more than just auth_id and version number.
343+
return Collection(auth_id, version)
344+
345+
346346
def granule_collection(configuration: config.Config, granule: Granule) -> Granule:
347347
"""
348-
Associate the Collection with the Granule.
348+
Associate collection information with the Granule.
349349
"""
350350
return dataclasses.replace(
351-
granule, collection=configuration.collection
351+
granule,
352+
collection=retrieve_collection(configuration.auth_id, configuration.version),
352353
)
353354

354355

@@ -399,7 +400,7 @@ def create_ummg(configuration: config.Config, granule: Granule) -> Granule:
399400
# }
400401
metadata_details = {}
401402
for data_file in granule.data_filenames:
402-
metadata_details[data_file] = granule.collection.data_reader(data_file)
403+
metadata_details[data_file] = configuration.data_reader(data_file)
403404

404405
# Collapse information about (possibly) multiple files into a granule summary.
405406
summary = metadata_summary(metadata_details)

src/nsidc/metgen/netcdf_reader.py

+2
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ def spatial_values(netcdf):
6969
for (lon, lat) in perimeter
7070
]
7171

72+
7273
def pixel_padding(netcdf):
7374
# Adding padding should give us values that match up to the
7475
# netcdf.attrs.geospatial_bounds
7576
return abs(float(netcdf.crs.GeoTransform.split()[1])) / 2
7677

78+
7779
def thinned_perimeter(xdata, ydata):
7880
"""
7981
Extract the thinned perimeter of a grid.

tests/test_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def expected_keys():
3232
"checksum_type",
3333
"number",
3434
"dry_run",
35-
"collection"
35+
"data_reader",
3636
]
3737
)
3838

tests/test_metgen.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,14 @@ def test_returns_datetime_range():
102102

103103

104104
def test_s3_object_path_has_no_leading_slash():
105-
granule = metgen.Granule(
106-
"foo",
107-
metgen.Collection("ABCD", 2, "my_reader"),
108-
uuid="abcd-1234"
109-
)
105+
granule = metgen.Granule("foo", metgen.Collection("ABCD", 2), uuid="abcd-1234")
110106
expected = "external/ABCD/2/abcd-1234/xyzzy.bin"
111107
assert metgen.s3_object_path(granule, "xyzzy.bin") == expected
112108

113109

114110
def test_s3_url_simple_case():
115111
staging_bucket_name = "xyzzy-bucket"
116-
granule = metgen.Granule(
117-
"foo",
118-
metgen.Collection("ABCD", 2, "my_reader"),
119-
uuid="abcd-1234"
120-
)
112+
granule = metgen.Granule("foo", metgen.Collection("ABCD", 2), uuid="abcd-1234")
121113
expected = "s3://xyzzy-bucket/external/ABCD/2/abcd-1234/xyzzy.bin"
122114
assert metgen.s3_url(staging_bucket_name, granule, "xyzzy.bin") == expected
123115

0 commit comments

Comments
 (0)