def ymodel(data_nus, z, dictkey_arrays, dict_modelfluxes, *par):
    """Constructs the total model from parameter values.

    ## inputs: data_nus, z, dictkey_arrays, dict_modelfluxes, *par

    ## output:
    - total model
    - bands
    - galaxy_flux (to be used by ln_prior)

    ## dependencies:
    This fct is used 
    (1)in ln_likelihood, this same script.
    (2)in scriptPLOTandWRITE.

    """
    STARBURSTFdict, BBBFdict, GALAXYFdict, TORUSFdict, _, _, _, _, _ = dict_modelfluxes
    gal_do, irlum_dict, nh_dict, BBebv_dict, _ = dictkey_arrays

    # Call MCMC-parameter values
    tau, agelog, nh, irlum, SB, BB, GA, TO, BBebv, GAebv = par[0:10]
    age = 10**agelog

    # Pick dictionary key-values, nearest to the MCMC- parameter values
    irlum_dct = model.pick_STARBURST_template(irlum, irlum_dict)
    nh_dct = model.pick_TORUS_template(nh, nh_dict)
    ebvbbb_dct = model.pick_BBB_template(BBebv, BBebv_dict)
    gal_do.nearest_par2dict(tau, age, GAebv)
    tau_dct, age_dct, ebvg_dct = gal_do.t, gal_do.a, gal_do.e

    # Call fluxes from dictionary using keys-values
    try:
        bands, gal_Fnu = GALAXYFdict[tau_dct, age_dct, ebvg_dct]
        bands, sb_Fnu = STARBURSTFdict[irlum_dct]
        bands, bbb_Fnu = BBBFdict[ebvbbb_dct]
        bands, tor_Fnu = TORUSFdict[nh_dct]

    except ValueError:
        print 'Error: Dictionary does not contain some values'

    # Renormalize to have similar amplitudes. Keep these fixed!
    sb_Fnu_norm = sb_Fnu.squeeze() / 1e20
    bbb_Fnu_norm = bbb_Fnu.squeeze() / 1e60
    gal_Fnu_norm = gal_Fnu.squeeze() / 1e18
    tor_Fnu_norm = tor_Fnu.squeeze() / 1e-40

    # Total SED sum
    #--------------------------------------------------------------------

    lum = 10**(SB)* sb_Fnu_norm + 10**(BB)*bbb_Fnu_norm    \
          + 10**(GA)*gal_Fnu_norm  +(10**TO) *tor_Fnu_norm

    #--------------------------------------------------------------------

    lum = lum.reshape((np.size(lum), ))

    return lum, bands, 10**(GA) * gal_Fnu_norm
def ymodel(data_nus, z, dictkey_arrays, dict_modelfluxes, *par):

    """Constructs the total model from parameter values.

    ## inputs: data_nus, z, dictkey_arrays, dict_modelfluxes, *par

    ## output:
    - total model
    - bands
    - galaxy_flux (to be used by ln_prior)

    ## dependencies:
    This fct is used 
    (1)in ln_likelihood, this same script.
    (2)in scriptPLOTandWRITE.

    """
    STARBURSTFdict, BBBFdict, GALAXYFdict, TORUSFdict, _, _, _, _, _ = dict_modelfluxes
    gal_do, irlum_dict, nh_dict, BBebv_dict, _ = dictkey_arrays

    # Call MCMC-parameter values
    tau, agelog, nh, irlum, SB, BB, GA, TO, BBebv, GAebv = par[0:10]
    age = 10 ** agelog

    # Pick dictionary key-values, nearest to the MCMC- parameter values
    irlum_dct = model.pick_STARBURST_template(irlum, irlum_dict)
    nh_dct = model.pick_TORUS_template(nh, nh_dict)
    ebvbbb_dct = model.pick_BBB_template(BBebv, BBebv_dict)
    gal_do.nearest_par2dict(tau, age, GAebv)
    tau_dct, age_dct, ebvg_dct = gal_do.t, gal_do.a, gal_do.e

    # Call fluxes from dictionary using keys-values
    try:
        bands, gal_Fnu = GALAXYFdict[tau_dct, age_dct, ebvg_dct]
        bands, sb_Fnu = STARBURSTFdict[irlum_dct]
        bands, bbb_Fnu = BBBFdict[ebvbbb_dct]
        bands, tor_Fnu = TORUSFdict[nh_dct]

    except ValueError:
        print "Error: Dictionary does not contain some values"

    # Renormalize to have similar amplitudes. Keep these fixed!
    sb_Fnu_norm = sb_Fnu.squeeze() / 1e20
    bbb_Fnu_norm = bbb_Fnu.squeeze() / 1e60
    gal_Fnu_norm = gal_Fnu.squeeze() / 1e18
    tor_Fnu_norm = tor_Fnu.squeeze() / 1e-40

    # Total SED sum
    # --------------------------------------------------------------------

    lum = 10 ** (SB) * sb_Fnu_norm + 10 ** (BB) * bbb_Fnu_norm + 10 ** (GA) * gal_Fnu_norm + (10 ** TO) * tor_Fnu_norm

    # --------------------------------------------------------------------

    lum = lum.reshape((np.size(lum),))

    return lum, bands, 10 ** (GA) * gal_Fnu_norm
def MOCKdata (z, dict_modelfiles, dict_modelfluxes, parameters):

	all_tau, all_age, all_nh, all_irlum, filename_0_galaxy, filename_0_starburst, filename_0_torus = dict_modelsfiles
	STARBURSTFdict , BBBFdict, GALAXYFdict, TORUSFdict, EBVbbb_array, EBVgal_array = dict_modelfluxes
	tau, age, nh, irlum, SB ,BB, GA,TO, BBebv, GAebv = parameters


	SB_filename = model.pick_STARBURST_template(irlum, filename_0_starburst, all_irlum)
	GA_filename = model.pick_GALAXY_template(tau, age, filename_0_galaxy, all_tau, all_age)
	TOR_filename = model.pick_TORUS_template(nh, all_nh, filename_0_torus)
	BB_filename = model.pick_BBB_template()

	EBV_bbb_0 = model.pick_EBV_grid(EBVbbb_array, BBebv)
	EBV_bbb = (  str(int(EBV_bbb_0)) if  float(EBV_bbb_0).is_integer() else str(EBV_bbb_0))
	EBV_gal_0 = model.pick_EBV_grid(EBVgal_array,GAebv)
	EBV_gal = (  str(int(EBV_gal_0)) if  float(EBV_gal_0).is_integer() else str(EBV_gal_0)) 

	if (GA_filename, EBV_gal) in GALAXYFdict:
		bands, gal_Fnu = GALAXYFdict[GA_filename, EBV_gal]
	else:	
		print 'Error: Dictionary does not contain key of ', GA_filename, EBV_gal, ' or the E(B-V) grid or the DICTIONARIES_AGNfitter file does not match when the one used in PARAMETERSPACE_AGNfitter/ymodel.py'

	if SB_filename in STARBURSTFdict:	
		bands, sb_Fnu= STARBURSTFdict[SB_filename]
	else:
		print 'Error: Dictionary does not contain key'+SB_filename+'. all STARBURST files or the E(B-V) grid.'

	if (BB_filename, EBV_bbb) in BBBFdict:
		bands, bbb_Fnu = BBBFdict[BB_filename, EBV_bbb]	
	else:
		print'Error: Dictionary does not contain key: '+ BB_filename, EBV_bbb +'or the E(B-V) grid of the DICTIONARIES_AGNfitter file does not match when the one used in PARAMETERSPACE_AGNfitter/ymodel.py'

	if TOR_filename in TORUSFdict:
		bands, tor_Fnu= TORUSFdict[TOR_filename]
	else:
		print 'Error: Dictionary does not contain TORUS file:'+TOR_filename

	NormalizationFLux = 1e-28 # This is the flux to which the models are normalized to  have comparable NORM factors
	sb_Fnu_norm = sb_Fnu / 1e18 
	bbb_Fnu_norm = bbb_Fnu / 1e58
	gal_Fnu_norm = gal_Fnu/ 1e12
	tor_Fnu_norm = tor_Fnu/  1e-42 

	# Sum components
	# ---------------------------------------------------------------------------------------------------------------------------------------------------------#
	lum =    10**(SB)* sb_Fnu_norm      +     10**(BB)*bbb_Fnu_norm    +     (10**GA)*gal_Fnu_norm     +     10**TO *tor_Fnu_norm 

	#-----------------------------------------------------------------------------------------------------------------------------------------------------------
	mockfluxes_array = lum.reshape((np.size(lum),))	

	return bands, mockfluxes_array
Example #4
0
def fluxes_arrays(data_nus, catalog, sourceline, dict_modelsfiles, filterdict, chain, Nrealizations, path, dict_modelfluxes, mock_input):
    """
	This function constructs the luminosities arrays for many realizations from the parameter values

	## inputs:
	- v: frequency 
	- catalog file name
	- sourcelines

	## output:
	- dictionary P with all parameter characteristics
    """
 


#LIST OF OUTPUT

    SBFnu_list = []
    BBFnu_list = []
    GAFnu_list= []
    TOFnu_list = []
    TOTALFnu_list = []
    BBFnu_deredd_list = []
    filtered_modelpoints_list = []


    STARBURSTFdict , BBBFdict, GALAXYFdict, TORUSFdict, EBVbbb_array, EBVgal_array = dict_modelfluxes


    all_tau, all_age, all_nh, all_irlum, filename_0_galaxy, filename_0_starburst, filename_0_torus = dict_modelsfiles


    nsample, npar = chain.shape
    source = NAME(catalog, sourceline)

#CALL PARAMETERS FROM INPUT

    itau, iage, inh, iirlum, iSB ,iBB, iGA ,iTO, iBBebv, iGAebv= mock_input[sourceline] #calling parameter
    

#CALL PARAMETERS OF OUTPUT
    tau, agelog, nh, irlum, SB ,BB, GA,TO, BBebv0, GAebv0= [ chain[:,i] for i in range(npar)] #calling parameters


    
    z = REDSHIFT(catalog, sourceline)
    age = 10**agelog

    agelog = np.log10(age)	
    iagelog = np.log10(iage)

#LEFT PLOT

    for inp in range(1):
        

        SB_filename = path + model.pick_STARBURST_template(iirlum, filename_0_starburst, all_irlum)
        GA_filename = path + model.pick_GALAXY_template(itau, iage, filename_0_galaxy, all_tau, all_age)
        TO_filename = path + model.pick_TORUS_template(inh, all_nh, filename_0_torus)
        BB_filename = path + model.pick_BBB_template()

        all_model_nus = np.arange(12, 16, 0.001)#np.log10(dicts.stack_all_model_nus(filename_0_galaxy, filename_0_starburst, filename_0_torus, z, path ))  

        gal_nu, gal_nored_Fnu = model.GALAXY_read_4plotting( GA_filename, all_model_nus)
        gal_nu, gal_Fnu_red = model.GALAXY_nf2( gal_nu, gal_nored_Fnu, iGAebv )
        all_gal_nus, all_gal_Fnus =gal_nu, gal_Fnu_red

        sb_nu0, sb_Fnu0 = model.STARBURST_read_4plotting(SB_filename, all_model_nus)
        all_sb_nus, all_sb_Fnus = sb_nu0, sb_Fnu0

        bbb_nu, bbb_nored_Fnu = model.BBB_read_4plotting(BB_filename, all_model_nus)
        all_bbb_nus, all_bbb_Fnus = model.BBB_nf2(bbb_nu, bbb_nored_Fnu, iBBebv, z )
        all_bbb_nus, all_bbb_Fnus_deredd =all_bbb_nus, bbb_nored_Fnu

        tor_nu0, tor_Fnu0 = model.TORUS_read_4plotting(TO_filename, z, all_model_nus)
        all_tor_nus, all_tor_Fnus = tor_nu0, tor_Fnu0

        par_input = itau, iagelog, inh, iirlum, iSB ,iBB, iGA ,iTO, iBBebv, iGAebv


        ifiltered_modelpoints = parspace.ymodel(data_nus, z, dict_modelsfiles, dict_modelfluxes, *par_input)


        if len(all_gal_nus)==len(all_sb_nus) and len(all_sb_nus)==len(all_bbb_nus) and len(all_tor_nus)==len(all_bbb_nus) :
            nu= all_gal_nus

            all_sb_Fnus_norm = all_sb_Fnus /1e20
            all_bbb_Fnus_norm = all_bbb_Fnus / 1e60
            all_gal_Fnus_norm = all_gal_Fnus/ 1e18
            all_tor_Fnus_norm = all_tor_Fnus/  1e-40
            all_bbb_Fnus_deredd_norm = all_bbb_Fnus_deredd / 1e60


            iSBFnu =   all_sb_Fnus_norm *10**float(iSB) 
            iBBFnu =  all_bbb_Fnus_norm * 10**float(iBB) 
            iGAFnu =   all_gal_Fnus_norm * 10**float(iGA) 
            iTOFnu =   all_tor_Fnus_norm * 10**float(iTO)# /(1+z)
            iBBFnu_deredd = all_bbb_Fnus_deredd_norm* 10**float(iBB)

            iTOTALFnu =    iSBFnu + iBBFnu + iGAFnu + iTOFnu
         

#RIGHT PLOT
    			
    for gi in range(Nrealizations): #LOOP for a 100th part of the realizations

        g= gi*(nsample/Nrealizations)



        BBebv1 = model.pick_EBV_grid(EBVbbb_array, BBebv0[g])
        BBebv2 = (  str(int(BBebv1)) if  float(BBebv1).is_integer() else str(BBebv1))
        GAebv1 = model.pick_EBV_grid(EBVgal_array,GAebv0[g])
        GAebv2 = (  str(int(GAebv1)) if  float(GAebv1).is_integer() else str(GAebv1))
    

        SB_filename = path + model.pick_STARBURST_template(irlum[g], filename_0_starburst, all_irlum)
        GA_filename = path + model.pick_GALAXY_template(tau[g], age[g], filename_0_galaxy, all_tau, all_age)
        TO_filename = path + model.pick_TORUS_template(nh[g], all_nh, filename_0_torus)
        BB_filename = path + model.pick_BBB_template()


        all_model_nus = np.arange(12, 16, 0.001)#np.log10(dicts.stack_all_model_nus(filename_0_galaxy, filename_0_starburst, filename_0_torus, z, path ))  
        gal_nu, gal_nored_Fnu = model.GALAXY_read_4plotting( GA_filename, all_model_nus)
        gal_nu, gal_Fnu_red = model.GALAXY_nf2( gal_nu, gal_nored_Fnu, float(GAebv2))
        all_gal_nus, all_gal_Fnus =gal_nu, gal_Fnu_red


        sb_nu0, sb_Fnu0 = model.STARBURST_read_4plotting(SB_filename, all_model_nus)
        all_sb_nus, all_sb_Fnus = sb_nu0, sb_Fnu0

        bbb_nu, bbb_nored_Fnu = model.BBB_read_4plotting(BB_filename, all_model_nus)
        bbb_nu0, bbb_Fnu_red = model.BBB_nf2(bbb_nu, bbb_nored_Fnu, float(BBebv2), z )
        all_bbb_nus, all_bbb_Fnus = bbb_nu0, bbb_Fnu_red
        all_bbb_nus, all_bbb_Fnus_deredd = bbb_nu0, bbb_nored_Fnu

        tor_nu0, tor_Fnu0 = model.TORUS_read_4plotting(TO_filename, z, all_model_nus)
        all_tor_nus, all_tor_Fnus = tor_nu0, tor_Fnu0

        par1 = tau[g], agelog[g], nh[g], irlum[g], SB[g] ,BB[g], GA[g] ,TO[g], float(BBebv2), float(GAebv2)
        filtered_modelpoints = parspace.ymodel(data_nus, z, dict_modelsfiles, dict_modelfluxes, *par1)


        if len(all_gal_nus)==len(all_sb_nus) and len(all_sb_nus)==len(all_bbb_nus) and len(all_tor_nus)==len(all_bbb_nus) :
            nu= all_gal_nus

           
            all_sb_Fnus_norm = all_sb_Fnus /1e20
            all_bbb_Fnus_norm = all_bbb_Fnus / 1e60
            all_gal_Fnus_norm = all_gal_Fnus/ 1e18
            all_tor_Fnus_norm = all_tor_Fnus/  1e-40
            all_bbb_Fnus_deredd_norm = all_bbb_Fnus_deredd / 1e60


            SBFnu =   all_sb_Fnus_norm *10**float(SB[g]) 
            BBFnu =  all_bbb_Fnus_norm * 10**float(BB[g]) 
            GAFnu =   all_gal_Fnus_norm * 10**float(GA[g])
            TOFnu =   all_tor_Fnus_norm * 10**float(TO[g]) #/(1+z)
            BBFnu_deredd = all_bbb_Fnus_deredd_norm* 10**float(BB[g])

            TOTALFnu =    SBFnu + BBFnu + GAFnu + TOFnu
            SBFnu_list.append(SBFnu)
            BBFnu_list.append(BBFnu)
            GAFnu_list.append(GAFnu)
            TOFnu_list.append(TOFnu)
            TOTALFnu_list.append(TOTALFnu)
            BBFnu_deredd_list.append(BBFnu_deredd)
            filtered_modelpoints_list.append(filtered_modelpoints)


    	else: 
	    	print 'Error:'
	    	print 'The frequencies in the MODELdict_plot dictionaries are not equal for all models and could not be added.'
	    	print 'Check that the dictionary_plot.py stacks all frequencies (bands+galaxy+bbb+sb+torus) properly.'	


	       
    SBFnu_array = np.array(SBFnu_list)
    BBFnu_array = np.array(BBFnu_list)
    GAFnu_array = np.array(GAFnu_list)
    TOFnu_array = np.array(TOFnu_list)
    TOTALFnu_array = np.array(TOTALFnu_list)
    BBFnu_array_deredd = np.array(BBFnu_deredd_list)	
    filtered_modelpoints = np.array(filtered_modelpoints_list)
    

    FLUXES4plotting = (SBFnu_array, BBFnu_array, GAFnu_array, TOFnu_array, TOTALFnu_array,BBFnu_array_deredd, iSBFnu, iBBFnu, iGAFnu, iTOFnu, iTOTALFnu,iBBFnu_deredd)


    return all_model_nus, FLUXES4plotting, filtered_modelpoints, ifiltered_modelpoints
    def fluxes(self, data):    

        """
        This is the main function of the class.
        """
        self.chain_obj.props()

        SBFnu_list = []
        BBFnu_list = []
        GAFnu_list= []
        TOFnu_list = []
        TOTALFnu_list = []
        BBFnu_deredd_list = []
        if self.output_type == 'plot':
            filtered_modelpoints_list = []


        gal_do,  irlum_dict, nh_dict, BBebv_dict,_ = data.dictkey_arrays
        # Take the last 4 dictionaries, which are for plotting. (the first 4 were at bands)
        _,_,_,_,STARBURSTFdict , BBBFdict, GALAXYFdict, TORUSFdict,_= data.dict_modelfluxes

        nsample, npar = self.chain_obj.flatchain.shape
        source = data.name

        if self.output_type == 'plot':
            tau, agelog, nh, irlum, SB ,BB, GA,TO, BBebv, GAebv= self.chain_obj.flatchain[np.random.choice(nsample, (self.out['realizations2plot'])),:].T
        elif self.output_type == 'int_lums':
            tau, agelog, nh, irlum, SB ,BB, GA,TO, BBebv, GAebv= self.chain_obj.flatchain[np.random.choice(nsample, (self.out['realizations2int'])),:].T
        elif self.output_type == 'best_fit':
            tau, agelog, nh, irlum, SB ,BB, GA,TO, BBebv, GAebv= self.best_fit_pars

        age = 10**agelog

        self.all_nus_rest = np.arange(11.5, 16, 0.001) 

        for g in range(len(tau)):


            # Pick dictionary key-values, nearest to the MCMC- parameter values
            irlum_dct = model.pick_STARBURST_template(irlum[g], irlum_dict)
            nh_dct = model.pick_TORUS_template(nh[g], nh_dict)
            ebvbbb_dct = model.pick_BBB_template(BBebv[g], BBebv_dict)
            gal_do.nearest_par2dict(tau[g], age[g], GAebv[g])
            tau_dct, age_dct, ebvg_dct=gal_do.t, gal_do.a,gal_do.e

            #Produce model fluxes at all_nus_rest for plotting, through interpolation
            all_gal_nus, gal_Fnus = GALAXYFdict[tau_dct, age_dct,ebvg_dct]   
            GAinterp = scipy.interpolate.interp1d(all_gal_nus, gal_Fnus, bounds_error=False, fill_value=0.)
            all_gal_Fnus = GAinterp(self.all_nus_rest)

            all_sb_nus, sb_Fnus= STARBURSTFdict[irlum_dct] 
            SBinterp = scipy.interpolate.interp1d(all_sb_nus, sb_Fnus, bounds_error=False, fill_value=0.)
            all_sb_Fnus = SBinterp(self.all_nus_rest)

            all_bbb_nus, bbb_Fnus = BBBFdict[ebvbbb_dct] 
            BBinterp = scipy.interpolate.interp1d(all_bbb_nus, bbb_Fnus, bounds_error=False, fill_value=0.)
            all_bbb_Fnus = BBinterp(self.all_nus_rest)

            all_bbb_nus, bbb_Fnus_deredd = BBBFdict['0.0']
            BBderedinterp = scipy.interpolate.interp1d(all_bbb_nus, bbb_Fnus_deredd, bounds_error=False, fill_value=0.)
            all_bbb_Fnus_deredd = BBderedinterp(self.all_nus_rest)

            all_tor_nus, tor_Fnus= TORUSFdict[nh_dct]
            TOinterp = scipy.interpolate.interp1d(all_tor_nus, np.log10(tor_Fnus), bounds_error=False, fill_value=0.)
            all_tor_Fnus = 10**(TOinterp(self.all_nus_rest))        


            if self.output_type == 'plot':
                par2 = tau[g], agelog[g], nh[g], irlum[g], SB[g] ,BB[g], GA[g] ,TO[g], BBebv[g], GAebv[g]
                filtered_modelpoints, _, _ = parspace.ymodel(data.nus,data.z, data.dictkey_arrays, data.dict_modelfluxes, *par2)
                

            #Using the costumized normalization 
            SBFnu =   (all_sb_Fnus /1e20) *10**float(SB[g]) 
            BBFnu = (all_bbb_Fnus /1e60) * 10**float(BB[g]) 
            GAFnu =   (all_gal_Fnus/ 1e18) * 10**float(GA[g]) 
            TOFnu =   (all_tor_Fnus/  1e-40) * 10**float(TO[g])
            BBFnu_deredd = (all_bbb_Fnus_deredd /1e60) * 10**float(BB[g])

            TOTALFnu =  SBFnu + BBFnu + GAFnu + TOFnu
            
            #Append to the list for all realizations
            SBFnu_list.append(SBFnu)
            BBFnu_list.append(BBFnu)
            GAFnu_list.append(GAFnu)
            TOFnu_list.append(TOFnu)
            TOTALFnu_list.append(TOTALFnu)
            BBFnu_deredd_list.append(BBFnu_deredd)
            #Only if SED plotting: do the same with the  modelled flux values at each data point 
            if self.output_type == 'plot':
                filtered_modelpoints_list.append(filtered_modelpoints)


        #Convert lists into Numpy arrays
        SBFnu_array = np.array(SBFnu_list)
        BBFnu_array = np.array(BBFnu_list)
        GAFnu_array = np.array(GAFnu_list)
        TOFnu_array = np.array(TOFnu_list)
        TOTALFnu_array = np.array(TOTALFnu_list)
        BBFnu_array_deredd = np.array(BBFnu_deredd_list)    

        #Put them all together to transport
        FLUXES4plotting = (SBFnu_array, BBFnu_array, GAFnu_array, TOFnu_array, TOTALFnu_array,BBFnu_array_deredd)
        #Convert Fluxes to nuLnu
        self.nuLnus4plotting = self.FLUXES2nuLnu_4plotting(self.all_nus_rest, FLUXES4plotting, data.z)

        #Only if SED plotting:
        if self.output_type == 'plot':
            filtered_modelpoints = np.array(filtered_modelpoints_list)
            distance= model.z2Dlum(data.z)
            lumfactor = (4. * math.pi * distance**2.)
            self.filtered_modelpoints_nuLnu = (filtered_modelpoints *lumfactor* 10**(data.nus))
        #Only if calculating integrated luminosities:    
        elif self.output_type == 'int_lums':
            self.int_lums= np.log10(self.integrated_luminosities(self.out ,self.all_nus_rest, self.nuLnus4plotting))
    def fluxes(self, data):
        """
        This is the main function of the class.
        """
        self.chain_obj.props()

        SBFnu_list = []
        BBFnu_list = []
        GAFnu_list = []
        TOFnu_list = []
        TOTALFnu_list = []
        BBFnu_deredd_list = []
        if self.output_type == 'plot':
            filtered_modelpoints_list = []

        gal_do, irlum_dict, nh_dict, BBebv_dict, _ = data.dictkey_arrays
        # Take the last 4 dictionaries, which are for plotting. (the first 4 were at bands)
        _, _, _, _, STARBURSTFdict, BBBFdict, GALAXYFdict, TORUSFdict, _ = data.dict_modelfluxes

        nsample, npar = self.chain_obj.flatchain.shape
        source = data.name

        if self.output_type == 'plot':
            tau, agelog, nh, irlum, SB, BB, GA, TO, BBebv, GAebv = self.chain_obj.flatchain[
                np.random.choice(nsample,
                                 (self.out['realizations2plot'])), :].T
        elif self.output_type == 'int_lums':
            tau, agelog, nh, irlum, SB, BB, GA, TO, BBebv, GAebv = self.chain_obj.flatchain[
                np.random.choice(nsample, (self.out['realizations2int'])), :].T
        elif self.output_type == 'best_fit':
            tau, agelog, nh, irlum, SB, BB, GA, TO, BBebv, GAebv = self.best_fit_pars

        age = 10**agelog

        self.all_nus_rest = np.arange(11.5, 16, 0.001)

        for g in range(len(tau)):

            # Pick dictionary key-values, nearest to the MCMC- parameter values
            irlum_dct = model.pick_STARBURST_template(irlum[g], irlum_dict)
            nh_dct = model.pick_TORUS_template(nh[g], nh_dict)
            ebvbbb_dct = model.pick_BBB_template(BBebv[g], BBebv_dict)
            gal_do.nearest_par2dict(tau[g], age[g], GAebv[g])
            tau_dct, age_dct, ebvg_dct = gal_do.t, gal_do.a, gal_do.e

            #Produce model fluxes at all_nus_rest for plotting, through interpolation
            all_gal_nus, gal_Fnus = GALAXYFdict[tau_dct, age_dct, ebvg_dct]
            GAinterp = scipy.interpolate.interp1d(all_gal_nus,
                                                  gal_Fnus,
                                                  bounds_error=False,
                                                  fill_value=0.)
            all_gal_Fnus = GAinterp(self.all_nus_rest)

            all_sb_nus, sb_Fnus = STARBURSTFdict[irlum_dct]
            SBinterp = scipy.interpolate.interp1d(all_sb_nus,
                                                  sb_Fnus,
                                                  bounds_error=False,
                                                  fill_value=0.)
            all_sb_Fnus = SBinterp(self.all_nus_rest)

            all_bbb_nus, bbb_Fnus = BBBFdict[ebvbbb_dct]
            BBinterp = scipy.interpolate.interp1d(all_bbb_nus,
                                                  bbb_Fnus,
                                                  bounds_error=False,
                                                  fill_value=0.)
            all_bbb_Fnus = BBinterp(self.all_nus_rest)

            all_bbb_nus, bbb_Fnus_deredd = BBBFdict['0.0']
            BBderedinterp = scipy.interpolate.interp1d(all_bbb_nus,
                                                       bbb_Fnus_deredd,
                                                       bounds_error=False,
                                                       fill_value=0.)
            all_bbb_Fnus_deredd = BBderedinterp(self.all_nus_rest)

            all_tor_nus, tor_Fnus = TORUSFdict[nh_dct]
            TOinterp = scipy.interpolate.interp1d(all_tor_nus,
                                                  np.log10(tor_Fnus),
                                                  bounds_error=False,
                                                  fill_value=0.)
            all_tor_Fnus = 10**(TOinterp(self.all_nus_rest))

            if self.output_type == 'plot':
                par2 = tau[g], agelog[g], nh[g], irlum[g], SB[g], BB[g], GA[
                    g], TO[g], BBebv[g], GAebv[g]
                filtered_modelpoints, _, _ = parspace.ymodel(
                    data.nus, data.z, data.dictkey_arrays,
                    data.dict_modelfluxes, *par2)

            #Using the costumized normalization
            SBFnu = (all_sb_Fnus / 1e20) * 10**float(SB[g])
            BBFnu = (all_bbb_Fnus / 1e60) * 10**float(BB[g])
            GAFnu = (all_gal_Fnus / 1e18) * 10**float(GA[g])
            TOFnu = (all_tor_Fnus / 1e-40) * 10**float(TO[g])
            BBFnu_deredd = (all_bbb_Fnus_deredd / 1e60) * 10**float(BB[g])

            TOTALFnu = SBFnu + BBFnu + GAFnu + TOFnu

            #Append to the list for all realizations
            SBFnu_list.append(SBFnu)
            BBFnu_list.append(BBFnu)
            GAFnu_list.append(GAFnu)
            TOFnu_list.append(TOFnu)
            TOTALFnu_list.append(TOTALFnu)
            BBFnu_deredd_list.append(BBFnu_deredd)
            #Only if SED plotting: do the same with the  modelled flux values at each data point
            if self.output_type == 'plot':
                filtered_modelpoints_list.append(filtered_modelpoints)

        #Convert lists into Numpy arrays
        SBFnu_array = np.array(SBFnu_list)
        BBFnu_array = np.array(BBFnu_list)
        GAFnu_array = np.array(GAFnu_list)
        TOFnu_array = np.array(TOFnu_list)
        TOTALFnu_array = np.array(TOTALFnu_list)
        BBFnu_array_deredd = np.array(BBFnu_deredd_list)

        #Put them all together to transport
        FLUXES4plotting = (SBFnu_array, BBFnu_array, GAFnu_array, TOFnu_array,
                           TOTALFnu_array, BBFnu_array_deredd)
        #Convert Fluxes to nuLnu
        self.nuLnus4plotting = self.FLUXES2nuLnu_4plotting(
            self.all_nus_rest, FLUXES4plotting, data.z)

        #Only if SED plotting:
        if self.output_type == 'plot':
            filtered_modelpoints = np.array(filtered_modelpoints_list)
            distance = model.z2Dlum(data.z)
            lumfactor = (4. * math.pi * distance**2.)
            self.filtered_modelpoints_nuLnu = (filtered_modelpoints *
                                               lumfactor * 10**(data.nus))
        #Only if calculating integrated luminosities:
        elif self.output_type == 'int_lums':
            self.int_lums = np.log10(
                self.integrated_luminosities(self.out, self.all_nus_rest,
                                             self.nuLnus4plotting))