def get_uvw_segment(d, segment=-1):
    """ Calculates uvw for each baseline at mid time of a given segment.
    d defines pipeline state. assumes segmenttimes defined by RT.set_pipeline.
    """

    # define times to read
    if segment != -1:
        assert 'segmenttimes' in d, 'd must have segmenttimes defined'

        t0 = d['segmenttimes'][segment][0]
        t1 = d['segmenttimes'][segment][1]
        datetime = qa.time(qa.quantity((t1+t0)/2, 'd'),
                           form=['ymdhms'], prec=9)[0]
        logger.debug('Calculating uvw for segment %d' % (segment))
    else:
        datetime = 0

    (u, v, w) = calc_uvw(d['filename'], d['scan'],
                         datetime=datetime, bdfdir=d['bdfdir'])

    # cast to units of lambda at first channel.
    # -1 keeps consistent with ms reading convention
    u = u * d['freq_orig'][0] * (1e9/3e8) * (-1)
    v = v * d['freq_orig'][0] * (1e9/3e8) * (-1)
    w = w * d['freq_orig'][0] * (1e9/3e8) * (-1)

    return u.astype('float32'), v.astype('float32'), w.astype('float32')