def measure_eqw(wav,
                flux,
                z,
                continuum_region=[[6000., 6250.] * u.AA,
                                  [6800., 7000.] * u.AA],
                eqw_region=[6400, 6800] * u.AA):

    # Test if wav has units. If it does convert to Angstroms. If it doesn't
    # then give it units. This might be a bit hacky.

    try:
        wav.unit
        wav = wav.to(u.AA)
    except AttributeError:
        wav = wav * u.AA

    wav = wav / (1.0 + z)

    # eqw region
    eqw_inds = (wav > eqw_region[0]) & (wav < eqw_region[1])

    # index is true in the region where we fit the continuum
    blue_inds = (wav > continuum_region[0][0]) & (wav < continuum_region[0][1])
    red_inds = (wav > continuum_region[1][0]) & (wav < continuum_region[1][1])

    xdat = np.array(
        [continuum_region[0].mean().value, continuum_region[1].mean().value])
    ydat = np.array([np.median(flux[blue_inds]), np.median(flux[red_inds])])

    # fit power-law to continuum region
    mod = PowerLawModel()
    pars = mod.make_params()
    pars['exponent'].value = 1.0
    pars['amplitude'].value = 1.0

    out = minimize(resid, pars, args=(xdat, mod, ydat), method='leastsq')

    # Normalised flux
    f = flux / mod.eval(params=pars, x=wav.value)

    eqw = (f[eqw_inds][:-1] - 1.0) * np.diff(wav[eqw_inds])

    #    fig, ax = plt.subplots()
    #
    #    ax.scatter(wav[blue_inds] , flux[blue_inds], c='red')
    #    ax.scatter(wav[red_inds] , flux[red_inds], c='red')
    #    ax.scatter(wav[eqw_inds], flux[eqw_inds])
    #    ax.scatter(xdat, ydat, c='yellow', s=70)
    #    xs = np.arange( xdat.min(), xdat.max(), 1)
    #    ax.plot( xs, mod.eval(params=pars, x=xs) , c='red')
    #    plt.show()
    #    plt.close()

    return eqw.sum()
Exemple #2
0
                    d_l = j[2]

                    # call on luminosity function to plot lum curves
                    lum = L(flux, d_l, z, beta)
                    lum_err = L_err(flux_err, d_l, z, beta)

                    # calculating best fit parameters and covariances for the data lmfit

                    # call on weight function to get the weights of the luminosity errorss
                    weight_lum = weight(lum_err)

                    # making power law model for linear fits
                    model1 = PowerLawModel(prefix='pow_')

                    # make parameters with starting values
                    par1 = model1.make_params(pow_amplitude=1e30,
                                              pow_exponent=-1.0)

                    # create results for these parameters using the weights
                    result1 = model1.fit(lum, par1, x=time, weights=weight_lum)

                    # get a and N from the results
                    a1, N1 = result1.best_values[
                        'pow_exponent'], result1.best_values['pow_amplitude']

                    # call on power law function to create line with these parameters
                    decay_line = power_law1(time, N1, a1)

                    # plot scatter graph of the individual luminosity curves
                    plt.title(
                        f'Luminosity in the 8.56GHz band for GRB{first_GRB}')
                    plt.xscale("log")
                    T2 = T[idx1]
                    w1 = 1 / lum_err[idx2]
                    w2 = 1 / lum_err[idx1]

                log_T = np.log10(T)
                log_lum = np.log10(lum)
                log_Ts = np.log10(Ts)

                #calculating best fit parameters and covariances for the data lmfit
                #making powerlaw model for linear fits
                model1 = PowerLawModel(prefix='pow_')
                #making powerlaw model for log fits
                model2 = LinearModel(independent_vars=['x'])

                # make parameters with starting values:
                par1 = model1.make_params(pow_amplitude=1e55,
                                          pow_exponent=-1.0)  #linear powerlaw
                par2 = model2.make_params(m=1, c=55)  #log
                par3 = model1.make_params(pow_amplitude=1e5, pow_exponent=-1.0)
                par4 = model1.make_params(pow_amplitude=1e52,
                                          pow_exponent=-1.0)

                np.nan_to_num(weight_lum_new, copy=False)

                #running sets of fits
                result1 = model1.fit(
                    lum, par1, x=T,
                    weights=weight_lum)  #linear whole with weights
                result2 = model1.fit(lum_new, par1, x=T_new,
                                     weights=weight_ts)  #linear ts with weight
                result3 = model2.fit(
                    np.log10(lum), par2, x=np.log10(T),