def test_wcs_keyword_removal_for_wcs_test_files(): """ Test, for the WCS test files, that keyword removall works as expected. Those cover a much broader range of WCS types than test_wcs_keywords_removed_from_header """ from astropy.nddata.ccddata import _generate_wcs_and_update_header from astropy.nddata.ccddata import _KEEP_THESE_KEYWORDS_IN_HEADER keepers = set(_KEEP_THESE_KEYWORDS_IN_HEADER) wcs_headers = get_pkg_data_filenames('../../wcs/tests/data', pattern='*.hdr') for hdr in wcs_headers: # Skip the files that are expected to be bad... if 'invalid' in hdr or 'nonstandard' in hdr or 'segfault' in hdr: continue header_string = get_pkg_data_contents(hdr) wcs = WCS(header_string) header = wcs.to_header(relax=True) new_header, new_wcs = _generate_wcs_and_update_header(header) # Make sure all of the WCS-related keywords have been removed. assert not (set(new_header) & set(new_wcs.to_header(relax=True)) - keepers) # Check that the new wcs is the same as the old. new_wcs_header = new_wcs.to_header(relax=True) for k, v in new_wcs_header.items(): if isinstance(v, str): assert header[k] == v else: np.testing.assert_almost_equal(header[k], v)
def test_wcs_keyword_removal_for_wcs_test_files(): """ Test, for the WCS test files, that keyword removall works as expected. Those cover a much broader range of WCS types than test_wcs_keywords_removed_from_header """ from astropy.nddata.ccddata import _generate_wcs_and_update_header from astropy.nddata.ccddata import _KEEP_THESE_KEYWORDS_IN_HEADER keepers = set(_KEEP_THESE_KEYWORDS_IN_HEADER) wcs_headers = get_pkg_data_filenames('../../wcs/tests/data', pattern='*.hdr') for hdr in wcs_headers: # Skip the files that are expected to be bad... if 'invalid' in hdr or 'nonstandard' in hdr or 'segfault' in hdr: continue header_string = get_pkg_data_contents(hdr) wcs = WCS(header_string) header = wcs.to_header(relax=True) new_header, new_wcs = _generate_wcs_and_update_header(header) # Make sure all of the WCS-related keywords have been removed. assert not (set(new_header) & set(new_wcs.to_header(relax=True)) - keepers) # Check that the new wcs is the same as the old. new_wcs_header = new_wcs.to_header(relax=True) for k, v in new_wcs_header.items(): if isinstance(v, str): assert header[k] == v else: np.testing.assert_almost_equal(header[k], v)
def test_solve_astrometry_hdu(tmpdir): data, index = get_image_index() hdu = fits.open(data)[0] header, wcs = _generate_wcs_and_update_header(hdu.header) hdu.header = header nwcs = solve_astrometry_hdu(hdu, return_wcs=True) assert_true(isinstance(nwcs, WCS)) assert_equal(nwcs.naxis, 2) compare_wcs(wcs, nwcs)
def test_wcs_keyword_removal_for_wcs_test_files(): """ Test, for the WCS test files, that keyword removal works as expected. Those cover a much broader range of WCS types than test_wcs_keywords_removed_from_header. Includes regression test for #8597 """ from astropy.nddata.ccddata import _generate_wcs_and_update_header from astropy.nddata.ccddata import (_KEEP_THESE_KEYWORDS_IN_HEADER, _CDs, _PCs) keepers = set(_KEEP_THESE_KEYWORDS_IN_HEADER) wcs_headers = get_pkg_data_filenames('../../wcs/tests/data', pattern='*.hdr') for hdr in wcs_headers: # Skip the files that are expected to be bad... if ('invalid' in hdr or 'nonstandard' in hdr or 'segfault' in hdr or 'chandra-pixlist-wcs' in hdr): continue header_string = get_pkg_data_contents(hdr) header = fits.Header.fromstring(header_string) wcs = WCS(header_string) header_from_wcs = wcs.to_header(relax=True) new_header, new_wcs = _generate_wcs_and_update_header(header) new_wcs_header = new_wcs.to_header(relax=True) # Make sure all of the WCS-related keywords generated by astropy # have been removed. assert not (set(new_header) & set(new_wcs_header) - keepers) # Check that new_header contains no remaining WCS information. # Specifically, check that # 1. The combination of new_header and new_wcs does not contain # both PCi_j and CDi_j keywords. See #8597. # Check for 1 final_header = new_header + new_wcs_header final_header_set = set(final_header) if _PCs & final_header_set: assert not (_CDs & final_header_set) elif _CDs & final_header_set: assert not (_PCs & final_header_set) # Check that the new wcs is the same as the old. for k, v in new_wcs_header.items(): if isinstance(v, str): assert header_from_wcs[k] == v else: np.testing.assert_almost_equal(header_from_wcs[k], v)
def test_wcs_keyword_removal_for_wcs_test_files(): """ Test, for the WCS test files, that keyword removal works as expected. Those cover a much broader range of WCS types than test_wcs_keywords_removed_from_header. Includes regression test for #8597 """ from astropy.nddata.ccddata import _generate_wcs_and_update_header from astropy.nddata.ccddata import (_KEEP_THESE_KEYWORDS_IN_HEADER, _CDs, _PCs) keepers = set(_KEEP_THESE_KEYWORDS_IN_HEADER) wcs_headers = get_pkg_data_filenames('../../wcs/tests/data', pattern='*.hdr') for hdr in wcs_headers: # Skip the files that are expected to be bad... if 'invalid' in hdr or 'nonstandard' in hdr or 'segfault' in hdr: continue header_string = get_pkg_data_contents(hdr) header = fits.Header.fromstring(header_string) wcs = WCS(header_string) header_from_wcs = wcs.to_header(relax=True) new_header, new_wcs = _generate_wcs_and_update_header(header) new_wcs_header = new_wcs.to_header(relax=True) # Make sure all of the WCS-related keywords generated by astropy # have been removed. assert not (set(new_header) & set(new_wcs_header) - keepers) # Check that new_header contains no remaining WCS information. # Specifically, check that # 1. The combination of new_header and new_wcs does not contain # both PCi_j and CDi_j keywords. See #8597. # Check for 1 final_header = new_header + new_wcs_header final_header_set = set(final_header) if _PCs & final_header_set: assert not (_CDs & final_header_set) elif _CDs & final_header_set: assert not (_PCs & final_header_set) # Check that the new wcs is the same as the old. for k, v in new_wcs_header.items(): if isinstance(v, str): assert header_from_wcs[k] == v else: np.testing.assert_almost_equal(header_from_wcs[k], v)
def test_solve_astrometry_image(tmpdir): data, index = get_image_index() hdu = fits.open(data)[0] header, wcs = _generate_wcs_and_update_header(hdu.header) hdu.header = header name = tmpdir.join('testimage.fits').strpath hdu.writeto(name) nwcs = solve_astrometry_image(name, return_wcs=True) assert_is_instance(nwcs, WCS) assert_equal(nwcs.naxis, 2) compare_wcs(wcs, nwcs)
def test_fit_wcs(tmpdir): data, index = get_image_index() hdu = fits.open(data)[0] imw, imh = hdu.data.shape header, wcs = _generate_wcs_and_update_header(hdu.header) hdu.header = header sources = starfind(hdu.data, 10, np.median(hdu.data), np.std(hdu.data), 4) sources['ra'], sources['dec'] = wcs.all_pix2world(sources['x'], sources['y'], 1) nwcs = fit_wcs(sources['x'], sources['y'], sources['ra'], sources['dec'], imw, imh) assert_is_instance(nwcs, WCS) assert_equal(nwcs.naxis, 2) compare_wcs(wcs, nwcs)
def test_solve_astrometry_xyl(tmpdir): data, index = get_image_index() hdu = fits.open(data)[0] header, wcs = _generate_wcs_and_update_header(hdu.header) hdu.header = header sources = starfind(hdu.data, 10, np.median(hdu.data), np.std(hdu.data), 4) phot = aperture_photometry(hdu.data, sources['x'], sources['y']) imw, imh = hdu.data.shape nwcs = solve_astrometry_xy(phot['x'], phot['y'], phot['flux'], header, imw, imh, return_wcs=True) assert_is_instance(nwcs, WCS) assert_equal(nwcs.naxis, 2) compare_wcs(wcs, nwcs)
def loader_generator(actually_load_data): # Ideally we would just return a shape instead of an empty data array in # the case where actually_load_data is false, but we can't use # `ccdproc.trim_image()` without having a CCDData in hand, so to get the # right shape we're going to need to create a full CCDData anyway. for fits_path in glob(join(dirname, '*.fits')): # `astropy.nddata.ccddata.fits_ccddata_reader` only opens FITS from # filenames, not from an open HDUList, which means that creating # multiple CCDDatas from the same FITS file rapidly becomes # inefficient. So, we emulate its logic. with fits.open(fits_path) as hdu_list: for idx, hdu in enumerate(hdu_list): if idx == 0: header0 = hdu.header else: hdr = hdu.header hdr.extend(header0, unique=True) # This ccddata function often generates annoying warnings with warnings.catch_warnings(): warnings.simplefilter('ignore') hdr, wcs = ccddata._generate_wcs_and_update_header( hdr) # Note: we skip all the unit-handling logic here since the LSST # sim data I'm using don't have anything useful. if actually_load_data: data = hdu.data else: data = np.empty(hdu.shape, dtype=np.void) ccd = ccddata.CCDData(data, meta=hdr, unit=unit, wcs=wcs) ccd = ccdproc.trim_image( ccd, fits_section=ccd.header['DATASEC']) yield (f'{fits_path}:{idx}', ccd)