Example #1
0
def process_Rfwhm(Rfwhm, wa, model, models):
    """ Convolve the input models using the Rfwhm option

    Return the new models.

    wa:  wavelength array, shape (N,)
    model: model flux array, shape (N,)
    models: list of model flux arrays each with shape (N,)

    Rfwhm is one of:

      'convolve_with_COS_FOS'
      a float
      an array floats with shape (N,)

    Returns
    -------
    new_model, new_models
    """

    model_out = None
    models_out = []

    #import pdb; pdb.set_trace()
    if Rfwhm is None:
        return model, models

    elif Rfwhm == 'convolve_with_COS_FOS':
        #print 'convolving with COS/FOS instrument profile'
        #import pdb; pdb.set_trace()
        model_out = convolve_with_COS_FOS(model, wa, use_COS_nuv=True)
        for m in models:
            #if m.min() < (1 - 1e-2):
            m = convolve_with_COS_FOS(m, wa, use_COS_nuv=True)
            models_out.append(m)

    elif isinstance(Rfwhm, float):
        #print 'Convolving with fwhm %.2f km/s' % Rfwhm
        # use a pixel velocity width 4 times smaller than the FWHM
        ndiv = 4.
        try:
            wa_dv = make_constant_dv_wa_scale(wa[0], wa[-1], Rfwhm / ndiv)
        except:
            import pdb
            pdb.set_trace()
        model_out = convolve_constant_dv(wa, model, wa_dv, ndiv)
        # do the same to every model if there's more than one
        for m in models:
            #if m.min() < (1 - 1e-2):
            m = convolve_constant_dv(wa, m, wa_dv, ndiv)
            models_out.append(m)
    else:
        raise ValueError('Unknown value for Rfwhm option')

    return model_out, models_out
Example #2
0
def process_Rfwhm(Rfwhm, wa, model, models):
    """ Convolve the input models using the Rfwhm option

    Return the new models.

    wa:  wavelength array, shape (N,)
    model: model flux array, shape (N,)
    models: list of model flux arrays each with shape (N,)

    Rfwhm is one of:

      'convolve_with_COS_FOS'
      a float
      an array floats with shape (N,)

    Returns
    -------
    new_model, new_models
    """

    model_out = None
    models_out = []

    #import pdb; pdb.set_trace()
    if Rfwhm is None:
        return model, models

    elif Rfwhm == 'convolve_with_COS_FOS':
        #print 'convolving with COS/FOS instrument profile'
        #import pdb; pdb.set_trace()
        model_out = convolve_with_COS_FOS(model, wa, use_COS_nuv=True)
        for m in models:
            #if m.min() < (1 - 1e-2):
            m = convolve_with_COS_FOS(m, wa, use_COS_nuv=True)
            models_out.append(m)

    elif isinstance(Rfwhm, float):
        #print 'Convolving with fwhm %.2f km/s' % Rfwhm
        # use a pixel velocity width 4 times smaller than the FWHM
        ndiv = 4.
        try:
            wa_dv = make_constant_dv_wa_scale(wa[0], wa[-1], Rfwhm / ndiv)
        except:
            import pdb
            pdb.set_trace()
        model_out = convolve_constant_dv(wa, model, wa_dv, ndiv)
        # do the same to every model if there's more than one
        for m in models:
            #if m.min() < (1 - 1e-2):
            m = convolve_constant_dv(wa, m, wa_dv, ndiv)
            models_out.append(m)
    else:
        raise ValueError('Unknown value for Rfwhm option')

    return model_out, models_out
Example #3
0
 def bin_shift_and_tilt_spline(self, order, singlebin, binSize, multiple,
                               shift, sigma, slope, offset, minuit,
                               **kwargs):
     """trying to smooth, interpolate, and integrate the fit."""
     mask = self.Orders[order]['mask']
     ok = reduce(np.logical_and,
                 [self.Orders[order][binSize][singlebin]['ok'], mask])
     iok = self.Orders[order][binSize][singlebin]['iok']
     iow = self.Orders[order]['iow'][iok]
     iof = self.Orders[order]['iof'][iok]
     wav = self.Orders[order]['wav'][ok]
     flx = self.Orders[order]['flx'][ok]
     err = self.Orders[order]['err'][ok]
     con = self.Orders[order]['con'][ok]
     pix = self.Orders[order]['pix'][ok]
     overflx = multiple * slope_to_array(
         slope,
         interp.interp_spline(
             wav + shift, iow,
             convolve.convolve_constant_dv(iow, iof, vfwhm=sigma))) + offset
     chi_square = np.sum((overflx - flx / con)**2 / (err / con)**2)
     if minuit == 0:
         return chi_square
     else:
         return chi_square, wav, flx / con, err / con, pix, overflx
Example #4
0
 def bin_shift_and_tilt_spline(self, order, singlebin, binSize, multiple, shift, sigma, slope, offset, minuit, **kwargs):
     """trying to smooth, interpolate, and integrate the fit."""
     mask = self.Orders[order]['mask']
     ok = reduce(np.logical_and, [self.Orders[order][binSize][singlebin]['ok'], mask])
     iok = self.Orders[order][binSize][singlebin]['iok']
     iow = self.Orders[order]['iow'][iok]
     iof = self.Orders[order]['iof'][iok]
     wav = self.Orders[order]['wav'][ok]
     flx = self.Orders[order]['flx'][ok]
     err = self.Orders[order]['err'][ok]
     con = self.Orders[order]['con'][ok]
     pix = self.Orders[order]['pix'][ok]
     overflx = multiple * slope_to_array(slope,
                                         wav + shift,
                                         interp.interp_spline(wav + shift,
                                                              iow,
                                                              convolve.convolve_constant_dv(iow,
                                                                                            iof,
                                                                                            vfwhm=sigma)
                                                              )
                                         ) + offset
     chi_square = np.sum((overflx - flx/con)**2 / (err/con)**2)
     if minuit == 0:
         return chi_square
     else:
         return chi_square, wav, flx/con, err/con, pix, overflx
Example #5
0
File: velplot.py Project: nhmc/H2
def process_Rfwhm(Rfwhm, wa, model, models):
    """ Convolve the input models using the Rfwhm option

    Return the new models.

    wa:  wavelength array, shape (N,)
    model: model flux array, shape (N,)
    models: list of model flux arrays each with shape (N,)

    Rfwm is one of:

      'convolve_with_COS_FOS'
      a float

    Returns
    -------
    new_model, new_models
    """

    model_out = None
    models_out = []

    if Rfwhm is None:
        return model, models

    elif Rfwhm == 'convolve_with_COS_FOS':
        print 'convolving with COS/FOS instrument profile'
        model_out = convolve_with_COS_FOS(model, wa, wa[1] - wa[0])
        models_out = [convolve_with_COS_FOS(m, wa, wa[1] - wa[0]) for m
                      in models]

    elif isinstance(Rfwhm, float):
        print 'Convolving with fwhm %.2f km/s' % Rfwhm
        # use a pixel velocity width 4 times smaller than the FWHM
        ndiv = 4.
        wa_dv = make_constant_dv_wa_scale(wa[0], wa[-1], Rfwhm / ndiv)
        model_out = convolve_constant_dv(wa, model, wa_dv, ndiv)
        # do the same to every model if there's more than one
        for m in models:
            models_out.append(convolve_constant_dv(wa, m, wa_dv, ndiv))
    else:
        raise ValueError('Unknown value for Rfwhm option')

    return model_out, models_out
Example #6
0
 def order_shift_and_scale_spline(self, order, multiple, shift, sigma,
                                  slope, offset, minuit, **kwargs):
     """trying to smooth, interpolate, and integrate the fit."""
     mask = self.Orders[order]['mask']
     iow = self.Orders[order]['iow']
     iof = self.Orders[order]['iof']
     wav = self.Orders[order]['wav'][mask]
     flx = self.Orders[order]['flx'][mask]
     err = self.Orders[order]['err'][mask]
     con = self.Orders[order]['con'][mask]
     pix = self.Orders[order]['pix'][mask]
     overflx = multiple * slope_to_array(
         slope,
         interp.interp_spline(
             wav + shift, iow,
             convolve.convolve_constant_dv(iow, iof, vfwhm=sigma))) + offset
     chi_square = np.sum((overflx - flx / con)**2 / (err / con)**2)
     if minuit == 0:
         return chi_square
     else:
         return chi_square, wav, flx / con, err / con, pix, overflx
Example #7
0
 def order_shift_and_scale_spline(self, order, multiple, shift, sigma, slope, offset, minuit, **kwargs):
     """trying to smooth, interpolate, and integrate the fit."""
     mask = self.Orders[order]['mask']
     iow = self.Orders[order]['iow']
     iof = self.Orders[order]['iof']
     wav = self.Orders[order]['wav'][mask]
     flx = self.Orders[order]['flx'][mask]
     err = self.Orders[order]['err'][mask]
     con = self.Orders[order]['con'][mask]
     pix = self.Orders[order]['pix'][mask]
     overflx = multiple * slope_to_array(slope,
                                         wav + shift,
                                         interp.interp_spline(wav + shift,
                                                              iow,
                                                              convolve.convolve_constant_dv(iow,
                                                                                            iof,
                                                                                            vfwhm=sigma)
                                                              )
                                         ) + offset
     chi_square = np.sum((overflx - flx/con)**2 / (err/con)**2)
     if minuit == 0:
         return chi_square
     else:
         return chi_square, wav, flx/con, err/con, pix, overflx
Example #8
0
def liske_conv(vel1, wav1, err1_in, disp1, vel2, wav2, err2_in, disp2, smoothing):
    """
    Finds a representative liske et al error for each chunk if the chunk were made
    of only noisy continuum.  Does this many times and give returns an average and standard deviation.
    """
    # give param info for chi2. see spectra_shift for more info on each param
    parinfo = [{} for i in range(3)]
    parinfo[0]["step"] = 0.005
    parinfo[1]["step"] = 1.0e-5
    parinfo[2]["step"] = 1.0e-6
    parinfo[0]["mpside"] = 2
    parinfo[1]["mpside"] = 2
    parinfo[2]["mpside"] = 2

    # list to put uncertainties in from each MC test. avearged later.
    uncerts = []

    # remove correction to error array before creating the flux array)
    err1 = array(err1_in) / sqrt(
        exp(
            -0.250538878193 * (log(smoothing / disp1)) ** 2 - 0.722721962458 * (log(smoothing / disp1)) - 0.811329540423
        )
    )
    err2 = array(err2_in) / sqrt(
        exp(
            -0.250538878193 * (log(smoothing / disp1)) ** 2 - 0.722721962458 * (log(smoothing / disp1)) - 0.811329540423
        )
    )

    for q in xrange(50):
        # create error arrays comprised of the median error
        med_err1 = median(array(err1_in))
        med_err2 = median(array(err2_in))
        err1 = med_err1 * ones_like(err1_in)
        err2 = med_err2 * ones_like(err2_in)

        flux1 = np.random.normal(1.0, med_err1, size=len(err1))
        flux2 = np.random.normal(1.0, med_err2, size=len(err2))

        # re-correct the error arrays.
        err1 = err1 * sqrt(
            exp(
                -0.250538878193 * (log(smoothing / disp1)) ** 2
                - 0.722721962458 * (log(smoothing / disp1))
                - 0.811329540423
            )
        )
        err2 = err2 * sqrt(
            exp(
                -0.250538878193 * (log(smoothing / disp1)) ** 2
                - 0.722721962458 * (log(smoothing / disp1))
                - 0.811329540423
            )
        )
        # convolve
        flux1_con = convolve.convolve_constant_dv(wav1, flux1, vfwhm=smoothing)
        flux2_con = convolve.convolve_constant_dv(wav2, flux2, vfwhm=smoothing)

        # find the spline coefficients for flux then error array
        kErr = UnivariateSpline(vel1, err1, s=0)
        kFlx = UnivariateSpline(vel1, flux1_con, s=0)

        # print 'in loop',pcont
        # print cor_flx_noise,cor_err_noise
        # calculate uncertatinty via Liske et al method
        [noise_liske] = Calc(kFlx(vel2), flux2_con, vel2, kErr(vel2), err2)

        # print noise_liske
        if noise_liske == inf:
            noise_liske = 0.0

        # correct liske et al uncertainty
        noise_liske = noise_liske / exp(
            -0.0815 * (log(smoothing / disp2)) ** 2 - 0.2490 * (log(smoothing / disp2)) - 0.3123
        )
        uncerts.append(noise_liske)

    avg_uncerts = average(array(uncerts))
    stdev_uncerts = std(array(uncerts))
    return [avg_uncerts, stdev_uncerts]
Example #9
0
             }

cfg = parse_config(config)
transitions = read_transitions(cfg.trfilename, ATOMDAT)

if 1:
    sp = barak.spec.read(cfg.spfilename)
    ndiv = 4.
    wa_dv = make_constant_dv_wa_scale(sp.wa[0], sp.wa[-1], cfg.Rfwhm / ndiv)

    expand_cont_adjustment = 5

    vp = readf26(cfg.f26name)
    lines = vp.lines[vp.lines.name != '<>']
    tau, ticks, alltau = find_tau(sp.wa, lines, ATOMDAT, per_trans=1)
    model = convolve_constant_dv(sp.wa, np.exp(-tau), wa_dv, ndiv)
    models = [convolve_constant_dv(sp.wa, np.exp(-t), wa_dv, ndiv) for t in
              alltau]
    adjust = [l for l in vp.lines if l['name'].strip() == '<>']
    print 'Nadjust', adjust
    if len(adjust) > 0:
        regions = vp.regions
        isort = regions.wmin.argsort()
        print 'Applying continuum adjustments for', len(adjust), 'regions'
        for val in adjust:
            wav0 = ATOMDAT['<>'].wa[0] * (1 + val['z'])
            i0 = regions.wmin[isort].searchsorted(wav0)
            i1 = regions.wmax[isort].searchsorted(wav0)
            #print i0, i1
            #import pdb; pdb.set_trace()
            assert i0 - 1 == i1
Example #10
0
}

cfg = parse_config(config)
transitions = read_transitions(cfg.trfilename, ATOMDAT)

if 1:
    sp = barak.spec.read(cfg.spfilename)
    ndiv = 4.
    wa_dv = make_constant_dv_wa_scale(sp.wa[0], sp.wa[-1], cfg.Rfwhm / ndiv)

    expand_cont_adjustment = 5

    vp = readf26(cfg.f26name)
    lines = vp.lines[vp.lines.name != '<>']
    tau, ticks, alltau = find_tau(sp.wa, lines, ATOMDAT, per_trans=1)
    model = convolve_constant_dv(sp.wa, np.exp(-tau), wa_dv, ndiv)
    models = [
        convolve_constant_dv(sp.wa, np.exp(-t), wa_dv, ndiv) for t in alltau
    ]
    adjust = [l for l in vp.lines if l['name'].strip() == '<>']
    print 'Nadjust', adjust
    if len(adjust) > 0:
        regions = vp.regions
        isort = regions.wmin.argsort()
        print 'Applying continuum adjustments for', len(adjust), 'regions'
        for val in adjust:
            wav0 = ATOMDAT['<>'].wa[0] * (1 + val['z'])
            i0 = regions.wmin[isort].searchsorted(wav0)
            i1 = regions.wmax[isort].searchsorted(wav0)
            #print i0, i1
            #import pdb; pdb.set_trace()