def test_findepochrange_cdfepoch(): start_time = "2013-12-01T12:24:22.000" end_time = "2014-12-01T12:24:22.000" x = cdfepoch.parse([start_time, end_time]) time_array = np.arange(x[0], x[1], step=1000000) test_start = [2014, 8, 1, 8, 1, 54, 123] test_end = [2018, 1, 1, 1, 1, 1, 1] index = cdfepoch.findepochrange(time_array, starttime=test_start, endtime=test_end) # Test that the test_start is less than the first index, but more than one less assert time_array[index[0]] >= cdfepoch.compute(test_start) assert time_array[index[0]-1] <= cdfepoch.compute(test_start) assert time_array[index[-1]] <= cdfepoch.compute(test_end)
def test_findepochrange_cdftt2000(): start_time = "2004-03-01T12:24:22.351793238" end_time = "2004-03-01T12:28:22.351793238" x = cdfepoch.parse([start_time, end_time]) time_array = np.arange(x[0], x[1], step=1000000) test_start = [2004, 3, 1, 12, 25, 54, 123, 111, 98] test_end = [2004, 3, 1, 12, 26, 4, 123, 456, 789] index = cdfepoch.findepochrange(time_array, starttime=test_start, endtime=test_end) # Test that the test_start is less than the first index, but more than one less assert time_array[index[0]] >= cdfepoch.compute(test_start) assert time_array[index[0]-1] <= cdfepoch.compute(test_start) assert time_array[index[-1]] <= cdfepoch.compute(test_end) assert time_array[index[-1]+1] >= cdfepoch.compute(test_end)
def test_findepochrange_cdfepoch16(): start_time = "1978-03-10T03:24:22.351793238462" end_time = "1978-06-13T01:28:22.338327950466" x = cdfepoch.parse([start_time, end_time]) first_int_step = int((x[1].real - x[0].real) / 1000) second_int_step = int((x[1].imag - x[0].imag) / 1000) time_array = [] for i in range(0, 1000): time_array.append(x[0]+complex(first_int_step*i, second_int_step*i)) test_start = [1978, 6, 10, 3, 24, 22, 351, 793, 238, 462] test_end = [1978, 6, 12, 23, 11, 1, 338, 341, 416, 466] index = cdfepoch.findepochrange(time_array, starttime=test_start, endtime=test_end) # Test that the test_start is less than the first index, but more than one less assert time_array[index[0]].real >= cdfepoch.compute(test_start).real assert time_array[index[0]-1].real <= cdfepoch.compute(test_start).real assert time_array[index[-1]].real <= cdfepoch.compute(test_end).real assert time_array[index[-1]+1].real >= cdfepoch.compute(test_end).real
def test_compute_cdftt2000(dtime): random_time = [dtime.year, dtime.month, dtime.day, dtime.hour, dtime.minute, dtime.second, dtime.microsecond // 1000, # Millisecond randint(0, 999), # Microsecond randint(0, 999), # Nanosecond ] x = cdfepoch.breakdown(cdfepoch.compute(random_time)) for i, t in enumerate(x): assert t == random_time[i], f'Time {random_time} was not equal to {x}'
def test_compute_cdfepoch(dtime): ''' Using random numbers for the compute tests ''' random_time = [dtime.year, dtime.month, dtime.day, dtime.hour, dtime.minute, dtime.second, dtime.microsecond // 1000] x = cdfepoch.breakdown(cdfepoch.compute(random_time)) i = 0 for t in x: assert t == random_time[i], f'Time {random_time} was not equal to {x}' i += 1
def test_compute_cdfepoch16(dtime): random_time = [dtime.year, dtime.month, dtime.day, dtime.hour, dtime.minute, dtime.second, dtime.microsecond // 1000, # Millisecond randint(0, 999), # Microsecond randint(0, 999), # Nanosecond randint(0, 999), # Picosecond ] x = cdfepoch.breakdown(cdfepoch.compute(random_time)) i = 0 for t in x: assert t == random_time[i], f'Time {random_time} was not equal to {x}' i += 1
def test_compute_cdftt2000(): random_time = [] random_time.append(randint(0, 2018)) # Year random_time.append(randint(1, 12)) # Month random_time.append(randint(1, 28)) # Date random_time.append(randint(0, 23)) # Hour random_time.append(randint(0, 59)) # Minute random_time.append(randint(0, 59)) # Second random_time.append(randint(0, 999)) # Millisecond random_time.append(randint(0, 999)) # Microsecond random_time.append(randint(0, 999)) # Nanosecond x = cdfepoch.breakdown(cdfepoch.compute(random_time)) i = 0 for t in x: assert t == random_time[i], 'Time {} was not equal to {}'.format( random_time, x) i += 1
def test_compute_cdftt2000(): random_time = [] # These are the supported years for CDF files; see # https://spdf.gsfc.nasa.gov/pub/software/cdf/doc/cdf371/cdf371ug.pdf # page 55 random_time.append(randint(1709, 2292)) # Year random_time.append(randint(1, 12)) # Month random_time.append(randint(1, 28)) # Date random_time.append(randint(0, 23)) # Hour random_time.append(randint(0, 59)) # Minute random_time.append(randint(0, 59)) # Second random_time.append(randint(0, 999)) # Millisecond random_time.append(randint(0, 999)) # Microsecond random_time.append(randint(0, 999)) # Nanosecond x = cdfepoch.breakdown(cdfepoch.compute(random_time)) i = 0 for t in x: assert t == random_time[i], 'Time {} was not equal to {}'.format( random_time, x) i += 1
def _convert_cdf_time_types(data, atts, properties, to_datetime=False, to_unixtime=False): ''' # Converts CDF time types into either datetime objects, unixtime, or nothing # If nothing, ALL CDF_EPOCH16 types are converted to CDF_EPOCH, because xarray can't handle int64s ''' if not hasattr(data, '__len__'): data = [data] if to_datetime and to_unixtime: print( "Cannot convert to both unixtime and datetime. Continuing with conversion to unixtime." ) to_datetime = False # Convert all data in the "data" variable to unixtime or datetime if needed data_type = properties['Data_Type_Description'] if len(data) == 0 or data_type not in ('CDF_EPOCH', 'CDF_EPOCH16', 'CDF_TIME_TT2000'): new_data = data else: if to_datetime: new_data = cdfepoch.to_datetime(data) if 'UNITS' in atts: atts['UNITS']['Data'] = 'Datetime (UTC)' elif to_unixtime: new_data = cdfepoch.unixtime(data) if 'UNITS' in atts: atts['UNITS']['Data'] = 'seconds' else: if data_type == 'CDF_EPOCH16': new_data = cdfepoch.compute(cdfepoch.breakdown(data)[0:7]) else: new_data = data # Convert all the attributes in the "atts" dictionary to unixtime or datetime if needed new_atts = {} for att in atts: data_type = atts[att]['Data_Type'] data = atts[att]['Data'] if not hasattr(data, '__len__'): data = [data] if len(data) == 0 or data_type not in ('CDF_EPOCH', 'CDF_EPOCH16', 'CDF_TIME_TT2000'): new_atts[att] = data else: if to_datetime: new_atts[att] = cdfepoch.to_datetime(data) elif to_unixtime: new_atts[att] = cdfepoch.unixtime(data) else: if data_type == 'CDF_EPOCH16': new_atts[att] = cdfepoch.compute( cdfepoch.breakdown(data)[0:7]) else: new_atts[att] = data return new_data, new_atts