def runSpec(catalog_entry, mask, objno): print catalog_entry['OBJNO'], catalog_entry['MASK'], catalog_entry[ 'ZBEST'], catalog_entry['RA'], catalog_entry['DEC'] t0 = time.time() path_to_spectrum = glob.glob( join(os.environ['DEEP2_DIR'], 'spectra', mask, '*', '*' + objno + '*_fc_tc.dat')) print path_to_spectrum if len(path_to_spectrum) >= 1: spec = gs.GalaxySpectrumFIREFLY("-", milky_way_reddening=True) spec.openObservedDEEP2pectrum(catalog_entry) ok = (spec.bad_flags == 1) & (spec.flux > 0) return np.median(spec.flux[ok] / spec.error[ok]) else: return -1.
def generic(self, wave, flux, redshift=None, fractional_error=0.1): ''' Read in **observed** frame wavelength [Ang] and flux [1e-17 erg/s/A/cm^2] to class compatible with gs.GalaxySpectrumFIREFLY object ''' self.ra = 0. self.dec = 0. if redshift is None: self.redshift = 0.01 else: self.redshift = redshift self.wavelength = wave self.restframe_wavelength = self.wavelength / (1. + self.redshift) #self.wavelength = self.restframe_wavelength * (1. + self.redshift) self.flux = flux self.error = self.flux * fractional_error self.bad_flags = np.ones(len(self.restframe_wavelength)) self.vdisp = 70. self.trust_flag = 1 self.objid = 0 lines_mask = self.emissionlineMask(self.restframe_wavelength) self.wavelength = self.wavelength[~lines_mask] self.restframe_wavelength = self.restframe_wavelength[~lines_mask] influx = self.flux[~lines_mask] inerror = self.error[~lines_mask] inbad_flags = self.bad_flags[~lines_mask] self.flux, self.error, self.bad_flags = gs.remove_bad_data( influx, inerror, inbad_flags) self.r_instrument = np.zeros(len(self.wavelength)) for wi, w in enumerate(self.wavelength): if w < 6000: self.r_instrument[wi] = (2270.0 - 1560.0) / ( 6000.0 - 3700.0) * w + 420.0 else: self.r_instrument[wi] = (2650.0 - 1850.0) / ( 9000.0 - 6000.0) * w + 250.0 self.ebv_mw = 0.0 return None
def DESIlike(self, desi_spec, redshift=None): ''' Read in desispec.io spectra object and return class compatible with gs.GalaxySpectrumFIREFLY object ''' if redshift is None: self.redshift = 0. else: self.redshift = redshift self.wavelength = np.concatenate( [desi_spec.wave[b] for b in ['b', 'r', 'z']]) self.restframe_wavelength = self.wavelength / (1. + self.redshift) self.flux = np.concatenate( [desi_spec.flux[b][0] for b in ['b', 'r', 'z']]) self.error = np.concatenate( [desi_spec.ivar[b][0]**(-0.5) for b in ['b', 'r', 'z']]) self.bad_flags = np.ones(len(self.restframe_wavelength)) self.vdisp = 70. self.trust_flag = 1 self.objid = 0 lines_mask = self.emissionlineMask(self.restframe_wavelength) self.wavelength = self.wavelength[~lines_mask] self.restframe_wavelength = self.restframe_wavelength[~lines_mask] influx = self.flux[~lines_mask] inerror = self.error[~lines_mask] inbad_flags = self.bad_flags[~lines_mask] self.flux, self.error, self.bad_flags = gs.remove_bad_data( influx, inerror, inbad_flags) # some generic instrument resolution. This is only used when downgrade_models=True, # which at this time I don't know what that's useful for... self.r_instrument = np.zeros(len(self.wavelength)) #for wi, w in enumerate(self.wavelength): # if w < 6000: # self.r_instrument[wi] = (2270.0-1560.0)/(6000.0-3700.0)*w + 420.0 # else: # self.r_instrument[wi] = (2650.0-1850.0)/(9000.0-6000.0)*w + 250.0 self.ebv_mw = 0.0 return None
def convert_spec_2_fits(catalog_entry, output_file, mask, objno): path_to_spectrum = glob.glob( join(deep2_dir, 'spectra', mask, '*', '*' + objno + '*_fc_tc.dat')) print path_to_spectrum if len(path_to_spectrum) >= 1: spec = gs.GalaxySpectrumFIREFLY("-", milky_way_reddening=True) spec.openObservedDEEP2pectrum(catalog_entry) prihdr = fits.Header() prihdr['FILE'] = os.path.basename(output_file) prihdr['MASK'] = catalog_entry['MASK'] prihdr['OBJNO'] = catalog_entry['OBJNO'] prihdr['RA'] = catalog_entry['RA'] prihdr['DEC'] = catalog_entry['DEC'] prihdr['redshift'] = catalog_entry['ZBEST'] prihdu = fits.PrimaryHDU(header=prihdr) waveCol = fits.Column(name="wavelength", format="D", unit="Angstrom", array=spec.wavelength) dataCol = fits.Column(name="flux", format="D", unit="1e-17erg/s/cm2/Angstrom", array=spec.flux) errorCol = fits.Column(name="flux_error", format="D", unit="1e-17erg/s/cm2/Angstrom", array=spec.error) cols = fits.ColDefs([waveCol, dataCol, errorCol]) tbhdu = fits.BinTableHDU.from_columns(cols) complete_hdus = fits.HDUList([prihdu, tbhdu]) if os.path.isfile(output_file): os.remove(output_file) complete_hdus.writeto(output_file)
def runSpec_DR16(specLiteFile): t0 = time.time() baseN = os.path.basename(specLiteFile).split('-') plate = baseN[1] mjd = baseN[2] fibre, dum = os.path.basename(baseN[3]).split('.') # This needs to be changed to your own directory outputFolder = join(os.environ['FF_DIR'], 'output') output_file = join(outputFolder, 'spFly-' + os.path.basename(specLiteFile)[5:-5]) + ".fits" if os.path.isfile(output_file): print('already done', time.time() - t0) return 0. if os.path.isdir(outputFolder) == False: os.mkdir(outputFolder) # Firefly modules and parameters import GalaxySpectrumFIREFLY as gs import StellarPopulationModel as spm import astropy.cosmology as co cosmo = co.Planck15 models_key = 'm11' testing = False #true: smaller wavelength range, older age spec = gs.GalaxySpectrumFIREFLY(specLiteFile, milky_way_reddening=True) spec.openObservedSDSSSpectrum(survey='sdssMain', testing=True) ageMin = 0. ageMax = 20. ZMin = 0.001 ZMax = 10. print(spec.hdulist[2].data['CLASS'][0], spec.hdulist[2].data['Z'][0], spec.hdulist[2].data['Z_ERR'][0], spec.hdulist[2].data['ZWARNING'][0]) if spec.hdulist[2].data['CLASS'][0] == "GALAXY" and spec.hdulist[2].data[ 'Z'][0] > spec.hdulist[2].data['Z_ERR'][0] and spec.hdulist[ 2].data['Z_ERR'][0] > 0 and ( (spec.hdulist[2].data['ZWARNING_NOQSO'][0] == 0) | (spec.hdulist[2].data['ZWARNING_NOQSO'][0] == 4)): print('Output file:') print(output_file) print("--------------------------") prihdr = spm.pyfits.Header() prihdr['FILE'] = os.path.basename(output_file) prihdr['PLATE'] = plate prihdr['MJD'] = mjd prihdr['FIBERID'] = fibre prihdr['MODELS'] = models_key prihdr['FITTER'] = "FIREFLY" prihdr['AGEMIN'] = str(ageMin) prihdr['AGEMAX'] = str(ageMax) prihdr['ZMIN'] = str(ZMin) prihdr['ZMAX'] = str(ZMax) prihdr['redshift'] = spec.hdulist[2].data['Z'][0] prihdr['HIERARCH age_universe'] = np.round( cosmo.age(spec.hdulist[2].data['Z'][0]).value, 3) prihdu = spm.pyfits.PrimaryHDU(header=prihdr) tables = [prihdu] did_not_converged = 0. try: model_1 = spm.StellarPopulationModel(spec, output_file, cosmo, models=models_key, model_libs=['MILES'], imfs=['cha'], age_limits=[ageMin, ageMax], downgrade_models=False, data_wave_medium='vacuum', Z_limits=[ZMin, ZMax], use_downgraded_models=False, write_results=True) model_1.fit_models_to_data() tables.append(model_1.tbhdu) print("m1", time.time() - t0) except (ValueError): tables.append(model_1.create_dummy_hdu()) did_not_converged += 1 print('did not converge') try: model_1 = spm.StellarPopulationModel(spec, output_file, cosmo, models=models_key, model_libs=['ELODIE'], imfs=['cha'], age_limits=[ageMin, ageMax], downgrade_models=False, data_wave_medium='vacuum', Z_limits=[ZMin, ZMax], use_downgraded_models=False, write_results=True) model_1.fit_models_to_data() tables.append(model_1.tbhdu) print("m2", time.time() - t0) except (ValueError): tables.append(model_1.create_dummy_hdu()) did_not_converged += 1 print('did not converge') #try : #model_1 = spm.StellarPopulationModel(spec, output_file, cosmo, models = models_key, model_libs = ['STELIB'], imfs = ['cha'], age_limits = [ageMin,ageMax], downgrade_models = False, data_wave_medium = 'vacuum', Z_limits = [ZMin,ZMax], use_downgraded_models = False, write_results = False) #model_1.fit_models_to_data() #tables.append( model_1.tbhdu ) #print( "m3", time.time()-t0) #except (ValueError): #tables.append( model_1.create_dummy_hdu() ) #did_not_converged +=1 #print('did not converge') if did_not_converged < 2: complete_hdus = spm.pyfits.HDUList(tables) if os.path.isfile(output_file): os.remove(output_file) complete_hdus.writeto(output_file) print("time used =", time.time() - t0, "seconds") return 1. #spec, model_1
def create_figure_add_chi2(filename, model_hdus): sp = gs.GalaxySpectrumFIREFLY(filename, milky_way_reddening=False) sp.openGAMAsimulatedSpectrum() spec = interp1d(sp.restframe_wavelength, sp.flux) err = interp1d(sp.restframe_wavelength, sp.error) wl_data_min = n.min(sp.restframe_wavelength) wl_data_max = n.max(sp.restframe_wavelength) # now creates the figure per model fig = p.figure(0, figsize=(7, 10), frameon=False) #, tight_layout=True) rect = 0.2, 0.15, 0.85, 0.95 #ax = fig.add_axes(rect, frameon=False) # panel with the spectrum fig.add_subplot(3, 1, 1) p.plot(sp.restframe_wavelength[::2], sp.flux[::2], 'k', rasterized=True, alpha=0.5, label='data') p.plot(model_hdus[0].data['wavelength'], model_hdus[0].data['model_flux'], label='model') p.yscale('log') mean_data = n.median(sp.flux) p.ylim((mean_data / 8., mean_data * 8.)) p.xlabel('Wavelength [Angstrom]') p.ylabel(r'Flux [$f_\lambda$ $10^{-17}$ erg/cm2/s/A]') p.title(os.path.basename(filename)) # second panel distribution of residuals fig.add_subplot(3, 1, 2) for hdu in model_hdus: #print hdu ok_model = (hdu.data['wavelength'] > wl_data_min) & (hdu.data['wavelength'] < wl_data_max) wl_model = hdu.data['wavelength'][ok_model] #print spec(wl_model),hdu.data['model_flux'][ok_model],err(wl_model) chi2s = (spec(wl_model) - hdu.data['model_flux'][ok_model]) / err(wl_model) p.hist(chi2s, bins=n.arange(-2, 2, 0.1), normed=True, histtype='step', label=hdu.header['IMF'] + hdu.header['library'] + ", EBV=" + str(n.round(hdu.header['EBV'], 3)) + r", $\chi^2=$" + str(n.round(n.sum(chi2s**2.) / (len(chi2s) - 2.), 4))) p.ylim((-0.02, 1.02)) #p.yscale('log') p.xlabel('(data-model)/error') p.ylabel('Normed distribution') hdu.header['chi2'] = n.sum(chi2s**2.) hdu.header['ndof'] = len(chi2s) - 2. p.plot(n.arange(-2, 2, 0.005), gaussD.pdf(n.arange(-2, 2, 0.005)), 'k--', label=r'N(0,1)', lw=0.5) p.grid() p.legend(frameon=False, loc=0, fontsize=8) fig.add_subplot(3, 1, 3) tpl = n.transpose( n.array([[ hdu.header['age_lightW'], hdu.header['stellar_mass'], hdu.header['age_lightW_up'] - hdu.header['age_lightW'], hdu.header['age_lightW'] - hdu.header['age_lightW_low'], hdu.header['stellar_mass_up'] - hdu.header['stellar_mass'], hdu.header['stellar_mass'] - hdu.header['stellar_mass_low'] ] for hdu in model_hdus])) p.errorbar(tpl[0], tpl[1], xerr=[tpl[2], tpl[3]], yerr=[tpl[4], tpl[5]], barsabove=True, fmt='o') #p.axvline(prihdr['age_universe'], color='r', ls='dashed') idsUP = n.argsort(tpl[1]) iterList = model_hdus[idsUP] for jj, hdu in enumerate(iterList): p.annotate(hdu.header['IMF'] + hdu.header['library'] + r", $\log(Z/Z_\odot)=$" + str(n.round(hdu.header['metallicity_lightW'], 4)), xy=(hdu.header['age_lightW'], hdu.header['stellar_mass']), xycoords='data', xytext=(0.85, (jj + 0.5) / len(iterList)), textcoords='axes fraction', arrowprops=dict(facecolor='black', shrink=0.05, width=0.2, headwidth=3), horizontalalignment='right', verticalalignment='top', fontsize=9) p.ylabel(r'$\log_{10}(M/[M_\odot])$') p.xlabel(r'$\log_{10}(age/[yr])$') #p.ylim((9,12.5)) p.grid() p.savefig(path_2_im(filename)) p.clf() return model_hdus
def runSpec(specLiteFile): baseN = os.path.basename(specLiteFile).split('-') plate = baseN[1] mjd = baseN[2] fibre, dum = os.path.basename(baseN[3]).split('.') t0 = time.time() spec = gs.GalaxySpectrumFIREFLY(specLiteFile, milky_way_reddening=True) spec.openObservedSDSSSpectrum(survey='sdssMain') ageMin = 0. ageMax = np.log10(cosmo.age(spec.redshift).value * 1e9) ZMin = 0.001 ZMax = 4. if spec.hdulist[2].data['CLASS'][0] == "GALAXY" and spec.hdulist[2].data[ 'Z'][0] > spec.hdulist[2].data['Z_ERR'][0] and spec.hdulist[2].data[ 'Z_ERR'][0] > 0 and spec.hdulist[2].data['ZWARNING'][0] == 0: # This needs to be changed to your own directory outputFolder = join(os.environ['FF_DIR'], 'output', 'plate') output_file = join(outputFolder, 'spFly-' + os.path.basename(specLiteFile)[5:-5]) + ".fits" print("Start time=", t0) print(output_file) print("--------------------------") if os.path.isdir(outputFolder) == False: os.mkdir(outputFolder) prihdr = spm.pyfits.Header() prihdr['FILE'] = os.path.basename(output_file) prihdr['PLATE'] = plate prihdr['MJD'] = mjd prihdr['FIBERID'] = fibre prihdr['MODELS'] = models_key prihdr['FITTER'] = "FIREFLY" prihdr['AGEMIN'] = str(ageMin) prihdr['AGEMAX'] = str(ageMax) prihdr['ZMIN'] = str(ZMin) prihdr['ZMAX'] = str(ZMax) prihdr['redshift'] = spec.hdulist[2].data['Z'][0] prihdr['HIERARCH age_universe'] = np.round( cosmo.age(spec.hdulist[2].data['Z'][0]).value, 3) prihdu = spm.pyfits.PrimaryHDU(header=prihdr) tables = [prihdu] did_not_converged = 0. try: model_1 = spm.StellarPopulationModel(spec, output_file, cosmo, models=models_key, model_libs=['MILES'], imfs=['cha'], age_limits=[ageMin, ageMax], downgrade_models=False, data_wave_medium='vacuum', Z_limits=[ZMin, ZMax], use_downgraded_models=True, write_results=False) model_1.fit_models_to_data() tables.append(model_1.tbhdu) # print "m1", time.time()-t0 except (ValueError): tables.append(model_1.create_dummy_hdu()) did_not_converged += 1 try: model_2 = spm.StellarPopulationModel(spec, output_file, cosmo, models=models_key, model_libs=['MILES'], imfs=['ss'], age_limits=[ageMin, ageMax], downgrade_models=False, data_wave_medium='vacuum', Z_limits=[ZMin, ZMax], use_downgraded_models=True, write_results=False) model_2.fit_models_to_data() tables.append(model_2.tbhdu) # print "m2", time.time()-t0 except (ValueError): tables.append(model_2.create_dummy_hdu()) did_not_converged += 1 try: model_3 = spm.StellarPopulationModel(spec, output_file, cosmo, models=models_key, model_libs=['MILES'], imfs=['kr'], age_limits=[ageMin, ageMax], downgrade_models=False, data_wave_medium='vacuum', Z_limits=[ZMin, ZMax], use_downgraded_models=True, write_results=False) model_3.fit_models_to_data() tables.append(model_3.tbhdu) # print "m3", time.time()-t0 except (ValueError): tables.append(model_3.create_dummy_hdu()) did_not_converged += 1 try: model_4 = spm.StellarPopulationModel(spec, output_file, cosmo, models=models_key, model_libs=['ELODIE'], imfs=['cha'], age_limits=[ageMin, ageMax], downgrade_models=False, data_wave_medium='vacuum', Z_limits=[ZMin, ZMax], use_downgraded_models=True, write_results=False) model_4.fit_models_to_data() # print "m4", time.time()-t0 tables.append(model_4.tbhdu) did_not_converged += 1 except (ValueError): tables.append(model_4.create_dummy_hdu()) try: model_5 = spm.StellarPopulationModel(spec, output_file, cosmo, models=models_key, model_libs=['ELODIE'], imfs=['ss'], age_limits=[ageMin, ageMax], downgrade_models=False, data_wave_medium='vacuum', Z_limits=[ZMin, ZMax], use_downgraded_models=True, write_results=False) model_5.fit_models_to_data() tables.append(model_5.tbhdu) # print "m5", time.time()-t0 except (ValueError): tables.append(model_5.create_dummy_hdu()) did_not_converged += 1 try: model_6 = spm.StellarPopulationModel(spec, output_file, cosmo, models=models_key, model_libs=['ELODIE'], imfs=['kr'], age_limits=[ageMin, ageMax], downgrade_models=False, data_wave_medium='vacuum', Z_limits=[ZMin, ZMax], use_downgraded_models=True, write_results=False) model_6.fit_models_to_data() tables.append(model_6.tbhdu) # print "m6", time.time()-t0 except (ValueError): tables.append(model_6.create_dummy_hdu()) did_not_converged += 1 try: model_7 = spm.StellarPopulationModel(spec, output_file, cosmo, models=models_key, model_libs=['STELIB'], imfs=['cha'], age_limits=[ageMin, ageMax], downgrade_models=False, data_wave_medium='vacuum', Z_limits=[ZMin, ZMax], use_downgraded_models=True, write_results=False) model_7.fit_models_to_data() # print "m4", time.time()-t0 tables.append(model_7.tbhdu) except (ValueError): tables.append(model_7.create_dummy_hdu()) did_not_converged += 1 try: model_8 = spm.StellarPopulationModel(spec, output_file, cosmo, models=models_key, model_libs=['STELIB'], imfs=['ss'], age_limits=[ageMin, ageMax], downgrade_models=False, data_wave_medium='vacuum', Z_limits=[ZMin, ZMax], use_downgraded_models=True, write_results=False) model_8.fit_models_to_data() tables.append(model_8.tbhdu) # print "m5", time.time()-t0 except (ValueError): tables.append(model_8.create_dummy_hdu()) did_not_converged += 1 try: model_9 = spm.StellarPopulationModel(spec, output_file, cosmo, models=models_key, model_libs=['STELIB'], imfs=['kr'], age_limits=[ageMin, ageMax], downgrade_models=False, data_wave_medium='vacuum', Z_limits=[ZMin, ZMax], use_downgraded_models=True, write_results=False) model_9.fit_models_to_data() tables.append(model_9.tbhdu) # print "m6", time.time()-t0 except (ValueError): tables.append(model_9.create_dummy_hdu()) did_not_converged += 1 if did_not_converged < 9: complete_hdus = spm.pyfits.HDUList(tables) if os.path.isfile(output_file): os.remove(output_file) complete_hdus.writeto(output_file) print("time used =", time.time() - t0, "seconds") return spec
import GalaxySpectrumFIREFLY as gs ## Index band blue continuum red continuum Units name #01 4142.125 4177.125 4080.125 4117.625 4244.125 4284.125 1 CN_1 iii, b0, b1, bc0, bc1, rc0, rc1, unit, name = n.loadtxt('../../data/lick_indices/indices_table.txt', unpack=True, dtype='str') li0 = b0.astype('float') li1 = b1.astype('float') dir_elodie = os.path.join( os.environ['STELLARPOPMODELS_DIR'], 'data', 'SSP_M11_ELODIE') dir_miles = os.path.join( os.environ['STELLARPOPMODELS_DIR'], 'data', 'SSP_M11_MILES') dir_stelib = os.path.join( os.environ['STELLARPOPMODELS_DIR'], 'data', 'SSP_M11_STELIB') IMF = {'cha': 'Ch', 'kr': 'Kr', 'ss': 'Sa'} spec_sdss = gs.GalaxySpectrumFIREFLY( '/home/comparat/SDSS/26/spectra/0266/spec-0266-51602-0004.fits' ) spec_sdss.openObservedSDSSSpectrum('sdssMain') spec_boss = gs.GalaxySpectrumFIREFLY( '/home/comparat/SDSS/v5_10_0/spectra/3586/spec-3586-55181-0024.fits' ) spec_boss.openObservedSDSSSpectrum('sdss4') path_2_deep2 = '/home/comparat/data2/firefly/v1_1_0/DEEP2/spectra/2/deep2-2249-22052688.fits' spec_deep2 = gs.GalaxySpectrumFIREFLY( path_2_deep2 ) spec_deep2.openObservedDEEP2pectrum(path_2_deep2, survey='deep2_fits') file_list_elodie = n.array([ # chabrier #os.path.join(dir_elodie, 'ssp_M11_ELODIE.chaz001'), os.path.join(dir_elodie, 'ssp_M11_ELODIE.chaz002'), #os.path.join(dir_elodie, 'ssp_M11_ELODIE.chaz004'), # kroupa
# for one galaxy spectrum import GalaxySpectrumFIREFLY as gs import StellarPopulationModel as spm #spec_deep2_file = "D:\data\spectraStacks\content\data\O2_3728\O2_3728-DEEP2R24.2-z0.927_stack_N_100_R_8686_L_1.84133041845e+41.fits" spec_vvds_file = "D:\data\spectraStacks\content\data\O2_3728\O2_3728-VVDSDEEPI24-z0.612_stack_N_400_R_217_L_2.1326925665e+41.fits" """ spec_deep2=gs.GalaxySpectrumFIREFLY(spec_deep2_file, milky_way_reddening=False) spec_deep2.openObservedStack() file =os.path.basename(spec_deep2_file) outFile = join(os.environ['SPECTRASTACKS_DIR'], "fits", file[:7], file) model_deep2 = spm.StellarPopulationModel(spec_deep2, outFile, cosmo, models = 'm11', model_libs = ['MILES'], imfs = ['ss'], age_limits = [6,10], downgrade_models = True, data_wave_medium = 'air', Z_limits = [-3.,1.],suffix="-SPM-MILES.fits", use_downgraded_models = False) model_deep2.fit_models_to_data() """ spec_vvds = gs.GalaxySpectrumFIREFLY(spec_vvds_file, milky_way_reddening=False) spec_vvds.openObservedStack() file = os.path.basename(spec_vvds_file) outFile = join(os.environ['SPECTRASTACKS_DIR'], "fits", file[:7], file) model_vvds = spm.StellarPopulationModel(spec_vvds, outFile, cosmo, models='m11', model_libs=['MILES'], imfs=['ss'], age_limits=[6, 10], downgrade_models=True, data_wave_medium='air', Z_limits=[-3., 1.], suffix="-SPM-MILES.fits", use_downgraded_models=False)