Skip to content
This repository was archived by the owner on Nov 26, 2023. It is now read-only.

Commit 3a7baa9

Browse files
committed
Added other pixel scales to calc_delta, and stopped passing the hdulist places only its header needed to be. For Ticket #138.
1 parent afc52cd commit 3a7baa9

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

mtpipeline/ephem/build_master_finders_table.py

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def cgi_session(command_list):
172172
return html
173173

174174

175-
def calc_delta(hdulist, record):
175+
def calc_delta(header, record):
176176
"""Calculate JPL coordinate and HST reference pixel deltas.
177177
178178
The calculation is performed in degrees and then converted to
@@ -182,8 +182,8 @@ def calc_delta(hdulist, record):
182182
`astrolib.coords` library.
183183
184184
Parameters:
185-
hdulist : HDUList instance
186-
pyfits.io.fits.hdulist object
185+
header : header instance
186+
astropy.io.fits.header object
187187
record : dict
188188
The record dictionary
189189
@@ -205,19 +205,36 @@ def calc_delta(hdulist, record):
205205
assert isinstance(record, dict), \
206206
'Expected dict got ' + str(type(record))
207207

208-
crval1 = hdulist[0].header['CRVAL1']
209-
crval2 = hdulist[0].header['CRVAL2']
208+
crval1 = header['CRVAL1']
209+
crval2 = header['CRVAL2']
210210

211211
# Convert the coordinates to coords instances in degrees.
212212
record = convert_coords(record)
213213
refpic_coords = coords.Degrees((crval1, crval2))
214214
crval1, crval2 = refpic_coords.a1, refpic_coords.a2
215215

216+
217+
# Set the pixel scale of the detector, in arcsec/pixel:
218+
# WFPC2 has no detector keyword
219+
instrument = header['instrume']
220+
try:
221+
detector = header['detector']
222+
except KeyError:
223+
detector = instrument
224+
225+
px_scales = {'WFPC2' : 0.046, #this is only the planetary chip
226+
'WFC' : 0.049,
227+
'HRC' : 0.027,
228+
'SBC' : 0.032,
229+
'UVIS' : 0.04,
230+
'IR' : 0.13}
231+
232+
px_scale = px_scales[detector]
233+
216234
# Take the difference and convert to pixels.
217235
# RA increases to the East (left) so we switch the sign on the delta.
218-
# Note these values are for WFPC2 only.
219-
delta_x = -1 * (record['jpl_ra'] - crval1) * (3600. / 0.05)
220-
delta_y = (record['jpl_dec'] - crval2) * (3600. / 0.05)
236+
delta_x = -1 * (record['jpl_ra'] - crval1) * (3600. / px_scale)
237+
delta_y = (record['jpl_dec'] - crval2) * (3600. / px_scale)
221238

222239
assert isinstance(delta_x, float), \
223240
'Expected float got ' + str(type(delta_x))
@@ -226,7 +243,7 @@ def calc_delta(hdulist, record):
226243
return delta_x, delta_y
227244

228245

229-
def calc_pixel_position(hdulist, delta_x, delta_y):
246+
def calc_pixel_position(header, delta_x, delta_y):
230247
"""Return the x and y position of the ephemeris in pixel space.
231248
232249
This function takes the ephemerides deltas (in pixels) and applies
@@ -235,8 +252,8 @@ def calc_pixel_position(hdulist, delta_x, delta_y):
235252
space.
236253
237254
Parameters:
238-
hdulist : HDUList instance
239-
pyfits.io.fits.hdulist object
255+
header : header instance
256+
astropy.io.fits.hdulist.header object
240257
delta_x : float
241258
Float of the delta_x shift in pixels.
242259
delta_y : float
@@ -252,8 +269,8 @@ def calc_pixel_position(hdulist, delta_x, delta_y):
252269
'Expected float type got ' + str(type(delta_x))
253270
assert isinstance(delta_y, float), \
254271
'Expected float type got ' + str(type(delta_y))
255-
ephem_x = hdulist[0].header['CRPIX1'] + delta_x
256-
ephem_y = hdulist[0].header['CRPIX2'] + delta_y
272+
ephem_x = header['CRPIX1'] + delta_x
273+
ephem_y = header['CRPIX2'] + delta_y
257274
assert isinstance(ephem_x, float), \
258275
'Expected float type got ' + str(type(ephem_x))
259276
assert isinstance(ephem_y, float), \
@@ -401,7 +418,7 @@ def make_all_moon_dict(targname):
401418
return all_moon_dict
402419

403420

404-
def insert_record(hdulist, moon_dict, master_images_id):
421+
def insert_record(header, moon_dict, master_images_id):
405422
"""Insert a new record into the `master_finders` table.
406423
407424
If there is no magnitude or diameter information, this raises an
@@ -412,8 +429,8 @@ def insert_record(hdulist, moon_dict, master_images_id):
412429
-999 in `parse_jpl_cgi` so these exceptions should never happen.
413430
414431
Parameters:
415-
hdulist : HDUList instance
416-
pyfits.io.fits.hdulist object
432+
header : FITS header instance
433+
astropy.io.fits.hdulist.header object
417434
all_moon_dict : dict
418435
A dict containing the JPL id and object information
419436
for each
@@ -436,8 +453,8 @@ def insert_record(hdulist, moon_dict, master_images_id):
436453
record.object_name = moon_dict['object']
437454
record.jpl_ra = moon_dict['jpl_ra']
438455
record.jpl_dec = moon_dict['jpl_dec']
439-
delta_x, delta_y = calc_delta(hdulist, moon_dict)
440-
ephem_x, ephem_y = calc_pixel_position(hdulist, delta_x, delta_y)
456+
delta_x, delta_y = calc_delta(header, moon_dict)
457+
ephem_x, ephem_y = calc_pixel_position(header, delta_x, delta_y)
441458
record.ephem_x = int(ephem_x)
442459
record.ephem_y = int(ephem_y)
443460
try:
@@ -502,16 +519,16 @@ def parse_jpl_cgi(data):
502519
soe_switch = True
503520

504521

505-
def update_record(hdulist, moon_dict, master_images_id): # changed this function. added hdulist as a parameter and added the ephemerides calculations.
522+
def update_record(header, moon_dict, master_images_id): # changed this function. added header as a parameter and added the ephemerides calculations.
506523
'''
507524
Update a record in the master_finders table.
508525
'''
509526
update_dict = {}
510527
update_dict['object_name'] = moon_dict['object']
511528
update_dict['jpl_ra'] = moon_dict['jpl_ra']
512529
update_dict['jpl_dec'] = moon_dict['jpl_dec']
513-
delta_x, delta_y = calc_delta(hdulist, moon_dict)
514-
ephem_x, ephem_y = calc_pixel_position(hdulist, delta_x, delta_y)
530+
delta_x, delta_y = calc_delta(header, moon_dict)
531+
ephem_x, ephem_y = calc_pixel_position(header, delta_x, delta_y)
515532
update_dict['ephem_x'] = int(ephem_x)
516533
update_dict['ephem_y'] = int(ephem_y)
517534
try:
@@ -594,6 +611,7 @@ def ephem_main(filename, reproc=False):
594611
# Extract needed header information
595612
file_dict = {}
596613
with fits.open(filename,mode='readonly') as hdulist:
614+
header = hdulist.header
597615
file_dict['date_obs'] = hdulist[0].header['date-obs']
598616
file_dict['time_obs'] = hdulist[0].header['time-obs']
599617
file_dict['targname'] = hdulist[0].header['targname']
@@ -614,7 +632,7 @@ def ephem_main(filename, reproc=False):
614632
if master_finders_count == 0:
615633
jpl_dict = get_jpl_data(moon_dict)
616634
moon_dict.update(jpl_dict)
617-
insert_record(hdulist, moon_dict, master_images_query.id)
635+
insert_record(header, moon_dict, master_images_query.id)
618636

619637
# When a record exists, check if it has jpl_ra info. If it
620638
# doesn't then update.
@@ -626,7 +644,7 @@ def ephem_main(filename, reproc=False):
626644
if master_finders_count == 1 or reproc == True:
627645
jpl_dict = get_jpl_data(moon_dict)
628646
moon_dict.update(jpl_dict)
629-
update_record(hdulist, moon_dict, master_images_query.id)
647+
update_record(header, moon_dict, master_images_query.id)
630648

631649
session.close()
632650

0 commit comments

Comments
 (0)