Skip to content

Commit

Permalink
Update test helper to use bscale if its present
Browse files Browse the repository at this point in the history
  • Loading branch information
nabobalis committed Mar 29, 2024
1 parent 6465cdf commit 165a078
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
4 changes: 3 additions & 1 deletion sunpy/data/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def get_dummy_map_from_header(filename):
"""
filepath = get_test_filepath(filename)
header = _fits.format_comments_and_history(astropy.io.fits.Header.fromtextfile(filepath))
data = np.random.rand(header['NAXIS2'], header['NAXIS1'])
data = np.random.randint(low=0, high=100, size = (header['NAXIS2'], header['NAXIS1']))
if "BSCALE" in header:
data = data * header["BSCALE"] + header["BZERO"]
if 'BITPIX' in header:
data = data.astype(astropy.io.fits.BITPIX2DTYPE[header['BITPIX']])
# NOTE: by reading straight from the data header pair, we are skipping
Expand Down
Empty file.
17 changes: 17 additions & 0 deletions sunpy/data/test/tests/test_funcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pathlib import Path

import numpy as np

from sunpy.data.test import get_dummy_map_from_header, get_test_filepath

TEST_IRIS_SJI_HEADER = get_test_filepath('iris_l2_20130801_074720_4040000014_SJI_1400_t000.header')

def test_get_dummy_map_from_header_b_scale_zero_applied():
# We use the IRIS one since it contains a BZERO and BSCALE in the test header
iris_map = get_dummy_map_from_header(Path(TEST_IRIS_SJI_HEADER))
assert iris_map.meta["bscale"] == 0.25
assert iris_map.meta["bzero"] == 7992
# Data from the test header is scaled by BSCALE and BZERO
# but the max value it will return is 100 and converted to int
# So its 24 instead of 25 due to the rounding
assert np.max(iris_map.data) == 8016
24 changes: 8 additions & 16 deletions sunpy/io/tests/test_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import astropy.io.fits as fits

from sunpy.data.test import get_test_data_filenames, get_test_filepath, write_image_file_from_header_file
from sunpy.data.test import get_test_data_filenames, get_test_filepath
from sunpy.data.test.waveunit import MEDN_IMAGE, MQ_IMAGE, NA_IMAGE, SVSM_IMAGE
from sunpy.io import _fits
from sunpy.util import MetaDict, SunpyMetadataWarning
Expand All @@ -17,7 +17,6 @@
TEST_EIT_HEADER = get_test_filepath('EIT_header/efz20040301.000010_s.header')
TEST_SWAP_HEADER = get_test_filepath('SWAP/resampled1_swap.header')
TEST_GONG_HEADER = get_test_filepath('gong_synoptic.header')
TEST_IRIS_SJI_HEADER = get_test_filepath('iris_l2_20130801_074720_4040000014_SJI_1400_t000.header')
# Some of the tests images contain an invalid BLANK keyword ignore the warning raised by this
pytestmark = pytest.mark.filterwarnings("ignore:Invalid 'BLANK' keyword in header")

Expand Down Expand Up @@ -133,8 +132,6 @@ def test_write_with_metadict_header_astropy(tmpdir):

# Various warnings are thrown in this test, but we just want to check that the code
# works without exceptions


@pytest.mark.filterwarnings('ignore')
def test_fitsheader():
"""Test that all test data can be converted back to a FITS header."""
Expand Down Expand Up @@ -177,20 +174,15 @@ def test_warn_longkey():
assert 'BADLONGKEY' not in fits.keys()


@pytest.mark.filterwarnings("ignore:Error validating header for HDU")
def test_read_memmap(tmpdir):
# We use the IRIS one since it contains a BZERO and BSCALE
file_path = write_image_file_from_header_file(Path(TEST_IRIS_SJI_HEADER), Path(tmpdir))
memmap_data, memmap_header = _fits.read(file_path, memmap=True)[0]
assert memmap_data.base is not None
assert isinstance(memmap_data.base, mmap.mmap)
def test_read_memmap():
data, _ = _fits.read(TEST_AIA_IMAGE, memmap=True)[0]
assert data.base is not None
# Simple check to see if the base is a memory map
assert isinstance(data.base, mmap.mmap)

raw_data, raw_header = _fits.read(file_path, memmap=False)[0]
assert raw_data.base is None
data, _ = _fits.read(TEST_AIA_IMAGE, memmap=False)[0]
assert data.base is None

# Check we get the same data back
np.testing.assert_allclose(memmap_data, raw_data)
assert memmap_header == raw_header

def test_merge_multiple_comment_history_entries():
header = fits.Header()
Expand Down

0 comments on commit 165a078

Please sign in to comment.