def _res(x):
        z = x[0]
        v = x[1]
        lnA = x[2]

        line_flux = np.exp(lnA)

        residuals = []

        for cam in cams:
            wave = spectra.wave[cam]
            res = Resolution(spectra.resolution_data[cam][fiber, :])
            flux = spectra.flux[cam][fiber, :]
            ivar = spectra.ivar[cam][fiber, :]
            mask = spectra.mask[cam][fiber, :]

            tw = twave(wave.min(), wave.max())

            rflux = doublet_obs(z,
                                tw,
                                wave,
                                res,
                                continuum=0.0,
                                sigmav=v,
                                r=0.0,
                                linea=0.0,
                                lineb=lineb)
            rflux *= line_flux

            res = np.sqrt(ivar[mask]) * np.abs(flux[mask] - rflux[mask])

            residuals += res.tolist()

        residuals = np.array(residuals)

        print(z, v, lnA, residuals.sum())

        return residuals
    def _res(x):
        z             = x[0]
        v             = x[1]

        result        = 0.0

        residuals     = []

        prior         = gaussian_prior(z, rrz, rrzerr)
        prior        *= gaussian_prior(v,  90,    50.)
        
        for i, singlet in enumerate(singlets):
            lineidb   = singlet['INDEX']
            lineb     = singlet['WAVELENGTH']
            
            if singlet['MASKED'] == 1:
                continue

            postage   = postages[lineidb]
            cams      = list(postage.keys())

            lnA       = x[i + 2]
            line_flux = np.exp(lnA)

            for cam in cams:
                wave  = postage[cam].wave
                res   = Resolution(postage[cam].R)
                flux  = postage[cam].flux
                ivar  = postage[cam].ivar
                mask  = postage[cam].mask == 0

                tw    = twave(wave.min(), wave.max())

                # lineida irrelevant when line ratio, r, is zero.
                rflux      = doublet_obs(z, tw, wave, res, continuum=0.0, sigmav=v, r=0.0, linea=0.0, lineb=lineb)
                rflux     *= line_flux

                res        = np.sqrt(ivar[mask]) * (flux[mask] - rflux[mask])
                
                residuals += res.tolist()

        for i in np.arange(ndoublet):
            doublet   = doublets[doublets['DOUBLET'] == i]

            if doublet['MASKED'][0] == 1:
                continue

            linea     = doublet['WAVELENGTH'][0]
            lineb     = doublet['WAVELENGTH'][1]

            lineidb   = doublet['INDEX'][1]
            
            postage   = postages[lineidb]
            cams      = list(postage.keys())

            lnA       = x[2 * i + 2 + nsinglet]
            line_flux = np.exp(lnA)

            r         = x[2 * i + 3 + nsinglet]

            prior    *= gaussian_prior(r, 0.7, 0.2)
            
            for cam in cams:
                wave  = postage[cam].wave
                res   = Resolution(postage[cam].R)
                flux  = postage[cam].flux
                ivar  = postage[cam].ivar
                mask  = postage[cam].mask == 0

                tw    = twave(wave.min(), wave.max())
                
                # print(lineidb, cam, z, v, r, line_flux, X2)

                rflux      = doublet_obs(z, tw, wave, res, continuum=0.0, sigmav=v, r=r, linea=linea, lineb=lineb)
                rflux     *= line_flux

                res        = np.sqrt(ivar[mask]) * (flux[mask] - rflux[mask])
        
                residuals += res.tolist()

        # Include a parameter prior. 
        nelement   = len(residuals)

        residuals += [np.sqrt(2. * mlogprior(prior) / (1. + nelement))]            
        residuals  = np.array(residuals)
        
        return  residuals