conn_out.close() 


if __name__ == '__main__':
    #get arguments
    args=parse_args()
    input_name=args.db_in
    output_name=args.db_out
    max_chi2=args.max_chi2
    chi2_function_name=args.reference
    # create mcpp_observable_ids in new database
    create_mcpp_observable_ids(input_name,output_name)
    # get array ids
    array_ids_dict, style=get_array_ids_and_style(input_name)
    # populate vars_lookups, vars_functions, and gauss_constraints with array ids
    gauss_constraints=tools.populate_with_array_ids((user.gauss_constraints.get()),style,array_ids_dict)
    contour_constraints=tools.populate_with_array_ids((user.contour_constraints.get()),style,array_ids_dict)
    # populate contours and add to managers
    contours=tools.populate_contours(user.contours.get())
    tools.add_contours(contours)
    # now add values to the managers
    tools.add_gauss_constraints(gauss_constraints)
    tools.add_contour_constraints(contour_constraints)
    # look for chi2 calculators
    if args.reference in user.constraints_sets.constraints.keys():
        tools.add_chi2_calculator(args.reference,user.constraints_sets.get(args.reference))
    # make selection query
    sql_where=''
    if args.sql_where is not None:
        sql_where='where ' + args.sql_where
    select_query='select rowid, * from points {};'.format(sql_where)
 # get all possible values
 value_dict_list=[vars_lookups,vars_functions,gauss_constraints,contour_constraints,mneu_mg_m12g_m3g_X2_lookups]
 array_ids_dict=get_array_ids_dict(values,value_dict_list,style) 
 # prepare select statement
 if not args.reduced_db:
     first_two_columns=['rowid','collections_rowid']
 else:
     first_two_columns=['points_rowid','rowid']
 columns=','.join(first_two_columns+[ oid_column_dict[oid] for oid in array_ids_dict.keys()])
 sql_where=''
 if args.sql_where is not None:
     sql_where='where ' + args.sql_where
 sql_selection='select  {} from points {};'.format(columns,sql_where)
 print('the sql query is:\n{}'.format(sql_selection))
 # get unique items in the list ordered
 vars_lookups=tools.populate_with_array_ids(vars_lookups,style,array_ids_dict)
 vars_functions=tools.populate_with_array_ids(vars_functions,style,array_ids_dict)
 gauss_constraints=tools.populate_with_array_ids(gauss_constraints,style,array_ids_dict)
 contour_constraints=tools.populate_with_array_ids(contour_constraints,style,array_ids_dict)
 mneu_mg_m12g_m3g_X2_lookups=tools.populate_with_array_ids(mneu_mg_m12g_m3g_X2_lookups,style,array_ids_dict)
 # pupulate mneu_mg_m12g_m3g_X2_lookups with the lookup data
 mneu_mg_m12g_m3g_X2_lookups=tools.populate_mneu_mg_m12g_m3g_X2_lookups(mneu_mg_m12g_m3g_X2_lookups)
 # populate contours and add to managers
 contours=tools.populate_contours(user.contours.get())
 tools.add_contours(contours)
 # now add values to the managers
 tools.add_vars_lookups(vars_lookups) 
 tools.add_vars_functions(vars_functions) 
 tools.add_gauss_constraints(gauss_constraints)
 tools.add_contour_constraints(contour_constraints)
 tools.add_mneu_mg_m12g_m3g_X2_lookups(mneu_mg_m12g_m3g_X2_lookups)