Exemplo n.º 1
0
def test_dz_from_dv():
    dz = ltu.dz_from_dv(1000. * u.km / u.s, 2.)
    np.testing.assert_allclose(dz, 0.0100236684175417)
    dz1 = ltu.dz_from_dv([1000., 1000., 1000.] * u.km / u.s,
                         np.array([2., 2., 2.]))
    dz2 = ltu.dz_from_dv([1000., 1000., 1000.] * u.km / u.s, [2., 2., 2.])
    np.testing.assert_allclose(dz1, [0.0100236684175417] * 3)
    np.testing.assert_allclose(dz2, [0.0100236684175417] * 3)

    # non-relativistic
    dz = ltu.dz_from_dv(1000. * u.km / u.s, 2., rel=False)
    np.testing.assert_allclose(dz, 0.010006922855944561)

    # test expected errors
    with pytest.raises(IOError):
        ltu.dz_from_dv('dv_not_a_quantity', 2.)
    with pytest.raises(IOError):
        ltu.dz_from_dv(1000. * u.km / u.s, 'zref_not_a_float_nor_array')
    with pytest.raises(IOError):
        ltu.dz_from_dv([1000., 1000., 1000.] * u.km / u.s,
                       np.array([2., 2.]))  # wrong shape for zref
    with pytest.raises(IOError):
        ltu.dz_from_dv([1000., 1000., 1000.] * u.km, 1.)  # wrong dv units
Exemplo n.º 2
0
def give_dz(dv, zref, **kwargs):
    """Wrapper for convinience"""
    print(
        "This function is now in linetools.utils.dz_from_dv(), please avoid using this one."
    )
    return ltu.dz_from_dv(dv, zref, **kwargs)
Exemplo n.º 3
0
def test_dz_from_dv():
    dz = ltu.dz_from_dv(1000. * u.km / u.s, 2.)
    np.testing.assert_allclose(dz, 0.0100236684175417)
    dz1 = ltu.dz_from_dv([1000., 1000., 1000.] * u.km / u.s, np.array([2., 2., 2.]))
    dz2 = ltu.dz_from_dv([1000., 1000., 1000.] * u.km / u.s, [2., 2., 2.])
    np.testing.assert_allclose(dz1, [0.0100236684175417] * 3)
    np.testing.assert_allclose(dz2, [0.0100236684175417] * 3)

    # non-relativistic
    dz = ltu.dz_from_dv(1000. * u.km / u.s, 2., rel=False)
    np.testing.assert_allclose(dz, 0.010006922855944561)

    # test expected errors
    with pytest.raises(IOError):
        ltu.dz_from_dv('dv_not_a_quantity', 2.)
    with pytest.raises(IOError):
        ltu.dz_from_dv(1000. * u.km / u.s, 'zref_not_a_float_nor_array')
    with pytest.raises(IOError):
        ltu.dz_from_dv([1000., 1000., 1000.] * u.km / u.s, np.array([2., 2.]))  # wrong shape for zref
    with pytest.raises(IOError):
        ltu.dz_from_dv([1000., 1000., 1000.] * u.km, 1.)  # wrong dv units
Exemplo n.º 4
0
def read_joebvp_to_components(filename,
                              coord,
                              llist=None,
                              specfile=None,
                              chk_vel=False):
    """ Generate a list of AbsComponent objects from a JoeB VP output file

    Parameters
    ----------
    filename : str
      joeB VP filename
    coord : SkyCoord
      QSO sightline
    llist : LineList, optional
      Used to construct AbsLine objects
    specfile : str, optional
    chk_vel : bool, optional
      Demand that the velocities of a given ion all be the same

    Returns
    -------
    comps : list
      list of AbsComponent objects
    """
    # init
    if llist is None:
        llist = LineList('ISM')
    comps = []
    # Read
    vp_data = Table.read(filename, format='ascii')

    # Subset by zsys + trans
    lbls = []
    for izsys, itrans in zip(vp_data['zsys'], vp_data['trans']):
        lbls.append('{:.6f}_{:s}'.format(izsys, itrans))
    lbls = np.array(lbls)
    ulbls = np.unique(lbls)

    # Subset by nflag; Build components
    for lbl in ulbls:
        mt_lines = np.where(lbls == lbl)[0]
        if chk_vel:
            if len(np.unique(vp_data['vel'][mt_lines])) != 1:
                pdb.set_trace()
        z_fit = ltu.z_from_dv(vp_data['vel'][mt_lines[0]] * u.km / u.s,
                              vp_data['zsys'][mt_lines[0]])
        # Loop on abs lines
        alines = []
        for idx in mt_lines:
            zlim = [
                vp_data['zsys'][idx] + vp_data[vkey][idx] *
                (1 + vp_data['zsys'][idx]) / ckms
                for vkey in ['vlim1', 'vlim2']
            ]
            absline = AbsLine(vp_data['restwave'][idx] * u.AA,
                              z=z_fit,
                              zlim=zlim,
                              linelist=llist)
            # Add measurements [JB -- Want to capture anything else??]
            absline.attrib['coord'] = coord
            absline.attrib['flag_N'] = 1
            absline.attrib['logN'] = vp_data['col'][idx]
            absline.attrib['sig_logN'] = vp_data['sigcol'][idx]
            absline.attrib['b'] = vp_data['bval'][idx]
            absline.attrib['sig_b'] = vp_data['sigbval'][idx]
            absline.attrib['z'] = z_fit
            absline.attrib['sig_z'] = ltu.dz_from_dv(
                vp_data['sigvel'][idx] * u.km / u.s, vp_data['z_comp'][idx])
            if specfile is None:
                absline.attrib['specfile'] = vp_data['specfile'][idx]
            else:
                absline.attrib['specfile'] = specfile
            # Fill N, sig_N
            _, _, = linear_clm(absline.attrib)
            alines.append(absline)

        # AbsComponent
        stars = '*' * alines[0].ion_name.count('*')
        if 'comment' in vp_data.keys():
            comment = vp_data['comment'][mt_lines[0]]
        else:
            comment = ''
        if 'rely' in vp_data.keys():
            reliability = vp_data['rely'][mt_lines[0]]
        else:
            reliability = 'none'
        abscomp = AbsComponent.from_abslines(alines,
                                             stars=stars,
                                             comment=comment,
                                             reliability=reliability)

        # Add measurements [JB -- Want to capture anything else??]
        abscomp.attrib = alines[0].attrib.copy()
        # Remove undesired keys
        for key in ['EW', 'sig_EW', 'flag_EW', 'N', 'sig_N']:
            abscomp.attrib.pop(key)
        # And more required
        for key in ['flag_N', 'logN', 'sig_logN']:
            setattr(abscomp, key, abscomp.attrib[key])
        # Errors must be in first line!
        assert abscomp.sig_logN > 0.
        comps.append(abscomp)
    # Finish
    return comps
Exemplo n.º 5
0
def give_dz(dv, zref, **kwargs):
    """Wrapper for convinience"""
    print("This function is now in linetools.utils.dz_from_dv(), please avoid using this one.")
    return ltu.dz_from_dv(dv, zref, **kwargs)