Skip to content

Commit

Permalink
add query-data-ids command
Browse files Browse the repository at this point in the history
  • Loading branch information
n8pease committed Oct 22, 2020
1 parent b80a229 commit 43c7627
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 11 deletions.
2 changes: 2 additions & 0 deletions python/lsst/daf/butler/cli/cmd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"config_validate",
"prune_collection",
"query_collections",
"query_data_ids",
"query_dataset_types",
"query_datasets",
"remove_dataset_type",
Expand All @@ -37,6 +38,7 @@
config_validate,
prune_collection,
query_collections,
query_data_ids,
query_dataset_types,
query_datasets,
remove_dataset_type,
Expand Down
44 changes: 33 additions & 11 deletions python/lsst/daf/butler/cli/cmd/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,28 @@

from ..opt import (
collection_type_option,
collections_option,
dataset_type_option,
datasets_option,
dimensions_argument,
directory_argument,
glob_argument,
options_file_option,
repo_argument,
transfer_option,
verbose_option,
where_option,
)
from ..utils import cli_handle_exception, split_commas, to_upper, typeStrAcceptsMultiple, unwrap

from ..utils import cli_handle_exception, split_commas, typeStrAcceptsMultiple, unwrap
from ... import script


willCreateRepoHelp = "REPO is the URI or path to the new repository. Will be created if it does not exist."
existingRepoHelp = "REPO is the URI or path to an existing data repository root or configuration file."
whereHelp = unwrap("""A string expression similar to a SQL WHERE clause. May involve any column of a dimension
table or a dimension name as a shortcut for the primary key column of a dimension
table.""")


# The conversion from the import command name to the butler_import function
Expand Down Expand Up @@ -182,16 +190,8 @@ def remove_dataset_type(*args, **kwargs):
@repo_argument(required=True)
@glob_argument(help="GLOB is one or more glob-style expressions that fully or partially identify the "
"dataset types to be queried.")
@click.option("--collections",
help=unwrap("""One or more expressions that fully or partially identify the collections to
search for datasets.If not provided all datasets are returned."""),
multiple=True,
metavar=typeStrAcceptsMultiple,
callback=split_commas)
@click.option("--where",
help=unwrap("""A string expression similar to a SQL WHERE clause. May involve any column of a
dimension table or a dimension name as a shortcut for the primary key column of a
dimension table."""))
@collections_option()
@where_option(help=whereHelp)
@click.option("--find-first",
is_flag=True,
help=unwrap("""For each result data ID, only yield one DatasetRef of each DatasetType, from the
Expand All @@ -210,3 +210,25 @@ def query_datasets(**kwargs):
print("")
table.pprint_all()
print("")


@click.command()
@repo_argument(required=True)
@dimensions_argument(help=unwrap("""DIMENSIONS are dimensions of the data IDs to yield. Will be expanded to a
complete DimensionGraph. --collections is required if using this
argument."""))
@collections_option()
@datasets_option(help=unwrap("""An expression that fully or partially identifies dataset types that should
constrain the yielded data IDs. For example, including "raw" here would
constrain the yielded "instrument", "exposure", "detector", and
"physical_filter" values to only those for which at least one "raw" dataset
exists in "collections"."""))
@where_option(help=whereHelp)
@options_file_option()
def query_data_ids(**kwargs):
"""List the data IDs in a repository."""
table = cli_handle_exception(script.queryDataIds, **kwargs)
if table:
table.pprint_all()
else:
print("None")
4 changes: 4 additions & 0 deletions python/lsst/daf/butler/cli/opt/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@
locations_argument = MWArgumentDecorator("locations",
callback=split_commas,
nargs=-1)

dimensions_argument = MWArgumentDecorator("dimensions",
callback=split_commas,
nargs=-1)
16 changes: 16 additions & 0 deletions python/lsst/daf/butler/cli/opt/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def makeCollectionTypes(context, param, value):
type=click.Choice(CollectionTypeCallback.collectionTypes,
case_sensitive=False))


collections_option = MWOptionDecorator("--collections",
help=unwrap("""One or more expressions that fully or partially identify
the collections to search for datasets.If not provided all
datasets are returned."""),
multiple=True,
callback=split_commas)


config_option = MWOptionDecorator("-c", "--config",
callback=split_kv,
help="Config override, as a key-value pair.",
Expand All @@ -76,6 +85,9 @@ def makeCollectionTypes(context, param, value):
multiple=True)


datasets_option = MWOptionDecorator("--datasets")


logLevelChoices = ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"]
log_level_option = MWOptionDecorator("--log-level",
callback=partial(split_kv,
Expand Down Expand Up @@ -128,3 +140,7 @@ def makeCollectionTypes(context, param, value):
verbose_option = MWOptionDecorator("-v", "--verbose",
help="Increase verbosity.",
is_flag=True)


where_option = MWOptionDecorator("--where",
help="A string expression similar to a SQL WHERE clause.")
1 change: 1 addition & 0 deletions python/lsst/daf/butler/script/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .configValidate import configValidate
from .pruneCollection import pruneCollection
from .queryCollections import queryCollections
from .queryDataIds import queryDataIds
from .queryDatasets import queryDatasets
from .queryDatasetTypes import queryDatasetTypes
from .removeDatasetType import removeDatasetType
39 changes: 39 additions & 0 deletions python/lsst/daf/butler/script/queryDataIds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This file is part of daf_butler.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from astropy.table import Table

from .. import Butler


def queryDataIds(repo, dimensions, datasets, where, collections):
# Docstring for supported parameters is the same as Registry.queryDataIds

butler = Butler(repo)
results = butler.registry.queryDataIds(dimensions,
datasets=datasets,
where=where,
collections=collections)

if len(results.graph) > 0:
return Table([{str(k): v for k, v in item.full.items()} for item in list(results)])
else:
return None

0 comments on commit 43c7627

Please sign in to comment.