def test_no_day_larger_than_366(self): """ Check for error if day is larger than 366 (leap year) :return: Nothing """ print("Testing that an error is thrown if a date exceeds 365") with self.assertRaises(Exception) as date_out_of_scope: cal2dec(11, 31, 300, 300) self.assertTrue('Day is out of scope of the year' in str( date_out_of_scope.exception))
def test_throws_error_if_month_too_large(self): """ Check that an error is thrown if the month exceeds 12 :return: Nothing """ print("Testing exception is thrown if month is out of scope...") with self.assertRaises(Exception) as month_out_of_scope: cal2dec(13, 1, 0, 0) self.assertTrue( 'Month is out of scope' in str(month_out_of_scope.exception))
def test_returns_365_for_last_day(self): """ Check that the last hour in the last day of the last month return 365 :return: Nothing """ print("Testing that the 24th hour on the last day returns 365") self.assertEqual(cal2dec(11, 31, 24, 0), 365, "last value should be 365")
def test_returns_0_for_first_day(self): """ Check that the first possible day is 0 :return: Nothing """ print("Testing that day 1 returns 0...") self.assertEqual(cal2dec(0, 1, 0, 0), 0, "Should return 0 on first day")
def test_returns_float(self): """ Check return type is a float :return: Nothing """ print("Testing return type is a float...") date = cal2dec(0, 1) self.assertTrue(isinstance(date, float), "cal2dec should return a float")
def change_dates(cal_dates): """ Seperates date into year, month, day, hour, minute :param cal_dates: array of date as one continuous int eg (01/02/1974, hour 3, minute 5, second 44 = 19740102030544) :return: decimalised date """ # might need to return zeroes for bad dates dec_dates = np.full(cal_dates.__len__(), 0, dtype=float) # cannot iterate through integers, so convert calendar dates into array of strings cal_dates_string = list(map(str, cal_dates)) # go through and decimalise dates for i in range(0, cal_dates_string.__len__()): try: # select the separate date entities (year, month, day, etc) hour = 0 minute = 0 year = int(cal_dates_string[i][0:4]) month = int(cal_dates_string[i][4:6]) day = int(cal_dates_string[i][6:8]) if cal_dates_string[i].__len__() > 9: hour = int(cal_dates_string[i][8:10]) if cal_dates_string[i].__len__() > 11: minute = int(cal_dates_string[i][10:12]) if 13 > month > 0: if 32 > day > 0: day = year + (cal2dec(month - 1, day, hour, minute) / 365) dec_dates[i] = day except ValueError: print("Date is incorrect length or format") continue return dec_dates