LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
sourceAssocArgumentParser.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2012 LSST Corporation.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 import argparse
23 import re
24 
25 import lsst.pipe.base as pipeBase
26 
27 
28 class SkyTileIdAction(argparse.Action):
29  """Action callback to add one sky-tile ID to namespace.skyTileIdSet.
30  """
31  def __call__(self, parser, namespace, values, option_string):
32  """Parse --id skyTile=value_list and add results to namespace.skyTileIdSet.
33 
34  The format of value_list is value(^value)*, where each value must be
35  an integer. Values are added to a set, so the final list of sky-tile IDs
36  is duplicate-free.
37  """
38  if len(values) != 1:
39  parser.error(str.format(
40  "{} must be followed by exactly one argument", option_string))
41  name, _, values = values[0].partition("=")
42  if name != "skyTile":
43  parser.error(str.format(
44  "{} argument must be of the form skyTile=VALUES", option_string))
45  if not hasattr(namespace, "skyTileIds"):
46  namespace.skyTileIds = set()
47  for v in values.split("^"):
48  interval = re.search(r"^(\d+)\.\.(\d+)$", v)
49  if interval:
50  namespace.skyTileIds.update(xrange(int(interval.group(1)),
51  int(interval.group(2))))
52  elif re.match(r"^\d+$", v):
53  namespace.skyTileIds.add(int(v))
54  else:
55  parser.error(str.format(
56  "cannot parse {} as an integer for {}", v, option_string))
57 
58 
59 class SourceAssocArgumentParser(pipeBase.ArgumentParser):
60  """A version of lsst.pipe.base.ArgumentParser
61  specialized for source association.
62  """
63  def __init__(self, **kwargs):
64  """Construct an option parser.
65  """
66  # need to swap out the behaviour of --id with something that makes
67  # sense for sourceAssoc
68  pipeBase.ArgumentParser.__init__(
69  self, conflict_handler='resolve', **kwargs)
70  self.add_argument("--id", nargs=1, action=SkyTileIdAction,
71  help="skytile ID. Can be specified multiple times; if omitted, all "
72  "sky-tiles are processed. Example: --id skyTile=12..23^45^67",
73  metavar="skyTile=VALUES")
74