Skip to content

Commit e274a2b

Browse files
authored
Merge pull request #2311 from esdc-esac-esa-int/tap_1.1_rename_compressed
Move functionality from gaia/core.py to utils/tap/core.py.
2 parents 44fc1a8 + 3db1dae commit e274a2b

File tree

3 files changed

+79
-76
lines changed

3 files changed

+79
-76
lines changed

astroquery/gaia/core.py

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -850,30 +850,9 @@ def launch_job(self, query, name=None, output_file=None,
850850
-------
851851
A Job object
852852
"""
853-
compressed_extension = ".gz"
854-
format_with_results_compressed = ['votable', 'fits']
855-
output_file_with_extension = output_file
856-
857-
if output_file is not None:
858-
if output_format in format_with_results_compressed:
859-
# In this case we will have to take also into account the .fits format
860-
if not output_file.endswith(compressed_extension):
861-
warnings.warn('By default, results in "votable" and "fits" format are returned in '
862-
f'compressed format therefore your file {output_file} '
863-
f'will be renamed to {output_file}.gz', InputWarning)
864-
if output_format == 'votable':
865-
if output_file.endswith('.vot'):
866-
output_file_with_extension = output_file + '.gz'
867-
else:
868-
output_file_with_extension = output_file + '.vot.gz'
869-
elif output_format == 'fits':
870-
if output_file.endswith('.fits'):
871-
output_file_with_extension = output_file + '.gz'
872-
else:
873-
output_file_with_extension = output_file + '.fits.gz'
874853

875854
return TapPlus.launch_job(self, query=query, name=name,
876-
output_file=output_file_with_extension,
855+
output_file=output_file,
877856
output_format=output_format,
878857
verbose=verbose,
879858
dump_to_file=dump_to_file,
@@ -899,7 +878,7 @@ def launch_job_async(self, query, name=None, output_file=None,
899878
output_format : str, optional, default 'votable'
900879
results format. Available formats are: 'votable', 'votable_plain',
901880
'fits', 'csv' and 'json', default is 'votable'.
902-
Returned results for 'votable' and 'fits' formats are compressed
881+
Returned results for 'votable' 'ecsv' and 'fits' format are compressed
903882
gzip files.
904883
verbose : bool, optional, default 'False'
905884
flag to display information about the process
@@ -921,41 +900,9 @@ def launch_job_async(self, query, name=None, output_file=None,
921900
-------
922901
A Job object
923902
"""
924-
compressed_extension = ".gz"
925-
format_with_results_compressed = ['votable', 'fits', 'ecsv']
926-
output_file_with_extension = output_file
927-
928-
if output_file is not None:
929-
if output_format in format_with_results_compressed:
930-
# In this case we will have to take also into account the .fits format
931-
if not output_file.endswith(compressed_extension):
932-
warnings.warn('WARNING!!! By default, results in "votable" and "fits" format are returned in '
933-
f'compressed format therefore your file {output_file} '
934-
f'will be renamed to {output_file}.gz')
935-
if output_format == 'votable':
936-
if output_file.endswith('.vot'):
937-
output_file_with_extension = output_file + '.gz'
938-
else:
939-
output_file_with_extension = output_file + '.vot.gz'
940-
elif output_format == 'fits':
941-
if output_file.endswith('.fits'):
942-
output_file_with_extension = output_file + '.gz'
943-
else:
944-
output_file_with_extension = output_file + '.fits.gz'
945-
elif output_format == 'ecsv':
946-
if output_file.endswith('.ecsv'):
947-
output_file_with_extension = output_file + '.gz'
948-
else:
949-
output_file_with_extension = output_file + '.ecsv.gz'
950-
# the output type is not compressed by default by the TAP SERVER but the users gives a .gz extension
951-
elif output_file.endswith(compressed_extension):
952-
output_file_renamed = output_file.removesuffix('.gz')
953-
warnings.warn(f'WARNING!!! The output format selected is not compatible with compression. {output_file}'
954-
f' will be renamed to {output_file}')
955-
956903
return TapPlus.launch_job_async(self, query=query,
957904
name=name,
958-
output_file=output_file_with_extension,
905+
output_file=output_file,
959906
output_format=output_format,
960907
verbose=verbose,
961908
dump_to_file=dump_to_file,

astroquery/utils/tap/core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ def launch_job(self, query, name=None, output_file=None,
265265
-------
266266
A Job object
267267
"""
268+
output_file_updated = taputils.get_suitable_output_file_name_for_current_output_format(output_file,
269+
output_format)
268270
query = taputils.set_top_in_query(query, 2000)
269271
if verbose:
270272
print(f"Launched query: '{query}'")
@@ -310,7 +312,7 @@ def launch_job(self, query, name=None, output_file=None,
310312
headers = response.getheaders()
311313
suitableOutputFile = taputils.get_suitable_output_file(self.__connHandler,
312314
False,
313-
output_file,
315+
output_file_updated,
314316
headers,
315317
isError,
316318
output_format)
@@ -385,6 +387,10 @@ def launch_job_async(self, query, name=None, output_file=None,
385387
-------
386388
A Job object
387389
"""
390+
391+
output_file_updated = taputils.get_suitable_output_file_name_for_current_output_format(output_file,
392+
output_format)
393+
388394
if verbose:
389395
print(f"Launched query: '{query}'")
390396
if upload_resource is not None:
@@ -414,7 +420,7 @@ def launch_job_async(self, query, name=None, output_file=None,
414420
headers = response.getheaders()
415421
suitableOutputFile = taputils.get_suitable_output_file(self.__connHandler,
416422
True,
417-
output_file,
423+
output_file_updated,
418424
headers,
419425
isError,
420426
output_format)

astroquery/utils/tap/taputils.py

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""
1717

1818
import re
19+
import warnings
1920
from datetime import datetime
2021

2122
TAP_UTILS_QUERY_TOP_PATTERN = re.compile(
@@ -101,7 +102,7 @@ def set_top_in_query(query, top):
101102
else:
102103
# no all nor distinct: add top after select
103104
p = q.replace("\n", " ").find("SELECT ")
104-
nq = f"{query[0:p+7]} TOP {top} {query[p+7:]}"
105+
nq = f"{query[0:p + 7]} TOP {top} {query[p + 7:]}"
105106
return nq
106107

107108

@@ -140,7 +141,7 @@ def parse_http_response_error(responseStr, status):
140141
pos2 = responseStr.find('</li>', pos1)
141142
if pos2 == -1:
142143
return parse_http_votable_response_error(responseStr, status)
143-
msg = responseStr[(pos1+len(TAP_UTILS_HTTP_ERROR_MSG_START)):pos2]
144+
msg = responseStr[(pos1 + len(TAP_UTILS_HTTP_ERROR_MSG_START)):pos2]
144145
return f"Error {status}:\n{msg}"
145146

146147

@@ -162,7 +163,7 @@ def parse_http_votable_response_error(responseStr, status):
162163
pos2 = responseStr.find(TAP_UTILS_VOTABLE_INFO, pos1)
163164
if pos2 == -1:
164165
return f"Error {status}:\n{responseStr}"
165-
msg = responseStr[(pos1+len(TAP_UTILS_HTTP_VOTABLE_ERROR)):pos2]
166+
msg = responseStr[(pos1 + len(TAP_UTILS_HTTP_VOTABLE_ERROR)):pos2]
166167
return f"Error {status}: {msg}"
167168

168169

@@ -178,7 +179,7 @@ def get_jobid_from_location(location):
178179
-------
179180
A jobid.
180181
"""
181-
pos = location.rfind('/')+1
182+
pos = location.rfind('/') + 1
182183
jobid = location[pos:]
183184
return jobid
184185

@@ -217,26 +218,75 @@ def get_table_name(full_qualified_table_name):
217218
pos = full_qualified_table_name.rfind('.')
218219
if pos == -1:
219220
return full_qualified_table_name
220-
name = full_qualified_table_name[pos+1:]
221+
name = full_qualified_table_name[pos + 1:]
221222
return name
222223

223224

224-
def get_suitable_output_file(conn_handler, async_job, outputFile, headers,
225-
isError, output_format):
226-
dateTime = datetime.now().strftime("%Y%m%d%H%M%S")
227-
fileName = ""
228-
if outputFile is None:
229-
fileName = conn_handler.get_file_from_header(headers)
230-
if fileName is None:
225+
def get_suitable_output_file(conn_handler, async_job, output_file, headers,
226+
is_error, output_format):
227+
date_time = datetime.now().strftime("%Y%m%d%H%M%S")
228+
if output_file is None:
229+
file_name = conn_handler.get_file_from_header(headers)
230+
if file_name is None:
231231
ext = conn_handler.get_suitable_extension(headers)
232232
if not async_job:
233-
fileName = f"sync_{dateTime}{ext}"
233+
file_name = f"sync_{date_time}{ext}"
234234
else:
235235
ext = conn_handler.get_suitable_extension_by_format(
236236
output_format)
237-
fileName = f"async_{dateTime}{ext}"
237+
file_name = f"async_{date_time}{ext}"
238238
else:
239-
fileName = outputFile
240-
if isError:
241-
fileName += ".error"
242-
return fileName
239+
file_name = output_file
240+
if is_error:
241+
file_name += ".error"
242+
return file_name
243+
244+
245+
def get_suitable_output_file_name_for_current_output_format(output_file, output_format):
246+
""" renames the name given for the output_file if the results for current_output format are returned compressed by default
247+
and the name selected by the user does not contain the correct extension.
248+
249+
output_file : str, optional, default None
250+
file name selected by the user
251+
output_format : str, optional, default 'votable'
252+
results format. Available formats in TAP are: 'votable', 'votable_plain',
253+
'fits', 'csv', 'ecsv' and 'json'. Default is 'votable'.
254+
Returned results for formats 'votable' 'ecsv' and 'fits' are compressed
255+
gzip files.
256+
257+
Returns
258+
-------
259+
A string with the new name for the file.
260+
"""
261+
compressed_extension = ".gz"
262+
format_with_results_compressed = ['votable', 'fits', 'ecsv']
263+
output_file_with_extension = output_file
264+
265+
if output_file is not None:
266+
if output_format in format_with_results_compressed:
267+
# In this case we will have to take also into account the .fits format
268+
if not output_file.endswith(compressed_extension):
269+
warnings.warn('By default, results in "votable", "ecsv" and "fits" format are returned in '
270+
f'compressed format therefore your file {output_file} '
271+
f'will be renamed to {output_file}.gz')
272+
if output_format == 'votable':
273+
if output_file.endswith('.vot'):
274+
output_file_with_extension = output_file + '.gz'
275+
else:
276+
output_file_with_extension = output_file + '.vot.gz'
277+
elif output_format == 'fits':
278+
if output_file.endswith('.fits'):
279+
output_file_with_extension = output_file + '.gz'
280+
else:
281+
output_file_with_extension = output_file + '.fits.gz'
282+
elif output_format == 'ecsv':
283+
if output_file.endswith('.ecsv'):
284+
output_file_with_extension = output_file + '.gz'
285+
else:
286+
output_file_with_extension = output_file + '.ecsv.gz'
287+
# the output type is not compressed by default by the TAP SERVER but the users gives a .gz extension
288+
elif output_file.endswith(compressed_extension):
289+
output_file_renamed = output_file.removesuffix('.gz')
290+
warnings.warn(f'The output format selected is not compatible with compression. {output_file}'
291+
f' will be renamed to {output_file_renamed}')
292+
return output_file_with_extension

0 commit comments

Comments
 (0)