Skip to content

Commit 83e6c20

Browse files
jespinosaarbsipocz
authored andcommitted
JWSTPCR-166: get_related_obs for L3, fixes in get_products
1 parent 1f40c98 commit 83e6c20

File tree

3 files changed

+124
-55
lines changed

3 files changed

+124
-55
lines changed

astroquery/jwst/core.py

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class JwstClass(object):
6666
'preview']
6767
INSTRUMENT_NAMES = ['NIRISS', 'NIRSPEC', 'NIRCAM', 'MIRI', 'FGS']
6868
TARGET_RESOLVERS = ['ALL', 'SIMBAD', 'NED', 'VIZIER']
69-
CAL_LEVELS = ['ALL', 1, 2, 3]
69+
CAL_LEVELS = ['ALL', 1, 2, 3, -1]
7070
REQUESTED_OBSERVATION_ID = "Missing required argument: 'observation_id'"
7171

7272
def __init__(self, tap_plus_handler=None, data_handler=None):
@@ -1002,17 +1002,17 @@ def get_product_list(self, observation_id=None,
10021002
----------
10031003
observation_id : str, mandatory
10041004
Observation identifier.
1005-
cal_level : str, optional
1006-
Calibration level. Default value ia 'ALL', to download all the
1007-
products associated to this observation_id and lower levels.
1008-
Requesting more accurate levels than the one associated to the
1009-
observation_id is not allowed (as level 3 observations are
1005+
cal_level : str or int, optional
1006+
Calibration level. Default value is 'ALL', to download all the
1007+
products associated to this observation_id and lower processing
1008+
levels. Requesting more accurate levels than the one associated
1009+
to the observation_id is not allowed (as level 3 observations are
10101010
composite products based on level 2 products). To request upper
10111011
levels, please use get_related_observations functions first.
1012-
Possible values: 'ALL', '3', '2', '1'
1012+
Possible values: 'ALL', 3, 2, 1, -1
10131013
product_type : str, optional, default None
1014-
List only products of the given type. If None, all products are \
1015-
listed. Possible values: 'thumbnail', 'preview', 'info', \
1014+
List only products of the given type. If None, all products are
1015+
listed. Possible values: 'thumbnail', 'preview', 'info',
10161016
'auxiliary', 'science'.
10171017
10181018
Returns
@@ -1029,9 +1029,10 @@ def get_product_list(self, observation_id=None,
10291029
list = self._get_associated_planes(plane_ids, cal_level,
10301030
max_cal_level, False)
10311031

1032-
query = "select distinct a.uri, a.filename, a.contenttype, "\
1033-
"a.producttype, p.calibrationlevel, p.public FROM {0} p JOIN {1} "\
1034-
"a ON (p.planeid=a.planeid) WHERE a.planeid IN {2}{3};"\
1032+
query = "select distinct a.uri, a.artifactid, a.filename, "\
1033+
"a.contenttype, a.producttype, p.calibrationlevel, "\
1034+
"p.public FROM {0} p JOIN {1} a ON (p.planeid=a.planeid) "\
1035+
"WHERE a.planeid IN {2}{3};"\
10351036
.format(self.JWST_PLANE_TABLE, self.JWST_ARTIFACT_TABLE, list,
10361037
self.__get_artifact_producttype_condition(product_type))
10371038
job = self.__jwsttap.launch_job(query=query)
@@ -1045,7 +1046,7 @@ def _get_associated_planes(self, plane_ids, cal_level,
10451046
max_cal_level, is_url):
10461047
if (cal_level == max_cal_level):
10471048
if (not is_url):
1048-
list = "('{}')".format(plane_ids)
1049+
list = "('{}')".format("', '".join(plane_ids))
10491050
else:
10501051
list = "{}".format(",".join(plane_ids))
10511052
return list
@@ -1132,8 +1133,10 @@ def __get_member_planes(self, planeid, cal_level='ALL'):
11321133
raise ValueError(e)
11331134

11341135
def get_related_observations(self, observation_id):
1135-
"""Get the list of level 3 products that make use of a given JWST
1136-
observation_id.
1136+
"""In case of processing levels < 3, get the list of level 3
1137+
products that make use of a given JWST observation_id. In case of
1138+
processing level 3, retrieves the list of products used to create
1139+
this composite observation
11371140
11381141
Parameters
11391142
----------
@@ -1154,7 +1157,11 @@ def get_related_observations(self, observation_id):
11541157
if any(job.get_results()["observationid"]):
11551158
oids = job.get_results()["observationid"].pformat(show_name=False)
11561159
else:
1157-
oids = [observation_id]
1160+
query_members = "select m.members from {} m where m.observationid"\
1161+
"='{}'".format(self.JWST_MAIN_TABLE, observation_id)
1162+
job = self.__jwsttap.launch_job(query=query_members)
1163+
oids = job.get_results()["members"][0].decode("utf-8").\
1164+
replace("caom:JWST/", "").split(" ")
11581165
return oids
11591166

11601167
def get_product(self, artifact_id=None, file_name=None):
@@ -1176,22 +1183,29 @@ def get_product(self, artifact_id=None, file_name=None):
11761183
params_dict['RETRIEVAL_TYPE'] = 'PRODUCT'
11771184
params_dict['DATA_RETRIEVAL_ORIGIN'] = 'ASTROQUERY'
11781185

1179-
if artifact_id is None and file_name is None:
1180-
raise ValueError("Missing required argument: "
1181-
"'artifact_id' or 'file_name'")
1182-
else:
1183-
if file_name is None:
1184-
output_file_name = str(artifact_id)
1186+
self.__check_product_input(artifact_id, file_name)
1187+
1188+
if file_name is None:
1189+
try:
1190+
output_file_name = self._query_get_product(artifact_id)
11851191
err_msg = str(artifact_id)
1186-
else:
1187-
output_file_name = str(file_name)
1188-
err_msg = str(file_name)
1192+
except Exception as exx:
1193+
raise ValueError('Cannot retrieve product for artifact_id ' +
1194+
artifact_id + ': %s' % str(exx))
1195+
else:
1196+
output_file_name = str(file_name)
1197+
err_msg = str(file_name)
1198+
1199+
if artifact_id is not None:
1200+
params_dict['ARTIFACTID'] = str(artifact_id)
1201+
else:
1202+
try:
1203+
params_dict['ARTIFACTID'] = (self._query_get_product(
1204+
file_name=file_name))
1205+
except Exception as exx:
1206+
raise ValueError('Cannot retrieve product for file_name ' +
1207+
file_name + ': %s' % str(exx))
11891208

1190-
if artifact_id is not None:
1191-
params_dict['ARTIFACTID'] = str(artifact_id)
1192-
else:
1193-
params_dict['ARTIFACT_URI'] = 'mast:JWST/product/' +\
1194-
str(file_name)
11951209
try:
11961210
self.__jwsttap.load_data(params_dict=params_dict,
11971211
output_file=output_file_name)
@@ -1202,6 +1216,23 @@ def get_product(self, artifact_id=None, file_name=None):
12021216
print("Product saved at: %s" % (output_file_name))
12031217
return output_file_name
12041218

1219+
def _query_get_product(self, artifact_id=None, file_name=None):
1220+
if(file_name):
1221+
query_artifactid = "select * from {} a where a.filename = "\
1222+
"'{}'".format(self.JWST_ARTIFACT_TABLE, file_name)
1223+
job = self.__jwsttap.launch_job(query=query_artifactid)
1224+
return job.get_results()['artifactid'][0].decode("utf-8")
1225+
else:
1226+
query_filename = "select * from {} a where a.artifactid = "\
1227+
"'{}'".format(self.JWST_ARTIFACT_TABLE, artifact_id)
1228+
job = self.__jwsttap.launch_job(query=query_filename)
1229+
return job.get_results()['filename'][0].decode("utf-8")
1230+
1231+
def __check_product_input(self, artifact_id, file_name):
1232+
if artifact_id is None and file_name is None:
1233+
raise ValueError("Missing required argument: "
1234+
"'artifact_id' or 'file_name'")
1235+
12051236
def get_obs_products(self, observation_id=None, cal_level="ALL",
12061237
product_type=None, output_file=None):
12071238
"""Get a JWST product given its observation ID.
@@ -1210,14 +1241,14 @@ def get_obs_products(self, observation_id=None, cal_level="ALL",
12101241
----------
12111242
observation_id : str, mandatory
12121243
Observation identifier.
1213-
cal_level : str, optional
1244+
cal_level : str or int, optional
12141245
Calibration level. Default value ia 'ALL', to download all the
12151246
products associated to this observation_id and lower levels.
12161247
Requesting more accurate levels than the one associated to the
12171248
observation_id is not allowed (as level 3 observations are
12181249
composite products based on level 2 products). To request upper
12191250
levels, please use get_related_observations functions first.
1220-
Possible values: 'ALL', '3', '2', '1'
1251+
Possible values: 'ALL', 3, 2, 1, -1
12211252
product_type : str, optional, default None
12221253
List only products of the given type. If None, all products are \
12231254
listed. Possible values: 'thumbnail', 'preview', 'auxiliary', \

astroquery/jwst/tests/test_jwstdata.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ def data_path(filename):
2929
return os.path.join(data_dir, filename)
3030

3131

32+
def get_product_mock(params, *args, **kwargs):
33+
if(args[0] == 'file_name_id'):
34+
return "00000000-0000-0000-8740-65e2827c9895"
35+
else:
36+
return "jw00617023001_02102_00001_nrcb4_uncal.fits"
37+
38+
39+
@pytest.fixture(autouse=True)
40+
def get_product_request(request):
41+
try:
42+
mp = request.getfixturevalue("monkeypatch")
43+
except AttributeError: # pytest < 3
44+
mp = request.getfuncargvalue("monkeypatch")
45+
mp.setattr(JwstClass, '_query_get_product', get_product_mock)
46+
return mp
47+
48+
3249
class TestData(unittest.TestCase):
3350

3451
def test_get_product(self):
@@ -47,11 +64,12 @@ def test_get_product(self):
4764
params_dict = {}
4865
params_dict['RETRIEVAL_TYPE'] = 'PRODUCT'
4966
params_dict['DATA_RETRIEVAL_ORIGIN'] = 'ASTROQUERY'
50-
params_dict['ARTIFACTID'] = 'my_artifact_id'
67+
params_dict['ARTIFACTID'] = '00000000-0000-0000-8740-65e2827c9895'
5168
parameters['params_dict'] = params_dict
52-
parameters['output_file'] = 'my_artifact_id'
69+
parameters['output_file'] = 'jw00617023001_02102_00001_nrcb4_'\
70+
'uncal.fits'
5371
parameters['verbose'] = False
54-
jwst.get_product(artifact_id='my_artifact_id')
72+
jwst.get_product(artifact_id='00000000-0000-0000-8740-65e2827c9895')
5573
dummyTapHandler.check_call('load_data', parameters)
5674

5775

astroquery/jwst/tests/test_jwsttap.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ def get_associated_planes_mock(url, params, *args, **kwargs):
6767
if(args[0] == 2):
6868
return "('00000000-0000-0000-879d-ae91fa2f43e2')"
6969
else:
70-
return "('00000000-0000-0000-879d-ae91fa2f43e2', "\
71-
"'00000000-0000-0000-9852-a9fa8c63f7ef')"
70+
return planeids
7271

7372

7473
@pytest.fixture(autouse=True)
@@ -81,6 +80,27 @@ def associated_planes_request(request):
8180
return mp
8281

8382

83+
def get_product_mock(params, *args, **kwargs):
84+
if(args[0] == 'file_name_id'):
85+
return "00000000-0000-0000-8740-65e2827c9895"
86+
else:
87+
return "jw00617023001_02102_00001_nrcb4_uncal.fits"
88+
89+
90+
@pytest.fixture(autouse=True)
91+
def get_product_request(request):
92+
try:
93+
mp = request.getfixturevalue("monkeypatch")
94+
except AttributeError: # pytest < 3
95+
mp = request.getfuncargvalue("monkeypatch")
96+
mp.setattr(JwstClass, '_query_get_product', get_product_mock)
97+
return mp
98+
99+
100+
planeids = "('00000000-0000-0000-879d-ae91fa2f43e2', "\
101+
"'00000000-0000-0000-9852-a9fa8c63f7ef')"
102+
103+
84104
class TestTap(unittest.TestCase):
85105

86106
def test_load_tables(self):
@@ -632,16 +652,17 @@ def test_get_product_by_artifactid(self):
632652
dummyTapHandler.reset()
633653

634654
parameters = {}
635-
parameters['output_file'] = 'my_artifact_id'
655+
parameters['output_file'] = 'jw00617023001_02102_00001_nrcb4_'\
656+
'uncal.fits'
636657
parameters['verbose'] = False
637658

638659
param_dict = {}
639660
param_dict['RETRIEVAL_TYPE'] = 'PRODUCT'
640661
param_dict['DATA_RETRIEVAL_ORIGIN'] = 'ASTROQUERY'
641-
param_dict['ARTIFACTID'] = 'my_artifact_id'
662+
param_dict['ARTIFACTID'] = '00000000-0000-0000-8740-65e2827c9895'
642663
parameters['params_dict'] = param_dict
643664

644-
jwst.get_product(artifact_id='my_artifact_id')
665+
jwst.get_product(artifact_id='00000000-0000-0000-8740-65e2827c9895')
645666
dummyTapHandler.check_call('load_data', parameters)
646667

647668
def test_get_product_by_filename(self):
@@ -663,7 +684,7 @@ def test_get_product_by_filename(self):
663684
param_dict = {}
664685
param_dict['RETRIEVAL_TYPE'] = 'PRODUCT'
665686
param_dict['DATA_RETRIEVAL_ORIGIN'] = 'ASTROQUERY'
666-
param_dict['ARTIFACT_URI'] = 'mast:JWST/product/file_name_id'
687+
param_dict['ARTIFACTID'] = '00000000-0000-0000-8740-65e2827c9895'
667688
parameters['params_dict'] = param_dict
668689

669690
jwst.get_product(file_name='file_name_id')
@@ -685,12 +706,11 @@ def test_get_products_list(self):
685706
cal_level_condition = " AND m.calibrationlevel = m.max_cal_level"
686707
prodtype_condition = ""
687708

688-
query = "select distinct a.uri, a.filename, a.contenttype, "\
689-
"a.producttype, p.calibrationlevel, p.public FROM {0} p JOIN {1} "\
690-
"a ON (p.planeid=a.planeid) WHERE a.planeid IN {2};"\
691-
.format(jwst.JWST_PLANE_TABLE, jwst.JWST_ARTIFACT_TABLE,
692-
"('00000000-0000-0000-879d-ae91fa2f43e2', "
693-
"'00000000-0000-0000-9852-a9fa8c63f7ef')")
709+
query = "select distinct a.uri, a.artifactid, a.filename, "\
710+
"a.contenttype, a.producttype, p.calibrationlevel, p.public "\
711+
"FROM {0} p JOIN {1} a ON (p.planeid=a.planeid) WHERE a.planeid "\
712+
"IN {2};".format(jwst.JWST_PLANE_TABLE, jwst.JWST_ARTIFACT_TABLE,
713+
planeids)
694714

695715
parameters = {}
696716
parameters['query'] = query
@@ -709,12 +729,13 @@ def test_get_products_list(self):
709729
cal_level = 2
710730
product_type = "science"
711731

712-
query = "select distinct a.uri, a.filename, a.contenttype, "\
713-
"a.producttype, p.calibrationlevel, p.public FROM {}"\
714-
" p JOIN {} a ON (p.planeid=a.planeid) WHERE a.planeid IN "\
715-
"('00000000-0000-0000-879d-ae91fa2f43e2') AND producttype "\
716-
"LIKE 'science';".format(jwst.JWST_PLANE_TABLE,
717-
jwst.JWST_ARTIFACT_TABLE)
732+
query = "select distinct a.uri, a.artifactid, a.filename, "\
733+
"a.contenttype, a.producttype, p.calibrationlevel, "\
734+
"p.public FROM {} p JOIN {} a ON (p.planeid=a.planeid) "\
735+
"WHERE a.planeid IN ('00000000-0000-0000-879d-ae91fa2f43e2') "\
736+
"AND producttype LIKE "\
737+
"'science';".format(jwst.JWST_PLANE_TABLE,
738+
jwst.JWST_ARTIFACT_TABLE)
718739

719740
parameters = {}
720741
parameters['query'] = query
@@ -760,8 +781,7 @@ def test_get_obs_products(self):
760781
param_dict = {}
761782
param_dict['RETRIEVAL_TYPE'] = 'OBSERVATION'
762783
param_dict['DATA_RETRIEVAL_ORIGIN'] = 'ASTROQUERY'
763-
param_dict['planeid'] = "('00000000-0000-0000-879d-ae91fa2f43e2', "\
764-
"'00000000-0000-0000-9852-a9fa8c63f7ef')"
784+
param_dict['planeid'] = planeids
765785
param_dict['calibrationlevel'] = 'ALL'
766786
parameters['params_dict'] = param_dict
767787

0 commit comments

Comments
 (0)