示例#1
0
def spec_generator(folder_data, obj_list, flux_norm, idx_start=0):

    for idx_obj, obj_name in enumerate(obj_list[idx_start:]):
        wave, data, hdrs = sr.import_fits_data(folder_data /
                                               f'{obj_name}.fits',
                                               instrument='SDSS')
        flux = data['flux'] * flux_norm
        z_i = hdrs[1]["z"][0]
        lm = sr.LineMesurer(wave,
                            flux,
                            redshift=z_i,
                            normFlux=normFlux,
                            crop_waves=(1 + z_i) *
                            np.array([4685 - 100, 5100]))
        yield idx_obj, obj_name, lm.wave, lm.flux
示例#2
0
        # Loop through the voxels
        counter = 0

        for i in range_box:
            for j in range_box:
                idx_i, idx_j = center_voxel[0] + i, center_voxel[1] + j
                print(f'-- Voxel {counter}: {idx_i} {idx_j}')

                # Declare voxels
                voxel = cube[:, idx_i, idx_j]
                flux_voxel = voxel.data.data

                if not np.isnan(flux_voxel).any():

                    lm = sr.LineMesurer(wave_rest, flux_voxel)

                    # Identify the emission lines
                    norm_flux = lm.continuum_remover(noise_region)
                    obsLinesTable = lm.line_finder(norm_flux, noiseWaveLim=noise_region, intLineThreshold=3)
                    obsLinesDF = lm.match_lines(obsLinesTable, sr._linesDb)
                    lm.plot_spectrum(obsLinesTable=obsLinesTable, matchedLinesDF=obsLinesDF)

                    # # Get matched lines
                    # idcsObsLines = (obsLinesDF.observation == 'detected')
                    # obsLines = obsLinesDF.loc[idcsObsLines].index.values
                    #
                    # # Fit and check the regions
                    # logs_name_i = fileList[idx_obj].replace(".fits", f"_{idx_i}-{idx_j}_lineLog.txt")
                    # lineslog_address_i = f'{dataFolder}/{logs_name_i}'
                    # lm = sr.LineMesurer(wave_rest, flux_voxel)
示例#3
0
        with fits.open(data_folder / file) as hdul:
            data, header = hdul[0].data, hdul[0].header
            wcs4 = WCS(header)

        index4 = np.arange(header['NAXIS1'])
        wave = wcs4.wcs_pix2world(index4, 0)[0]

        w_min = header['CRVAL1']
        dw = header['CDELT1']  # dw (Wavelength interval per pixel)
        pixels = header['NAXIS1']  # nw number of output pixels
        w_max = w_min + dw * pixels
        # wave = np.linspace(w_min, w_max, pixels, endpoint=False)
        flux = data

    lm = sr.LineMesurer(wave, flux, redshift=z_obj, normFlux=np.median(flux))
    lm.fit_from_wavelengths('O3_5007A_b',
                            line_wavelengths=waves_array,
                            user_conf=fit_conf)
    # lm.print_results(show_plot=True)
    # lm.plot_line_velocity(output_address=data_folder/f'{instrument}_velocity_plot', dark_mode=False)
    # plt.show()
    # lm.save_lineslog(lm.linesDF, data_folder/f'{instrument}_measurements_log.txt')

    print(f'Treating: {file} with {instrument} configuration')
    spec_dict[instrument] = [wave, flux]
    fit_dict[file] = lm

sizing_dict = {}
sizing_dict['font.family'] = 'Times New Roman'
sizing_dict['figure.figsize'] = (12, 10)
示例#4
0
# # wave_lnLambda = 1
# # flux_interpolated = Interpolation(lm.wave)
# # flux_speed = flux_interpolated
#
# fig, ax = plt.subplots()
# ax.step(wave, flux_voxel/norm_flux, label='Observed spectrum in ln(wave)')
# # ax.step(wave_ln_resample, flux_ln_resample, label='Resample in ln(wave)')
# ax.step(wave_unifSpeed, flux_unifSpeed, label='Resample in ln(wave)')
#
# # ax.plot(wave_obs, flux_obs, label='Observed wavelength')
# ax.legend()
# plt.show()

lm = sr.LineMesurer(wave_rest,
                    flux_voxel,
                    input_err=flux_err,
                    redshift=0.0,
                    normFlux=norm_flux)
obsLm = sr.LineMesurer()
zeroLm = sr.LineMesurer()
unifSpeedLm = sr.LineMesurer()

lm.plot_spectrum(specLabel=f'{obj} voxel {idx_j}-{idx_i}', log_scale=False)

# Security check for pixels with nan values:
idcs_nan = np.isnan(lm.flux)
flux_interpolated = None

if idcs_nan.any():
    if region_mask[idx_j, idx_i]:
        Interpolation = interp1d(lm.wave[~idcs_nan],
示例#5
0
    wave, flux, header = sr.import_fits_data(file_address, instrument='OSIRIS')
    z_mean = obsData['sample_data']['z_array'][i]
    wmin_array, wmax_array = obsData['sample_data']['wmin_array'], obsData[
        'sample_data']['wmax_array']

    # Define wave and flux ranges
    wave_rest = wave / (1 + z_mean)
    idx_wave = (wave_rest >= wmin_array[i]) & (wave_rest <= wmax_array[i])

    # Open lineslog
    fitsFolder, fitsFile = file_address.parent, file_address.name
    logFolder, logFile = fitsFolder / 'pre_analysis', fitsFile.replace(
        '.fits', '_rawlinesLog.txt')

    # Load line measurer object
    lm = sr.LineMesurer(wave_rest[idx_wave], flux[idx_wave],
                        logFolder / logFile)

    # Plot the matched lines:
    lm.plot_line_mask_selection(lm.linesDF, ncols=10)

    # # Get observation data
    # objName = header['OBJECT']
    # objReference = obsData['file_information']['object_list'][i]
    # objWaves = obsData['sample_data'][f'{objReference}_obsWaves_array']
    #
    # # Compute the redshifts
    # # redshifts = (objWaves/refLines) - 1
    # # z_mean, z_std = redshifts.mean(), redshifts.std()
    # # wave_rest = wave / (1 + z_mean)
    # # print(objReference, z_mean, z_std)
    #
    fits_file = dataFolder / f'{obj}_{ext}.fits'
    objMask = dataFolder / f'{obj}_{ext}_mask.txt'  # Declare input files

    # Declare output files
    lineLog_file = objFolder / f'{obj}_{ext}_linesLog_{cycle}.txt'
    lineGrid_file = objFolder / f'{obj}_{ext}_lineGrid_{cycle}.png'

    # Load data
    wave, flux_dict, header = sr.import_fits_data(fits_file, instrument='SDSS')
    flux, err = flux_dict['flux'], flux_dict['ivar']
    maskDF = pd.read_csv(objMask, delim_whitespace=True, header=0, index_col=0)

    # Create line measurer object
    lm = sr.LineMesurer(wave,
                        flux,
                        redshift=z_objs[i],
                        normFlux=flux_norm,
                        crop_waves=(wmin_array[i], wmax_array[i]))
    # lm.plot_spectrum_components(matchedLinesDF=maskDF)

    # # Fit and check the regions
    obsLines = maskDF.index.values
    for j, lineLabel in enumerate(obsLines):

        print(f'-- {lineLabel}:')
        wave_regions = maskDF.loc[lineLabel, 'w1':'w6'].values
        lm.fit_from_wavelengths(lineLabel,
                                wave_regions,
                                fit_conf=obsData[f'SDSS_line_fitting'])
        # lm.print_results(show_fit_report=True, show_plot=True)
        i], data_folder / sigmafile_list[i]

    # Output data
    lineslog_file = results_folder / f'{objName}_linesLog.txt'
    lineslog_table = results_folder / f'{objName}_flux_table'

    # Load inputs
    wave, flux, header = sr.import_fits_data(spec_file,
                                             instrument='xshooter',
                                             frame_idx=0)
    wave_sigma, sigma, header_sigma = sr.import_fits_data(
        sigm_file, instrument='xshooter', frame_idx=0)

    lm = sr.LineMesurer(wave,
                        flux,
                        crop_waves=[wmin_array[i], wmax_array[i]],
                        input_err=sigma,
                        normFlux=norm_flux,
                        redshift=z_obj)

    # lm.plot_spectrum(obsLinesTable=obsLinesTable, matchedLinesDF=maskLinesDF, specLabel=f'{objName}')
    mask_local = data_folder / f'{objName}_mask'
    mask_local_df = sr.lineslogFile_to_DF(mask_local)

    obsLines = mask_local_df.index.values
    for j, lineLabel in enumerate(obsLines):
        wave_regions = mask_local_df.loc[lineLabel, 'w1':'w6'].values
        if lineLabel in ['O3_5007A_b']:
            # wave_regions[2] = 8400.0/(1+lm.redshift)
            # wave_regions[3] = 8445.0/(1+lm.redshift)
            lm.fit_from_wavelengths(lineLabel,
                                    wave_regions,
ext = '_BR'
cycle = 'c2'

for i_obj, obj in enumerate(objList):
    if i_obj < 3:
        print(f'Treating: {obj}')
        objFolder = resultsFolder / f'{obj}'
        lineLog_file = objFolder / f'{obj}{ext}_linesLog_{cycle}.txt'
        linesDF = sr.lineslogFile_to_DF(lineLog_file)

        fits_file = dataFolder / f'{obj}{ext}.fits'
        wave, flux, header = sr.import_fits_data(fits_file,
                                                 instrument='OSIRIS')
        lm = sr.LineMesurer(wave,
                            flux,
                            redshift=z_objs[i_obj],
                            normFlux=flux_norm,
                            crop_waves=(wmin_array[i_obj], wmax_array[i_obj]))

        fits_blue_file = dataFolder / f'{obj}_B.fits'
        wave_b, flux, header_b = sr.import_fits_data(fits_blue_file,
                                                     instrument='OSIRIS')
        flux_b = flux[idx_band][0]
        lm_b = sr.LineMesurer(wave_b,
                              flux_b,
                              redshift=z_objs[i_obj],
                              normFlux=flux_norm,
                              crop_waves=(wmin_array[i_obj],
                                          wmax_array[i_obj]))

        fits_red_file = dataFolder / f'{obj}_R.fits'
示例#9
0
    # Load the data
    wave, data, hdrs = sr.import_fits_data(sampleFiles[i], instrument='SDSS')
    flux = data['flux'] * normFlux
    z_i = hdrs[1]["z"][0]

    #Output files
    specPlotAddress = f'{obj}_SDSS_spectrum.png'

    print(f'\nGalaxy {obj}')
    lineLabels = ['He_II 4685', 'H_beta', 'H_alpha', '[O_III] 4959', '[O_III] 5007']
    lineWaves = [4685.0, 4861.0, 5007]
    for lineRef in lineLabels:
        idx_line = np.where(hdrs[2]['LINENAME'] == lineRef)[0][0]
        lineArea, lineAreaErr = hdrs[2]['LINEAREA'][idx_line], hdrs[2]['LINEAREA_ERR'][idx_line]
        print(f'{lineRef} linea area : {lineArea:.2f}+/-{lineAreaErr:.2f}')

    lm = sr.LineMesurer(wave, flux, redshift=z_i, normFlux=normFlux, crop_waves=(1+z_i) * np.array([4685-100, 5100]))
    # lm.plot_spectrum_components(specLabel=f'Galaxy {obj}',
    #                             axConf={'ylabel': r'Flux $(10^{17}\,erg\,cm^{-2} s^{-1} \AA^{-1})$'},
    #                             output_address=resultsFolder/specPlotAddress)
    # lm.plot_spectrum_components(specLabel=f'Galaxy {obj}',
    #                             axConf={'ylabel': r'Flux $(10^{17}\,erg\,cm^{-2} s^{-1} \AA^{-1})$'})


    lineWaves = [4685.0, 4861.0, 5007]
    lineLabels = [r'He_II4685\AA', r'$H\beta$', r'[O_III]5007\AA']

    lineid_plot.plot_line_ids(lm.wave, lm.flux, lineWaves, lineLabels)

    plt.show()
示例#10
0
from pathlib import Path
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import src.specsiser as sr
import pyneb as pn

# spec_address = '/home/vital/Astro-data/Observations/IZW18_Blue_cr_f_t_w_e__test1_fglobal.fits'
#
# wave, data, header = sr.import_fits_data(spec_address, instrument='ISIS', frame_idx=0)
# norm_flux = 1e-17
# z_obj = 0.0
#
# lm = sr.LineMesurer(wave, data[0], normFlux=norm_flux, redshift=z_obj)
# lm.plot_spectrum(specLabel='IZwicky18 Blue arm ISIS spectrum')

spec_address = '/home/vital/Dropbox/Astrophysics/Seminars/LzLCS/spec-0266-51630-0100.fits'

wave, data, header = sr.import_fits_data(spec_address,
                                         instrument='SDSS',
                                         frame_idx=0)
norm_flux = 1e-17
z_obj = 0.0

lm = sr.LineMesurer(wave,
                    data['flux'] * norm_flux,
                    normFlux=norm_flux,
                    redshift=z_obj)
lm.plot_spectrum(specLabel='CGCG007-025 SLOAN spectrum')
示例#11
0
            # Load inputs
            wave, flux, header = sr.import_fits_data(spec_file,
                                                     instrument='xshooter',
                                                     frame_idx=0)
            wave_sigma, sigma, header_sigma = sr.import_fits_data(
                sigm_file, instrument='xshooter', frame_idx=0)

            # Convert to angstroms
            wave = wave * 10 if objName != 'j131037' else wave
            wave_sigma = wave_sigma * 10 if objName != 'j131037' else wave_sigma

            trim_limits = [wmin_array[j], wmax_array[j]]
            lm = sr.LineMesurer(wave,
                                flux,
                                crop_waves=trim_limits,
                                input_err=sigma,
                                normFlux=norm_flux,
                                redshift=z_obj[i])
            if verbose:
                lm.plot_spectrum(continuumFlux=lm.errFlux)

            # Find lines
            global_mask = data_folder / f'global_mask.txt'
            global_mask_df = sr.lineslogFile_to_DF(global_mask)

            lineLabel = 'H1_6563A_b'
            wave_regions = global_mask_df.loc[lineLabel, 'w1':'w6'].values
            lm.fit_from_wavelengths(lineLabel,
                                    wave_regions,
                                    user_conf=profile_conf)
            w80 = lm.plot_line_velocity(plot_title=objName)
示例#12
0
    local_mask = objFolder / f'mask_{obj}.txt'

    # Outputs location
    local_lineslog = objFolder / f'lineslog_{obj}.txt'
    pdf_lineslog = objFolder / f'tablelog_{obj}'

    # Make folder if not available
    objFolder.mkdir(parents=True, exist_ok=True)

    # Read the spectrum
    wave, data, hdrs = sr.import_fits_data(fits_address, instrument='SDSS')
    flux = data['flux'] * normFlux
    z_i = hdrs[1]["z"][0]

    # Measure the emission lines
    lm = sr.LineMesurer(wave, flux, redshift=z_i, normFlux=normFlux)
    # if local_mask.is_file():
    #     objMaskDF = sr.lineslogFile_to_DF(local_mask)
    #     for i, lineLabel in enumerate(objMaskDF.index.values):
    #         wave_regions = objMaskDF.loc[lineLabel, 'w1':'w6'].values
    #         try:
    #             lm.fit_from_wavelengths(lineLabel, wave_regions)
    #         except:
    #             print(f'- Failure at: {lineLabel}')
    #
    # # Check Extinction
    # lm.plot_line_grid(lm.linesDF, output_address=objFolder / f'{obj}_grid_plot.png')
    # lm.save_lineslog(lm.linesDF, local_lineslog)
    # lm.table_fluxes(lm.linesDF, pdf_lineslog)

    lm.linesDF = sr.lineslogFile_to_DF(local_lineslog)
示例#13
0
    plotFolder, plotFile = fitsFolder / 'flux_analysis', fitsFile.replace(
        '.fits', '_linesGrid')

    # Get fits data
    wave, flux, header = sr.import_fits_data(file_address, instrument='OSIRIS')
    z_mean = obsData['sample_data']['z_array'][i]
    wmin_array, wmax_array = obsData['sample_data']['wmin_array'], obsData[
        'sample_data']['wmax_array']

    # Define wave and flux ranges
    wave_rest = wave / (1 + z_mean)
    idx_wave = (wave_rest >= wmin_array[i]) & (wave_rest <= wmax_array[i])

    # Load line measurer object
    lm = sr.LineMesurer(wave_rest[idx_wave],
                        flux[idx_wave],
                        masksFolder / masksFile,
                        normFlux=flux_norm)

    # Loop through the lines
    print(f'\n- {objName}:')
    obsLines = lm.linesDF.index.values
    for j, lineLabel in enumerate(obsLines):

        # Declare regions data
        print(f'-- {lineLabel}:')
        wave_regions = lm.linesDF.loc[lineLabel, 'w1':'w6'].values
        idcsLinePeak, idcsContinua = lm.define_masks(wave_regions)

        # Measure line data
        lm.line_properties(idcsLinePeak, idcsContinua, bootstrap_size=1000)
    sigma = obs_fit_output.params[f"{lineLabel}_sigma"].value

    mu_err = obs_fit_output.params[f"{lineLabel}_center"].stderr
    sigma_err = obs_fit_output.params[f"{lineLabel}_sigma"].stderr

    x_obs, y_obs = obs_fit_output.userkws['x'], obs_fit_output.data
    wave_obs = np.linspace(x_obs[0], x_obs[-1], 500)
    flux_comps_obs = obs_fit_output.eval_components(x=wave_obs)
    flux_obs = flux_comps_obs.get(f'{lineLabel}_cont_',
                                  0.0) + flux_comps_obs[f'{lineLabel}_']

    return x_fit, y_fit, wave_obs, flux_obs, mu, sigma, mu_err, sigma_err


lineLabel = 'H1_6563A'
lm = sr.LineMesurer()
wave, flux_voxel, flux_err = np.loadtxt(voxel_spec_file, unpack=True)
flux_voxel, flux_err = flux_voxel / norm_flux, np.sqrt(flux_err / norm_flux)
obsLineWaves = mask_df.loc[lineLabel, 'w1':'w6'].values * (1 + z_objs[i])

MC_size = 500
data_in, curve_out = {}, {}
mu_array, sigma_array = np.zeros(MC_size), np.zeros(MC_size)
mu_err_array, sigma_err_array = np.zeros(MC_size), np.zeros(MC_size)

# Case all pixels share x_error value
# x_err = np.random.normal(loc=0, scale=0.625, size=(MC_size, wave.size))
# x_err = np.random.uniform(low=-0.625, high=0.625, size=MC_size)
# for i in range(MC_size):
#     x_in, y_in, x_out, y_out, mu, sigma, mu_err, sigma_err = lmfit_gaussian(wave + x_err[i], flux_voxel, flux_err, obsLineWaves)
#     data_in[i] = (x_in, y_in)
示例#15
0
        # Declare files location
        fits_file = dataFolder/f'{obj}{ext}.fits'
        lineLog_file = dataFolder/'flux_analysis'/f'{obj}{ext}_linesLog.txt'
        pdfTableFile = dataFolder/'flux_analysis'/f'{obj}{ext}_linesTable'
        txtTableFile = dataFolder/'flux_analysis'/f'{obj}{ext}_linesTable.txt'
        print(f'\n- {i}: {lineLog_file}')

        # Set wavelength and flux
        print(f'\n-- Treating {counter} :{obj}{ext}.fits')
        wave, flux_array, header = sr.import_fits_data(fits_file, instrument='OSIRIS')
        wave_rest = wave / (1 + z)
        idx_wave = (wave_rest >= wmin_array[i]) & (wave_rest <= wmax_array[i])
        flux = flux_array[idx_band][0] if ext in ('_B', '_R') else flux_array

        # Load line measurer object
        lm = sr.LineMesurer(wave_rest[idx_wave], flux[idx_wave], linesDF_address=lineLog_file, normFlux=flux_norm)
        pdf = PdfPrinter()
        tableDF = pd.DataFrame(columns=tableHeaders[1:])

        # Measure line fluxes
        idcs_lines = ~lm.linesDF.index.str.contains('_b')
        obsLines = lm.linesDF.loc[idcs_lines].index.values

        # Measure line fluxes
        pdf.create_pdfDoc(pdfTableFile, pdf_type='table')
        pdf.pdf_insert_table(tableHeaders)

        # Normalizing flux
        if 'H1_6563A' in lm.linesDF.index:
            flux_Halpha = lm.linesDF.loc['H1_6563A', 'gauss_flux']
            flux_Hbeta = lm.linesDF.loc['H1_4861A', 'intg_flux']
        # Set and crop the wavelength
        print(f'-- Treating {counter} :{obj}{ext}.fits')
        wave, flux_array, header = sr.import_fits_data(fits_address,
                                                       instrument='OSIRIS')
        wave_rest = wave / (1 + z)

        if ext in ('_B', '_R'):
            flux = flux_array[idx_band][0]
        else:
            flux = flux_array

        # Define wave and flux ranges
        idx_wave = (wave_rest >= wmin_array[i]) & (wave_rest <= wmax_array[i])

        # Load line measurer object
        lm = sr.LineMesurer(wave_rest[idx_wave], flux[idx_wave])

        # Find the lines
        lm.plot_spectrum(matchedLinesDF=linesLog_0_DF)

        # # Set and crop the wavelength
        # print(f'-- Treating {counter} :{obj}{ext}.fits')
        # wave, flux_array, header = sr.import_fits_data(fits_address, instrument='OSIRIS')
        # wave_rest = wave/(1+z)
        #
        # if ext in ('_B', '_R'):
        #     flux = flux_array[idx_band][0]
        # else:
        #     flux = flux_array

    #     # Define wave and flux ranges
z_mean, z_err = sr.redshift_calculation(obsData['sample_data']['obs_waves'],
                                        obsData['sample_data']['emis_waves'])
norm_flux = obsData['sample_data']['norm_flux']
obj_list = ['B6479s', 'R8731s']

for idx_obj, obj in enumerate(['R8731s']):

    fits_address = data_folder / f'{obj}.fits'
    wave_data, flux_data = read_spectra(fits_address, scaleFlux=1)

    mask_address = data_folder / f'{obj}_mask.txt'
    mask_DF = sr.lineslogFile_to_DF(mask_address)

    # Individual line measurement
    lm = sr.LineMesurer(wave_data[0],
                        flux_data[0],
                        normFlux=norm_flux,
                        redshift=z_mean)
    fitConf = obsData[f'default_line_fitting']

    # Loop through the lines
    for lineLabel in ['H1_6563A_b']:

        # Get line ranges
        lineWaves = mask_DF.loc[lineLabel, 'w1':'w6'].values

        # Perform fit
        lm.fit_from_wavelengths(lineLabel, lineWaves, fit_conf=fitConf)

        # Display results
        lm.print_results(show_fit_report=True, show_plot=True)
                print(f'-- Treating voxel {idx_voxel} {idx_pair}')
                idx_j, idx_i = idx_pair
                voxel_dict = {}

                # local_mask = voxelFolder/f'{idx_j}-{idx_i}_mask_{obj}.txt'
                # local_lineslog = voxelFolder/f'{idx_j}-{idx_i}_lineslog_{obj}.txt'
                # grid_address_i = voxelFolder/f'{idx_j}-{idx_i}_LineGrid_{obj}.png'
                # pdfTableFile = voxelFolder/f'{idx_j}-{idx_i}_linesTable'
                # txtTableFile = voxelFolder/f'{idx_j}-{idx_i}_linesTable.txt'

                flux_voxel = cube[:, idx_j, idx_i].data.data * norm_flux
                flux_err = cube[:, idx_j, idx_i].var.data * norm_flux

                lm = sr.LineMesurer(wave,
                                    flux_voxel,
                                    input_err=flux_err,
                                    redshift=z_objs[i],
                                    normFlux=norm_flux)

                if verbose:
                    lm.plot_spectrum(specLabel=f'{obj} voxel {idx_j}-{idx_i}',
                                     log_scale=True)

                # Security check for pixels with nan values:
                idcs_nan = np.isnan(lm.flux)
                flux_interpolated = None

                if idcs_nan.any():
                    if region_mask[idx_j, idx_i]:
                        Interpolation = interp1d(lm.wave[~idcs_nan],
                                                 lm.flux[~idcs_nan],
示例#19
0
        objName = obsData['file_information']['object_list'][i]
        fitsFolder, fitsFile = file_address.parent, file_address.name
        masksFolder, masksFile = fitsFolder, fitsFile.replace(
            '.fits', '_masks.txt')
        lineLogFolder, lineLogFile = fitsFolder / 'flux_analysis', fitsFile.replace(
            '.fits', '_linesLog_Emission.txt')
        emissionSpectrumAddress = fitsFolder / 'flux_analysis' / f'{objName}_gasSpectrum.txt'
        plotFolder, plotFile = fitsFolder / 'flux_analysis', fitsFile.replace(
            '.fits', '_linesGrid_Emission')

        # Get fits data
        wave, flux = np.loadtxt(emissionSpectrumAddress, unpack=True)

        # Load line measurer object
        lm = sr.LineMesurer(wave,
                            flux,
                            masksFolder / masksFile,
                            normFlux=flux_norm)

        # Loop through the lines
        print(f'\n- {objName}:')
        obsLines = lm.linesDF.index.values
        for j, lineLabel in enumerate(obsLines):

            # Declare regions data
            print(f'-- {lineLabel}:')
            wave_regions = lm.linesDF.loc[lineLabel, 'w1':'w6'].values
            idcsLinePeak, idcsContinua = lm.define_masks(wave_regions)

            # Measure line data
            lm.line_properties(idcsLinePeak, idcsContinua, bootstrap_size=1000)
            LightFracPlotFile = outputFolder/f'{obj}{ext}_SSP_LightFrac.png'
            stellarPlotFile = outputFolder/f'{obj}{ext}_stellarFit.png'

            # Set wavelength and flux
            print(f'\n-- Treating {counter} :{obj}{ext}.fits')
            wave, flux_array, header = sr.import_fits_data(fits_file, instrument='OSIRIS')
            wave_rest = wave / (1 + z)
            idx_wave = (wave_rest >= wmin_array[i]) & (wave_rest <= wmax_array[i])
            flux = flux_array[idx_band][0] if ext in ('_B', '_R') else flux_array

            # Load the data
            obsWave, obsFlux = wave_rest[idx_wave], flux[idx_wave]
            specWave, specFlux = np.loadtxt(nebFluxNoNebCompFile, unpack=True)

            # Measuring objects
            lm = sr.LineMesurer(specWave, specFlux, lineLog_file, normFlux=flux_norm)
            sw = SSPsynthesizer()

            # Generate starlight files
            idcs_lines = ~lm.linesDF.index.str.contains('_b')
            gridFileName, outputFile, saveFolder, waveResample, fluxResample = sw.generate_starlight_files(starlightFolder,
                                                                                                             f'{obj}{ext}',
                                                                                                             specWave,
                                                                                                             specFlux,
                                                                                                             lm.linesDF.loc[idcs_lines])

            # # Launch starlight
            # print(f'\n-Initiating starlight: {obj}')
            # sw.starlight_launcher(gridFileName, starlightFolder)
            # print('\n-Starlight finished succesfully ended')
        # Set wavelength and flux
        print(f'\n-- Treating {counter} :{obj}{ext}.fits')
        wave, flux_array, header = sr.import_fits_data(fits_file,
                                                       instrument='OSIRIS')
        wave_rest = wave / (1 + z)
        idx_wave = (wave_rest >= wmin_array[i]) & (wave_rest <= wmax_array[i])
        flux = flux_array[idx_band][0] if ext in ('_B', '_R') else flux_array

        # Load line measurer object
        maskDF = pd.read_csv(objMask,
                             delim_whitespace=True,
                             header=0,
                             index_col=0)
        lm = sr.LineMesurer(wave_rest[idx_wave],
                            flux[idx_wave],
                            normFlux=flux_norm)

        # Fit and check the regions
        obsLines = maskDF.index.values
        for j, lineLabel in enumerate(obsLines):

            print(f'-- {lineLabel}:')
            wave_regions = maskDF.loc[lineLabel, 'w1':'w6'].values
            lm.fit_from_wavelengths(lineLabel, wave_regions, fit_conf=fit_conf)

            if lm.blended_check:
                plotFile = f'{obj}{ext}_{lineLabel}.png'
                lm.plot_fit_components(lm.fit_output,
                                       output_address=dataFolder /
                                       'flux_analysis' / plotFile)
                idx_j, idx_i = idx_pair
                print(
                    f'\n - Treating {idx_j}-{idx_i} ({idx_voxel}/{n_voxels})\n'
                )

                local_mask = voxelFolder / f'{idx_j}-{idx_i}_mask_{color}.txt'
                local_lineslog = voxelFolder / f'{idx_j}-{idx_i}_lineslog_{color}.txt'
                grid_address_i = voxelFolder / f'{idx_j}-{idx_i}_LineGrid_{color}.png'
                pdfTableFile = voxelFolder / f'{idx_j}-{idx_i}_linesTable_{color}'
                txtTableFile = voxelFolder / f'{idx_j}-{idx_i}_linesTable_{color}.txt'

                flux_voxel = data[:, idx_j, idx_i]

                lm = sr.LineMesurer(wave,
                                    flux_voxel,
                                    redshift=z_list[i],
                                    normFlux=norm_flux)
                if verbose:
                    lm.plot_spectrum()

                # Identify the emission lines
                norm_spec = lm.continuum_remover(
                    obsConf[obj]['noiseRegion_array'])
                obsLinesTable = lm.line_finder(
                    norm_spec,
                    noiseWaveLim=obsConf[obj]['noiseRegion_array'],
                    intLineThreshold=2.5)
                maskLinesDF = lm.match_lines(obsLinesTable,
                                             mask_global_DF,
                                             tol=10,
                                             find_line_borders=False)
示例#23
0
for i, file_address in enumerate(addressList):

    # Establish files location
    objName = obsData['file_information']['object_list'][i]
    fitsFolder, fitsFile = file_address.parent, file_address.name
    lineLogFolder, lineLogFile = fitsFolder / 'flux_analysis', fitsFile.replace(
        '.fits', '_linesLog.txt')
    simFolder, simConf = fitsFolder / 'chemical_analysis', fitsFile.replace(
        '_BR.fits', '_config.txt')
    inputLinesLog = f'{objName}_inputLinesLog.txt'
    outputDb = f'{objName}_fitting.db'
    outputTxt = f'{objName}_fitting.txt'
    print(f'-{objName}')

    obj1_model = sr.SpectraSynthesizer()
    lm = sr.LineMesurer(linesDF_address=lineLogFolder / lineLogFile)

    blended_dict = obsData['blended_groups']
    blended_list = []
    for group in blended_dict:
        blended_list += blended_dict[group].split('-')

    idcs_blended = lm.linesDF.index.isin(blended_list)

    # Asociate the corresponding flux to the appropiate line
    lm.linesDF.insert(loc=1, column='obsFlux', value=np.nan)
    lm.linesDF.insert(loc=2, column='obsFluxErr', value=np.nan)
    flux_Hbeta = lm.linesDF.loc['H1_4861A', 'intg_flux']
    lm.linesDF.loc[idcs_blended,
                   'obsFlux'] = lm.linesDF.loc[idcs_blended,
                                               'gauss_flux'] / flux_Hbeta
示例#24
0
for i, file_address in enumerate(addressList):

    # Open lineslog
    fitsFolder, fitsFile = file_address.parent, file_address.name
    lineLogFolder, lineLogFile = fitsFolder/'flux_analysis', fitsFile.replace('.fits', '_linesLog.txt')
    pdfLogFolder, pdfLogFile = fitsFolder / 'flux_analysis', fitsFile.replace('.fits', '_linesLog')

    objName = fitsFile.replace('.fits', '')
    print(f'- {i}: {objName}')

    # Set and crop the wavelength
    wave_rest, flux, header = sr.import_fits_data(fitsFolder/fitsFile, instrument='SDSS')
    idx_wave = (wave_rest >= obsData['sample_data']['wmin_array']) & (wave_rest <= obsData['sample_data']['wmax_array'])

    # Load line measurer object
    lm = sr.LineMesurer(wave_rest[idx_wave], flux[idx_wave], lineLogFolder / lineLogFile, normFlux=flux_norm)

    # Measure line fluxes
    idcs_lines = ~lm.linesDF.index.str.contains('_b')
    obsLines = lm.linesDF.loc[idcs_lines].index.values

    # Equivalent width Hbeta
    eqw_Hbeta, eqwErr_Hbeta = lm.linesDF.loc['H1_4861A', 'eqw'], lm.linesDF.loc['H1_4861A', 'eqw_err']
    eqw_entry = r'${:0.2f}$ $\pm$ ${:0.2f}$'.format(eqw_Hbeta, eqwErr_Hbeta)

    # Normalizing flux
    flux_Halpha = lm.linesDF.loc['H1_6563A', 'gauss_flux']
    flux_Hbeta = lm.linesDF.loc['H1_4861A', 'intg_flux']
    halpha_norm = flux_Halpha / flux_Hbeta
    halphaRatio_entry = r'${:0.2f}$'.format(halpha_norm)
    colorGrade = colorChooser(halpha_norm, 2.86)
    # Declare input files
    objFolder = resultsFolder / f'{obj}'
    fits_file = dataFolder / f'{obj}_{ext}.fits'
    objMask = dataFolder / f'{obj}_{ext}_mask.txt'
    results_file = objFolder / f'{obj}_{ext}_measurements.txt'
    lineLog_file = objFolder / f'{obj}_{ext}_linesLog_it3.txt'

    # output spectrum
    spectrum_ssp = objFolder / f'{obj}_starlight_spectrum.txt'

    # Load the data
    wave, flux_array, header = sr.import_fits_data(fits_file,
                                                   instrument='OSIRIS')
    flux = flux_array[idx_band][0] if ext in ('_B', '_R') else flux_array
    lm = sr.LineMesurer(wave,
                        flux,
                        redshift=z_array[i],
                        crop_waves=(wmin_array[i], wmax_array[i]))
    linesDF = sr.lineslogFile_to_DF(lineLog_file)

    # Add new masks
    linesDF = sr.lineslogFile_to_DF(lineLog_file)
    ini_mask, end_points = obsData[obj]['ini_mask_array'], obsData[obj][
        'end_mask_array']
    labels = ['cont_mask_' + str(int(x)) for x in ini_mask]
    for j, label_mask in enumerate(labels):
        linesDF.loc[labels[j], ['w3', 'w4']] = ini_mask[j], end_points[j]

    # Compute the mask array
    mask_array = np.zeros(lm.wave.size)

    for mask_label in linesDF.index:
示例#26
0
import src.specsiser as sr

# Declare the data location
obsFitsFile = Path('./sample_data/gp121903_BR.fits')
lineMaskFile = Path('./sample_data/gp121903_BR_mask.txt')
cfgFile = Path('./sample_data/gtc_greenpeas_data.ini')

# Load the data
obsConf = sr.loadConfData(cfgFile, objList_check=True, group_variables=False)
maskDF = sr.lineslogFile_to_DF(lineMaskFile)
wave, flux, header = sr.import_fits_data(obsFitsFile, instrument='OSIRIS')
user_conf = obsConf['gp121903_line_fitting']

# Declare line measuring object
lm = sr.LineMesurer(wave,
                    flux,
                    redshift=obsConf['sample_data']['z_array'][2],
                    normFlux=obsConf['sample_data']['norm_flux'])
lm.plot_spectrum()

# Find lines
norm_spec = lm.continuum_remover(
    noiseRegionLims=obsConf['sample_data']['noiseRegion_array'])
obsLinesTable = lm.line_finder(
    norm_spec,
    noiseWaveLim=obsConf['sample_data']['noiseRegion_array'],
    intLineThreshold=3)
matchedDF = lm.match_lines(obsLinesTable, maskDF)
lm.plot_spectrum(obsLinesTable=obsLinesTable,
                 matchedLinesDF=matchedDF,
                 specLabel=f'Emission line detection')
    # Open lineslog
    linesLogAddress = str(file_address).replace('.fits',
                                                '_treatedlinesLog.txt')
    tableLogAddress = str(file_address).replace('.fits', '_treatedlinesLog')
    figureLogAddress = str(file_address).replace('.fits',
                                                 '_treatedlinesLog.png')

    # Set and crop the wavelength
    wave_rest, flux, header = sr.import_fits_data(file_address,
                                                  instrument='SDSS')
    idx_wave = (wave_rest >= obsData['sample_data']['wmin_array']) & (
        wave_rest <= obsData['sample_data']['wmax_array'])

    # Load line measurer object
    lm = sr.LineMesurer(wave_rest[idx_wave],
                        flux[idx_wave] / fluxNorm,
                        linesDF_address=linesLogAddress)
    pdf = PdfPrinter()

    # Measure line fluxes
    idcs_lines = ~lm.linesDF.index.str.contains('_b')
    obsLines = lm.linesDF.loc[idcs_lines].index.values

    pdf.create_pdfDoc(tableLogAddress, pdf_type='table')
    pdf.pdf_insert_table(tableHeaders)

    flux_Hbeta = lm.linesDF.loc['H1_4861A', 'intg_flux']

    for lineLabel in obsLines:

        label_entry = lm.linesDF.loc[lineLabel, 'latexLabel']
                                   height_ratios=[2.5, 1],
                                   hspace=0.0,
                                   wspace=0.0)
        ax_big = fig.add_subplot(gs_obj[0, :])
        ax_small = fig.add_subplot(gs_obj[1, :])

        if obj in ['gp030321', 'gp101157', 'gp121903']:

            # Load spectrum
            wave, flux_array, header = sr.import_fits_data(fits_file,
                                                           instrument='OSIRIS')
            flux = flux_array[idx_band][0] if ext in ('_B',
                                                      '_R') else flux_array
            lm = sr.LineMesurer(wave,
                                flux,
                                redshift=z,
                                normFlux=flux_norm,
                                crop_waves=(wmin, wmax))

            # Labels for object
            obj_labels = labels_dict[obj]
            ion_array, wave_array, latex_array = label_decomposition(
                input_lines=obj_labels,
                combined_dict=obsData[f'{obj}_line_fitting'])
            latex_array = double_line_blended(latex_array)

            # Big spectrum
            ax_big.step(lm.wave_rest,
                        lm.flux,
                        color='tab:blue',
                        label=obj.replace('gp', 'GP'),
import numpy as np
from pathlib import Path
import src.specsiser as sr

spec_address = Path(
    '/home/vital/Dropbox/Astrophysics/Papers/gtc_greenpeas/data/gp030321_B.fits'
)
wave, flux_array, header = sr.import_fits_data(spec_address,
                                               instrument='OSIRIS')
flux, err, normFlux = flux_array[0][0], flux_array[3][0], 1e-17
lm_osiris = sr.LineMesurer(wave, flux, input_err=err, normFlux=normFlux)
lm_osiris.plot_spectrum(continuumFlux=lm_osiris.errFlux,
                        log_scale=True,
                        axConf={'title': 'OSIRIS spectrum'})
lineWaves = np.array([5600.0, 5635.0, 5651.0, 5675.0, 5697.0, 5729.0])
lm_osiris.fit_from_wavelengths('H1_4861A', lineWaves)
lm_osiris.print_results(show_plot=True)
#
spec_address = Path(
    '/home/vital/Astro-data/Observations/MUSE - Amorin/CGCG007.fits'
)  #D:/Google drive/Astrophysics/Datos/MUSE - Amorin/CGCG007.fits')
wave, cube, header = sr.import_fits_data(spec_address, instrument='MUSE')
idx_j, idx_i = 171, 171
flux_voxel = cube[:, idx_j, idx_i].data.data
flux_err = cube[:, idx_j, idx_i].var.data
lm_muse = sr.LineMesurer(wave, flux_voxel, input_err=flux_err)
lm_muse.plot_spectrum(continuumFlux=lm_muse.errFlux,
                      log_scale=True,
                      axConf={'title': 'MUSE spectrum'})
lineWaves = np.array([4835.0, 4868.0, 4877.0, 4891.0, 4913.0, 4941.0])
lm_muse.fit_from_wavelengths('H1_4861A', lineWaves)
        fits_address = data_folder / f'{obj}.fits'
        mask_address = data_folder / f'{obj}_mask.txt'
        wave_data, flux_data = read_spectra(fits_address, scaleFlux=1)
        mask_DF = sr.lineslogFile_to_DF(mask_address)
        output_log = data_folder / f'{obj}_linesLog.txt'

        # Blue and red arm have a different data array structure
        if obj == 'B6479s':
            wave, flux = wave_data[0], flux_data[0][0]
        if obj == 'R8731s':
            wave, flux = wave_data[0], flux_data[0]

        # Individual line measurement
        lm = sr.LineMesurer(wave,
                            flux,
                            input_err=flux_data[0][1],
                            normFlux=norm_flux,
                            redshift=z_mean)
        fitConf = obsData[f'{obj}_individual_line_fitting']
        lm.plot_spectrum(continuumFlux=lm.errFlux)

        # Loop through the lines
        gauss_fluxes = {}
        sigma_fluxes = {}
        for lineLabel in ('H1_4861A_b', 'O3_4959A_b', 'O3_5007A_b'):

            lineWaves = mask_DF.loc[lineLabel[0:-2], 'w1':'w6'].values

            # Perform fit
            lm.fit_from_wavelengths(lineLabel, lineWaves, fit_conf=fitConf)