Skip to content

Commit 5fc031a

Browse files
committed
Add modify_unrecognized_table_units() function
Both xmlparser/utils and model/modelutils use this snippet of code, so pull it out into a separate function
1 parent 6836e01 commit 5fc031a

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

astroquery/utils/tap/model/modelutils.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
"""
1717

1818
import os
19+
1920
from astropy.table import Table as APTable
20-
from astropy import units as u, io
2121

2222
from astroquery.utils.tap.xmlparser import utils
2323

@@ -39,15 +39,7 @@ def read_results_table_from_file(file_name, output_format, correct_units=True):
3939
result = APTable.read(file_name, format=astropy_format)
4040

4141
if correct_units:
42-
for cn in result.colnames:
43-
col = result[cn]
44-
if isinstance(col.unit, u.UnrecognizedUnit):
45-
try:
46-
col.unit = u.Unit(col.unit.name.replace(".", " ").replace("'", ""))
47-
except Exception as ex:
48-
pass
49-
elif isinstance(col.unit, str):
50-
col.unit = col.unit.replace(".", " ").replace("'", "")
42+
utils.modify_unrecognized_table_units(result)
5143

5244
return result
5345
else:

astroquery/utils/tap/xmlparser/utils.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,7 @@ def read_http_response(response, output_format, correct_units=True):
3838
pass
3939

4040
if correct_units:
41-
for cn in result.colnames:
42-
col = result[cn]
43-
if isinstance(col.unit, u.UnrecognizedUnit):
44-
try:
45-
col.unit = u.Unit(col.unit.name.replace(".", " ").replace("'", ""))
46-
except Exception as ex:
47-
pass
48-
elif isinstance(col.unit, str):
49-
col.unit = col.unit.replace(".", " ").replace("'", "")
41+
modify_unrecognized_table_units(result)
5042

5143
return result
5244

@@ -66,3 +58,17 @@ def read_file_content(file_path):
6658
file_content = file_handler.read()
6759
file_handler.close()
6860
return file_content
61+
62+
63+
def modify_unrecognized_table_units(table):
64+
"""Modifies the units of an input table column in place
65+
"""
66+
for cn in table.colnames:
67+
col = table[cn]
68+
if isinstance(col.unit, u.UnrecognizedUnit):
69+
try:
70+
col.unit = u.Unit(col.unit.name.replace(".", " ").replace("'", ""))
71+
except Exception:
72+
pass
73+
elif isinstance(col.unit, str):
74+
col.unit = col.unit.replace(".", " ").replace("'", "")

0 commit comments

Comments
 (0)