12
12
from astropy import units as u
13
13
from bs4 import BeautifulSoup
14
14
15
+ from .. import log
16
+
15
17
from ..query import BaseQuery
16
18
from ..utils import prepend_docstr_nosections , async_to_sync
17
19
from . import conf
@@ -27,19 +29,14 @@ class AtomicLineListClass(BaseQuery):
27
29
28
30
def __init__ (self ):
29
31
super (AtomicLineListClass , self ).__init__ ()
30
- self ._default_form_values = None
31
-
32
- def query_object (self , wavelength_range = None , wavelength_type = None ,
33
- wavelength_accuracy = None , element_spectrum = None ,
34
- minimal_abundance = None , depl_factor = None ,
35
- lower_level_energy_range = None ,
36
- upper_level_energy_range = None , nmax = None ,
37
- multiplet = None , transitions = None ,
38
- show_fine_structure = None ,
39
- show_auto_ionizing_transitions = None ,
40
- output_columns = ('spec' , 'type' , 'conf' ,
41
- 'term' , 'angm' , 'prob' ,
42
- 'ener' )):
32
+ self .__default_form_values = None
33
+
34
+ def query_object (self , wavelength_range = None , wavelength_type = None , wavelength_accuracy = None , element_spectrum = None ,
35
+ minimal_abundance = None , depl_factor = None , lower_level_energy_range = None ,
36
+ upper_level_energy_range = None , nmax = None , multiplet = None , transitions = None ,
37
+ show_fine_structure = None , show_auto_ionizing_transitions = None ,
38
+ output_columns = ('spec' , 'type' , 'conf' , 'term' , 'angm' , 'prob' , 'ener' ), cache = True ,
39
+ get_query_payload = False ):
43
40
"""
44
41
Queries Atomic Line List for the given parameters adnd returns the
45
42
result as a `~astropy.table.Table`. All parameters are optional.
@@ -163,34 +160,26 @@ def query_object(self, wavelength_range=None, wavelength_type=None,
163
160
multiplet = multiplet , transitions = transitions ,
164
161
show_fine_structure = show_fine_structure ,
165
162
show_auto_ionizing_transitions = show_auto_ionizing_transitions ,
166
- output_columns = output_columns )
163
+ output_columns = output_columns , cache = cache ,
164
+ get_query_payload = get_query_payload )
165
+ if get_query_payload :
166
+ return input
167
167
table = self ._parse_result (response )
168
168
return table
169
169
170
170
@prepend_docstr_nosections (query_object .__doc__ )
171
- def query_object_async (self , wavelength_range = None , wavelength_type = '' ,
172
- wavelength_accuracy = None , element_spectrum = None ,
173
- minimal_abundance = None , depl_factor = None ,
174
- lower_level_energy_range = None ,
175
- upper_level_energy_range = None , nmax = None ,
176
- multiplet = None , transitions = None ,
177
- show_fine_structure = None ,
178
- show_auto_ionizing_transitions = None ,
179
- output_columns = ('spec' , 'type' , 'conf' ,
180
- 'term' , 'angm' , 'prob' ,
181
- 'ener' )):
171
+ def query_object_async (self , wavelength_range = None , wavelength_type = '' , wavelength_accuracy = None ,
172
+ element_spectrum = None , minimal_abundance = None , depl_factor = None ,
173
+ lower_level_energy_range = None , upper_level_energy_range = None , nmax = None , multiplet = None ,
174
+ transitions = None , show_fine_structure = None , show_auto_ionizing_transitions = None ,
175
+ output_columns = ('spec' , 'type' , 'conf' , 'term' , 'angm' , 'prob' , 'ener' ),
176
+ cache = True , get_query_payload = False ):
182
177
"""
183
178
Returns
184
179
-------
185
180
response : `requests.Response`
186
181
The HTTP response returned from the service.
187
182
"""
188
- if self ._default_form_values is None :
189
- response = self ._request ("GET" , url = self .FORM_URL , data = {},
190
- timeout = self .TIMEOUT )
191
- bs = BeautifulSoup (response .text )
192
- form = bs .find ('form' )
193
- self ._default_form_values = self ._get_default_form_values (form )
194
183
default_values = self ._default_form_values
195
184
wltype = (wavelength_type or default_values .get ('air' , '' )).lower ()
196
185
if wltype in ('air' , 'vacuum' ):
@@ -233,7 +222,7 @@ def query_object_async(self, wavelength_range=None, wavelength_type='',
233
222
upper_level_erange = upper_level_erange .to (
234
223
u .cm ** - 1 , equivalencies = u .spectral ()).value
235
224
input = {
236
- 'wavl' : '- ' .join (map (str , wlrange_in_angstroms )),
225
+ 'wavl' : ' ' .join (map (str , wlrange_in_angstroms )),
237
226
'wave' : 'Angstrom' ,
238
227
'air' : air ,
239
228
'wacc' : wavelength_accuracy ,
@@ -250,8 +239,12 @@ def query_object_async(self, wavelength_range=None, wavelength_type='',
250
239
'hydr' : show_fine_structure ,
251
240
'auto' : show_auto_ionizing_transitions ,
252
241
'form' : output_columns ,
253
- 'tptype' : 'as_a' }
254
- response = self ._submit_form (input )
242
+ 'jval' : 'usej' ,
243
+ 'tptype' : 'as_a' ,
244
+ }
245
+ if get_query_payload :
246
+ return input
247
+ response = self ._submit_form (input , cache = cache )
255
248
return response
256
249
257
250
def _parse_result (self , response ):
@@ -288,32 +281,39 @@ def _parse_result(self, response):
288
281
# return an empty table if the query yielded no results
289
282
return Table ()
290
283
291
- def _submit_form (self , input = None ):
284
+ def _submit_form (self , input = None , cache = True ):
292
285
"""Fill out the form of the SkyView site and submit it with the
293
286
values given in `input` (a dictionary where the keys are the form
294
287
element's names and the values are their respective values).
295
288
296
289
"""
297
290
if input is None :
298
291
input = {}
299
- response = self ._request ("GET" , url = self .FORM_URL , data = {},
300
- timeout = self .TIMEOUT )
301
- bs = BeautifulSoup (response .text )
302
- form = bs .find ('form' )
303
- # cache the default values to save HTTP traffic
304
- if self ._default_form_values is None :
305
- self ._default_form_values = self ._get_default_form_values (form )
306
292
# only overwrite payload's values if the `input` value is not None
307
293
# to avoid overwriting of the form's default values
308
294
payload = self ._default_form_values .copy ()
309
295
for k , v in six .iteritems (input ):
310
296
if v is not None :
311
297
payload [k ] = v
312
- url = urlparse .urljoin (self .FORM_URL , form .get ('action' ))
298
+ url = urlparse .urljoin (self .FORM_URL , self ._default_form .get ('action' ))
299
+ log .debug (f"final payload = { payload } from url={ url } " )
313
300
response = self ._request ("POST" , url = url , data = payload ,
314
- timeout = self .TIMEOUT )
301
+ timeout = self .TIMEOUT , cache = cache )
302
+ log .debug ("Retrieved data from POST request" )
315
303
return response
316
304
305
+ @property
306
+ def _default_form_values (self ):
307
+
308
+ if self .__default_form_values is None :
309
+ response = self ._request ("GET" , url = self .FORM_URL , data = {},
310
+ timeout = self .TIMEOUT , cache = True )
311
+ bs = BeautifulSoup (response .text )
312
+ self ._default_form = form = bs .find ('form' )
313
+ self .__default_form_values = self ._get_default_form_values (form )
314
+
315
+ return self .__default_form_values
316
+
317
317
def _get_default_form_values (self , form ):
318
318
"""Return the already selected values of a given form (a BeautifulSoup
319
319
form node) as a dict.
0 commit comments