diff --git a/changelog/331.bugfix.rst b/changelog/331.bugfix.rst new file mode 100644 index 00000000..21025eb2 --- /dev/null +++ b/changelog/331.bugfix.rst @@ -0,0 +1 @@ +The time range printed for a `~sunraster.SpectrogramSequence` now runs from the earliest to the latest time, even when time runs backwards along an axis (for example IRIS v34 rasters); before, it could print the start after the end. diff --git a/sunraster/spectrogram_sequence.py b/sunraster/spectrogram_sequence.py index 9a657008..68bdea0a 100644 --- a/sunraster/spectrogram_sequence.py +++ b/sunraster/spectrogram_sequence.py @@ -168,9 +168,10 @@ def __str__(self): ) data0 = self.data[0] if data0._time_name: - start_time = data0.time if data0.time.isscalar else data0.time.squeeze()[0] - data_1 = self.data[-1] - stop_time = data_1.time if data_1.time.isscalar else data_1.time.squeeze()[-1] + # Time can run backwards along an axis (for example IRIS v34 rasters), + # so the first and last values are not always the start and end. + start_time = min(cube.time.min() for cube in self.data) + stop_time = max(cube.time.max() for cube in self.data) time_period = start_time if start_time == stop_time else Time([start_time.iso, stop_time.iso]) else: time_period = None diff --git a/sunraster/tests/test_spectrogramsequence.py b/sunraster/tests/test_spectrogramsequence.py index 73d4825a..aa395b60 100644 --- a/sunraster/tests/test_spectrogramsequence.py +++ b/sunraster/tests/test_spectrogramsequence.py @@ -322,3 +322,16 @@ def test_sns_instrument_axes_types(input_sequence, expected_sns_axes_types): def test_slice_as_raster(): assert isinstance(sequence_DN[:, 0], SpectrogramSequence) + + +def test_str_time_range_when_time_runs_backwards(): + # IRIS v34 rasters step backwards in time, so the first time is the latest one. + times = Time("2017-01-01") + TimeDelta(np.arange(TIME_DIM_LEN * 2)[::-1], format="sec") + cubes = [] + for i in range(2): + cube = SpectrogramCube(SOURCE_DATA_DN, WCS0, u.ct, SOURCE_UNCERTAINTY_DN, meta=meta_exposure0) + cube.extra_coords.add("time", 0, times[i * TIME_DIM_LEN : (i + 1) * TIME_DIM_LEN]) + cubes.append(cube) + assert "Time Range: ['2017-01-01 00:00:00.000' '2017-01-01 00:00:03.000']" in str( + RasterSequence(cubes, meta=meta_seq, common_axis=0) + )