Repository: cta-observatory/ctapipe (python)

Issue:
TelescopeParameterLookup  does not check wether telescope is in attaached subarray
The following code fails:
```python
TelescopeParamtereLookup.tel[0]
*** KeyError: "TelescopeParameterLookup: no parameter value was set for telescope with tel_id=0. Please set it explicitly, or by telescope type or '*'."
```
which is ok, because it was really not set by '*', because it is not in the attached subarray.

The error message could be improved though, because `0` is a nonsenical telescope id.

I would prefer that for those cases the error message would be 
```
KeyError: "TelescopeParameterLookup: telescope with tel_id=0 is not in attached subarray.
```

https://github.com/cta-observatory/ctapipe/blob/2021ad199b079313b7368eeeea2757dec3e344a9/ctapipe/core/telescope_component.py#L208-L219

Pull request description:
Check if tel_id exists when looking up TelescopeParameters
Fixes #2233.

Patch:
diff --git a/ctapipe/core/telescope_component.py b/ctapipe/core/telescope_component.py
index 2bac22e6..6b49dae9 100644
--- a/ctapipe/core/telescope_component.py
+++ b/ctapipe/core/telescope_component.py
@@ -217,8 +217,10 @@ class TelescopeParameterLookup:
             try:
                 return self._value_for_tel_id[tel]
             except KeyError:
+                if tel not in self._subarray.tel:
+                    raise KeyError(f"No telescope with id {tel} in subarray")
                 raise KeyError(
-                    f"TelescopeParameterLookup: no "
+                    "TelescopeParameterLookup: no "
                     f"parameter value was set for telescope with tel_id="
                     f"{tel}. Please set it explicitly, "
                     f"or by telescope type or '*'."
diff --git a/docs/changes/2429.feature.rst b/docs/changes/2429.feature.rst
new file mode 100644
index 00000000..6dc4cec8
--- /dev/null
+++ b/docs/changes/2429.feature.rst
@@ -0,0 +1,3 @@
+In case no configuration is found for a telescope in ``TelescopeParameter``,
+it is now checked whether the telescope exists at all to provide a better
+error message.


Test patch:
diff --git a/ctapipe/core/tests/test_telescope_component.py b/ctapipe/core/tests/test_telescope_component.py
index 7aae1de4..e7f38995 100644
--- a/ctapipe/core/tests/test_telescope_component.py
+++ b/ctapipe/core/tests/test_telescope_component.py
@@ -342,3 +342,16 @@ def test_telescope_parameter_none(trait_type, mock_subarray):
 
     f = Foo(mock_subarray, bar=[("type", "*", 1), ("id", 1, None)])
     assert f.bar.tel[1] is None
+
+
+def test_telescope_parameter_nonexistent_telescope(mock_subarray):
+    class Foo(TelescopeComponent):
+        bar = IntTelescopeParameter(
+            default_value=None,
+            allow_none=True,
+        ).tag(config=True)
+
+    foo = Foo(subarray=mock_subarray)
+
+    with pytest.raises(KeyError, match="No telescope with id 0"):
+        foo.bar.tel[0]