11
11
European Space Agency (ESA)
12
12
13
13
Created on 30 jun. 2016
14
-
15
-
14
+ Modified on 1 jun. 2021 by mhsarmiento
16
15
"""
17
16
from astroquery .utils .tap import taputils
18
17
from astroquery .utils .tap .conn .tapconn import TapConn
32
31
from astropy .table .table import Table
33
32
import tempfile
34
33
35
-
36
34
__all__ = ['Tap' , 'TapPlus' ]
37
35
38
36
VERSION = "20200428.1"
@@ -246,6 +244,8 @@ def launch_job(self, query, name=None, output_file=None,
246
244
----------
247
245
query : str, mandatory
248
246
query to be executed
247
+ name : str, optional, default None
248
+ custom name defined by the user for the job that is going to be created
249
249
output_file : str, optional, default None
250
250
file name where the results are saved if dumpToFile is True.
251
251
If this parameter is not provided, the jobid is used instead
@@ -358,6 +358,8 @@ def launch_job_async(self, query, name=None, output_file=None,
358
358
----------
359
359
query : str, mandatory
360
360
query to be executed
361
+ name : str, optional, default None
362
+ custom name defined by the user for the job that is going to be created
361
363
output_file : str, optional, default None
362
364
file name where the results are saved if dumpToFile is True.
363
365
If this parameter is not provided, the jobid is used instead
@@ -571,7 +573,7 @@ def __launchJobMultipart(self, query, uploadResource, uploadTableName,
571
573
"FORMAT" : str (outputFormat ),
572
574
"tapclient" : str (TAP_CLIENT_ID ),
573
575
"QUERY" : str (query ),
574
- "UPLOAD" : "" + str (uploadValue )}
576
+ "UPLOAD" : "" + str (uploadValue )}
575
577
if autorun is True :
576
578
args ['PHASE' ] = 'RUN'
577
579
if name is not None :
@@ -657,7 +659,7 @@ def __parseUrl(self, url, verbose=False):
657
659
if urlInfoPos < 0 :
658
660
raise ValueError ("Invalid URL format" )
659
661
660
- urlInfo = url [(urlInfoPos + 3 ):]
662
+ urlInfo = url [(urlInfoPos + 3 ):]
661
663
662
664
items = urlInfo .split ("/" )
663
665
@@ -672,7 +674,7 @@ def __parseUrl(self, url, verbose=False):
672
674
if portPos > 0 :
673
675
# port found
674
676
host = hostPort [0 :portPos ]
675
- port = int (hostPort [portPos + 1 :])
677
+ port = int (hostPort [portPos + 1 :])
676
678
else :
677
679
# no port found
678
680
host = hostPort
@@ -693,10 +695,10 @@ def __parseUrl(self, url, verbose=False):
693
695
tapContext = f"/{ items [2 ]} "
694
696
else :
695
697
data = []
696
- for i in range (1 , itemsSize - 1 ):
698
+ for i in range (1 , itemsSize - 1 ):
697
699
data .append (f"/{ items [i ]} " )
698
700
serverContext = utils .util_create_string_from_buffer (data )
699
- tapContext = f"/{ items [itemsSize - 1 ]} "
701
+ tapContext = f"/{ items [itemsSize - 1 ]} "
700
702
if verbose :
701
703
print (f"protocol: '{ protocol } '" )
702
704
print (f"host: '{ host } '" )
@@ -713,6 +715,7 @@ class TapPlus(Tap):
713
715
"""TAP plus class
714
716
Provides TAP and TAP+ capabilities
715
717
"""
718
+
716
719
def __init__ (self , url = None ,
717
720
host = None ,
718
721
server_context = None ,
@@ -1245,7 +1248,7 @@ def get_datalinks(self, ids, verbose=False):
1245
1248
print ("Retrieving datalink." )
1246
1249
if ids is None :
1247
1250
raise ValueError ("Missing mandatory argument 'ids'" )
1248
- if isinstance (ids , str ):
1251
+ if isinstance (ids , str ):
1249
1252
ids_arg = f"ID={ ids } "
1250
1253
else :
1251
1254
if isinstance (ids , int ):
@@ -1368,7 +1371,7 @@ def login(self, user=None, password=None, credentials_file=None,
1368
1371
user = ins .readline ().strip ()
1369
1372
password = ins .readline ().strip ()
1370
1373
if user is None :
1371
- user = input ("User: " )
1374
+ user = input ("User: " )
1372
1375
if user is None :
1373
1376
print ("Invalid user name" )
1374
1377
return
@@ -1532,7 +1535,7 @@ def __uploadTableMultipart(self, resource, table_name=None,
1532
1535
chunk = f .read ()
1533
1536
files = [['FILE' , os .path .basename (resource ), chunk ]]
1534
1537
contentType , body = connHandler .encode_multipart (args , files )
1535
- else : # upload from URL
1538
+ else : # upload from URL
1536
1539
args = {
1537
1540
"TASKID" : str (- 1 ),
1538
1541
"TABLE_NAME" : str (table_name ),
@@ -1628,14 +1631,14 @@ def delete_user_table(self, table_name=None, force_removal=False,
1628
1631
raise ValueError ("Table name cannot be null" )
1629
1632
if force_removal is True :
1630
1633
args = {
1631
- "TABLE_NAME" : str (table_name ),
1632
- "DELETE" : "TRUE" ,
1633
- "FORCE_REMOVAL" : "TRUE" }
1634
+ "TABLE_NAME" : str (table_name ),
1635
+ "DELETE" : "TRUE" ,
1636
+ "FORCE_REMOVAL" : "TRUE" }
1634
1637
else :
1635
1638
args = {
1636
- "TABLE_NAME" : str (table_name ),
1637
- "DELETE" : "TRUE" ,
1638
- "FORCE_REMOVAL" : "FALSE" }
1639
+ "TABLE_NAME" : str (table_name ),
1640
+ "DELETE" : "TRUE" ,
1641
+ "FORCE_REMOVAL" : "FALSE" }
1639
1642
connHandler = self .__getconnhandler ()
1640
1643
data = connHandler .url_encode (args )
1641
1644
response = connHandler .execute_upload (data , verbose = verbose )
@@ -1648,15 +1651,90 @@ def delete_user_table(self, table_name=None, force_removal=False,
1648
1651
msg = f"Table '{ table_name } ' deleted."
1649
1652
print (msg )
1650
1653
1654
+ def rename_table (self , table_name = None , new_table_name = None , new_column_names_dict = None ,
1655
+ verbose = False ):
1656
+ """ This method allows you to update the column names of a user table.
1657
+
1658
+ Parameters
1659
+ ----------
1660
+ table_name: str, required
1661
+ old name of the user's table
1662
+ new_table_name: str, required
1663
+ new name of the user's table
1664
+ new_column_names_dict: dict str:str
1665
+ dict with pairs "old_column1_name:new_column1_name"
1666
+ verbose : bool, optional, default 'False'
1667
+ flag to display information about the process
1668
+
1669
+ """
1670
+ if new_column_names_dict is None :
1671
+ new_column_names_dict = {}
1672
+ args = {}
1673
+
1674
+ if table_name is None :
1675
+ raise ValueError (
1676
+ "Argument 'table_name' is mandatory. "
1677
+ "Please introduce the name of the table that is going to be updated" )
1678
+ if (new_table_name is None ) and (new_column_names_dict is None ):
1679
+ raise ValueError ("Please introduce as minimum a new name for the table or a new name for a column with "
1680
+ "format old_column1_name:new_column1_name, ... ,old_columnN_name:new_columnN_name" )
1681
+ if new_table_name is not None :
1682
+ if new_column_names_dict is None :
1683
+ # case 1: We only need to rename the table
1684
+ args = self .get_args_4_rename_table (table_name , new_table_name )
1685
+ else :
1686
+ # case 2: We need to rename both, table and column names
1687
+ args = self .get_args_4_rename_table (table_name , new_table_name , new_column_names_dict )
1688
+
1689
+ if new_table_name is None :
1690
+ if new_column_names_dict :
1691
+ # case 3: We only need to rename the columns but same table name
1692
+ args = self .get_args_4_rename_table (table_name , table_name , new_column_names_dict )
1693
+
1694
+ connHandler = self .__getconnhandler ()
1695
+ data = connHandler .url_encode (args )
1696
+ response = connHandler .execute_table_tool (data , verbose = verbose )
1697
+
1698
+ if verbose :
1699
+ print (response .status , response .reason )
1700
+ print (response .getheaders ())
1701
+ connHandler .check_launch_response_status (response ,
1702
+ verbose ,
1703
+ 200 )
1704
+ if verbose :
1705
+ msg = f"Table '{ table_name } ' updated."
1706
+ print (msg )
1707
+
1708
+ def get_args_4_rename_table (self , table_name , new_table_name , new_column_names_dict ):
1709
+
1710
+ args = {}
1711
+
1712
+ if not new_column_names_dict :
1713
+ args = {
1714
+ "action" : "rename" ,
1715
+ "new_table_name" : new_table_name ,
1716
+ "table_name" : table_name
1717
+ }
1718
+ else :
1719
+ new_column_names = ',' .join (f'{ key } :{ value } ' for key , value in new_column_names_dict .items ())
1720
+
1721
+ args = {
1722
+ "action" : "rename" ,
1723
+ "new_column_names" : new_column_names ,
1724
+ "new_table_name" : new_table_name ,
1725
+ "table_name" : table_name
1726
+ }
1727
+ return args
1728
+
1651
1729
def update_user_table (self , table_name = None , list_of_changes = [],
1652
1730
verbose = False ):
1653
1731
"""Updates a user table
1654
1732
1655
1733
Parameters
1656
1734
----------
1657
- table_name : str, required
1735
+ table_name : str
1658
1736
table to be updated
1659
- list_of_changes : list, required
1737
+ list_of_changes : list
1660
1738
list of lists, each one of them containing sets of
1661
1739
[column_name, field_name, value].
1662
1740
column_name is the name of the column to be updated
@@ -1738,11 +1816,11 @@ def update_user_table(self, table_name=None, list_of_changes=[],
1738
1816
def get_table_update_arguments (table_name , columns , list_of_changes ):
1739
1817
num_cols = len (columns )
1740
1818
args = {
1741
- "ACTION" : "edit" ,
1742
- "NUMTABLES" : str (1 ),
1743
- "TABLE0_NUMCOLS" : str (num_cols ),
1744
- "TABLE0" : str (table_name ),
1745
- }
1819
+ "ACTION" : "edit" ,
1820
+ "NUMTABLES" : str (1 ),
1821
+ "TABLE0_NUMCOLS" : str (num_cols ),
1822
+ "TABLE0" : str (table_name ),
1823
+ }
1746
1824
index = 0
1747
1825
for column in columns :
1748
1826
found_in_changes = False
@@ -1872,11 +1950,11 @@ def set_ra_dec_columns(self, table_name=None,
1872
1950
1873
1951
Parameters
1874
1952
----------
1875
- table_name : str, required
1953
+ table_name : str
1876
1954
table to be set
1877
- ra_column_name : str, required
1955
+ ra_column_name : str
1878
1956
ra column to be set
1879
- dec_column_name : str, required
1957
+ dec_column_name : str
1880
1958
dec column to be set
1881
1959
verbose : bool, optional, default 'False'
1882
1960
flag to display information about the process
@@ -1890,11 +1968,11 @@ def set_ra_dec_columns(self, table_name=None,
1890
1968
raise ValueError ("Dec column name cannot be null" )
1891
1969
1892
1970
args = {
1893
- "ACTION" : "radec" ,
1894
- "TABLE_NAME" : str (table_name ),
1895
- "RA" : str (ra_column_name ),
1896
- "DEC" : str (dec_column_name ),
1897
- }
1971
+ "ACTION" : "radec" ,
1972
+ "TABLE_NAME" : str (table_name ),
1973
+ "RA" : str (ra_column_name ),
1974
+ "DEC" : str (dec_column_name ),
1975
+ }
1898
1976
connHandler = self .__getconnhandler ()
1899
1977
data = connHandler .url_encode (args )
1900
1978
response = connHandler .execute_table_edit (data , verbose = verbose )
@@ -1933,7 +2011,7 @@ def login(self, user=None, password=None, credentials_file=None,
1933
2011
user = ins .readline ().strip ()
1934
2012
password = ins .readline ().strip ()
1935
2013
if user is None :
1936
- user = input ("User: " )
2014
+ user = input ("User: " )
1937
2015
if user is None :
1938
2016
log .info ("Invalid user name" )
1939
2017
return
0 commit comments