Skip to content

Commit

Permalink
Fix use of ads module to only get references for returned pulsars rat…
Browse files Browse the repository at this point in the history
…her than trying to get them all
  • Loading branch information
mattpitkin committed Nov 27, 2017
1 parent 15e4f60 commit b5c3c43
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
3 changes: 3 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
'sphinx.ext.napoleon'
]

autodoc_default_flags = ['members', 'show-inheritance']
autodoc_member_order = 'bysource'

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

Expand Down
1 change: 0 additions & 1 deletion docs/source/query.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ Querying

.. automodule:: psrqpy.search
:members:

2 changes: 1 addition & 1 deletion psrqpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

""" A Python tool for interacting with the ATNF pulsar catalogue """

__version__ = "0.3.5"
__version__ = "0.3.6"

from .search import QueryATNF
from .pulsar import Pulsar, Pulsars
Expand Down
23 changes: 20 additions & 3 deletions psrqpy/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def __init__(self, params=None, condition=None, psrtype=None, assoc=None, bincom

# get references is required
if self._include_refs:
self._refs = get_references(useads=self._adsref)
self._refs = get_references()

# perform query
self._query_content = self.generate_query()
Expand Down Expand Up @@ -390,8 +390,25 @@ def parse_query(self, requestcontent=''):
refstring2 = re.sub(r'\s+', ' ', refstring.format(**thisref)) # remove any superfluous whitespace
self._query_output[p+'_REF'][idx] = ','.join([a for a in refstring2.split(',') if a.strip()]) # remove any superfluous empty ',' seperated values

if self._adsref and 'ADS URL' in thisref:
self._query_output[p+'_REFURL'][idx] = thisref['ADS URL'] # remove any superfluous whitespace
if self._adsref:
if 'ADS URL' not in thisref: # get ADS reference
try:
import ads
except ImportError:
warnings.warn('Could not import ADS module, so no ADS information will be included', UserWarning)
article = []

try:
article = ads.SearchQuery(year=thisref['year'], first_author=thisref['authors'][0], title=thisref['title'])
except IOError:
warnings.warn('Could not get reference information, so no ADS information will be included', UserWarning)
article = []

article = list(article)
if len(article) > 0:
self._refs[reftag]['ADS URL'] = ADS_URL.format(list(article)[0].bibcode)

self._query_output[p+'_REFURL'][idx] = thisref['ADS URL']
else:
warnings.warn('Reference tag "{}" not found so omitting reference'.format(reftag), UserWarning)
vidx += 1
Expand Down
18 changes: 13 additions & 5 deletions psrqpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_references(useads=False):
queryrefs = requests.get(ATNF_BASE_URL + 'psrcat_ref.html')

if queryrefs.status_code != 200:
warnings.warn("Could query the ATNF references. No references returned".format(ATNF_VERSION), UserWarning)
warnings.warn("Could query the ATNF references. No references returned", UserWarning)
else:
try:
refsoup = BeautifulSoup(queryrefs.content, 'html.parser')
Expand Down Expand Up @@ -231,12 +231,20 @@ def get_references(useads=False):
warnings.warn('Could not import ADS module, so no ADS information will be included', UserWarning)
continue

refs[reftag]['ADS'] = None
refs[reftag]['ADS URL'] = ''

try:
article = list(ads.SearchQuery(year=refs[reftag]['year'], first_author=refs[reftag]['authors'][0], title=refs[reftag]['title']))[0]
refs[reftag]['ADS'] = article
refs[reftag]['ADS URL'] = ADS_URL.format(article.bibcode)
article = ads.SearchQuery(year=refs[reftag]['year'], first_author=refs[reftag]['authors'][0], title=refs[reftag]['title'])
except IOError:
warnings.warn('Could not import ADS module, so no ADS information will be included', UserWarning)
warnings.warn('Could not get reference information, so no ADS information will be included', UserWarning)
continue

article = list(article)

if len(article) > 0:
refs[reftag]['ADS'] = list(article)[0]
refs[reftag]['ADS URL'] = ADS_URL.format(list(article)[0].bibcode)

return refs

Expand Down

0 comments on commit b5c3c43

Please sign in to comment.