Skip to content

Commit

Permalink
Added astropy.table handling to add_data method.
Browse files Browse the repository at this point in the history
  • Loading branch information
hover2pi committed Jun 5, 2017
1 parent d09afac commit 305775b
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions astrodbkit/astrodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ def add_data(self, data, table, delimiter='|', bands='', verbose=False):
Parameters
----------
data: str, sequence
The path to an ascii file or a list of lists. The first row or element must
data: str, array-like, astropy.table.Table
The path to an ascii file, array-like object, or table. The first row or element must
be the list of column names
table: str
The name of the table into which the data should be inserted
Expand All @@ -236,7 +236,11 @@ def add_data(self, data, table, delimiter='|', bands='', verbose=False):
# Or read the sequence of data elements into a table
elif isinstance(data, (list, tuple, np.ndarray)):
data = ii.read(['|'.join(map(str, row)) for row in data], data_start=1, delimiter='|')


# Or if it's already an astropy table
elif isinstance(data, at.Table):
pass

else:
data = None

Expand Down Expand Up @@ -309,6 +313,28 @@ def add_data(self, data, table, delimiter='|', bands='', verbose=False):

# Remove bad records from the table
new_records.remove_rows(del_records)

# For images, try to populate the table by reading the FITS header
if table.lower() == 'images':
for n, new_rec in enumerate(new_records):

# Convert relative path to absolute path
relpath = new_rec['image']
if relpath.startswith('$'):
abspath = os.popen('echo {}'.format(relpath.split('/')[0])).read()[:-1]
if abspath:
new_rec['image'] = relpath.replace(relpath.split('/')[0], abspath)

# Test if the file exists and try to pull metadata from the FITS header
if os.path.isfile(new_rec['image']):
new_records[n]['image'] = relpath
new_records[n] = _autofill_spec_record(new_rec)
else:
print('Error adding the image at {}'.format(new_rec['image']))
del_records.append(n)

# Remove bad records from the table
new_records.remove_rows(del_records)

# Get some new row ids for the good records
rowids = self._lowest_rowids(table, len(new_records))
Expand Down

0 comments on commit 305775b

Please sign in to comment.