Ejemplo n.º 1
0
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']
Ejemplo n.º 2
0
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(input_reader, output_writer, cutout_region_str,
                            'FITS')
        output_writer.close()
        input_reader.close()

    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.')
Ejemplo n.º 3
0
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]
Ejemplo n.º 4
0
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.'
Ejemplo n.º 5
0
def _create_mef_file(dir_name='/tmp'):
    mef_file = test_context.random_test_file_name_path(dir_name=dir_name)
    hdu0 = fits.PrimaryHDU()

    data1 = np.arange(10000).reshape(100, 100)
    hdu1 = fits.ImageHDU(data=data1)

    data2 = np.arange(20000).reshape(200, 100)
    hdu2 = fits.ImageHDU(data=data2)

    data3 = np.arange(50000).reshape(100, 500)
    hdu3 = fits.ImageHDU(data=data3)

    hdulist = fits.HDUList([hdu0, hdu1, hdu2, hdu3])

    hdulist.writeto(mef_file, overwrite=True)
    return mef_file
Ejemplo n.º 6
0
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_region_str = '[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(input_reader, output_writer, cutout_region_str,
                            'FITS')
        output_writer.close()
        input_reader.close()

    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.')
Ejemplo n.º 7
0
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']