LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
LSST Data Management Base Package
read_curated_calibs.py
Go to the documentation of this file.
2 from lsst.ip.isr import (Linearizer, CrosstalkCalib, Defects, BrighterFatterKernel)
3 
4 import os
5 import glob
6 import dateutil.parser
7 
8 
9 def read_one_chip(root, chip_name, chip_id):
10  """Read data for a particular sensor from the standard format at a particular root.
11 
12  Parameters
13  ----------
14  root : `str`
15  Path to the top level of the data tree. This is expected to hold directories
16  named after the sensor names. They are expected to be lower case.
17  chip_name : `str`
18  The name of the sensor for which to read data.
19  chip_id : `int`
20  The identifier for the sensor in question.
21 
22  Returns
23  -------
24  `dict`
25  A dictionary of objects constructed from the appropriate factory class.
26  The key is the validity start time as a `datetime` object.
27  """
28  factory_map = {'qe_curve': Curve, 'defects': Defects, 'linearizer': Linearizer,
29  'crosstalk': CrosstalkCalib, 'bfk': BrighterFatterKernel}
30  files = []
31  extensions = (".ecsv", ".yaml")
32  for ext in extensions:
33  files.extend(glob.glob(os.path.join(root, chip_name, f"*{ext}")))
34  parts = os.path.split(root)
35  instrument = os.path.split(parts[0])[1] # convention is that these reside at <instrument>/<data_name>
36  data_name = parts[1]
37  if data_name not in factory_map:
38  raise ValueError(f"Unknown calibration data type, '{data_name}' found. "
39  f"Only understand {','.join(k for k in factory_map)}")
40  factory = factory_map[data_name]
41  data_dict = {}
42  for f in files:
43  date_str = os.path.splitext(os.path.basename(f))[0]
44  valid_start = dateutil.parser.parse(date_str)
45  data_dict[valid_start] = factory.readText(f)
46  check_metadata(data_dict[valid_start], valid_start, instrument, chip_id, f, data_name)
47  return data_dict, data_name
48 
49 
50 def check_metadata(obj, valid_start, instrument, chip_id, filepath, data_name):
51  """Check that the metadata is complete and self consistent
52 
53  Parameters
54  ----------
55  obj : object of same type as the factory
56  Object to retrieve metadata from in order to compare with
57  metadata inferred from the path.
58  valid_start : `datetime`
59  Start of the validity range for data
60  instrument : `str`
61  Name of the instrument in question
62  chip_id : `int`
63  Identifier of the sensor in question
64  filepath : `str`
65  Path of the file read to construct the data
66  data_name : `str`
67  Name of the type of data being read
68 
69  Returns
70  -------
71  None
72 
73  Raises
74  ------
75  ValueError
76  If the metadata from the path and the metadata encoded
77  in the path do not match for any reason.
78  """
79  md = obj.getMetadata()
80  finst = md['INSTRUME']
81  fchip_id = md['DETECTOR']
82  fdata_name = md['OBSTYPE']
83  if not ((finst.lower(), int(fchip_id), fdata_name.lower())
84  == (instrument.lower(), chip_id, data_name.lower())):
85  raise ValueError(f"Path and file metadata do not agree:\n"
86  f"Path metadata: {instrument} {chip_id} {data_name}\n"
87  f"File metadata: {finst} {fchip_id} {fdata_name}\n"
88  f"File read from : %s\n"%(filepath)
89  )
90 
91 
92 def read_all(root, camera):
93  """Read all data from the standard format at a particular root.
94 
95  Parameters
96  ----------
97  root : `str`
98  Path to the top level of the data tree. This is expected to hold directories
99  named after the sensor names. They are expected to be lower case.
100  camera : `lsst.afw.cameraGeom.Camera`
101  The camera that goes with the data being read.
102 
103  Returns
104  -------
105  dict
106  A dictionary of dictionaries of objects constructed with the appropriate factory class.
107  The first key is the sensor name lowered, and the second is the validity
108  start time as a `datetime` object.
109 
110  Notes
111  -----
112  Each leaf object in the constructed dictionary has metadata associated with it.
113  The detector ID may be retrieved from the DETECTOR entry of that metadata.
114  """
115  root = os.path.normpath(root)
116  dirs = os.listdir(root) # assumes all directories contain data
117  dirs = [d for d in dirs if os.path.isdir(os.path.join(root, d))]
118  data_by_chip = {}
119  name_map = {det.getName().lower(): det.getName() for
120  det in camera} # we assume the directories have been lowered
121 
122  if not dirs:
123  raise RuntimeError(f"No data found on path {root}")
124 
125  calib_types = set()
126  for d in dirs:
127  chip_name = os.path.basename(d)
128  # Give informative error message if the detector name is not known
129  # rather than a simple KeyError
130  if chip_name not in name_map:
131  detectors = [det for det in camera.getNameIter()]
132  max_detectors = 10
133  note_str = "knows"
134  if len(detectors) > max_detectors:
135  # report example subset
136  note_str = "examples"
137  detectors = detectors[:max_detectors]
138  raise RuntimeError(f"Detector {chip_name} not known to supplied camera "
139  f"{camera.getName()} ({note_str}: {','.join(detectors)})")
140  chip_id = camera[name_map[chip_name]].getId()
141  data_by_chip[chip_name], calib_type = read_one_chip(root, chip_name, chip_id)
142  calib_types.add(calib_type)
143  if len(calib_types) != 1: # set.add(None) has length 1 so None is OK here.
144  raise ValueError(f'Error mixing calib types: {calib_types}')
145 
146  no_data = all([v == {} for v in data_by_chip.values()])
147  if no_data:
148  raise RuntimeError("No data to ingest")
149 
150  return data_by_chip, calib_type
daf::base::PropertySet * set
Definition: fits.cc:912
bool all(CoordinateExpr< N > const &expr) noexcept
Return true if all elements are true.
def check_metadata(obj, valid_start, instrument, chip_id, filepath, data_name)
def read_one_chip(root, chip_name, chip_id)