def fit_multiyso(data, filter_names, apertures, models_topdir, n_data_min=3, extinction_file=None, av_range=None, distance_range=None, output_format=('F', 6.), output_convolved=False, remove_resolved=False, cpd=4.): """ Created on Wed May 8 11:17:09 2019 @author: mspovich Call the sedfitter fit function multiple times to fit various R17 YSO model sets to a given source sample. All arguments in call are passed along to the fit module. """ # Extinction law extinction = Extinction.from_file(extinction_file, columns=[0, 3], wav_unit=u.micron, chi_unit=u.cm**2 / u.g) # DISK-only models model_dir_spsi = models_topdir + '/01_sp--s-i' model_dir_sphi = models_topdir + '/02_sp--h-i' # Disks with inner holes at Rsublimation.. fit(data, filter_names, apertures, model_dir_spsi, '01_sp--s-i.fitinfo', n_data_min=3, extinction_law=extinction, distance_range=distance_range, av_range=av_range, output_format=output_format) # Disks with varying inner hole size.. fit(data, filter_names, apertures, model_dir_sphi, '02_sp--h-i.fitinfo', n_data_min=3, extinction_law=extinction, distance_range=distance_range, av_range=av_range, output_format=output_format) # DISK+ENVELOPE models model_dir_spusmi = models_topdir + '/14_spu-smi' model_dir_spuhmi = models_topdir + '/15_spu-hmi' # Disks+Ulrich envelopes with inner holes fit(data, filter_names, apertures, model_dir_spusmi, '14_spu-smi.fitinfo', n_data_min=3, extinction_law=extinction, distance_range=distance_range, av_range=av_range, output_format=output_format) fit(data, filter_names, apertures, model_dir_spuhmi, '15_spu-hmi.fitinfo', n_data_min=3, extinction_law=extinction, distance_range=distance_range, av_range=av_range, output_format=output_format) # DISK+ENVELOPE+BipolarCavity models model_dir_spubsmi = models_topdir + '/16_spubsmi' model_dir_spubhmi = models_topdir + '/17_spubhmi' # Disks+Ulrich envelopes with inner holes and bipolar cavities fit(data, filter_names, apertures, model_dir_spubsmi, '16_spubsmi.fitinfo', n_data_min=3, extinction_law=extinction, distance_range=distance_range, av_range=av_range, output_format=output_format) fit(data, filter_names, apertures, model_dir_spubhmi, '17_spubhmi.fitinfo', n_data_min=3, extinction_law=extinction, distance_range=distance_range, av_range=av_range, output_format=output_format) # Splitting output to identify well-fit sources. print("Splitting output into well-fit vs. poorly-fit using cpd = %4.1f" % cpd) filter_output('01_sp--s-i.fitinfo', cpd=cpd) filter_output('02_sp--h-i.fitinfo', cpd=cpd) filter_output('14_spu-smi.fitinfo', cpd=cpd) filter_output('15_spu-hmi.fitinfo', cpd=cpd) filter_output('16_spubsmi.fitinfo', cpd=cpd) filter_output('17_spubhmi.fitinfo', cpd=cpd) #Write out parameters of WELL-FIT models from each set #for subsequent cross-set analysis cpdstr = '{:02.0f}'.format(cpd) print("Writing ASCII parameter files pars_*g" + cpdstr + ".txt") write_parameters('01_sp--s-i.fitinfo_good', 'pars_01g' + cpdstr + '.txt', select_format=('A', -1)) write_parameters('02_sp--h-i.fitinfo_good', 'pars_02g' + cpdstr + '.txt', select_format=('A', -1)) write_parameters('14_spu-smi.fitinfo_good', 'pars_14g' + cpdstr + '.txt', select_format=('A', -1)) write_parameters('15_spu-hmi.fitinfo_good', 'pars_15g' + cpdstr + '.txt', select_format=('A', -1)) write_parameters('16_spubsmi.fitinfo_good', 'pars_16g' + cpdstr + '.txt', select_format=('A', -1)) write_parameters('17_spubhmi.fitinfo_good', 'pars_17g' + cpdstr + '.txt', select_format=('A', -1))
def fit_all_sources(sourcetable): ''' Wrap around Robitaille's fitting routine ''' from sedfitter.extinction import Extinction # load up extinction law as suggested by website; might switch over to other dust files extinction = Extinction.from_file(SEDfolder+'extinction_law.ascii',columns=[0,1],wav_unit=u.micron,chi_unit=u.cm**2/u.g) # only fit the isolated and clustered sources types = sourcetable.group_by('Property') totsourcetable = vstack([types.groups[0],types.groups[2]]) # list of columns with fluxes to be used to fit columnlist = ['j','h','ks','i1','i2','i3','i4','F11','F19','m1','F31','F37','m2','S450','S850','H70','H160','H250','H350','H500'] errorlist = ["e_"+col for col in columnlist] flaglist = ["flag_"+col for col in columnlist if "flag_"+col in totsourcetable.columns] # set up input table to be fed to sedfitter # fluxlist = [] # for col in columnlist: # fluxlist.append(col);fluxlist.append('e_'+col) # cluster names and distances fieldlist = Table(names=["CepA","CepC","IRAS20050","NGC2264","NGC1333","NGC7129","Oph","S140","S171","NGC2071"]) fieldlist.add_row([730,730,700,760,240,1000,160,900,850,490]) # for each cluster clusters = totsourcetable.group_by('Cluster') for key,group in izip(clusters.groups.keys,clusters.groups): # isolate source and use only relevant columns newsourcetable = group[['SOFIA_name','RA','DEC']+columnlist+errorlist+flaglist] print "Working on cluster ",key['Cluster'] print newsourcetable # number of sources in that cluster L = len(newsourcetable) # for each column for col in columnlist: # re-check that there are no negative fluxes that are not masked for i in range(len(newsourcetable)): if newsourcetable[col][i]<0: newsourcetable.mask[col][i] = True # create the flag table based on mask newsourcetable["flag"+col] = np.ma.array(~newsourcetable[col].mask).astype(int) # make sure the errors are flagged as well newsourcetable['e_'+col].mask = newsourcetable[col].mask # multiplies the flag by 3 for the Herschel fluxes that we know are taken with a very large aperture for col in ['H250','H350','H500']: newsourcetable["flag"+col] *= 3 for col in columnlist: for i in range(len(newsourcetable)): # if flag string contains a 'U', then the flux is an upper limit if 'flag_'+col in newsourcetable.columns: if 'U' in newsourcetable['flag_'+col][i] and newsourcetable["flag"+col][i] != 3: newsourcetable["flag"+col][i] *= 3 # convert to mJy for col in columnlist: newsourcetable[col] *= 1000.0 newsourcetable['e_'+col] *= 1000.0 # creates the list of columns containing the flags newflaglist = ["flag"+col for col in columnlist] # create proper flux;error lists fluxlist = [] for col in columnlist: fluxlist.append(col);fluxlist.append('e_'+col) # creates the table in proper format for feeding to sedfitter final = newsourcetable[['SOFIA_name','RA','DEC']+newflaglist+fluxlist].filled(-1.0) final.write(folder_export+key['Cluster']+'_sedfitter.tab',format='ascii') # remove first line (column headers) os.system('sed -i -e "1d" %s' % (folder_export+key['Cluster']+'_sedfitter.tab')) # name of the filter names for use in sedfitter sednamelist = ['2J','2H','2K','I1','I2','I3','I4','F11','F19','M1','F31','F37','M2','W1','W2','H70','H160','H250','H350','H500'] # list of aperture sizes apertures = [8,8,8,12,12,12,12,9,9,35,9,9,45,20,40,12,22,22,30,42] *u.arcsec ### CHECK APERTURE SIZES ,12,22,22,30,42 # distance to the current cluster distance = fieldlist[key['Cluster']][0] # cleans up the directories before starting os.system('rm -rf %s' % (folder_export+'plot_'+key['Cluster'])) os.system('rm %s' % (SEDfolder+'output_'+key['Cluster']+'.fitinfo')) # fitting routine fit(folder_export+key['Cluster']+'_sedfitter.tab',sednamelist,apertures, model_dir,SEDfolder+'output_'+key['Cluster']+'.fitinfo', extinction_law = extinction, distance_range = [distance*0.8,distance*1.2] * u.pc, av_range = [0.,10]) print "Generating some plots" plot(SEDfolder+'output_'+key['Cluster']+'.fitinfo',folder_export+'plot_'+key['Cluster'],select_format=('N',10.)) #plot_params_1d(SEDfolder+'output_'+key['Cluster']+'.fitinfo','MDISK',output_dir=folder_export+'plot_'+key['Cluster']+"/1d",log_x=True,select_format=('F',3.)) print "Extracting the parameters from the fits" write_parameters(SEDfolder+'output_'+key['Cluster']+'.fitinfo',folder_export+'plot_'+key['Cluster']+'/params.txt',select_format=('N',10.)) write_parameter_ranges(SEDfolder+'output_'+key['Cluster']+'.fitinfo',folder_export+'plot_'+key['Cluster']+'/params_ranges.txt',select_format=('N',10.)) print "Parsing results..." parse_params_table(folder_export+'plot_'+key['Cluster']) print "Done parsing results"
import os import glob from astropy import units as u from sedfitter import (fit, plot, plot_params_1d, plot_params_2d, write_parameters, write_parameter_ranges) from sedfitter.extinction import Extinction # Define path to models model_dir = '/Volumes/Data/models/models_r06' # Read in extinction law. We read in columns 1 (the wavelength in microns) and # 4 (the opacity in cm^2/g) extinction = Extinction.from_file('kmh94.par', columns=[0, 3], wav_unit=u.micron, chi_unit=u.cm**2 / u.g) # Define filters and apertures filters = ['2J', '2H', '2K', 'I1', 'I2', 'I3', 'I4'] apertures = [3., 3., 3., 3., 3., 3., 3.] * u.arcsec # Run the fitting fit('data_glimpse', filters, apertures, model_dir, 'output.fitinfo', extinction_law=extinction, distance_range=[1., 2.] * u.kpc, av_range=[0., 40.])
plt.savefig(photplotdir + s._name + "_Photometry.png") if do_show: plt.show() #print(s.sedfitterinput()) print("Starting with %d sources" % len(seds)) # ugh sedfitter.fit() only allows one distance so we have to do # each source one at a time. default_av_range = [0., 5.] dust_model = 'whitney.r550.par' topdir = '/n/subaruraid/mpound/' model_dir = topdir + 'sedfittermodels/' #model_dir = '/lupus3/mpound/filter_convolve/' sed_model_dir = model_dir + 'models_r17/s---s-i/' extinction = Extinction.from_file(dust_model, columns=[0, 3], wav_unit=u.micron, chi_unit=u.cm**2 / u.g) if False: sedmodels = SEDCube.read(sed_model_dir + "flux.fits") print(sedmodels.names) print(sedmodels.val.shape) s = sedmodels.get_sed('03ZZRVTe_01') plt.loglog(s.wav, s.flux.transpose(), 'k-', alpha=0.5) plt.loglog(seds[0].wavelengths(), seds[0].fluxes()) plt.show() #plt.ylim(1e-2, 1e8) bad = open(badfile, "w") if do_fit: for s in seds:
def fit_all_sources(sourcetable): ''' Wrap around Robitaille's fitting routine ''' from sedfitter.extinction import Extinction # load up extinction law as suggested by website; might switch over to other dust files extinction = Extinction.from_file(SEDfolder + 'extinction_law.ascii', columns=[0, 1], wav_unit=u.micron, chi_unit=u.cm**2 / u.g) # only fit the isolated and clustered sources types = sourcetable.group_by('Property') totsourcetable = vstack([types.groups[0], types.groups[2]]) # list of columns with fluxes to be used to fit columnlist = [ 'j', 'h', 'ks', 'i1', 'i2', 'i3', 'i4', 'F11', 'F19', 'm1', 'F31', 'F37', 'm2', 'S450', 'S850', 'H70', 'H160', 'H250', 'H350', 'H500' ] errorlist = ["e_" + col for col in columnlist] flaglist = [ "flag_" + col for col in columnlist if "flag_" + col in totsourcetable.columns ] # set up input table to be fed to sedfitter # fluxlist = [] # for col in columnlist: # fluxlist.append(col);fluxlist.append('e_'+col) # cluster names and distances fieldlist = Table(names=[ "CepA", "CepC", "IRAS20050", "NGC2264", "NGC1333", "NGC7129", "Oph", "S140", "S171", "NGC2071" ]) fieldlist.add_row([730, 730, 700, 760, 240, 1000, 160, 900, 850, 490]) # for each cluster clusters = totsourcetable.group_by('Cluster') for key, group in izip(clusters.groups.keys, clusters.groups): # isolate source and use only relevant columns newsourcetable = group[['SOFIA_name', 'RA', 'DEC'] + columnlist + errorlist + flaglist] print "Working on cluster ", key['Cluster'] print newsourcetable # number of sources in that cluster L = len(newsourcetable) # for each column for col in columnlist: # re-check that there are no negative fluxes that are not masked for i in range(len(newsourcetable)): if newsourcetable[col][i] < 0: newsourcetable.mask[col][i] = True # create the flag table based on mask newsourcetable["flag" + col] = np.ma.array( ~newsourcetable[col].mask).astype(int) # make sure the errors are flagged as well newsourcetable['e_' + col].mask = newsourcetable[col].mask # multiplies the flag by 3 for the Herschel fluxes that we know are taken with a very large aperture for col in ['H250', 'H350', 'H500']: newsourcetable["flag" + col] *= 3 for col in columnlist: for i in range(len(newsourcetable)): # if flag string contains a 'U', then the flux is an upper limit if 'flag_' + col in newsourcetable.columns: if 'U' in newsourcetable[ 'flag_' + col][i] and newsourcetable["flag" + col][i] != 3: newsourcetable["flag" + col][i] *= 3 # convert to mJy for col in columnlist: newsourcetable[col] *= 1000.0 newsourcetable['e_' + col] *= 1000.0 # creates the list of columns containing the flags newflaglist = ["flag" + col for col in columnlist] # create proper flux;error lists fluxlist = [] for col in columnlist: fluxlist.append(col) fluxlist.append('e_' + col) # creates the table in proper format for feeding to sedfitter final = newsourcetable[['SOFIA_name', 'RA', 'DEC'] + newflaglist + fluxlist].filled(-1.0) final.write(folder_export + key['Cluster'] + '_sedfitter.tab', format='ascii') # remove first line (column headers) os.system('sed -i -e "1d" %s' % (folder_export + key['Cluster'] + '_sedfitter.tab')) # name of the filter names for use in sedfitter sednamelist = [ '2J', '2H', '2K', 'I1', 'I2', 'I3', 'I4', 'F11', 'F19', 'M1', 'F31', 'F37', 'M2', 'W1', 'W2', 'H70', 'H160', 'H250', 'H350', 'H500' ] # list of aperture sizes apertures = [ 8, 8, 8, 12, 12, 12, 12, 9, 9, 35, 9, 9, 45, 20, 40, 12, 22, 22, 30, 42 ] * u.arcsec ### CHECK APERTURE SIZES ,12,22,22,30,42 # distance to the current cluster distance = fieldlist[key['Cluster']][0] # cleans up the directories before starting os.system('rm -rf %s' % (folder_export + 'plot_' + key['Cluster'])) os.system('rm %s' % (SEDfolder + 'output_' + key['Cluster'] + '.fitinfo')) # fitting routine fit(folder_export + key['Cluster'] + '_sedfitter.tab', sednamelist, apertures, model_dir, SEDfolder + 'output_' + key['Cluster'] + '.fitinfo', extinction_law=extinction, distance_range=[distance * 0.8, distance * 1.2] * u.pc, av_range=[0., 10]) print "Generating some plots" plot(SEDfolder + 'output_' + key['Cluster'] + '.fitinfo', folder_export + 'plot_' + key['Cluster'], select_format=('N', 10.)) #plot_params_1d(SEDfolder+'output_'+key['Cluster']+'.fitinfo','MDISK',output_dir=folder_export+'plot_'+key['Cluster']+"/1d",log_x=True,select_format=('F',3.)) print "Extracting the parameters from the fits" write_parameters(SEDfolder + 'output_' + key['Cluster'] + '.fitinfo', folder_export + 'plot_' + key['Cluster'] + '/params.txt', select_format=('N', 10.)) write_parameter_ranges( SEDfolder + 'output_' + key['Cluster'] + '.fitinfo', folder_export + 'plot_' + key['Cluster'] + '/params_ranges.txt', select_format=('N', 10.)) print "Parsing results..." parse_params_table(folder_export + 'plot_' + key['Cluster']) print "Done parsing results"
import os import glob from astropy import units as u from sedfitter import (fit, plot, plot_params_1d, plot_params_2d, write_parameters, write_parameter_ranges) from sedfitter.extinction import Extinction # Define path to models model_dir = '/Volumes/Data/models/models_r06' # Read in extinction law. We read in columns 1 (the wavelength in microns) and # 4 (the opacity in cm^2/g) extinction = Extinction.from_file('kmh94.par', columns=[0, 3], wav_unit=u.micron, chi_unit=u.cm**2 / u.g) # Define filters and apertures filters = ['2J', '2H', '2K', 'I1', 'I2', 'I3', 'I4'] apertures = [3., 3., 3., 3., 3., 3., 3.] * u.arcsec # Run the fitting fit('data_glimpse', filters, apertures, model_dir, 'output.fitinfo', extinction_law=extinction, distance_range=[1., 2.] * u.kpc, av_range=[0., 40.]) # For the remaining commands, we always select the models with chi^2-chi_best^2 # per datapoint less than 3. select_format = ('F', 3)