LSSTApplications  19.0.0,19.0.0+1,19.0.0+10,19.0.0+13,19.0.0+3,19.0.0+5,19.0.0+9,tickets.DM-22703-ga158cbef15,w.2019.51
LSSTDataManagementBasePackage
read_defects.py
Go to the documentation of this file.
1 from lsst.meas.algorithms import Defects
2 import os
3 import glob
4 import dateutil.parser
5 
6 
7 def read_defects_one_chip(root, chip_name, chip_id):
8  """Read defects for a particular sensor from the standard format at a particular root.
9 
10  Parameters
11  ----------
12  root : str
13  Path to the top level of the defects tree. This is expected to hold directories
14  named after the sensor names. They are expected to be lower case.
15  chip_name : str
16  The name of the sensor for which to read defects.
17  chip_id : int
18  The identifier for the sensor in question.
19 
20  Returns
21  -------
22  dict
23  A dictionary of `lsst.meas.algorithms.Defects`.
24  The key is the validity start time as a `datetime` object.
25  """
26  files = glob.glob(os.path.join(root, chip_name, '*.ecsv'))
27  parts = os.path.split(root)
28  instrument = os.path.split(parts[0])[1] # convention is that these reside at <instrument>/defects
29  defect_dict = {}
30  for f in files:
31  date_str = os.path.splitext(os.path.basename(f))[0]
32  valid_start = dateutil.parser.parse(date_str)
33  defect_dict[valid_start] = Defects.readText(f)
34  check_metadata(defect_dict[valid_start], valid_start, instrument, chip_id, f)
35  return defect_dict
36 
37 
38 def check_metadata(defects, valid_start, instrument, chip_id, f):
39  """Check that the metadata is complete and self consistent
40 
41  Parameters
42  ----------
43  defects : `lsst.meas.algorithms.Defects`
44  Object to retrieve metadata from in order to compare with
45  metadata inferred from the path.
46  valid_start : datetime
47  Start of the validity range for defects
48  instrument : str
49  Name of the instrument in question
50  chip_id : int
51  Identifier of the sensor in question
52  f : str
53  Path of the file read to produce ``defects``
54 
55  Returns
56  -------
57  None
58 
59  Raises
60  ------
61  ValueError
62  If the metadata from the path and the metadata encoded
63  in the path do not match for any reason.
64  """
65  md = defects.getMetadata()
66  finst = md.get('INSTRUME')
67  fchip_id = md.get('DETECTOR')
68  fcalib_date = md.get('CALIBDATE')
69  if not (finst.lower(), int(fchip_id), fcalib_date) == (instrument.lower(),
70  chip_id, valid_start.isoformat()):
71  raise ValueError("Path and file metadata do not agree:\n" +
72  "Path metadata: %s, %s, %s\n"%(instrument, chip_id, valid_start.isoformat()) +
73  "File metadata: %s, %s, %s\n"%(finst, fchip_id, fcalib_date) +
74  "File read from : %s\n"%(f)
75  )
76 
77 
78 def read_all_defects(root, camera):
79  """Read all defects from the standard format at a particular root.
80 
81  Parameters
82  ----------
83  root : str
84  Path to the top level of the defects tree. This is expected to hold directories
85  named after the sensor names. They are expected to be lower case.
86  camera : `lsst.afw.cameraGeom.Camera`
87  The camera that goes with the defects being read.
88 
89  Returns
90  -------
91  dict
92  A dictionary of dictionaries of `lsst.meas.algorithms.Defects`.
93  The first key is the sensor name, and the second is the validity
94  start time as a `datetime` object.
95  """
96  root = os.path.normpath(root)
97  dirs = os.listdir(root) # assumes all directories contain defects
98  dirs = [d for d in dirs if os.path.isdir(os.path.join(root, d))]
99  defects_by_chip = {}
100  name_map = {det.getName().lower(): det.getName() for
101  det in camera} # we assume the directories have been lowered
102  for d in dirs:
103  chip_name = os.path.basename(d)
104  chip_id = camera[name_map[chip_name]].getId()
105  defects_by_chip[chip_name] = read_defects_one_chip(root, chip_name, chip_id)
106  return defects_by_chip
def read_all_defects(root, camera)
Definition: read_defects.py:78
def read_defects_one_chip(root, chip_name, chip_id)
Definition: read_defects.py:7
Fit spatial kernel using approximate fluxes for candidates, and solving a linear system of equations...
def check_metadata(defects, valid_start, instrument, chip_id, f)
Definition: read_defects.py:38