Skip to content

Commit d22da47

Browse files
authored
Refactor xy_to_radec method and update theta calculation
1 parent 3279f6c commit d22da47

1 file changed

Lines changed: 21 additions & 16 deletions

File tree

src/pista/imaging.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def __init__(self, df, coords=None, tel_params=None, n_x=1000,
143143
self.n_y = n_y
144144
self.psf_oversamp = int(self.tel_params['psf_oversamp'])
145145
self.pixel_scale = self.tel_params['pixel_scale']
146-
self.theta = self.tel_params['theta']*np.pi/180
146+
self.theta = np.radians(self.tel_params['theta'])
147147

148148
self.response_funcs = self.tel_params['response_funcs']
149149
self.coeffs = self.tel_params['coeffs']
@@ -296,8 +296,7 @@ def check_df(self):
296296
if 'ra' not in self.df or 'dec' not in self.df.keys():
297297
if 'x' in self.df.keys() and 'y' in self.df.keys():
298298
print("Converting xy to ra-dec")
299-
self.df = self.xy_to_radec(self.df, self.n_x, self.n_y,
300-
self.pixel_scale,self.theta)
299+
self.df = self.xy_to_radec()
301300
else:
302301
raise Exception("'ra','dec','x',or 'y', \
303302
columns not found in input dataframe ")
@@ -346,21 +345,27 @@ def init_image_array(self, return_img=False):
346345

347346
return image
348347

349-
def xy_to_radec(self, df, n_x, n_y, pixel_scale, theta=0):
350-
348+
def xy_to_radec(self):
351349
w = WCS(naxis=2)
352-
w.wcs.crpix = [n_x//2, n_y//2]
353-
w.wcs.cdelt = np.array([-pixel_scale/3600, pixel_scale/3600])
350+
w.wcs.crpix = [self.n_x/2 + 0.5, self.n_y/2 + 0.5]
351+
w.wcs.cdelt = np.array([-self.pixel_scale/3600, self.pixel_scale/3600])
354352
w.wcs.crval = [10, 10]
355353
w.wcs.ctype = ['RA---TAN', 'DEC--TAN']
356-
357-
w.wcs.pc = np.array([[np.cos(theta), -np.sin(theta)],
358-
[np.sin(theta), np.cos(theta)]])
359-
360-
pos = np.array([df['x'], df['y']])
361-
coords = np.array(w.pixel_to_world_values(pos.T))
362-
df['ra'] = np.flip(coords[:, 0])
363-
df['dec'] = np.flip(coords[:, 1])
354+
355+
c, s = np.cos(self.theta), np.sin(self.theta)
356+
w.wcs.pc = np.array([[c, -s],
357+
[s, c]])
358+
359+
# Extract pixel coordinates
360+
x = self.df['x'].values
361+
y = self.df['y'].values
362+
363+
# Convert to sky
364+
ra, dec = w.pixel_to_world_values(x, y)
365+
366+
# Assign
367+
df['ra'] = ra
368+
df['dec'] = dec
364369

365370
return df
366371

@@ -385,7 +390,7 @@ def create_wcs(self, n_x, n_y, ra, dec, pixel_scale, theta=0):
385390
386391
"""
387392
w = WCS(naxis=2)
388-
w.wcs.crpix = [n_x//2, n_y//2]
393+
w.wcs.crpix = [n_x/2 + 0.5, n_y/2 + 0.5]
389394
w.wcs.cdelt = np.array([-pixel_scale/3600, pixel_scale/3600])
390395
w.wcs.crval = [ra, dec]
391396
w.wcs.ctype = ["RA---TAN", "DEC--TAN"]

0 commit comments

Comments
 (0)