Input/Output¶
List of available functions¶
Examples¶
Below are a few examples on how to use some functions. First, let us generate fake data and write them into a VOtable file:
from astropy.table import Table
tab = Table(names=('ID', 'value1', 'value2'))
tab.add_row((1, 25, 13))
tab.add_row((2, 0, 6))
tab.add_row((3, 2, -7))
tab.write('data.vot', format='votable')
Now we can play with the VOtable file.
from wilfried.utilities.io import is_VOtable, loadVOtable, VOtableColumns, write_array_to_vot
file = 'data.vot'
# First let us print column names
columns = VOtableColumns(file)
print(columns)
# Then we check file type and then load data
if is_VOtable(file): # IOError is automatically raised if file is not a VOtable
catalogue1 = load_VOtable(file) # default output is astropy Table
print(catalogue1)
# Now we load it as an astropy structured array
if is_VOtable(file):
catalogue2 = loadVOtable(file, outputType='array')
print(catalogue2)
# We can directly save the array into a new VOtable file without converting to a Table
new_file = 'new_data.vot'
write_array_to_vot(catalogue2, 'new_data.vot')
# Let us check whether it saved data correctly
catalogue3 = loadVOtable(new_file)
print(catalogue3)
Documentation¶
Code author: Wilfried Mercier - IRAP <wilfried.mercier@irap.omp.eu>
Functions related to input-output interaction.
- utilities.io.is_VOtable(fullname)[source]¶
Code author: Wilfried Mercier - IRAP <wilfried.mercier@irap.omp.eu>
Check whether a file is a VOtable.
- Parameters
fullname (str) – name of the file
- Returns
True if it is a VOtable
- Return type
bool
- Raises
IOError – if fullname is no a VOtable
- utilities.io.loadVOtable(name, outputType='Table', num=0, pedantic=False, use_names_over_ids=False)[source]¶
Code author: Wilfried Mercier - IRAP <wilfried.mercier@irap.omp.eu>
Load a VOtable using astropy.
- Parameters
name (str) – VOtable catalogue file name
num (int) – (Optional) positon of the table to load within the VOtable file
outputType (str) –
(Optional) type of output data. Must be one of the following:
’default’ to load it as an Astropy VOtable data type
’array’ to load it as a numpy structured array
’Table’ to load it as an Astropy Table
pedantic (bool) – (Optional) whether to disable Exceptions when reading VOtable files with unusual keywords
use_names_over_id (bool) – (Optional) whether (when creating as astropy Table out of the VOtable table element) to use columns names rather than the unique IDs in the VOtable columns to name the astropy Table columns
- Returns
loaded table
- Raises
TypeError –
if outputType is not of type str
if num is not of type int
- utilities.io.VOtableColumns(name, fullInfo=False)[source]¶
Code author: Wilfried Mercier - IRAP <wilfried.mercier@irap.omp.eu>
Show the column names in the VOtable object.
- Parameters
name (str) – VOtable file name
fullInfo (bool) – (Optional) whether to show full information or just the names
- utilities.io.write_array_to_vot(array, outputFile)[source]¶
Code author: Wilfried Mercier - IRAP <wilfried.mercier@irap.omp.eu>
Write a numpy array or an astropy Table into a VOtable file.
Note
If array is not an astropy Table, it is converted to it.
- Parameters
array (numpy structured array of astropy Table) – array to write into the file
outputFile (str) – file to write the array into
- utilities.io.add_new_array_to_previous(newArray, fullFileName, fields, oldArray=None, isFirstArray=False, fieldsToDrop=None, typesToDrop=None)[source]¶
Code author: Wilfried Mercier - IRAP <wilfried.mercier@irap.omp.eu>
Append a new structured array to another one, only keeping some fields after applying their corresponding data types onto the new columns. This function is used to combine data from different MUSE catalogues where field names and data types may vary between different versions.
- Parameters
fields (list[str]) – field names
fullfilename (str) – name of the new array to append to the previous one
newArray – new array to append to the previous one
fieldsToDrop (list[str]) – (Optional) names of the fields to move to the bottom and change their type. If not None, typesToDrop must be a list of the same size.
isFirstArray (bool) – (Optional) whether this is the first array generated with this function or not. If True a check will be made at the end.
oldArray ((Optional) structured ndarray) – previous array whereto append new data
typesToDrop (list) – (Optional) data types corresponding to the specified fields which must be dropped to the bottom
- Returns
new structured array where all the content of the previous ones has been correctly appended
- Return type
structured ndarray
- utilities.io.move_bad_fields_to_bottom(array, orderedFieldList, orderedTypeList)[source]¶
Code author: Wilfried Mercier - IRAP <wilfried.mercier@irap.omp.eu>
Move a list of fields from a structured array to the bottom and change their type.
- Parameters
array (structured ndarray) – array to modify
orderedFieldList (list[str]) – list of field names to move and to change type
orderedTypeList (list) – list of new types for the fields (same order as orderedFieldList)
- Returns
array with some fields moved to the bottom and with different types
- Return type
structured ndarray