diff --git a/pyreduce/reduce.py b/pyreduce/reduce.py index 10b28e9c..ba2c6395 100755 --- a/pyreduce/reduce.py +++ b/pyreduce/reduce.py @@ -1173,6 +1173,8 @@ def __init__(self, *args, **config): self.dimensionality = config["dimensionality"] #:int: Number of detector offset steps, due to detector design self.nstep = config["nstep"] + #:int: How many columns to use in the 2D cross correlation alignment. 0 means all pixels (slow). + self.correlate_cols = config['correlate_cols'] #:float: fraction of columns, to allow individual orders to shift self.shift_window = config["shift_window"] #:str: elements of the spectral lamp @@ -1220,6 +1222,7 @@ def run(self, wavecal_master, wavecal_init): iterations=self.iterations, dimensionality=self.dimensionality, nstep=self.nstep, + correlate_cols=self.correlate_cols, shift_window=self.shift_window, element=self.element, medium=self.medium, diff --git a/pyreduce/settings/settings_pyreduce.json b/pyreduce/settings/settings_pyreduce.json index 19a83266..e83bbd4f 100644 --- a/pyreduce/settings/settings_pyreduce.json +++ b/pyreduce/settings/settings_pyreduce.json @@ -98,6 +98,7 @@ 6 ], "nstep": 0, + "correlate_cols": 0, "shift_window": 0.01, "element": "thar", "medium": "vac", diff --git a/pyreduce/settings/settings_schema.json b/pyreduce/settings/settings_schema.json index 171850ee..b650ab2c 100644 --- a/pyreduce/settings/settings_schema.json +++ b/pyreduce/settings/settings_schema.json @@ -369,6 +369,11 @@ "description": "Use only manual order alignment if true. When false still allow manual alignment after automatic alignment if plot is true", "type": "boolean" }, + "correlate_cols": { + "description": "The number of columns used for 2D cross correlation alignment. 0 means all pixels (slow).", + "type": "integer", + "minimum": 0 + }, "shift_window": { "description": "The fraction of the columns that each order can be shifted individually to align with the reference", "type": "number", diff --git a/pyreduce/wavelength_calibration.py b/pyreduce/wavelength_calibration.py index efe1ee75..01ae5923 100644 --- a/pyreduce/wavelength_calibration.py +++ b/pyreduce/wavelength_calibration.py @@ -266,6 +266,7 @@ def __init__( iterations=3, dimensionality="2D", nstep=0, + correlate_cols=0, shift_window=0.01, manual=False, polarim=False, @@ -290,6 +291,8 @@ def __init__( self.dimensionality = dimensionality #:bool: Whether to fit for pixel steps (offsets) in the detector self.nstep = nstep + #:int: How many columns to use in the 2D cross correlation alignment. 0 means all pixels (slow). + self.correlate_cols = correlate_cols #:float: Fraction if the number of columns to use in the alignment of individual orders. Set to 0 to disable self.shift_window = shift_window #:bool: Whether to manually align the reference instead of using cross correlation @@ -485,23 +488,32 @@ def align(self, obs, lines): # make image from lines img = self.create_image_from_lines(lines) + # Crop the image to speed up cross correlation + if self.correlate_cols != 0: + _slice = slice((self.ncol - self.correlate_cols) // 2, + (self.ncol + self.correlate_cols) // 2 + 1) + ccimg = img[:,_slice] + ccobs = obs[:,_slice] + else: + ccimg, ccobs = img, obs + # Cross correlate with obs image # And determine overall offset - correlation = signal.correlate2d(obs, img, mode="same") + correlation = signal.correlate2d(ccobs, ccimg, mode="same") offset_order, offset_x = np.unravel_index( np.argmax(correlation), correlation.shape ) if self.plot >= 2: plt.imshow(correlation, aspect="auto") - plt.vlines(offset_x, -0.5, self.nord - 0.5, color="red") - plt.hlines(offset_order, -0.5, self.ncol - 0.5, color="red") + plt.vlines(offset_x, -0.5, correlation.shape[0] - 0.5, color="red") + plt.hlines(offset_order, -0.5, correlation.shape[1] - 0.5, color="red") if self.plot_title is not None: plt.title(self.plot_title) plt.show() - offset_order = offset_order - img.shape[0] / 2 + 1 - offset_x = offset_x - img.shape[1] / 2 + 1 + offset_order = offset_order - ccimg.shape[0] / 2 + 1 + offset_x = offset_x - ccimg.shape[1] / 2 + 1 offset = [int(offset_order), int(offset_x)] # apply offset