bottom = fit[0] * np.log10(flux - np.sqrt(flux))
    delm = top - bottom
    return delm / 2

# User specifies filename information
os.chdir(config.blazar_photometry)
cat_list = glob.glob('*.cat')
# ref_source_input = input('Enter ref_source: ')
ref_source_input = 'ref_mrk501'
year_input = input('Enter year: ')
ref_source_data = np.copy(ref_cats.ref_mrk501)

# loop to create .txt files containing all data(verbose_file) and data for plotting(data_file)
for catalog in cat_list:
    os.chdir(config.blazar_photometry)
    head_info = read_fits.decode_fitshead(catalog)
    filter_input = read_fits.get_filter(head_info)
    verbose_file = open(ref_source_input + '_' + filter_input + '_' + year_input + '_verbose.txt', 'a')
    data_file = open(ref_source_input + '_' + filter_input + '_' + year_input + '.txt', 'a')
    cat_data_list = mag_fit(ref_source=ref_source_data, cat=catalog, filt=filter_input)

    if not cat_data_list:
        pass
    else:
        for x in cat_data_list:
            verbose_file.write(str(x))
            verbose_file.write(',')
        verbose_file.write('\n')
        verbose_file.close()

        cat_name = cat_data_list[0]
target_dec = td.bytes_to_str(target_dec)

RA_dict = td.target_dict(target_name, target_RA)
dec_dict = td.target_dict(target_name, target_dec)

obj_ra = float(RA_dict[config.obj.lower()]) * 15.
obj_dec = float(dec_dict[config.obj.lower()])

print(obj_ra, obj_dec)

# grab all catalogs with correct object
os.chdir(config.catalogs)
catalogs = np.asarray(glob.glob('*.cat'), dtype=str)
good_cats = np.empty(len(catalogs), dtype=bool)
for i in range(len(catalogs)):
    header = read_fits.decode_fitshead(catalogs[i])
    obj = str(read_fits.get_info('OBJECT', header))
    obj = obj.strip()
    obj = obj.replace('_', '')
    obj = obj.replace(' ', '')
    obj = obj.lower()
    if obj == config.obj.lower():
        good_cats[i] = True
    else:
        good_cats[i] = False
# catalogs = catalogs[good_cats]
mega = []

# create 3D array containing source number, flux, ra and dec for each catalog.
# fig1 = aplpy.FITSFigure(catalogs[0])
for i in catalogs: