def test_mef_cutout(): test_subject = OpenCADCCutout() target_file_name = _create_mef_file() cutout_file_name_path = test_context.random_test_file_name_path() logger.info('Testing with {}'.format(cutout_file_name_path)) cutout_region_str = '[2][20:35,40:50][3]' # Write out a test file with the test result FITS data. with open(cutout_file_name_path, 'ab+') as output_writer, \ open(target_file_name, 'rb') as input_reader: test_subject.cutout_from_string(cutout_region_str, input_reader, output_writer, 'FITS') with fits.open(cutout_file_name_path, mode='readonly') as result_hdu_list: assert len(result_hdu_list) == 3, 'Should have 3 HDUs.' hdu1 = result_hdu_list[1] hdu2 = result_hdu_list[2] wcs1 = WCS(header=hdu1.header) wcs2 = WCS(header=hdu2.header) np.testing.assert_array_equal(wcs1.wcs.crpix, [-19.0, -39.0], 'Wrong CRPIX values.') np.testing.assert_array_equal(wcs1.wcs.crval, [0.0, 0.0], 'Wrong CRVAL values.') np.testing.assert_array_equal(wcs2.wcs.crpix, [0.0, 0.0], 'Wrong CRPIX values.') np.testing.assert_array_equal(wcs2.wcs.crval, [0.0, 0.0], 'Wrong CRVAL values.') assert hdu1.header['NAXIS'] == 2, 'Wrong NAXIS value.' assert hdu2.header['NAXIS'] == 2, 'Wrong NAXIS value.' assert hdu1.header.get( 'CHECKSUM') is None, 'Should not contain CHECKSUM.' assert hdu2.header.get( 'CHECKSUM') is None, 'Should not contain CHECKSUM.' assert hdu1.header.get( 'DATASUM') is None, 'Should not contain DATASUM.' assert hdu2.header.get( 'DATASUM') is None, 'Should not contain DATASUM.' expected1 = np.zeros((11, 16), dtype=hdu1.data.dtype) expected2 = np.arange(50000, dtype=hdu2.data.dtype).reshape(100, 500) for i in range(11): start = 3918 + (i * 100) expected1[i] = np.arange(start, start + 16, dtype=hdu1.data.dtype) np.testing.assert_array_equal(hdu1.data, expected1, 'Arrays 1 do not match.') np.testing.assert_array_equal(hdu2.data, expected2, 'Arrays 2 do not match.')
def test_integration_test(cutout_region_string, target_file_name, expected_cutout_file_path, use_fits_diff, test_dir_name, wcs_naxis_val, use_extension_names): test_subject = OpenCADCCutout() result_cutout_file_path = random_test_file_name_path( dir_name=test_dir_name) logger.info('Testing output to {}'.format(result_cutout_file_path)) # Write out a test file with the test result FITS data. with open(result_cutout_file_path, 'ab+') as test_file_handle, \ open(target_file_name, 'rb') as input_file_handle: test_subject.cutout_from_string(cutout_region_string, input_file_handle, test_file_handle, 'FITS') with fits.open(expected_cutout_file_path, mode='readonly', do_not_scale_image_data=True) \ as expected_hdu_list, \ fits.open(result_cutout_file_path, mode='readonly', do_not_scale_image_data=True) \ as result_hdu_list: if use_fits_diff: fits_diff = fits.FITSDiff(expected_hdu_list, result_hdu_list) np.testing.assert_array_equal((), fits_diff.diff_hdu_count, 'HDU count diff should be empty.') if use_extension_names: result_hdu_list.sort(key=_extname_sort_func) expected_hdu_list.sort(key=_extname_sort_func) for extension, result_hdu in enumerate(result_hdu_list): logger.debug('\nChecking extension {}\n'.format(extension)) expected_hdu = expected_hdu_list[extension] expected_wcs = WCS(header=expected_hdu.header, naxis=wcs_naxis_val) result_wcs = WCS(header=result_hdu.header, naxis=wcs_naxis_val) np.testing.assert_array_equal(expected_wcs.wcs.crpix, result_wcs.wcs.crpix, 'Wrong CRPIX values.') np.testing.assert_array_equal(expected_wcs.wcs.crval, result_wcs.wcs.crval, 'Wrong CRVAL values.') np.testing.assert_array_equal(expected_wcs.wcs.naxis, result_wcs.wcs.naxis, 'Wrong NAXIS values.') assert expected_hdu.header.get( 'CHECKSUM') is None, 'Should not contain CHECKSUM.' assert expected_hdu.header.get( 'DATASUM') is None, 'Should not contain DATASUM.' np.testing.assert_array_equal(np.squeeze(expected_hdu.data), result_hdu.data, 'Arrays do not match.')
def test_hst_mef_cutout_missing_one(): # Should result in a 3-HDU MEF. Extension 2 is an ERR one with no data. cutout_region_string = \ '[SCI,10][80:220,100:150][2][10:16,70:90][106][8:32,88:112][126]' test_subject = OpenCADCCutout() result_cutout_file_path = random_test_file_name_path() logger.info('Testing output to {}'.format(result_cutout_file_path)) # Write out a test file with the test result FITS data. with open(result_cutout_file_path, 'ab+') as test_file_handle, \ open(target_file_name, 'rb') as input_file_handle: test_subject.cutout_from_string(cutout_region_string, input_file_handle, test_file_handle, 'FITS') with fits.open(expected_cutout_file_path, mode='readonly') \ as expected_hdu_list, \ fits.open(result_cutout_file_path, mode='readonly') \ as result_hdu_list: # Index 0's value is missing from the result, so remove it here. expected_hdu_list.pop(0) fits_diff = fits.FITSDiff(expected_hdu_list, result_hdu_list) np.testing.assert_array_equal((), fits_diff.diff_hdu_count, 'HDU count diff should be empty.') for extension in [('SCI', 10), ('SCI', 22), ('SCI', 26)]: expected_hdu = expected_hdu_list[expected_hdu_list.index_of( extension)] result_hdu = result_hdu_list[result_hdu_list.index_of(extension)] expected_wcs = WCS(header=expected_hdu.header, fix=False) result_wcs = WCS(header=result_hdu.header, fix=False) np.testing.assert_array_equal(expected_wcs.wcs.crpix, result_wcs.wcs.crpix, 'Wrong CRPIX values.') np.testing.assert_array_equal(expected_wcs.wcs.crval, result_wcs.wcs.crval, 'Wrong CRVAL values.') assert expected_hdu.header.get('NAXIS1') == result_hdu.header.get( 'NAXIS1'), 'Wrong NAXIS1 values.' assert expected_hdu.header.get('NAXIS2') == result_hdu.header.get( 'NAXIS2'), 'Wrong NAXIS2 values.' assert expected_hdu.header.get( 'CHECKSUM') is None, 'Should not contain CHECKSUM.' assert expected_hdu.header.get( 'DATASUM') is None, 'Should not contain DATASUM.' np.testing.assert_array_equal( expected_hdu.data, result_hdu.data, 'Arrays do not match for extension {}.'.format(extension))
def test_mef_cutout_no_overlap(): test_subject = OpenCADCCutout() target_file_name = _create_mef_file() cutout_file_name_path = test_context.random_test_file_name_path() logger.info('Testing with {}'.format(cutout_file_name_path)) cutout_region_str = '[1][300:800,810:1000]' try: # Write out a test file with the test result FITS data. with open(cutout_file_name_path, 'ab+') as output_writer, \ open(target_file_name, 'rb') as input_reader: test_subject.cutout_from_string(cutout_region_str, input_reader, output_writer, 'FITS') except NoContentError as err: assert str(err) == 'No content (arrays do not overlap).', \ 'Wrong message.'
def test_integration_wcs_test(cutout_region_string, target_file_name, expected_cutout_file_path, use_fits_diff, test_dir_name, wcs_naxis_val, use_extension_names): test_subject = OpenCADCCutout() result_cutout_file_path = random_test_file_name_path( dir_name=test_dir_name) logging.info('Testing output to {}'.format(result_cutout_file_path)) # Write out a test file with the test result FITS data. with open(result_cutout_file_path, 'ab+') as test_file_handle, \ open(target_file_name, 'rb') as input_file_handle: test_subject.cutout_from_string(cutout_region_string, input_file_handle, test_file_handle, 'FITS') with fits.open(expected_cutout_file_path, mode='readonly', do_not_scale_image_data=True) \ as expected_hdu_list, \ fits.open(result_cutout_file_path, mode='readonly', do_not_scale_image_data=True) \ as result_hdu_list: if use_fits_diff: fits_diff = fits.FITSDiff(expected_hdu_list, result_hdu_list) np.testing.assert_array_equal((), fits_diff.diff_hdu_count, 'HDU count diff should be empty.') if use_extension_names: result_hdu_list.sort(key=_extname_sort_func) expected_hdu_list.sort(key=_extname_sort_func) for extension, result_hdu in enumerate(result_hdu_list): logging.info('\nChecking extension {}\n'.format(extension)) expected_hdu = expected_hdu_list[extension] expected_wcs = WCS(header=expected_hdu.header, naxis=wcs_naxis_val, fix=False) result_wcs = WCS(header=result_hdu.header, naxis=wcs_naxis_val, fix=False) np.testing.assert_array_almost_equal(expected_wcs.wcs.crpix, result_wcs.wcs.crpix, decimal=-4, err_msg='Wrong CRPIX values.') np.testing.assert_array_equal(expected_wcs.wcs.crval, result_wcs.wcs.crval, 'Wrong CRVAL values.') np.testing.assert_array_equal(expected_wcs.wcs.naxis, result_wcs.wcs.naxis, 'Wrong NAXIS values.') assert expected_hdu.header.get( 'CHECKSUM') is None, 'Should not contain CHECKSUM.' assert expected_hdu.header.get( 'DATASUM') is None, 'Should not contain DATASUM.' expected_data = expected_hdu.data result_data = result_hdu.data try: if expected_data is not None and result_data is not None: assert expected_data.shape == result_data.shape, \ 'Shapes don\'t match.' np.testing.assert_array_equal(expected_data, result_data, 'Arrays do not match.') else: assert expected_data == result_data except AssertionError: # Check the shape if the data array doesn't match. np.testing.assert_array_almost_equal( expected_hdu.data.shape, result_hdu.data.shape, decimal=-4, err_msg='Arrays match closely enough.')