Ejemplo n.º 1
0
def test_ibis8542model_save(ibis8542model_results, ibis8542model_resultsobjs,
                            tmp_path):

    # Testing mcalf.models.FitResults.save method

    res1, m = ibis8542model_results[:2]
    results10, results11 = ibis8542model_resultsobjs(res1)

    tmp10 = os.path.join(tmp_path, "ibis8542model_fit_save_results10.fits")
    results10.save(tmp10, m)
    tmp11 = os.path.join(tmp_path, "ibis8542model_fit_save_results11.fits")
    results11.save(tmp11, m)

    diff_kwargs = {
        'ignore_keywords': ['CHECKSUM', 'DATASUM', 'VERSION'],
        'atol': 1e-6,
        'rtol':
        1e-5,  # 1e-6 was failing at results10 CHI2[2, 2] on macOS CI env (but nowhere else)
    }
    for saved, truth in [(tmp10, "ibis8542model_fit_results10.fits"),
                         (tmp11, "ibis8542model_fit_results11.fits")]:
        saved = fits.open(saved)
        truth = fits.open(data_path(truth))
        diff = fits.FITSDiff(saved, truth, **diff_kwargs)
        if not diff.identical:  # If this fails tolerances *may* need to be adjusted
            fits.printdiff(saved, truth, **diff_kwargs)
            raise ValueError(
                f"{saved.filename()} and {truth.filename()} differ")
Ejemplo n.º 2
0
    def recursiveFITSDiff(parentCmp, ignore):
        # bottom up

        comparison = []
        for subDirCmp in parentCmp.subdirs.values():
            comparison.extend(recursiveFITSDiff(subDirCmp, ignore))

        for file in parentCmp.common_files:
            if not file.endswith('.fits'):
                #if '.fits' not in file:
                continue

            a = os.path.join(parentCmp.left, file)
            b = os.path.join(parentCmp.right, file)

            try:
                diff = fits.FITSDiff(a,
                                     b,
                                     ignore_keywords=ignore,
                                     numdiffs=0,
                                     ignore_blank_cards=True)
            except OSError as err:
                # print(err)
                # continue
                raise err
            if diff.identical:
                print('"{0}" & "{1}" are identical'.format(a, b))
            else:
                print('"{0}" & "{1}" differ'.format(a, b))
            comparison.append(diff)

        return comparison
Ejemplo n.º 3
0
def test_simple_write_compressed_difftypeinst(tmpdir):
    # `hdu_type=fits.CompImageHDU` and `hdu_type=fits.CompImageHDU()`
    # should produce identical FITS files
    data, header = _fits.read(TEST_AIA_IMAGE)[0]
    outfile_type = str(tmpdir / "test_type.fits")
    outfile_inst = str(tmpdir / "test_inst.fits")
    _fits.write(outfile_type, data, header, hdu_type=fits.CompImageHDU)
    _fits.write(outfile_inst, data, header, hdu_type=fits.CompImageHDU())
    assert fits.FITSDiff(outfile_type, outfile_inst, ignore_comments=['PCOUNT']).identical
Ejemplo n.º 4
0
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.')
Ejemplo n.º 5
0
def check_fdiff(file1, file2, tool):
    output = fits.FITSDiff(file1,
                           file2,
                           ignore_comments=['*'],
                           ignore_keywords=['DATE', 'HISTORY', 'CHECKSUM'])

    if (output.identical):
        print(f'{tool}: comparison to refdata SUCCESSFUL')
    elif (output.identical == False):
        print(output.report())
        print(f'*** error *** {tool}: comparison to refdata FAILED')
        exit(1)
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(
        dir_name='/usr/src/app')

    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(input_file_handle, test_file_handle,
                            cutout_region_string, 'FITS')
        test_file_handle.close()
        input_file_handle.close()

    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)
            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.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(
                np.squeeze(expected_hdu.data), result_hdu.data,
                'Arrays do not match for extension {}.'.format(extension))
Ejemplo n.º 7
0
def diff_dirs(original_path, new_paths, patterns):
    _diffs = []

    # Build a list of all FITS files in the "original" path
    original_path_fits_files = get_all_files_of_type(original_path, patterns)

    # Build a dict of {path: list of all FITS files in path} for each "new" path
    new_paths_to_fits_files = dict()
    for new_path in new_paths:
        new_path_fits_files = get_all_files_of_type(new_path, patterns)
        new_paths_to_fits_files[new_path] = new_path_fits_files
        # Check for differences between the "new" path and the "original" path. Note
        # that we are diffing sets here
        diff = DeepDiff(original_path_fits_files, new_path_fits_files)
        if diff:
            print("Differences detected between directory trees {} and {}".
                  format(original_path, new_path))
            pprint(diff)
            _diffs.append((original_path, new_path))

    for relative_fits_file_path in original_path_fits_files:
        full_old_fits_file_path = os.path.join(original_path,
                                               relative_fits_file_path)
        print("Comparing {} with:".format(full_old_fits_file_path))
        old_fits_hdu = fits.open(full_old_fits_file_path)
        for new_path in new_paths_to_fits_files:
            full_new_fits_file_path = os.path.join(new_path,
                                                   relative_fits_file_path)
            try:
                new_fits_hdu = fits.open(full_new_fits_file_path)
            except FileNotFoundError:
                print("  {} does not exist, but should!".format(
                    full_new_fits_file_path))
                _diffs.append(
                    (relative_fits_file_path, full_new_fits_file_path))
            else:
                print("  {}".format(full_new_fits_file_path))
                diff = fits.FITSDiff(
                    old_fits_hdu,
                    new_fits_hdu,
                    ignore_keywords=["DATE", "DATE-MAP"],
                )
                if diff:
                    print("Are different:")
                    print(diff.report())
                    print("-" * 30)
                    _diffs.append(
                        (relative_fits_file_path, full_new_fits_file_path))

    return _diffs
Ejemplo n.º 8
0
def test_ds9_get_fits(ds9_obj, test_fits):
    '''get a fits file as an astropy fits object'''

    ds9_obj.set('file {}'.format(test_fits))

    with pytest.warns(None) as warn_records:
        hdul_from_ds9 = ds9_obj.get_fits()

    assert isinstance(hdul_from_ds9, fits.HDUList)
    assert len(warn_records) == 0

    diff = fits.FITSDiff(test_fits.strpath, hdul_from_ds9,
                         ignore_comments=['*', ])

    assert diff.identical
def test_vlass_cube_cutout():
    test_subject = OpenCADCCutout()
    result_cutout_file_path = random_test_file_name_path()
    # expected_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(input_file_handle, test_file_handle,
                            cutout_region_string, 'FITS')
        test_file_handle.close()
        input_file_handle.close()

    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:
        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.º 10
0
def test_ds9_set_fits(tmpdir, ds9_obj, test_fits):
    '''Set the astropy fits'''

    with fits.open(test_fits.strpath) as hdul,\
            pytest.warns(None) as warn_records:
        success = ds9_obj.set_fits(hdul)

    assert success == 1
    assert len(warn_records) == 0

    out_fits = tmpdir.join('out.fits')
    with out_fits.open('w') as f:
        sp.call(['xpaget', ds9_obj.target, 'fits'], stdout=f)

    diff = fits.FITSDiff(test_fits.strpath, out_fits.strpath,
                         ignore_comments=['*', ])

    assert diff.identical
Ejemplo n.º 11
0
def test_ds9_get_fits(monkeypatch, ds9_obj, test_fits, meth, n_warning):
    '''get a fits file as an astropy fits object'''
    monkeypatch.setitem(pyds9.ds9Globals, 'pyfits', False)

    ds9_obj.set('file {}'.format(test_fits))

    with pytest.warns(None) as warn_records:
        hdul_from_ds9 = getattr(ds9_obj, meth)()

    assert isinstance(hdul_from_ds9, fits.HDUList)
    assert len(warn_records) == n_warning

    diff = fits.FITSDiff(test_fits.strpath,
                         hdul_from_ds9,
                         ignore_comments=[
                             '*',
                         ])

    assert diff.identical
Ejemplo n.º 12
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_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.')
Ejemplo n.º 13
0
    def test_create_fitshdu_from_filename(self):
        """Regression test on `FitsHDU.fromfile`"""

        # Build up a simple test FITS file
        a = np.arange(100)
        phdu = fits.PrimaryHDU(data=a)
        phdu.header['TEST1'] = 'A'
        phdu.header['TEST2'] = 'B'
        imghdu = fits.ImageHDU(data=a + 1)
        phdu.header['TEST3'] = 'C'
        phdu.header['TEST4'] = 'D'

        hdul = fits.HDUList([phdu, imghdu])
        hdul.writeto(self.temp('test.fits'))

        fitshdu = fits.FitsHDU.fromfile(self.temp('test.fits'))
        hdul2 = fitshdu.hdulist

        assert len(hdul2) == 2
        assert fits.FITSDiff(hdul, hdul2).identical
Ejemplo n.º 14
0
def test_ds9_set_fits(monkeypatch, tmpdir, ds9_obj, test_fits, meth,
                      n_warning):
    '''Set the astropy fits'''
    monkeypatch.setitem(pyds9.ds9Globals, 'pyfits', False)

    with fits.open(test_fits.strpath) as hdul,\
            pytest.warns(None) as warn_records:
        success = getattr(ds9_obj, meth)(hdul)

    assert success == 1
    assert len(warn_records) == n_warning

    out_fits = tmpdir.join('out.fits')
    with out_fits.open('w') as f:
        sp.call(['xpaget', ds9_obj.target, 'fits'], stdout=f)

    diff = fits.FITSDiff(test_fits.strpath,
                         out_fits.strpath,
                         ignore_comments=[
                             '*',
                         ])

    assert diff.identical
Ejemplo n.º 15
0
    else:
        hd.identical = True

    return hd


if __name__ == '__main__':

    basename = 'beast_example_phat'

    # check the FITS files
    fits_files = ['stats', 'pdf1d']
    for cfile in fits_files:
        file1 = basename + '/' + basename + '_' + cfile + '.fits'
        if os.path.isfile(file1):
            fd = fits.FITSDiff(
                file1, basename + '_good/' + basename + '_' + cfile + '.fits')
            print(cfile, fd.identical)
            if not fd.identical:
                print(fd.report())
        else:
            print(cfile, 'does not exist, not checking')

    # check the HDF5 files
    #    lnp not checked as it changes everytime as the sparse sampling
    #    of the likelihood is random
    hdf5_files = [
        'seds.grid', 'seds_trim.grid', 'noisemodel', 'noisemodel_trim.grid'
    ]
    for cfile in hdf5_files:
        file1 = basename + '/' + basename + '_' + cfile + '.hd5'
        if os.path.isfile(file1):
Ejemplo n.º 16
0
 def get_astropy_diff(self):
     return fits.FITSDiff(self.hdulist, self.hdulist2)
Ejemplo n.º 17
0
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.')
Ejemplo n.º 18
0
def test_merge_results(tmp_path):

    # Compatible files to test merging
    compatible_files = [
        data_path('test_merge_results_1.fits'),
        data_path('test_merge_results_2.fits'),
        data_path('test_merge_results_3.fits'),
    ]

    # Merge and save
    output_file = tmp_path / "test_merge_results_output.fits"
    merge_results(compatible_files, output_file)

    # Compare merged files to expected merge
    test = fits.open(output_file, mode='readonly')
    verify = fits.open(data_path('test_merge_results_all.fits'),
                       mode='readonly')
    # Diff ignoring checksums as too strict (compare values instead)
    diff = fits.FITSDiff(test, verify, ignore_keywords=['CHECKSUM', 'DATASUM'])
    assert diff.identical  # If this fails tolerances *may* need to be adjusted

    # Incompatible files to test merging
    incompatible_files = [
        data_path('test_merge_results_1.fits'),
        data_path('test_merge_results_2.fits'),
        data_path('test_merge_results_3.fits'),
        data_path('test_merge_results_2.fits'
                  ),  # Duplicate (overlapping) file should fail
    ]

    # Merge (should fail before saving)
    with pytest.raises(ValueError):
        merge_results(incompatible_files, output_file)

    # Compatible files but wrong time for one (should fail)
    compatible_files_wrongtime = [
        data_path('test_merge_results_1.fits'),
        data_path('test_merge_results_2_wrongtime.fits'),
        data_path('test_merge_results_3.fits'),
    ]

    # Merge (should fail before saving)
    with pytest.raises(ValueError):
        merge_results(compatible_files_wrongtime, output_file)

    # Must provide a list of multiple files
    with pytest.raises(TypeError):  # single string
        merge_results(data_path('test_merge_results_1.fits'), output_file)
    with pytest.raises(TypeError):  # list of length 1
        merge_results([data_path('test_merge_results_1.fits')], output_file)

    # The extra HDU should cause an error
    with pytest.raises(ValueError) as excinfo:
        merge_results([
            data_path('test_merge_results_2.fits'),
            data_path('test_merge_results_1_extrahdu.fits'),
        ], output_file)
    assert 'nexpected' in str(excinfo.value)  # "Unexpected"
    # reverse (now extra is in first file)
    with pytest.raises(ValueError) as excinfo:
        merge_results([
            data_path('test_merge_results_1_extrahdu.fits'),
            data_path('test_merge_results_2.fits'),
        ], output_file)
    assert 'nexpected' in str(excinfo.value)  # "Unexpected"
Ejemplo n.º 19
0
    else:
        hd.identical = True

    return hd


if __name__ == "__main__":

    basename = "beast_example_phat"

    # check the FITS files
    fits_files = ["stats", "pdf1d"]
    for cfile in fits_files:
        file1 = basename + "/" + basename + "_" + cfile + ".fits"
        if os.path.isfile(file1):
            fd = fits.FITSDiff(
                file1, basename + "_good/" + basename + "_" + cfile + ".fits")
            print(cfile, fd.identical)
            if not fd.identical:
                print(fd.report())
        else:
            print(cfile, "does not exist, not checking")

    # check the HDF5 files
    #    lnp not checked as it changes everytime as the sparse sampling
    #    of the likelihood is random
    hdf5_files = [
        "seds.grid", "seds_trim.grid", "noisemodel", "noisemodel_trim.grid"
    ]
    for cfile in hdf5_files:
        file1 = basename + "/" + basename + "_" + cfile + ".hd5"
        if os.path.isfile(file1):
Ejemplo n.º 20
0
def check_fits_changes(fname, fname_modified, allow_header_value_changes=None):
	"""
	Check FITS files for changes.

	If no changes between the two files are detected, an error is also raised, as it is assumed
	that a change was needed. The header keywords CHECKSUM and DATASUM are always allowed to change.

	Parameters:
		fname (str, fits.HDUList): Original FITS file to check against.
		fname_modified (str, fits.HDUList): Modified FITS file to check.
		allow_header_value_changes (list): List of header keywords allowed to change.

	Returns:
		bool: True if file check was okay, False otherwise.

	.. codeauthor:: Rasmus Handberg <*****@*****.**>
	"""

	logger = logging.getLogger(__name__)

	if allow_header_value_changes is None:
		allow_header_value_changes = ['CHECKSUM', 'DATASUM']
	else:
		allow_header_value_changes = ['CHECKSUM', 'DATASUM'] + allow_header_value_changes

	with warnings.catch_warnings():
		warnings.filterwarnings('ignore', category=fits.verify.VerifyWarning)
		diff = fits.FITSDiff(fname, fname_modified)

	fname_str = 'HDU' if isinstance(fname, fits.HDUList) else fname

	if logger.isEnabledFor(logging.DEBUG):
		report = diff.report()
		logger.debug(report)

	if diff.identical:
		logger.error("%s: Nothing has changed?", fname_str)
		return False

	if diff.diff_hdu_count:
		logger.error("%s: Different number of HDUs: %s", fname_str, diff.diff_hdu_count)
		return False

	everything_ok = True
	for dh in diff.diff_hdus:
		# Historically this has changed in astropy, from containing two to four entries,
		# which is the reason for unpacking like this:
		d = dh[1]
		# Headers:
		hdr = d.diff_headers
		if not hdr.identical:
			if hdr.diff_keywords:
				# Keywords only in header A:
				if hdr.diff_keywords[0]:
					logger.error("%s: Extra keyword in original: %s", fname_str, hdr.diff_keywords[0])
					everything_ok = False
				# Keywords only in header B:
				if hdr.diff_keywords[1]:
					if any([key not in allow_header_value_changes for key in hdr.diff_keywords[1]]):
						logger.error("%s: Extra keyword in modified: %s", fname_str, hdr.diff_keywords[1])
						everything_ok = False

			for key, val in hdr.diff_keyword_values.items():
				if key not in allow_header_value_changes:
					logger.error("%s: Keyword with different values: %s: %s != %s",
						fname_str, key, val[0][0], val[0][1])
					everything_ok = False

			for key, val in hdr.diff_keyword_comments.items():
				if key not in allow_header_value_changes:
					logger.error("%s: Keyword with different comments: %s: %s != %s",
						fname_str, key, val[0][0], val[0][1])
					everything_ok = False

		# Data:
		dat = d.diff_data
		if dat is not None and not dat.identical:
			if isinstance(dat, fits.diff.TableDataDiff):
				if dat.diff_column_count:
					logger.error("%s: Different number of table columns!", fname_str)
					everything_ok = False

				for something in dat.diff_column_attributes:
					column_change = something[0][1]
					if column_change == 'disp' and any([kw.startswith('TDISP') for kw in allow_header_value_changes]):
						pass
					elif column_change == 'unit' and any([kw.startswith('TUNIT') for kw in allow_header_value_changes]):
						pass
					else:
						column_key = something[0][0]
						column_changes = something[1]
						logger.error("%s: Table header with different values: %s (%s): %s != %s",
							fname_str,
							column_key,
							column_change,
							column_changes[0],
							column_changes[1])
						everything_ok = False

				if dat.diff_values:
					logger.error("%s: Data has been changed!", fname_str)
					everything_ok = False
			else:
				logger.error("%s: Data has been changed!", fname_str)
				everything_ok = False

	# If we have made it this far, things should be okay:
	return everything_ok