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']
			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))
Exemplo n.º 2
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
Exemplo n.º 3
0
    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_obj, sb_obj, tor_obj, bbb_obj = data.dictkey_arrays

        # Take the  4 dictionaries for plotting. Dicts are described in DICTIONARIES_AGNfitter.py
        _, _, _, _, STARBURSTFdict, BBBFdict, GALAXYFdict, TORUSFdict, _, _ = data.dict_modelfluxes

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

        if self.output_type == 'plot':
            par = self.chain_obj.flatchain[np.random.choice(
                nsample, (self.out['realizations2plot'])), :]  #.T
        elif self.output_type == 'int_lums':
            par = self.chain_obj.flatchain[np.random.choice(
                nsample, (self.out['realizations2int'])), :]  #.T
        elif self.output_type == 'best_fit':
            par = self.chain_obj.best_fit_pars

        if self.models['BBB'] == 'D12_S' or self.models['BBB'] == 'D12_K':
            self.all_nus_rest = np.arange(11.5, 19, 0.001)
        else:
            self.all_nus_rest = np.arange(11.5, 16.2, 0.001)

        if self.output_type == 'plot' or self.output_type == 'int_lums':

            for g in range(self.out['realizations2plot']):

                ## Pick dictionary key-values, nearest to the MCMC- parameter values
                ## Use pick_nD if model has more than one parameter,
                ## and pick_1D if it has only one.
                gal_obj.pick_nD(par[g][self.P['idxs'][0]:self.P['idxs'][1]])
                tor_obj.pick_1D(par[g][self.P['idxs'][2]:self.P['idxs'][3]])

                if len(sb_obj.par_names) == 1:
                    sb_obj.pick_1D(par[g][self.P['idxs'][1]:self.P['idxs'][2]])
                    all_sb_nus, sb_Fnus = STARBURSTFdict[
                        sb_obj.matched_parkeys]
                else:
                    sb_obj.pick_nD(par[g][self.P['idxs'][1]:self.P['idxs'][2]])
                    all_sb_nus, sb_Fnus = STARBURSTFdict[tuple(
                        sb_obj.matched_parkeys)]

                if len(bbb_obj.par_names) == 1:
                    GA, SB, TO, BB = par[g][-4:]
                    bbb_obj.pick_1D(
                        par[g][self.P['idxs'][3]:self.P['idxs'][4]])
                    all_bbb_nus, bbb_Fnus = BBBFdict[bbb_obj.matched_parkeys]
                #print bbb_Fnus
                else:
                    GA, SB, TO = par[g][-3:]
                    BB = 0.
                    bbb_obj.pick_nD(
                        par[g][self.P['idxs'][3]:self.P['idxs'][4]])
                    all_bbb_nus, bbb_Fnus = BBBFdict[tuple(
                        bbb_obj.matched_parkeys)]

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

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

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

                ### Plot dereddened
                if len(bbb_obj.par_names) == 1:
                    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)
                else:
                    all_bbb_Fnus_deredd = all_bbb_Fnus

                all_tor_nus, tor_Fnus = TORUSFdict[tor_obj.matched_parkeys]
                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))
                all_tor_Fnus[self.all_nus_rest > 16] = 0

                if self.output_type == 'plot':
                    par2 = par[g]
                    #print data.dictkey_arrays
                    filtered_modelpoints, _, _ = parspace.ymodel(
                        data.nus, data.z, data.dlum, data.dictkey_arrays,
                        data.dict_modelfluxes, self.P, *par2)
                    #print filtered_modelpoints

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

                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)

        elif self.output_type == 'best_fit':

            ## Pick dictionary key-values, nearest to the MCMC- parameter values
            ## Use pick_nD if model has more than one parameter,
            ## and pick_1D if it has only one.
            gal_obj.pick_nD(par[self.P['idxs'][0]:self.P['idxs'][1]])
            tor_obj.pick_1D(par[self.P['idxs'][2]:self.P['idxs'][3]])

            if len(sb_obj.par_names) == 1:
                sb_obj.pick_1D(par[self.P['idxs'][1]:self.P['idxs'][2]])
                all_sb_nus, sb_Fnus = STARBURSTFdict[sb_obj.matched_parkeys]
            else:
                sb_obj.pick_nD(par[self.P['idxs'][1]:self.P['idxs'][2]])
                all_sb_nus, sb_Fnus = STARBURSTFdict[tuple(
                    sb_obj.matched_parkeys)]

            if len(bbb_obj.par_names) == 1:
                GA, SB, TO, BB = par[-4:]
                bbb_obj.pick_1D(par[self.P['idxs'][3]:self.P['idxs'][4]])
                all_bbb_nus, bbb_Fnus = BBBFdict[bbb_obj.matched_parkeys]
            #print bbb_Fnus
            else:
                GA, SB, TO = par[-3:]
                BB = 0.
                bbb_obj.pick_nD(par[self.P['idxs'][3]:self.P['idxs'][4]])
                all_bbb_nus, bbb_Fnus = BBBFdict[tuple(
                    bbb_obj.matched_parkeys)]

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

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

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

            ### Plot dereddened
            if len(bbb_obj.par_names) == 1:
                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)
            else:
                all_bbb_Fnus_deredd = all_bbb_Fnus

            all_tor_nus, tor_Fnus = TORUSFdict[tor_obj.matched_parkeys]
            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))
            all_tor_Fnus[self.all_nus_rest > 16] = 0

            par2 = par
            #print data.dictkey_arrays
            filtered_modelpoints, _, _ = parspace.ymodel(
                data.nus, data.z, data.dlum, data.dictkey_arrays,
                data.dict_modelfluxes, self.P, *par2)
            #print filtered_modelpoints

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

            TOTALFnu = SBFnu + BBFnu + GAFnu + TOFnu

            #Put them all together to transport
            FLUXES4plotting = (SBFnu, BBFnu, GAFnu, TOFnu, TOTALFnu,
                               BBFnu_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))
            #print self.filtered_modelpoints_nuLnu
            #print 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))
        elif self.output_type == 'best_fit':
            distance = model.z2Dlum(data.z)
            lumfactor = (4. * math.pi * distance**2.)
            self.filtered_modelpoints_nuLnu = (filtered_modelpoints *
                                               lumfactor * 10**(data.nus))
Exemplo n.º 4
0
    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))