def get_stellar_mass_growth(galaxies_by_id, galaxy_num):
    time = [time_slice[1]*1.e3 for time_slice in galaxies_by_id[galaxy_num]] 
    stellar_mass = [time_slice[3] for time_slice in galaxies_by_id[galaxy_num]]
    stellar_mass_coefficients = galaxies_star_mass.get_func_coeffs(time, stellar_mass)
#    print 'Stellar Mass Growth Coefficients 10**(a*t**7. + b*t**6. + c*t**5. + d*t**4. + e*t**3. + f*t**2. + g*t + h), t in Myr:'
#    print stellar_mass_coefficients
    stellar_mass_fitted = [10.**(galaxies_star_mass.poly_func(t, *tuple(stellar_mass_coefficients))) for t in time]
    return time, stellar_mass, stellar_mass_coefficients, stellar_mass_fitted
def get_star_mass_coefficients(galaxies_by_id, galaxy_num):
    #time orig. in Gyr; use Myr to not have to convert it in hermite code
    time = [time_slice[1]*1.e3 for time_slice in galaxies_by_id[galaxy_num]] 
    stellar_mass = [time_slice[3] for time_slice in galaxies_by_id[galaxy_num]]
    popt = galaxies_star_mass.get_func_coeffs(time, stellar_mass)
    stellar_mass_fitted = [10.**(galaxies_star_mass.poly_func(t, *tuple(popt))) for t in time]
    galaxies_star_mass.plot_stellar_mass(time, galaxy_num, stellar_mass, stellar_mass_fitted)
    plt.show()
    return popt
def run():
    mpl.rcParams.update({'font.size': 15})
    plt.rc('text', usetex=True)
    plt.rc('font', family='Computer Modern Sans serif')
    H0 = Constants.H0
    WM = Constants.WM
    WV = Constants.WV

#    smbh_cluster = get_clusters.process_smbh_cluster(H0, WM, WV)
    smbh_cluster = get_clusters.get_pickled_file('smbh_cluster.pkl')
    smbh_by_id = analyze_clusters.get_smbh_by_id(smbh_cluster)

#    galaxies_cluster = get_clusters.process_galaxies_cluster(H0, WM, WV)
    galaxies_cluster_no_bad_z = get_clusters.get_pickled_file('galaxies_cluster_no_bad_z.pkl')
    galaxies_by_id = analyze_clusters.get_galaxies_by_id(galaxies_cluster_no_bad_z)
    galaxies_masses, final_masses = analyze_clusters.get_galaxies_masses(galaxies_by_id)


    for galaxy_num in ['1', '65', '149', '187', '217']:
#    galaxy_num = '187'
        smbh_mass = np.array(analyze_clusters.get_cbh_accreted_plus_seed_mass(smbh_by_id, galaxy_num))
        popt, smbh_mass_fitted = curve_fit_central_bh_masses(galaxy_num, smbh_mass)
        smbh_mass_fitted = np.array(smbh_mass_fitted)
        
        galaxy_mass_v_time = zip(galaxies_masses[galaxy_num][1], galaxies_masses[galaxy_num][2])
        galaxy_mass_v_time = np.array(map(list, galaxy_mass_v_time))
        popt_galaxy_mass = analyze_clusters.curve_fit_any_galaxy_mass(galaxies_masses, galaxy_num)
        ydata_est = [analyze_clusters.poly_func(x, *tuple(popt_galaxy_mass)) for x in galaxies_masses[galaxy_num][1]]
        galaxies_mass_fitted = [galaxies_masses[galaxy_num][1], [10**y for y in ydata_est]]
        galaxies_mass_fitted = np.array(galaxies_mass_fitted)

        stellar_mass = analyze_clusters.get_galaxy_stellar_mass(galaxies_by_id, galaxy_num)
        stellar_mass_v_time = zip(galaxies_masses[galaxy_num][1], stellar_mass)
        stellar_mass_v_time = np.array(map(list, stellar_mass_v_time))
#        print galaxies_by_id[galaxy_num]
#        print galaxy_mass_v_time, '\n'
#        print stellar_mass_v_time, '\n'
#        print smbh_mass

        plt.figure()
        plt.semilogy(galaxy_mass_v_time[:,0], galaxy_mass_v_time[:,1], 'b', lw=3.5, alpha=0.5, label='Galaxy Mass')
        plt.semilogy(galaxies_mass_fitted[0,:], galaxies_mass_fitted[1,:], 'b--', lw=3.5, alpha=0.5)
#        plt.ylabel('Mass [M$_\odot$]')
        plt.ylabel(r'$M_{BH} [M_\odot]$')

        plt.semilogy(stellar_mass_v_time[:,0], stellar_mass_v_time[:,1], 'g', lw=2., alpha=0.5, label='Stellar Mass')
        popt_star_mass = galaxies_star_mass.get_func_coeffs(stellar_mass_v_time[:,0]*1.e3, stellar_mass_v_time[:,1])
        stellar_mass_fitted = zip(stellar_mass_v_time[:,0]*1.e3, 
                                  [10.**(galaxies_star_mass.poly_func(t, *tuple(popt_star_mass))) for t in 
                                                                                         stellar_mass_v_time[:,0]*1.e3])
        stellar_mass_fitted = np.array(stellar_mass_fitted)
        plt.semilogy(stellar_mass_fitted[:,0]/1.e3, stellar_mass_fitted[:,1], 'g--', lw=2., alpha=0.5)

        print(galaxy_num, smbh_mass[:,1])
        plt.semilogy(smbh_mass[:,0], smbh_mass[:,1], 'r', lw=0.5, alpha=1., label='Central BH Mass')
        plt.semilogy(smbh_mass_fitted[0,:], smbh_mass_fitted[1,:], 'r--', lw=1., alpha=0.5)

        plt.xticks(np.arange(0., math.ceil(max(galaxy_mass_v_time[:,0]))+1., 1.0))
        plt.xlabel('Time [Gyr]')
        plt.xticks(np.arange(0, 16, 2))
#        plt.xlim([0, 14])
        plt.ylim([10.**4, 10.**15])
        plt.tight_layout()
        plt.legend(loc='lower right')
        plt.savefig(os.path.join(plots_folder, 'Masses_plot_galaxy_%s.png' % (galaxy_num)))
#        plt.show()
        plt.close()

#    print galaxies_by_id[galaxy_num]

#    galaxy_1 = [line for line in galaxies_cluster if line[2]==1]
#    black_holes = [time_slice[-1] for time_slice in galaxy_1 if time_slice[0]<=0.0][0]
#    print black_holes


#    analyze_clusters.plot_galaxies_all_masses(galaxies_masses)
    '''
    top_masses = 3
    galaxies_max_mass = analyze_clusters.get_most_massive_galaxies(galaxies_masses, final_masses, top_masses)
#    print galaxies_max_mass

    F1 = open(os.path.join(get_clusters.cluster_folder, ''.join([str(top_masses), '_max_mass_galaxies.pkl'])), 'wb')
    pickle.dump(galaxies_max_mass, F1)
    F1.close()
#    analyze_clusters.plot_max_galaxies_masses(galaxies_max_mass)

    popt_galaxies_masses, galaxies_max_mass_fitted = analyze_clusters.curve_fit_most_massive_galaxies(galaxies_max_mass)
    print popt_galaxies_masses

    F = open(os.path.join(get_clusters.cluster_folder,
                          ''.join([str(top_masses), '_max_mass_galaxies_fitted.pkl'])), 'wb')
    pickle.dump(galaxies_max_mass_fitted, F)
    F.close()
####################################################
    f = open(os.path.join(os.path.expanduser('~'), 'Dropbox', 'Columbia', 'Ostriker', 'Cannibalism', 'hermite',
                      'galaxy_mass.txt'), 'rb')
    time_mass = f.readlines()
    time = []
    mass = []
    for line in time_mass:
        time.append(float(line.split()[0]))
        mass.append(float(line.split()[1]))
    time = np.array(time)
    garbage = []
    mass = np.array(mass)
    galaxies_max_mass_fitted['65'] = [time, garbage, mass]

    analyze_clusters.plot_max_masses_galaxies_orig_and_fitted(galaxies_max_mass, galaxies_max_mass_fitted, top_masses)
    '''
    fit_and_plot_central_bh_masses(smbh_cluster, galaxies_cluster_no_bad_z)