Repository: gammapy/gammapy

Issue:
allow datastore.obs to return all observations
currently one needs to supply a list of observations ids. What if I want all of them? Is that a valid use case?


Discussion:
To get get all observations, currently you'd first get a list of all obs_id via:
```
obs_id = data_store.obs_table['OBS_ID'].data
```
(in this case a Numpy array, can also call `list()` on it if you like that)
And then you could use either
```
obs_list = data_store.obs_list(obs_id)
```
and then access the `obs` you like from that list, e.g. via
```
obs = obs_list[42]
```
or you could loop over the `obs_id` in the calling code and access `obs` like this:
```
for oid in obs_id:
    obs = data_store.obs(oid)
    # do something
```

Given that we said that we want to introduce an "observations" container (see #1303) and have the data store as just one way to get that, the pattern using `obs_list` is preferable for now IMO.

I'm not sure if the use case to want all obs is very common.

My suggestion would be to improve the docs instead and show an example:
See http://docs.gammapy.org/dev/api/gammapy.data.DataStore.html

If we think it is common to access all available obs, we could add a default `obs_id=None` in [DataStore.obs_list](http://docs.gammapy.org/dev/api/gammapy.data.DataStore.html#gammapy.data.DataStore.obs_list) and in that case put:
```
if obs_id is None:
    obs_id = self.obs_table['OBS_ID'].data
```
In that case adding a docs example would still be needed.

@mackaiver - What do you think we should put? You or me?
I think it's okay to ask people to put this one line

```
obs_id = data_store.obs_table['OBS_ID'].data
```

if they want all observations

Closing this old issue. We're discussing a one-line convenience addition, and opinions are split whether it's useful. If someone send a PR to improve gammapy.data (or anything really), I'll hit the merge button.

Resolving patch:
diff --git a/gammapy/data/data_store.py b/gammapy/data/data_store.py
--- a/gammapy/data/data_store.py
+++ b/gammapy/data/data_store.py
@@ -236,13 +236,13 @@ def obs(self, obs_id):
         """
         return DataStoreObservation(obs_id=int(obs_id), data_store=self)
 
-    def get_observations(self, obs_id, skip_missing=False):
+    def get_observations(self, obs_id=None, skip_missing=False):
         """Generate a `~gammapy.data.Observations`.
 
         Parameters
         ----------
         obs_id : list
-            Observation IDs.
+            Observation IDs (default of ``None`` means "all")
         skip_missing : bool, optional
             Skip missing observations, default: False
 
@@ -251,6 +251,9 @@ def get_observations(self, obs_id, skip_missing=False):
         observations : `~gammapy.data.Observations`
             Container holding a list of `~gammapy.data.DataStoreObservation`
         """
+        if obs_id is None:
+            obs_id = self.obs_table["OBS_ID"].data
+
         obs_list = []
         for _ in obs_id:
             try:


Test patch:
diff --git a/gammapy/data/tests/test_data_store.py b/gammapy/data/tests/test_data_store.py
--- a/gammapy/data/tests/test_data_store.py
+++ b/gammapy/data/tests/test_data_store.py
@@ -66,6 +66,10 @@ def test_datastore_get_observations(data_store):
     observations = data_store.get_observations([23523, 23592])
     assert observations[0].obs_id == 23523
 
+    # Test that default is all observations
+    observations = data_store.get_observations()
+    assert len(observations) == 105
+
     with pytest.raises(ValueError):
         data_store.get_observations([11111, 23592])