Skip to content

Commit

Permalink
Merge pull request #65 from tronsgaard/tronsgaard-patch-2
Browse files Browse the repository at this point in the history
Set the number of columns used for 2D cross correlation alignment
  • Loading branch information
AWehrhahn authored Jul 28, 2023
2 parents 27398d2 + a42aed3 commit 74e6f44
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pyreduce/reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions pyreduce/settings/settings_pyreduce.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
6
],
"nstep": 0,
"correlate_cols": 0,
"shift_window": 0.01,
"element": "thar",
"medium": "vac",
Expand Down
5 changes: 5 additions & 0 deletions pyreduce/settings/settings_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 17 additions & 5 deletions pyreduce/wavelength_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def __init__(
iterations=3,
dimensionality="2D",
nstep=0,
correlate_cols=0,
shift_window=0.01,
manual=False,
polarim=False,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 74e6f44

Please sign in to comment.