def rebuild_params(values, keys):
    """
    Build parameter dictionary from flattened values and ordered parameter names.

    For example, given the following:

       values = np.array([1., 5., .2, .5, 50., .1, 3, 3]),
       keys = [("Rt_fac", ["mu", "gamma", "b-a"]), ("R_fac", ["mu", "gamma", "b-a"]), ("consts", ["En", "Im"])]

    rebuild_params(values, keys) would return (in yaml representation for clarity):

       Rt_fac:
          mu: 1.
          gamma: 5.
          a: .9
          b: 1.1
       R_fac:
          mu: .5
          gamma: 50.
          a: .45
          b: .55
       consts:
          En: 3
          Im: 3
    """
    v_i = 0
    d = {}
    for p0, p1s in keys:
        d[p0] = {}
        r = None
        mu = None
        for p1 in p1s:
            if p1 == "b-a":
                r = values[v_i]
            else:
                if p1 == "mu":
                    mu = values[v_i]
                d[p0][p1] = values[v_i]
            v_i += 1
        if r is not None and mu is not None:
            d[p0]["a"] = mu - r / 2
            d[p0]["b"] = mu + r / 2
    return d