def test_multiple_ext_cutouts(): test_subject = OpenCADCCutout() cutout_file_name_path = test_context.random_test_file_name_path() cutout_regions = [ PixelCutoutHDU([(1, 100), (1, 100)], "1"), PixelCutoutHDU([(1, 100), (1, 100)], "3") ] with open(cutout_file_name_path, 'wb') as output_writer, \ open(target_file_name, 'rb') as input_reader: test_subject.cutout(cutout_regions, input_reader, output_writer, 'FITS') expected = fits.open(target_file_name, do_not_scale_image_data=True) actual = fits.open(cutout_file_name_path, do_not_scale_image_data=True) assert len(actual) == 3 # test primary header unchanged assert len(expected[0].header) == len(actual[0].header) for key in expected[0].header.keys(): assert expected[0].header[key] == actual[0].header[key] # check BSCALE and BZERO correct in cutout file assert expected[1].header['BSCALE'] == actual[1].header['BSCALE'] assert expected[1].header['BZERO'] == actual[1].header['BZERO'] assert expected[3].header['BSCALE'] == actual[2].header['BSCALE'] assert expected[3].header['BZERO'] == actual[2].header['BZERO']
def test_construct(): test_subject = OpenCADCCutout() with pytest.raises(ValueError) as ve: test_subject.cutout([]) assert str(ve.value) == 'No Cutout regions specified.', \ 'Wrong error message.' with pytest.raises(FileNotFoundError): test_subject.cutout([PixelCutoutHDU([(8, 10)])], input_reader=open('/no/such/file'))
def test_astropy_scaling(): test_subject = OpenCADCCutout() cutout_file_name_path = test_context.random_test_file_name_path() cutout_regions = [PixelCutoutHDU([], "2")] # Write out a test file with the test result FITS data. with open(cutout_file_name_path, 'wb') as output_writer, \ open(target_file_name, 'rb') as input_reader: test_subject.cutout(cutout_regions, input_reader, output_writer, 'FITS') # now check that BZERO and BSCALE have not been changed expected = fits.open(target_file_name, do_not_scale_image_data=True) actual = fits.open(cutout_file_name_path, do_not_scale_image_data=True) # check headers and data not changed. Except ... # expected missing keywords from actual: PCOUNT, XTENSION and GCOUNT # added to actual: SIMPLE del expected[2].header['PCOUNT'] del expected[2].header['XTENSION'] del expected[2].header['GCOUNT'] assert 'SIMPLE' in actual[0].header del actual[0].header['SIMPLE'] assert len(expected[2].header) == len(actual[0].header) for key in expected[2].header.keys(): assert expected[2].header[key] == actual[0].header[key] np.testing.assert_array_equal(expected[2].data, actual[0].data, 'Arrays do not match.') # do a cutout cutout_regions = [PixelCutoutHDU([(1, 100), (1, 100)], "3")] # Write out a test file with the test result FITS data. with open(cutout_file_name_path, 'wb') as output_writer, \ open(target_file_name, 'rb') as input_reader: test_subject.cutout(cutout_regions, input_reader, output_writer, 'FITS') # now check that BZERO and BSCALE have not been changed expected = fits.open(target_file_name, do_not_scale_image_data=True) actual = fits.open(cutout_file_name_path, do_not_scale_image_data=True) # check only expected headers changed # changed headers del expected[3].header['PCOUNT'] del expected[3].header['XTENSION'] del expected[3].header['GCOUNT'] assert 'SIMPLE' in actual[0].header del actual[0].header['SIMPLE'] assert len(expected[3].header) == len(actual[0].header) for key in expected[3].header.keys(): if key == 'NAXIS1' or key == 'NAXIS2': assert actual[0].header[key] == 100 else: assert expected[3].header[key] == actual[0].header[key]
def test_simple_cutout(): test_subject = OpenCADCCutout() cutout_file_name_path = test_context.random_test_file_name_path() logger.info('Testing with {}'.format(cutout_file_name_path)) cutout_regions = [PixelCutoutHDU([(300, 800), (810, 1000)])] # 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(cutout_regions, input_reader, output_writer, 'FITS') with fits.open(expected_cutout_file_name, mode='readonly') \ as expected_hdu_list, \ fits.open(cutout_file_name_path, mode='readonly') \ as result_hdu_list: 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, result_hdu in enumerate(result_hdu_list): expected_hdu = expected_hdu_list[extension] expected_wcs = WCS(header=expected_hdu.header) result_wcs = WCS(header=result_hdu.header) 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['NAXIS1'] \ == result_hdu.header['NAXIS1'], 'Wrong NAXIS1 values.' assert expected_hdu.header['NAXIS2'] \ == result_hdu.header['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( np.squeeze(expected_hdu.data), result_hdu.data, 'Arrays do not match.')
def test_multiple_cutouts_single_ext(): test_subject = OpenCADCCutout() cutout_file_name_path = test_context.random_test_file_name_path() cutout_regions = [ PixelCutoutHDU([(1, 100), (1, 100)], "1"), PixelCutoutHDU([(200, 300), (2, 300)], "1") ] with open(cutout_file_name_path, 'wb') as output_writer, \ open(target_file_name, 'rb') as input_reader: test_subject.cutout(cutout_regions, input_reader, output_writer, 'FITS') expected = fits.open(target_file_name, do_not_scale_image_data=True) actual = fits.open(cutout_file_name_path, do_not_scale_image_data=True) # cutouts in the same extension => no extra primary HDU assert len(actual) == 2 # test primary header changed assert len(expected[0].header) != len(actual[0].header) # check BSCALE and BZERO correct in cutout file assert expected[1].header['BSCALE'] == actual[0].header['BSCALE'] assert expected[1].header['BZERO'] == actual[0].header['BZERO'] assert expected[1].header['BSCALE'] == actual[1].header['BSCALE'] assert expected[1].header['BZERO'] == actual[1].header['BZERO']
def test_construct(): test_subject = OpenCADCCutout() with pytest.raises(ValueError) as ve: test_subject.cutout([]) assert '{}'.format(ve) == 'No Cutout regions specified.', \ 'Wrong error message.' with pytest.raises(ValueError) as ve: test_subject.cutout([PixelCutoutHDU([(8, 10)])], input_reader=None) assert '{}'.format(ve) == 'No input source specified.', \ 'Wrong error message.' with pytest.raises(ValueError) as ve: test_subject.cutout([PixelCutoutHDU([(8, 10)])], output_writer=None) assert '{}'.format(ve) == 'No output target specified.', \ 'Wrong error message.'