def test_write_file_ana(): ana = sunpy.io.read_file(get_test_filepath("test_ana.fz"))[0] sunpy.io.write_file("ana_test_write.fz", ana[0], str(ana[1])) assert os.path.exists("ana_test_write.fz") outpair = sunpy.io.read_file(get_test_filepath("test_ana.fz")) assert np.all(np.equal(outpair[0][1], ana[1])) assert outpair[0][1] == ana[1] os.remove("ana_test_write.fz")
def test_calculate_radiative_loss_rate(): # Define input variables. goeslc_input = timeseries.TimeSeries(get_test_filepath("go1520110607.fits")) not_goeslc = [] goeslc_no_em = goes.calculate_temperature_em(goeslc_input) del goeslc_no_em.data["em"] # Check correct exceptions are raised to incorrect inputs with pytest.raises(TypeError): goes_test = goes.calculate_radiative_loss_rate(not_goeslc) # Check function gives correct results. # Test case 1: GOESLightCurve object with only flux data goeslc_test = goes.calculate_radiative_loss_rate(goeslc_input) exp_data = np.array([1.78100055e+19, 1.66003113e+19, 1.71993065e+19, 1.60171768e+19, 1.71993065e+19]) np.testing.assert_allclose(goeslc_test.data.rad_loss_rate[:5], exp_data) # Test case 2: GOESLightCurve object with flux and temperature # data, but no EM data. goes_test = goes.calculate_radiative_loss_rate(goeslc_no_em) # we test that the column has been added assert "rad_loss_rate" in goes_test.columns # Compare every 50th value to save on filesize return np.array(goes_test.data[::50])
def test_parse_observing_summary_dbase_file(): """ Test that we get the observing summary dbase file with the content we expect. """ obssum = rhessi.parse_observing_summary_dbase_file(get_test_filepath("hsi_obssumm_filedb_201104.txt")) assert obssum['filename'][0][0:20] == 'hsi_obssumm_20110401' assert obssum['filename'][1][0:20] == 'hsi_obssumm_20110402' assert obssum['orb_st'][0] == 0 assert obssum['orb_st'][-1] == 0 assert obssum['orb_end'][0] == 0 assert obssum['orb_end'][-1] == 0 assert obssum['start_time'][0] == parse_time((2011, 4, 1, 0, 0, 0)) assert obssum['start_time'][-1] == parse_time((2011, 4, 30, 0, 0, 0)) assert obssum['end_time'][0] == parse_time((2011, 4, 2, 0, 0, 0)) assert obssum['end_time'][-1] == parse_time((2011, 5, 1, 0, 0, 0)) assert obssum['status_flag'][0] == 0 assert obssum['status_flag'][-1] == 0 assert obssum['npackets'][0] == 0 assert obssum['npackets'][-1] == 0
def test_read_file_ana(): ana_data = sunpy.io.read_file(get_test_filepath("test_ana.fz")) assert isinstance(ana_data, list) assert len(ana_data) == 1 assert len(ana_data[0]) == 2 assert isinstance(ana_data[0][0], np.ndarray) assert isinstance(ana_data[0][1], sunpy.io.header.FileHeader)
def test_sources(file, mapcls): p = pathlib.Path(get_test_filepath(file)) if p.suffix == '.header': m = get_dummy_map_from_header(p) else: m = sunpy.map.Map(p) assert isinstance(m, mapcls)
def test_read_file_header_jp2(): # Test jp2 hlist = sunpy.io.read_file_header( get_test_filepath("2013_06_24__17_31_30_84__SDO_AIA_AIA_193.jp2")) assert isinstance(hlist, list) assert len(hlist) == 1 assert isinstance(hlist[0], sunpy.io.header.FileHeader)
def test_parse_observing_summary_dbase_file(): """ Test that we get the observing summary dbase file with the content we expect. """ obssum = rhessi.parse_observing_summary_dbase_file( get_test_filepath("hsi_obssumm_filedb_201104.txt")) assert obssum['filename'][0][0:20] == 'hsi_obssumm_20110401' assert obssum['filename'][1][0:20] == 'hsi_obssumm_20110402' assert obssum['orb_st'][0] == 0 assert obssum['orb_st'][-1] == 0 assert obssum['orb_end'][0] == 0 assert obssum['orb_end'][-1] == 0 assert obssum['start_time'][0] == parse_time((2011, 4, 1, 0, 0, 0)) assert obssum['start_time'][-1] == parse_time((2011, 4, 30, 0, 0, 0)) assert obssum['end_time'][0] == parse_time((2011, 4, 2, 0, 0, 0)) assert obssum['end_time'][-1] == parse_time((2011, 5, 1, 0, 0, 0)) assert obssum['status_flag'][0] == 0 assert obssum['status_flag'][-1] == 0 assert obssum['npackets'][0] == 0 assert obssum['npackets'][-1] == 0
def test_get_parse_obssum_hdulist(): hdulist = sunpy.io.read_file( get_test_filepath('hsi_obssumm_20110404_042.fits.gz')) header, _data = rhessi.parse_observing_summary_hdulist(hdulist) assert header.get('DATE_OBS') == '2011-04-04T00:00:00.000' assert header.get('DATE_END') == '2011-04-05T00:00:00.000' assert header.get('TELESCOP') == 'HESSI'
def test_load_100_file_with_no_shift(): fname = get_test_filepath("aiamap_genericmap_1.0.0.asdf") with asdf.open(fname) as af: aiamap = af['object'] assert isinstance(aiamap, sunpy.map.sources.AIAMap) assert "crval1" not in aiamap.meta.modified_items assert "crval2" not in aiamap.meta.modified_items
def test_calculate_temperature_em(): # Create XRSTimeSeries object, then create new one with # temperature & EM using with calculate_temperature_em(). goeslc = timeseries.TimeSeries(get_test_filepath("go1520110607.fits")) goeslc_new = goes.calculate_temperature_em(goeslc) # Test correct exception is raised if a XRSTimeSeries object is # not inputted. with pytest.raises(TypeError): goes.calculate_temperature_em([]) # Find temperature and EM manually with _goes_chianti_tem() temp, em = goes._goes_chianti_tem( goeslc.quantity("xrsb"), goeslc.quantity("xrsa"), satellite=int(goeslc.meta.metas[0]["TELESCOP"].split()[1]), date="2014-01-01") # Check that temperature and EM arrays from _goes_chianti_tem() # are same as those in new XRSTimeSeries object. assert goeslc_new.data.temperature.all() == temp.value.all() assert goeslc_new.data.em.all() == em.value.all() # Check rest of data frame of new XRSTimeSeries object is same # as that in original object. goeslc_revert = copy.deepcopy(goeslc_new) del goeslc_revert.data["temperature"] del goeslc_revert.data["em"] assert_frame_equal(goeslc_revert.data, goeslc.data)
def test_load_100_file_with_shift(): fname = get_test_filepath("aiamap_shift_genericmap_1.0.0.asdf") with asdf.open(fname) as af: aiamap = af['object'] assert isinstance(aiamap, sunpy.map.sources.AIAMap) assert "crval1" in aiamap.meta.modified_items crval1 = aiamap.meta.modified_items["crval1"] assert crval1.current - crval1.original == 10
def test_backprojection(): """ Test that backprojection returns a map with the expected time. """ test_filename = 'hsi_calib_ev_20020220_1106_20020220_1106_25_40.fits' amap = rhessi.backprojection(get_test_filepath(test_filename)) assert isinstance(amap, sunpy.map.GenericMap) assert is_time_equal(amap.date, parse_time((2002, 2, 20, 11, 6, 21)))
def heliographic_test_map(): (data, header), = sunpy.io.read_file(get_test_filepath('heliographic_phase_map.fits.gz')) # Fix unit strings to prevent some astropy fits fixing warnings header['CUNIT1'] = 'deg' header['CUNIT2'] = 'deg' # Set observer location to avoid warnings later header['HGLN_OBS'] = 0.0 return Map((data, header))
def hmi_test_map(): (data, header), = sunpy.io.read_file(get_test_filepath('resampled_hmi.fits')) # Get rid of the blank keyword to prevent some astropy fits fixing warnings header.pop('BLANK') header.pop('CRDER2') header.pop('CRDER1') return sunpy.map.Map((data, header))
def test_read_file_jp2(): # Test read jp2 pair = sunpy.io.read_file( get_test_filepath("2013_06_24__17_31_30_84__SDO_AIA_AIA_193.jp2")) assert isinstance(pair, list) assert len(pair) == 1 assert len(pair[0]) == 2 assert isinstance(pair[0][0], np.ndarray) assert isinstance(pair[0][1], sunpy.io.header.FileHeader)
def test_read_file_fits_gzip(): # Test read gzipped fits file for fits_extension in [".fts", ".fit", ".fits"]: pair = sunpy.io.read_file( get_test_filepath(f"gzip_test{fits_extension}.gz")) assert isinstance(pair, list) assert len(pair) == 1 assert len(pair[0]) == 2 assert isinstance(pair[0][0], np.ndarray) assert isinstance(pair[0][1], sunpy.io.header.FileHeader) assert np.all( pair[0][0] == np.tile(np.arange(32), (32, 1)).transpose())
def test_calculate_xray_luminosity(): # Check correct exceptions are raised to incorrect inputs not_goeslc = [] with pytest.raises(TypeError): goes_test = goes.calculate_xray_luminosity(not_goeslc) # Check function gives correct results. goeslc_input = timeseries.TimeSeries(get_test_filepath("go1520110607.fits")) goeslc_test = goes.calculate_xray_luminosity(goeslc_input) exp_xrsa = u.Quantity([2.8962085e+14, 2.8962085e+14, 2.8962085e+14, 2.8962085e+14, 2.8962085e+14], "W") exp_xrsb = u.Quantity([5.4654352e+16, 5.3133844e+16, 5.3895547e+16, 5.2375035e+16, 5.3895547e+16], "W") assert_quantity_allclose(exp_xrsa, goeslc_test.quantity("luminosity_xrsa")[:5]) assert_quantity_allclose(exp_xrsb, goeslc_test.quantity("luminosity_xrsb")[:5])
def test_parse_obssum_dbase_file(): fname = get_test_filepath("hsi_obssumm_filedb_201104.txt") obssum = rhessi.parse_observing_summary_dbase_file(fname) assert obssum['filename'][0] == 'hsi_obssumm_20110401_043.fit' assert obssum['filename'][-1] == 'hsi_obssumm_20110430_029.fit' assert obssum['orb_st'][0] == 0 assert obssum['orb_st'][-1] == 0 assert obssum['orb_end'][0] == 0 assert obssum['orb_end'][-1] == 0 assert obssum['start_time'][0] == parse_time((2011, 4, 1, 0, 0, 0)) assert obssum['start_time'][-1] == parse_time((2011, 4, 30, 0, 0, 0)) assert obssum['end_time'][0] == parse_time((2011, 4, 2, 0, 0, 0)) assert obssum['end_time'][-1] == parse_time((2011, 5, 1, 0, 0, 0)) assert obssum['status_flag'][0] == 0 assert obssum['status_flag'][-1] == 0 assert obssum['npackets'][0] == 0 assert obssum['npackets'][-1] == 0
def lasco_map(request): return get_dummy_map_from_header(get_test_filepath(request.param))
import astropy.units as u from astropy.io import fits from astropy.table import Table from astropy.time import TimeDelta import sunpy.io import sunpy.net.attrs as a import sunpy.timeseries from sunpy.data.test import get_test_filepath, rootdir, test_data_filenames from sunpy.net import Fido from sunpy.time import parse_time from sunpy.util import SunpyUserWarning from sunpy.util.datatype_factory_base import NoMatchError from sunpy.util.metadata import MetaDict eve_filepath = get_test_filepath('EVE_L0CS_DIODES_1m_truncated.txt') eve_many_filepath = [ f for f in test_data_filenames() if f.parents[0].relative_to(f.parents[1]).name == 'eve' ] goes_filepath = get_test_filepath('go1520110607.fits') psp_filepath = get_test_filepath('psp_fld_l2_mag_rtn_1min_20200104_v02.cdf') swa_filepath = get_test_filepath('solo_L1_swa-pas-mom_20200706_V01.cdf') fermi_gbm_filepath = get_test_filepath('gbm.fits') @pytest.mark.filterwarnings('ignore:Unknown units') def test_factory_concatenate_same_source(): # Test making a TimeSeries that is the concatenation of multiple files ts_from_list = sunpy.timeseries.TimeSeries(eve_many_filepath, source='EVE',
LONGLAT.add_column( MaskedColumn(data=[x['longitude'] for x in COORDINATES], name='Longitude', unit=u.deg, mask=True, fill_value=-99999)) LONGLAT.add_column( MaskedColumn(data=[x['latitude'] for x in COORDINATES], name='Latitude', unit=u.deg, fill_value=-99999)) @pytest.mark.filterwarnings('ignore:dropping mask in Quantity column') @pytest.mark.parametrize("path, number_of_rows", [(get_test_filepath(elem['file']), elem['rows']) for elem in filenames]) def test_number_of_rows(path, number_of_rows): table = srs.read_srs(path) assert len(table) == number_of_rows @pytest.mark.parametrize("text, longitude", [(elem['text'], elem['longitude']) for elem in COORDINATES]) def test_parse_longitude(text, longitude): assert srs.parse_longitude(text) == longitude @pytest.mark.parametrize("text, latitude", [(elem['text'], elem['latitude']) for elem in COORDINATES]) def test_parse_latitude(text, latitude):
""" EVE tests """ from __future__ import absolute_import import pytest #pylint: disable=C0103,R0904,W0201,W0232,E1103 import sunpy import sunpy.lightcurve from sunpy.data.test import get_test_filepath EVE_AVERAGES_CSV = get_test_filepath("EVE_He_II_304_averages.csv") @pytest.mark.online def test_eve(): eve = sunpy.lightcurve.EVELightCurve.create('2013/04/15') assert isinstance(eve, sunpy.lightcurve.EVELightCurve) @pytest.mark.online def test_txt(): """Check support for parsing EVE TXT files """ eve = sunpy.lightcurve.EVELightCurve.create( "http://lasp.colorado.edu/eve/data_access/quicklook/quicklook_data/L0CS/LATEST_EVE_L0CS_DIODES_1m.txt") assert isinstance(eve, sunpy.lightcurve.EVELightCurve) def test_csv_parsing(): """Check support for parsing EVE CSV files""" csv = sunpy.lightcurve.EVELightCurve.create(EVE_AVERAGES_CSV) assert isinstance(csv, sunpy.lightcurve.sources.eve.EVELightCurve)
import pytest import sunpy.timeseries from sunpy.data.test import get_test_filepath goes_filepath = get_test_filepath('go1520110607.fits') goes_filepath_com = get_test_filepath('go1520120601.fits.gz') new_goes15_filepath = get_test_filepath('goes_truncated_test_goes15.nc') new_goes17_filepath = get_test_filepath('goes_truncated_test_goes17.nc') def test_implicit_goes(): # Test a GOES TimeSeries ts_goes = sunpy.timeseries.TimeSeries(goes_filepath) assert isinstance(ts_goes, sunpy.timeseries.sources.goes.XRSTimeSeries) def test_implicit_goes_com(): # Test a GOES TimeSeries ts_goes = sunpy.timeseries.TimeSeries(goes_filepath_com) assert isinstance(ts_goes, sunpy.timeseries.sources.goes.XRSTimeSeries) def test_implicit_new_goes15(): # Test a GOES TimeSeries ts_goes = sunpy.timeseries.TimeSeries(new_goes15_filepath) assert isinstance(ts_goes, sunpy.timeseries.sources.goes.XRSTimeSeries) def test_implicit_new_goes17(): # Test a GOES TimeSeries
import sunpy.timeseries from sunpy.data.test import get_test_filepath rhessi_filepath = get_test_filepath('hsi_obssumm_20120601_018_truncated.fits.gz') def test_implicit_rhessi(): # Test a RHESSI TimeSeries ts_rhessi = sunpy.timeseries.TimeSeries(rhessi_filepath) assert isinstance(ts_rhessi, sunpy.timeseries.sources.rhessi.RHESSISummaryTimeSeries) def test_rhessi(): # Test a RHESSI TimeSeries ts_rhessi = sunpy.timeseries.TimeSeries(rhessi_filepath, source='RHESSI') assert isinstance(ts_rhessi, sunpy.timeseries.sources.rhessi.RHESSISummaryTimeSeries)
from astropy.time import TimeDelta import sunpy import sunpy.timeseries from sunpy.data.test import get_test_filepath, test_data_filenames from sunpy.tests.helpers import figure_test from sunpy.time import TimeRange, parse_time from sunpy.timeseries import TimeSeriesMetaData from sunpy.util import SunpyUserWarning from sunpy.util.metadata import MetaDict # ============================================================================= # TimeSeries Tests # ============================================================================= eve_filepath = get_test_filepath('EVE_L0CS_DIODES_1m_truncated.txt') esp_filepath = get_test_filepath('eve_l1_esp_2011046_00_truncated.fits') fermi_gbm_filepath = get_test_filepath('gbm.fits') norh_filepath = get_test_filepath('tca110810_truncated') lyra_filepath = get_test_filepath( 'lyra_20150101-000000_lev3_std_truncated.fits.gz') rhessi_filepath = get_test_filepath( 'hsi_obssumm_20120601_018_truncated.fits.gz') noaa_ind_json_filepath = get_test_filepath( 'observed-solar-cycle-indices-truncated.json') noaa_pre_json_filepath = get_test_filepath( 'predicted-solar-cycle-truncated.json') goes_filepath = get_test_filepath('go1520120601.fits.gz') a_list_of_many = [ f for f in test_data_filenames() if f.parents[0].relative_to(f.parents[1]).name == 'eve'
def aia171_test_map(): aia_path = get_test_filepath("aia_171_level1.fits") return sunpy.map.Map(aia_path)
def original(): return sunpy.map.Map(test.get_test_filepath("aia_171_level1.fits"))
import tempfile import pytest import numpy as np import sunpy.map import sunpy.data.test as test from sunpy.instr.aia import aiaprep # Define the original and prepped images first so they're available to all # functions @pytest.fixture(scope="module", params=[test.get_test_filepath("aia_171_level1.fits"), test.get_test_filepath("resampled_hmi.fits")]) def original(request): return sunpy.map.Map(request.param) @pytest.fixture def prep_map(original): return aiaprep(original) def test_aiaprep(original, prep_map): # Test that header info for the map has been correctly updated # Check all of these for Map attributes and .meta values? # Check array shape assert prep_map.data.shape == original.data.shape # Check crpix values
import numpy as np from sunpy.data.test import get_test_filepath from sunpy.io.header import FileHeader from sunpy.map import GenericMap, Map from sunpy.tests.helpers import skip_glymur AIA_193_JP2 = get_test_filepath("2013_06_24__17_31_30_84__SDO_AIA_AIA_193.jp2") @skip_glymur def test_read_data(): """ Tests the reading of the JP2 data. """ import glymur data = glymur.Jp2k(AIA_193_JP2).read() assert isinstance(data, np.ndarray) @skip_glymur def test_read_header(): """ Tests the reading of the JP2 header. """ from sunpy.io.jp2 import get_header header = get_header(AIA_193_JP2)[0] assert isinstance(header, FileHeader) @skip_glymur
def lasco_helioviewer(): return Map( get_test_filepath( "2013_05_13__16_54_06_137__SOHO_LASCO_C3_white-light.jp2"))
""" JPEG2000 reading tests """ from __future__ import absolute_import #pylint: disable=C0103,R0904,W0201,W0212,W0232,E1103 import numpy as np from sunpy.data.test import get_test_filepath from sunpy.io.header import FileHeader from sunpy.map import GenericMap from sunpy.map import Map from sunpy.tests.helpers import skip_glymur AIA_193_JP2 = get_test_filepath("2013_06_24__17_31_30_84__SDO_AIA_AIA_193.jp2") @skip_glymur def test_read_data(): """Tests the reading of the JP2 data""" import glymur data = glymur.Jp2k(AIA_193_JP2).read() assert isinstance(data, np.ndarray) @skip_glymur def test_read_header(): """Tests the reading of the JP2 header""" from sunpy.io.jp2 import get_header header = get_header(AIA_193_JP2)[0]
def aia171_test_map(): return smap.Map(test.get_test_filepath('aia_171_level1.fits'))
import datetime import numpy as np import pytest from sunpy.data.test import get_test_filepath from sunpy.io.special import genx TESTING = genx.read_genx(get_test_filepath('generated_sample.genx')) def test_skeleton(): # Top level toplevel_dims = { 'MYTEXT': 63, 'MYTEXT_ARRAY': 3, 'MYTEXT_ARRAY_DIMENSION': (2, 3), 'MYNUMBER': 1, 'MYNUMBER_ARRAY': 3, 'MYNUMBER_ARRAY_DIMENSION': (2, 3, 4, 5), 'MYUINT': 1, 'MYSTRUCTURE': 14, # the elements inside the OrderedDict 'MYSTRUCTURE_ARRAY': 6, 'HEADER': 5 } assert sorted(list(TESTING.keys())) == sorted(list(toplevel_dims.keys())) for key, val in toplevel_dims.items(): if isinstance(val, tuple): assert TESTING[key].shape == tuple(reversed(val)) else: if val > 1:
def kcor(): """Creates an KCorMap from a FITS file.""" return get_dummy_map_from_header( get_test_filepath("20181209_180305_kcor_l1.5_rebinned.header"))
import numpy as np import pytest from astropy.io.fits.verify import VerifyWarning import sunpy.data.test as test import sunpy.map from sunpy.instr.aia import aiaprep # Define the original and prepped images first so they're available to all # functions @pytest.fixture(scope="module", params=[ test.get_test_filepath("aia_171_level1.fits"), test.get_test_filepath("resampled_hmi.fits") ]) def original(request): return sunpy.map.Map(request.param) @pytest.fixture def prep_map(original): return aiaprep(original) def test_aiaprep(original, prep_map): # Test that header info for the map has been correctly updated # Check all of these for Map attributes and .meta values? # Check array shape
def test_get_parse_obssum_hdulist(): hdulist = sunpy.io.read_file(get_test_filepath('hsi_obssumm_20110404_042.fits.gz')) header, _data = rhessi.parse_observing_summary_hdulist(hdulist) assert header.get('DATE_OBS') == '2011-04-04T00:00:00.000' assert header.get('DATE_END') == '2011-04-05T00:00:00.000' assert header.get('TELESCOP') == 'HESSI'