Skip to content

Created method to retrieve the wavelength lines from NIST. #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .rtd-environment.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: packagename
name: specreduce

channels:
- astropy
Expand All @@ -8,5 +8,7 @@ dependencies:
- Cython
- matplotlib
- numpy
- pandas
- beautifulsoup4
# - pip:
# - dependency_from_pip
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ env:
# For this package-template, we include examples of Cython modules,
# so Cython is required for testing. If your package does not include
# Cython code, you can set CONDA_DEPENDENCIES=''
- CONDA_DEPENDENCIES='Cython'
- CONDA_DEPENDENCIES='Cython beautifulsoup4 lxml pandas requests'

# Conda packages for affiliated packages are hosted in channel
# "astropy" while builds for astropy LTS with recent numpy versions
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ license = BSD
url = http://astropy.org/
edit_on_github = False
github_project = astropy/specreduce
install_requires = astropy, numpy
install_requires = astropy, beautifulsoup4, lxml, numpy, pandas, requests
# version should be PEP440 compatible (http://www.python.org/dev/peps/pep-0440)
version = 0.0.dev

Expand Down
33 changes: 33 additions & 0 deletions specreduce/tests/test_nist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-

from __future__ import division, print_function

import unittest
import numpy

from specreduce.utils import nist

__author__ = 'Bruno Quint'


class NistTest(unittest.TestCase):

def test_get_data(self):

df = nist.get_nist_data('Ne', 6570., 6630.)

test_ne_wav = numpy.array([6598.9528, 6602.9007])
test_ne_int = numpy.array([10000., 1000.])

self.assertTrue('spectrum' in df.columns)
self.assertTrue('wavelength' in df.columns)
self.assertTrue('rel_int' in df.columns)
self.assertTrue('reference' in df.columns)

numpy.testing.assert_almost_equal(df.wavelength, test_ne_wav)
numpy.testing.assert_almost_equal(df.rel_int, test_ne_int)


if __name__ == '__main__':
unittest.main()
2 changes: 2 additions & 0 deletions specreduce/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

from . import nist
120 changes: 120 additions & 0 deletions specreduce/utils/nist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
"""
File which contains a method that can be used to retrieve data from
the National Institute of Standards and Technology (NIST) Atomic Spectra
Database.
"""

from __future__ import print_function, absolute_import, division

import pandas as pd
import requests

from bs4 import BeautifulSoup


__author__ = 'Bruno Quint'


NIST_URL = 'https://physics.nist.gov/cgi-bin/ASD/lines1.pl'
NIST_PAYLOAD = {
'spectra': None,
'limits_type': 0,
'low_w': None,
'upp_w': None,
'unit': 0,
'submit': 'Retrieve Data',
'de': 0,
'format': 0,
'line_out': 0,
'en_unit': 0,
'output': 0,
'bibrefs': 1,
'page_size': 15,
'show_obs_wl': 1,
'show_calc_wl': 1,
'unc_out': 1,
'order_out': 0,
'max_low_enrg': '',
'show_av': 2,
'max_upp_enrg': '',
'tsb_value': 0,
'min_str': '',
'A_out': 0,
'intens_out': 'on',
'max_str': '',
'allowed_out': 1,
'forbid_out': 1,
'min_accur': '',
'min_intens': '',
'conf_out': 'on',
'term_out': 'on',
'enrg_out': 'on',
'J_out': 'on',
}


def get_nist_data(element, blue_limit, red_limit):
"""
Retrieve data from NIST using a GET request and parsing the results into
two NumPy arrays: one for the wavelengths and other for the intensities.

Parameters
----------
element : str
A string that matches a element (e.g.: "Th", "Cu", "Na").

blue_limit : float
Blue wavelength in Angstrom.

red_limit : float
Red wavelength in Angstrom.

Returns
-------
df : DataFrame
A Pandas DataFrame containing four columns
[spectrum, wavelength, rel_int, reference].

References
----------

- Kramida, A., Ralchenko, Yu., Reader, J., and NIST ASD Team (2018).
NIST Atomic Spectra Database (ver. 5.5.6), [Online].
Available: https://physics.nist.gov/asd [2018, June 22].
National Institute of Standards and Technology, Gaithersburg, MD.

"""
NIST_PAYLOAD['spectra'] = element
NIST_PAYLOAD['low_w'] = blue_limit
NIST_PAYLOAD['upp_w'] = red_limit

r = requests.get(NIST_URL, params=NIST_PAYLOAD)
soup = BeautifulSoup(r.content, 'lxml')

_ = [s.extract() for s in soup.find_all('table',
attrs={'width': ['75%', '100%']})]

_ = [s.extract() for s in soup(['script', 'span'])]

table = soup.find('table', attrs={'rules': 'groups'})
header, body = table.find_all('tbody')
header.name = 'theader'

_ = [s.extract() for s in table('theader')]

_df = pd.read_html(str(table))[0]
_df = _df[[0, 1, 5, 13]]

_df.columns = ['spectrum', 'wavelength', 'rel_int', 'reference']

_df['wavelength'] = _df['wavelength'].str.replace(" ", "")
_df['wavelength'] = _df['wavelength'].astype(float)

_df = _df[_df['rel_int'].apply(lambda x: str(x).isnumeric())]
_df['rel_int'] = _df['rel_int'].astype(float)

_df = _df.dropna()

return _df