Skip to content

Commit

Permalink
Merge pull request #129 from adl1995/io/overwrite
Browse files Browse the repository at this point in the history
Add overwrite option in write_image function for FITS files
  • Loading branch information
cdeil committed Jul 21, 2018
2 parents ead742f + 74acfea commit 023abe2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 6 additions & 1 deletion hips/draw/tests/test_ui.py
Expand Up @@ -71,6 +71,11 @@ def test_make_sky_image(tmpdir, pars):
assert_allclose(np.sum(result.image, dtype=float), pars['data_sum'])
assert_allclose(result.image[200, 994], pars['data_1'])
assert_allclose(result.image[200, 995], pars['data_2'])
result.write_image(str(tmpdir / 'test.' + pars['file_format']))
result.write_image(str(tmpdir / 'test.' + pars['file_format']), False)

# Try and overwrite the image.
with pytest.raises(FileExistsError):
result.write_image(str(tmpdir / 'test.' + pars['file_format']), False)

result.plot()
result.report()
8 changes: 7 additions & 1 deletion hips/draw/ui.py
Expand Up @@ -2,6 +2,7 @@
"""The high-level end user interface (UI)."""
import numpy as np
from PIL import Image
from pathlib import Path
from astropy.io import fits
from typing import List, Union
from ..utils.wcs import WCSGeometry
Expand Down Expand Up @@ -100,14 +101,19 @@ def from_painter(cls, painter: HipsPainter) -> 'HipsDrawResult':
stats=painter._stats,
)

def write_image(self, filename: str) -> None:
def write_image(self, filename: str, overwrite: bool = False) -> None:
"""Write image to file.
Parameters
----------
filename : str
Filename
overwrite : bool
Overwrite the output file, if it exists
"""
if overwrite == False and Path(filename).exists():
raise FileExistsError(f"File {filename} already exists.")

if self.tile_format == 'fits':
hdu = fits.PrimaryHDU(data=self.image, header=self.geometry.fits_header)
hdu.writeto(filename)
Expand Down

0 comments on commit 023abe2

Please sign in to comment.