コード例 #1
0
 def test_02_wrong(self):
     with self.assertRaises(ValueError):
         timetools.Timegrid(self.year97, self.year97, self.oneday)
     with self.assertRaises(ValueError):
         timetools.Timegrid(self.year98, self.year97, self.oneday)
     with self.assertRaises(ValueError):
         timetools.Timegrid(self.year97, self.year98,
                            timetools.Period('360d'))
コード例 #2
0
 def setUp(self):
     self.year97 = timetools.Date('01.11.1996')
     self.year98 = timetools.Date('01.11.1997')
     self.oneday = timetools.Period('1d')
     self.onehour = timetools.Period('1h')
     self.timegrid = timetools.Timegrid(self.year97, self.year98,
                                        self.oneday)
コード例 #3
0
 def test_02_automatic_mode(self):
     with self.assertRaises(RuntimeError):
         self.indexer.dayofyear
     pub.timegrids = timetools.Timegrids(
         timetools.Timegrid('01.01.2004', '1.01.2005', '1d'))
     self.assertIsInstance(self.indexer.dayofyear, numpy.ndarray)
     self.assertEqual(len(self.indexer.dayofyear), 366)
     self.assertTupleEqual(tuple(self.indexer.dayofyear), tuple(range(366)))
     pub.timegrids = timetools.Timegrids(
         timetools.Timegrid('01.01.2005', '1.01.2006', '1d'))
     del self.indexer.dayofyear
     self.assertIsInstance(self.indexer.dayofyear, numpy.ndarray)
     self.assertEqual(len(self.indexer.dayofyear), 365)
     self.assertTupleEqual(
         tuple(self.indexer.dayofyear),
         tuple(list(range(31 + 28)) + list(range(31 + 28 + 1, 366))))
     self.assertIs(self.indexer.dayofyear, self.indexer.dayofyear)
コード例 #4
0
 def test_02_automatic_mode(self):
     with self.assertRaises(RuntimeError):
         self.indexer.monthofyear
     pub.timegrids = timetools.Timegrids(
         timetools.Timegrid('01.01.2004', '1.01.2005', '1d'))
     self.assertIsInstance(self.indexer.monthofyear, numpy.ndarray)
     self.assertEqual(len(self.indexer.monthofyear), 366)
     self.assertTupleEqual(
         tuple(self.indexer.monthofyear),
         tuple(31 * [0] + 29 * [1] + 31 * [2] + 30 * [3] + 31 * [4] +
               30 * [5] + 31 * [6] + 31 * [7] + 30 * [8] + 31 * [9] +
               30 * [10] + 31 * [11]))
     self.assertIs(self.indexer.monthofyear, self.indexer.monthofyear)
コード例 #5
0
    def timeofyear(self):
        """Index values, representing the time of the year.

        Let us reconsider one of the examples of the documentation on
        property |Indexer.dayofyear|:

        >>> from hydpy import pub
        >>> from hydpy import Timegrids, Timegrid
        >>> from hydpy.core.indextools import Indexer
        >>> pub.timegrids = "27.02.2005", "3.03.2005", "1d"

        Due to the simulation step size of one day, the index arrays
        calculated by properties |Indexer.dayofyear| and |Indexer.timeofyear|
        are identical:

        >>> Indexer().dayofyear
        array([57, 58, 60, 61])
        >>> Indexer().timeofyear
        array([57, 58, 60, 61])

        In the next example, we halve the step size:

        >>> pub.timegrids = "27.02.2005", "3.03.2005", "12h"

        Now two subsequent simulation steps associated are with the same day:

        >>> Indexer().dayofyear
        array([57, 57, 58, 58, 60, 60, 61, 61])

        However, the `timeofyear` array gives the index of the
        respective simulation steps of the actual year:

        >>> Indexer().timeofyear
        array([114, 115, 116, 117, 120, 121, 122, 123])

        Note the gap in the returned index array due to 2005 being not a
        leap year.
        """
        # pylint: disable=no-self-use
        # pylint does not understand descriptors well enough, so far
        def _timeofyear(date):
            date = copy.deepcopy(date)
            date.year = 2000
            return refgrid[date]

        refgrid = timetools.Timegrid(
            timetools.Date("2000.01.01"),
            timetools.Date("2001.01.01"),
            _get_timegrids(_timeofyear).stepsize,
        )
        return _timeofyear
コード例 #6
0
 def test_01_manual_mode(self):
     with self.assertRaises(BaseException):
         self.indexer.dayofyear = 'a'
     with self.assertRaises(BaseException):
         self.indexer.dayofyear = ['a', 'b']
     with self.assertRaises(ValueError):
         self.indexer.dayofyear = [[1, 2], [3, 4]]
     self.indexer.dayofyear = [1, 2]
     self.assertIsInstance(self.indexer.dayofyear, numpy.ndarray)
     self.assertTupleEqual(tuple(self.indexer.dayofyear), (1, 2))
     del self.indexer.dayofyear
     self.assertIsNone(self.indexer._dayofyear)
     pub.timegrids = timetools.Timegrids(
         timetools.Timegrid('01.01.2004', '1.01.2005', '1d'))
     with self.assertRaises(ValueError):
         self.indexer.dayofyear = [1, 2]
コード例 #7
0
    def _gettimeofyear(self):
        """Time of the year index (first simulation step of each year = 0...).

        The property |Indexer.timeofyear| is best explained through
        comparing it with property |Indexer.dayofyear|:

        Let us reconsider one of the examples of the documentation on
        property |Indexer.dayofyear|:

        >>> from hydpy import pub
        >>> from hydpy import Timegrids, Timegrid
        >>> from hydpy.core.indextools import Indexer
        >>> pub.timegrids = Timegrids(Timegrid('27.02.2005',
        ...                                    '3.03.2005',
        ...                                    '1d'))

        Due to the simulation stepsize being one day, the index arrays
        calculated by both properties are identical:

        >>> Indexer().dayofyear
        array([57, 58, 60, 61])
        >>> Indexer().timeofyear
        array([57, 58, 60, 61])

        In the next example the step size is halved:

        >>> pub.timegrids = Timegrids(Timegrid('27.02.2005',
        ...                                    '3.03.2005',
        ...                                    '12h'))

        Now the there a generally two subsequent simulation steps associated
        with the same day:

        >>> Indexer().dayofyear
        array([57, 57, 58, 58, 60, 60, 61, 61])

        However, the `timeofyear` array gives the index of the
        respective simulation steps of the actual year:

        >>> Indexer().timeofyear
        array([114, 115, 116, 117, 120, 121, 122, 123])

        Note the gap in the returned index array due to 2005 being not a
        leap year.
        """
        if ((self._timeofyear is None) or
                (hash(pub.timegrids) != self._timeofyear_hash)):
            if pub.timegrids is None:
                refgrid = None
            else:
                refgrid = timetools.Timegrid(timetools.Date('2000.01.01'),
                                             timetools.Date('2001.01.01'),
                                             pub.timegrids.stepsize)

            def timeofyear(date):
                date = date.copy()
                date.year = 2000
                return refgrid[date]

            self._timeofyear = self._calcidxs(timeofyear)
            self._timeofyear_hash = hash(pub.timegrids)
        return self._timeofyear
コード例 #8
0
 def test_01_right(self):
     timetools.Timegrid(self.year97, self.year98, self.oneday)