|
| 1 | +# Licensed under a 3-clause BSD style license - see LICENSE.rst |
| 2 | +""" |
| 3 | +
|
| 4 | +@author: Javier Espinosa |
| 5 | + |
| 6 | +
|
| 7 | +European Space Astronomy Centre (ESAC) |
| 8 | +European Space Agency (ESA) |
| 9 | +
|
| 10 | +Created on 13 Jan. 2021 |
| 11 | +
|
| 12 | +
|
| 13 | +""" |
| 14 | +import tempfile |
| 15 | + |
| 16 | +import unittest |
| 17 | +import os |
| 18 | + |
| 19 | +from astropy.tests.helper import remote_data |
| 20 | +from requests.models import Response |
| 21 | +from astroquery.esa.hubble import ESAHubble |
| 22 | +from astroquery.utils.testing_tools import MockResponse |
| 23 | +from astropy import coordinates |
| 24 | +from unittest.mock import MagicMock |
| 25 | +from astropy.table.table import Table |
| 26 | +import shutil |
| 27 | +import random |
| 28 | +from PIL import Image |
| 29 | + |
| 30 | + |
| 31 | +esa_hubble = ESAHubble() |
| 32 | + |
| 33 | + |
| 34 | +def data_path(filename): |
| 35 | + data_dir = os.path.join(os.path.dirname(__file__), 'data') |
| 36 | + return os.path.join(data_dir, filename) |
| 37 | + |
| 38 | + |
| 39 | +def create_temp_folder(): |
| 40 | + return tempfile.TemporaryDirectory() |
| 41 | + |
| 42 | + |
| 43 | +def remove_last_job(): |
| 44 | + jobs = esa_hubble._tap.list_async_jobs() |
| 45 | + if len(jobs) > 0: |
| 46 | + esa_hubble._tap.remove_jobs(jobs[-1].jobid) |
| 47 | + |
| 48 | + |
| 49 | +@remote_data |
| 50 | +class TestRemoteData(unittest.TestCase): |
| 51 | + |
| 52 | + temp_file_vot = "/temp.vot" |
| 53 | + |
| 54 | + obs_query = "select top 2050 o.observation_id from ehst.observation o" |
| 55 | + |
| 56 | + top_obs_query = "select top 100 o.observation_id from ehst.observation o" |
| 57 | + |
| 58 | + hst_query = "select top 50 o.observation_id from ehst.observation "\ |
| 59 | + "o where o.collection='HST'" |
| 60 | + |
| 61 | + top_artifact_query = "select top 50 a.artifact_id, a.observation_id "\ |
| 62 | + " from ehst.artifact a" |
| 63 | + |
| 64 | + def test_query_hst_tap(self): |
| 65 | + result = esa_hubble.query_hst_tap(self.obs_query) |
| 66 | + # assert len(result) <= 2000 |
| 67 | + assert "observation_id" in result.keys() |
| 68 | + temp_folder = create_temp_folder() |
| 69 | + temp_file = temp_folder.name + self.temp_file_vot |
| 70 | + result = esa_hubble.query_hst_tap(query=self.obs_query, |
| 71 | + output_file=temp_file, |
| 72 | + output_format="votable") |
| 73 | + assert os.path.exists(temp_file) |
| 74 | + remove_last_job() |
| 75 | + |
| 76 | + def test_query_hst_tap_async(self): |
| 77 | + result = esa_hubble.query_hst_tap(self.obs_query, async_job=True) |
| 78 | + assert len(result) > 2000 |
| 79 | + remove_last_job() |
| 80 | + result = esa_hubble.query_hst_tap(self.top_obs_query, async_job=True) |
| 81 | + assert len(result) > 10 |
| 82 | + assert "observation_id" in result.keys() |
| 83 | + remove_last_job() |
| 84 | + |
| 85 | + def test_download_product(self): |
| 86 | + result = esa_hubble.query_hst_tap(self.hst_query) |
| 87 | + observation_id = random.choice(result['observation_id']) |
| 88 | + temp_folder = create_temp_folder() |
| 89 | + temp_file = temp_folder.name + "/" + observation_id + ".tar" |
| 90 | + esa_hubble.download_product(observation_id=observation_id, |
| 91 | + filename=temp_file) |
| 92 | + assert os.path.exists(temp_file) |
| 93 | + |
| 94 | + def test_get_artifact(self): |
| 95 | + result = esa_hubble.query_hst_tap(self.top_artifact_query) |
| 96 | + assert "artifact_id" in result.keys() |
| 97 | + artifact_id = random.choice(result["artifact_id"]) |
| 98 | + temp_folder = create_temp_folder() |
| 99 | + temp_file = temp_folder.name + "/" + artifact_id + ".gz" |
| 100 | + esa_hubble.get_artifact(artifact_id, temp_file) |
| 101 | + assert os.path.exists(temp_file) |
| 102 | + |
| 103 | + def test_get_postcard(self): |
| 104 | + result = esa_hubble.query_hst_tap(self.top_artifact_query + |
| 105 | + " where a.archive_class = " |
| 106 | + "'preview'") |
| 107 | + assert "observation_id" in result.keys() |
| 108 | + observation_id = random.choice(result["observation_id"]) |
| 109 | + temp_folder = create_temp_folder() |
| 110 | + temp_file = temp_folder.name + "/" + observation_id + ".jpg" |
| 111 | + esa_hubble.get_postcard(observation_id, "RAW", 256, temp_file) |
| 112 | + assert os.path.exists(temp_file) |
| 113 | + img = Image.open(temp_file) |
| 114 | + assert img.format == "JPEG" |
| 115 | + |
| 116 | + def test_query_target(self): |
| 117 | + temp_folder = create_temp_folder() |
| 118 | + temp_file = temp_folder.name + "/" + "m31_query.xml" |
| 119 | + table = esa_hubble.query_target("m31", temp_file) |
| 120 | + assert os.path.exists(temp_file) |
| 121 | + assert 'OBSERVATION_ID' in table.columns |
| 122 | + assert len(table) > 0 |
| 123 | + |
| 124 | + def test_query_criteria(self): |
| 125 | + temp_folder = create_temp_folder() |
| 126 | + temp_file = temp_folder.name + "/output1.vot.gz" |
| 127 | + table = esa_hubble.query_criteria(calibration_level=1, |
| 128 | + data_product_type="image", |
| 129 | + intent="SCIENCE", |
| 130 | + async_job=False, |
| 131 | + output_file=temp_file, |
| 132 | + output_format="votable", |
| 133 | + verbose=True, |
| 134 | + get_query=False) |
| 135 | + assert os.path.exists(temp_file) |
| 136 | + assert 'observation_id' in table.columns |
| 137 | + assert len(table) > 0 |
| 138 | + remove_last_job() |
| 139 | + |
| 140 | + def test_cone_search(self): |
| 141 | + esa_hubble = ESAHubble() |
| 142 | + c = coordinates.SkyCoord("00h42m44.51s +41d16m08.45s", frame='icrs') |
| 143 | + temp_folder = create_temp_folder() |
| 144 | + temp_file = temp_folder.name + "/cone_search_m31_5.vot" |
| 145 | + table = esa_hubble.cone_search(c, 7, temp_file) |
| 146 | + assert os.path.exists(temp_file) |
| 147 | + assert 'observation_id' in table.columns |
| 148 | + assert len(table) > 0 |
| 149 | + remove_last_job() |
| 150 | + |
| 151 | + def test_cone_search_criteria(self): |
| 152 | + esa_hubble = ESAHubble() |
| 153 | + c = coordinates.SkyCoord("00h42m44.51s +41d16m08.45s", frame='icrs') |
| 154 | + temp_folder = create_temp_folder() |
| 155 | + temp_file = temp_folder.name + "/output1.vot.gz" |
| 156 | + table = esa_hubble.cone_search_criteria(target='m31', radius=7, |
| 157 | + obs_collection=['HST'], |
| 158 | + data_product_type='image', |
| 159 | + async_job=False, |
| 160 | + filename=temp_file, |
| 161 | + output_format="votable") |
| 162 | + assert os.path.exists(temp_file) |
| 163 | + assert 'observation_id' in table.columns |
| 164 | + assert len(table) > 0 |
| 165 | + remove_last_job() |
0 commit comments