LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
utils.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008, 2009, 2010 LSST Corporation.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 
23 from optparse import OptionParser
24 import os
25 import sys
26 
27 import lsst.afw.geom as afwGeom
28 import lsst.pex.policy as pexPolicy
29 import lsst.daf.persistence as dafPersist
30 from lsst.pex.harness.simpleStageTester import SimpleStageTester
31 
32 haveCfht = True
33 haveLsstSim = True
34 try:
35  from lsst.obs.cfht import CfhtMapper
36 except:
37  haveCfht = False
38 try:
39  from lsst.obs.lsstSim import LsstSimMapper
40 except:
41  haveLsstSim = False
42 
43 
44 def runStage(stage, policyString, clip):
45  if policyString.startswith("#<?cfg "):
46  policyString = pexPolicy.PolicyString(policyString)
47  pol = pexPolicy.Policy.createPolicy(policyString)
48  sst = SimpleStageTester(stage(pol))
49  return sst.runWorker(clip)
50 
51 def cfhtMain(processFunction, outDatasetType, need=(), defaultRoot="."):
52  parser = OptionParser()
53  parser.add_option("-i", "--input", dest="root",
54  default=defaultRoot, help="input root")
55  parser.add_option("-o", "--output", dest="outRoot", default=".",
56  help="output root")
57  parser.add_option("-f", "--force", action="store_true", default=False,
58  help="execute even if output dataset exists")
59  if "calib" in need:
60  parser.add_option("-C", "--calibRoot", dest="calibRoot",
61  help="calibration root")
62  parser.add_option("-R", "--registry", help="registry")
63  if "skyTile" in need:
64  parser.add_option("-t", "--skyTile", action="append", type="int",
65  help="sky tile numbers (can be repeated)")
66  else:
67  parser.add_option("-v", "--visit", action="append", type="int",
68  help="visit numbers (can be repeated)")
69  if "ccd" in need or "amp" in need:
70  parser.add_option("-c", "--ccd", action="append", type="int",
71  help="ccd number (can be repeated)")
72  if "amp" in need:
73  parser.add_option("-a", "--amp", action="append", type="int",
74  help="amp number (can be repeated)")
75  (options, args) = parser.parse_args()
76 
77  if options.registry is None:
78  if os.path.exists(os.path.join(options.root, "registry.sqlite3")):
79  options.registry = os.path.join(options.root, "registry.sqlite3")
80  if options.registry is None:
81  if os.path.exists("/lsst/DC3/data/obstest/CFHTLS/registry.sqlite3"):
82  options.registry = "/lsst/DC3/data/obstest/CFHTLS/registry.sqlite3"
83  if "calib" in need:
84  if options.calibRoot is None:
85  if os.path.exists("/lsst/DC3/data/obstest/CFHTLS/calib"):
86  options.calibRoot = "/lsst/DC3/data/obstest/CFHTLS/calib"
87  bf = dafPersist.ButlerFactory(mapper=CfhtMapper(
88  root=options.root, calibRoot=options.calibRoot,
89  registry=options.registry))
90  else:
91  bf = dafPersist.ButlerFactory(mapper=CfhtMapper(
92  root=options.root, registry=options.registry))
93  inButler = bf.create()
94  obf = dafPersist.ButlerFactory(mapper=CfhtMapper(
95  root=options.outRoot, registry=options.registry))
96  outButler = obf.create()
97 
98  if "skyTile" in need:
99  if options.skyTile is None:
100  print >>sys.stderr, "Running over all sky tiles"
101  options.skyTile = inButler.queryMetadata("raw", "skyTile")
102  elif not hasattr(options.skyTile, "__iter__"):
103  options.skyTile = [options.skyTile]
104  for skyTile in options.skyTile:
105  if options.force or not outButler.datasetExists(outDatasetType,
106  skyTile=skyTile):
107  print >>sys.stderr, \
108  "***** Processing skyTile %d" % (skyTile,)
109  processFunction(inButler=inButler, outButler=outButler,
110  skyTile=skyTile)
111  return
112 
113  if options.visit is None:
114  print >>sys.stderr, "Running over all input visits"
115  options.visit = inButler.queryMetadata("raw", "visit")
116  elif not hasattr(options.visit, "__iter__"):
117  options.visit = [options.visit]
118  if "ccd" in need or "amp" in need:
119  if options.ccd is None:
120  print >>sys.stderr, "Running over all CCDs"
121  options.ccd = inButler.queryMetadata("raw", "ccd")
122  elif not hasattr(options.ccd, "__iter__"):
123  options.ccd = [options.ccd]
124  if "amp" in need:
125  if options.amp is None:
126  print >>sys.stderr, "Running over all amps"
127  options.amp = inButler.queryMetadata("raw", "amp")
128  elif not hasattr(options.amp, "__iter__"):
129  options.amp = [options.amp]
130 
131  for visit in options.visit:
132  if "ccd" in need or "amp" in need:
133  for ccd in options.ccd:
134  if "amp" in need:
135  for amp in options.amp:
136  if options.force or \
137  not outButler.datasetExists(outDatasetType,
138  visit=visit, ccd=ccd, amp=amp):
139  print >>sys.stderr, \
140  "***** Processing visit %d ccd %d amp %d" % \
141  (visit, ccd, amp)
142  processFunction(inButler=inButler,
143  outButler=outButler,
144  visit=visit, ccd=ccd, amp=amp)
145  else:
146  if options.force or \
147  not outButler.datasetExists(outDatasetType,
148  visit=visit, ccd=ccd):
149  print >>sys.stderr, \
150  "***** Processing visit %d ccd %d" % \
151  (visit, ccd)
152  processFunction(inButler=inButler, outButler=outButler,
153  visit=visit, ccd=ccd)
154  else:
155  if options.force or \
156  not outButler.datasetExists(outDatasetType, visit=visit):
157  print >>sys.stderr, "***** Processing visit %d" % (visit,)
158  processFunction(inButler=inButler, outButler=outButler,
159  visit=visit)
160 
161 
162 def lsstSimMain(processFunction, outDatasetType, need=(), defaultRoot="."):
163  parser = OptionParser()
164  parser.add_option("-i", "--input", dest="root",
165  default=defaultRoot, help="input root")
166  parser.add_option("-o", "--output", dest="outRoot", default=".",
167  help="output root")
168  parser.add_option("-f", "--force", action="store_true", default=False,
169  help="execute even if output dataset exists")
170  if "calib" in need:
171  parser.add_option("-C", "--calibRoot", dest="calibRoot",
172  help="calibration root")
173  parser.add_option("-R", "--registry", help="registry")
174  if "skyTile" in need:
175  parser.add_option("-t", "--skyTile", action="append", type="int",
176  help="sky tile numbers (can be repeated)")
177  else:
178  parser.add_option("-v", "--visit", action="append", type="int",
179  help="visit number (can be repeated)")
180  if "snap" in need:
181  parser.add_option("-S", "--snap", action="append", type="int",
182  help="snap number (can be repeated)")
183  if "sensor" in need or "channel" in need:
184  parser.add_option("-r", "--raft", action="append",
185  help="raft coords (can be repeated)")
186  parser.add_option("-s", "--sensor", action="append",
187  help="sensor coords (can be repeated)")
188  if "channel" in need:
189  parser.add_option("-a", "--channel", action="append",
190  help="channel coords (can be repeated)")
191  (options, args) = parser.parse_args()
192 
193  if options.registry is None:
194  if os.path.exists(os.path.join(options.root, "registry.sqlite3")):
195  options.registry = os.path.join(options.root, "registry.sqlite3")
196  elif os.path.exists("/lsst/DC3/data/obstest/ImSim/registry.sqlite3"):
197  options.registry = "/lsst/DC3/data/obstest/ImSim/registry.sqlite3"
198  if "calib" in need:
199  if os.path.exists("/lsst/DC3/data/obstest/ImSim"):
200  options.calibRoot = "/lsst/DC3/data/obstest/ImSim"
201  bf = dafPersist.ButlerFactory(mapper=LsstSimMapper(
202  root=options.root, calibRoot=options.calibRoot,
203  registry=options.registry))
204  else:
205  bf = dafPersist.ButlerFactory(mapper=LsstSimMapper(
206  root=options.root, registry=options.registry))
207  inButler = bf.create()
208  obf = dafPersist.ButlerFactory(mapper=LsstSimMapper(
209  root=options.outRoot, registry=options.registry))
210  outButler = obf.create()
211 
212  if "skyTile" in need:
213  if options.skyTile is None:
214  print >>sys.stderr, "Running over all sky tiles"
215  options.skyTile = inButler.queryMetadata("raw", "skyTile")
216  elif not hasattr(options.skyTile, "__iter__"):
217  options.skyTile = [options.skyTile]
218  for skyTile in options.skyTile:
219  if options.force or not outButler.datasetExists(outDatasetType,
220  skyTile=skyTile):
221  print >>sys.stderr, \
222  "***** Processing skyTile %d" % (skyTile,)
223  processFunction(inButler=inButler, outButler=outButler,
224  skyTile=skyTile)
225  return
226 
227  if options.visit is None:
228  print >>sys.stderr, "Running over all input visits"
229  options.visit = inButler.queryMetadata("raw", "visit")
230  elif not hasattr(options.visit, "__iter__"):
231  options.visit = [options.visit]
232 
233  if "snap" in need:
234  if options.snap is None:
235  print >>sys.stderr, "Running over all snaps"
236  options.snap = inButler.queryMetadata("raw", "snap")
237  elif not hasattr(options.snap, "__iter__"):
238  options.snap = [options.snap]
239  else:
240  setattr(options, "snap", [0])
241 
242  if "sensor" in need or "channel" in need:
243  if options.raft is None:
244  print >>sys.stderr, "Running over all rafts"
245  options.raft = inButler.queryMetadata("raw", "raft")
246  elif not hasattr(options.raft, "__iter__"):
247  options.raft = [options.raft]
248 
249  if "sensor" in need or "channel" in need:
250  if options.sensor is None:
251  print >>sys.stderr, "Running over all sensors"
252  options.sensor = inButler.queryMetadata("raw", "sensor")
253  elif not hasattr(options.sensor, "__iter__"):
254  options.sensor = [options.sensor]
255 
256  if "channel" in need:
257  if options.channel is None:
258  print >>sys.stderr, "Running over all channels"
259  options.channel = inButler.queryMetadata("raw", "channel")
260  elif not hasattr(options.channel, "__iter__"):
261  options.channel = [options.channel]
262 
263  for visit in options.visit:
264  if "sensor" in need or "channel" in need:
265  if "snap" in need:
266  for snap in options.snap:
267  for raft in options.raft:
268  for sensor in options.sensor:
269  if "channel" in need:
270  for channel in options.channel:
271  if options.force or \
272  not outButler.datasetExists(
273  outDatasetType,
274  visit=visit, snap=snap,
275  raft=raft, sensor=sensor,
276  channel=channel):
277  print >>sys.stderr, \
278  ("***** Processing " + \
279  "visit %d snap %d raft %s " + \
280  "sensor %s channel %s") % \
281  (visit, snap, raft, sensor,
282  channel)
283  processFunction(inButler=inButler,
284  outButler=outButler,
285  visit=visit, snap=snap,
286  raft=raft, sensor=sensor,
287  channel=channel)
288  else:
289  if options.force or \
290  not outButler.datasetExists(
291  outDatasetType,
292  visit=visit, snap=snap,
293  raft=raft, sensor=sensor):
294  print >>sys.stderr, \
295  ("***** Processing visit %d " + \
296  "snap %d raft %s sensor %s") % \
297  (visit, snap, raft, sensor)
298  processFunction(inButler=inButler,
299  outButler=outButler, visit=visit,
300  snap=snap, raft=raft, sensor=sensor)
301  else: # snap
302  for raft in options.raft:
303  for sensor in options.sensor:
304  if "channel" in need:
305  for channel in options.channel:
306  if options.force or \
307  not outButler.datasetExists(
308  outDatasetType, visit=visit,
309  raft=raft, sensor=sensor,
310  channel=channel):
311  print >>sys.stderr, \
312  ("***** Processing visit %d " + \
313  "raft %s sensor %s channel %s") % \
314  (visit, raft, sensor, channel)
315  processFunction(inButler=inButler,
316  outButler=outButler,
317  visit=visit, raft=raft,
318  sensor=sensor, channel=channel)
319  else:
320  if options.force or \
321  not outButler.datasetExists(outDatasetType,
322  visit=visit, raft=raft,
323  sensor=sensor):
324  print >>sys.stderr, \
325  ("***** Processing visit %d " + \
326  "raft %s sensor %s") % \
327  (visit, raft, sensor)
328  processFunction(inButler=inButler,
329  outButler=outButler, visit=visit,
330  raft=raft, sensor=sensor)
331  else: # raft, sensor
332  if options.force or \
333  not outButler.datasetExists(outDatasetType, visit=visit):
334  print >>sys.stderr, "***** Processing visit %d" % (visit,)
335  processFunction(inButler=inButler, outButler=outButler,
336  visit=visit)
337 
338 def cfhtSetup(root, outRoot, registry, calibRoot, inButler, outButler):
339  if inButler is None:
340  if calibRoot is None:
341  if os.path.exists("/lsst/DC3/data/obstest/CFHTLS/calib"):
342  calibRoot = "/lsst/DC3/data/obstest/CFHTLS/calib"
343  if registry is None and root is not None:
344  if os.path.exists(os.path.join(root, "registry.sqlite3")):
345  registry = os.path.join(root, "registry.sqlite3")
346  bf = dafPersist.ButlerFactory(mapper=CfhtMapper(
347  root=root, calibRoot=calibRoot, registry=registry))
348  inButler = bf.create()
349  if outButler is None:
350  if outRoot is None:
351  outRoot = root
352  obf = dafPersist.ButlerFactory(mapper=CfhtMapper(
353  root=outRoot, registry=registry))
354  outButler = obf.create()
355  return (inButler, outButler)
356 
357 def getDataset(butler, dataset, dataId, strict, warn):
358  """Get a dataset from a repository with an optional exception or warning if not found
359 
360  @param[in] butler: data butler
361  @param[in] dataset: name of desired dataset
362  @param[in] dataId: data ID dict
363  @param[in] strict: if True then raise RuntimeError if dataset not found
364  @param[in] warn: if True and strict False then print a warning to stderr if dataset not found
365 
366  @raise RuntimeError if dataset not found and strict true
367  """
368  try:
369  ds = butler.get(dataset, dataId=dataId, immediate=True)
370  except:
371  ds = None
372  if ds == None:
373  msg = '{} : Failed to retrieve {} dataset'.format(dataId, dataset)
374  if strict:
375  raise RuntimeError(msg)
376  elif warn:
377  print >>sys.stderr, '*** Skipping ' + msg
378  return ds
379 
380 def getPsf(butler, dataset, dataId, strict, warn):
381  """Get the PSF from a repository without reading (very much of) the exposure
382 
383  @param[in] butler: data butler
384  @param[in] dataset: name of desired dataset
385  @param[in] dataId: data ID dict of exposure containing desired PSF
386  @param[in] strict: if True then raise RuntimeError if psf not found
387  @param[in] warn: if True and strict False then print a warning to stderr if psf not found
388 
389  @raise RuntimeError if exposure not found (regardless of strict)
390  @raise RuntimeError if exposure has no PSF and strict true
391  """
392  # there is not yet a way to read just the PSF, so read a 1x1 subregion of the exposure
393  tinyBBox = afwGeom.Box2I(afwGeom.Point2I(0,0), afwGeom.Extent2I(1,1))
394  tinyExposure = butler.get(dataset + "_sub", dataId=dataId, bbox=tinyBBox, imageOrigin="LOCAL", immediate=True)
395  psf = tinyExposure.getPsf()
396  if psf is None:
397  msg = '%s : %s exposure had no PSF' % (dataId, dataset)
398  psf = None
399  if strict:
400  raise RuntimeError(msg)
401  elif warn:
402  print >>sys.stderr, '*** Skipping ' + msg
403  return psf
404 
405 def lsstSimSetup(root, outRoot, registry, calibRoot, inButler, outButler):
406  if inButler is None:
407  if calibRoot is None:
408  if os.path.exists("/lsst/DC3/data/obstest/ImSim"):
409  calibRoot = "/lsst/DC3/data/obstest/ImSim"
410  if registry is None and root is not None:
411  if os.path.exists(os.path.join(root, "registry.sqlite3")):
412  registry = os.path.join(root, "registry.sqlite3")
413  bf = dafPersist.ButlerFactory(mapper=LsstSimMapper(
414  root=root, calibRoot=calibRoot, registry=registry))
415  inButler = bf.create()
416  if outButler is None:
417  if outRoot is None:
418  outRoot = root
419  obf = dafPersist.ButlerFactory(mapper=LsstSimMapper(
420  root=outRoot, registry=registry))
421  outButler = obf.create()
422  return (inButler, outButler)
An integer coordinate rectangle.
Definition: Box.h:53
a representation of a string containing Policy parameter data
Definition: PolicyString.h:52