26 | 26 |
from astropy.io import fits as pyfits
|
27 | 27 |
from PyWCSTools import wcs
|
28 | 28 |
import astropy.wcs as apywcs
|
29 | |
import numpy
|
|
29 |
import numpy as np
|
30 | 30 |
import locale
|
31 | 31 |
|
32 | 32 |
# if True, -1 from pixel coords to be zero-indexed like numpy. If False, use
|
|
115 | 115 |
self.headerSource.remove(z)
|
116 | 116 |
self.header=headerSource
|
117 | 117 |
|
118 | |
# If we use astropy.wcs, then we have issues if NAXIS != 2
|
119 | |
if useAstropyWCS == True and self.header['NAXIS'] > 2:
|
120 | |
self.header['NAXIS']=2
|
121 | |
for key in self.header.keys():
|
122 | |
if key[:5] == 'NAXIS' and len(key) == 6 and int(key[5]) > 2:
|
123 | |
del self.header[key]
|
124 | |
|
125 | 118 |
# This enables a shim to allow code written for astLib to use astropy.wcs underneath
|
126 | 119 |
self.useAstropyWCS=useAstropyWCS
|
127 | 120 |
if NUMPY_MODE == True:
|
|
263 | 256 |
def wcs2pix(self, RADeg, decDeg):
|
264 | 257 |
"""Returns the pixel coordinates corresponding to the input WCS
|
265 | 258 |
coordinates (given in decimal degrees). RADeg, decDeg can be single
|
266 | |
floats, or lists or numpy arrays.
|
|
259 |
floats, or lists or np arrays.
|
267 | 260 |
|
268 | 261 |
@rtype: list
|
269 | 262 |
@return: pixel coordinates in format [x, y]
|
270 | 263 |
|
271 | 264 |
"""
|
272 | 265 |
if self.useAstropyWCS == False:
|
273 | |
if type(RADeg) == numpy.ndarray or type(RADeg) == list:
|
274 | |
if type(decDeg) == numpy.ndarray or type(decDeg) == list:
|
|
266 |
if type(RADeg) == np.ndarray or type(RADeg) == list:
|
|
267 |
if type(decDeg) == np.ndarray or type(decDeg) == list:
|
275 | 268 |
pixCoords = []
|
276 | 269 |
for ra, dec in zip(RADeg, decDeg):
|
277 | 270 |
pix = wcs.wcs2pix(self.WCSStructure, float(ra), float(dec))
|
|
301 | 294 |
|
302 | 295 |
else:
|
303 | 296 |
# astropy.wcs shim
|
304 | |
pixCoords = self.AWCS.wcs_world2pix(RADeg, decDeg, self._apywcsOrigin)
|
305 | |
pixCoords = numpy.array(pixCoords).transpose().tolist()
|
|
297 |
if self.header['NAXIS'] == 2:
|
|
298 |
pixCoords = self.AWCS.all_world2pix(RADeg, decDeg, self._apywcsOrigin)
|
|
299 |
elif self.header['NAXIS'] == 3:
|
|
300 |
pixCoords = self.AWCS.all_world2pix(RADeg, decDeg, 0, self._apywcsOrigin)
|
|
301 |
else:
|
|
302 |
raise Exception("Not handling NAXIS > 3 with astropy.wcs shim")
|
|
303 |
pixCoords = np.array(pixCoords)[:2].transpose().tolist()
|
306 | 304 |
|
307 | 305 |
return pixCoords
|
308 | 306 |
|
|
316 | 314 |
|
317 | 315 |
"""
|
318 | 316 |
if self.useAstropyWCS == False:
|
319 | |
if type(x) == numpy.ndarray or type(x) == list:
|
320 | |
if type(y) == numpy.ndarray or type(y) == list:
|
|
317 |
if type(x) == np.ndarray or type(x) == list:
|
|
318 |
if type(y) == np.ndarray or type(y) == list:
|
321 | 319 |
WCSCoords = []
|
322 | 320 |
for xc, yc in zip(x, y):
|
323 | 321 |
if NUMPY_MODE == True:
|
|
332 | 330 |
WCSCoords = wcs.pix2wcs(self.WCSStructure, float(x), float(y))
|
333 | 331 |
else:
|
334 | 332 |
# astropy.wcs shim
|
335 | |
WCSCoords = self.AWCS.wcs_pix2world(x, y, self._apywcsOrigin)
|
336 | |
WCSCoords = numpy.array(WCSCoords).transpose().tolist()
|
|
333 |
if self.header['NAXIS'] == 2:
|
|
334 |
WCSCoords = self.AWCS.all_pix2world(x, y, self._apywcsOrigin)
|
|
335 |
elif self.header['NAXIS'] == 3:
|
|
336 |
WCSCoords = self.AWCS.all_pix2world(x, y, 0, self._apywcsOrigin)
|
|
337 |
else:
|
|
338 |
raise Exception("Not handling NAXIS > 3 with astropy.wcs shim")
|
|
339 |
WCSCoords = np.array(WCSCoords)[:2].transpose().tolist()
|
337 | 340 |
|
338 | 341 |
return WCSCoords
|
339 | 342 |
|
|
347 | 350 |
|
348 | 351 |
"""
|
349 | 352 |
|
350 | |
if self.useAstropyWCS == False:
|
351 | |
pixCoords = wcs.wcs2pix(self.WCSStructure, RADeg, decDeg)
|
352 | |
if pixCoords[0] >= 0 and pixCoords[0] < self.header['NAXIS1'] and \
|
353 | |
pixCoords[1] >= 0 and pixCoords[1] < self.header['NAXIS2']:
|
354 | |
return True
|
355 | |
else:
|
356 | |
return False
|
|
353 |
pixCoords = self.wcs2pix(RADeg, decDeg)
|
|
354 |
if pixCoords[0] >= 0 and pixCoords[0] < self.header['NAXIS1'] and \
|
|
355 |
pixCoords[1] >= 0 and pixCoords[1] < self.header['NAXIS2']:
|
|
356 |
return True
|
357 | 357 |
else:
|
358 | |
# astropy.wcs shim
|
359 | |
import IPython
|
360 | |
print("implement for shim")
|
361 | |
IPython.embed()
|
362 | |
sys.exit()
|
|
358 |
return False
|
363 | 359 |
|
364 | 360 |
|
365 | 361 |
def getRotationDeg(self):
|