Exemplo n.º 1
0
 def test_fits_header_can_update_with_it(self, basic_pmh):
     hdr = Header()
     hdr.update(basic_pmh)
     assert hdr["XTENSION"] == 'IMAGE'
     assert hdr.comments["XTENSION"] == 'Image extension'
Exemplo n.º 2
0
def model_to_table(model, xunit=None, yunit=None, zunit=None):
    """
    Convert a model instance to a Table, suitable for attaching to an AstroData
    object or interrogating.

    Parameters
    ----------
    model : a callable function , either a `~astropy.modeling.core.Model`
        or a `~scipy.interpolate.BSpline` instance
    xunit, yunit, zunit : Unit or None
        unit of each axis (y axis may be dependent or independent variable)

    Returns
    -------
    Table : a Table describing the model, with some information in the
        meta["header"] dict
    """
    # Override existing model units with new ones if requested
    meta = getattr(model, "meta", {})
    if xunit:
        meta["xunit"] = xunit
    if yunit:
        meta["yunit"] = yunit
    if zunit:
        meta["zunit"] = zunit

    model_class = model.__class__.__name__
    if isinstance(model, Model):
        header = Header({"MODEL": model_class})
        ndim = model.n_inputs
        if ndim == 1:
            if getattr(model, "domain", None) is not None:
                header.update({
                    "DOMAIN_START": model.domain[0],
                    "DOMAIN_END": model.domain[1]
                })
        elif ndim == 2:
            if getattr(model, "x_domain", None) is not None:
                header.update({
                    "XDOMAIN_START": model.x_domain[0],
                    "XDOMAIN_END": model.x_domain[1]
                })
            if getattr(model, "y_domain", None) is not None:
                header.update({
                    "YDOMAIN_START": model.y_domain[0],
                    "YDOMAIN_END": model.y_domain[1]
                })
        else:
            raise ValueError(f"Cannot handle model class '{model_class}' "
                             f"with dimensionality {ndim}")
        table = Table(model.parameters, names=model.param_names)
        for unit in ("xunit", "yunit", "zunit"):
            if unit in meta:
                header[unit.upper()] = (str(meta[unit]),
                                        f"Units of {unit[0]} axis")
    elif isinstance(model, BSpline):
        knots, coeffs, order = model.tck
        header = Header({"MODEL": f"spline{order}"})
        table = Table([knots, coeffs],
                      names=("knots", "coefficients"),
                      units=(meta.get("xunit"), meta.get("yunit")))
    else:
        raise TypeError(f"Cannot convert object of class '{model_class}'")

    table.meta["header"] = header
    return table