Exemplo n.º 1
0
 def test_2_can_append_dt_range(self):
     self.tt.append(
         DateTimeRange(start_time=datetime.now(), stop_time=datetime.now()))
     self.assertEqual(len(self.tt), 1)
     self.tt += DateTimeRange(start_time=datetime.now(),
                              stop_time=datetime.now())
     self.assertEqual(len(self.tt), 2)
Exemplo n.º 2
0
 def test_3_can_append_dt_range_list(self):
     sz = len(self.tt)
     self.tt.append([
         DateTimeRange(start_time=datetime.now(), stop_time=datetime.now())
     ] * 10)
     self.assertEqual(len(self.tt), sz + 10)
     self.tt += [
         DateTimeRange(start_time=datetime.now(), stop_time=datetime.now())
     ] * 10
     self.assertEqual(len(self.tt), sz + 20)
Exemplo n.º 3
0
 def test_len_equals_number_of_ranges_inserted(self):
     tt = TimeTable("Empty TimeTable")
     self.assertEqual(len(tt), 0)
     tt.append(
         DateTimeRange(start_time=datetime.now(), stop_time=datetime.now()))
     self.assertEqual(len(tt), 1)
     for _ in range(9):
         tt.append(
             DateTimeRange(start_time=datetime.now(),
                           stop_time=datetime.now()))
     self.assertEqual(len(tt), 10)
Exemplo n.º 4
0
    def parameter_range(
        self, parameter_id: str or AMDAParameterIndex or AMDADatasetIndex
    ) -> Optional[DateTimeRange]:
        """Get product time range.

        Parameters
        ----------
        parameter_id: str or AMDAParameterIndex or AMDADatasetIndex
            parameter id

        Returns
        -------
        Optional[DateTimeRange]
            Data time range

        Examples
        --------

        >>> import speasy as spz
        >>> spz.amda.parameter_range("imf")
        <DateTimeRange: 1997-09-02T00:00:12+00:00 -> ...>

        """
        if not len(flat_inventories.amda.parameters):
            self._impl.update_inventory()
        parameter_id = to_xmlid(parameter_id)
        dataset_name = self._find_parent_dataset(parameter_id)

        if dataset_name in flat_inventories.amda.datasets:
            dataset = flat_inventories.amda.datasets[dataset_name]
            return DateTimeRange(
                datetime.strptime(dataset.dataStart, '%Y-%m-%dT%H:%M:%SZ'),
                datetime.strptime(dataset.dataStop, '%Y-%m-%dT%H:%M:%SZ'))
Exemplo n.º 5
0
 def test_can_pop_dt_ranges(self):
     prev_len = len(self.tt)
     self.tt.append(
         DateTimeRange(start_time=datetime.now(), stop_time=datetime.now()))
     self.assertEqual(len(self.tt), prev_len + 1)
     self.tt.pop(-1)
     self.assertEqual(len(self.tt), prev_len)
Exemplo n.º 6
0
 def test_has_a_time_range_encompassing_all_variables(self):
     ds = Dataset(name="test",
                  variables={
                      'v1': make_simple_var(10., 100.),
                      'v2': make_simple_var(30., 40.),
                      'v3': make_simple_var(60., 500.)
                  },
                  meta={})
     self.assertEqual(DateTimeRange(10., 499.), ds.time_range())
Exemplo n.º 7
0
 def time_range(self) -> Optional[DateTimeRange]:
     start = min(map(lambda v: v.time[0],
                     filter(len, self.variables.values())),
                 default=None)
     stop = max(map(lambda v: v.time[-1],
                    filter(len, self.variables.values())),
                default=None)
     if start and stop:
         return DateTimeRange(start, stop)
     return None
Exemplo n.º 8
0
class SpanTransComparaisons(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    @data(([1., 1.], [1., 1.], True), ([1., 10.], [0., 10.], False),
          ([1., 10.], [5., 15.], False), ([-1., 1.], [-.5, .5], True),
          ([-2., 2.], [-10., 10.], False))
    @unpack
    def test_contains(self, span, other, expected_result):
        self.assertEqual(span_utils.contains(span, other), expected_result)

    @data(
        ([1., 1.], [1., 1.], True),
        ([1., 10.], [0., 10.], False),
        ([1., 10.], [1., 15.], False),
        (DateTimeRange(datetime(2001, 1, 8, 1, 0, 0),
                       datetime(2001, 1, 8, 2, 0, 0)), [
                           datetime(2001, 1, 8, 1, 0, 0, tzinfo=timezone.utc),
                           datetime(2001, 1, 8, 2, 0, 0, tzinfo=timezone.utc)
                       ], True),
        ([
            datetime(2001, 1, 8, 1, 0, 0, tzinfo=timezone.utc),
            datetime(2001, 1, 8, 2, 0, 0, tzinfo=timezone.utc)
        ],
         DateTimeRange(datetime(2001, 1, 8, 1, 0, 0),
                       datetime(2001, 1, 8, 3, 0, 0)), False),
    )
    @unpack
    def test_equal(self, span, other, expected_result):
        self.assertEqual(span_utils.equals(span, other), expected_result)

    @data(([1., 1.], [1., 1.], True), ([1., 10.], [0., 10.], True),
          ([1., 10.], [20., 25.], False), ([20., 25.], [1., 10.], False))
    @unpack
    def test_intersects(self, span, other, expected_result):
        self.assertEqual(span_utils.intersects(span, other), expected_result)
Exemplo n.º 9
0
def _round_for_cache(dt_range: DateTimeRange, fragment_hours: int):
    start_time = datetime(dt_range.start_time.year,
                          dt_range.start_time.month,
                          dt_range.start_time.day,
                          _round(dt_range.start_time.hour, fragment_hours),
                          tzinfo=dt_range.start_time.tzinfo)
    stop_time = datetime(dt_range.stop_time.year,
                         dt_range.stop_time.month,
                         dt_range.stop_time.day,
                         _round(dt_range.stop_time.hour, fragment_hours),
                         tzinfo=dt_range.stop_time.tzinfo)
    if stop_time != dt_range.stop_time:
        stop_time += timedelta(hours=fragment_hours)
    return DateTimeRange(start_time, stop_time)
Exemplo n.º 10
0
def _change_tz(dt: Union[DateTimeRange, datetime], tz):
    if type(dt) is datetime:
        if tz != dt.tzinfo:
            return datetime(dt.year,
                            dt.month,
                            dt.day,
                            dt.hour,
                            dt.minute,
                            dt.second,
                            dt.microsecond,
                            tzinfo=tz)
        else:
            return dt
    elif type(dt) is DateTimeRange:
        return DateTimeRange(_change_tz(dt.start_time, tz),
                             _change_tz(dt.stop_time, tz))
    else:
        raise TypeError()
Exemplo n.º 11
0
def _make_range(start_time, stop_time):
    dt_range = DateTimeRange(make_utc_datetime(start_time),
                             make_utc_datetime(stop_time))
    return _change_tz(dt_range, timezone.utc)
Exemplo n.º 12
0
 def test_is_convertible_to_dataframe(self):
     df = self.tt.to_dataframe()
     self.assertTrue(len(df) > 0)
     self.assertListEqual(list(df.columns), ['start_time', 'stop_time'])
     df_ranges = list(map(lambda row: DateTimeRange(*row), df.values))
     self.assertListEqual(df_ranges, list(self.tt))
Exemplo n.º 13
0
 def test_substract_with_wrong_type(self):
     with self.assertRaises(TypeError):
         DateTimeRange(datetime(2006, 1, 8, 3, 0, 0),
                       datetime(2006, 1, 8, 4, 0, 0)) - 1
Exemplo n.º 14
0
 def test_range_substract_timedelta(self):
     self.assertEqual(
         DateTimeRange(datetime(2006, 1, 8, 1, 0, 0),
                       datetime(2006, 1, 8, 2, 0, 0)) - timedelta(hours=1),
         DateTimeRange(datetime(2006, 1, 8, 0, 0, 0),
                       datetime(2006, 1, 8, 1, 0, 0)))
Exemplo n.º 15
0
class _DateTimeRangeTest(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    @data((DateTimeRange(datetime(2001, 1, 8, 1, 0, 0),
                         datetime(2001, 1, 8, 2, 0, 0)),
           DateTimeRange(datetime(2001, 1, 8, 0, 0, 0),
                         datetime(2001, 1, 8, 3, 0, 0)), []),
          (DateTimeRange(datetime(2002, 1, 8, 1, 0, 0),
                         datetime(2002, 1, 8, 2, 0, 0)),
           DateTimeRange(datetime(2002, 1, 8, 1, 0, 0),
                         datetime(2002, 1, 8, 2, 0, 0)), []),
          (DateTimeRange(datetime(2003, 1, 8, 0, 0, 0),
                         datetime(2003, 1, 8, 4, 0, 0)),
           DateTimeRange(datetime(
               2003, 1, 8, 1, 0, 0), datetime(2003, 1, 8, 2, 0, 0)), [
                   DateTimeRange(datetime(2003, 1, 8, 0, 0, 0),
                                 datetime(2003, 1, 8, 1, 0, 0)),
                   DateTimeRange(datetime(2003, 1, 8, 2, 0, 0),
                                 datetime(2003, 1, 8, 4, 0, 0)),
               ]), (DateTimeRange(datetime(2004, 1, 8, 0, 0, 0),
                                  datetime(2004, 1, 8, 4, 0, 0)),
                    DateTimeRange(datetime(
                        2004, 1, 8, 3, 0, 0), datetime(2004, 1, 8, 5, 0, 0)), [
                            DateTimeRange(datetime(2004, 1, 8, 0, 0, 0),
                                          datetime(2004, 1, 8, 3, 0, 0))
                        ]),
          (DateTimeRange(datetime(2005, 1, 8, 2, 0, 0),
                         datetime(2005, 1, 8, 4, 0, 0)),
           DateTimeRange(datetime(
               2005, 1, 8, 0, 0, 0), datetime(2005, 1, 8, 3, 0, 0)), [
                   DateTimeRange(datetime(2005, 1, 8, 3, 0, 0),
                                 datetime(2005, 1, 8, 4, 0, 0))
               ]), (DateTimeRange(datetime(2006, 1, 8, 2, 0, 0),
                                  datetime(2006, 1, 8, 4, 0, 0)),
                    DateTimeRange(datetime(
                        2006, 1, 8, 0, 0, 0), datetime(2006, 1, 8, 1, 0, 0)), [
                            DateTimeRange(datetime(2006, 1, 8, 2, 0, 0),
                                          datetime(2006, 1, 8, 4, 0, 0))
                        ]))
    @unpack
    def test_range_diff(self, range1, range2, expected):
        self.assertEqual(range1 - range2, expected)

    def test_range_substract_timedelta(self):
        self.assertEqual(
            DateTimeRange(datetime(2006, 1, 8, 1, 0, 0),
                          datetime(2006, 1, 8, 2, 0, 0)) - timedelta(hours=1),
            DateTimeRange(datetime(2006, 1, 8, 0, 0, 0),
                          datetime(2006, 1, 8, 1, 0, 0)))

    def test_add_with_wrong_type(self):
        with self.assertRaises(TypeError):
            DateTimeRange(datetime(2006, 1, 8, 3, 0, 0),
                          datetime(2006, 1, 8, 4, 0, 0)) + 1

    def test_substract_with_wrong_type(self):
        with self.assertRaises(TypeError):
            DateTimeRange(datetime(2006, 1, 8, 3, 0, 0),
                          datetime(2006, 1, 8, 4, 0, 0)) - 1

    @data((DateTimeRange(datetime(2000, 1, 1, 0, 0, 0),
                         datetime(2000, 1, 1, 1, 0, 0)), 1.,
           DateTimeRange(datetime(2000, 1, 1, 0, 0, 0),
                         datetime(2000, 1, 1, 1, 0, 0))),
          (DateTimeRange(datetime(2000, 1, 1, 1, 0, 0),
                         datetime(2000, 1, 1, 2, 0, 0)), 2.,
           DateTimeRange(datetime(2000, 1, 1, 0, 30, 0),
                         datetime(2000, 1, 1, 2, 30, 0))),
          (DateTimeRange(datetime(2000, 1, 1, 0, 30, 0),
                         datetime(2000, 1, 1, 2, 30, 0)), .5,
           DateTimeRange(datetime(2000, 1, 1, 1, 0, 0),
                         datetime(2000, 1, 1, 2, 0, 0))))
    @unpack
    def test_scale(self, dt_range, factor, expected):
        self.assertEqual(dt_range * factor, expected)

    @data(
        (DateTimeRange(datetime(2000, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
                       datetime(2000, 1, 1, 1, 0, 0, tzinfo=timezone.utc)), 1,
         DateTimeRange(datetime(2000, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
                       datetime(2000, 1, 1, 1, 0, 0, tzinfo=timezone.utc))),
        (DateTimeRange(datetime(2000, 1, 1, 0, 0, 1, tzinfo=timezone.utc),
                       datetime(2000, 1, 1, 0, 0, 2, tzinfo=timezone.utc)), 1,
         DateTimeRange(datetime(2000, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
                       datetime(2000, 1, 1, 1, 0, 0, tzinfo=timezone.utc))),
        (DateTimeRange(datetime(2000, 1, 1, 3, 30, 0, tzinfo=timezone.utc),
                       datetime(2000, 1, 1, 5, 30, 0,
                                tzinfo=timezone.utc)), 12,
         DateTimeRange(datetime(2000, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
                       datetime(2000, 1, 1, 12, 0, 0, tzinfo=timezone.utc))),
    )
    @unpack
    def test_range_rounding(self, dt_range, fragment_hours, expected):
        self.assertEqual(_round_for_cache(dt_range, fragment_hours), expected)

    @data((DateTimeRange(datetime(2000, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
                         datetime(2000, 1, 1, 12, 0, 0, tzinfo=timezone.utc)),
           DateTimeRange(datetime(2000, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
                         datetime(2000, 1, 1, 12, 0, 0,
                                  tzinfo=timezone.utc)), operator.eq),
          (DateTimeRange(datetime(2000, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
                         datetime(2000, 1, 1, 12, 0, 0, tzinfo=timezone.utc)),
           DateTimeRange(datetime(2000, 1, 1, 1, 0, 0, tzinfo=timezone.utc),
                         datetime(2000, 1, 1, 2, 0, 0,
                                  tzinfo=timezone.utc)), operator.contains),
          (DateTimeRange(datetime(2000, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
                         datetime(2000, 1, 1, 12, 0, 0, tzinfo=timezone.utc)),
           (datetime(2000, 1, 1, 1, 0, 0, tzinfo=timezone.utc),
            datetime(2000, 1, 1, 2, 0, 0,
                     tzinfo=timezone.utc)), operator.contains))
    @unpack
    def test_comparaisons(self, range1, range2, op):
        self.assertTrue(op(range1, range2))
Exemplo n.º 16
0
 def setUp(self):
     self.tt = TimeTable("Non empty timetable")
     for _ in range(10):
         self.tt.append(
             DateTimeRange(start_time=datetime.now(),
                           stop_time=datetime.now()))