Skip to content

Commit edaa365

Browse files
jespinosaarbsipocz
authored andcommitted
JWSTPCR-179: remote-data tests
1 parent 83e6c20 commit edaa365

File tree

2 files changed

+351
-1
lines changed

2 files changed

+351
-1
lines changed
Lines changed: 350 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,350 @@
1+
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2+
"""
3+
===============
4+
JWST REMOTE DATA Tests
5+
===============
6+
7+
@author: Javier Espinosa Aranda
8+
9+
10+
European Space Astronomy Centre (ESAC)
11+
European Space Agency (ESA)
12+
13+
Created on 19 aug. 2020
14+
15+
16+
"""
17+
18+
import unittest
19+
import os
20+
import pytest
21+
import shutil
22+
import mock
23+
import requests
24+
import getpass
25+
26+
from astroquery.jwst import JwstClass
27+
import astropy.units as u
28+
from astropy.coordinates.sky_coordinate import SkyCoord
29+
from astropy.units import Quantity
30+
import numpy as np
31+
from astroquery.utils.tap.xmlparser import utils
32+
from astroquery.utils.tap.core import TapPlus
33+
from astropy.io.votable import parse
34+
from astropy.table.table import Table
35+
from astroquery.utils import TableList
36+
from astroquery.utils.tap.model.filter import Filter
37+
from astroquery.simbad import Simbad
38+
from astroquery.vizier import Vizier
39+
from astroquery.ned import Ned
40+
from astropy.table import Table
41+
from astropy import units
42+
from astropy.tests.helper import remote_data
43+
44+
from decimal import Decimal
45+
46+
47+
import tempfile
48+
49+
50+
def data_path(filename):
51+
data_dir = os.path.join(os.path.dirname(__file__), 'data')
52+
return os.path.join(data_dir, filename)
53+
54+
55+
def create_temp_folder():
56+
return tempfile.TemporaryDirectory()
57+
58+
59+
def get_license():
60+
with open(data_path('test_license.txt'), "r") as file:
61+
user = file.readline()
62+
password = file.readline()
63+
return user, password
64+
65+
66+
@remote_data
67+
class TestTap(unittest.TestCase):
68+
69+
temp_file_vot = '/temp.vot'
70+
71+
def test_load_tables(self):
72+
# This test will cover load_tables and load_table methods
73+
jwst = JwstClass()
74+
# load_tables
75+
tables = jwst.load_tables(only_names=True, include_shared_tables=True)
76+
table_names = []
77+
for table in (tables):
78+
table_names.append(table.name)
79+
# Checking main tables in TAP
80+
assert 'jwst.main' in table_names
81+
assert 'jwst.artifact' in table_names
82+
assert 'jwst.observation' in table_names
83+
assert 'jwst.observationmember' in table_names
84+
assert 'tap_schema.tables' in table_names
85+
assert 'tap_schema.schemas' in table_names
86+
87+
# load_table
88+
table = jwst.load_table('jwst.main')
89+
columns = []
90+
for column in table.columns:
91+
columns.append(column.name)
92+
# Checking columns in table
93+
assert 'obsid' in columns
94+
assert 'planeid' in columns
95+
assert 'calibrationlevel' in columns
96+
assert 'observationid' in columns
97+
assert 'target_ra' in columns
98+
assert 'target_dec' in columns
99+
print('a')
100+
101+
def test_launch_job(self):
102+
# This test will cover launch_job method
103+
jwst = JwstClass()
104+
job = jwst.launch_job('SELECT TOP 100 '
105+
'instrument_name, observationuri, planeid, '
106+
'calibrationlevel, dataproducttype, '
107+
'target_ra, target_dec FROM jwst.main ORDER BY '
108+
'instrument_name, observationuri',)
109+
r = job.get_results()
110+
assert(len(r) == 100)
111+
112+
# save_results => check if this method works
113+
# jwst.save_results(job)
114+
115+
temp_folder = create_temp_folder()
116+
temp_file = temp_folder.name + self.temp_file_vot
117+
jwst.launch_job('SELECT TOP 100 '
118+
'instrument_name, observationuri, planeid, '
119+
'calibrationlevel, dataproducttype, '
120+
'target_ra, target_dec FROM jwst.main ORDER BY '
121+
'instrument_name, observationuri',
122+
output_file=temp_file, dump_to_file=True)
123+
result = Table.read(temp_file)
124+
columns = []
125+
for column in result.columns:
126+
columns.append(column)
127+
# Checking columns in table
128+
assert 'observationuri' in columns
129+
assert 'planeid' in columns
130+
assert 'calibrationlevel' in columns
131+
assert 'instrument_name' in columns
132+
assert 'target_ra' in columns
133+
assert 'target_dec' in columns
134+
temp_folder.cleanup()
135+
136+
def test_async_job(self):
137+
# This test will cover launch_job_async, load_async_job,
138+
# search_async_job, list_async_job, save_results and
139+
# remove_jobs methods
140+
jwst = JwstClass()
141+
# launch_job_async
142+
job = jwst.launch_job_async(query='select top 100 * from jwst.main',
143+
name='test_job')
144+
jobid = job.jobid
145+
146+
# list_async_job
147+
jobs = jwst.list_async_jobs()
148+
assert len(jobs) > 0
149+
150+
# search_async_job
151+
# jobfilter = Filter()
152+
# jobfilter.add_filter('job', 'test_job')
153+
# Not working, something to be solved in TAP class?
154+
# searched_job = jwst.search_async_jobs(jobfilter)
155+
# assert job == searched_job
156+
157+
# load_async_job
158+
loaded_job = jwst.load_async_job(jobid)
159+
assert job.jobid == loaded_job.jobid
160+
assert job.name == loaded_job.name
161+
for x in range(len(loaded_job.get_results()[0])):
162+
assert str(job.get_results()[0][x]) == \
163+
str(loaded_job.get_results()[0][x])
164+
165+
# remove_jobs
166+
jwst.remove_jobs([jobid])
167+
jobs = jwst.list_async_jobs()
168+
for job in jobs:
169+
assert (job.jobid != jobid)
170+
171+
def test_query_region(self):
172+
# This test will cover query_region and query_region_async methods
173+
jwst = JwstClass()
174+
# query_region
175+
coord = SkyCoord(ra=53, dec=-27, unit=(u.degree, u.degree),
176+
frame='icrs')
177+
width = u.Quantity(5, u.deg)
178+
height = u.Quantity(5, u.deg)
179+
r = jwst.query_region(coordinate=coord, width=width, height=height)
180+
assert len(r) > 0 and len(r) <= 2000
181+
# check if the results are in the required box
182+
for result in r:
183+
assert Decimal(result['target_ra']) >= 50.5
184+
assert Decimal(result['target_ra']) <= 55.5
185+
assert Decimal(result['target_dec']) >= -29.5
186+
assert Decimal(result['target_dec']) <= -24.5
187+
188+
# query_region_async
189+
r_async = jwst.query_region_async(coordinate=coord, width=width,
190+
height=height)
191+
assert len(r_async) > 0
192+
# check if the results are in the required box
193+
for result in r_async:
194+
assert Decimal(result['target_ra']) >= 50.5
195+
assert Decimal(result['target_ra']) <= 55.5
196+
assert Decimal(result['target_dec']) >= -29.5
197+
assert Decimal(result['target_dec']) <= -24.5
198+
199+
def test_cone_search(self):
200+
# This test will cover cone_search and cone_search_async methods
201+
jwst = JwstClass()
202+
# cone_search
203+
coord = SkyCoord(ra=53, dec=-27, unit=(u.degree, u.degree),
204+
frame='icrs')
205+
radius = u.Quantity(5.0, u.deg)
206+
j = jwst.cone_search(coord, radius)
207+
r = j.get_results()
208+
for result in r:
209+
result_coord = SkyCoord(ra=Decimal(result['target_ra']),
210+
dec=Decimal(result['target_dec']),
211+
unit=(u.degree, u.degree),
212+
frame='icrs')
213+
sep = coord.separation(result_coord)
214+
assert sep.deg <= 5
215+
216+
# cone_search_async
217+
coord = SkyCoord(ra=45, dec=20, unit=(u.degree, u.degree),
218+
frame='icrs')
219+
radius = u.Quantity(2.0, u.deg)
220+
j_async = jwst.cone_search_async(coord, radius)
221+
r_async = j_async.get_results()
222+
for result in r_async:
223+
result_coord = SkyCoord(ra=Decimal(result['target_ra']),
224+
dec=Decimal(result['target_dec']),
225+
unit=(u.degree, u.degree),
226+
frame='icrs')
227+
sep = coord.separation(result_coord)
228+
assert sep.deg <= 2
229+
230+
def test_query_target(self):
231+
# This test will cover query_target and
232+
# resolve_target_coordinates methods
233+
jwst = JwstClass()
234+
235+
# resolve_target_coordinates 1
236+
target_name = 'M1'
237+
target_resolver = 'ALL'
238+
coord = jwst.resolve_target_coordinates(target_name,
239+
target_resolver)
240+
assert coord.ra.deg
241+
assert coord.dec.deg
242+
243+
# query_target 1
244+
radius = u.Quantity(3, u.deg)
245+
r = jwst.query_target(target_name=target_name,
246+
target_resolver=target_resolver,
247+
radius=radius)
248+
for result in r:
249+
result_coord = SkyCoord(ra=Decimal(result['target_ra']),
250+
dec=Decimal(result['target_dec']),
251+
unit=(u.degree, u.degree),
252+
frame='icrs')
253+
sep = coord.separation(result_coord)
254+
assert sep.deg <= 3
255+
256+
# resolve_target_coordinates 2
257+
target_name = 'LMC'
258+
target_resolver = 'NED'
259+
coord = jwst.resolve_target_coordinates(target_name,
260+
target_resolver)
261+
assert coord.ra.deg
262+
assert coord.dec.deg
263+
264+
# query_target 2
265+
width = u.Quantity(5, u.deg)
266+
height = u.Quantity(5, u.deg)
267+
r = jwst.query_target(target_name=target_name,
268+
target_resolver=target_resolver,
269+
width=width,
270+
height=height)
271+
for result in r:
272+
assert Decimal(result['target_ra']) >= coord.ra.deg - 2.5
273+
assert Decimal(result['target_ra']) <= coord.ra.deg + 2.5
274+
assert Decimal(result['target_dec']) >= coord.dec.deg - 2.5
275+
assert Decimal(result['target_dec']) <= coord.dec.deg + 2.5
276+
277+
@pytest.mark.skipif(not os.path.exists(data_path('test_license.txt')),
278+
reason='Test license is not available')
279+
def test_login_logout(self):
280+
# This test will cover login, login_gui and logout methods
281+
jwst = JwstClass()
282+
user, password = get_license()
283+
# login
284+
with pytest.raises(requests.HTTPError) as err:
285+
jwst.login(user='test', password='test')
286+
assert "HTTP Status 401 – Unauthorized" in err.value.args[0]
287+
288+
jwst.login(user=user, password=password)
289+
290+
# logout
291+
jwst.logout()
292+
293+
print('a')
294+
295+
def test_get_products(self):
296+
# This test will cover get_product_list, get_related_observation,
297+
# get_product and get_obs_products methods
298+
jwst = JwstClass()
299+
300+
# get_product_list
301+
observation_id = 'jw00777011001_02104_00001_nrcblong'
302+
product_list = jwst.get_product_list(observation_id=observation_id)
303+
assert(len(product_list) > 0)
304+
305+
# get_related_observation
306+
rel_obs = jwst.get_related_observations(observation_id=observation_id)
307+
assert(len(rel_obs) > 0)
308+
309+
# get_product
310+
query = "select a.artifactid, a.uri from jwst.artifact a, jwst.plane "\
311+
"p where p.planeid=a.planeid and "\
312+
"p.obsid='00000000-0000-0000-9c08-f5be8f3df805'"
313+
job = jwst.launch_job(query)
314+
job.get_results()
315+
artifact_id = '00000000-0000-0000-b8af-4b8f29f2b0d6'
316+
file_name = 'jw00617-o113_t001_nircam_f277w_cat.ecsv'
317+
output_file = jwst.get_product(file_name=file_name)
318+
assert os.path.exists(output_file)
319+
os.remove(output_file)
320+
output_file = jwst.get_product(artifact_id=artifact_id)
321+
assert os.path.exists(output_file)
322+
os.remove(output_file)
323+
324+
# get_obs_products
325+
observation_id = 'jw00617-o113_t001_nircam_f277w'
326+
temp_folder = create_temp_folder()
327+
jwst.get_obs_products(observation_id=observation_id,
328+
cal_level='ALL',
329+
product_type='science',
330+
output_file=temp_folder.name + '/test.tar')
331+
assert os.path.exists(temp_folder.name)
332+
assert os.path.exists(temp_folder.name + '/test.tar')
333+
assert os.path.exists(temp_folder.name + '/jw00617')
334+
assert os.path.exists(temp_folder.name + '/jw00617/level_3')
335+
temp_folder.cleanup()
336+
337+
@pytest.mark.skip
338+
def decode(self, key, string):
339+
encoded_chars = []
340+
for i in range(len(string)):
341+
key_c = key[i % len(key)]
342+
encoded_c = chr((ord(string[i]) - ord(key_c) + 256) % 256)
343+
encoded_chars.append(encoded_c)
344+
encoded_string = ''.join(encoded_chars)
345+
return encoded_string
346+
347+
348+
if __name__ == "__main__":
349+
# import sys;sys.argv = ['', 'Test.testName']
350+
unittest.main()

astroquery/jwst/tests/test_jwsttap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def associated_planes_request(request):
8181

8282

8383
def get_product_mock(params, *args, **kwargs):
84-
if(args[0] == 'file_name_id'):
84+
if('file_name' in kwargs and kwargs.get('file_name') == 'file_name_id'):
8585
return "00000000-0000-0000-8740-65e2827c9895"
8686
else:
8787
return "jw00617023001_02102_00001_nrcb4_uncal.fits"

0 commit comments

Comments
 (0)