def _list_tables(filename, pedantic=False): votable = parse(filename, pedantic=pedantic) tables = {} for i, table in enumerate(votable.iter_tables()): tables[i] = table.name return tables
def read(self, filename, pedantic=False, tid=-1, verbose=True): ''' Read a table from a VOT file Required Arguments: *filename*: [ string ] The VOT file to read the table from Optional Keyword Arguments: *tid*: [ integer ] The ID of the table to read from the VO file (this is only required if there are more than one table in the VO file) *pedantic*: [ True | False ] When *pedantic* is True, raise an error when the file violates the VO Table specification, otherwise issue a warning. ''' _check_vo_installed() self.reset() # If no table is requested, check that there is only one table if tid==-1: tables = _list_tables(filename, pedantic=pedantic) if len(tables) == 1: tid = 0 elif len(tables) == 0: raise Exception("There are no tables present in this file") else: raise TableException(tables, 'tid') votable = parse(filename, pedantic=pedantic) for id, table in enumerate(votable.iter_tables()): if id==tid: break if table.ID: self.table_name = str(table.ID) elif table.name: self.table_name = str(table.name) for field in table.fields: if type(field.name) == str: colname = field.name else: if type(field._ID) == str: colname = field._ID else: raise Exception("Error reading in the VO table: no name or ID for field") data = table.array[colname] if len(data) > 0 and data.ndim == 1 and not np.all([np.isscalar(x) for x in data]): warnings.warn("VO Variable length vector column detected (%s) - converting to string" % colname) data = np.array([str(x) for x in data]) if self._masked: self.add_column(colname, data, \ unit=field.unit, mask=table.mask[colname]) else: self.add_column(colname, data, \ unit=field.unit)
def read(self, filename, pedantic=False, tid=-1, verbose=True): """ Read a table from a VOT file Required Arguments: *filename*: [ string ] The VOT file to read the table from Optional Keyword Arguments: *tid*: [ integer ] The ID of the table to read from the VO file (this is only required if there are more than one table in the VO file) *pedantic*: [ True | False ] When *pedantic* is True, raise an error when the file violates the VO Table specification, otherwise issue a warning. """ _check_vo_installed() self.reset() # If no table is requested, check that there is only one table if tid == -1: tables = _list_tables(filename, pedantic=pedantic) if len(tables) == 1: tid = 0 elif len(tables) == 0: raise Exception("There are no tables present in this file") else: raise TableException(tables, "tid") votable = parse(filename, pedantic=pedantic) for id, table in enumerate(votable.iter_tables()): if id == tid: break if table.ID: self.table_name = str(table.ID) elif table.name: self.table_name = str(table.name) for field in table.fields: if type(field.name) == str: colname = field.name else: if type(field._ID) == str: colname = field._ID else: raise Exception("Error reading in the VO table: no name or ID for field") # ensure that colname is a valid python variable name (i.e. contains no non-allowed chars) # the following will replace any invalid chars with underscores, and/or prepend an initial # underscore to names that begin with a digit. clean = lambda varStr: re.sub("\W|^(?=\d)", "_", varStr) colname = clean(colname) data = table.array[colname] if len(data) > 0 and data.ndim == 1 and not np.all([np.isscalar(x) for x in data]): warnings.warn("VO Variable length vector column detected (%s) - converting to string" % colname) data = np.array([str(x) for x in data]) if self._masked: self.add_column(colname, data, unit=field.unit, mask=table.mask[colname], description=field.description) else: self.add_column(colname, data, unit=field.unit, description=field.description)