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_encode_csv_vector_no_gen():
    num_bins = 30
    num_beams = 4

    vel = EarthVelocity(num_bins, num_beams)

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

    vel.remove_vessel_speed(-1.3, -1.4, 0.3)

    dt = datetime.datetime.now()

    # Create CSV lines
    result = vel.encode_csv(dt, 'A', 1, 1.3, 1.0)

    # Check the csv data
    for line in result:
        if bool(re.search(Ensemble.CSV_MAG, line[0])):
            assert bool(re.search(str(2.477), line[0]))
            assert bool(re.search(Ensemble.CSV_MAG, line[0]))
        elif bool(re.search(Ensemble.CSV_DIR, line[0])):
            assert bool(re.search(str(49.3987), line[0]))
            assert bool(re.search(Ensemble.CSV_DIR, line[0]))
        elif bool(re.search(Ensemble.CSV_EARTH_VEL, line[0])):
            assert True
        else:
            assert False
def test_encode_csv():
    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)

    dt = datetime.datetime.now()

    # Create CSV lines
    result = vel.encode_csv(dt, 'A', 1, 1.3, 1.0)

    # Check the csv data
    test_value = 1.0
    for line in result:
        if bool(re.search(Ensemble.CSV_EARTH_VEL, line[0])):
            assert bool(re.search(str(test_value), line[0]))
            assert bool(re.search(Ensemble.CSV_EARTH_VEL, line[0]))
            test_value += 1.1
def test_avg_mag_dir_nan():
    num_bins = 30
    num_beams = 4

    vel = EarthVelocity(num_bins, num_beams)

    vel.Magnitude = [np.NaN, np.NaN]
    vel.Direction = [np.NaN, np.NaN]

    avg_mag, avg_dir = vel.average_mag_dir()

    assert avg_mag is None
    assert avg_dir is None
def test_avg_mag_dir_nan():
    num_bins = 30
    num_beams = 4

    vel = EarthVelocity(num_bins, num_beams)

    vel.Magnitude = [5.0, 5.0, 5.0, 5.0, np.NaN]
    vel.Direction = [1.2, 1.2, 1.2, 1.2, np.NaN]

    avg_mag, avg_dir = vel.average_mag_dir()

    assert avg_mag == pytest.approx(5, 0.001)
    assert avg_dir == pytest.approx(1.2, 0.001)