예제 #1
0
    def get_timeseries(self, datetime=None, format='uv'):
        """
        Returns the timeseries in requested format. If datetime=None,
        then the original timeseries that was entered is returned.
        If datetime is a list containing datetime objects, then the value
        for each of those date times is determined by the underlying
        C++ object and the timeseries is returned.

        The output format is defined by the strings 'r-theta', 'uv'

        :param datetime: [optional] datetime object or list of datetime
                         objects for which the value is desired
        :type datetime: datetime object
        :param format: output format for the times series:
                       either 'r-theta' or 'uv'
        :type format: either string or integer value defined by
                      basic_types.ts_format.* (see cy_basic_types.pyx)

        :returns: numpy array containing dtype=basic_types.datetime_value_2d.
                  Contains user specified datetime and the corresponding
                  values in user specified ts_format
        """
        if datetime is None:
            datetimeval = to_datetime_value_2d(self.ossm.timeseries, format)
        else:
            datetime = np.asarray(datetime, dtype='datetime64[s]').reshape(-1)
            timeval = np.zeros((len(datetime), ),
                               dtype=basic_types.time_value_pair)
            timeval['time'] = date_to_sec(datetime)
            timeval['value'] = self.ossm.get_time_value(timeval['time'])
            datetimeval = to_datetime_value_2d(timeval, format)

        return datetimeval
def test_exceptions(wind_ts, invalid_rq):
    """
    test exceptions
    """

    with pytest.raises(ValueError):

        # incorrect dtype

        to_time_value_pair(wind_ts['tv'], ts_format.magnitude_direction)

    with pytest.raises(ValueError):

        # incorrect format

        to_time_value_pair(wind_ts['dtv_rq'], -1)
        to_datetime_value_2d(wind_ts['tv'], -1)

    with pytest.raises(ValueError):

        # string input can only be 'r-theta' or 'uv'

        to_time_value_pair(wind_ts['dtv_rq'], 'magnitude')
        to_datetime_value_2d(wind_ts['tv'], 'magnitude')

    # following also raises ValueError. This gives invalid (r,theta) inputs
    # which are rejected by the transforms.r_theta_to_uv_wind method.
    # It tests the inner exception is correct

    with pytest.raises(ValueError):
        invalid_dtv_rq = np.zeros((len(invalid_rq['rq']), ),
                                  dtype=datetime_value_2d)
        invalid_dtv_rq['value'] = invalid_rq['rq']
        to_time_value_pair(invalid_dtv_rq,
                           ts_format.magnitude_direction)
예제 #3
0
    def get_timeseries(self, datetime=None, format='uv'):
        """
        Returns the timeseries in requested format. If datetime=None,
        then the original timeseries that was entered is returned.
        If datetime is a list containing datetime objects, then the value
        for each of those date times is determined by the underlying
        C++ object and the timeseries is returned.

        The output format is defined by the strings 'r-theta', 'uv'

        :param datetime: [optional] datetime object or list of datetime
                         objects for which the value is desired
        :type datetime: datetime object
        :param format: output format for the times series:
                       either 'r-theta' or 'uv'
        :type format: either string or integer value defined by
                      basic_types.ts_format.* (see cy_basic_types.pyx)

        :returns: numpy array containing dtype=basic_types.datetime_value_2d.
                  Contains user specified datetime and the corresponding
                  values in user specified ts_format
        """
        if datetime is None:
            datetimeval = to_datetime_value_2d(self.ossm.timeseries, format)
        else:
            datetime = np.asarray(datetime, dtype='datetime64[s]').reshape(-1)
            timeval = np.zeros((len(datetime), ),
                               dtype=basic_types.time_value_pair)
            timeval['time'] = date_to_sec(datetime)
            timeval['value'] = self.ossm.get_time_value(timeval['time'])
            datetimeval = to_datetime_value_2d(timeval, format)

        return datetimeval
예제 #4
0
def test_exceptions(wind_ts, invalid_rq):
    """
    test exceptions
    """
    with pytest.raises(ValueError):
        # incorrect dtype
        to_time_value_pair(wind_ts['tv'], ts_format.magnitude_direction)

    with pytest.raises(ValueError):
        # incorrect format
        to_time_value_pair(wind_ts['dtv_rq'], -1)
        to_datetime_value_2d(wind_ts['tv'], -1)

    with pytest.raises(ValueError):
        # string input can only be 'r-theta' or 'uv'
        to_time_value_pair(wind_ts['dtv_rq'], 'magnitude')
        to_datetime_value_2d(wind_ts['tv'], 'magnitude')

    # following also raises ValueError. This gives invalid (r,theta) inputs
    # which are rejected by the transforms.r_theta_to_uv_wind method.
    # It tests the inner exception is correct
    with pytest.raises(ValueError):
        length_of_dim1 = len(invalid_rq['rq'])
        zero_times = [zero_time()] * length_of_dim1

        invalid_dtv_rq = np.array(zip(zero_times, invalid_rq['rq']),
                                  dtype=datetime_value_2d)

        invalid_dtv_rq['value'] = invalid_rq['rq']

        to_time_value_pair(invalid_dtv_rq, ts_format.magnitude_direction)
예제 #5
0
    def get_timeseries(
        self,
        datetime=None,
        units=None,
        format='r-theta',
        ):
        """
        Returns the timeseries in the requested format. If datetime=None,
        then the original timeseries that was entered is returned.
        If datetime is a list containing datetime objects, then the wind value
        for each of those date times is determined by the underlying
        CyOSSMTime object and the timeseries is returned.

        The output format is defined by the strings 'r-theta', 'uv'

        :param datetime: [optional] datetime object or list of datetime
                         objects for which the value is desired
        :type datetime: datetime object
        :param units: [optional] outputs data in these units. Default is to
                      output data in units
        :type units: string. Uses the hazpy.unit_conversion module.
                     hazpy.unit_conversion throws error for invalid units
        :param format: output format for the times series:
                       either 'r-theta' or 'uv'
        :type format: either string or integer value defined by
                      basic_types.ts_format.* (see cy_basic_types.pyx)

        :returns: numpy array containing dtype=basic_types.datetime_value_2d.
                  Contains user specified datetime and the corresponding
                  values in user specified ts_format
        """

        if datetime is None:
            datetimeval = \
                convert.to_datetime_value_2d(self.ossm.timeseries,
                    format)
        else:
            datetime = np.asarray(datetime, dtype='datetime64[s]'
                                  ).reshape(-1)
            timeval = np.zeros((len(datetime), ),
                               dtype=basic_types.time_value_pair)
            timeval['time'] = time_utils.date_to_sec(datetime)
            timeval['value'] = self.ossm.get_time_value(timeval['time'])
            datetimeval = convert.to_datetime_value_2d(timeval, format)

        if units is not None:
            datetimeval['value'] = \
                self._convert_units(datetimeval['value'], format,
                                    'meter per second', units)
        else:
            datetimeval['value'] = \
                self._convert_units(datetimeval['value'], format,
                                    'meter per second', self.units)

        return datetimeval
예제 #6
0
    def get_timeseries(self, datetime=None, coord_sys='uv'):
        """
        Returns the timeseries in requested coordinate system.
        If datetime=None, then the original timeseries that was entered is
        returned.
        If datetime is a list containing datetime objects, then the value
        for each of those date times is determined by the underlying
        C++ object and the timeseries is returned.

        The output coordinate system is defined by the strings 'r-theta', 'uv'

        :param datetime: [optional] datetime object or list of datetime
                         objects for which the value is desired
        :type datetime: datetime object

        :param coord_sys: output coordinate system for the times series:
                          either 'r-theta' or 'uv'
        :type coord_sys: either string or integer value defined by
                         basic_types.ts_format.* (see cy_basic_types.pyx)

        :returns: numpy array containing dtype=basic_types.datetime_value_2d.
                  Contains user specified datetime and the corresponding
                  values in user specified ts_format
        """
        if datetime is None:
            datetimeval = to_datetime_value_2d(self.ossm.timeseries, coord_sys)
        else:
            datetime = np.asarray(datetime, dtype='datetime64[s]').reshape(-1)
            timeval = np.zeros((len(datetime), ),
                               dtype=basic_types.time_value_pair)
            timeval['time'] = date_to_sec(datetime)
            (timeval['value'], err) = self.ossm.get_time_value(timeval['time'])

            if err != 0:
                msg = ('No available data in the time interval that is being '
                       'modeled\n'
                       '\tModel time: {}\n'
                       '\tMover: {} of type {}\n'
                       .format(datetime, self.name, self.__class__))

                self.logger.error(msg)
                raise RuntimeError(msg)

            datetimeval = to_datetime_value_2d(timeval, coord_sys)

        return np.copy(datetimeval)
예제 #7
0
    def get_timeseries(self, datetime=None, coord_sys='uv'):
        """
        Returns the timeseries in requested coordinate system.
        If datetime=None, then the original timeseries that was entered is
        returned.
        If datetime is a list containing datetime objects, then the value
        for each of those date times is determined by the underlying
        C++ object and the timeseries is returned.

        The output coordinate system is defined by the strings 'r-theta', 'uv'

        :param datetime: [optional] datetime object or list of datetime
                         objects for which the value is desired
        :type datetime: datetime object

        :param coord_sys: output coordinate system for the times series:
                          either 'r-theta' or 'uv'
        :type coord_sys: either string or integer value defined by
                         basic_types.ts_format.* (see cy_basic_types.pyx)

        :returns: numpy array containing dtype=basic_types.datetime_value_2d.
                  Contains user specified datetime and the corresponding
                  values in user specified ts_format
        """
        if datetime is None:
            datetimeval = to_datetime_value_2d(self.ossm.timeseries, coord_sys)
        else:
            datetime = np.asarray(datetime, dtype='datetime64[s]').reshape(-1)
            timeval = np.zeros((len(datetime), ),
                               dtype=basic_types.time_value_pair)
            timeval['time'] = date_to_sec(datetime)
            (timeval['value'], err) = self.ossm.get_time_value(timeval['time'])

            if err != 0:
                msg = ('No available data in the time interval that is being '
                       'modeled\n'
                       '\tModel time: {}\n'
                       '\tMover: {} of type {}\n'
                       .format(datetime, self.name, self.__class__))

                self.logger.error(msg)
                raise RuntimeError(msg)

            datetimeval = to_datetime_value_2d(timeval, coord_sys)

        return np.copy(datetimeval)
예제 #8
0
def _get_timeseries_from_cpp(windmover):
    """
    local method for tests - returns the timeseries used internally
    by the C++ WindMover_c object.
    This should be the same as the timeseries stored in the self.wind object

    Data is returned as a datetime_value_2d array in units of meter per second
    in format = uv

    This is simply used for testing.
    """
    dtv = windmover.wind.get_wind_data(format=ts_format.uv)
    tv = convert.to_time_value_pair(dtv, ts_format.uv)
    val = windmover.mover.get_time_value(tv['time'])
    tv['value']['u'] = val['u']
    tv['value']['v'] = val['v']

    return convert.to_datetime_value_2d(tv, ts_format.uv)
예제 #9
0
def _get_timeseries_from_cpp(windmover):
    """
    local method for tests - returns the timeseries used internally
    by the C++ WindMover_c object.
    This should be the same as the timeseries stored in the self.wind object

    Data is returned as a datetime_value_2d array in units of meter per second
    in format = uv

    This is simply used for testing.
    """
    dtv = windmover.wind.get_wind_data(format=ts_format.uv)
    tv = convert.to_time_value_pair(dtv, ts_format.uv)
    val = windmover.mover.get_time_value(tv['time'])
    tv['value']['u'] = val['u']
    tv['value']['v'] = val['v']

    return convert.to_datetime_value_2d(tv, ts_format.uv)
def test_to_datetime_value_2d_uv(wind_ts, out_ts_format):
    out_dtval = to_datetime_value_2d(wind_ts['tv'],
            out_ts_format).view(dtype=np.recarray)
    #assert np.all(out_dtval.time == wind_ts['dtv_rq'].time)
    assert np.allclose(out_dtval.value, wind_ts['dtv_uv'].value, atol,
                       rtol)
예제 #11
0
def test_to_datetime_value_2d_uv(wind_ts, out_ts_format):
    out_dtval = (to_datetime_value_2d(wind_ts['tv'],
                                      out_ts_format).view(dtype=np.recarray))

    assert np.allclose(out_dtval.value, wind_ts['dtv_uv'].value, atol, rtol)