def test_year_00_uses_fallback_year(self):
     # Ensure that a timebase error is not raised due to old date!
     #Other than the year 2000 or possibly 2100, no date values
     # can be all 0's
     dt = datetime(2012, 12, 12, 12, 12, 10, tzinfo=pytz.utc)
     # Test only without second and empty year
     hdf = MockHDF(
         {
             'Year':
             P('Year', np.ma.array([0, 0, 0, 0])),
             'Month':
             P('Month', np.ma.array([11, 11, 11, 11])),
             'Day':
             P('Day', np.ma.array([11, 11, 11, 11])),
             'Hour':
             P(
                 'Hour',
                 np.ma.array([11, 11, 11, 11],
                             mask=[True, False, False, False])),
             'Minute':
             P('Minute', np.ma.array([11, 11]), frequency=0.5),
         },
         duration=4)
     # add a masked invalid value
     hdf['Year'].array[2] = 50
     hdf['Year'].array[2] = np.ma.masked
     res, precise_timestamp = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res,
                      datetime(2012, 11, 11, 11, 11, 10, tzinfo=pytz.utc))
     self.assertFalse(precise_timestamp)
    def test_no_year_with_a_very_recent_fallback(self):
        """When fallback is a year after the flight and Year is not recorded,
        the result would be in the future. Ensure that a year is taken off if
        required.

        Generally Day and Month are recorded together and Year is optional.

        Should only Time be recorded, using the fallback date part is as good
        as you get.
        """
        # ensure current datetime is very recent
        dt = datetime.utcnow().replace(tzinfo=pytz.utc)
        # Year is not recorded, and the data is for the very end of the
        # previous year. NB: This test could fail if ran on the very last day
        # of the month!
        hdf = MockHDF(
            {
                'Month': P('Month', np.ma.array([12, 12
                                                 ])),  # last month of year
                'Day': P('Day', np.ma.array([31, 31])),  # last day
                'Hour': P('Hour', np.ma.array([23, 23])),  # last hour
                'Minute': P('Minute', np.ma.array([59, 59])),  # last minute
                'Second': P('Minute', np.ma.array([58, 59
                                                   ])),  # last two seconds
            },
            duration=2)
        res, precise_timestamp = _calculate_start_datetime(hdf, dt, dt)
        # result is a year behind the fallback datetime, even though the
        # fallback Year was used.
        self.assertEqual(res.year, datetime.now().year - 1)
        self.assertFalse(precise_timestamp)
 def test_no_year_with_a_very_recent_fallback(self):
     """When fallback is a year after the flight and Year is not recorded,
     the result would be in the future. Ensure that a year is taken off if
     required.
     
     Generally Day and Month are recorded together and Year is optional.
     
     Should only Time be recorded, using the fallback date part is as good
     as you get.
     """
     # ensure current datetime is very recent
     dt = datetime.now()
     # Year is not recorded, and the data is for the very end of the
     # previous year. NB: This test could fail if ran on the very last day
     # of the month!
     hdf = {
            'Month': P('Month', np.ma.array([12, 12])), # last month of year
            'Day':   P('Day',   np.ma.array([31, 31])), # last day
            'Hour':  P('Hour',  np.ma.array([23, 23])), # last hour
            'Minute':P('Minute',np.ma.array([59, 59])), # last minute
            'Second':P('Minute',np.ma.array([58, 59])), # last two seconds
            }
     res = _calculate_start_datetime(hdf, dt)
     # result is a year behind the fallback datetime, even though the
     # fallback Year was used.
     self.assertEqual(res.year, datetime.now().year -1)
     #self.assertEqual(res, datetime(2012,6,01,11,11,1))
    def test_calculate_start_datetime_multiple_valid_slices(self):
        aspd = load_array('airspeed_sample_masked_under_80.npz')
        duration = len(aspd)

        airspeed = P('Airspeed', array=aspd)
        year = P('Year', array=np.ma.array([2019]*duration))
        month = P('Month', array=np.ma.array([2]*duration))
        day = P('Day', array=np.ma.array([1]*duration))
        hour = P('Hour', array=np.ma.array([0]*duration))
        minute = P('Minute', array=np.ma.repeat(np.ma.arange(duration//60), 61)[:duration])
        second = P('Second', array=np.ma.array(np.tile(np.arange(60), duration//59))[:duration])
        values = {'Year': year, 'Month': month, 'Day': day, 'Hour': hour, 'Minute': minute, 'Second': second, 'Airspeed': airspeed}

        def hdf_get(arg):
            return values[arg]

        hdf = mock.Mock()
        hdf.duration = duration
        hdf.get = mock.Mock()
        hdf.get.side_effect = hdf_get
        start_dt = datetime(2019, 2, 1, 0, 0, tzinfo=pytz.utc)
        new_dt, _precise_timestamp, _conf = _calculate_start_datetime(hdf, datetime(2019, 2, 2, 0, 1, 1, tzinfo=pytz.utc),
                                           datetime(2012, 2, 2, 1, 2, 2, tzinfo=pytz.utc))

        self.assertEqual(new_dt, start_dt)
 def test_empty_year_no_seconds(self):
     # NB: 12's are the fallback_dt, 11's are the recorded time parameters
     dt = datetime(2012, 12, 12, 12, 12, 10, tzinfo=pytz.utc)
     # Test only without second and empty year
     hdf = MockHDF({
         'Month': P('Month', np.ma.array([11, 11, 11, 11])),
         'Day': P('Day', np.ma.array([])),
         'Hour': P('Hour', np.ma.array([11, 11, 11, 11], mask=[True, False, False, False])),
         'Minute': P('Minute', np.ma.array([11, 11]), frequency=0.5),
     }, duration=4)
     res = _calculate_start_datetime(hdf, dt, dt)
     # 9th second as the first sample (10th second) was masked
     self.assertEqual(res, datetime(2012, 11, 12, 11, 11, 10, tzinfo=pytz.utc))
 def test_empty_year_no_seconds(self):
     # NB: 12's are the fallback_dt, 11's are the recorded time parameters
     dt = datetime(2012,12,12,12,12,10)
     # Test only without second and empty year
     hdf = {
            'Month': P('Month',np.ma.array([11, 11, 11,11])),
            'Day':   P('Day',np.ma.array([])),
            'Hour':  P('Hour',np.ma.array([11,11,11,11], mask=[True, False, False, False])),
            'Minute':P('Minute',np.ma.array([11,11]), frequency=0.5),
            }
     res = _calculate_start_datetime(hdf, dt)
     # 9th second as the first sample (10th second) was masked
     self.assertEqual(res, datetime(2012,11,12,11,11,9))
 def test_calculate_start_datetime(self):
     """
     """
     hdf = MockHDF({
         'Year': P('Year', np.ma.array([2011])),
         'Month': P('Month', np.ma.array([11])),
         'Day': P('Day', np.ma.array([11])),
         'Hour': P('Hour', np.ma.array([11])),
         'Minute': P('Minute', np.ma.array([11])),
         'Second': P('Second', np.ma.array([11]))
     })
     dt = datetime(2012, 12, 12, 12, 12, 12, tzinfo=pytz.utc)
     # test with all params
     res, precise_timestamp = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res,
                      datetime(2011, 11, 11, 11, 11, 11, tzinfo=pytz.utc))
     self.assertTrue(precise_timestamp)
     # test without Year
     del hdf['Year']
     res, precise_timestamp = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res,
                      datetime(2012, 11, 11, 11, 11, 11, tzinfo=pytz.utc))
     self.assertFalse(precise_timestamp)
     # test without Month
     del hdf['Month']
     res, precise_timestamp = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res,
                      datetime(2012, 12, 11, 11, 11, 11, tzinfo=pytz.utc))
     self.assertFalse(precise_timestamp)
     # test without Day
     del hdf['Day']
     res, precise_timestamp = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res,
                      datetime(2012, 12, 12, 11, 11, 11, tzinfo=pytz.utc))
     self.assertFalse(precise_timestamp)
     # test without Hour
     del hdf['Hour']
     res, precise_timestamp = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res,
                      datetime(2012, 12, 12, 12, 11, 11, tzinfo=pytz.utc))
     self.assertFalse(precise_timestamp)
     # test without Minute
     del hdf['Minute']
     res, precise_timestamp = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res,
                      datetime(2012, 12, 12, 12, 12, 11, tzinfo=pytz.utc))
     self.assertFalse(precise_timestamp)
     # test without Second
     del hdf['Second']
     res, precise_timestamp = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res,
                      datetime(2012, 12, 12, 12, 12, 12, tzinfo=pytz.utc))
     self.assertFalse(precise_timestamp)
 def test_empty_year_no_seconds(self):
     # NB: 12's are the fallback_dt, 11's are the recorded time parameters
     dt = datetime(2012, 12, 12, 12, 12, 10, tzinfo=pytz.utc)
     # Test only without second and empty year
     hdf = MockHDF({
         'Month': P('Month', np.ma.array([11, 11, 11, 11])),
         'Day': P('Day', np.ma.array([])),
         'Hour': P('Hour', np.ma.array([11, 11, 11, 11], mask=[True, False, False, False])),
         'Minute': P('Minute', np.ma.array([11, 11]), frequency=0.5),
     }, duration=4)
     res, precise_timestamp, _ = _calculate_start_datetime(hdf, dt, dt)
     # 9th second as the first sample (10th second) was masked
     self.assertEqual(res, datetime(2012, 11, 12, 11, 11, 10, tzinfo=pytz.utc))
     self.assertFalse(precise_timestamp)
 def test_midnight_rollover(self):
     """
     When a flight starts just before midnight, the majority of the
     flight will be in the next day so the fallback_dt needs to adjust
     throughout the data otherwise it will try to force the next day's
     flight to appear to that of the previous.
     """
     # fallback is at start of the recording
     dt = datetime(2012, 12, 12, 23, 59, 58, tzinfo=pytz.utc)
     hdf = MockHDF({
         'Hour': P('Hour', np.ma.array([23, 23] + [0] * 18)),
         'Minute': P('Minute', np.ma.array([59, 59] + [0] * 18)),
         'Second': P('Minute', np.ma.array([58, 59] + range(18))),  # last two seconds and start of next flight
     }, duration=20)
     res = _calculate_start_datetime(hdf, dt, dt)
     # result is the exact start of the data for the timestamp (not a day before!)
     self.assertEqual(res, datetime(2012, 12, 12, 23, 59, 58, tzinfo=pytz.utc))
 def test_year_00_uses_fallback_year(self):
     # Ensure that a timebase error is not raised due to old date!
     #Other than the year 2000 or possibly 2100, no date values
     # can be all 0's        
     dt = datetime(2012,12,12,12,12,10)
     # Test only without second and empty year
     hdf = {'Year':  P('Year',np.ma.array([0, 0, 0, 0])),
            'Month': P('Month',np.ma.array([11, 11, 11,11])),
            'Day':   P('Day',np.ma.array([11, 11, 11,11])),
            'Hour':  P('Hour',np.ma.array([11,11,11,11], mask=[True, False, False, False])),
            'Minute':P('Minute',np.ma.array([11,11]), frequency=0.5),
            }
     # add a masked invalid value
     hdf['Year'].array[2] = 50
     hdf['Year'].array[2] = np.ma.masked
     res = _calculate_start_datetime(hdf, dt)
     self.assertEqual(res, datetime(2012,11,11,11,11,9))
 def test_midnight_rollover(self):
     """
     When a flight starts just before midnight, the majority of the
     flight will be in the next day so the fallback_dt needs to adjust
     throughout the data otherwise it will try to force the next day's
     flight to appear to that of the previous.
     """
     # fallback is at start of the recording
     dt = datetime(2012, 12, 12, 23, 59, 58, tzinfo=pytz.utc)
     hdf = MockHDF({
         'Hour': P('Hour', np.ma.array([23, 23] + [0] * 18)),
         'Minute': P('Minute', np.ma.array([59, 59] + [0] * 18)),
         'Second': P('Minute', np.ma.array([58, 59] + range(18))),  # last two seconds and start of next flight
     }, duration=20)
     res = _calculate_start_datetime(hdf, dt)
     # result is the exact start of the data for the timestamp (not a day before!)
     self.assertEqual(res, datetime(2012, 12, 12, 23, 59, 58, tzinfo=pytz.utc))
 def test_calculate_start_datetime(self):
     """
     """
     hdf = MockHDF({
         'Year': P('Year', np.ma.array([2011])),
         'Month': P('Month', np.ma.array([11])),
         'Day': P('Day', np.ma.array([11])),
         'Hour': P('Hour', np.ma.array([11])),
         'Minute': P('Minute', np.ma.array([11])),
         'Second': P('Second', np.ma.array([11]))
     })
     dt = datetime(2012, 12, 12, 12, 12, 12, tzinfo=pytz.utc)
     # test with all params
     res, precise_timestamp, _ = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res, datetime(2011, 11, 11, 11, 11, 11, tzinfo=pytz.utc))
     self.assertTrue(precise_timestamp)
     # test without Year
     del hdf['Year']
     res, precise_timestamp, _ = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res, datetime(2012, 11, 11, 11, 11, 11, tzinfo=pytz.utc))
     self.assertFalse(precise_timestamp)
     # test without Month
     del hdf['Month']
     res, precise_timestamp, _ = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res, datetime(2012, 12, 11, 11, 11, 11, tzinfo=pytz.utc))
     self.assertFalse(precise_timestamp)
     # test without Day
     del hdf['Day']
     res, precise_timestamp, _ = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res, datetime(2012, 12, 12, 11, 11, 11, tzinfo=pytz.utc))
     self.assertFalse(precise_timestamp)
     # test without Hour
     del hdf['Hour']
     res, precise_timestamp, _ = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res, datetime(2012, 12, 12, 12, 11, 11, tzinfo=pytz.utc))
     self.assertFalse(precise_timestamp)
     # test without Minute
     del hdf['Minute']
     res, precise_timestamp, _ = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res, datetime(2012, 12, 12, 12, 12, 11, tzinfo=pytz.utc))
     self.assertFalse(precise_timestamp)
     # test without Second
     del hdf['Second']
     res, precise_timestamp, _ = _calculate_start_datetime(hdf, dt, dt)
     self.assertEqual(res, datetime(2012, 12, 12, 12, 12, 12, tzinfo=pytz.utc))
     self.assertFalse(precise_timestamp)
 def test_calculate_start_datetime(self):
     """
     """
     hdf = {
         'Year':  P('Year',np.ma.array([2011])),
         'Month': P('Month',np.ma.array([11])),
         'Day':   P('Day',np.ma.array([11])),
         'Hour':  P('Hour',np.ma.array([11])),
         'Minute':P('Minute',np.ma.array([11])),
         'Second':P('Second',np.ma.array([11]))
     }
     dt = datetime(2012,12,12,12,12,12)
     # test with all params
     res = _calculate_start_datetime(hdf, dt)
     self.assertEqual(res, datetime(2011,11,11,11,11,11))
     # test without Year
     del hdf['Year']
     res = _calculate_start_datetime(hdf, dt)
     self.assertEqual(res, datetime(2012,11,11,11,11,11))
     # test without Month
     del hdf['Month']
     res = _calculate_start_datetime(hdf, dt)
     self.assertEqual(res, datetime(2012,12,11,11,11,11))
     # test without Day
     del hdf['Day']
     res = _calculate_start_datetime(hdf, dt)
     self.assertEqual(res, datetime(2012,12,12,11,11,11))
     # test without Hour
     del hdf['Hour']
     res = _calculate_start_datetime(hdf, dt)
     self.assertEqual(res, datetime(2012,12,12,12,11,11))
     # test without Minute
     del hdf['Minute']
     res = _calculate_start_datetime(hdf, dt)
     self.assertEqual(res, datetime(2012,12,12,12,12,11))
     # test without Second
     del hdf['Second']
     res = _calculate_start_datetime(hdf, dt)
     self.assertEqual(res, datetime(2012,12,12,12,12,12))