|
| 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 getpass |
| 19 | +import os |
| 20 | +import shutil |
| 21 | +import tempfile |
| 22 | +import unittest |
| 23 | +from decimal import Decimal |
| 24 | + |
| 25 | +import astropy.units as u |
| 26 | +import mock |
| 27 | +import numpy as np |
| 28 | +import pytest |
| 29 | +import requests |
| 30 | +from astropy import units as u |
| 31 | +from astropy.coordinates.sky_coordinate import SkyCoord |
| 32 | +from astropy.io.votable import parse |
| 33 | +from astropy.table import Table |
| 34 | +from astropy.table.table import Table |
| 35 | +from astropy.tests.helper import remote_data |
| 36 | +from astropy.units import Quantity |
| 37 | +from astroquery.jwst import JwstClass |
| 38 | +from astroquery.ned import Ned |
| 39 | +from astroquery.simbad import Simbad |
| 40 | +from astroquery.utils import TableList |
| 41 | +from astroquery.utils.tap import TapPlus |
| 42 | +from astroquery.utils.tap.core import TapPlus |
| 43 | +from astroquery.utils.tap.model.filter import Filter |
| 44 | +from astroquery.utils.tap.xmlparser import utils |
| 45 | +from astroquery.vizier import Vizier |
| 46 | +from astroquery.utils.tap import TapPlus |
| 47 | + |
| 48 | + |
| 49 | +def data_path(filename): |
| 50 | + data_dir = os.path.join(os.path.dirname(__file__), 'data') |
| 51 | + return os.path.join(data_dir, filename) |
| 52 | + |
| 53 | + |
| 54 | +def create_temp_folder(): |
| 55 | + return tempfile.TemporaryDirectory() |
| 56 | + |
| 57 | + |
| 58 | +def get_license(): |
| 59 | + with open(data_path('test_license.txt'), "r") as file: |
| 60 | + user = file.readline() |
| 61 | + password = file.readline() |
| 62 | + return user, password |
| 63 | + |
| 64 | + |
| 65 | +@remote_data |
| 66 | +class TestEndToEnd(unittest.TestCase): |
| 67 | + |
| 68 | + temp_file_vot = '/temp.vot' |
| 69 | + coord = SkyCoord(ra=98.831495, |
| 70 | + dec=-66.81864444, |
| 71 | + unit=(u.degree, u.degree), |
| 72 | + frame='icrs') |
| 73 | + radius = u.Quantity(0.001, u.deg) |
| 74 | + default_columns = ['dist', 'observationid', 'calibrationlevel', 'public', |
| 75 | + 'dataproducttype', 'instrument_name', |
| 76 | + 'energy_bandpassname', 'target_name', 'target_ra', |
| 77 | + 'target_dec', 'position_bounds_center', |
| 78 | + 'position_bounds_spoly'] |
| 79 | + all_columns = ['dist', 'public', 'algorithm_name', 'calibrationlevel', |
| 80 | + 'collection', 'compositeuri', 'creatorid', |
| 81 | + 'dataproducttype', 'energy_bandpassname', |
| 82 | + 'energy_bounds', 'energy_bounds_lower', |
| 83 | + 'energy_bounds_upper', 'energy_bounds_width', |
| 84 | + 'energy_dimension', 'energy_emband', |
| 85 | + 'energy_freqsamplesize', 'energy_freqwidth', |
| 86 | + 'energy_resolvingpower', 'energy_restwav', |
| 87 | + 'energy_samplesize', 'energy_transition_species', |
| 88 | + 'energy_transition_transition', 'environment_ambienttemp', |
| 89 | + 'environment_elevation', 'environment_humidity', |
| 90 | + 'environment_photometric', 'environment_seeing', |
| 91 | + 'environment_tau', 'environment_wavelengthtau', |
| 92 | + 'instrument_keywords', 'instrument_name', 'intent', |
| 93 | + 'max_cal_level', 'members', 'metrics_background', |
| 94 | + 'metrics_backgroundstddev', 'metrics_fluxdensitylimit', |
| 95 | + 'metrics_maglimit', 'metrics_sourcenumberdensity', |
| 96 | + 'obs_accmetachecksum', 'obs_lastmodified', |
| 97 | + 'obs_maxlastmodified', 'obs_metachecksum', |
| 98 | + 'obs_metarelease', 'observationid', 'observationuri', |
| 99 | + 'obsid', 'plane_accmetachecksum', 'plane_datarelease', |
| 100 | + 'plane_lastmodified', 'plane_maxlastmodified', |
| 101 | + 'plane_metachecksum', 'plane_metarelease', 'planeid', |
| 102 | + 'planeuri', 'polarization_dimension', 'polarization_states', |
| 103 | + 'position_bounds_area', 'position_bounds_center', |
| 104 | + 'position_bounds_size', 'position_bounds_spoly', |
| 105 | + 'position_dimension_naxis1', 'position_dimension_naxis2', |
| 106 | + 'position_resolution', 'position_samplesize', |
| 107 | + 'position_timedependent', 'productid', 'proposal_id', |
| 108 | + 'proposal_keywords', 'proposal_pi', 'proposal_project', |
| 109 | + 'proposal_title', 'provenance_inputs', |
| 110 | + 'provenance_keywords', 'provenance_lastexecuted', |
| 111 | + 'provenance_name', 'provenance_producer', |
| 112 | + 'provenance_project', 'provenance_reference', |
| 113 | + 'provenance_runid', 'provenance_version', 'publisherid', |
| 114 | + 'quality_flag', 'requirements_flag', 'sequencenumber', |
| 115 | + 'target_dec', 'target_keywords', 'target_moving', |
| 116 | + 'target_name', 'target_ra', 'target_redshift', |
| 117 | + 'target_standard', 'target_type', 'targetposition_coordsys', |
| 118 | + 'targetposition_equinox', 'telescope_geolocationx', |
| 119 | + 'telescope_geolocationy', 'telescope_geolocationz', |
| 120 | + 'telescope_keywords', 'telescope_name', 'time_bounds', |
| 121 | + 'time_bounds_lower', 'time_bounds_upper', |
| 122 | + 'time_bounds_width', 'time_dimension', 'time_exposure', |
| 123 | + 'time_resolution', 'time_samplesize', 'type', 'typecode'] |
| 124 | + |
| 125 | + def test_TAP_connection(self): |
| 126 | + tap = TapPlus(url="http://jwstdummytap.com", |
| 127 | + data_context='data') |
| 128 | + jwst = JwstClass(tap_plus_handler=tap) |
| 129 | + with pytest.raises(requests.HTTPError) as err: |
| 130 | + jwst.cone_search_async(self.coord, self.radius) |
| 131 | + assert "404" in err.value.args[0] |
| 132 | + tap = TapPlus(url="http://jwstdummytap.com", |
| 133 | + data_context='data') |
| 134 | + jwst = JwstClass(tap_plus_handler=tap) |
| 135 | + jwst.cone_search_async(self.coord, self.radius) |
| 136 | + |
| 137 | + def test_cone_search_async_no_filters(self): |
| 138 | + jwst = JwstClass() |
| 139 | + j = jwst.cone_search_async(self.coord, self.radius) |
| 140 | + table = j.get_results() |
| 141 | + assert(table[0][0] == 5.88302107189034e-06) |
| 142 | + assert(table[0][1].decode("UTF-8") == "jw00617198001_02102_'\ |
| 143 | + '00001_nrcb4") |
| 144 | + for colnames in table.colnames: |
| 145 | + assert(colnames in self.default_columns) |
| 146 | + |
| 147 | + def test_cone_search_async_show_all_columns(self): |
| 148 | + jwst = JwstClass() |
| 149 | + j = jwst.cone_search_async(self.coord, |
| 150 | + self.radius, |
| 151 | + show_all_columns=True) |
| 152 | + table = j.get_results() |
| 153 | + for colnames in table.colnames: |
| 154 | + assert(colnames in self.all_columns) |
| 155 | + for result in table['observationid', 'calibrationlevel'][0:5]: |
| 156 | + assert "jw" in str(result[0]) |
| 157 | + assert result[1] <= 3 |
| 158 | + |
| 159 | + def test_cone_search_async_with_proposal_id(self): |
| 160 | + jwst = JwstClass() |
| 161 | + j = jwst.cone_search_async(self.coord, |
| 162 | + self.radius, |
| 163 | + proposal_id='00617', |
| 164 | + show_all_columns=True) |
| 165 | + table = j.get_results() |
| 166 | + for colnames in table.colnames: |
| 167 | + assert(colnames in self.all_columns) |
| 168 | + assert "dist" in table.colnames |
| 169 | + for result in table['observationid', 'proposal_id'][0:5]: |
| 170 | + assert "00617" in str(result[1]) |
| 171 | + |
| 172 | + def test_cone_search_async_with_proposal_cal_instr_and_output(self): |
| 173 | + jwst = JwstClass() |
| 174 | + temp_folder = create_temp_folder() |
| 175 | + output_file = temp_folder.name + '/test_csv.vot' |
| 176 | + j = jwst.cone_search_async(self.coord, self.radius, |
| 177 | + proposal_id='00617', |
| 178 | + cal_level=3, |
| 179 | + show_all_columns=True, |
| 180 | + prod_type='image', |
| 181 | + instrument_name='NIRCAM', |
| 182 | + output_file=output_file, |
| 183 | + dump_to_file=True) |
| 184 | + table = j.get_results() |
| 185 | + assert os.path.exists(output_file) |
| 186 | + table.sort(['observationid']) |
| 187 | + loaded_result = Table.read(output_file) |
| 188 | + loaded_result.sort(['observationid']) |
| 189 | + for i in range(0, 6): |
| 190 | + for j in range(0, len(table[i])): |
| 191 | + assert(str(table[i][j]) == str(loaded_result[i][j])) |
| 192 | + temp_folder.cleanup() |
| 193 | + |
| 194 | + def test_cone_search_async_with_options_proposal_and_obsid(self): |
| 195 | + jwst = JwstClass() |
| 196 | + j = jwst.cone_search_async(self.coord, self.radius, |
| 197 | + proposal_id='00617', |
| 198 | + observation_id='jw00617-o023_t001_nircam_' |
| 199 | + 'f090w-sub160', |
| 200 | + instrument_name='NIRCAM', |
| 201 | + show_all_columns=True) |
| 202 | + table = j.get_results() |
| 203 | + assert(len(table) > 0) |
| 204 | + observation_id_index = table.colnames.index('observationid') |
| 205 | + proposal_id_index = table.colnames.index('proposal_id') |
| 206 | + instrument_index = table.colnames.index('instrument_name') |
| 207 | + assert(table[0][proposal_id_index].decode('UTF-8') == '00617') |
| 208 | + assert(table[0][observation_id_index].decode('UTF-8') == 'jw00617-' |
| 209 | + 'o023_t001_nircam_f090w-sub160') |
| 210 | + assert(table[0][instrument_index].decode('UTF-8') == 'NIRCAM') |
| 211 | + |
| 212 | + def test_filtering_by_filter_cone_search_saving_votable(self): |
| 213 | + jwst = JwstClass() |
| 214 | + temp_folder = create_temp_folder() |
| 215 | + output_file = temp_folder.name + '/test.vot' |
| 216 | + j = jwst.cone_search(self.coord, self.radius, cal_level=3, |
| 217 | + filter_name='F090W', output_file=output_file, |
| 218 | + dump_to_file=True, output_format='votable', |
| 219 | + only_public=True) |
| 220 | + table = j.get_results() |
| 221 | + assert os.path.exists(output_file) |
| 222 | + assert(len(table) > 0) |
| 223 | + loaded_result = Table.read(output_file) |
| 224 | + loaded_result.sort(['observationid']) |
| 225 | + table.sort(['observationid']) |
| 226 | + for i in range(0, len(table)): |
| 227 | + for j in range(0, len(table[i])): |
| 228 | + assert(str(table[i][j]) == str(loaded_result[i][j])) |
| 229 | + temp_folder.cleanup() |
| 230 | + |
| 231 | + def test_query_region(self): |
| 232 | + jwst = JwstClass() |
| 233 | + width = u.Quantity(3, u.deg) |
| 234 | + height = u.Quantity(3, u.deg) |
| 235 | + coord = SkyCoord(ra=273.08916875, |
| 236 | + dec=65.604808, |
| 237 | + unit=(u.degree, u.degree), |
| 238 | + frame='icrs') |
| 239 | + r = jwst.query_region(coordinate=coord, |
| 240 | + width=width, |
| 241 | + height=height, |
| 242 | + show_all_columns=True, |
| 243 | + only_public=True) |
| 244 | + assert(len(r) > 0) |
| 245 | + index_of_observation_id = r.colnames.index['observationid'] |
| 246 | + assert(r[0][index_of_observation_id].decode('UTF-8') == 'jw00747011' |
| 247 | + '001_0210a_00002_nrca3') |
| 248 | + |
| 249 | + def test_product_list_with_observation_id(self): |
| 250 | + jwst = JwstClass() |
| 251 | + product_list = jwst.get_product_list('jw01054002001_xx102_00001_' |
| 252 | + 'miri', cal_level=-1) |
| 253 | + assert(len(product_list) == 0) |
| 254 | + product_list = jwst.get_product_list('jw00617-o023_t001_nircam_' |
| 255 | + 'f090w-sub160') |
| 256 | + uri_index = product_list.colnames.index('uri') |
| 257 | + cal_level_index = product_list.colnames.index('calibrationlevel') |
| 258 | + has_level_one = False |
| 259 | + has_level_two = False |
| 260 | + has_level_three = False |
| 261 | + assert(len(product_list) > 0) |
| 262 | + for product in product_list: |
| 263 | + assert('jw00617' in product[uri_index].decode('UTF-8')) |
| 264 | + has_level_one = has_level_one or product[cal_level_index] == 1 |
| 265 | + has_level_two = has_level_two or product[cal_level_index] == 2 |
| 266 | + has_level_three = has_level_three or product[cal_level_index] == 3 |
| 267 | + assert(has_level_one and has_level_two and has_level_three) |
| 268 | + |
| 269 | + def test_get_product(self): |
| 270 | + jwst = JwstClass() |
| 271 | + product1 = jwst.get_product(artifact_id=None, |
| 272 | + file_name='jw00626-o025_t007_nirspec_f1' |
| 273 | + '70lp-g235h-s1600a1-sub2048_x1dints.fits') |
| 274 | + assert os.path.exists(product1) |
| 275 | + product2 = jwst.get_product(artifact_id='00000000-0000-0000-9d16-' |
| 276 | + '5ee9bfba9d2c', file_name=None) |
| 277 | + assert os.path.exists(product2) |
| 278 | + assert(product1 == product2) |
| 279 | + |
| 280 | + def test_get_obs_product(self): |
| 281 | + jwst = JwstClass() |
| 282 | + # All levels |
| 283 | + products = jwst.get_obs_products(observation_id='jw00626-o025_t007_' |
| 284 | + 'nirspec_f170lp-g235h-s1600a1-' |
| 285 | + 'sub2048', cal_level='ALL') |
| 286 | + has_level_one = False |
| 287 | + has_level_two = False |
| 288 | + has_level_three = False |
| 289 | + for product in products: |
| 290 | + assert os.path.exists(product) |
| 291 | + has_level_one = has_level_one or 'level_1' in product |
| 292 | + has_level_two = has_level_two or 'level_2' in product |
| 293 | + has_level_three = has_level_three or 'level_3' in product |
| 294 | + assert(has_level_one and has_level_two and has_level_three) |
| 295 | + # Level 3 |
| 296 | + products = jwst.get_obs_products(observation_id='jw00626-o025_t007_ni' |
| 297 | + 'rspec_f170lp-g235h-s1600a1-sub2048', |
| 298 | + cal_level=3) |
| 299 | + has_level_three = False |
| 300 | + for product in products: |
| 301 | + assert os.path.exists(product) |
| 302 | + has_level_three = has_level_three or 'level_3' in product |
| 303 | + assert has_level_three |
| 304 | + # Level 2 |
| 305 | + products = jwst.get_obs_products(observation_id='jw00617-o023_t001_' |
| 306 | + 'nircam_f090w-sub160', cal_level=2) |
| 307 | + has_level_two = False |
| 308 | + for product in products: |
| 309 | + assert os.path.exists(product) |
| 310 | + has_level_two = has_level_two or 'level_2' in product |
| 311 | + assert has_level_two |
| 312 | + # Level 1 |
| 313 | + products = jwst.get_obs_products(observation_id='jw00617-o023_t001_' |
| 314 | + 'nircam_f090w-sub160', cal_level=1) |
| 315 | + has_level_one = False |
| 316 | + for product in products: |
| 317 | + assert os.path.exists(product) |
| 318 | + has_level_one = has_level_one or 'level_1' in product |
| 319 | + assert has_level_one |
| 320 | + # Level -1 |
| 321 | + with pytest.raises(ValueError) as err: |
| 322 | + jwst.get_obs_products(observation_id='jw80800056001_xx11d_' |
| 323 | + '00021_miri', cal_level=-1) |
| 324 | + assert "Cannot retrieve products" in err.value.args[0] |
| 325 | + |
| 326 | + def test_get_related_observations(self): |
| 327 | + jwst = JwstClass() |
| 328 | + product_list = jwst.get_related_observations('jw00617023001_02102_' |
| 329 | + '00001_nrcb4') |
| 330 | + assert('jw00617-o023_t001_nircam_f090w-sub160' in product_list) |
| 331 | + product_list = jwst.get_related_observations('jw00777011001_02104_' |
| 332 | + '00001_nrcblong') |
| 333 | + assert('jw00777-o011_t005_nircam_f277w-sub160' in product_list) |
| 334 | + assert('jw00777-c1005_t005_nircam_f277w-sub160' in product_list) |
| 335 | + |
| 336 | + def test_query_target_name(self): |
| 337 | + jwst = JwstClass() |
| 338 | + width = u.Quantity(3, u.deg) |
| 339 | + height = u.Quantity(3, u.deg) |
| 340 | + target_name = 'LMC' |
| 341 | + target_resolver = 'SIMBAD' |
| 342 | + tb5 = jwst.query_target(target_name, |
| 343 | + target_resolver, |
| 344 | + width, |
| 345 | + height, |
| 346 | + filter_name='F277W', |
| 347 | + cal_level=-1) |
| 348 | + |
| 349 | + tb5 = jwst.query_by_target_name(target_name, |
| 350 | + target_resolver, |
| 351 | + width, height, |
| 352 | + instrument_name='NIRCAM', |
| 353 | + observation_id='jw00322001003_02101_' |
| 354 | + '00001_nrca3', |
| 355 | + proposal_id='00322', |
| 356 | + show_all_columns=True) |
| 357 | + |
| 358 | + |
| 359 | +if __name__ == "__main__": |
| 360 | + # import sys;sys.argv = ['', 'Test.testName'] |
| 361 | + unittest.main() |
0 commit comments