LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
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