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