Coverage for python/lsst/obs/lsst/script/generateCamera.py: 5%
178 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-05 04:38 -0700
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-05 04:38 -0700
1#!/usr/bin/env python
2# This file is part of obs_lsst.
3#
4# Developed for the LSST Data Management System.
5# This product includes software developed by the LSST Project
6# (http://www.lsst.org).
7# See the COPYRIGHT file at the top-level directory of this distribution
8# for details of code ownership.
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the LSST License Statement and
21# the GNU General Public License along with this program. If not,
22# see <http://www.lsstcorp.org/LegalNotices/>.
23#
25__all__ = ("main",)
27import argparse
28import os
29import sys
30import shutil
31import yaml
32import numpy as np
35def findYamlOnPath(fileName, searchPath):
36 """Find and return a file somewhere in the directories listed in
37 searchPath"""
38 for d in searchPath:
39 f = os.path.join(d, fileName)
40 if os.path.exists(f):
41 return f
43 raise FileNotFoundError("Unable to find %s on path %s" % (fileName, ":".join(searchPath)))
46def parseYamlOnPath(fileName, searchPath):
47 """Find the named file in search path, parse the YAML, and return contents.
48 """
49 yamlFile = findYamlOnPath(fileName, searchPath)
50 with open(yamlFile) as fd:
51 content = yaml.load(fd, Loader=yaml.CSafeLoader)
52 return content
55def build_argparser():
56 """Construct an argument parser for the ``generateCamera.py`` script.
58 Returns
59 -------
60 argparser : `argparse.ArgumentParser`
61 The argument parser that defines the ``translate_header.py``
62 command-line interface.
63 """
65 parser = argparse.ArgumentParser(description="""
66 Generate a camera.yaml file for a camera by assembling descriptions of
67 rafts, sensors, etc.
69 Because we have many similar cameras, the assembly uses a :-separated
70 search path of directories to find desired information. The _first_
71 occurrence of a filename is used.
72 """)
74 parser.add_argument('outputFile', type=str, help="Name of generated file")
75 parser.add_argument('--path', type=str, help="List of directories to search for components",
76 default=False)
77 parser.add_argument('--verbose', action="store_true", help="How chatty should I be?", default=False)
79 return parser
82def applyRaftYaw(offset, raftYaw):
83 """Apply raft yaw angle to internal offsets of the CCDs.
85 Parameters
86 ----------
87 offset : `list` of `float`
88 A list of the offsets to rotate: [x, y, z]
89 raftYaw : `float`
90 Raft yaw angle in degrees.
92 Returns
93 -------
94 offsets : `list` of `float
95 3-item sequence of floats containing the rotated offsets.
96 """
97 if raftYaw == 0.:
98 return offset
99 new_offset = np.zeros(3, dtype=float)
100 sinTheta = np.sin(np.radians(raftYaw))
101 cosTheta = np.cos(np.radians(raftYaw))
102 new_offset[0] = cosTheta*offset[0] - sinTheta*offset[1]
103 new_offset[1] = sinTheta*offset[0] + cosTheta*offset[1]
104 new_offset[2] = offset[2]
105 return new_offset
108def generateCamera(cameraFile, path):
109 """Generate a combined camera YAML definition from component parts.
111 Parameters
112 ----------
113 cameraFile : `str`
114 Path to output YAML file.
115 path : `str` or `list` of `str`
116 List of directories to search for component YAML files or a
117 colon-separated path string. If relative paths are given they will be
118 converted to absolute path by combining with directory specified with
119 the output ``cameraFile``.
120 """
121 cameraFileDir = os.path.dirname(cameraFile)
122 # In some places, it's convenient to have aliases to rafts that should be
123 # removed in the built camera.
124 raftNameMap = {'R00W': 'R00', 'R44W': 'R44', 'R04W': 'R04', 'R40W': 'R40'}
126 if not cameraFile.endswith(".yaml"):
127 raise RuntimeError(f"Output file name ({cameraFile}) does not end with .yaml")
129 if isinstance(path, str):
130 path = path.split(":")
131 searchPath = [os.path.join(cameraFileDir, d) for d in path]
133 cameraSkl = parseYamlOnPath("cameraHeader.yaml", searchPath)
134 cameraTransforms = parseYamlOnPath("cameraTransforms.yaml", searchPath)
135 raftData = parseYamlOnPath("rafts.yaml", searchPath)
136 ccdData = parseYamlOnPath("ccdData.yaml", searchPath)
138 # See if we have an override of the name
139 try:
140 nameYaml = parseYamlOnPath("name.yaml", searchPath)
141 except FileNotFoundError:
142 nameOverride = None
143 else:
144 nameOverride = nameYaml["name"]
146 # Copy the camera header, replacing the name if needed. We can not
147 # write out the cameraSkl dataset because that will expand all the
148 # YAML references. We must edit the file itself.
149 inputHeader = findYamlOnPath("cameraHeader.yaml", searchPath)
150 if nameOverride:
151 with open(inputHeader) as infd:
152 with open(cameraFile, "w") as outfd:
153 replaced = False
154 for line in infd:
155 if not replaced and line.startswith("name :") or line.startswith("name:"):
156 line = f"name : {nameOverride}\n"
157 replaced = True
158 print(line, file=outfd, end="")
159 if not replaced:
160 raise RuntimeError(f"Override name {nameOverride} specified but no name"
161 f" to replace in {inputHeader}")
162 else:
163 shutil.copyfile(inputHeader, cameraFile)
165 nindent = 0 # current number of indents
167 def indent():
168 """Return the current indent string"""
169 dindent = 2 # number of spaces per indent
170 return(nindent*dindent - 1)*" " # print will add the extra " "
172 with open(cameraFile, "a") as fd:
173 print("""
174#
175# Specify the geometrical transformations relevant to the camera in all appropriate
176# (and known!) coordinate systems
177#""", file=fd)
178 for k, v in cameraTransforms.items():
179 print("%s : %s" % (k, v), file=fd)
181 print("""
182#
183# Define our specific devices
184#
185# All the CCDs present in this file
186#
187CCDs :\
188""", file=fd)
190 for raftName, perRaftData in raftData["rafts"].items():
191 try:
192 raftCcdData = parseYamlOnPath(f"{raftName}.yaml", searchPath)[raftName]
193 except FileNotFoundError:
194 print("Unable to load CCD descriptions for raft %s" % raftName, file=sys.stderr)
195 continue
197 try:
198 detectorType = raftCcdData["detectorType"]
199 except KeyError:
200 raise RuntimeError("Unable to lookup detector type for %s" % raftName)
202 try:
203 _ccds = cameraSkl['RAFT_%s' % detectorType]["ccds"] # describe this *type* of raft
204 except KeyError:
205 raise RuntimeError("No raft for detector type %s" % detectorType)
207 try:
208 sensorTypes = raftCcdData["sensorTypes"]
209 except KeyError:
210 sensorTypes = None
212 # only include CCDs in the raft for which we have a serial
213 # (the value isn't checked)
214 ccds = {}
215 for ccdName in raftCcdData["ccdSerials"]:
216 try:
217 ccds[ccdName] = _ccds[ccdName]
218 except KeyError:
219 raise RuntimeError("Unable to look up CCD %s in %s" %
220 (ccdName, 'RAFT_%s' % detectorType))
221 del _ccds
223 try:
224 amps = cameraSkl['CCD_%s' % detectorType]["amplifiers"] # describe this *type* of ccd
225 except KeyError:
226 raise RuntimeError("Unable to lookup amplifiers for CCD type CCD_%s" % detectorType)
228 try:
229 crosstalkCoeffs = ccdData["crosstalk"][detectorType]
230 except KeyError:
231 crosstalkCoeffs = None
233 nindent += 1
235 raftOffset = perRaftData["offset"]
236 if len(raftOffset) == 2:
237 raftOffset.append(0.0) # Default offset_z is 0.0
238 id0 = perRaftData['id0']
239 try:
240 raftYaw = perRaftData['yaw']
241 except KeyError:
242 raftYaw = 0.
243 geometryWithinRaft = raftCcdData.get('geometryWithinRaft', {})
245 for ccdName, ccdLayout in ccds.items():
246 if ccdName in geometryWithinRaft:
247 doffset = geometryWithinRaft[ccdName]['offset']
248 if len(doffset) == 2:
249 doffset.append(0.0) # Default offset_z is 0.0
250 yaw = geometryWithinRaft[ccdName]['yaw'] + raftYaw
251 else:
252 doffset = (0.0, 0.0, 0.0)
253 yaw = None
255 print(indent(), "%s_%s : " % (raftNameMap.get(raftName, raftName), ccdName), file=fd)
256 nindent += 1
257 print(indent(), "<< : *%s_%s" % (ccdName, detectorType), file=fd)
258 if sensorTypes is not None:
259 print(indent(), "detectorType : %i" % (sensorTypes[ccdName]), file=fd)
260 print(indent(), "id : %s" % (id0 + ccdLayout['id']), file=fd)
261 print(indent(), "serial : %s" % (raftCcdData['ccdSerials'][ccdName]), file=fd)
262 print(indent(), "physicalType : %s" % (detectorType), file=fd)
263 print(indent(), "refpos : %s" % (ccdLayout['refpos']), file=fd)
264 if len(ccdLayout['offset']) == 2:
265 ccdLayout['offset'].append(0.0) # Default offset_z is 0.0
266 ccdLayoutOffset = applyRaftYaw([el1+el2 for el1, el2 in zip(ccdLayout['offset'], doffset)],
267 raftYaw)
268 print(indent(), "offset : [%g, %g, %g]" % (ccdLayoutOffset[0] + raftOffset[0],
269 ccdLayoutOffset[1] + raftOffset[1],
270 ccdLayoutOffset[2] + raftOffset[2]),
271 file=fd)
272 if yaw is not None:
273 print(indent(), "yaw : %g" % (yaw), file=fd)
275 if crosstalkCoeffs is not None:
276 print(indent(), "crosstalk : [", file=fd)
277 nindent += 1
278 print(indent(), file=fd, end="")
279 for iAmp in amps:
280 for jAmp in amps:
281 print("%11.3e," % crosstalkCoeffs[iAmp][jAmp], file=fd, end='')
282 print(file=fd, end="\n" + indent())
283 nindent -= 1
284 print("]", file=fd)
286 try:
287 amplifierData = raftCcdData['amplifiers'][ccdName]
288 except KeyError:
289 raise RuntimeError("Unable to lookup amplifier data for detector %s_%s" %
290 (raftName, ccdName))
292 print(indent(), "amplifiers :", file=fd)
293 nindent += 1
294 for ampName, ampData in amps.items():
295 print(indent(), "%s :" % ampName, file=fd)
297 if ampName not in amplifierData:
298 raise RuntimeError("Unable to lookup amplifier data for amp %s in detector %s_%s" %
299 (ampName, raftName, ccdName))
301 nindent += 1
302 print(indent(), "<< : *%s_%s" % (ampName, detectorType), file=fd)
303 print(indent(), "gain : %g" % (amplifierData[ampName]['gain']), file=fd)
304 print(indent(), "readNoise : %g" % (amplifierData[ampName]['readNoise']), file=fd)
305 saturation = amplifierData[ampName].get('saturation')
306 if saturation: # if known, override the per-CCD-type default from cameraHeader.yaml
307 print(indent(), "saturation : %g" % (saturation), file=fd)
308 nindent -= 1
309 nindent -= 1
311 nindent -= 1
313 nindent -= 1
316def main():
317 args = build_argparser().parse_args()
319 try:
320 generateCamera(args.outputFile, args.path)
321 except Exception as e:
322 print(f"{e}", file=sys.stderr)
323 return 1
324 return 0