Skip to content

Commit

Permalink
Move to a net module and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Maloney committed Oct 18, 2020
1 parent 474f855 commit 961f860
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 30 deletions.
4 changes: 4 additions & 0 deletions radiospectra/net/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Import to register with Fido but keep out of namespace
from .sources.psp import RFRClient

__all__ = ['RFHClient']
Empty file.
45 changes: 27 additions & 18 deletions radiospectra/sources/psp.py → radiospectra/net/sources/psp.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
from datetime import datetime

import astropy.units as u
from astropy.time.core import TimeDelta
from astropy.time import Time

from astropy.time.core import TimeDelta
from sunpy.net.dataretriever.client import GenericClient
from sunpy.time.timerange import TimeRange
from sunpy.util.scraper import Scraper

__all__ = ['RFRClient']


class RFRClient(GenericClient):
"""
Provides access to the LYRA/Proba2 data `archive <http://proba2.oma.be/lyra/data/bsd/>`__
hosted by the `PROBA2 Science Center <http://proba2.oma.be>`__.
Provides access to Parker Solar Probe FIELDS Radio Frequency Receiver data
`archive <https://spdf.gsfc.nasa.gov/pub/data/psp/fields/>`__ at `NASA Goddard Space Physics
Data Facility (SPDF) <https://spdf.gsfc.nasa.gov>`__.
Examples
--------
>>> import radiospectra.net
>>> from sunpy.net import Fido, attrs as a
>>> results = Fido.search(a.Time("2019/10/01", "2019/11/01"),
... a.Instrument('RFR')) #doctest: +REMOTE_DATA
>>> results #doctest: +REMOTE_DATA +ELLIPSIS
<sunpy.net.fido_factory.UnifiedResponse object at ...>
... results = Fido.search(a.Time("2019/10/02", "2019/10/05"),
... a.Instrument('RFR')) #doctest: +REMOTE_DATA
... print(results)
Results from 1 Provider:
<BLANKLINE>
2 Results from the LYRAClient:
4 Results from the RFRClient:
Start Time End Time Source Instrument Wavelength
str19 str19 str3 str6 str3
------------------- ------------------- ------ ---------- ----------
2016-01-01 00:00:00 2016-01-02 00:00:00 Proba2 lyra nan
2016-01-01 00:00:00 2016-01-02 00:00:00 Proba2 lyra nan
<BLANKLINE>
2019-10-02 00:00:00 2019-10-03 00:00:00 PSP FIELDS nan
2019-10-03 00:00:00 2019-10-04 00:00:00 PSP FIELDS nan
2019-10-04 00:00:00 2019-10-05 00:00:00 PSP FIELDS nan
2019-10-05 00:00:00 2019-10-06 00:00:00 PSP FIELDS nan
<BLANKLINE>
"""

def _get_url_for_timerange(self, timerange, **kwargs):
"""
Return URL(s) for corresponding timerange.
Parameters
----------
timerange : `~sunpy.time.TimeRange`
The time range you want the files for.
Returns
-------
`list`
Expand All @@ -60,31 +66,34 @@ def _get_time_for_url(self, urls):
times.append(TimeRange(time_start, time_start + TimeDelta(1 * u.day)))
return times


def _makeimap(self):
"""
Helper Function:used to hold information about source.
"""
self.map_['source'] = 'PSP'
self.map_['instrument'] = 'FIELDS'
self.map_['detector'] = 'RFR'
self.map_['physobs'] = 'flux'
self.map_['provider'] = 'gsfc'

@classmethod
def _can_handle_query(cls, *query):
"""
Answers whether client can service the query.
Can this client can service the query.
Parameters
----------
query : list of query objects
query : `tuple`
Query parameters
Returns
-------
boolean
answer as to whether client can service the query
`bool`
True if client can handle query
"""
chkattr = ['Time', 'Instrument']
chklist = [x.__class__.__name__ in chkattr for x in query]
for x in query:
if x.__class__.__name__ == 'Instrument' and x.value.lower() == 'rfr':
return all(chklist)
return False
return False
Empty file.
61 changes: 61 additions & 0 deletions radiospectra/net/sources/tests/test_psp_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import pytest

from astropy.time import Time
from sunpy.net import Fido
from sunpy.net import attrs as a
from sunpy.time.timerange import TimeRange

from radiospectra.net.sources.psp import RFRClient

client = RFRClient()


@pytest.mark.remote_data
def test_fido():
atr = a.Time('2019/10/01', '2019/11/01')
res = Fido.search(atr, a.Instrument('rfr'))
res0 = res.get_response(0)
isinstance(res0.client, RFRClient)
assert len(res0) == 31
res_time_range = res0.time_range()
assert res_time_range.start.datetime == Time('2019-10-01T00:00').datetime
assert res_time_range.end.datetime == Time('2019-11-02T00:00').datetime


@pytest.mark.remote_data
def test_get_url_for_time_range():
url_start = 'https://spdf.gsfc.nasa.gov/pub/data/psp/fields/l2/rfs_hfr/2019/' \
'psp_fld_l2_rfs_hfr_20191001_v02.cdf'
url_end = 'https://spdf.gsfc.nasa.gov/pub/data/psp/fields/l2/rfs_hfr/2019/' \
'psp_fld_l2_rfs_hfr_20191015_v02.cdf'
tr = TimeRange('2019/10/01', '2019/10/15')
urls = client._get_url_for_timerange(tr)
assert isinstance(urls, list)
assert urls[0] == url_start
assert urls[-1] == url_end


def test_get_time_for_url():
urls = ['https://spdf.gsfc.nasa.gov/pub/data/psp/fields/l2/rfs_hfr/2019/'
'psp_fld_l2_rfs_hfr_20191001_v02.cdf',
'https://spdf.gsfc.nasa.gov/pub/data/psp/fields/l2/rfs_hfr/2019/'
'psp_fld_l2_rfs_hfr_20191015_v02.cdf']

times = client._get_time_for_url(urls)
assert times[0] == TimeRange('2019-10-01', '2019-10-02')
assert times[1] == TimeRange('2019-10-15', '2019-10-16')


def test_can_handle_query():
atr = a.Time('2019/10/01', '2019/11/01')
res = client._can_handle_query(atr, a.Instrument('rfr'))
assert res is True
res = client._can_handle_query(atr)
assert res is False


@pytest.mark.remote_data
def test_get():
query = client.search(a.Time('2019/10/05', '2019/10/10'), a.Instrument('rfr'))
download_list = client.fetch(query)
assert len(download_list) == len(query)
1 change: 0 additions & 1 deletion radiospectra/sources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@

from .callisto import CallistoSpectrogram
from .swaves import SWavesSpectrogram
from .psp import RFRClient
11 changes: 0 additions & 11 deletions radiospectra/tests/test_psp_client.py

This file was deleted.

0 comments on commit 961f860

Please sign in to comment.