LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
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)
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}
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)