Repository: StingraySoftware/stingray (python)

Issue:
apply_gti_lists returns same result multiple times if input is a single GTI
### Description of the Bug

As follows

### Steps/Code to Replicate the Bug

The following unit test fails: 
```
    reader = FITSTimeseriesReader(fname, output_class=EventList)

    evs = list(reader.apply_gti_lists([
            [[80000000, 80001024]]
    ]))
    assert len(evs) == 1
```

### Expected Results

The list should have length of 1

### Actual Results

The event lists returned are actually two, despite giving a single gti list 

Pull request description:
Fix split at gtis
Resolve #857 

Patch:
diff --git a/docs/changes/858.bugfix.rst b/docs/changes/858.bugfix.rst
new file mode 100644
index 00000000..9aa6ffde
--- /dev/null
+++ b/docs/changes/858.bugfix.rst
@@ -0,0 +1,1 @@
+Fix issue when passing a single GTI to FitsTimeseriesReader.apply_gti_lists()
diff --git a/stingray/io.py b/stingray/io.py
index 6d92f17c..89e1b413 100644
--- a/stingray/io.py
+++ b/stingray/io.py
@@ -1139,23 +1139,24 @@ class FITSTimeseriesReader(object):
                 ev.write(output_file, fmt=fmt)
                 yield output_file
 
-        for i, gti in enumerate(new_gti_lists):
-            if len(gti) == 0:
-                continue
-
-            lower_edge, upper_edge = self._get_idx_from_time_range(gti[0, 0], gti[-1, 1])
-
-            ev = self[lower_edge : upper_edge + 1]
-            if hasattr(ev, "gti"):
-                ev.gti = gti
-
-            if root_file_name is not None:
-                new_file = root_file_name + f"_{i:002d}." + fmt.lstrip(".")
-                logger.info(f"Writing {new_file}")
-                ev.write(new_file, fmt=fmt)
-                yield new_file
-            else:
-                yield ev
+        else:
+            for i, gti in enumerate(new_gti_lists):
+                if len(gti) == 0:
+                    continue
+                gti = np.asarray(gti)
+                lower_edge, upper_edge = self._get_idx_from_time_range(gti[0, 0], gti[-1, 1])
+
+                ev = self[lower_edge : upper_edge + 1]
+                if hasattr(ev, "gti"):
+                    ev.gti = gti
+
+                if root_file_name is not None:
+                    new_file = root_file_name + f"_{i:002d}." + fmt.lstrip(".")
+                    logger.info(f"Writing {new_file}")
+                    ev.write(new_file, fmt=fmt)
+                    yield new_file
+                else:
+                    yield ev
 
     def _trace_nphots_in_file(self, nedges=1001):
         """Trace the number of photons as time advances in the file.


Test patch:
diff --git a/stingray/tests/test_io.py b/stingray/tests/test_io.py
index 8c22ab14..f81928d0 100644
--- a/stingray/tests/test_io.py
+++ b/stingray/tests/test_io.py
@@ -236,6 +236,40 @@ class TestFITSTimeseriesReader(object):
         all_ev = reader[:]
         assert np.all((all_ev.time > 80000000) & (all_ev.time < 80001024))
 
+    @pytest.mark.parametrize("root_file_name", [None, "test"])
+    @pytest.mark.parametrize("gti_kind", ["same", "one", "multiple"])
+    def test_read_apply_gti_lists(self, root_file_name, gti_kind):
+        reader = FITSTimeseriesReader(self.fname, output_class=EventList)
+        if gti_kind == "same":
+            gti_list = [reader.gti]
+        elif gti_kind == "one":
+            gti_list = [[[80000000, 80001024]]]
+        elif gti_kind == "multiple":
+            gti_list = [[[80000000, 80000512]], [[80000513, 80001024]]]
+
+        evs = list(reader.apply_gti_lists(gti_list, root_file_name=root_file_name))
+
+        # Check that the number of event lists is the same as the number of GTI lists we input
+        assert len(evs) == len(gti_list)
+
+        # If the root_file_name is not None, read the event lists and delete the file(s)
+        if root_file_name is not None:
+            ev_str = evs
+            evs = [EventList.read(ev, fmt=DEFAULT_FORMAT) for ev in ev_str]
+            for ev in ev_str:
+                os.unlink(ev)
+
+        for i, ev in enumerate(evs):
+            # Check that the gtis of the output event lists are the same we input
+            assert np.allclose(ev.gti, gti_list[i])
+
+    def test_read_apply_gti_lists_ignore_empty(self):
+        reader = FITSTimeseriesReader(self.fname, output_class=EventList)
+        gti_list = [[], [[80000000, 80000512]], [[80000513, 80001024]]]
+        evs = list(reader.apply_gti_lists(gti_list))
+        assert np.allclose(evs[0].gti, gti_list[1])
+        assert np.allclose(evs[1].gti, gti_list[2])
+
     def test_read_fits_timeseries_by_nsamples(self):
         reader = FITSTimeseriesReader(self.fname, output_class=EventList)
         # Full slice