예제 #1
0
 def test_quantity_output(self):
     q = 500.25 * u.day
     dt = TimeDelta(q)
     assert dt.to(u.day) == q
     assert dt.to_value(u.day) == q.value
     assert dt.to_value('day') == q.value
     assert dt.to(u.second).value == q.to_value(u.second)
     assert dt.to_value(u.second) == q.to_value(u.second)
     assert dt.to_value('s') == q.to_value(u.second)
     # Following goes through "format", but should be the same.
     assert dt.to_value('sec') == q.to_value(u.second)
예제 #2
0
 def test_quantity_output(self):
     q = 500.25*u.day
     dt = TimeDelta(q)
     assert dt.to(u.day) == q
     assert dt.to_value(u.day) == q.value
     assert dt.to(u.second).value == q.to_value(u.second)
     assert dt.to_value(u.second) == q.to_value(u.second)
     with pytest.raises(u.UnitsError):
         dt.to(u.m)
     with pytest.raises(u.UnitsError):
         dt.to_value(u.m)
예제 #3
0
 def test_quantity_output_errors(self):
     dt = TimeDelta(250., format='sec')
     with pytest.raises(u.UnitsError):
         dt.to(u.m)
     with pytest.raises(u.UnitsError):
         dt.to_value(u.m)
     with pytest.raises(u.UnitsError):
         dt.to_value(unit=u.m)
     with pytest.raises(ValueError,
                        match=("not one of the known formats.*"
                               "failed to parse as a unit")):
         dt.to_value('parrot')
     with pytest.raises(TypeError):
         dt.to_value('sec', unit=u.s)
     with pytest.raises(TypeError):
         # TODO: would be nice to make this work!
         dt.to_value(u.s, subfmt='str')
예제 #4
0
    def test_to_value(self):
        dt = TimeDelta(86400.0, format='sec')
        assert dt.to_value('jd') == 1.
        assert dt.to_value('jd', 'str') == '1.0'
        assert dt.to_value('sec', subfmt='str') == '86400.0'
        with pytest.raises(ValueError,
                           match=("not one of the known formats.*"
                                  "failed to parse as a unit")):
            dt.to_value('julian')

        with pytest.raises(TypeError, match='missing required format or unit'):
            dt.to_value()
예제 #5
0
def test_no_units_warning():
    with pytest.warns(TimeDeltaMissingUnitWarning):
        delta = TimeDelta(1)
        assert delta.to_value(u.day) == 1

    with pytest.warns(TimeDeltaMissingUnitWarning):
        table = Table({"t": [1, 2, 3]})
        delta = TimeDelta(table["t"])
        assert np.all(delta.to_value(u.day) == [1, 2, 3])

    with pytest.warns(TimeDeltaMissingUnitWarning):
        delta = TimeDelta(np.array([1, 2, 3]))
        assert np.all(delta.to_value(u.day) == [1, 2, 3])

    with pytest.warns(TimeDeltaMissingUnitWarning):
        t = Time('2012-01-01') + 1
        assert t.isot[:10] == '2012-01-02'

    with pytest.warns(TimeDeltaMissingUnitWarning):
        comp = TimeDelta([1, 2, 3], format="jd") >= 2
        assert np.all(comp == [False, True, True])

    with pytest.warns(TimeDeltaMissingUnitWarning):
        # 2 is also interpreted as days, not seconds
        assert (TimeDelta(5 * u.s) > 2) is False

    # with unit is ok
    assert TimeDelta(1 * u.s).to_value(u.s) == 1

    # with format is also ok
    assert TimeDelta(1, format="sec").to_value(u.s) == 1
    assert TimeDelta(1, format="jd").to_value(u.day) == 1

    # table column with units
    table = Table({"t": [1, 2, 3] * u.s})
    assert np.all(TimeDelta(table["t"]).to_value(u.s) == [1, 2, 3])
예제 #6
0
def check_errors(init_time, dt, sat, trajectory, generate_plots=False):
    """Checks the errors for the given trajectory."""
    # **** Check the results ****
    step_offset = 0
    test_stepsize = 1.0 * u.s
    test_end = 0.01 * u.day

    # print(f"Test duration: {(test_end - step_offset * dt).to(u.day)}")
    # print(f"Step size: {test_stepsize}")

    dt_list_hires = TimeDelta(
        np.arange(
            step_offset * dt.to_value(u.s),
            test_end.to_value(u.s),
            test_stepsize.to_value(u.s),
        ),
        format="sec",
    )
    time_list_hires = init_time + dt_list_hires

    # Generate the high-res test trajectory
    e_test_list, r_test_list, v_test_list = sat.sgp4_array(
        time_list_hires.jd1, time_list_hires.jd2)

    r_test_list = CartesianRepresentation(r_test_list.transpose(), unit=u.km)

    # Generate the error list
    r_err_list = (
        trajectory(time_list_hires).cartesian.without_differentials() -
        r_test_list)

    # Error stats
    max_err = r_err_list.norm().max()  # max expected error
    max_nominal_err = r_err_list[-100:].norm().max(
    )  # max expected error nominal

    if generate_plots:
        print(f"max error at the beginning  : {max_err.to(u.mm)}")
        print(f"max error after settle down : {max_nominal_err.to(u.mm)}")

        dt_list_hires_plot = dt_list_hires.to_value(u.day)

        # Plot the error per axis
        fr_ticker = np.arange(dt_list_hires_plot[0], dt_list_hires_plot[-1],
                              dt.to_value(u.day))
        y_ticker = np.zeros(len(fr_ticker))
        plt.figure()
        plt.plot(
            dt_list_hires_plot,
            np.asarray(r_err_list.get_xyz(xyz_axis=1)),
            fr_ticker,
            y_ticker,
            "x",
        )
        plt.legend(["x", "y", "z", "stepsize"])
        plt.title(f"Interpolation Error\n({trajectory.interpolator_name})")
        plt.xlabel("Time [days]")
        plt.ylabel("Error [km]")
        # plt.yscale("log")  # Uncomment to get y axis in log scale
        plt.show()

    return max_err, max_nominal_err