예제 #1
0
    def __init__(self, wind=None, extrapolate=False, **kwargs):
        #def __init__(self, wind=None, **kwargs):
        """
        Uses super to call CyMover base class __init__

        :param wind: wind object -- provides the wind time series for the mover

        Remaining kwargs are passed onto WindMoversBase __init__ using super.
        See Mover documentation for remaining valid kwargs.

        .. note:: Can be initialized with wind=None; however, wind must be
            set before running. If wind is not None, toggle make_default_refs
            to False since user provided a valid Wind and does not wish to
            use the default from the Model.
        """
        self.mover = CyWindMover()

        self._wind = None
        if wind is not None:
            self.wind = wind
            kwargs['make_default_refs'] = \
                kwargs.pop('make_default_refs', False)
            kwargs['name'] = \
                kwargs.pop('name', wind.name)

        self.extrapolate = extrapolate
        # set optional attributes
        super(WindMover, self).__init__(**kwargs)

        # this will have to be updated when wind is set or changed
        if self.wind is not None:
            self.real_data_start = time_utils.sec_to_datetime(
                self.wind.ossm.get_start_time())
            self.real_data_stop = time_utils.sec_to_datetime(
                self.wind.ossm.get_end_time())
예제 #2
0
    def __init__(self, wind=None, **kwargs):
        """
        Uses super to call CyMover base class __init__

        :param wind: wind object -- provides the wind time series for the mover

        Remaining kwargs are passed onto WindMoversBase __init__ using super.
        See Mover documentation for remaining valid kwargs.

        .. note:: Can be initialized with wind=None; however, wind must be
            set before running. If wind is not None, toggle make_default_refs
            to False since user provided a valid Wind and does not wish to
            use the default from the Model.
        """
        self.mover = CyWindMover()

        self._wind = None
        if wind is not None:
            self.wind = wind
            self.name = wind.name
            kwargs['make_default_refs'] = kwargs.pop('make_default_refs',
                                                     False)
            kwargs['name'] = kwargs.pop('name', wind.name)

        # set optional attributes
        super(WindMover, self).__init__(**kwargs)
예제 #3
0
class TestObjectSerialization:
    '''
        Test all the serialization and deserialization methods that are
        available to the CyTimeseries object.
    '''
    ossmT = CyTimeseries(filename=testdata['timeseries']['wind_ts'],
                         file_format=ts_format.magnitude_direction)
    wm = CyWindMover()
    wm.set_ossm(ossmT)

    def test_repr(self):
        '''
            Test that the repr method produces a string capable of reproducing
            the object.
        '''
        # This works, but in order to properly eval the repr string, we need
        # the top level gnome module, as well as the numpy 'array' type in the
        # global name space.
        # So before we try it, we first need to import like this:
        import gnome
        from numpy import array

        new_wm = eval(repr(self.wm))

        assert new_wm == self.wm
        assert repr(new_wm) == repr(self.wm)
예제 #4
0
class ConstantWind(cy_fixtures.CyTestMove):
    """
    Wind Mover object instantiated with a constant wind using member method
    set_constant_wind(...)
    Used for test setup
    """
    wm = CyWindMover()

    def __init__(self):
        super(ConstantWind, self).__init__()
        self.const_wind = const_wind
        self.wm.set_constant_wind(const_wind['u'], const_wind['v'])

    def test_move(self):
        """ forecast move """
        self.wm.prepare_for_model_step(self.model_time, self.time_step)
        self.wm.get_move(self.model_time, self.time_step, self.ref, self.delta,
                         self.windage, self.status, spill_type.forecast)

    def test_move_uncertain(self):
        """ uncertain LEs """
        self.wm.prepare_for_model_step(self.model_time, self.time_step,
                                       len(self.spill_size), self.spill_size)
        self.wm.get_move(self.model_time, self.time_step, self.ref,
                         self.u_delta, self.windage, self.status,
                         spill_type.uncertainty)
예제 #5
0
class ConstantWindWithOSSM(cy_fixtures.CyTestMove):
    """
    This defines the OSSMTimeValue_c object using the CyTimeseries class,
    then uses the set_ossm method of CyWindMover object to set the
    time_dep member of the underlying WindMover_c C++ object

    Used for test setup
    """
    wm = CyWindMover()

    def __init__(self):
        super(ConstantWindWithOSSM, self).__init__()

        time_val = np.empty((1, ), dtype=time_value_pair)
        time_val['time'] = 0  # should not matter
        time_val['value'] = const_wind

        self.ossm = CyTimeseries(timeseries=time_val)
        self.wm.set_ossm(self.ossm)

    def test_move(self):
        """
        invoke get_move (no uncertainty)
        """
        self.wm.prepare_for_model_step(self.model_time, self.time_step)
        self.wm.get_move(self.model_time, self.time_step, self.ref, self.delta,
                         self.windage, self.status, spill_type.forecast)

    def test_move_uncertain(self):
        """ invoke get_move (uncertain LEs) """
        self.wm.prepare_for_model_step(self.model_time, self.time_step,
                                       len(self.spill_size), self.spill_size)
        self.wm.get_move(self.model_time, self.time_step, self.ref,
                         self.u_delta, self.windage, self.status,
                         spill_type.uncertainty)
예제 #6
0
def test_init():
    # can we create a wind_mover?
    wm = CyWindMover()

    assert wm.uncertain_duration == 10800
    assert wm.uncertain_time_delay == 0
    assert wm.uncertain_speed_scale == 2
    assert wm.uncertain_angle_scale == 0.4
예제 #7
0
def test_properties():
    wm = CyWindMover()

    wm.uncertain_duration = 1
    wm.uncertain_time_delay = 2
    wm.uncertain_speed_scale = 3
    wm.uncertain_angle_scale = 4

    assert wm.uncertain_duration == 1
    assert wm.uncertain_time_delay == 2
    assert wm.uncertain_speed_scale == 3
    assert wm.uncertain_angle_scale == 4
예제 #8
0
def test_LE_not_in_water():
    """
    Tests get_move returns 0 for LE's that have a status
    different from in_water
    """
    wm = CyWindMover()
    cm = cy_fixtures.CyTestMove()
    delta = np.zeros((cm.num_le, ), dtype=world_point)
    cm.status[:] = 0

    wm.prepare_for_model_step(cm.model_time, cm.time_step)
    wm.get_move(cm.model_time, cm.time_step, cm.ref, delta, cm.windage,
                cm.status, spill_type.forecast)

    assert np.all(delta['lat'] == 0)
    assert np.all(delta['long'] == 0)
    assert np.all(delta['z'] == 0)
예제 #9
0
class TestVariableWind:
    """
    Uses OSSMTimeValue_c to define a variable wind
    - variable wind has 'v' component, so movement
      should only be in 'lat' direction of world point

    Leave as a class as we may add more methods to it for testing
    """
    wm = CyWindMover()
    cm = cy_fixtures.CyTestMove()
    delta = np.zeros((cm.num_le, ), dtype=world_point)

    time_val = np.zeros((2, ), dtype=time_value_pair)
    (time_val['time'])[:] = np.add([0, 3600], cm.model_time)  # after 1 hour
    (time_val['value']['v'])[:] = [100, 200]

    # CyTimeseries needs the same scope as CyWindMover because CyWindMover
    # uses the C++ pointer defined in CyTimeseries.time_dep. This must be defined
    # for the scope of CyWindMover

    ossm = CyTimeseries(timeseries=time_val)
    wm.set_ossm(ossm)

    def test_move(self):
        for x in range(0, 3):
            vary_time = x * 1800
            self.wm.prepare_for_model_step(self.cm.model_time + vary_time,
                                           self.cm.time_step)
            self.wm.get_move(self.cm.model_time + vary_time,
                             self.cm.time_step,
                             self.cm.ref,
                             self.delta,
                             self.cm.windage,
                             self.cm.status,
                             spill_type.forecast)

            print self.delta
            assert np.all(self.delta['lat'] != 0)
            assert np.all(self.delta['long'] == 0)
            assert np.all(self.delta['z'] == 0)
예제 #10
0
def test_eq():
    wm = CyWindMover()

    other_wm = CyWindMover()
    assert wm == other_wm

    other_wm = CyWindMover()
    other_wm.uncertain_duration = 1
    assert wm != other_wm

    other_wm = CyWindMover()
    other_wm.uncertain_time_delay = 2
    assert wm != other_wm

    other_wm = CyWindMover()
    other_wm.uncertain_speed_scale = 3
    assert wm != other_wm

    other_wm = CyWindMover()
    other_wm.uncertain_angle_scale = 4
    assert wm != other_wm
예제 #11
0
class TestVariableWind:
    """
    Uses OSSMTimeValue_c to define a variable wind
    - variable wind has 'v' component, so movement
      should only be in 'lat' direction of world point

    Leave as a class as we may add more methods to it for testing
    """
    wm = CyWindMover()
    cm = cy_fixtures.CyTestMove()
    delta = np.zeros((cm.num_le, ), dtype=world_point)

    time_val = np.zeros((2, ), dtype=time_value_pair)
    (time_val['time'])[:] = np.add([0, 3600], cm.model_time)  # after 1 hour
    (time_val['value']['v'])[:] = [100, 200]

    # CyTimeseries needs the same scope as CyWindMover because CyWindMover
    # uses the C++ pointer defined in CyTimeseries.time_dep.
    # This must be defined for the scope of CyWindMover

    ossm = CyTimeseries(timeseries=time_val)
    wm.set_ossm(ossm)

    def test_move(self):
        for x in range(0, 3):
            vary_time = x * 1800
            self.wm.prepare_for_model_step(self.cm.model_time + vary_time,
                                           self.cm.time_step)

            self.wm.get_move(self.cm.model_time + vary_time,
                             self.cm.time_step,
                             self.cm.ref,
                             self.delta,
                             self.cm.windage,
                             self.cm.status,
                             spill_type.forecast)

            print self.delta
            assert np.all(self.delta['lat'] != 0)
            assert np.all(self.delta['long'] == 0)
            assert np.all(self.delta['z'] == 0)

    def test_move_out_of_bounds(self):
        '''
            Our wind mover should fail in the prepare_for_model_step() function
            if our wind time series is out of bounds with respect to the
            model time we are preparing for, unless our wind time series is
            configured to extrapolate wind values.
        '''
        # setup a time series that's out of range of our model time
        time_val = np.zeros((2, ), dtype=time_value_pair)
        (time_val['time'])[:] = np.add([3600, 7200], self.cm.model_time)
        (time_val['value']['v'])[:] = [100, 200]

        # extrapolation should be off by default
        ossm = CyTimeseries(timeseries=time_val)
        self.wm.set_ossm(ossm)

        # this should fail because our time series is not set to extrapolate
        with raises(OSError):
            self.wm.prepare_for_model_step(self.cm.model_time,
                                           self.cm.time_step)

        ossm = CyTimeseries(timeseries=time_val, extrapolation_is_allowed=True)
        self.wm.set_ossm(ossm)

        # We set our time series to extrapolate, so this should pass
        self.wm.prepare_for_model_step(self.cm.model_time,
                                       self.cm.time_step)

        # clean up our time series
        self.wm.set_ossm(self.ossm)