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))
# 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) # Make SED plots plot('output.fitinfo', 'plots_seds', plot_max=100, select_format=select_format) # Make histograms of the disk mass plot_params_1d('output.fitinfo', 'MDISK', 'plots_mdisk',
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"
from sedfitter.extinction import Extinction # Define path to models model_dir = '/Users/cadeadams/Desktop/PythonPrac/models_kurucz' # Read in extinction law) extinction = Extinction.from_file('kmh94.par', columns=[0, 3], wav_unit=u.micron, chi_unit=u.cm**2 / u.g) # Define filters and apertures filt = [ '2H', '2J', '2K', 'S4', 'S1', 'S2', 'S3', 'WISE1', 'WISE2', 'WISE3', 'WISE4' ] apertures = [3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.] * u.arcsec # Run the fitting fit('V380Ori.dat', filt, apertures, model_dir, 'output.fitinfo', extinction_law=extinction, distance_range=[1., 2.] * u.kpc, av_range=[0., 40.]) from sedfitter import plot plot('output.fitinfo', 'plots_seds')
if ma.is_masked(s._av): av_range = default_av_range else: av_range = s.avrange() if None in av_range: av_range = default_av_range #av_range = default_av_range print("AV RANGE is ", av_range) select_format = ('N', 25) try: fit(datafile, filter_names=filters, apertures=apertures, extinction_law=extinction, distance_range=distance_range, av_range=av_range, model_dir=sed_model_dir, output_convolved=True, output=outfit, remove_resolved=False, output_format=select_format) except Exception as e: bad.write("%s %s\n" % (nospacename, e)) thisbad = True print(e) #info = fitter.fit(source) #FitInfo.write(info,nospacename+'.sedfit') if thisbad: continue else: #z = FitInfoFile(outdir+nospacename+'.sedfit',mode="r")
] * u.arcsec ext = "_all_filts" else: ext = "" #filters = ['2J', '2H', '2K', 'I1', 'I2', 'I3', 'I4', 6735.4*u.angstrom, 5319.9*u.angstrom, 7992.9*u.angstrom, 3561.8*u.angstrom, 4718.9*u.angstrom, 6185.2*u.angstrom, 7499.7*u.angstrom, 8961.5*u.angstrom] #apertures = [ 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.] * u.arcsec # Run the fitting ext = "onesource_testr17" outfile = data_dir + 'one_source_sedfit_output.info' + ext if True: try: fit(data=onedatafile, filter_names=filters, apertures=apertures, model_dir=sed_model_dir, output=outfile + ext, extinction_law=extinction, distance_range=distance_range, av_range=av_range, output_convolved=False, remove_resolved=True) except ValueError, v: traceback.print_exc() print("\n### ERROR: %s" % v) print( "### Check that number of filters matches number of photometric data points" ) sys.exit(255) if False: mysources = dict() with open(visibledatafile) as srcfile:
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"
# 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) # Make SED plots plot('output.fitinfo', 'plots_seds', plot_max=100, select_format=select_format) # Make histograms of the disk mass plot_params_1d('output.fitinfo', 'MDISK', 'plots_mdisk', log_x=True, select_format=select_format) # Make 2-d plots of the envelope infall rate vs disk mass