Пример #1
0
 def help(self, stream_name):
     """Print help for a given stream"""
     try:
         self._print_stream_parameters(self.streams[stream_name])
     except KeyError:
         log.error("There is no stream called '{}'".format(stream_name))
         print("Available streams:\n{}".format(", ".join(
             s.stream for s in sorted(self.streams.values()))))
Пример #2
0
def runtable(det_id,
             n=5,
             run_range=None,
             target=None,
             compact=False,
             sep="\t",
             regex=None):
    """Print the run table of the last `n` runs for given detector"""
    runs = km3db.StreamDS(container="nt").get("runs", detid=det_id)

    if run_range is not None:
        try:
            from_run, to_run = [int(r) for r in run_range.split("-")]
        except ValueError:
            log.critical("Please specify a valid range (e.g. 3100-3200)!")
            raise SystemExit
        else:
            runs = [r for r in runs if from_run <= r.run <= to_run]

    if regex is not None:
        try:
            pattern = re.compile(regex)
        except re.error:
            log.error("Invalid regex!")
            return
        else:
            runs = [
                r for r in runs if re.match(pattern, r.runsetupname)
                or re.match(pattern, r.runsetupid)
            ]

    if target is not None:
        runs = [r for r in runs if r.jobtarget == target.capitalize()]

    if n is not None:
        runs = runs[-n:]

    if not runs:
        log.warning("No runs found.")
        return

    if compact:
        attributes = ["run", "runsetupname"]
        header = sep.join(attributes)

        def lineformatter(entry):
            return sep.join([str(getattr(entry, attr)) for attr in attributes])

    else:
        header = sep.join(runs[0]._fields)  # the dataset is homogenious

        def lineformatter(entry):
            return sep.join(map(str, entry))

    print(header)
    for entry in runs:
        print(lineformatter(entry))
Пример #3
0
def todetid(det_oid):
    """Convert det ID (e.g. 49) to det OID (e.g. D_ORCA006)

    If a det OID is provided it will simple be returned.
    """
    if isinstance(det_oid, int):
        return det_oid
    detectors = StreamDS(container="nt").get("detectors")
    for detector in detectors:
        if detector.oid == det_oid:
            return detector.serialnumber
    log.error("Could not convert det OID '{}' to ID".format(det_oid))
Пример #4
0
    def get(self, url, default=None, retry=True):
        "Get HTML content"
        target_url = self._db_url + "/" + km3db.compat.unquote(url)
        try:
            f = self.opener.open(target_url)
        except km3db.compat.HTTPError as e:
            if e.code == 403:
                if retry:
                    log.error(
                        "Access forbidden, your session has expired. "
                        "Deleting the cookie ({}) and retrying once.".format(
                            COOKIE_FILENAME))
                else:
                    log.critical("Access forbidden. Giving up...")
                    return default
                time.sleep(1)
                self.reset()
                os.remove(COOKIE_FILENAME)
                return self.get(url, default=default, retry=False)
            log.error("HTTP error: {}\n"
                      "Target URL: {}".format(e, target_url))
            return default
        try:
            content = f.read()
        except km3db.compat.IncompleteRead as icread:
            log.error("Incomplete data received from the DB.")
            content = icread.partial
        log.debug("Got {0} bytes of data.".format(len(content)))

        return content.decode("utf-8")
Пример #5
0
def detx_for_run(det_id, run):
    """Retrieve the calibrate detector file for given run"""
    run_table = StreamDS(container="nt").get("runs", detid=det_id)
    if run_table is None:
        log.error("No run table found for detector ID {}".format(det_id))
        return None

    for run_info in run_table:
        if run_info.run == run:
            break
    else:
        log.error("Run {} not found for detector {}".format(run, det_id))
        return None

    tcal = run_info.t0_calibsetid
    if str(tcal) == "nan":
        log.warning(
            "No time calibration found for run {} (detector {})".format(
                run, det_id))
        tcal = 0

    try:
        pcal = int(run_info.pos_calibsetid)
    except ValueError:
        log.warning(
            "No position calibration found for run {} (detector {})".format(
                run, det_id))
        pcal = 0

    try:
        rcal = int(run_info.rot_calibsetid)
    except ValueError:
        log.warning(
            "No rotation calibration found for run {} (detector {})".format(
                run, det_id))
        rcal = 0

    return detx(det_id, pcal=pcal, rcal=rcal, tcal=tcal)
Пример #6
0
    def get(self, stream, fmt="txt", container=None, renamemap=None, **kwargs):
        """Retrieve the data for a given stream manually

        Parameters
        ==========
        stream: str
          Name of the stream (e.g. detectors)
        fmt: str ("txt", "text", "bin")
          Retrieved raw data format, depends on the stream type
        container: str or None
          The container to wrap the returned data, as specified in
          `StreamDS`.
        """
        sel = "".join(["&{0}={1}".format(k, v) for (k, v) in kwargs.items()])
        url = "streamds/{0}.{1}?{2}".format(stream, fmt, sel[1:])
        data = self._db.get(url)
        if not data:
            log.error("No data found at URL '%s'." % url)
            return
        if data.startswith("ERROR"):
            log.error(data)
            return

        if container is None and self._default_container is not None:
            container = self._default_container

        try:
            if container == "pd":
                return topandas(data)
            if container == "nt":
                return tonamedtuples(stream.capitalize(),
                                     data,
                                     renamemap=renamemap)
        except ValueError:
            log.critical("Unable to convert data to container type '{}'. "
                         "Database response: {}".format(container, data))
        else:
            return data