diff --git a/hips/draw/paint.py b/hips/draw/paint.py index 84336cf..0608f5a 100644 --- a/hips/draw/paint.py +++ b/hips/draw/paint.py @@ -1,5 +1,6 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst import time +import warnings import numpy as np from typing import List, Tuple, Union, Dict, Any from astropy.wcs.utils import proj_plane_pixel_scales @@ -142,6 +143,8 @@ def warp_image(self, tile: HipsTile) -> np.ndarray: self.projection(tile), output_shape=self.geometry.shape, preserve_range=True, + mode="constant", + cval=np.nan, ) def run(self) -> np.ndarray: @@ -187,7 +190,7 @@ def _make_empty_sky_image(self): height=self.geometry.shape.height, fmt=self.tile_format, ) - return np.zeros(shape, dtype=np.float32) + return np.full(shape, fill_value=np.nan, dtype=np.float32) def draw_all_tiles(self): """Make an empty sky image and draw all the tiles.""" @@ -200,9 +203,12 @@ def draw_all_tiles(self): for tile in tiles: tile_image = self.warp_image(tile) - # TODO: put better algorithm here instead of summing pixels - # this can lead to pixels that are painted twice and become to bright - image += tile_image + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", + message="All-NaN slice encountered", + category=RuntimeWarning, + ) + image = np.nanmedian(np.stack([image, tile_image]), axis=0) # Store the result self.float_image = image diff --git a/hips/draw/ui.py b/hips/draw/ui.py index b12ee19..ee231bf 100644 --- a/hips/draw/ui.py +++ b/hips/draw/ui.py @@ -111,13 +111,12 @@ def write_image(self, filename: str, overwrite: bool = False) -> None: 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) + hdu.writeto(filename, overwrite=overwrite) else: + if overwrite is False and Path(filename).exists(): + raise FileExistsError(f"File {filename} already exists.") image = Image.fromarray(self.image) image.save(filename)