def run_ephem_main(reproc=False):
    '''
    The main controller for the module. It executes the code in ephem_main 
    and writes the output to the database.
    '''
    #print 'Processing ' + str(len(file_list)) + ' files.'
    count = 0
    query_list = session.query(MasterFinders, MasterImages).\
        join(MasterImages).\
        options(subqueryload('master_images_rel')).\
        filter(or_(MasterFinders.jpl_ra != None, MasterFinders.jpl_dec != None)).\
        all()

    print 'Processing ' + str(len(query_list)) + ' files.' 
    for record in query_list:
        if record.MasterFinders.ephem_x == None \
                or record.MasterFinders.ephem_y == None \
                or reproc == True:
            file_dict = get_header_info(\
                os.path.join(\
                    record.MasterImages.file_location[0:-4], 
                    record.MasterImages.fits_file))
            delta_x, delta_y = calc_delta(file_dict, record.MasterFinders)
            ephem_x, ephem_y = calc_pixel_position(file_dict, delta_x, delta_y)
            update_dict = {}
            update_dict['ephem_x'] = int(ephem_x)
            update_dict['ephem_y'] = int(ephem_y)
            session.query(MasterFinders).\
                filter(MasterFinders.id == record.MasterFinders.id).\
                update(update_dict)
        count = counter(count)
    session.commit()
    session.close()
def run_ephem_main(reproc=False):
    '''
    The main controller for the module. It executes the code in ephem_main 
    and writes the output to the database.
    '''

    # Build the record list and log the length.
    query_all_records = session.query(MasterFinders, MasterImages).\
        join(MasterImages).\
        options(subqueryload('master_images_rel'))
    logging.info('{} total records in master_finders.'.\
        format(query_all_records.count()))
    if reproc == True:
        query_list = query_all_records.all()
        logging.info('reproc == True, Reprocessing all records')
    else:
        logging.info('reproc == False')    
        query_list = session.query(MasterFinders, MasterImages).\
            join(MasterImages).\
            options(subqueryload('master_images_rel')).\
            filter(or_(MasterFinders.jpl_ra != None, MasterFinders.jpl_dec != None)).\
            all()
        logging.info('Processing {} files where ephem_x or ephem_y IS NULL.'.\
            format(len(query_list)))

    for counter, record in enumerate(query_list):
        logging.info('Working on {}'.\
            format(record.MasterImages.name, record.MasterFinders.object_name))
        file_dict = get_header_info(\
            os.path.join(\
                record.MasterImages.file_location[0:-4], 
                record.MasterImages.fits_file))
        delta_x, delta_y = calc_delta(file_dict, record.MasterFinders)
        ephem_x, ephem_y = calc_pixel_position(file_dict, delta_x, delta_y)
        update_dict = {}
        update_dict['ephem_x'] = int(ephem_x)
        update_dict['ephem_y'] = int(ephem_y)
        session.query(MasterFinders).\
            filter(MasterFinders.id == record.MasterFinders.id).\
            update(update_dict)
        if counter % 100 == 0:
            session.commit()
    session.close()
def jpl2db_main(filename, reproc=False):
    '''
    The main controller. 
    '''
    # Get the unique record from the master_images table.
    assert os.path.splitext(filename)[1] == '.fits', \
        'Expected .fits got ' + filename
    master_images_query = session.query(MasterImages).filter(\
        MasterImages.fits_file == os.path.basename(filename)).one()

    # Gather some file information and iterate over the moons
    file_dict = get_header_info(os.path.abspath(filename))
    file_dict = convert_datetime(file_dict)
    all_moon_dict = make_all_moon_dict('planets_and_moons.txt', file_dict)
    for moon in all_moon_dict.keys():
        logging.info('Processing {} for {}'.format(moon, filename))
        moon_dict = all_moon_dict[moon]
        moon_dict.update(file_dict)

        # Check if a record exists. If there's no record insert one.
        master_finders_count = session.query(MasterFinders).filter(\
            MasterFinders.master_images_id == master_images_query.id).filter(\
            MasterFinders.object_name == moon).count()
        if master_finders_count == 0:
            jpl_dict = get_jpl_data(moon_dict)
            moon_dict.update(jpl_dict)
            insert_record(moon_dict, master_images_query.id)

        # When a record exists, check if it has jpl_ra info. If it 
        # doesn't then update.        
        else:
            master_finders_count = session.query(MasterFinders).filter(\
                MasterFinders.master_images_id == master_images_query.id).filter(\
                MasterFinders.object_name == moon).filter(\
                MasterFinders.jpl_ra == None).count()
            if master_finders_count == 1 or reproc == True:
                jpl_dict = get_jpl_data(moon_dict)
                moon_dict.update(jpl_dict)
                update_record(moon_dict, master_images_query.id)

    session.close()
def build_sub_images_table_main(filename, reproc):
    '''
    The main controller.
    '''
    assert os.path.splitext(filename)[1] == '.png', \
        'Expected .png got ' + filename
    path = os.path.split(filename)[0] 
    basename = os.path.basename(filename)
    master_filename = get_master_filename(basename)
    master_images_query = session.query(MasterImages).filter(\
        MasterImages.name == master_filename).one()
    sub_images_query = session.query(SubImages).filter(\
        SubImages.name == basename).count()
    image_width, image_height = get_image_size(filename)

    # Make the input dict
    record_dict = {}
    record_dict['master_images_id'] = master_images_query.id
    record_dict['master_images_name'] = master_images_query.name
    record_dict['name'] = basename
    record_dict['file_location'] = path
    record_dict['image_width'] = image_width 
    record_dict['image_height'] = image_height
    record_dict['region'] = get_region(filename)

    if sub_images_query == 0:
        record = SubImages()
        insert_record(record_dict, record)
    elif sub_images_query == 1:
        sub_images_query = session.query(SubImages).filter(\
            SubImages.name == basename).update(record_dict)
        session.commit()
    else:
        pass

    session.close()
def build_finders_table_main(reproc):
    '''
    Calculate the ephemeris information for the subimages.
    '''
    query = session.query(MasterFinders).count()
    print str(query) + ' total ephemerides'
    query = session.query(MasterFinders, MasterImages)\
        .join(MasterImages, MasterImages.id == MasterFinders.master_images_id)\
        .filter(MasterFinders.ephem_x >= 0)\
        .filter(MasterFinders.ephem_y >= 0)\
        .filter(MasterFinders.ephem_x <= 1725)\
        .filter(MasterFinders.ephem_y <= 1300)\
        .filter(MasterImages.drz_mode == 'wide')\
        .all()
    print str(len(query)) + ' ephemerides in wide mode image FOVs'

    count = 0
    for record in query:
        count = counter(count)
        region_list = get_region_list(record.MasterFinders.ephem_x, record.MasterFinders.ephem_y)
        for region in region_list:
            add_new_record(record, region)
    session.commit()
    session.close()