diff --git a/lsmtool/skymodel.py b/lsmtool/skymodel.py index f9e27e650ee72eb50edd9f6f001a1f721e5ae4a5..2ac4a9bd8a2f5d39fbba207b09457a2c268c2a31 100644 --- a/lsmtool/skymodel.py +++ b/lsmtool/skymodel.py @@ -21,6 +21,7 @@ from . import _logging from . import tableio from . import operations +# Python 3 compatibility try: dict.iteritems except AttributeError: @@ -29,13 +30,14 @@ except AttributeError: return iter(d.values()) def iteritems(d): return iter(d.items()) + numpy_type = "U" else: # Python 2 def itervalues(d): return d.itervalues() def iteritems(d): return d.iteritems() - + numpy_type = "S" class SkyModel(object): """ @@ -926,7 +928,7 @@ class SkyModel(object): else: if colName == 'Patch': # Specify length of 50 characters - newCol = Column(name=colName, data=data, dtype='S50') + newCol = Column(name=colName, data=data, dtype='{}50'.format(numpy_type)) else: newCol = Column(name=colName, data=data) self.table.add_column(newCol, index=index) diff --git a/lsmtool/tableio.py b/lsmtool/tableio.py index f48a395a3769604b1acce527ea01fd09934565ed..be754a6315633493ec8b7ed13d53f4e8d1821a59 100644 --- a/lsmtool/tableio.py +++ b/lsmtool/tableio.py @@ -28,6 +28,24 @@ import re import logging import os +# Python 3 compatibility +try: + dict.iteritems +except AttributeError: + # Python 3 + def itervalues(d): + return iter(d.values()) + def iteritems(d): + return iter(d.items()) + numpy_type = "U" +else: + # Python 2 + def itervalues(d): + return d.itervalues() + def iteritems(d): + return d.iteritems() + numpy_type = "S" + # Define the valid columns here as dictionaries. The entry key is the lower-case # name of the column, the entry value is the key used in the astropy table of the @@ -150,12 +168,12 @@ def createTable(outlines, metaDict, colNames, colDefaults): converters = {} nameCol = 'col{0}'.format(colNames.index('Name')+1) - converters[nameCol] = [ascii.convert_numpy('S100')] + converters[nameCol] = [ascii.convert_numpy('{}100'.format(numpy_type))] typeCol = 'col{0}'.format(colNames.index('Type')+1) - converters[typeCol] = [ascii.convert_numpy('S100')] + converters[typeCol] = [ascii.convert_numpy('{}100'.format(numpy_type))] if 'Patch' in colNames: patchCol = 'col{0}'.format(colNames.index('Patch')+1) - converters[patchCol] = [ascii.convert_numpy('S100')] + converters[patchCol] = [ascii.convert_numpy('{}100'.format(numpy_type))] log.debug('Creating table...') table = Table.read('\n'.join(outlines), guess=False, format='ascii.no_header', delimiter=',', @@ -985,7 +1003,7 @@ def coneSearch(VOService, position, radius): # Make sure Name is a str column NameRaw = table['Name'].data.tolist() - NameCol = Column(name='Name', data=NameRaw, dtype='S100') + NameCol = Column(name='Name', data=NameRaw, dtype='{}100'.format(numpy_type)) table.remove_column('Name') table.add_column(NameCol, index=0) @@ -1005,7 +1023,7 @@ def coneSearch(VOService, position, radius): for i, maj in enumerate(table[allowedColumnNames['majoraxis']]): if maj > 0.0: types[i] = 'GAUSSIAN' - col = Column(name='Type', data=types, dtype='S100') + col = Column(name='Type', data=types, dtype='{}100'.format(numpy_type)) table.add_column(col, index=1) # Add reference-frequency column @@ -1089,9 +1107,9 @@ def makeEmptyTable(): colNames = ['Name', 'Type', 'Ra', 'Dec', 'I'] converters = {} nameCol = 'col{0}'.format(colNames.index('Name')+1) - converters[nameCol] = [ascii.convert_numpy('S100')] + converters[nameCol] = [ascii.convert_numpy('{}100'.format(numpy_type))] typeCol = 'col{0}'.format(colNames.index('Type')+1) - converters[typeCol] = [ascii.convert_numpy('S100')] + converters[typeCol] = [ascii.convert_numpy('{}100'.format(numpy_type))] table = Table.read(outlines, guess=False, format='ascii.no_header', delimiter=',', names=colNames, comment='#', data_start=0, converters=converters) table.remove_rows(0)