def test_encode_decode():

    num_bins = 30
    num_beams = 4

    vel = EarthVelocity(num_bins, num_beams)

    # Populate data
    val = 1.0
    for beam in range(vel.element_multiplier):
        for bin_num in range(vel.num_elements):
            vel.Velocities[bin_num][beam] = val
            val += 1.1
    vel.Magnitude, vel.Direction = EarthVelocity.generate_vectors(
        vel.Velocities)

    result = vel.encode()

    vel1 = EarthVelocity(num_bins, num_beams)
    vel1.decode(bytearray(result))

    for beam in range(vel1.element_multiplier):
        for bin_num in range(vel1.num_elements):
            assert vel.Velocities[bin_num][beam] == pytest.approx(
                vel1.Velocities[bin_num][beam], 0.1)
            assert vel.Magnitude[bin_num] == pytest.approx(
                vel1.Magnitude[bin_num], 0.1)
            assert vel.Direction[bin_num] == pytest.approx(
                vel1.Direction[bin_num], 0.1)
def test_velocities():
    vels = EarthVelocity(30, 4)

    # Populate data
    val = 1.0
    for beam in range(vels.element_multiplier):
        for bin_num in range(vels.num_elements):
            vels.Velocities[bin_num][beam] = val
            val += 1.1

    result = vels.encode()

    # Value type
    assert 0xA == result[0]
    assert 0x0 == result[1]
    assert 0x0 == result[2]
    assert 0x0 == result[3]

    # Num Elements
    assert 0x1E == result[4]
    assert 0x0 == result[5]
    assert 0x0 == result[6]
    assert 0x0 == result[7]

    # Element Multiplier
    assert 0x4 == result[8]
    assert 0x0 == result[9]
    assert 0x0 == result[10]
    assert 0x0 == result[11]

    # Imag
    assert 0x0 == result[12]
    assert 0x0 == result[13]
    assert 0x0 == result[14]
    assert 0x0 == result[15]

    # Name Length
    assert 0x8 == result[16]
    assert 0x0 == result[17]
    assert 0x0 == result[18]
    assert 0x0 == result[19]

    # Name
    assert ord('E') == result[20]
    assert ord('0') == result[21]
    assert ord('0') == result[22]
    assert ord('0') == result[23]
    assert ord('0') == result[24]
    assert ord('0') == result[25]
    assert ord('3') == result[26]
    assert ord('\0') == result[27]

    # Length
    assert len(result) == 28 + (
        (vels.element_multiplier * vels.num_elements) * Ensemble.BytesInFloat)

    # Beam Velocities data
    result_val = 1.0
    index = 28  # 28 = Header size
    for beam in range(vels.element_multiplier):
        for bin_num in range(vels.num_elements):
            test_val = Ensemble.GetFloat(index,
                                         Ensemble().BytesInFloat,
                                         bytearray(result))
            assert result_val == pytest.approx(test_val, 0.1)
            result_val += 1.1
            index += Ensemble().BytesInFloat