LSSTApplications  17.0+124,17.0+14,17.0+73,18.0.0+37,18.0.0+80,18.0.0-4-g68ffd23+4,18.1.0-1-g0001055+12,18.1.0-1-g03d53ef+5,18.1.0-1-g1349e88+55,18.1.0-1-g2505f39+44,18.1.0-1-g5315e5e+4,18.1.0-1-g5e4b7ea+14,18.1.0-1-g7e8fceb+4,18.1.0-1-g85f8cd4+48,18.1.0-1-g8ff0b9f+4,18.1.0-1-ga2c679d+1,18.1.0-1-gd55f500+35,18.1.0-10-gb58edde+2,18.1.0-11-g0997b02+4,18.1.0-13-gfe4edf0b+12,18.1.0-14-g259bd21+21,18.1.0-19-gdb69f3f+2,18.1.0-2-g5f9922c+24,18.1.0-2-gd3b74e5+11,18.1.0-2-gfbf3545+32,18.1.0-26-g728bddb4+5,18.1.0-27-g6ff7ca9+2,18.1.0-3-g52aa583+25,18.1.0-3-g8ea57af+9,18.1.0-3-gb69f684+42,18.1.0-3-gfcaddf3+6,18.1.0-32-gd8786685a,18.1.0-4-gf3f9b77+6,18.1.0-5-g1dd662b+2,18.1.0-5-g6dbcb01+41,18.1.0-6-gae77429+3,18.1.0-7-g9d75d83+9,18.1.0-7-gae09a6d+30,18.1.0-9-gc381ef5+4,w.2019.45
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