Esempio n. 1
0
    'band': 'WISE_W2',
    'magnitude': 12.990,
    'magnitude_error': 0.028,
    'telescope': 'WISE',
    'reference': 'Cutr12'
}, {
    'source': '2MASS J13571237+1428398',
    'band': 'WISE_W3',
    'magnitude': 12.476,
    'magnitude_error': 0.279,
    'telescope': 'WISE',
    'reference': 'Cutr12'
}, {
    'source': '2MASS J13571237+1428398',
    'band': 'WISE_W4',
    'magnitude': 9.560,
    'magnitude_error': None,
    'telescope': 'WISE',
    'reference': 'Cutr12'
}]
db.Photometry.insert().execute(phot_data)

# Checking object
_ = db.inventory('2MASS J13571237+1428398', pretty_print=True)

# Save single object
db.save_json('2MASS J13571237+1428398', 'data')

# Save entire database to directory 'data'
db.save_database('data')
full_bands = [s['band'] for s in filters_to_add]
existing_bands = db.query(db.PhotometryFilters).filter(db.PhotometryFilters.c.band.in_(full_bands)).table()
if len(existing_bands) > 0:
    existing_bands = existing_bands['band'].tolist()
new_bands = list(set(full_bands)-set(existing_bands))

# Data to be inserted
insert_data = list(filter(lambda d: d['band'] in new_bands, filters_to_add))
if len(insert_data) > 0:
    print(f'Entries to insert: {insert_data}')
    db.PhotometryFilters.insert().execute(insert_data)

# Data to be updated
update_data = list(filter(lambda d: d['band'] in existing_bands, filters_to_add))
if len(update_data) > 0:
    print(f'Entries to update: {update_data}')
    # Use bindparam to handle multiple UPDATE commands
    # had to rename band since bindparam complains about it being the same as the column name
    update_data = [{'_band': s['band'], **s} for s in update_data]
    stmt = db.PhotometryFilters.update().where(db.PhotometryFilters.c.band == bindparam('_band')).\
        values(telescope=bindparam('telescope'),
               instrument=bindparam('instrument'),
               effective_wavelength=bindparam('effective_wavelength'),
               width=bindparam('width')
               )
    db.engine.execute(stmt, update_data)

# Save output
if SAVE_DB:
    db.save_database(directory='data/')