LSSTApplications  18.1.0
LSSTDataManagementBasePackage
astrometryNetDataConfig.py
Go to the documentation of this file.
1 '''
2 We used to have AstrometryNetDataConfig() use the pex_config
3 mechanism, but we need nested lists, so we do this home-brew version
4 instead.
5 '''
6 from __future__ import absolute_import, division, print_function
7 
8 __all__ = ["AstrometryNetDataConfig"]
9 
10 from builtins import object
11 
12 
13 def _checkMagMap(magmap):
14  '''
15  Checks the validity of a magnitude column map in AstrometryNetDataConfig.
16  '''
17  if not isinstance(magmap, dict):
18  raise RuntimeError('Mag maps must be dicts')
19  for k, v in magmap.items():
20  if not isinstance(k, str):
21  raise RuntimeError('Mag maps must be dicts mapping str->str: got bad key \"%s\"' % str(k))
22  if not isinstance(v, str):
23  raise RuntimeError('Mag maps must be dicts mapping str->str: got bad value \"%s\"' % str(v))
24  if not (len(k) > 0 and len(v) > 0):
25  raise RuntimeError('Mag maps items must be non-empty: got bad values \"%s\" -> \"%s\"' %
26  (str(k), str(v)))
27 
28 
29 def _checkIndexList(indexList):
30  '''
31  Checks the validity of an index list in AstrometryNetDataConfig.
32  '''
33  if not isinstance(indexList, list):
34  raise RuntimeError('indexList config item must be a list')
35  for k in indexList:
36  if not isinstance(k, str):
37  raise RuntimeError('indexList config items must be strings: got bad one \"%s\"' % (str(k),))
38  if len(k) == 0:
39  raise RuntimeError('indexList config items must be non-empty strings')
40 
41 
42 def _checkMultiIndexList(multiIndexList):
43  '''
44  Checks the validity of a multi_index list in AstrometryNetDataConfig.
45  '''
46  if not isinstance(multiIndexList, list):
47  raise RuntimeError('multiIndexList config item must be a list')
48  for k in multiIndexList:
49  if not isinstance(k, list):
50  raise RuntimeError('multiIndexList config items must be lists: got bad one \"%s\"' % (str(k),))
51  if len(k) == 0:
52  raise RuntimeError('multiIndexList config items must be non-empty lists')
53  for kk in k:
54  if not isinstance(kk, str):
55  raise RuntimeError('multiIndexList config items must be strings: got bad one \"%s\"' %
56  (str(kk),))
57  if len(kk) == 0:
58  raise RuntimeError('multiIndexList config items must be non-empty strings')
59 
60 
62  '''
63  Astrometry.net data config object. This is a plain-python config
64  structure similar to pexConfig.
65 
66  For examples of use, see tests/astrometry_net_data/photocal/andConfig*.py
67 
68  '''
69  fields = [
70  ('idColumn', str, 'id', None,
71  'Column name (in the index files) of the ID number of reference sources'),
72  ('defaultMagColumn', str, 'mag', None,
73  'Default column name (in the index files) of the reference source mag'),
74  ('defaultMagErrorColumn', str, '', None,
75  'Default column name (in the index files) of the reference source mag error'),
76  ('starGalaxyColumn', str, None, None,
77  'Column name of the star/galaxy flag'),
78  ('variableColumn', str, None, None,
79  'Column name of the star variability flag'),
80  ('magErrorColumnMap', dict, {}, _checkMagMap,
81  'Mapping from LSST filter name to mag error column name'),
82  ('magColumnMap', dict, {}, _checkMagMap,
83  'Mapping from LSST filter name to mag column name'),
84  ('indexFiles', list, [], _checkIndexList,
85  'List of Astrometry.net index filenames'),
86  ('multiIndexFiles', list, [], _checkMultiIndexList,
87  'Astrometry.net multi-index filename lists. '
88  'Each item in this list must itself be a list of filenames. '
89  'The first filename is the file that contains the star kd-tree and tag-along tables. '
90  'Subsequent filenames must be files containing just the non-star index parts '
91  '(quads and code kd-tree). Note that this means you may need to repeat the first filename '
92  'if it contains a star kd-tree and the first index.'),
93  ('allowCache', bool, True, None,
94  'Allow use of cache for reading index file regions?'),
95  ]
96 
97  def load(self, fn):
98  # Hold on to your socks!
99  loc = dict(root=self)
100  with open(fn, 'rb') as file:
101  code = compile(file.read(), fn, 'exec')
102  exec(code, globals(), loc)
103 
104  def __init__(self, **kwargs):
105  self.setDefaults()
106  for k, v in kwargs.items():
107  self.set(k, v)
108 
109  def setDefaults(self):
110  for nm, typ, deef, check, doc in AstrometryNetDataConfig.fields:
111  self.set(nm, deef)
112 
113  def set(self, k, v):
114  setattr(self, k, v)
115 
116  def __setattr__(self, k, v):
117  for nm, typ, deef, check, doc in AstrometryNetDataConfig.fields:
118  if k != nm:
119  continue
120  if typ is not None:
121  if v is None:
122  pass
123  elif not isinstance(v, typ):
124  raise RuntimeError(('Attempted to set AstrometryNetDataConfig'
125  ' field \"%s\" to type %s, but need type %s') %
126  (nm, str(typ), str(type(v))))
127  if check is not None:
128  check(v)
129  # Looks ok; set it!
130  object.__setattr__(self, nm, v)
131  return
132 
133  raise RuntimeError('Attempted to set invalid AstrometryNetDataConfig'
134  ' field \"%s\"' % k)
table::Key< int > type
Definition: Detector.cc:167