Skip to content

Commit

Permalink
WIP refactor query method that orders granules
Browse files Browse the repository at this point in the history
Separate out the subset and non-subset options
  • Loading branch information
trey-stafford committed Nov 13, 2024
1 parent 6e6fa92 commit fb2a9a7
Showing 1 changed file with 58 additions and 55 deletions.
113 changes: 58 additions & 55 deletions icepyx/core/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,58 @@ def avail_granules(
else:
return granules.info(self.granules.avail)

def _order_subset_granules(self, **kwargs):
# This call ensures that `self._cmr_reqparams` is set.
self.cmr_reqparams
if self._cmr_reqparams._reqtype == "search":
self._cmr_reqparams._reqtype = "download"

# TODO: is it necessary to delete the `self._subsetparams` attr? We are
# performing a subset so this seems the opposite of what is expected.
if hasattr(self, "_subsetparams") and self._subsetparams is None:
del self._subsetparams

# TODO: this shouldn't be necessary:
cmr_params = {**self.CMRparams, **self.cmr_reqparams}

# REFACTOR: add checks here to see if the granules object has been created,
# and also if it already has a list of avail granules (if not, need to create one and add session)

# Place multiple orders, one per granule, if readable_granule_name is used.
# TODO/Question: is "readable granule name" a thing in harmony? This may
# only be relevant for non-subset requests that will be handled by e.g.,
# `earthaccess`.
# Answer: there appears to be a `granuleName` parameter that harmony can
# take for its own internal queries to CMR, but it is unclear how that
# works. See https://harmony.earthdata.nasa.gov/docs#query-parameters
# `harmony-py` accepts `granule_name` as an input.
if "readable_granule_name[]" in cmr_params:
gran_name_list = cmr_params["readable_granule_name[]"]
tempCMRparams = cmr_params.copy()
if len(gran_name_list) > 1:

Check failure on line 1028 in icepyx/core/query.py

View workflow job for this annotation

GitHub Actions / test

Argument of type "object" cannot be assigned to parameter "obj" of type "Sized" in function "len"   "object" is incompatible with protocol "Sized"     "__len__" is not present (reportArgumentType)
print(
"Harmony only allows ordering of one granule by name at a time;"
" your orders will be placed accordingly."
)
for gran in gran_name_list:

Check failure on line 1033 in icepyx/core/query.py

View workflow job for this annotation

GitHub Actions / test

"object" is not iterable   "__iter__" method not defined (reportGeneralTypeIssues)
tempCMRparams["readable_granule_name[]"] = gran
self.granules.place_harmony_subset_order(
tempCMRparams,
self.subsetparams(granule_name=gran, **kwargs),
geom_filepath=self._spatial._geom_file,
)

else:
self.granules.place_harmony_subset_order(
cmr_params,
self.subsetparams(**kwargs),
geom_filepath=self._spatial._geom_file,
)

def _order_whole_granules(self):
raise NotImplementedError
self._subsetparams = None

# DevGoal: display output to indicate number of granules successfully ordered (and number of errors)
# DevGoal: deal with subset=True for variables now, and make sure that if a variable subset
# Coverage kwarg is input it's successfully passed through all other functions even if this is the only one run.
Expand All @@ -1023,9 +1075,7 @@ def order_granules(
**kwargs : key-value pairs
Additional parameters to be passed to the subsetter.
By default temporal and spatial subset keys are passed.
Acceptable key values are ['format','projection','projection_parameters','Coverage'].
The variable 'Coverage' list should be constructed using the `order_vars.wanted` attribute of the object.
At this time (2020-05), only variable ('Coverage') parameters will be automatically formatted.
Acceptable key values are [TODO].
See Also
--------
Expand All @@ -1044,59 +1094,12 @@ def order_granules(
.
Retry request status is: complete
"""
if not subset:
raise NotImplementedError

# This call ensures that `self._cmr_reqparams` is set.
self.cmr_reqparams
if self._cmr_reqparams._reqtype == "search":
self._cmr_reqparams._reqtype = "download"

if subset is False:
self._subsetparams = None
elif (
subset is True
and hasattr(self, "_subsetparams")
and self._subsetparams is None
):
del self._subsetparams

# TODO: this shouldn't be necessary:
cmr_params = {**self.CMRparams, **self.cmr_reqparams}

# REFACTOR: add checks here to see if the granules object has been created,
# and also if it already has a list of avail granules (if not, need to create one and add session)

# Place multiple orders, one per granule, if readable_granule_name is used.
# TODO/Question: is "readable granule name" a thing in harmony? This may
# only be relevant for non-subset requests that will be handled by e.g.,
# `earthaccess`.
# Answer: there appears to be a `granuleName` parameter that harmony can
# take for its own internal queries to CMR, but it is unclear how that
# works. See https://harmony.earthdata.nasa.gov/docs#query-parameters
# `harmony-py` accepts `granule_name` as an input.
if "readable_granule_name[]" in cmr_params:
gran_name_list = cmr_params["readable_granule_name[]"]
tempCMRparams = cmr_params.copy()
if len(gran_name_list) > 1:
print(
"Harmony only allows ordering of one granule by name at a time;"
" your orders will be placed accordingly."
)
for gran in gran_name_list:
tempCMRparams["readable_granule_name[]"] = gran
self.granules.place_subset_order(
tempCMRparams,
self.subsetparams(granule_name=gran, **kwargs),
geom_filepath=self._spatial._geom_file,
)

else:
self.granules.place_subset_order(
cmr_params,
self.subsetparams(**kwargs),
geom_filepath=self._spatial._geom_file,
if subset:
self._order_subset_granules(
**kwargs,
)
else:
self._order_whole_granules()

# DevGoal: put back in the kwargs here so that people can just call download granules with subset=False!
def download_granules(
Expand Down

0 comments on commit fb2a9a7

Please sign in to comment.