Example #1
0
def test_unifiedresponse_slicing_reverse():
    results = Fido.search(a.Time("2012/1/1", "2012/1/5"), a.Instrument("lyra"))
    assert isinstance(results[::-1], UnifiedResponse)
    assert len(results[::-1]) == len(results)
    assert isinstance(results[0, ::-1], UnifiedResponse)
    assert all(results._list[0][::-1].build_table() == results[
        0, ::-1]._list[0].build_table())
Example #2
0
def test_responses():
    results = Fido.search(a.Time("2012/1/1", "2012/1/5"), a.Instrument("lyra"))

    for i, resp in enumerate(results.responses):
        assert isinstance(resp, QueryResponse)

    assert i + 1 == len(results)
Example #3
0
def download(dtime, dt, dir_path, *args):
    """
    Description:
              download fits files for specified time window and wavelength channels
              from the VSO


    dtime:    should be a datetime object. EX: datetime(2010, 6, 13, 5, 25, 0)
    dt:       should be a time delta object in correct units. EX: timedelta(seconds=6)
    dir_path: path to directory for storing fits files
    *argv:    wavelength channels you want fits files for. EX: 193, 211, etc... 
    """


    sform = '%Y/%m/%d %H:%M:%S'  # format datetime objects correctly

    start = datetime.strftime(dtime-dt, sform)
    end = datetime.strftime(dtime+dt, sform)

    time = attrs.Time(start, end)
    ins = attrs.Instrument('aia') 


    for thing in args:
        print("Downloading {} angstrom data\n".format(thing)) 
        wave = attrs.Wavelength(int(thing)*u.AA)
        searchResults = Fido.search(time, ins, wave)

        for file_ in searchResults:
            print("{}".format(file_))

        dl_fil = Fido.fetch(searchResults, path=dir_path, wait=True) 
Example #4
0
def qr_block_with_missing_physobs():
    return vso.VSOClient().search(
        net_attrs.Time('20130805T120000', '20130805T121000'),
        net_attrs.Instrument('SWAVES'), net_attrs.Source('STEREO_A'),
        net_attrs.Provider('SSC'), net_attrs.Wavelength(
            10 * u.kHz, 160 * u.kHz),
        response_format="legacy")[0]
Example #5
0
def test_can_handle_query():
    ans1 = rhessi.RHESSIClient._can_handle_query(
        a.Time('2012/8/9', '2012/8/9'), a.Instrument('rhessi'))
    assert ans1 is True
    ans2 = rhessi.RHESSIClient._can_handle_query(a.Time(
        '2013/2/7', '2013/2/7'))
    assert ans2 is False
Example #6
0
def test_fetch_separate_filenames():
    # Setup
    db = Database('sqlite:///')

    download_query = [
        net_attrs.Time('2012-08-05', '2012-08-05 00:00:05'),
        net_attrs.Instrument('AIA')
    ]

    tmp_test_dir = os.path.join(sunpy.config.get('downloads', 'download_dir'),
                                'tmp_test_dir/')

    if not os.path.isdir(tmp_test_dir):
        os.makedirs(tmp_test_dir)

    path = tmp_test_dir + '{file}'

    db.fetch(*download_query, path=path)

    # Test
    assert len(db) == 4

    dir_contents = os.listdir(tmp_test_dir)
    assert 'aia_lev1_335a_2012_08_05t00_00_02_62z_image_lev1.fits' in dir_contents
    assert 'aia_lev1_94a_2012_08_05t00_00_01_12z_image_lev1.fits' in dir_contents
    assert os.path.isfile(
        os.path.join(tmp_test_dir,
                     'aia_lev1_335a_2012_08_05t00_00_02_62z_image_lev1.fits'))
    assert os.path.isfile(
        os.path.join(tmp_test_dir,
                     'aia_lev1_94a_2012_08_05t00_00_01_12z_image_lev1.fits'))

    # Teardown
    shutil.rmtree(tmp_test_dir)
Example #7
0
def test_client_fetch_wrong_type(mock_fetch):
    query = a.Time("2011/01/01", "2011/01/02") & a.Instrument("goes")

    qr = Fido.search(query)

    with pytest.raises(TypeError):
        Fido.fetch(qr)
Example #8
0
def test_unified_response():
    start = parse_time("2012/1/1")
    end = parse_time("2012/1/2")
    qr = Fido.search(a.Instrument('EVE'), a.Level(0), a.Time(start, end))
    assert qr.file_num == 2
    strings = ['eve', 'SDO', start.strftime(TIMEFORMAT), end.strftime(TIMEFORMAT)]
    assert all(s in qr._repr_html_() for s in strings)
Example #9
0
def test_srs_tar_unpack_midyear():
    qr = Fido.search(
        a.Instrument("soon") & a.Time("2011/06/07", "2011/06/08T23:59:29"))
    res = Fido.fetch(qr)
    assert len(res) == 2
    assert res.data[0].endswith("20110607SRS.txt")
    assert res.data[-1].endswith("20110608SRS.txt")
Example #10
0
def test_repr():
    results = Fido.search(a.Time("2012/1/1", "2012/1/5"), a.Instrument("lyra"))

    rep = repr(results)
    rep = rep.split('\n')
    # 6 header lines, the results table and two blank lines at the end
    assert len(rep) == 6 + len(list(results.responses)[0]) + 2
Example #11
0
def test_fido(mock_fetch):
    qr = Fido.search(a.Time("2012/10/4", "2012/10/6"),
                     a.Instrument('noaa-indices'))
    assert isinstance(qr, UnifiedResponse)

    response = Fido.fetch(qr)
    assert len(response) == qr._numfile
Example #12
0
def test_srs_current_year():
    # Current year is nothing but text files, all older years should be tar files.
    year = datetime.date.today().year
    qr = Fido.search(a.Instrument("soon") & a.Time(f"{year}/01/01", f"{year}/01/01T23:59:29"))
    res = Fido.fetch(qr)
    assert len(res) <= 1
    if len(res):
        assert res.data[0].endswith(f"{year}0101SRS.txt")
Example #13
0
def test_srs_current_year():
    year = datetime.date.today().year
    qr = Fido.search(
        a.Instrument("soon")
        & a.Time(f"{year}/01/01", f"{year}/01/01T23:59:29"))
    res = Fido.fetch(qr)
    assert len(res) == 1
    assert res.data[0].endswith(f"{year}0101SRS.txt")
Example #14
0
def test_query(mock_get_observing_summary_dbase_file,
               mock_parse_observing_summary_dbase_file, mock_get_base_url):
    qr1 = LCClient.search(a.Time('2003-11-01', '2003-11-03'),
                          a.Instrument('rhessi'))
    assert isinstance(qr1, QueryResponse)
    assert len(qr1) == 3
    assert qr1.time_range().start == parse_time('2003/11/01')
    assert qr1.time_range().end == parse_time('2003/11/03T23:59:59.999')
Example #15
0
def test_response_block_properties(client):
    res = client.search(a.Time('2020/3/4', '2020/3/6'),
                        a.Instrument('aia'),
                        a.Wavelength(171 * u.angstrom),
                        a.Sample(10 * u.minute),
                        response_format="legacy")
    properties = res.response_block_properties()
    assert len(properties) == 0
def test_can_handle_query(suvi_client, time):
    ans1 = suvi_client._can_handle_query(time, a.Instrument('suvi'))
    assert ans1 is True
    ans2 = suvi_client._can_handle_query(time, a.Instrument('suvi'),
                                         a.Wavelength(131 * u.Angstrom))
    assert ans2 is True
    ans3 = suvi_client._can_handle_query(time, a.Instrument('suvi'),
                                         a.Wavelength(131 * u.Angstrom),
                                         a.Level(2))
    assert ans3 is True
    ans4 = suvi_client._can_handle_query(time)
    assert ans4 is False
    ans5 = suvi_client._can_handle_query(time, a.Instrument('aia'))
    assert ans5 is False
    ans6 = suvi_client._can_handle_query(time, a.Instrument('suvi'),
                                         a.goes.SatelliteNumber(16))
    assert ans6 is True
Example #17
0
def test_save_path(tmpdir):
    qr = Fido.search(a.Instrument('EVE'), a.Time("2016/10/01", "2016/10/02"), a.Level(0))

    # Test when path is str
    files = Fido.fetch(qr, path=str(tmpdir / "{instrument}" / "{level}"))
    for f in files:
        assert str(tmpdir) in f
        assert "eve{}0".format(os.path.sep) in f
Example #18
0
def test_path_read_only(tmp_path):
    results = Fido.search(a.Time("2012/1/1", "2012/1/5"), a.Instrument("lyra"))

    # chmod dosen't seem to work correctly on the windows CI
    os.chmod(tmp_path, S_IREAD | S_IRGRP | S_IROTH)
    # Check to see if it's actually read only before running the test
    if not os.access(tmp_path, os.W_OK):
        with pytest.raises(PermissionError):
            Fido.fetch(results, path=tmp_path / "{file}")
Example #19
0
def test_fido():
    atr = a.Time('2019/10/01', '2019/10/02')
    res = Fido.search(atr, a.Instrument('rfs'))
    res0 = res[0]
    isinstance(res0.client, RFSClient)
    assert len(res0) == 4
    assert res['rfs']['Start Time'].min() == Time('2019-10-01T00:00').datetime
    assert res['rfs']['End Time'].max() == Time(
        '2019-10-02T23:59:59.999').datetime
Example #20
0
def test_vso_fetch_hmi(tmpdir):
    start_time = "2017-01-25"
    end_time = "2017-01-25T23:59:59"
    results = Fido.search(a.Time(start_time, end_time),
                          a.Instrument('HMI') & a.Physobs("LOS_magnetic_field"),
                          a.Sample(1 * u.minute))

    files = Fido.fetch(results[0, 0], path=tmpdir)
    assert len(files) == 1
Example #21
0
def fido_search_result():
    # A search query with responses from all instruments
    # No JSOC query
    return Fido.search(
        net_attrs.Time("2012/1/1", "2012/1/2"),
        net_attrs.Instrument('lyra') | net_attrs.Instrument('eve')
        | net_attrs.Instrument('XRS') | net_attrs.Instrument('noaa-indices')
        | net_attrs.Instrument('noaa-predict') |
        (net_attrs.Instrument('norh') & net_attrs.Wavelength(17 * u.GHz))
        | net_attrs.Instrument('rhessi') |
        (net_attrs.Instrument('EVE') & net_attrs.Level(0)))
Example #22
0
def vso_session():
    client = vso.VSOClient()
    qr = client.search(a.Time((2011, 9, 20, 1), (2011, 9, 20, 2)),
                       a.Instrument('RHESSI'))
    entries = tables.entries_from_query_result(qr)
    database = Database('sqlite:///:memory:')
    for entry in entries:
        database.add(entry)
    database.commit()
    return database.session
Example #23
0
def offline_instruments():
    """
    Returns a strategy for any instrument that does not need the internet to do
    a query
    """
    offline_instr = ['lyra', 'norh', 'noaa-indices', 'noaa-predict', 'goes']
    offline_instr = st.builds(a.Instrument, st.sampled_from(offline_instr))

    eve = st.just(a.Instrument('eve') & a.Level(0))
    return st.one_of(offline_instr, eve)
Example #24
0
def test_save_path_pathlib(tmpdir):
    qr = Fido.search(a.Instrument('EVE'), a.Time("2016/10/01", "2016/10/02"), a.Level(0))

    # Test when path is pathlib.Path
    target_dir = tmpdir.mkdir("down")
    path = pathlib.Path(target_dir, "{instrument}", "{level}")
    files = Fido.fetch(qr, path=path)
    for f in files:
        assert target_dir.strpath in f
        assert "eve{}0".format(os.path.sep) in f
Example #25
0
def test_client(urlopen, client, http_responses):
    urlopen.return_value.read = mock.MagicMock()
    urlopen.return_value.read.side_effect = http_responses
    urlopen.close = mock.MagicMock(return_value=None)
    query = client.search(a.Time('2003/03/15 00:00', '2003/03/15 23:59'),
                          a.Instrument('RSTN'))
    assert urlopen.call_count == 5
    # last call arg should be san-vito url
    assert urlopen.call_args[0][0] == 'https://www.ngdc.noaa.gov/stp/space-weather/solar-data/' \
                                      'solar-features/solar-radio/rstn-spectral/san-vito/2003/03/'
    assert len(query) == 3
Example #26
0
def test_tables_single_response():
    results = Fido.search(a.Time("2012/1/1", "2012/1/5"), a.Instrument("lyra"))
    tables = results.tables

    assert isinstance(tables, list)
    assert isinstance(tables[0], Table)
    assert len(tables) == 1

    columns = ['Start Time', 'End Time', 'Source', 'Instrument', 'Wavelength']
    assert columns == tables[0].colnames
    assert len(tables[0]) == 5
Example #27
0
def test_deprecated_identifier():
    id = a.Instrument('EUI')
    time = a.Time('2021-02-01', '2021-02-02')
    level = a.Level(1)
    with pytest.warns(SunpyDeprecationWarning):
        identifier = a.soar.Identifier('EUI-FSI174-IMAGE')
    product = a.soar.Product('EUI-FSI174-IMAGE')
    res1 = Fido.search(id, time, level, identifier)
    res2 = Fido.search(id, time, level, product)

    assert res1.__str__() == res2.__str__()
Example #28
0
def test_insitu_search():
    id = a.Instrument('MAG')
    time = a.Time('2020-04-16', '2020-04-17')
    identifier = a.soar.Product('MAG-RTN-NORMAL-1-MINUTE')

    res = Fido.search(id, time, identifier)
    assert len(res) == 1
    assert len(res[0]) == 2

    files = Fido.fetch(res[0, 0])
    assert len(files) == 1
Example #29
0
def test_save_path():
    with tempfile.TemporaryDirectory() as target_dir:
        qr = Fido.search(a.Instrument('EVE'), a.Time("2016/10/01",
                                                     "2016/10/02"), a.Level(0))
        files = Fido.fetch(qr,
                           path=os.path.join(
                               target_dir,
                               "{instrument}" + os.path.sep + "{level}"))
        for f in files:
            assert target_dir in f
            assert "eve{}0".format(os.path.sep) in f
Example #30
0
def test_client_observatory(urlopen, client, http_responses):
    urlopen.return_value.read = mock.MagicMock()
    urlopen.return_value.read.side_effect = http_responses[-1:]
    urlopen.close = mock.MagicMock(return_value=None)
    query = client.search(a.Time('2003/03/15 00:00', '2003/03/15 23:59'),
                          a.Instrument('RSTN'), Observatory('San Vito'))
    urlopen.assert_called_once_with(
        'https://www.ngdc.noaa.gov/stp/space-weather/solar-data/'
        'solar-features/solar-radio/rstn-spectral/san-vito/2003/03/')
    assert len(query) == 1
    assert query['Observatory'] == 'San Vito'