diff --git a/spaceKLIP/analysistools.py b/spaceKLIP/analysistools.py index 5d77bf6b..18b7e85f 100644 --- a/spaceKLIP/analysistools.py +++ b/spaceKLIP/analysistools.py @@ -36,7 +36,7 @@ from spaceKLIP.psf import get_offsetpsf, JWST_PSF from spaceKLIP.starphot import get_stellar_magnitudes, read_spec_file from spaceKLIP.pyklippipeline import get_pyklip_filepaths -from spaceKLIP.utils import write_starfile +from spaceKLIP.utils import write_starfile, set_surrounded_pixels import logging log = logging.getLogger(__name__) @@ -223,7 +223,7 @@ def raw_contrast(self, nanmask = np.pad(nanmask, pad) # Upsample array to improve centering. - samp = 15 #Upsampling factor + samp = 1 #Upsampling factor nanmask = nanmask.repeat(samp, axis=0).repeat(samp, axis=1) # Define rectangle edges @@ -231,7 +231,7 @@ def raw_contrast(self, thinrect_width = 2*samp #pixels cent_rect = [(center[0]+pad)*samp,(center[0]+pad)*samp, - (center[1]+pad)*samp,(center[1]+pad)*samp] + (center[1]+pad)*samp,(center[1]+pad)*samp] rect = [int(cent_rect[i]-(rect_width/2*(-1)**(i%2))) for i in range(4)] thinrect = [int(cent_rect[i]-(thinrect_width/2*(-1)**(i%2))) for i in range(4)] @@ -268,6 +268,7 @@ def raw_contrast(self, # Downsample, remove padding, and mask data nanmask = nanmask[::samp,::samp] nanmask = nanmask[pad:-pad,pad:-pad] + nanmask = set_surrounded_pixels(nanmask) data *= nanmask elif self.database.red[key]['EXP_TYPE'][j] in ['MIR_LYOT']: raise NotImplementedError() diff --git a/spaceKLIP/utils.py b/spaceKLIP/utils.py index 3528f23b..58ea5ece 100644 --- a/spaceKLIP/utils.py +++ b/spaceKLIP/utils.py @@ -618,11 +618,11 @@ def write_starfile(starfile, Parameters ---------- starfile : str - Path to original stellar spectrum file + Path to original stellar spectrum file. new_starfile_path : str - Path to new stellar spectrum file + Path to new stellar spectrum file. new_header : str - Header to be inserted + Header to be inserted. Returns ------- @@ -639,3 +639,31 @@ def write_starfile(starfile, new_starfile.write(text) else: new_starfile.write(new_header+text) + +def set_surrounded_pixels(array, user_value=np.nan): + """ + Identifies pixels in a 2D array surrounded by NaN values + on all eight sides and sets them to a user-defined value. + + Parameters + ---------- + array : numpy.ndarray + 2D array containing numeric values and NaNs. + user_value : float or any valid value type, optional + Value to set for pixels surrounded by NaNs on all eight sides. Defaults to NaN. + + Returns + ------- + numpy.ndarray + The input array with pixels surrounded by NaNs on all eight sides set to the user-defined value. + """ + nan_mask = np.isnan(array) + surrounded_pixels = ( + ~nan_mask[1:-1, 1:-1] & + nan_mask[:-2, :-2] & nan_mask[:-2, 1:-1] & nan_mask[:-2, 2:] & + nan_mask[1:-1, :-2] & nan_mask[1:-1, 2:] & + nan_mask[2:, :-2] & nan_mask[2:, 1:-1] & nan_mask[2:, 2:] + ) + + array[1:-1, 1:-1][surrounded_pixels] = user_value + return array \ No newline at end of file