Beispiel #1
0
def _get_Qs(Q, res, name, showall):
    try:
        srsQs = res[name].srs.srs.keys()
    except AttributeError:
        return None
    srsQs = list(srsQs)
    if Q == "auto":
        Qs = srsQs
    else:
        Q_in = n2p._ensure_iter(Q)
        Qs = []
        for q in Q_in:
            if q in srsQs:
                Qs.append(q)
            else:
                warnings.warn(f"no Q={q} SRS data for {name}", RuntimeWarning)
        if len(Qs) == 0:
            return None
    Qs = n2p._ensure_iter(Qs)
    if len(Qs) > 1 and showall:
        raise ValueError("`Q` must be a scalar if `showall` is true")
    return Qs
Beispiel #2
0
    def _handle_defaults(self, name):
        """Handle default values and take default actions"""
        # first, the defaults:
        ns = self[name]

        if ns.drfunc is None:
            ns.drfunc = name

        if ns.drfile is None:
            # this is set to defaults only if it is in defaults,
            # otherwise, leave it None
            ns.drfile = self.defaults.get("drfile", None)

        if ns.se is None:
            ns.se = self.defaults.get("se", 0)

        if ns.uf_reds is None:
            ns.uf_reds = self.defaults.get("uf_reds", (1, 1, 1, 1))

        if ns.srsQs is None:
            for k, v in ns.__dict__.items():
                if k.startswith("srs") and v is not None:
                    ns.srsQs = self.defaults
                    break

        if ns.srsfrq is None and ns.srsQs is not None:
            ns.srsfrq = self.defaults

        dct = ns.__dict__
        for key in dct:
            if dct[key] is self.defaults:
                try:
                    dct[key] = self.defaults[key]
                except KeyError:
                    msg = f"{key} set to `defaults` but is not found in `defaults`!"
                    raise ValueError(msg)

        if ns.drfile == ".":
            ns.drfile = None

        # add path to `drfile` if needed:
        if _is_valid_identifier(ns.drfunc):
            try:
                ns.drfile = self._drfilemap[ns.drfile]
            except KeyError:
                calling_file = os.path.realpath(inspect.stack()[2][1])
                if ns.drfile is None:
                    ns.drfile = drfile = calling_file
                else:
                    drfile = ns.drfile
                    callerdir = os.path.dirname(calling_file)
                    ns.drfile = os.path.join(callerdir, drfile)
                self._drfilemap[drfile] = ns.drfile
            self._check_for_drfunc(ns.drfile, ns.drfunc)

        # ensure uf_reds has no None values:
        ns.uf_reds = _merge_uf_reds((1, 1, 1, 1), ns.uf_reds)

        # next, the default actions:
        if ns.desc is None:
            ns.desc = name

        if isinstance(ns.labels, numbers.Integral):
            ns.labels = [f"Row {i + 1:6d}" for i in range(ns.labels)]

        # check filter value:
        try:
            nf = len(ns.filterval)
        except TypeError:
            pass
        else:
            if nf != len(ns.labels):
                raise ValueError(
                    f"length of `filterval` ({nf}) does not match length of"
                    f"labels ({len(ns.labels)})")
            ns.filterval = np.atleast_1d(ns.filterval)

        ns.ignorepv = self._get_pv(ns.ignorepv, "ignorepv", len(ns.labels))

        if ns.srsconv is None and ns.srsQs is not None:
            ns.srsconv = 1.0

        if ns.srsQs is not None:
            ns.srsQs = n2p._ensure_iter(ns.srsQs)
            if ns.srspv is None:
                ns.srspv = slice(len(ns.labels))

        if ns.srspv is not None and ns.srsopts is None:
            ns.srsopts = {}

        # fill in hist-labels/units and srs-labels/units if needed:
        for i in ("hist", "srs"):
            pv = i + "pv"
            dct[pv] = self._get_pv(dct[pv], pv, len(ns.labels))
            if dct[pv] is not None:
                lbl = i + "labels"
                unt = i + "units"
                if dct[lbl] is None:
                    dct[lbl] = self._get_labels(dct[pv], ns.labels)
                if dct[unt] is None:
                    dct[unt] = ns.units

        # ensure that the labels are lists (or None):
        for i in ("", "hist", "srs"):
            lbl = i + "labels"
            if not isinstance(dct[lbl], (list, type(None))):
                dct[lbl] = list(dct[lbl])