def _setupRegistry(self, name, path, policy, policyKey, root): """Set up a registry (usually SQLite3), trying a number of possible paths. @param name (string) Name of registry @param path (string) Path for registry @param policyKey (string) Key in policy for registry path @param root (string) Root directory to look in @return (lsst.daf.butlerUtils.Registry) Registry object""" if path is None and policy.exists(policyKey): path = dafPersist.LogicalLocation( policy.getString(policyKey)).locString() if not os.path.exists(path): if not os.path.isabs(path) and root is not None: newPath = self._parentSearch(os.path.join(root, path)) if newPath is None: self.log.log(pexLog.Log.WARN, "Unable to locate registry at policy path (also looked in root): %s" % path) path = newPath else: self.log.log(pexLog.Log.WARN, "Unable to locate registry at policy path: %s" % path) path = None if path is None and root is not None: newPath = self._parentSearch(os.path.join(root, "%s_pgsql.py" % name)) if newPath is not None: pgsqlConf = PgSqlConfig() pgsqlConf.load(newPath) return PgSqlRegistry(pgsqlConf) if path is None and root is not None: path = os.path.join(root, "%s.sqlite3" % name) newPath = self._parentSearch(path) if newPath is None: self.log.log(pexLog.Log.WARN, "Unable to locate %s registry in root: %s" % (name, path)) path = newPath if path is None: path = os.path.join(".", "%s.sqlite3" % name) newPath = self._parentSearch(path) if newPath is None: self.log.log(pexLog.Log.WARN, "Unable to locate %s registry in current dir: %s" % (name, path)) path = newPath if path is not None: if not os.path.exists(path): newPath = self._parentSearch(path) if newPath is not None: path = newPath self.log.log(pexLog.Log.INFO, "Loading %s registry from %s" % (name, path)) registry = Registry.create(path) if registry is None: raise RuntimeError, "Unable to load %s registry from %s" % (name, path) return registry else: # TODO Try a FsRegistry(root) self.log.log(pexLog.Log.WARN, "No registry loaded; proceeding without one") return None
def queryCalibRegistry(what, filterName=None, summary=False): """Query a calib registry""" where = []; vals = [] if filterName: where.append('filter like ?') vals.append(filterName.replace("*", "%")) where = "WHERE " + " AND ".join(where) if where else "" query = """ SELECT validStart, validEnd, calibDate, filter, calibVersion, count(ccd) FROM %s %s GROUP BY filter, calibDate ORDER BY filter, calibDate """ % (what, where) n = {}; expTimes = {}; visits = {} if registryFile.endswith('sqlite3'): conn = sqlite.connect(registryFile) isSqlite = True else: pgsqlConf = PgSqlConfig() pgsqlConf.load(registryFile) conn = pgsql.connect(host=pgsqlConf.host, port=pgsqlConf.port, user=pgsqlConf.user, password=pgsqlConf.password, database=pgsqlConf.db) isSqlite = False cursor = conn.cursor() if not isSqlite: query = query.replace("?", "%s") if summary: print >> sys.stderr, "No summary is available for calib data" return else: print "%-10s--%-10s %-10s %-7s %-24s %4s" % ( "validStart", "validEnd", "calibDate", "filter", "calibVersion", "nCCD") if isSqlite: ret = cursor.execute(query, vals) else: cursor.execute(query, vals) ret = cursor.fetchall() for line in ret: validStart, validEnd, calibDate, filter, calibVersion, nCCD = line print "%-10s--%-10s %-10s %-7s %-24s %4d" % ( validStart, validEnd, calibDate, filter, calibVersion, nCCD) conn.close()
raise RuntimeError("Camera not recognised: %s" % camera) if os.path.exists(os.path.join(opts.root, 'calibRegistry_pgsql.py')) and havePgSql: isSqlite = False else: isSqlite = True if isSqlite: registry = os.path.join(opts.root, "calibRegistry.sqlite3") if os.path.exists(registry): os.unlink(registry) conn = sqlite.connect(registry) else: pgsqlConf = PgSqlConfig() pgsqlConf.load(os.path.join(opts.root, 'calibRegistry_pgsql.py')) conn = pgsql.connect(host=pgsqlConf.host, port=pgsqlConf.port, user=pgsqlConf.user, password=pgsqlConf.password, database=pgsqlConf.db) cur = conn.cursor() Row = collections.namedtuple("Row", ["calibDate", "calibVersion", "ccd"]) for calib in ('bias', 'dark', 'flat', 'fringe'): if isSqlite: cmd = "create table " + calib.lower( ) + " (id integer primary key autoincrement" cmd += ", validStart text, validEnd text"
def queryRegistry(field=None, visit=None, filterName=None, summary=False): """Query an input registry""" where = []; vals = [] if field: where.append('field like ?') vals.append(field.replace("*", "%")) if filterName: where.append('filter like ?') vals.append(filterName.replace("*", "%")) if visit: where.append("visit = ?") vals.append(visit) where = "WHERE " + " AND ".join(where) if where else "" query = """ SELECT max(field), visit, max(filter), max(expTime), max(dateObs), max(pointing), count(ccd) FROM raw %s GROUP BY visit ORDER BY max(filter), visit """ % (where) n = {}; expTimes = {}; visits = {} if registryFile.endswith('sqlite3'): conn = sqlite.connect(registryFile) isSqlite = True else: pgsqlConf = PgSqlConfig() pgsqlConf.load(registryFile) conn = pgsql.connect(host=pgsqlConf.host, port=pgsqlConf.port, user=pgsqlConf.user, password=pgsqlConf.password, database=pgsqlConf.db) isSqlite = False cursor = conn.cursor() if args.summary: print "%-7s %-20s %7s %s" % ("filter", "field", "expTime", "visit") else: print "%-7s %-20s %10s %7s %8s %6s %4s" % ("filter", "field", "dataObs", "expTime", "pointing", "visit", "nCCD") if not isSqlite: query = query.replace("?", "%s") if isSqlite: ret = cursor.execute(query, vals) else: cursor.execute(query, vals) ret = cursor.fetchall() for line in ret: field, visit, filter, expTime, dateObs, pointing, nCCD = line if summary: k = (filter, field) if not n.get(k): n[k] = 0 expTimes[k] = 0 visits[k] = [] n[k] += 1 expTimes[k] += expTime visits[k].append(visit) else: print "%-7s %-20s %10s %7.1f %8d %6d %4d" % (filter, field, dateObs, expTime, pointing, visit, nCCD) conn.close() if summary: for k in sorted(n.keys()): filter, field = k print "%-7s %-20s %7.1f %s" % (filter, field, expTimes[k], formatVisits(visits[k]))
if opts.camera.lower() not in ("suprime-cam", "suprimecam", "sc", "hsc", "hscsim"): raise RuntimeError("Camera not recognised: %s" % camera) if os.path.exists(os.path.join(opts.root, 'calibRegistry_pgsql.py')) and havePgSql: isSqlite = False else: isSqlite = True if isSqlite: registry = os.path.join(opts.root, "calibRegistry.sqlite3") if os.path.exists(registry): os.unlink(registry) conn = sqlite.connect(registry) else: pgsqlConf = PgSqlConfig() pgsqlConf.load(os.path.join(opts.root, 'calibRegistry_pgsql.py')) conn = pgsql.connect(host=pgsqlConf.host, port=pgsqlConf.port, user=pgsqlConf.user, password=pgsqlConf.password, database=pgsqlConf.db) cur = conn.cursor() Row = collections.namedtuple("Row", ["calibDate", "calibVersion", "ccd"]) for calib in ('bias', 'dark', 'flat', 'fringe'): if isSqlite: cmd = "create table " + calib.lower() + " (id integer primary key autoincrement" cmd += ", validStart text, validEnd text" cmd += ", calibDate text, filter text, calibVersion text, ccd int" cmd += ")" conn.execute(cmd)