Repository: gammapy/gammapy

Issue:
Writing of TemplateNPredModel allows no overwrite
The option `overwrite` is not passed to this line https://github.com/gammapy/gammapy/blob/6321261682b83528858a57baa34fd21b1100b9f4/gammapy/modeling/models/cube.py#L1007  
As a result an error is raised if the map is already written and one cannot overwrite it even if the option in the .write method is set to True
Expected: `self.map.write(self.filename, overwrite=overwrite)`

Discussion:

Patch:
diff --git a/gammapy/modeling/models/cube.py b/gammapy/modeling/models/cube.py
index 72f034d44..9640cff1e 100644
--- a/gammapy/modeling/models/cube.py
+++ b/gammapy/modeling/models/cube.py
@@ -1004,7 +1004,7 @@ class TemplateNPredModel(ModelBase):
         elif os.path.isfile(make_path(self.filename)) and not overwrite:
             log.warning("Template file already exits, and overwrite is False")
         else:
-            self.map.write(self.filename)
+            self.map.write(self.filename, overwrite=overwrite)
 
     @classmethod
     def from_dict(cls, data, **kwargs):


Test patch:
diff --git a/gammapy/modeling/models/tests/test_cube.py b/gammapy/modeling/models/tests/test_cube.py
index 1cfcc2de6..a75e0a09f 100644
--- a/gammapy/modeling/models/tests/test_cube.py
+++ b/gammapy/modeling/models/tests/test_cube.py
@@ -250,6 +250,7 @@ def test_background_model_io(tmpdir, background):
     filename = str(tmpdir / "test-bkg-file.fits")
     bkg = TemplateNPredModel(background, filename=filename)
     bkg.spectral_model.norm.value = 2.0
+    bkg.write(overwrite=False)
     bkg.write(overwrite=True)
     bkg_dict = bkg.to_dict()
     bkg_read = bkg.from_dict(bkg_dict)