diff -r -c -b -N cosmomc/camb/cmbmain.f90 cosmomc_wiggles/camb/cmbmain.f90
*** cosmomc/camb/cmbmain.f90	2009-02-20 00:43:34.000000000 +0100
--- cosmomc_wiggles/camb/cmbmain.f90	2009-10-28 16:19:36.708218142 +0100
***************
*** 636,652 ****
  !     polarization will be calculated. For low values of k we
  !     use a logarithmic spacing. closed case dealt with by SetClosedkValues
           if (CP%WantScalars .and. CP%Reion%Reionization .and. CP%AccuratePolarization) then
!             dlnk0=2._dl/10/AccuracyBoost
              !Need this to get accurate low l polarization
           else
!             dlnk0=5._dl/10/AccuracyBoost
              if (CP%closed) dlnk0=dlnk0/2
           end if
  
           if (CP%AccurateReionization) dlnk0 = dlnk0/2
! 
!          dkn1=0.6_dl/taurst/AccuracyBoost 
!          dkn2=1.1_dl/taurst/AccuracyBoost 
            if (CP%WantTensors .or. CP%WantVectors) then
                dkn1=dkn1  *0.8_dl
                dlnk0=dlnk0/2 !*0.3_dl
--- 636,662 ----
  !     polarization will be calculated. For low values of k we
  !     use a logarithmic spacing. closed case dealt with by SetClosedkValues
           if (CP%WantScalars .and. CP%Reion%Reionization .and. CP%AccuratePolarization) then
! !wig
! !            dlnk0=2._dl/10/AccuracyBoost
!             dlnk0=2._dl/10/AccuracyBoost/kSrcSampleBoost
! !end wig
              !Need this to get accurate low l polarization
+ 
           else
! !wig
! !            dlnk0=5._dl/10/AccuracyBoost
!             dlnk0=5._dl/10/AccuracyBoost/kSrcSampleBoost
! !end wig
              if (CP%closed) dlnk0=dlnk0/2
           end if
  
           if (CP%AccurateReionization) dlnk0 = dlnk0/2
! !wig
! !         dkn1=0.6_dl/taurst/AccuracyBoost 
! !         dkn2=1.1_dl/taurst/AccuracyBoost 
!           dkn1=0.6_dl/taurst/AccuracyBoost/kSrcSampleBoost
!           dkn2=1.1_dl/taurst/AccuracyBoost/kSrcSampleBoost
! !end wig
            if (CP%WantTensors .or. CP%WantVectors) then
                dkn1=dkn1  *0.8_dl
                dlnk0=dlnk0/2 !*0.3_dl
***************
*** 1030,1036 ****
         qmax_int = min(qmax,max_bessels_etak/CP%tau0)
  
  
!        IntSampleBoost=AccuracyBoost
  
  !     Fixing the # of k for the integration. 
        
--- 1040,1048 ----
         qmax_int = min(qmax,max_bessels_etak/CP%tau0)
  
  
! !wig
! !     IntSampleBoost=AccuracyBoost
!       IntSampleBoost=AccuracyBoost/kIntSampleBoost
  
  !     Fixing the # of k for the integration. 
        
diff -r -c -b -N cosmomc/camb/inidriver.F90 cosmomc_wiggles/camb/inidriver.F90
*** cosmomc/camb/inidriver.F90	2008-09-16 18:04:28.000000000 +0200
--- cosmomc_wiggles/camb/inidriver.F90	2009-10-28 16:19:45.772967747 +0100
***************
*** 253,258 ****
--- 253,272 ----
           call Ini_SaveReadValues(trim(outroot) //'params.ini',1)
         end if
  
+ !wig
+            
+        kSrcSampleBoost= Ini_Read_Double('k_src_sample_boost',1.d0)
+        kIntSampleBoost= Ini_Read_Double('k_int_sample_boost',1.d0)
+        DoClsInterpol  = Ini_Read_Logical('do_cls_interpol',.true.)
+        ComputeEveryl  = Ini_Read_Logical('compute_every_l',.true.)
+ 
+        if (FeedbackLevel > 0) then
+           write (*,*)'do_cls_interpol is',DoClsInterpol
+           write (*,*)'compute_every_l is',ComputeEveryl
+           write (*,*)'primordial power spectra is: ',Power_Name
+        endif
+ !end wig
+ 
         call Ini_Close
  
         if (.not. CAMB_ValidateParams(P)) stop 'Stopped due to parameter error'
***************
*** 264,269 ****
--- 278,316 ----
         if (FeedbackLevel > 0) then
           Age = CAMB_GetAge(P) 
           write (*,'("Age of universe/GYr  = ",f7.3)') Age  
+ !wig
+          if (Power_Name.eq.'power_tilt') then
+ 
+              do i=1,P%InitPower%nn
+                 write (*,'("Scalar tilt          = ",f12.8)') P%InitPower%an(i)
+                 write (*,'("Scalar running       = ",f12.8)') P%InitPower%n_run(i)
+                 if (P%WantTensors) then
+                    write (*,'("primordial T/S       = ",f12.8)') P%InitPower%rat(i)
+                    write (*,'("tensor tilt          = ",f12.8)') P%InitPower%ant(i)
+                 endif                
+                 write (*,'("Wiggles amplitude    = ",f12.8)') P%InitPower%amposc(i)
+                 write (*,'("Wiggles frequency    = ",f12.8)') P%InitPower%logomega(i)
+                 write (*,'("Wiggles phase        = ",f12.8)') P%InitPower%phase(i)
+                 write (*,'("Wiggles power        = ",f12.8)') P%InitPower%powosc(i)
+              enddo
+              write (*,'("Wiggles pivot        = ",f12.8)') P%InitPower%k_0_osc
+ 
+           elseif ((Power_Name.eq.'power_sr').or.(Power_Name.eq.'power_dbi')) then
+ 
+              do i=1,P%InitPower%nn
+                 write (*,'("Slow-roll epsilon1   = ",f12.8)') P%InitPower%eps1(i)
+                 write (*,'("Slow-roll epsilon2   = ",f12.8)') P%InitPower%eps2(i)
+ 		write (*,'("Slow-roll epsilon3   = ",f12.8)') P%InitPower%eps3(i)
+                 write (*,'("DBI       delta1     = ",f12.8)') P%InitPower%del1(i)
+                 write (*,'("DBI       gamma      = ",f12.8)') P%InitPower%gam(i)
+                 write (*,'("TPL amplitude        = ",f12.8)') P%InitPower%Xsigma0(i)
+                 write (*,'("TPL scale            = ",f12.8)') P%InitPower%loginvsigma0(i)
+                 write (*,'("TPL phase            = ",f12.8)') P%InitPower%varphi(i)
+              enddo
+           else
+              stop 'Power_Name unknown'
+           endif
+ !end wig
         end if 
  
         call CAMB_GetResults(P)
diff -r -c -b -N cosmomc/camb/Makefile cosmomc_wiggles/camb/Makefile
*** cosmomc/camb/Makefile	2009-10-22 15:59:31.000000000 +0200
--- cosmomc_wiggles/camb/Makefile	2009-10-28 16:24:26.068717962 +0100
***************
*** 1,69 ****
! #CAMB Makefile
  
! #Edit for your compiler
! #Note there are many ifc versions, some of which behave oddly
  
- #Intel , -openmp toggles mutli-processor:
- #note version 10.0 gives wrong result for lensed when compiled with -openmp [fixed in 10.1]
- F90C     = ifort
- FFLAGS = -openmp -O2 -ip -W0 -WB -fpp2 -vec_report0
- 
- # Intel 9 on IA-64 (eg. COSMOS)
- # (do "module load icomp90" before compiling)
- #F90C = ifort
- #FFLAGS = -openmp -fpp2 -w -O3 -ip -mP2OPT_hlo_prefetch=F
- 
- #Intel ifc, add -openmp for multi-processor (some have bugs):
- #F90C     = ifc
- #FFLAGS = -O2 -Vaxlib -ip -W0 -WB -quiet -fpp2
- #some systems can can also add e.g. -tpp7 -xW
- 
- #G95 compiler
- #F90C   = g95
- #FFLAGS = -O2
- 
- #Gfortran compiler: if pre v4.3 add -D__GFORTRAN__
- #F90C     = gfc
- #FFLAGS =  -O2 
- 
- #SGI, -mp toggles multi-processor. Use -O2 if -Ofast gives problems.
- #F90C     = f90
- #FFLAGS  = -Ofast -mp
- 
- #Digital/Compaq fortran, -omp toggles multi-processor
- #F90C    = f90
- #FFLAGS  = -omp -O4 -arch host -math_library fast -tune host -fpe1
- 
- #Absoft ProFortran, single processor:
- #F90C     = f95
- #FFLAGS = -O2 -cpu:athlon -s -lU77 -w -YEXT_NAMES="LCS" -YEXT_SFX="_"
- 
- #NAGF95, single processor:
- #F90C     = f95
- #FFLAGS = -DNAGF95 -O3
- 
- #PGF90
- #F90C = pgf90
- #FFLAGS = -O2 -DESCAPEBACKSLASH
- 
- #Sun V880
- #F90C = mpf90
- #FFLAGS =  -O4 -openmp -ftrap=%none -dalign -DMPI
- 
- #Sun parallel enterprise:
- #F90C     = f95
- #FFLAGS =  -O2 -xarch=native64 -openmp -ftrap=%none
- #try removing -openmp if get bus errors. -03, -04 etc are dodgy.
- 
- #IBM XL Fortran, multi-processor (run gmake)
- #F90C     = xlf90_r
- #FFLAGS  = -DESCAPEBACKSLASH -DIBMXL -qsmp=omp -qsuffix=f=f90:cpp=F90 -O3 -qstrict -qarch=pwr3 -qtune=pwr3
- 
- #Settings for building camb_fits
- #Location of FITSIO and name of library
- FITSDIR       = /home/cpac/cpac-tools/lib
- FITSLIB       = cfitsio
- #Location of HEALPIX for building camb_fits
- HEALPIXDIR    = /home/cpac/cpac-tools/healpix
  
- include ./Makefile_main
\ No newline at end of file
--- 1,91 ----
!  # >>> DESIGNED FOR GMAKE <<<
!  #CAMB System unified Makefile 
  
! ext=$(shell uname | cut -c1-3)
! ifeq ($(ext),IRI)
! F90C=f90	
! FFLAGS = -Ofast -mp -n32 -LANG:recursive=ON
! endif
! 
! ifeq ($(ext),Lin)
! F90C=gfortran
! FFLAGS= -O -fopenmp #-D__GFORTRAN__
! endif 
! 
! ifeq ($(ext),OSF)
! F90C=f90
! FFLAGS= -omp -O -arch host -math_library fast -tune host -fpe1
! endif
! 
! ifeq ($(ext),Sun)
! F90C=f90
! FFLAGS= -O4 -xarch=native64 -openmp -ftrap=%none
! endif
! 
! ifeq ($(ext),AIX)
! F90C=xlf90_r
! FFLAGS= -O4 -qsmp=omp -qmaxmem=-1 -qstrict -qfree=f90 -qsuffix=f=f90:cpp=F90
! endif
! 
! 
! #Files containing evolution equations initial power spectrum module
! EQUATIONS     = equations
! POWERSPECTRUM = power_tilt
! REIONIZATION = reionization
! RECOMBINATION = recfast
! 
! #Module doing non-linear scaling
! NONLINEAR     = halofit
! 
! #Driver program
! DRIVER        = inidriver.F90
! #DRIVER        = sigma8.f90
! #DRIVER        = tester.f90
! 
! 
! CAMBLIB       = libcamb.a
! 
! #Shouldn't need to change anything else...
! 
! F90FLAGS      = $(FFLAGS)
! HEALPIXLD     = -L$(HEALPIXDIR)/lib -lhealpix -L$(FITSDIR) -l$(FITSLIB)
! 
! CAMBOBJ       = constants.o utils.o subroutines.o inifile.o $(POWERSPECTRUM).o $(RECOMBINATION).o $(REIONIZATION).o modules.o \
!         bessels.o $(EQUATIONS).o $(NONLINEAR).o lensing.o cmbmain.o camb.o
! 
! default: camb.$(ext)
! 
! all: camb.$(ext) $(CAMBLIB)
! 
! subroutines.o: constants.o utils.o
! $(POWERSPECTRUM): subroutines.o  inifile.o
! $(RECOMBINATION).o: subroutines.o inifile.o
! $(REIONIZATION).o: constants.o inifile.o
! modules.o: $(REIONIZATION).o
! bessels.o: modules.o
! $(EQUATIONS): bessels.o
! $(NONLINEAR).o:  modules.o
! lensing.o: bessels.o
! cmbmain.o: lensing.o
! camb.o: cmbmain.o
! 
! 
! camb.$(ext): $(CAMBOBJ) $(DRIVER)
! 	$(F90C) $(F90FLAGS) $(CAMBOBJ) $(DRIVER) -o $@
! 
! $(CAMBLIB): $(CAMBOBJ)
! 	ar -r $@ $?
! 
! camb_fits: writefits.f90 $(CAMBOBJ) $(DRIVER)
! 	$(F90C) $(F90FLAGS) -I$(HEALPIXDIR)/include tils.o $(CAMBOBJ) writefits.f90 $(DRIVER) $(HEALPIXLD) -DWRITE_FITS -o $@
! 
! %.o: %.f90
! 	$(F90C) $(F90FLAGS) -c $*.f90
! 
! utils.o:
! 	$(F90C) $(F90FLAGS) -c utils.F90	
! 
! clean:
! 	-rm -f *.o *.a *.d core *.mod
  
  
diff -r -c -b -N cosmomc/camb/modules.f90 cosmomc_wiggles/camb/modules.f90
*** cosmomc/camb/modules.f90	2008-09-16 17:57:58.000000000 +0200
--- cosmomc_wiggles/camb/modules.f90	2009-10-28 16:24:40.040209809 +0100
***************
*** 37,44 ****
          character(LEN=*), parameter :: version = 'Sept_08'
          
          integer :: FeedbackLevel = 0 !if >0 print out useful information about the model
! 
!         logical, parameter :: DebugMsgs=.false. !Set to true to view progress and timing
  
          logical, parameter :: DebugEvolution = .false. !Set to true to do all the evolution for all k
  
--- 37,46 ----
          character(LEN=*), parameter :: version = 'Sept_08'
          
          integer :: FeedbackLevel = 0 !if >0 print out useful information about the model
! !wig
! !        logical, parameter :: DebugMsgs=.false. !Set to true to view progress and timing
!         logical, parameter :: DebugMsgs=.false.
! !end wig
  
          logical, parameter :: DebugEvolution = .false. !Set to true to do all the evolution for all k
  
***************
*** 177,192 ****
  
        real(dl) :: lSampleBoost=1._dl
            !Increase lSampleBoost to increase sampling in lSamp%l for Cl interpolation
!           
!       real(dl) :: AccuracyBoost =1._dl  
! 
! 
            !Decrease step sizes, etc. by this parameter. Useful for checking accuracy.
            !Can also be used to improve speed significantly if less accuracy is required.              
            !or improving accuracy for extreme models. 
            !Note this does not increase lSamp%l sampling or massive neutrino q-sampling
! 
!       real(sp) :: lAccuracyBoost=1. 
            !Boost number of multipoles integrated in Boltzman heirarchy
  
        integer, parameter :: lmin = 2
--- 179,196 ----
  
        real(dl) :: lSampleBoost=1._dl
            !Increase lSampleBoost to increase sampling in lSamp%l for Cl interpolation
! !wig          
! !      real(dl) :: AccuracyBoost =1._dl  
!       real(dl) :: AccuracyBoost =8._dl  
! !end wig
            !Decrease step sizes, etc. by this parameter. Useful for checking accuracy.
            !Can also be used to improve speed significantly if less accuracy is required.              
            !or improving accuracy for extreme models. 
            !Note this does not increase lSamp%l sampling or massive neutrino q-sampling
! !wig
! !      real(sp) :: lAccuracyBoost=1. 
!       real(sp) :: lAccuracyBoost=2. 
! !end wig
            !Boost number of multipoles integrated in Boltzman heirarchy
  
        integer, parameter :: lmin = 2
***************
*** 202,208 ****
        integer, parameter:: l0max=4000
  
  !     lmax is max possible number of l's evaluated
!       integer, parameter :: lmax_arr = 100+l0max/7
   
          contains
        
--- 206,219 ----
        integer, parameter:: l0max=4000
  
  !     lmax is max possible number of l's evaluated
! !wig
! !      integer, parameter :: lmax_arr = 100+l0max/7
!       integer, parameter :: lmax_arr = 100+l0max
!       real(dl) :: kSrcSampleBoost=1._dl
!       real(dl) :: kIntSampleBoost=1._dl
!       logical :: DoClsInterpol=.true.
!       logical :: ComputeEveryl=.true.
! !end wig
   
          contains
        
***************
*** 554,559 ****
--- 565,589 ----
          integer lind, lvar, step,top,bot,ls(lmax_arr)
          real(dl) AScale
      
+ !wig        
+         if (ComputeEveryl) then
+            write(*,*) 'Warning: computing every l'           
+            if (max_l.gt.lmax_arr) then
+               write(*,*)'lmax_arr maxl',lmax_arr,max_l
+               stop
+            endif
+            lind=0
+            do lvar=2, max_l
+               lind=lind+1
+               ls(lind)=lvar 
+            end do
+ 
+            lSet%l0=lind
+            lSet%l = ls(1:lind)
+ 
+         else
+ !end wig
+ 
          Ascale=scale/lSampleBoost       
        
          lind=0
***************
*** 677,682 ****
--- 707,715 ----
          end if
          lSet%l0=lind
          lSet%l(1:lind) = ls(1:lind)
+ !wig        
+         endif
+ !end wig
          
        end subroutine initlval
  
***************
*** 696,702 ****
  
        xl = real(lSet%l(1:lSet%l0),dl)
        call spline(xl,iCl(1),max_ind,cllo,clhi,ddCl(1))
!      
              llo=1
              do il=lmin,lSet%l(max_ind)
                 xi=il
--- 729,736 ----
  
        xl = real(lSet%l(1:lSet%l0),dl)
        call spline(xl,iCl(1),max_ind,cllo,clhi,ddCl(1))
! !wig
!       if (DoClsInterpol) then
              llo=1
              do il=lmin,lSet%l(max_ind)
                 xi=il
***************
*** 712,717 ****
--- 746,765 ----
                         +(b0**3-b0)*ddCl(lhi))*ho**2/6
                
              end do
+       else
+         llo=1
+          do il=2,lSet%l(max_ind)
+             xi=il
+             if ((xi > lSet%l(llo+1)).and.(llo < max_ind)) then
+                llo=llo+1
+             end if
+ 
+             all_Cl(il) = iCl(llo+1)
+ 
+          end do
+          all_Cl(2)=iCl(1)
+       endif
+ !end wig 
  
        end subroutine InterpolateClArr
  
***************
*** 2018,2023 ****
--- 2066,2075 ----
               dlnkh = 0.02
               points = log(MTrans%TransferData(Transfer_kh,MTrans%num_q_trans,itf)/minkh)/dlnkh+1
  !             dlnkh = log(MTrans%TransferData(Transfer_kh,MTrans%num_q_trans,itf)/minkh)/(points-0.999)
+ !wig
+              write(*,*)'Transfert_SaveToFiles: points= ',points
+              write(*,*)'you may need to increase this to sample HF oscillations!'
+ !endwig
               allocate(outpower(points,CP%InitPower%nn))
               do in = 1, CP%InitPower%nn
                call Transfer_GetMatterPower(MTrans,outpower(1,in), itf, in, minkh,dlnkh, points)
diff -r -c -b -N cosmomc/camb/params.ini cosmomc_wiggles/camb/params.ini
*** cosmomc/camb/params.ini	2008-03-26 15:24:18.000000000 +0100
--- cosmomc_wiggles/camb/params.ini	2009-10-28 16:24:52.501027579 +0100
***************
*** 1,12 ****
  #Parameters for CAMB
  
  #output_root is prefixed to output file names
! output_root = test
  
  #What to do
  get_scalar_cls = T
  get_vector_cls = F
! get_tensor_cls = F
  get_transfer   = F
  
  #if do_lensing then scalar_output_file contains additional columns of l^4 C_l^{pp} and l^3 C_l^{pT}
--- 1,12 ----
  #Parameters for CAMB
  
  #output_root is prefixed to output file names
! output_root = tpl
  
  #What to do
  get_scalar_cls = T
  get_vector_cls = F
! get_tensor_cls = T
  get_transfer   = F
  
  #if do_lensing then scalar_output_file contains additional columns of l^4 C_l^{pp} and l^3 C_l^{pT}
***************
*** 32,42 ****
  #Main cosmological parameters, neutrino masses are assumed degenerate
  # If use_phyical set phyiscal densities in baryone, CDM and neutrinos + Omega_k
  use_physical   = T
! ombh2          = 0.022
! omch2          = 0.12
  omnuh2         = 0
  omk            = 0
! hubble         = 70
  #effective equation of state parameter for dark energy, assumed constant
  w              = -1
  #constant comoving sound speed of the dark energy (1=quintessence)
--- 32,42 ----
  #Main cosmological parameters, neutrino masses are assumed degenerate
  # If use_phyical set phyiscal densities in baryone, CDM and neutrinos + Omega_k
  use_physical   = T
! ombh2          = 0.0221
! omch2          = 0.1059
  omnuh2         = 0
  omk            = 0
! hubble         = 72.46
  #effective equation of state parameter for dark energy, assumed constant
  w              = -1
  #constant comoving sound speed of the dark energy (1=quintessence)
***************
*** 63,68 ****
--- 63,76 ----
  #Fraction of total omega_nu h^2 accounted for by each eigenstate, eg. 0.5 0.5
  nu_mass_fractions = 1
  
+ #Type of initial power spectrum: power_tilt or power_sr
+ power_name = power_sr
+  
+ ######################################################################################
+ 
+ #for power_tilt power spectra
+ 
+ 
  #Initial power spectrum, amplitude, spectral index and running. Pivot k in Mpc^{-1}.
  initial_power_num         = 1
  pivot_scalar              = 0.05
***************
*** 96,101 ****
--- 104,140 ----
  RECFAST_Heswitch = 6
  
  
+ #primordial power_tilted wiggle
+ osc_logomega(1)           = 1.
+ osc_amp(1)                = 0.
+ osc_phase(1)              = 0.
+ osc_logpower(1)           = 0.
+ osc_pivot                 = 0.05
+ 
+ #####################################################################################
+ 
+ #for power_sr power spectra
+ 
+ #slow-roll parameters
+ ln[10^{10} P_scalar] = 0.303161E+01
+ eps1(1) = 0.210391E-02
+ eps2(1) = 0.421584E-01
+ eps3(1) = 0
+ 
+ #dbi parameter
+ gam(1) = 1.
+ del1(1) = 0.
+ 
+ #wiggles parameters
+ logeps1invsigma0(1) = 1.
+ Xsigma0(1) = 0.
+ varphi(1) = 0
+ 
+ #tpl pivot
+ k_star = 0.05
+ 
+ #####################################################################################
+ 
  #Initial scalar perturbation mode (adiabatic=1, CDM iso=2, Baryon iso=3, 
  # neutrino density iso =4, neutrino velocity iso = 5) 
  initial_condition   = 1
***************
*** 186,188 ****
--- 225,239 ----
  #interpolation errors may be up to 3%
  #Decrease to speed up non-flat models a bit
  l_sample_boost          = 1
+ 
+ # Boost the sampling on k sources only, in unit of AccuracyBoost
+ k_src_sample_boost      = 1.
+ 
+ # Boost the sampling on k line of sight integral only, in unit of AccuracyBoost
+ k_int_sample_boost      = 1.
+ 
+ #Use spline interpolation for the Cls
+ do_cls_interpol         = T
+  
+ # Remove undersampling of the Cls: if true lSampleBoost has no effect
+ compute_every_l         = F
diff -r -c -b -N cosmomc/camb/power_tilt.f90 cosmomc_wiggles/camb/power_tilt.f90
*** cosmomc/camb/power_tilt.f90	2008-03-13 15:40:20.000000000 +0100
--- cosmomc_wiggles/camb/power_tilt.f90	2009-10-28 16:24:59.108718668 +0100
***************
*** 33,40 ****
  
       private
      
!       character(LEN=*), parameter :: Power_Name = 'power_tilt'
! 
        integer, parameter :: nnmax= 5 
         !Maximum possible number of different power spectra to use
  
--- 33,43 ----
  
       private
      
! !wig      character(LEN=*), parameter :: Power_Name = 'power_tilt'
!       character(80) :: Power_Name = 'power_sr'
!       logical, parameter :: useTplPhase = .false.      
!       logical, save :: useSecondOrder = .false.
! !end wig
        integer, parameter :: nnmax= 5 
         !Maximum possible number of different power spectra to use
  
***************
*** 51,56 ****
--- 54,109 ----
         real(dl) k_0_scalar, k_0_tensor
         real(dl) ScalarPowerAmp(nnmax)
   
+ !wig
+ ! slow-roll power
+        real(dl) P_scalar
+        real(dl) k_star
+        real(dl) eps1(nnmax)
+        real(dl) eps2(nnmax)
+        real(dl) eps3(nnmax)
+ !dbi params
+        real(dl) gam(nnmax)      
+        real(dl) del1(nnmax)
+ ! tpl power
+        real(dl) invsigma0(nnmax)
+        real(dl) loginvsigma0(nnmax)
+        real(dl) logeps1invsigma0(nnmax)
+        real(dl) Xsigma0(nnmax)
+        real(dl) varphi(nnmax) 
+ ! osc power
+        real(dl) amposc(nnmax)
+        real(dl) omega(nnmax)
+        real(dl) logomega(nnmax)
+        real(dl) phase(nnmax)
+        real(dl) powosc(nnmax)
+        real(dl) logpowosc(nnmax)
+        real(dl) k_0_osc
+ !Internal parameters              
+        real(dl) ks_pivot          !scalar pivot wave number
+        real(dl) kt_pivot          !tensor pivot wave number
+        real(dl) ko_pivot          !wiggles pivot wave number
+        real(dl) P_0_scalar(nnmax) !scalar normalisation during inflation
+        real(dl) P_0_tensor(nnmax) !tensor normalisation during inflation
+        real(dl) as0(nnmax)        !scalar power spectrum coefficients: order 0
+        real(dl) as1(nnmax)        !order 1
+        real(dl) as2(nnmax)        !order 2
+        real(dl) amps0(nnmax)      !amplitudes of tpl effect in scalar power: order 0
+        real(dl) amps1(nnmax)      !order 1
+        real(dl) amps2(nnmax)      !order 2
+        real(dl) args0(nnmax)      !argument of the modulations: order 0
+        real(dl) args1(nnmax)      !order 1
+        real(dl) args2(nnmax)      !order 2
+        real(dl) at0(nnmax)        !Tensor power spectrum coefficients: same as scalars
+        real(dl) at1(nnmax)
+        real(dl) at2(nnmax)
+        real(dl) ampt0(nnmax) 
+        real(dl) ampt1(nnmax)
+        real(dl) ampt2(nnmax)
+        real(dl) argt0(nnmax)
+        real(dl) argt1(nnmax) 
+        real(dl) argt2(nnmax)
+ !end wig
+         
        end Type InitialPowerParams
  
        real(dl) curv  !Curvature contant, set in InitializePowers     
***************
*** 69,74 ****
--- 122,129 ----
          Type (InitialPowerParams) :: AP
  
           AP%nn     = 1 !number of initial power spectra
+ !wig
+            
           AP%an     = 1 !scalar spectral index
           AP%n_run   = 0 !running of scalar spectral index
           AP%ant    = 0 !tensor spectra index
***************
*** 77,82 ****
--- 132,163 ----
           AP%k_0_tensor = 0.05
           AP%ScalarPowerAmp = 1
  
+          AP%amposc = 0.
+          AP%logomega = 2.
+          AP%omega = 10**(AP%logomega)
+          AP%phase = 0.
+          AP%logpowosc = 0.
+          AP%powosc = 10**(AP%logpowosc)
+          AP%k_0_osc = 0.05
+          
+          AP%eps1 = 0.01
+          AP%eps2 = 0.01 
+          AP%eps3 = 0.
+          AP%P_scalar=1.d-9
+          AP%k_star = 0.05            
+ 
+          AP%del1 = 0.
+          AP%gam = 1.
+         
+ 
+          AP%Xsigma0 = 0.                
+          AP%logeps1invsigma0 = 0. 
+          AP%loginvsigma0 = AP%logeps1invsigma0 - log10(AP%eps1) 
+          AP%invsigma0=10**(AP%loginvsigma0)
+          AP%varphi=0.
+         
+ !end wig
+ 
         end subroutine SetDefPowerParams
  
         subroutine InitializePowers(AParamSet,acurv)
***************
*** 85,90 ****
--- 166,181 ----
           !Could read spectra from disk here, do other processing, etc.
  
          real(dl) acurv
+ !wig
+         integer :: i        
+ !from Hankel functions
+         real(dl), parameter :: C_const = -0.7296
+ !wkb or uniform approx gives a slightly different constant
+         real(dl), parameter :: D_const = -0.7652
+ !        real(dl), parameter :: C_const = D_const
+ 
+         real(dl), parameter :: pi = 3.14159265
+ !end wig
  
          if (AParamSet%nn > nnmax) then
             write (*,*) 'To use ',AParamSet%nn,'power spectra you need to increase'
***************
*** 95,100 ****
--- 186,347 ----
          curv=acurv         
  
  !Write implementation specific code here...
+ !wig
+         if (Power_Name.eq.'power_tilt') then
+            
+            P%ks_pivot = P%k_0_scalar
+            P%kt_pivot = P%k_0_tensor
+            P%ko_pivot = P%k_0_osc
+ 
+            do i=1, P%nn        
+                                                 
+               P%omega(i) = 10**(P%logomega(i))
+               P%powosc(i) = 10**(P%logpowosc(i))
+ 
+               P%as0(i) = 0.
+               P%as1(i) = P%an(i)-1
+               P%as2(i) = P%n_run(i)/2.
+               P%amps0(i) = P%amposc(i)
+               P%amps1(i) = 0.
+               P%amps2(i) = 0.
+               P%args0(i) = P%phase(i)
+               P%args1(i) = P%omega(i)/P%powosc(i)
+               P%args2(i) = 0.
+               
+               P%at0(i) = 0.
+               P%at1(i) = P%ant(i)
+               P%at2(i) = 0.
+               P%ampt0(i) = P%amps0(i)
+               P%ampt1(i) = 0.
+               P%ampt2(i) = 0.
+               P%argt0(i) = P%phase(i)
+               P%argt1(i) = P%omega(i)/P%powosc(i)
+               P%argt2(i) = 0.
+              
+               P%P_0_scalar(i) = P%ScalarPowerAmp(i)
+               P%P_0_tensor(i) = P%rat(i)*P%ScalarPowerAmp(i)
+ 
+            enddo
+ 
+         elseif ((Power_Name.eq.'power_sr').or.(Power_Name.eq.'power_dbi')) then
+            if(abs(curv) .gt. 1.d-3) then
+               write(*,*)'Non flat cosmology, curv = ',curv
+               write(*,*)'Cannot use SR or TPL spectrum.'
+               stop
+            endif
+ 
+            P%ks_pivot = P%k_star
+            P%kt_pivot = P%k_star
+            P%ko_pivot = P%k_star
+ 
+ 
+            do i=1, P%nn        
+ 
+ !TODO: derive second order tpl corrections?
+               if (useSecondOrder) then
+                  if (P%Xsigma0(i).ne.0.) then
+                     useSecondOrder=.false.
+                     write(*,*)'PowerIni: SR second order disabled: TPL not implemented! '
+                  endif
+                  if ((P%del1(i).ne.0.).or.(P%gam(i).ne.1.)) then
+                     useSecondorder = .false.
+                     write(*,*)'PowerIni: SR second order disabled: DBI slow-roll not implemented!'
+                  endif
+               endif
+ 
+               if ((power_Name.eq.'power_sr').and. &
+                    ((P%del1(i).ne.0.).or.(P%gam(i).ne.1.)) ) then
+                  write(*,*)'power_sr with non-vanishing DBI params!'
+                  write(*,*)'resetting to standard SR'
+                  P%del1(i) = 0.
+                  P%gam(i) = 1.
+               endif
+ 
+ 
+ !first order tpl corrections
+               
+               P%loginvsigma0(i) = P%logeps1invsigma0(i) - log10(P%eps1(i))
+               P%invsigma0(i)=10**(P%loginvsigma0(i))
+               
+               P%amps0(i) = 2.*P%Xsigma0(i)
+               P%amps1(i) = pi*P%Xsigma0(i)*(2.*P%eps1(i) + P%eps2(i))
+               P%amps2(i) = 0.
+               if (useTplPhase) then
+                  P%args0(i) = 2.*P%invsigma0(i)*(1. + P%eps1(i)) + P%varphi(i)
+               else
+                  P%args0(i) = P%varphi(i)
+               endif
+               P%args1(i) = 2.*P%invsigma0(i)*P%eps1(i)
+               P%args2(i) = 0.
+ 
+               P%ampt0(i) = P%amps0(i)
+               P%ampt1(i) = pi*P%Xsigma0(i)*2.*P%eps1(i)
+               P%ampt2(i) = 0.
+               P%argt0(i) = P%args0(i)
+               P%argt1(i) = P%args1(i)
+               P%argt2(i) = 0.
+ 
+ 
+ !first and second order slowroll
+ 
+               P%as0(i) = 1. + P%eps1(i)*(-2.*(C_const+1.))-P%eps2(i)*C_const &
+                    + (C_const + 2.)*P%del1(i)
+               P%as1(i) = -2.*P%eps1(i) - P%eps2(i) + P%del1(i)
+               P%as2(i) = 0.
+                             
+               P%at0(i) = 1. + P%eps1(i)*(-2.*(C_const+1.))
+               P%at1(i) = -2.*P%eps1(i)
+               P%at2(i) = 0.
+ 
+               if (useSecondOrder) then
+                                                    
+ 
+                  P%as0(i) = P%as0(i) &
+                       + P%eps1(i)*P%eps1(i) * (2.*C_const**2 + 2.*C_const + pi**2/2. - 5.) &
+                       + P%eps1(i)*P%eps2(i) * (C_const**2 - C_const + 7.*pi**2/12. - 7.) &
+                       + P%eps2(i)*P%eps2(i) * (0.5*C_const**2 + pi**2/8. - 1.) &
+                       + P%eps2(i)*P%eps3(i) * (-0.5*C_const**2 + pi**2/24.)
+ 
+                  P%as1(i) = P%as1(i) &
+                       + P%eps1(i)*P%eps1(i) * 2.*(2.*C_const + 1.) &
+                       + P%eps1(i)*P%eps2(i) * (2.*C_const - 1.) &
+                       + P%eps2(i)*P%eps2(i) * C_const &
+                       + P%eps2(i)*P%eps3(i) * (-C_const)
+ 
+                  P%as2(i) = P%as2(i) &
+                       + P%eps1(i)*P%eps1(i) * 4. &
+                       + P%eps1(i)*P%eps2(i) * 2. &
+                       + P%eps2(i)*P%eps2(i) * 1. &
+                       + P%eps2(i)*P%eps3(i) * (-1.)
+ 
+                  
+                  P%at0(i) = P%at0(i) &
+                       +  P%eps1(i)*P%eps1(i) * (2.*C_const**2 + 2.*C_const + pi**2/2. - 5.) &
+                       +  P%eps1(i)*P%eps2(i) * (-C_const**2 - 2.*C_const + pi**2/12. - 2.)
+                  
+                  P%at1(i) = P%at1(i) &
+                       + P%eps1(i)*P%eps1(i) * 2.*(2.*C_const + 1.) &
+                       + P%eps1(i)*P%eps2(i) * 2.*(-C_const - 1.)
+ 
+                  P%at2(i) = P%at2(i)  &
+                       + P%eps1(i)*P%eps1(i) * 4. &
+                       + P%eps1(i)*P%eps2(i) * (-2.)
+                       
+               endif
+ 
+               
+               !Relative normalisation of scalar and tensor spectra
+                  
+               P%P_0_scalar(i)=P%P_scalar/(P%as0(i))
+ !              P%P_0_tensor(i)=P%P_scalar/(P%as0(i))*P%eps1(i)*16.
+               P%P_0_tensor(i)=P%P_scalar/(P%as0(i))*P%eps1(i)*16/P%gam(i)
+ 
+            enddo
+ 
+         else
+            stop 'No such initial power spectra'
+         endif
+ !end wig
  
         end subroutine InitializePowers
         
***************
*** 119,126 ****
          real(dl) ScalarPower,k, lnrat
          integer in
  
!           lnrat = log(k/P%k_0_scalar)
!           ScalarPower=P%ScalarPowerAmp(in)*exp((P%an(in)-1)*lnrat + P%n_run(in)/2*lnrat**2)   
   
        end function ScalarPower
  
--- 366,405 ----
          real(dl) ScalarPower,k, lnrat
          integer in
  
! !wig
!         real :: angle
!         
! !          lnrat = log(k/P%k_0_scalar)
! !          ScalarPower=P%ScalarPowerAmp(in)*exp((P%an(in)-1)*lnrat + P%n_run(in)/2*lnrat**2)   
!                  
!         lnrat = log(k/P%ks_pivot)
! 
!         if (Power_Name.eq.'power_tilt') then
! 
!            angle = P%args0(in) + P%args1(in) * ((k/P%ko_pivot)**P%powosc(in)-1.)
! 
!            ScalarPower = exp(P%as1(in)*lnrat + P%as2(in)*lnrat**2) &
!                 * (1. - P%amps0(in)*cos(angle))
! 
!         elseif ((Power_Name.eq.'power_sr').or.(Power_Name.eq.'power_dbi')) then
! 
!            angle = P%args0(in) + P%args1(in) * log(k/P%ko_pivot) 
! 
!            ScalarPower = (P%as0(in) + P%as1(in)*lnrat + 0.5*P%as2(in)*lnrat**2) &
!                 * (1. - P%amps0(in)*cos(angle)) - P%amps1(in)*sin(angle)
! 
!         else
!            stop 'No such initial power spectra'
!         endif
! 
!         ScalarPower = ScalarPower*P%P_0_scalar(in)
! 
!         if(ScalarPower .lt. 0.0) then
!            ScalarPower =0.
!         end if
! 
! 
! !end wig
   
        end function ScalarPower
  
***************
*** 142,150 ****
     
          integer in
  
!         TensorPower=P%rat(in)*P%ScalarPowerAmp(in)*exp(P%ant(in)*log(k/P%k_0_tensor))
          if (curv < 0) TensorPower=TensorPower*tanh(PiByTwo*sqrt(-k**2/curv-3)) 
  
         
        end function TensorPower
  
--- 421,463 ----
     
          integer in
  
! !wig
!          real :: lnrat,angle
!  
! !        TensorPower=P%rat(in)*P%ScalarPowerAmp(in)*exp(P%ant(in)*log(k/P%k_0_tensor))
! !        if (curv < 0) TensorPower=TensorPower*tanh(PiByTwo*sqrt(-k**2/curv-3)) 
!          
!          lnrat = log(k/P%kt_pivot)
!  
!          if (Power_Name.eq.'power_tilt') then
!  
!             angle = P%argt0(in) + P%argt1(in) * ((k/P%ko_pivot)**P%powosc(in)-1.)
!  
!             TensorPower = exp(P%at1(in)*lnrat) &
!                  * (1. - P%ampt0(in)*cos(angle))
!  
!  
!          elseif ((Power_Name.eq.'power_sr').or.(Power_Name.eq.'power_dbi')) then
!  
!             angle = P%argt0(in) + P%argt1(in) * log(k/P%ko_pivot)
!  
!             TensorPower = (P%at0(in) + P%at1(in)*lnrat + 0.5*P%at2(in)*lnrat**2) &
!                  * (1. - P%ampt0(in)*cos(angle)) - P%ampt1(in)*sin(angle)
!  
!          else
!             stop 'No such initial power spectra'
!          endif
!  
!          TensorPower = TensorPower*P%P_0_tensor(in)
!   
          if (curv < 0) TensorPower=TensorPower*tanh(PiByTwo*sqrt(-k**2/curv-3)) 
  
+          if(TensorPower .lt. 0.0) then
+             TensorPower=0.
+          endif
+          
+ !end wig
+ 
         
        end function TensorPower
  
***************
*** 156,161 ****
--- 469,477 ----
           logical, intent(IN) :: Scal, Tens
           integer num, Power_Descript
           num=0
+ !wig
+          if (Power_Name.eq.'power_tilt') then
+ 
           if (Scal) then
           num=num+1
           Keys(num) = 'n_s'
***************
*** 180,185 ****
--- 496,587 ----
             Vals(num) = P%rat(in)
           end if
           end if
+             num=num+1
+             Keys(num) = 'amposc'
+             Vals(num) = P%amposc(in)
+             num=num+1
+             Keys(num) = 'logomega'
+             Vals(num) = P%logomega(in)
+             num=num+1
+             Keys(num) = 'phase'
+             Vals(num) = P%phase(in)
+             num=num+1
+             Keys(num) = 'o_logpow'
+             Vals(num) = P%logpowosc(in)
+             num=num+1
+             Keys(num) = 'o_pivot'
+             Vals(num) = P%k_0_osc
+ 
+          elseif (Power_Name.eq.'power_sr') then
+ 
+             num=num+1
+             Keys(num) = 'k_star'
+             Vals(num) = P%k_star
+             num=num+1 
+             Keys(num) = 'P_scalar'
+             Vals(num) = P%P_scalar     
+             num=num+1 
+             Keys(num) = 'eps1'
+             Vals(num) = P%eps1(in)
+             num=num+1 
+             Keys(num) = 'eps2'
+             Vals(num) = P%eps2(in)
+             num=num+1 
+             Keys(num) = 'eps3'
+             Vals(num) = P%eps3(in)
+             num=num+1 
+             Keys(num) = 'isigma0'
+             Vals(num) = P%invsigma0(in)
+             num=num+1
+             Keys(num) = 'lisigma0'
+             Vals(num) = P%loginvsigma0(in)
+             num=num+1 
+             Keys(num) = 'Xsigma0'
+             Vals(num) = P%Xsigma0(in)
+             num=num+1 
+             Keys(num) = 'varphi'
+             Vals(num) = P%varphi(in)
+ 
+          elseif (Power_Name.eq.'power_dbi') then
+ 
+             num=num+1
+             Keys(num) = 'k_star'
+             Vals(num) = P%k_star
+             num=num+1 
+             Keys(num) = 'P_scalar'
+             Vals(num) = P%P_scalar     
+             num=num+1 
+             Keys(num) = 'eps1'
+             Vals(num) = P%eps1(in)
+             num=num+1 
+             Keys(num) = 'eps2'
+             Vals(num) = P%eps2(in)
+             num=num+1 
+             Keys(num) = 'eps3'
+             Vals(num) = P%eps3(in)
+             num=num+1 
+             Keys(num) = 'gam'
+             Vals(num) = P%gam(in)
+             num=num+1 
+             Keys(num) = 'del1'
+             Vals(num) = P%del1(in)
+             num=num+1 
+             Keys(num) = 'isigma0'
+             Vals(num) = P%invsigma0(in)
+             num=num+1
+             Keys(num) = 'lisigma0'
+             Vals(num) = P%loginvsigma0(in)
+             num=num+1 
+             Keys(num) = 'Xsigma0'
+             Vals(num) = P%Xsigma0(in)
+             num=num+1 
+             Keys(num) = 'varphi'
+             Vals(num) = P%varphi(in)
+ 
+          else
+             stop 'No such initial power spectra'
+          endif
+ !end wig
           Power_Descript = num
  
         end  function Power_Descript
***************
*** 195,200 ****
--- 597,609 ----
             InitPower%k_0_tensor = Ini_Read_Double_File(Ini,'pivot_tensor',InitPower%k_0_tensor) 
             InitPower%nn = Ini_Read_Int('initial_power_num')
             if (InitPower%nn>nnmax) stop 'Too many initial power spectra - increase nnmax in InitialPower'
+ 
+ !wig
+  
+            Power_Name = Ini_Read_String('power_name')
+            
+            if (Power_Name.eq.'power_tilt') then
+ !end wig
             InitPower%rat(:) = 1
             do i=1, InitPower%nn
  
***************
*** 208,215 ****
--- 617,676 ----
  
                InitPower%ScalarPowerAmp(i) = Ini_Read_Double_Array_File(Ini,'scalar_amp',i,1._dl) 
                !Always need this as may want to set tensor amplitude even if scalars not computed
+ 
+ !wig
+                  InitPower%logomega(i) = Ini_Read_Double_Array_File(Ini,'osc_logomega',i,2.d0)
+                  InitPower%amposc(i) = Ini_Read_Double_Array_File(Ini,'osc_amp',i,0.d0)             
+                  InitPower%omega(i) = 10**(InitPower%logomega(i))
+                  InitPower%phase(i) = Ini_Read_Double_Array_File(Ini,'osc_phase',i,0.d0)
+                  InitPower%logpowosc(i) = Ini_Read_Double_Array_File(Ini,'osc_logpower',i,0.d0)
+                  InitPower%powosc(i) = 10**(InitPower%logpowosc(i))
+ !end wig
+ 
             end do
            
+ !wig
+               InitPower%k_0_osc = Ini_Read_Double('osc_pivot',5d-2)
+ 
+            elseif ((Power_Name.eq.'power_sr').or.(Power_Name.eq.'power_dbi')) then
+            
+               do i=1, InitPower%nn
+               
+ !                 if (P%WantScalars .or. P%WantTransfer) then
+ 
+                  InitPower%eps1(i)= Ini_Read_Double_Array_File(Ini,'eps1',i,0.01d0)
+                  InitPower%eps2(i)=Ini_Read_Double_Array_File(Ini,'eps2',i,0.01d0)
+                  InitPower%eps3(i)=Ini_Read_Double_Array_File(Ini,'eps3',i,0.00d0)
+                  InitPower%gam(i)=Ini_Read_Double_Array_File(Ini,'gam',i,1.00d0)
+                  InitPower%del1(i)=Ini_Read_Double_Array_File(Ini,'del1',i,0.00d0)
+                  InitPower%logeps1invsigma0(i) &
+                       = Ini_Read_Double_Array_File(Ini,'logeps1invsigma0',i,1d0)
+                  InitPower%loginvsigma0(i) = InitPower%logeps1invsigma0(i) &
+                       - log10(InitPower%eps1(i))
+                  InitPower%invsigma0(i) = 10**(InitPower%loginvsigma0(i))
+                  InitPower%Xsigma0(i) = Ini_Read_Double_Array_File(Ini,'Xsigma0',i,0d0)
+                  InitPower%varphi(i) = Ini_Read_Double_Array_file(Ini,'varphi',i,0.d0)
+ 
+ !                 endif
+               
+               enddo
+            
+               InitPower%k_star= Ini_Read_Double('k_star',0.05d0)
+               InitPower%P_scalar= 1e-10*exp(Ini_Read_Double('ln[10^{10} P_scalar]',1d0))
+ 
+            else
+ 
+               stop 'No such initial power spectrum'
+ 
+            endif
+  
+ !end wig
+ 
+ 
+               
+ 
+ 
+           
         end  subroutine InitialPower_ReadParams 
  
  
diff -r -c -b -N cosmomc/distparams.ini cosmomc_wiggles/distparams.ini
*** cosmomc/distparams.ini	2009-09-17 15:14:51.000000000 +0200
--- cosmomc_wiggles/distparams.ini	2009-10-28 16:27:22.860218535 +0100
***************
*** 49,54 ****
--- 49,55 ----
  
  plot_meanlikes = T
  shade_meanlikes = T
+ td_meanlikes = F
  
  # if non-zero, output _thin file, thinned by thin_factor
  thin_factor = 0
***************
*** 100,106 ****
  
  #Parameters to use. If zero use all parameters which have lables.
  plotparams_num = 0
! plotparams = omegabh2 omegadmh2 tau ns
  
  #Get set label to empty to not include a parameter in the parameter_names file
  #lab[asz]=
--- 101,107 ----
  
  #Parameters to use. If zero use all parameters which have lables.
  plotparams_num = 0
! plotparams = omegabh2 omegadmh2 tau ns eps1 eps2 eps3 logomega amp psi logsig
  
  #Get set label to empty to not include a parameter in the parameter_names file
  #lab[asz]=
diff -r -c -b -N cosmomc/params_CMB.paramnames cosmomc_wiggles/params_CMB.paramnames
*** cosmomc/params_CMB.paramnames	2009-09-17 17:47:04.000000000 +0200
--- cosmomc_wiggles/params_CMB.paramnames	2009-10-28 16:57:21.948206196 +0100
***************
*** 5,14 ****
  omegak        \Omega_K
  fnu           f_\nu		#neutrino energy density as fraction of omegadmh2
  w             w			#constant equation of state parameter for scalar field dark energy 
! ns            n_s		#beware that pivot scale can change in .ini file
! nt            n_t
! nrun          n_{run}
! logA          log[10^{10} A_s]
  r             r			#ratio of tensor to scalar primordial amplitudes at pivot scale
  asz           A_{SZ}		#SZ template amplitude, as in WMAP
  omegal*       \Omega_\Lambda
--- 5,19 ----
  omegak        \Omega_K
  fnu           f_\nu		#neutrino energy density as fraction of omegadmh2
  w             w			#constant equation of state parameter for scalar field dark energy 
! eps1	      \epsilon_1-\delta_1	#first slow roll parameter - first sound flow param
! eps2	      \epsilon_2+\delta_1	#second slow roll parameter + first sound flow param
! eps3	      \epsilon_3		#third slow roll parameter
! unused	      \log(\epsilon_1)		#unused
! logfreq	      \log(\epsilon_1/\sigma_0)	#superimposed oscillation frequency
! amposc	      x\sigma_0			#superimposed oscillation amplitude
! phase	      \psi			#superimposed oscillation phase shift
! logpow	      log(p)			#superimposed power law dependence
! lnA           log[10^{10} P_*]
  r             r			#ratio of tensor to scalar primordial amplitudes at pivot scale
  asz           A_{SZ}		#SZ template amplitude, as in WMAP
  omegal*       \Omega_\Lambda
diff -r -c -b -N cosmomc/params_CMB.paramnames.powerlaw cosmomc_wiggles/params_CMB.paramnames.powerlaw
*** cosmomc/params_CMB.paramnames.powerlaw	1970-01-01 01:00:00.000000000 +0100
--- cosmomc_wiggles/params_CMB.paramnames.powerlaw	2009-10-28 16:57:55.436207482 +0100
***************
*** 0 ****
--- 1,25 ----
+ omegabh2      \Omega_b h^2	#physical baryon density
+ omegadmh2     \Omega_{DM} h^2	#physical dark matter density, including CDM and massive neutrinos
+ theta         \theta		#100 times the ratio of the angular diameter distance to the LSS sound horizon (approx)
+ tau           \tau
+ omegak        \Omega_K
+ fnu           f_\nu		#neutrino energy density as fraction of omegadmh2
+ w             w			#constant equation of state parameter for scalar field dark energy 
+ ns            n_s		#beware that pivot scale can change in .ini file
+ nt            n_t
+ nrun          n_{run}
+ unused	      unused		#unused
+ logfreq	      log(\omega)	#superimposed oscillation frequency
+ amposc	      A_\omega		#superimposed oscillation amplitude
+ phase	      \psi		#superimposed oscillation phase shift
+ logpow	      log(p)		#superimposed power law dependence
+ lnA           log[10^{10} P_*]
+ r             r			#ratio of tensor to scalar primordial amplitudes at pivot scale
+ asz           A_{SZ}		#SZ template amplitude, as in WMAP
+ omegal*       \Omega_\Lambda
+ age*          Age/Gyr
+ omegam*       \Omega_m
+ sigma8*       \sigma_8
+ zrei*         z_{re}
+ r10*          r_{10}		#tensor-scalar C_l amplitude at l=10
+ H0*           H_0		#hubble parameter is H0 km/s/Mpc
diff -r -c -b -N cosmomc/params.ini cosmomc_wiggles/params.ini
*** cosmomc/params.ini	2009-10-27 15:35:25.000000000 +0100
--- cosmomc_wiggles/params.ini	2009-10-28 16:57:27.384206271 +0100
***************
*** 67,84 ****
  
  #use fast-slow parameter distinctions to speed up 
  #(note for basic models WMAP3 code is only ~3x as fast as CAMB)
! use_fast_slow = F
  
  #Can use covariance matrix for proposal density, otherwise use settings below
  #Covariance matrix can be produced using "getdist" program.
! propose_matrix = params_CMB.covmat
  
  #If propose_matrix is blank (first run), can try to use numerical Hessian to 
  #estimate a good propose matrix. As a byproduct you also get an approx best fit point
  estimate_propose_matrix = F
  
  #Tolerance on log likelihood to use when estimating best fit point
! delta_loglike = 2
  
  #Scale of proposal relative to covariance; 2.4 is recommended by astro-ph/0405462 for Gaussians
  #If propose_matrix is much broader than the new distribution, make proportionately smaller
--- 67,84 ----
  
  #use fast-slow parameter distinctions to speed up 
  #(note for basic models WMAP3 code is only ~3x as fast as CAMB)
! use_fast_slow = T
  
  #Can use covariance matrix for proposal density, otherwise use settings below
  #Covariance matrix can be produced using "getdist" program.
! #propose_matrix = params_CMB.covmat
  
  #If propose_matrix is blank (first run), can try to use numerical Hessian to 
  #estimate a good propose matrix. As a byproduct you also get an approx best fit point
  estimate_propose_matrix = F
  
  #Tolerance on log likelihood to use when estimating best fit point
! delta_loglike = 0.1
  
  #Scale of proposal relative to covariance; 2.4 is recommended by astro-ph/0405462 for Gaussians
  #If propose_matrix is much broader than the new distribution, make proportionately smaller
***************
*** 135,146 ****
  #If true, generate checkpoint files and terminated runs can be restarted using exactly the same command
  #and chains continued from where they stopped
  #With checkpoint=T note you must delete all chains/file_root.* files if you want new chains with an old file_root
! checkpoint = F
  
  
  #CAMB parameters
  #If we are including tensors
! compute_tensors = F
  #Initial power spectrum amplitude point (Mpc^{-1})
  pivot_k = 0.05
  #If using tensors, enforce n_T = -A_T/(8A_s)
--- 135,146 ----
  #If true, generate checkpoint files and terminated runs can be restarted using exactly the same command
  #and chains continued from where they stopped
  #With checkpoint=T note you must delete all chains/file_root.* files if you want new chains with an old file_root
! checkpoint = T
  
  
  #CAMB parameters
  #If we are including tensors
! compute_tensors = T
  #Initial power spectrum amplitude point (Mpc^{-1})
  pivot_k = 0.05
  #If using tensors, enforce n_T = -A_T/(8A_s)
***************
*** 150,156 ****
  CMB_lensing = T
  #increase accuracy_level to run CAMB on higher accuracy
  #(default is about 0.3%, accuracy_level=2 around 0.1% at high l)
! accuracy_level = 1
  
  #If action = 1
  redo_likelihoods = T
--- 150,161 ----
  CMB_lensing = T
  #increase accuracy_level to run CAMB on higher accuracy
  #(default is about 0.3%, accuracy_level=2 around 0.1% at high l)
! accuracy_level = 6
! l_accuracy_level = 2
! l_sample_level = 1
! k_src_sample_level = 1.
! k_int_sample_level = 1.
! camb_compute_every_l = T
  
  #If action = 1
  redo_likelihoods = T
***************
*** 176,187 ****
  param[fnu] = 0 0 0 0 0
  param[w] = -1 -1 -1 0 0
  
! param[ns] = 0.95 0.5 1.5 0.02 0.01
! param[nt] = 0 0 0 0 0
! param[nrun] = 0 0 0 0 0
! 
! #log[10^10 A_s]
! param[logA] = 3 2.7 4 0.01 0.01
! param[r] = 0 0 0 0 0
  #SZ amplitude, as in WMAP analysis
  param[asz]= 1 0 2 0.4 0.4
--- 181,206 ----
  param[fnu] = 0 0 0 0 0
  param[w] = -1 -1 -1 0 0
  
! ### power law ###
! 
! #param[ns] = 0.95 0.5 1.5 0.02 0.01
! #param[nt] = 0 0 0 0 0
! #param[nrun] = 0 0 0 0 0
! #param[unused] = 0 0 0 0 0
! ### or slow roll (default) ###
! 
! param[eps1] = 0.1 0.00001 0.2 0.01 0.02
! param[eps2] = -0.04 -0.2 0.2  0.01 0.02
! param[eps3] = -0.04 -0.2 0.2  0.01 0.02
! param[unused] = 0 0 0 0 0
! 
! param[logfreq] = 1.5 1 2.5 0.5 0.2
! param[amposc] = 0.05 0 0.45 0.1 0.2
! param[phase] = 0 0 6.28 0.4 0.1
! param[logpow] = 0 0 0 0 0
! 
! #ln[10^10 A_s]
! param[lnA] = 3 2.7 4 0.01 0.01
! param[r] = 1 1 1 0 0
  #SZ amplitude, as in WMAP analysis
  param[asz]= 1 0 2 0.4 0.4
diff -r -c -b -N cosmomc/source/CMB_Cls_simple.f90 cosmomc_wiggles/source/CMB_Cls_simple.f90
*** cosmomc/source/CMB_Cls_simple.f90	2009-10-21 22:14:28.000000000 +0200
--- cosmomc_wiggles/source/CMB_Cls_simple.f90	1970-01-01 01:00:00.000000000 +0100
***************
*** 1,506 ****
- !Use CAMB
- module CMB_Cls
-   use cmbtypes
-   use CAMB, only : CAMB_GetResults, CAMB_GetAge, CAMBParams, CAMB_SetDefParams,Transfer_GetMatterPower, &
-           AccuracyBoost,  Cl_scalar, Cl_tensor, Cl_lensed, outNone, w_lam, &
-           CAMBParams_Set, MT, CAMBdata, NonLinear_Pk, Reionization_GetOptDepth, CAMB_GetZreFromTau, &
-           CAMB_GetTransfers,CAMB_FreeCAMBdata,CAMB_InitCAMBdata, CAMB_TransfersToPowers, &
-           initial_adiabatic,initial_vector,initial_iso_baryon,initial_iso_neutrino, initial_iso_neutrino_vel
-           
-   use settings
-   use snovae
-   use bao
-   use HST
-   use IO
-   implicit none
-   logical :: Use_SN =.false. !Compute Supernovae likelihoods only when background changes
-   logical :: Use_HST =.false. !Compute HST likelihoods only when background changes
-   logical :: Use_BAO = .false.
- 
-   logical :: compute_tensors = .false.
-   logical :: CMB_lensing = .false.
-   logical :: use_nonlinear = .false.
- 
-   Type ParamSetInfo  
-     
-     Type (CosmoTheory) :: Theory
-     Type (CAMBdata)    :: Transfers
-     Type (CMBParams)   :: LastParams
-   end Type ParamSetInfo
- 
-   integer, parameter :: ScalClOrder(3) = (/1,3,2/), TensClOrder(4) = (/1,4,2,3/)
-       !Mapping of CAMB CL array ordering to TT , TE, EE, BB  
-   integer :: ncalls = 0
-   type(CAMBParams)  CAMBP 
-   logical :: w_is_w  = .true.
- 
- contains
-   subroutine CMBToCAMB(CMB,P)
-     use LambdaGeneral
-     type(CMBParams) CMB
-     type(CAMBParams)  P
-     P = CAMBP
-     P%omegab = CMB%omb
-     P%omegan = CMB%omnu
-     P%omegac = CMB%omc
-     P%omegav = CMB%omv
-     P%H0 = CMB%H0
-     P%Reion%redshift= CMB%zre    
-     if (w_is_w) then
-      w_lam = CMB%w
-     else
-      P%InitialConditionVector(initial_iso_baryon) = CMB%w
-      w_lam = -1
-     end if
-   end subroutine CMBToCAMB
- 
-  function RecomputeTransfers (A, B)
-    logical RecomputeTransfers
-    type(CMBParams) A, B
- 
-    RecomputeTransfers =  .not. (A%omb == B%omb .and. A%omc == B%omc .and. A%omv == B%omv .and. &
-              A%omnu == B%omnu .and. A%zre == B%zre .and. A%omk == B%omk .and. A%w == B%w)
-               
-  end function RecomputeTransfers
- 
- 
-  subroutine GetCls(CMB,Info, Cls, error)
-    use ModelParams, only : ThreadNum
- #ifdef DR71RG 
-    use lrggettheory
-    real(dl) :: getabstransferscale 
-    !! BR09: this variable is for renormalizing the power spectra to the z=0 value; 
-    !this is the assumption of the LRG model.
- #endif     
-    type(CMBParams) CMB
-    integer error
-    Type(ParamSetInfo) Info
-    real Cls(lmax,1:num_Cls)
-    type(CAMBParams)  P
-    logical NewTransfers
-    integer zix
-    character(LEN=128) :: LogLine
-   
-   
-     error = 0
-     Newtransfers = .false.
-   
-     if (RecomputeTransfers(CMB, Info%LastParams))  then
-      !Slow parameters have changed
-          call CAMB_InitCAMBdata(Info%Transfers)
-          call CMBToCAMB(CMB, P) 
-       
-          if (Feedback > 1) write (*,*) 'Calling CAMB'
-          Threadnum =num_threads
-   
-          call CAMB_GetTransfers(P, Info%Transfers, error)
-          NewTransfers = .true.
-          Info%LastParams = CMB
-          if (Use_SN) then
-             Info%Theory%SN_Loglike = SN_LnLike(CMB)
-          else
-             Info%Theory%SN_Loglike = 0     
-          end if  
-          if (Use_BAO) then
-             Info%Theory%BAO_loglike = BAO_LnLike(CMB)
-          else
-             Info%Theory%BAO_loglike = 0
-          end if 
-          if (Use_HST) then
-             Info%Theory%HST_Loglike = HST_LnLike(CMB)
-          else
-             Info%Theory%HST_Loglike = 0     
-          end if  
-          
-          ncalls=ncalls+1
-          if (mod(ncalls,100)==0 .and. logfile_unit/=0) then
-           write (logLine,*) 'CAMB called ',ncalls, ' times'
-           call IO_WriteLog(logfile_unit,logLine)
-          end if 
-          if (Feedback > 1) write (*,*) 'CAMB done'
- 
-     end if 
-     
- !    if ((error==0) .and. (Newtransfers .or. any(CMB%InitPower /= Info%LastParams%InitPower))) then
-        !Use the initial power spectra to get the Cls and matter power spectrum
-      if (error == 0) then
-       !Always get everything again. Slight waste of time in general, but allows complete mixing of fast
-       !parameters, and works with lensing
- 
-          call SetCAMBInitPower(Info%Transfers%Params,CMB,1)       
- 
-          call CAMB_TransfersToPowers(Info%Transfers)
-             !this sets slow CAMB params correctly from value stored in Transfers
-            
-          call SetTheoryFromCAMB(Info%Theory)
-     
-          if (any(Info%Theory%cl(:,1) < 0 )) then
-             error = 1
-             !Kill initial power spectra that go negative
-             return
-          end if
-    
-          if (Use_LSS) then
-             Info%Theory%sigma_8 = Info%Transfers%MTrans%sigma_8(matter_power_lnzsteps,1)
- #ifdef DR71RG 
-             !! BR09 get lrgtheory info
-             if (num_matter_power /= 0 .and. use_dr7lrg) then
-                 do zix = 1,matter_power_lnzsteps
-                  if(zix .eq. iz0lrg .or. zix .eq. izNEARlrg .or. zix .eq. izMIDlrg .or. zix .eq. izFARlrg) then
-                    call Transfer_GetMatterPowerAndNW(Info%Transfers%MTrans,&
-                      Info%Theory%matter_power(:,zix),matter_power_lnzsteps-zix+1,&
-                       1,matter_power_minkh, matter_power_dlnkh,num_matter_power,&
-                        kmindata,getabstransferscale, &
-                        Info%Theory%mpk_nw(:,zix),Info%Theory%mpkrat_nw_nl(:,zix))
-                      if(zix == iz0lrg) powerscaletoz0(1) = getabstransferscale**2.0d0
-                      if(zix == izNEARlrg)   powerscaletoz0(2) = powerscaletoz0(1)/getabstransferscale**2.0d0
-                      if(zix == izMIDlrg)   powerscaletoz0(3) = powerscaletoz0(1)/getabstransferscale**2.0d0
-                      if(zix == izFARlrg)   powerscaletoz0(4) = powerscaletoz0(1)/getabstransferscale**2.0d0
-                   else  !! not an LRG redshift, so call regular function.
-                    call Transfer_GetMatterPower(Info%Transfers%MTrans,&
-                      Info%Theory%matter_power(:,zix),matter_power_lnzsteps-zix+1,&
-                      1,matter_power_minkh, matter_power_dlnkh,num_matter_power)
-                   end if
-                  end do
-                  if(zix == iz0lrg) powerscaletoz0(1) = 1.0d0
-              else if (num_matter_power /= 0) then
-             !! end BR09 get lrgtheory info
- #else
-             if (num_matter_power /= 0) then
- #endif
-                 do zix = 1,matter_power_lnzsteps
-                  call Transfer_GetMatterPower(Info%Transfers%MTrans,& 
-                    Info%Theory%matter_power(:,zix),matter_power_lnzsteps-zix+1,&
-                     1,matter_power_minkh, matter_power_dlnkh,num_matter_power) 
-                  end do             
-              end if
-          else
-             Info%Theory%sigma_8 = 0
-          end if
- 
-      end if
-      if (error /= 0) return
- 
-      call ClsFromTheoryData(Info%Theory, CMB, Cls)
-      
-  end subroutine GetCls
- 
-  subroutine SetTheoryFromCAMB(Theory)
-    Type(CosmoTheory) Theory
-    real, parameter :: cons =  2.726e6**2*2*pi
-    real nm
-    integer l
- 
-    !The reason we store tensors separately is that can then importance sample re-computing scalars only,
-    !using the stored tensor C_l
- 
-     do l = 2, lmax
- 
-        nm = cons/(l*(l+1))
-        if (CMB_Lensing) then
-             Theory%cl(l,1:num_clsS) =  nm*Cl_lensed(l,1, TensClOrder(1:num_clsS))
-          else 
-             Theory%cl(l,1:num_clsS) =  nm*Cl_scalar(l,1, scalClOrder(1:num_clsS))
-        end if         
-    
-        if (compute_tensors .and. l<=lmax_tensor) then
-             Theory%cl_tensor(l,1:num_cls) =  nm*Cl_tensor(l,1, TensClOrder(1:num_cls))
-        end if
-     end do 
-     
-     if (num_cls>num_clsS) Theory%cl(:,num_clsS+1:num_cls) = 0 
- 
- 
-  end subroutine SetTheoryFromCAMB
- 
-  subroutine GetClsInfo(CMB, Theory, error, DoCls, DoPk)
-    use ModelParams, only : ThreadNum
- #ifdef DR71RG
-    use lrggettheory
-    real(dl) :: getabstransferscale
-    !! BR09: this variable is for renormalizing the power spectra to the z=0 value;
-    !this is the assumption of the LRG model.
- #endif
-    type(CMBParams) CMB
-    Type(CosmoTheory) Theory
-    integer error
-    logical, intent(in) :: DoCls, DoPk
-    type(CAMBParams)  P
-    logical MatterOnly
-    integer zix
-    error = 0
-    Threadnum =num_threads
-    call CMBToCAMB(CMB, P)
-    P%OnlyTransfers = .false.
-    call SetCAMBInitPower(P,CMB,1)   
-     
-    MatterOnly = .false.
-    if (DoPk) then
-       P%WantTransfer = .true.
-       if (.not. DoCls) then
-          MatterOnly = .true.         
-          P%WantScalars = .false.
-          P%WantTensors = .false.
-       end if
-    end if
-    if (DoCls) then
-       !Assume we just want Cls to higher l
-       P%WantScalars = .true.
-       !P%WantTensors = .false.
-       !compute_tensors = .false.
-       P%WantTensors = compute_tensors 
-        
-        if (.not. DoPk) then
-         P%WantTransfer = .false.
-        end if
-    end if
-    
-    call CAMB_GetResults(P)
-    error = 0 !using error optional parameter gives seg faults on SGI
-    if (error==0) then
-        
-       if (DoCls) then
-   
-        Theory%cl_tensor(2:lmax_tensor,1:num_cls) = 0
-        call SetTheoryFromCAMB(Theory)
-       end if
- 
- !!BR09 new addition, putting LRGs back here as well, same structure as above.  
-       if (DoPk) then 
-          Theory%sigma_8 = MT%sigma_8(matter_power_lnzsteps,1)
- 
- #ifdef DR71RG
-          !! BR09 get lrgtheory info
-          if (num_matter_power /= 0 .and. use_dr7lrg) then
-              do zix = 1,matter_power_lnzsteps
-               if(zix .eq. iz0lrg .or. zix .eq. izNEARlrg .or. zix .eq. izMIDlrg .or. zix .eq. izFARlrg) then
-                 call Transfer_GetMatterPowerAndNW(MT,&
-                   Theory%matter_power(:,zix),matter_power_lnzsteps-zix+1,&
-                    1,matter_power_minkh, matter_power_dlnkh,num_matter_power,&
-                     kmindata,getabstransferscale, &
-                     Theory%mpk_nw(:,zix),Theory%mpkrat_nw_nl(:,zix))
-                      if(zix == iz0lrg) powerscaletoz0(1) = getabstransferscale**2.0d0
-                      if(zix == izNEARlrg)   powerscaletoz0(2) = powerscaletoz0(1)/getabstransferscale**2.0d0
-                      if(zix == izMIDlrg)   powerscaletoz0(3) = powerscaletoz0(1)/getabstransferscale**2.0d0
-                      if(zix == izFARlrg)   powerscaletoz0(4) = powerscaletoz0(1)/getabstransferscale**2.0d0
-                else  !! not an LRG redshift, so call regular function.
-                 call Transfer_GetMatterPower(MT,&
-                   Theory%matter_power(:,zix),matter_power_lnzsteps-zix+1,&
-                   1,matter_power_minkh, matter_power_dlnkh,num_matter_power)
-                end if
-              end do
-              if(zix == iz0lrg) powerscaletoz0(1) = 1.0d0
-               else if (num_matter_power /= 0) then
-          !! end BR09 get lrgtheory info
- #else
-             if (num_matter_power /= 0) then
- #endif
-                 do zix = 1,matter_power_lnzsteps
-                  call Transfer_GetMatterPower(MT,&
-                    Theory%matter_power(:,zix),matter_power_lnzsteps-zix+1,&
-                     1,matter_power_minkh, matter_power_dlnkh,num_matter_power)
-                  end do
-             end if
-       end if
-       Theory%Age = CAMB_GetAge(P)
- 
-    end if
-  end subroutine GetClsInfo
- 
- 
-  subroutine InitCAMB(CMB,error, DoReion)
-    type(CMBParams), intent(in) :: CMB
-    logical, optional, intent(in) :: DoReion
-    logical WantReion
-    type(CAMBParams)  P
-    integer error
-    
-    if (present(DoReion)) then
-     WantReion = DoReion
-    else
-     WantReion = .true.
-    end if
- 
-    call CMBToCAMB(CMB, P)
-    call CAMBParams_Set(P,error,WantReion)
- 
-  end subroutine InitCAMB
- 
-  function GetOpticalDepth(CMB)
-    type(CMBParams) CMB
-    real GetOpticalDepth
-    type(CAMBParams)  P
-    integer error
-    
-    call CMBToCAMB(CMB, P)
-    call CAMBParams_Set(P,error)
- 
-    if (error/= 0) then
-       GetOpticalDepth = -1 
-    else
-       GetOpticalDepth = Reionization_GetOptDepth(P%Reion, P%ReionHist) 
-    end if
-  end  function GetOpticalDepth
- 
-  function GetZreFromTau(CMB, tau)
-    type(CMBParams) CMB
-    real, intent(in) :: tau
-    real GetZreFromTau
-    type(CAMBParams)  P
-    
-    call CMBToCAMB(CMB, P)
-    GetZreFromTau = CAMB_GetZreFromTau(P,dble(tau))
-  
-  end  function GetZreFromTau 
-  
-  function GetAge(CMB, Info)
-    !Return <0 if error
-    real GetAge
-    type(CMBParams) CMB
-    Type(ParamSetInfo) Info
-    type(CAMBParams)  P
-    call CMBToCAMB(CMB, P)
-  
-    Info%Theory%Age = CAMB_GetAge(P)
-  
-    GetAge = Info%Theory%Age
-  end function GetAge
- 
-  subroutine InitCAMBParams(P)
-    use lensing
-    use ModelParams
-    use Lya
-    use mpk
-    type(CAMBParams)  P 
-    integer zix
-    real redshifts(matter_power_lnzsteps)
- 
-         Threadnum =num_threads
-         w_lam = -1
-         call CAMB_SetDefParams(P)
-  
-         P%OutputNormalization = outNone
-       
-         P%WantScalars = .true.
-         P%WantTensors = compute_tensors
-         P%WantTransfer = Use_LSS
- 
-         P%Max_l=lmax
-         P%Max_eta_k=lmax*2
-         P%Max_l_tensor=lmax_tensor
-         P%Max_eta_k_tensor=lmax_tensor*5./2
-  
-         P%Transfer%k_per_logint=0
-  
-         if (use_nonlinear) then
-          P%NonLinear = NonLinear_Pk
-          P%Transfer%kmax = 1.2
-         else
-          P%Transfer%kmax = 0.8
-         end if
-         if (Use_Lya) P%Transfer%kmax = lya_kmax
-         P%Transfer%num_redshifts = matter_power_lnzsteps
-         
-         if (AccuracyLevel > 1) then
-           if (USE_LSS) then
-             P%Transfer%high_precision=.true.
-             P%Transfer%kmax=P%Transfer%kmax + 0.2
-            end if
-           AccuracyBoost = AccuracyLevel
-           lAccuracyBoost = AccuracyLevel
-           lSampleBoost = AccuracyLevel
-           P%AccurateReionization = .true.
-         end if
-                 
-         if (max_transfer_redshifts < matter_power_lnzsteps) then
-           stop 'Need to manually set max_transfer_redshifts larger in CAMB''s modules.f90'
-         end if
-         if (use_LSS) then
-            redshifts(1) = 0
-            
-            do zix=2, matter_power_lnzsteps
-             !Default Linear spacing in log(z+1) if matter_power_lnzsteps > 1           
-              redshifts(zix) = exp( log(matter_power_maxz+1) * &
-                 real(zix-1)/(max(2,matter_power_lnzsteps)-1) )-1
-                !put in max(2,) to stop compilers complaining of div by zero
-            end do
-            
-            if (use_mpk) call mpk_SetTransferRedshifts(redshifts) !can modify to use specific redshifts
-            if (redshifts(1) > 0.0001) call MpiStop('mpk redshifts: lowest redshift must be zero')
-            do zix=1, matter_power_lnzsteps 
-             !CAMB's ordering is from highest to lowest
-             P%Transfer%redshifts(zix) = redshifts(matter_power_lnzsteps-zix+1)
-            end do
-         else 
-           P%Transfer%redshifts(1) = 0
-          end if   
-         
-         P%Num_Nu_Massive = 3
-         P%Num_Nu_Massless = 0.04
-         P%InitPower%nn = 1
-         P%AccuratePolarization = num_cls/=1 
-         P%Reion%use_optical_depth = .false.
-         P%OnlyTransfers = .true.
- 
-         if (CMB_Lensing) then
-             P%DoLensing = .true.
-             P%Max_l = lmax +250 + 50 !+50 in case accuracyBoost>1 and so odd l spacing
-             P%Max_eta_k = P%Max_l*2 
-         end if
-         
-         lensing_includes_tensors = .false.
- 
-         P%Scalar_initial_condition = initial_vector
-         P%InitialConditionVector = 0
-         P%InitialConditionVector(initial_adiabatic) = -1
- 
- 
-  end subroutine InitCAMBParams
- 
-  subroutine CMB_Initialize(Info)
-    Type(ParamSetInfo) Info
-    type(CAMBParams)  P 
-         compute_tensors = Ini_Read_Logical('compute_tensors',.false.)
-         if (num_cls==3 .and. compute_tensors) write (*,*) 'WARNING: computing tensors with num_cls=3 (BB=0)'
-         CMB_lensing = Ini_Read_Logical('CMB_lensing',.false.)
- 
-         if (Feedback > 0 ) then
-           write (*,*) 'Computing tensors:', compute_tensors
-           write (*,*) 'Doing CMB lensing:',CMB_lensing
-           write(*,'(" lmax           = ",1I4)') lmax       
-           if (compute_tensors) write(*,'(" lmax_tensor    = ",1I4)') lmax_tensor
-           write(*,'(" Number of C_ls = ",1I4)') num_cls
-         end if
- 
-         if (CMB_lensing) num_clsS = num_cls   !Also scalar B in this case
-      
-         call InitCAMBParams(P)
-   
-         call CAMB_InitCAMBdata(Info%Transfers)
-     
-         P%WantTensors = compute_tensors
-         Info%LastParams%omb = -1 !Make sure we calculate the CMB first time called
-         CAMBP = P
-         
-  end subroutine CMB_Initialize
- 
- 
-  subroutine AcceptReject(accpt, CurParams, Trial)
-    logical, intent(in) :: accpt
-    Type(ParamSetInfo) CurParams, Trial
- 
-    if (.not. associated(CurParams%Transfers%ClTransScal%Delta_p_l_k,&
-                         Trial%Transfers%ClTransScal%Delta_p_l_k)) then
-     !If they point to same memory don't need to free anything
-     if (accpt) then
-        call CAMB_FreeCAMBdata(CurParams%Transfers)
-     else
-        call CAMB_FreeCAMBdata(Trial%Transfers)
-     end if
- 
-    end if
- 
-  end subroutine AcceptReject
- 
- end module CMB_Cls
- 
--- 0 ----
diff -r -c -b -N cosmomc/source/CMB_Cls_simple.F90 cosmomc_wiggles/source/CMB_Cls_simple.F90
*** cosmomc/source/CMB_Cls_simple.F90	1970-01-01 01:00:00.000000000 +0100
--- cosmomc_wiggles/source/CMB_Cls_simple.F90	2009-10-28 16:33:14.328468040 +0100
***************
*** 0 ****
--- 1,530 ----
+ !Use CAMB
+ module CMB_Cls
+   use cmbtypes
+   use CAMB, only : CAMB_GetResults, CAMB_GetAge, CAMBParams, CAMB_SetDefParams,Transfer_GetMatterPower, &
+           AccuracyBoost,  Cl_scalar, Cl_tensor, Cl_lensed, outNone, w_lam, &
+           CAMBParams_Set, MT, CAMBdata, NonLinear_Pk, Reionization_GetOptDepth, CAMB_GetZreFromTau, &
+           CAMB_GetTransfers,CAMB_FreeCAMBdata,CAMB_InitCAMBdata, CAMB_TransfersToPowers, &
+           initial_adiabatic,initial_vector,initial_iso_baryon,initial_iso_neutrino, initial_iso_neutrino_vel
+           
+   use settings
+   use snovae
+   use bao
+   use HST
+   use IO
+   implicit none
+   logical :: Use_SN =.false. !Compute Supernovae likelihoods only when background changes
+   logical :: Use_HST =.false. !Compute HST likelihoods only when background changes
+   logical :: Use_BAO = .false.
+ 
+   logical :: compute_tensors = .false.
+   logical :: CMB_lensing = .false.
+   logical :: use_nonlinear = .false.
+ 
+   Type ParamSetInfo  
+     
+     Type (CosmoTheory) :: Theory
+     Type (CAMBdata)    :: Transfers
+     Type (CMBParams)   :: LastParams
+   end Type ParamSetInfo
+ 
+   integer, parameter :: ScalClOrder(3) = (/1,3,2/), TensClOrder(4) = (/1,4,2,3/)
+       !Mapping of CAMB CL array ordering to TT , TE, EE, BB  
+   integer :: ncalls = 0
+   type(CAMBParams)  CAMBP 
+   logical :: w_is_w  = .true.
+ 
+ contains
+   subroutine CMBToCAMB(CMB,P)
+     use LambdaGeneral
+     type(CMBParams) CMB
+     type(CAMBParams)  P
+     P = CAMBP
+     P%omegab = CMB%omb
+     P%omegan = CMB%omnu
+     P%omegac = CMB%omc
+     P%omegav = CMB%omv
+     P%H0 = CMB%H0
+     P%Reion%redshift= CMB%zre    
+     if (w_is_w) then
+      w_lam = CMB%w
+     else
+      P%InitialConditionVector(initial_iso_baryon) = CMB%w
+      w_lam = -1
+     end if
+   end subroutine CMBToCAMB
+ 
+  function RecomputeTransfers (A, B)
+    logical RecomputeTransfers
+    type(CMBParams) A, B
+ 
+    RecomputeTransfers =  .not. (A%omb == B%omb .and. A%omc == B%omc .and. A%omv == B%omv .and. &
+              A%omnu == B%omnu .and. A%zre == B%zre .and. A%omk == B%omk .and. A%w == B%w)
+               
+  end function RecomputeTransfers
+ 
+ 
+  subroutine GetCls(CMB,Info, Cls, error)
+    use ModelParams, only : ThreadNum
+ #ifdef DR71RG 
+    use lrggettheory
+    real(dl) :: getabstransferscale 
+    !! BR09: this variable is for renormalizing the power spectra to the z=0 value; 
+    !this is the assumption of the LRG model.
+ #endif     
+    type(CMBParams) CMB
+    integer error
+    Type(ParamSetInfo) Info
+    real Cls(lmax,1:num_Cls)
+    type(CAMBParams)  P
+    logical NewTransfers
+    integer zix
+    character(LEN=128) :: LogLine
+   
+   
+     error = 0
+     Newtransfers = .false.
+   
+     if (RecomputeTransfers(CMB, Info%LastParams))  then
+      !Slow parameters have changed
+          call CAMB_InitCAMBdata(Info%Transfers)
+          call CMBToCAMB(CMB, P) 
+       
+          if (Feedback > 1) write (*,*) 'Calling CAMB'
+          Threadnum =num_threads
+   
+          call CAMB_GetTransfers(P, Info%Transfers, error)
+          NewTransfers = .true.
+          Info%LastParams = CMB
+          if (Use_SN) then
+             Info%Theory%SN_Loglike = SN_LnLike(CMB)
+          else
+             Info%Theory%SN_Loglike = 0     
+          end if  
+          if (Use_BAO) then
+             Info%Theory%BAO_loglike = BAO_LnLike(CMB)
+          else
+             Info%Theory%BAO_loglike = 0
+          end if 
+          if (Use_HST) then
+             Info%Theory%HST_Loglike = HST_LnLike(CMB)
+          else
+             Info%Theory%HST_Loglike = 0     
+          end if  
+          
+          ncalls=ncalls+1
+          if (mod(ncalls,100)==0 .and. logfile_unit/=0) then
+           write (logLine,*) 'CAMB called ',ncalls, ' times'
+           call IO_WriteLog(logfile_unit,logLine)
+          end if 
+          if (Feedback > 1) write (*,*) 'CAMB done'
+ 
+     end if 
+     
+ !    if ((error==0) .and. (Newtransfers .or. any(CMB%InitPower /= Info%LastParams%InitPower))) then
+        !Use the initial power spectra to get the Cls and matter power spectrum
+      if (error == 0) then
+       !Always get everything again. Slight waste of time in general, but allows complete mixing of fast
+       !parameters, and works with lensing
+ 
+          call SetCAMBInitPower(Info%Transfers%Params,CMB,1)       
+ 
+          call CAMB_TransfersToPowers(Info%Transfers)
+             !this sets slow CAMB params correctly from value stored in Transfers
+            
+          call SetTheoryFromCAMB(Info%Theory)
+     
+          if (any(Info%Theory%cl(:,1) < 0 )) then
+             error = 1
+             !Kill initial power spectra that go negative
+             return
+          end if
+    
+          if (Use_LSS) then
+             Info%Theory%sigma_8 = Info%Transfers%MTrans%sigma_8(matter_power_lnzsteps,1)
+ #ifdef DR71RG 
+             !! BR09 get lrgtheory info
+             if (num_matter_power /= 0 .and. use_dr7lrg) then
+                 do zix = 1,matter_power_lnzsteps
+                  if(zix .eq. iz0lrg .or. zix .eq. izNEARlrg .or. zix .eq. izMIDlrg .or. zix .eq. izFARlrg) then
+                    call Transfer_GetMatterPowerAndNW(Info%Transfers%MTrans,&
+                      Info%Theory%matter_power(:,zix),matter_power_lnzsteps-zix+1,&
+                       1,matter_power_minkh, matter_power_dlnkh,num_matter_power,&
+                        kmindata,getabstransferscale, &
+                        Info%Theory%mpk_nw(:,zix),Info%Theory%mpkrat_nw_nl(:,zix))
+                      if(zix == iz0lrg) powerscaletoz0(1) = getabstransferscale**2.0d0
+                      if(zix == izNEARlrg)   powerscaletoz0(2) = powerscaletoz0(1)/getabstransferscale**2.0d0
+                      if(zix == izMIDlrg)   powerscaletoz0(3) = powerscaletoz0(1)/getabstransferscale**2.0d0
+                      if(zix == izFARlrg)   powerscaletoz0(4) = powerscaletoz0(1)/getabstransferscale**2.0d0
+                   else  !! not an LRG redshift, so call regular function.
+                    call Transfer_GetMatterPower(Info%Transfers%MTrans,&
+                      Info%Theory%matter_power(:,zix),matter_power_lnzsteps-zix+1,&
+                      1,matter_power_minkh, matter_power_dlnkh,num_matter_power)
+                   end if
+                  end do
+                  if(zix == iz0lrg) powerscaletoz0(1) = 1.0d0
+              else if (num_matter_power /= 0) then
+             !! end BR09 get lrgtheory info
+ #else
+             if (num_matter_power /= 0) then
+ #endif
+                 do zix = 1,matter_power_lnzsteps
+                  call Transfer_GetMatterPower(Info%Transfers%MTrans,& 
+                    Info%Theory%matter_power(:,zix),matter_power_lnzsteps-zix+1,&
+                     1,matter_power_minkh, matter_power_dlnkh,num_matter_power) 
+                  end do             
+              end if
+          else
+             Info%Theory%sigma_8 = 0
+          end if
+ 
+      end if
+      if (error /= 0) return
+ 
+      call ClsFromTheoryData(Info%Theory, CMB, Cls)
+      
+  end subroutine GetCls
+ 
+  subroutine SetTheoryFromCAMB(Theory)
+    Type(CosmoTheory) Theory
+    real, parameter :: cons =  2.726e6**2*2*pi
+    real nm
+    integer l
+ 
+    !The reason we store tensors separately is that can then importance sample re-computing scalars only,
+    !using the stored tensor C_l
+ 
+     do l = 2, lmax
+ 
+        nm = cons/(l*(l+1))
+        if (CMB_Lensing) then
+             Theory%cl(l,1:num_clsS) =  nm*Cl_lensed(l,1, TensClOrder(1:num_clsS))
+          else 
+             Theory%cl(l,1:num_clsS) =  nm*Cl_scalar(l,1, scalClOrder(1:num_clsS))
+        end if         
+    
+        if (compute_tensors .and. l<=lmax_tensor) then
+             Theory%cl_tensor(l,1:num_cls) =  nm*Cl_tensor(l,1, TensClOrder(1:num_cls))
+        end if
+     end do 
+     
+     if (num_cls>num_clsS) Theory%cl(:,num_clsS+1:num_cls) = 0 
+ 
+ 
+  end subroutine SetTheoryFromCAMB
+ 
+  subroutine GetClsInfo(CMB, Theory, error, DoCls, DoPk)
+    use ModelParams, only : ThreadNum
+ #ifdef DR71RG
+    use lrggettheory
+    real(dl) :: getabstransferscale
+    !! BR09: this variable is for renormalizing the power spectra to the z=0 value;
+    !this is the assumption of the LRG model.
+ #endif
+    type(CMBParams) CMB
+    Type(CosmoTheory) Theory
+    integer error
+    logical, intent(in) :: DoCls, DoPk
+    type(CAMBParams)  P
+    logical MatterOnly
+    integer zix
+    error = 0
+    Threadnum =num_threads
+    call CMBToCAMB(CMB, P)
+    P%OnlyTransfers = .false.
+    call SetCAMBInitPower(P,CMB,1)   
+     
+    MatterOnly = .false.
+    if (DoPk) then
+       P%WantTransfer = .true.
+       if (.not. DoCls) then
+          MatterOnly = .true.         
+          P%WantScalars = .false.
+          P%WantTensors = .false.
+       end if
+    end if
+    if (DoCls) then
+       !Assume we just want Cls to higher l
+       P%WantScalars = .true.
+       !P%WantTensors = .false.
+       !compute_tensors = .false.
+       P%WantTensors = compute_tensors 
+        
+        if (.not. DoPk) then
+         P%WantTransfer = .false.
+        end if
+    end if
+    
+    call CAMB_GetResults(P)
+    error = 0 !using error optional parameter gives seg faults on SGI
+    if (error==0) then
+        
+       if (DoCls) then
+   
+        Theory%cl_tensor(2:lmax_tensor,1:num_cls) = 0
+        call SetTheoryFromCAMB(Theory)
+       end if
+ 
+ !!BR09 new addition, putting LRGs back here as well, same structure as above.  
+       if (DoPk) then 
+          Theory%sigma_8 = MT%sigma_8(matter_power_lnzsteps,1)
+ 
+ #ifdef DR71RG
+          !! BR09 get lrgtheory info
+          if (num_matter_power /= 0 .and. use_dr7lrg) then
+              do zix = 1,matter_power_lnzsteps
+               if(zix .eq. iz0lrg .or. zix .eq. izNEARlrg .or. zix .eq. izMIDlrg .or. zix .eq. izFARlrg) then
+                 call Transfer_GetMatterPowerAndNW(MT,&
+                   Theory%matter_power(:,zix),matter_power_lnzsteps-zix+1,&
+                    1,matter_power_minkh, matter_power_dlnkh,num_matter_power,&
+                     kmindata,getabstransferscale, &
+                     Theory%mpk_nw(:,zix),Theory%mpkrat_nw_nl(:,zix))
+                      if(zix == iz0lrg) powerscaletoz0(1) = getabstransferscale**2.0d0
+                      if(zix == izNEARlrg)   powerscaletoz0(2) = powerscaletoz0(1)/getabstransferscale**2.0d0
+                      if(zix == izMIDlrg)   powerscaletoz0(3) = powerscaletoz0(1)/getabstransferscale**2.0d0
+                      if(zix == izFARlrg)   powerscaletoz0(4) = powerscaletoz0(1)/getabstransferscale**2.0d0
+                else  !! not an LRG redshift, so call regular function.
+                 call Transfer_GetMatterPower(MT,&
+                   Theory%matter_power(:,zix),matter_power_lnzsteps-zix+1,&
+                   1,matter_power_minkh, matter_power_dlnkh,num_matter_power)
+                end if
+              end do
+              if(zix == iz0lrg) powerscaletoz0(1) = 1.0d0
+               else if (num_matter_power /= 0) then
+          !! end BR09 get lrgtheory info
+ #else
+             if (num_matter_power /= 0) then
+ #endif
+                 do zix = 1,matter_power_lnzsteps
+                  call Transfer_GetMatterPower(MT,&
+                    Theory%matter_power(:,zix),matter_power_lnzsteps-zix+1,&
+                     1,matter_power_minkh, matter_power_dlnkh,num_matter_power)
+                  end do
+             end if
+       end if
+       Theory%Age = CAMB_GetAge(P)
+ 
+    end if
+  end subroutine GetClsInfo
+ 
+ 
+  subroutine InitCAMB(CMB,error, DoReion)
+    type(CMBParams), intent(in) :: CMB
+    logical, optional, intent(in) :: DoReion
+    logical WantReion
+    type(CAMBParams)  P
+    integer error
+    
+    if (present(DoReion)) then
+     WantReion = DoReion
+    else
+     WantReion = .true.
+    end if
+ 
+    call CMBToCAMB(CMB, P)
+    call CAMBParams_Set(P,error,WantReion)
+ 
+  end subroutine InitCAMB
+ 
+  function GetOpticalDepth(CMB)
+    type(CMBParams) CMB
+    real GetOpticalDepth
+    type(CAMBParams)  P
+    integer error
+    
+    call CMBToCAMB(CMB, P)
+    call CAMBParams_Set(P,error)
+ 
+    if (error/= 0) then
+       GetOpticalDepth = -1 
+    else
+       GetOpticalDepth = Reionization_GetOptDepth(P%Reion, P%ReionHist) 
+    end if
+  end  function GetOpticalDepth
+ 
+  function GetZreFromTau(CMB, tau)
+    type(CMBParams) CMB
+    real, intent(in) :: tau
+    real GetZreFromTau
+    type(CAMBParams)  P
+    
+    call CMBToCAMB(CMB, P)
+    GetZreFromTau = CAMB_GetZreFromTau(P,dble(tau))
+  
+  end  function GetZreFromTau 
+  
+  function GetAge(CMB, Info)
+    !Return <0 if error
+    real GetAge
+    type(CMBParams) CMB
+    Type(ParamSetInfo) Info
+    type(CAMBParams)  P
+    call CMBToCAMB(CMB, P)
+  
+    Info%Theory%Age = CAMB_GetAge(P)
+  
+    GetAge = Info%Theory%Age
+  end function GetAge
+ 
+  subroutine InitCAMBParams(P)
+    use lensing
+    use ModelParams
+    use Lya
+    use mpk
+    type(CAMBParams)  P 
+    integer zix
+    real redshifts(matter_power_lnzsteps)
+ 
+ !wig
+    AccuracyBoost = AccuracyLevel
+    lAccuracyBoost = lAccuracyLevel
+    lSampleBoost = lSampleLevel
+    kSrcSampleBoost = kSrcSampleLevel
+    kIntSampleBoost = kIntSampleLevel
+    ComputeEveryl = CambComputeEveryl
+ 
+    write(*,*)
+    write(*,*) '>>>>>>>>>>>>>> accuracy settings <<<<<<<<<<<<<<'
+    write(*,*) 'Power_Name     =    ',Power_Name
+    write(*,*) 'write_all_cls  =   ',write_all_cls
+    write(*,*) 'AccuracyBoost  = ',AccuracyBoost
+    write(*,*) 'lAccuracyBoost = ',lAccuracyBoost          
+    write(*,*) 'DoClsInterpol  =   ',DoClsInterpol
+    write(*,*) 'ComputeEveryl  =   ',ComputeEveryl
+    write(*,*) '>>>>>>>>>>>>>> accuracy settings <<<<<<<<<<<<<<'
+    write(*,*)
+ 
+    if ((Power_Name == 'power_sr').and.(.not.compute_tensors)) stop 'Slow-Roll requires tensors'
+ !end wig
+ 
+         Threadnum =num_threads
+         w_lam = -1
+         call CAMB_SetDefParams(P)
+  
+         P%OutputNormalization = outNone
+       
+         P%WantScalars = .true.
+         P%WantTensors = compute_tensors
+         P%WantTransfer = Use_LSS
+ 
+         P%Max_l=lmax
+         P%Max_eta_k=lmax*2
+         P%Max_l_tensor=lmax_tensor
+         P%Max_eta_k_tensor=lmax_tensor*5./2
+  
+         P%Transfer%k_per_logint=0
+  
+         if (use_nonlinear) then
+          P%NonLinear = NonLinear_Pk
+          P%Transfer%kmax = 1.2
+         else
+          P%Transfer%kmax = 0.8
+         end if
+         if (Use_Lya) P%Transfer%kmax = lya_kmax
+         P%Transfer%num_redshifts = matter_power_lnzsteps
+         
+         if (AccuracyLevel > 1) then
+           if (USE_LSS) then
+             P%Transfer%high_precision=.true.
+             P%Transfer%kmax=P%Transfer%kmax + 0.2
+            end if
+ !wig
+ !          AccuracyBoost = AccuracyLevel
+ !          lAccuracyBoost = AccuracyLevel
+ !          lSampleBoost = AccuracyLevel
+ !end wig
+           P%AccurateReionization = .true.
+         end if
+                 
+         if (max_transfer_redshifts < matter_power_lnzsteps) then
+           stop 'Need to manually set max_transfer_redshifts larger in CAMB''s modules.f90'
+         end if
+         if (use_LSS) then
+            redshifts(1) = 0
+            
+            do zix=2, matter_power_lnzsteps
+             !Default Linear spacing in log(z+1) if matter_power_lnzsteps > 1           
+              redshifts(zix) = exp( log(matter_power_maxz+1) * &
+                 real(zix-1)/(max(2,matter_power_lnzsteps)-1) )-1
+                !put in max(2,) to stop compilers complaining of div by zero
+            end do
+            
+            if (use_mpk) call mpk_SetTransferRedshifts(redshifts) !can modify to use specific redshifts
+            if (redshifts(1) > 0.0001) call MpiStop('mpk redshifts: lowest redshift must be zero')
+            do zix=1, matter_power_lnzsteps 
+             !CAMB's ordering is from highest to lowest
+             P%Transfer%redshifts(zix) = redshifts(matter_power_lnzsteps-zix+1)
+            end do
+         else 
+           P%Transfer%redshifts(1) = 0
+          end if   
+         
+         P%Num_Nu_Massive = 3
+         P%Num_Nu_Massless = 0.04
+         P%InitPower%nn = 1
+         P%AccuratePolarization = num_cls/=1 
+         P%Reion%use_optical_depth = .false.
+         P%OnlyTransfers = .true.
+ 
+         if (CMB_Lensing) then
+             P%DoLensing = .true.
+             P%Max_l = lmax +250 + 50 !+50 in case accuracyBoost>1 and so odd l spacing
+             P%Max_eta_k = P%Max_l*2 
+         end if
+         
+         lensing_includes_tensors = .false.
+ 
+         P%Scalar_initial_condition = initial_vector
+         P%InitialConditionVector = 0
+         P%InitialConditionVector(initial_adiabatic) = -1
+ 
+ 
+  end subroutine InitCAMBParams
+ 
+  subroutine CMB_Initialize(Info)
+    Type(ParamSetInfo) Info
+    type(CAMBParams)  P 
+         compute_tensors = Ini_Read_Logical('compute_tensors',.false.)
+         if (num_cls==3 .and. compute_tensors) write (*,*) 'WARNING: computing tensors with num_cls=3 (BB=0)'
+         CMB_lensing = Ini_Read_Logical('CMB_lensing',.false.)
+ 
+         if (Feedback > 0 ) then
+           write (*,*) 'Computing tensors:', compute_tensors
+           write (*,*) 'Doing CMB lensing:',CMB_lensing
+           write(*,'(" lmax           = ",1I4)') lmax       
+           if (compute_tensors) write(*,'(" lmax_tensor    = ",1I4)') lmax_tensor
+           write(*,'(" Number of C_ls = ",1I4)') num_cls
+         end if
+ 
+         if (CMB_lensing) num_clsS = num_cls   !Also scalar B in this case
+      
+         call InitCAMBParams(P)
+   
+         call CAMB_InitCAMBdata(Info%Transfers)
+     
+         P%WantTensors = compute_tensors
+         Info%LastParams%omb = -1 !Make sure we calculate the CMB first time called
+         CAMBP = P
+         
+  end subroutine CMB_Initialize
+ 
+ 
+  subroutine AcceptReject(accpt, CurParams, Trial)
+    logical, intent(in) :: accpt
+    Type(ParamSetInfo) CurParams, Trial
+ 
+    if (.not. associated(CurParams%Transfers%ClTransScal%Delta_p_l_k,&
+                         Trial%Transfers%ClTransScal%Delta_p_l_k)) then
+     !If they point to same memory don't need to free anything
+     if (accpt) then
+        call CAMB_FreeCAMBdata(CurParams%Transfers)
+     else
+        call CAMB_FreeCAMBdata(Trial%Transfers)
+     end if
+ 
+    end if
+ 
+  end subroutine AcceptReject
+ 
+ end module CMB_Cls
+ 
diff -r -c -b -N cosmomc/source/cmbtypes.f90 cosmomc_wiggles/source/cmbtypes.f90
*** cosmomc/source/cmbtypes.f90	2009-10-20 16:45:00.000000000 +0200
--- cosmomc_wiggles/source/cmbtypes.f90	1970-01-01 01:00:00.000000000 +0100
***************
*** 1,425 ****
- !Define the data types and read/writes them to disk. Also change l_max here.
- 
- module cmbtypes
- use settings
- implicit none
- 
- 
- !Number of Cls, 1 for just temperature, 3 (4) for polarization (with B)
-   integer, parameter  :: num_cls  = 3
- 
- !l_max. Tensors are not computed unless compute_tensors = T in input file
- !Make these multiples of 50, should be 50 more than you need accurately
-   integer, parameter :: lmax = 2100, lmax_tensor = 400
- 
- !Parameters for calculating/storing the matter power spectrum
- !Note that by default everything is linear
- !Old mpk settings
- #ifdef DR71RG
- !!! BR09: Reid et al 2009 settings for the LRG power spectrum.
-   integer, parameter :: num_matter_power = 300 !number of points computed in matter power spectrum
-   real, parameter    :: matter_power_minkh =  0.999e-4  !minimum value of k/h to store
-   real, parameter    :: matter_power_dlnkh = 0.03     !log spacing in k/h
-   real, parameter    :: matter_power_maxz = 0.
-   integer, parameter :: matter_power_lnzsteps = 4  ! z=0 to get sigma8 (this first entry appears to be coded in some spots in the code!!), plus 3 LRG redshifts.
- #else
-   integer, parameter :: num_matter_power = 74 !number of points computed in matter power spectrum
-   real, parameter    :: matter_power_minkh =  0.999e-4  !1e-4 !minimum value of k/h to store
-   real, parameter    :: matter_power_dlnkh = 0.143911568     !log spacing in k/h
-   real, parameter    :: matter_power_maxz = 0.    !6.0
-   integer, parameter :: matter_power_lnzsteps = 1 !20
- #endif
- !Only used in params_CMB
-    real :: pivot_k = 0.05 !Point for defining primordial power spectra
-    logical :: inflation_consistency = .false. !fix n_T or not
- 
-         !Note these are the interpolated/extrapolated values. The k at which matter power is computed up to 
-         !by CAMB is set in CMB_Cls_xxx with, e.g. P%Transfer%kmax = 0.6 (which is enough for 2dF)
- 
- !Number of scalar-only cls
- !if num_cls=4 and CMB_lensing then increased to 4 
-   integer :: num_clsS=min(num_cls,3) 
- 
-   integer, parameter :: norm_As=1, norm_amp_ratio=2, norm_freq_ix = 3
-   
-   Type CMBParams
-      real nuisance(1:num_nuisance_params)
-       !unit Gaussians for experimental parameters
-      real norm(1:num_norm)
-       !These are fast parameters controling amplitudes, calibrations, etc.
-      real InitPower(1:num_initpower)
-       !These are fast paramters for the initial power spectrum
-      !Now remaining (non-independent) parameters
-      real omb, omc, omv, omnu, omk, omdm
-      real ombh2, omch2, omnuh2, omdmh2
-      real zre, nufrac
-      real h, H0
-      real w
-      real reserved(5)
-   
-   end Type CMBParams
- 
-   Type CosmoTheory
-      real Age, r10
-      real SN_loglike, HST_loglike, BAO_loglike, reserved(1)
-      real cl(lmax,num_cls), cl_tensor(lmax_tensor,num_cls) !TT, TE, EE and BB  in that order
-      real sigma_8
-      real matter_power(num_matter_power,matter_power_lnzsteps)
-        !second index is redshifts from 0 to matter_power_maxz
-        !if custom_redshift_steps = false with equal spacing in
-        !log(1+z) and matter_power_lnzsteps points
-        !if custom_redshift_steps = true set in mpk.f90 
-      ! BR09 additions
-      real mpk_nw(num_matter_power,matter_power_lnzsteps) !no wiggles fit to matter power spectrum
-      real mpkrat_nw_nl(num_matter_power,matter_power_lnzsteps) !halofit run on mpk_nw
-      real finalLRGtheoryPk(num_matter_power)  !! this is the quantity that enters the LRG likelihood calculation
-     ! end BR09 additions
-   end Type CosmoTheory
- 
-   logical, parameter ::  Old_format  = .false.
-   logical, parameter :: write_all_Cls = .false. 
-    !if false use CAMB's flat interpolation scheme (lossless if models are flat except near lmax when lensed)
- 
- contains
- 
- 
-    subroutine WriteModel(i,CMB, T, like, mult)
-     integer i
-     real, intent(in), optional :: mult
-     Type(CosmoTheory) T
-     real like, amult
-     Type(CMBParams) CMB
-     integer j 
- 
-     if (present(mult)) then
-        amult = mult
-     else
-        amult = 1
-     end if
-     
-     if (Old_format) then
- 
-       stop 'old not supported'    
-     else
- 
-     j = 0 !format ID
-     if (write_all_cls) j=1
-     write(i) j
- 
-     write(i) amult, num_matter_power, lmax, lmax_tensor, num_cls
- 
-     write(i) T%SN_loglike, T%HST_loglike, T%reserved
- 
-     write(i) like
-     write(i) CMB
- 
-     write(i) T%Age, T%r10, T%sigma_8, T%matter_power
- 
-     if (write_all_cls) then
-      write(i) T%cl(2:lmax,1:num_cls)
-      write(i) T%cl_tensor(2:lmax_tensor,1:num_cls)
-     else
- 
-        !Use interpolation scheme CAMB uses for flat models
-        !If using significantly non-flat, or increasing interpolation accuracy, save all th cls instead
-         write(i) T%cl(2:20,1:num_cls)
-         do j=30,90,10 
-          write(i) T%cl(j,1:num_cls)
-         end do
-         do j=110,130, 20
-          write(i) T%cl(j,1:num_cls)
-         end do
-         do j=150,lmax, 50
-          write(i) T%cl(j,1:num_cls)
-         end do
- 
-         if (lmax_tensor /= 0) then
-             if (lmax_tensor<150) stop 'lmax_tensor too small'
-             write(i) T%cl_tensor(2:20,1:num_cls)
-             do j=30,90,10 
-              write(i) T%cl_tensor(j,1:num_cls)
-             end do
-             do j=110,130,20 
-              write(i) T%cl_tensor(j,1:num_cls)
-             end do
-             do j=150,lmax_tensor, 50
-              write(i) T%cl_tensor(j,1:num_cls)
-             end do
-         end if
-     end if
- 
-     end if 
- 
-     if (flush_write) call FlushFile(i)
- 
-    end subroutine WriteModel
- 
-    
-  subroutine ReadModel(i,CMB, T, mult, like, error)
-     integer, intent(in) :: i
-     integer, intent(out) :: error
-     real, intent(out) :: mult
-     Type(CosmoTheory) T
-     real like
-     Type(CMBParams) CMB
-     real icl(lmax,1:num_cls)
-     integer allcl,j,ind, ix(lmax)
-     integer almax,almaxtensor, anumpowers, anumcls
-    
-     error = 0
- 
-     if (old_format) then
- 
-        stop 'old not supported'
- 
-     else
- 
-         read(i,end=100,err=100) allcl
- 
-         if (allcl/=0 .and. allcl/=1) stop 'wrong file format'
- 
-         read(i,end=100,err=100) mult,anumpowers,almax, almaxtensor, anumcls
-         if (almax > lmax) stop 'reading file with larger lmax'
-         if (anumcls > num_cls) stop 'reading file with more Cls (polarization)'
- 
-         read(i) T%SN_loglike, T%HST_loglike,T%reserved
-    
-         read(i,end = 100, err=100) like
-         read(i) CMB
- 
-     
-         read(i) T%Age, T%r10, T%sigma_8, T%matter_power(1:anumpowers,1:matter_power_lnzsteps)
-         T%cl = 0
-         T%cl_tensor = 0
-          
-         if(allcl==1) then  
-          read(i) T%cl(2:almax,1:anumcls)
-          read(i) T%cl_tensor(2:almaxtensor,1:anumcls)
-         else
- 
-             read(i) icl(1:19,1:num_cls)
-             ind =1
-             do j =2,20 
-                ix(ind)=j
-                ind=ind+1
-             end do
-             do j=30,90,10 
-              read(i) icl(ind,1:num_cls)
-              ix(ind) = j
-              ind = ind + 1
-             end do
-              do j=110,130,20 
-              read(i) icl(ind,1:num_cls)
-              ix(ind) = j
-              ind = ind + 1
-             end do
-             do j=150,almax, 50
-              read(i) icl(ind,1:num_cls)
-              ix(ind) = j
-              ind = ind+1
-             end do
-             ind = ind-1
-  
-            call InterpCls(ix,icl, T%cl, ind, almax, num_Cls)
- 
-            if (almaxtensor /= 0) then     
-             read(i) icl(1:19,1:num_cls)
-             ind =1
-             do j =2,20 
-                ix(ind)=j
-                ind=ind+1
-             end do
-             do j=30,90,10 
-              read(i) icl(ind,1:num_cls)
-              ix(ind) = j
-              ind = ind + 1
-             end do
-             do j=110,130,20 
-              read(i) icl(ind,1:num_cls)
-              ix(ind) = j
-              ind = ind + 1
-             end do
-             do j=150,almaxtensor, 50
-              read(i) icl(ind,1:num_cls)
-              ix(ind) = j
-              ind = ind+1
-             end do
-             ind = ind-1
-             call InterpCls(ix,icl, T%cl_tensor, ind, almaxtensor,num_cls)
-            end if
-         
-         end if 
- 
-         return
-     100 error = 1
- 
-     end if
- 
-    end subroutine ReadModel
- 
-       subroutine InterpCls(l,iCl, all_Cl, n, almax, ncls)
-       integer, intent(in) :: n, almax,ncls
-    
-       real, intent(in) :: iCl(lmax,1:num_cls)
-       integer l(n),p
-       real all_Cl(:,:)
-    
-       integer il,llo,lhi,xi
-       real xl(n),  ddCl(n)
- 
-       real a0,b0,ho
-       real inCl(n)
- 
-     
-       do p =1, ncls
- 
-         do il=1,n
-            inCl(il) = iCl(il,p)*l(il)**2
-         end do
- 
-         xl = l
-         call spline_real(xl,inCl,n,ddCl)
-      
-         llo=1
-         do il=2,l(n)
-            xi=il
-            if ((xi > l(llo+1)).and.(llo < n)) then
-               llo=llo+1
-            end if
-            lhi=llo+1
-            ho=l(lhi)-l(llo)
-            a0=(xl(lhi)-xi)/ho
-            b0=(xi-xl(llo))/ho
-           
-            all_Cl(il,p) = (a0*inCl(llo)+ b0*inCl(lhi)+((a0**3-a0)* ddCl(llo) &
-                    +(b0**3-b0)*ddCl(lhi))*ho**2/6)/il**2
-   
-         end do
- 
-       end do
- 
-       all_Cl(l(n)+1:almax,:) = 0
-        
- 
-       end subroutine InterpCls
-   
-   
-    subroutine ClsFromTheoryData(T, CMB, Cls)
-      Type(CosmoTheory) T
-      Type(CMBParams) CMB
-      real Cls(lmax,num_cls)
-      integer i
-      
-      Cls(2:lmax,1:num_clsS) =T%cl(2:lmax,1:num_clsS)    !CMB%norm(norm_As)*T%cl(2:lmax,1:num_clsS)
-      if (num_cls>3 .and. num_ClsS==3) Cls(2:lmax,num_cls)=0
- 
-      i = norm_amp_ratio !this convolution is to avoid compile-time bounds-check errors on CMB%norm
-      if (CMB%norm(i) /= 0) then
-         Cls(2:lmax_tensor,:) =  Cls(2:lmax_tensor,:)+ T%cl_tensor(2:lmax_tensor,:) 
-          !CMB%norm(norm_As)*CMB%norm(norm_amp_ratio)*T%cl_tensor(2:lmax_tensor,:)
-      end if 
- 
-    end subroutine ClsFromTheoryData
- 
-    subroutine WriteTextCls(aname,T, CMB)
-      Type(CosmoTheory) T
-      Type(CMBParams) CMB
-      character (LEN=*), intent(in) :: aname
-      integer l
-      real Cls(lmax,num_cls)
- 
-      call ClsFromTheoryData(T,CMB,Cls)
-      open(unit = tmp_file_unit, file = aname, form='formatted', status = 'replace')
-      do l=2, lmax
-         write (tmp_file_unit,*) l, Cls(l,:)*l*(l+1)/(2*pi)
-      end do
-      close(tmp_file_unit)
- 
-    end subroutine WriteTextCls
- 
-    function MatterPowerAt(T,kh)
-      !get matter power spectrum today at kh = k/h by interpolation from stored values
-      real, intent(in) :: kh
-      Type(CosmoTheory) T
-      real MatterPowerAt
-      real x, d
-      integer i
-    
-      x = log(kh/matter_power_minkh) / matter_power_dlnkh
-      if (x < 0 .or. x >= num_matter_power-1) then
-         write (*,*) ' k/h out of bounds in MatterPowerAt (',kh,')'
-         stop 
-      end if
-      i = int(x)
-      d = x - i
-      MatterPowerAt = exp(log(T%matter_power(i+1,1))*(1-d) &
-        + log(T%matter_power(i+2,1))*d)
-      !Just do linear interpolation in logs for now..
-      !(since we already cublic-spline interpolated to get the stored values)
-      !Assume matter_power_lnzsteps is at redshift zero
-    end function
- 
- 
- 
- !BR09 this function is just a copy of the one above but with LRG theory put in instead of linear theory
-    function LRGPowerAt(T,kh)
-      !get LRG matter power spectrum today at kh = k/h by interpolation from stored values
-      real, intent(in) :: kh
-      Type(CosmoTheory) T
-      real LRGPowerAt
-      real x, d
-      integer i
-    
-      x = log(kh/matter_power_minkh) / matter_power_dlnkh
-      if (x < 0 .or. x >= num_matter_power-1) then
-         write (*,*) ' k/h out of bounds in MatterPowerAt (',kh,')'
-         stop 
-      end if
-      i = int(x)
-      d = x - i
-      LRGPowerAt = exp(log(T%finalLRGtheoryPk(i+1))*(1-d) + log(T%finalLRGtheoryPk(i+2))*d)
-      !Just do linear interpolation in logs for now..
-      !(since we already cublic-spline interpolated to get the stored values)
-    end function
- !!BRO09 addition end
- 
-    function MatterPowerAt_Z(T,kh,z)
-      !get matter power spectrum at z at kh = k/h by interpolation from stored values
- 
-      real, intent(in) :: kh
-      Type(CosmoTheory) T
-      real MatterPowerAt_Z
-      real x, d, z, y, dz, mup, mdn
-      real matter_power_dlnz
-      integer i, iz
-    
-      matter_power_dlnz = log(matter_power_maxz+1) / (matter_power_lnzsteps -1 + 1e-13)
-      y = log(1.+ z) / matter_power_dlnz 
- 
-      if (z > matter_power_maxz ) then
-         write (*,*) ' z out of bounds in MatterPowerAt_Z (',z,')'
-         stop
-      end if
-      x = log(kh/matter_power_minkh) / matter_power_dlnkh
-      if (x < 0 .or. x >= num_matter_power-1) then
-         write (*,*) ' k/h out of bounds in MatterPowerAt_Z (',kh,')'
-         stop
-      end if
- 
-      iz = int(y*0.99999999)
-      dz = y - iz
- 
-      i = int(x)
-      d = x - i
- 
-      mup = log(T%matter_power(i+1,iz+2))*(1-d) + log(T%matter_power(i+2,iz+2))*d
-      mdn = log(T%matter_power(i+1,iz+1))*(1-d) + log(T%matter_power(i+2,iz+1))*d
- 
-      MatterPowerAt_Z = exp(mdn*(1-dz) + mup*dz)
- 
-    end function MatterPowerAt_Z
- 
- 
- 
- 
- end module cmbtypes
--- 0 ----
diff -r -c -b -N cosmomc/source/cmbtypes.F90 cosmomc_wiggles/source/cmbtypes.F90
*** cosmomc/source/cmbtypes.F90	1970-01-01 01:00:00.000000000 +0100
--- cosmomc_wiggles/source/cmbtypes.F90	2009-10-28 16:33:32.348968827 +0100
***************
*** 0 ****
--- 1,429 ----
+ !Define the data types and read/writes them to disk. Also change l_max here.
+ 
+ module cmbtypes
+ use settings
+ implicit none
+ 
+ 
+ !Number of Cls, 1 for just temperature, 3 (4) for polarization (with B)
+ !wig
+   integer, parameter  :: num_cls  = 4
+ !end wig
+ !l_max. Tensors are not computed unless compute_tensors = T in input file
+ !Make these multiples of 50, should be 50 more than you need accurately
+   integer, parameter :: lmax = 2100, lmax_tensor = 400
+ 
+ !Parameters for calculating/storing the matter power spectrum
+ !Note that by default everything is linear
+ !Old mpk settings
+ #ifdef DR71RG
+ !!! BR09: Reid et al 2009 settings for the LRG power spectrum.
+   integer, parameter :: num_matter_power = 300 !number of points computed in matter power spectrum
+   real, parameter    :: matter_power_minkh =  0.999e-4  !minimum value of k/h to store
+   real, parameter    :: matter_power_dlnkh = 0.03     !log spacing in k/h
+   real, parameter    :: matter_power_maxz = 0.
+   integer, parameter :: matter_power_lnzsteps = 4  ! z=0 to get sigma8 (this first entry appears to be coded in some spots in the code!!), plus 3 LRG redshifts.
+ #else
+   integer, parameter :: num_matter_power = 74 !number of points computed in matter power spectrum
+   real, parameter    :: matter_power_minkh =  0.999e-4  !1e-4 !minimum value of k/h to store
+   real, parameter    :: matter_power_dlnkh = 0.143911568     !log spacing in k/h
+   real, parameter    :: matter_power_maxz = 0.    !6.0
+   integer, parameter :: matter_power_lnzsteps = 1 !20
+ #endif
+ !Only used in params_CMB
+    real :: pivot_k = 0.05 !Point for defining primordial power spectra
+    logical :: inflation_consistency = .false. !fix n_T or not
+ 
+         !Note these are the interpolated/extrapolated values. The k at which matter power is computed up to 
+         !by CAMB is set in CMB_Cls_xxx with, e.g. P%Transfer%kmax = 0.6 (which is enough for 2dF)
+ 
+ !Number of scalar-only cls
+ !if num_cls=4 and CMB_lensing then increased to 4 
+   integer :: num_clsS=min(num_cls,3) 
+ 
+   integer, parameter :: norm_As=1, norm_amp_ratio=2, norm_freq_ix = 3
+   
+   Type CMBParams
+      real nuisance(1:num_nuisance_params)
+       !unit Gaussians for experimental parameters
+      real norm(1:num_norm)
+       !These are fast parameters controling amplitudes, calibrations, etc.
+      real InitPower(1:num_initpower)
+       !These are fast paramters for the initial power spectrum
+      !Now remaining (non-independent) parameters
+      real omb, omc, omv, omnu, omk, omdm
+      real ombh2, omch2, omnuh2, omdmh2
+      real zre, nufrac
+      real h, H0
+      real w
+      real reserved(5)
+   
+   end Type CMBParams
+ 
+   Type CosmoTheory
+      real Age, r10
+      real SN_loglike, HST_loglike, BAO_loglike, reserved(1)
+      real cl(lmax,num_cls), cl_tensor(lmax_tensor,num_cls) !TT, TE, EE and BB  in that order
+      real sigma_8
+      real matter_power(num_matter_power,matter_power_lnzsteps)
+        !second index is redshifts from 0 to matter_power_maxz
+        !if custom_redshift_steps = false with equal spacing in
+        !log(1+z) and matter_power_lnzsteps points
+        !if custom_redshift_steps = true set in mpk.f90 
+      ! BR09 additions
+      real mpk_nw(num_matter_power,matter_power_lnzsteps) !no wiggles fit to matter power spectrum
+      real mpkrat_nw_nl(num_matter_power,matter_power_lnzsteps) !halofit run on mpk_nw
+      real finalLRGtheoryPk(num_matter_power)  !! this is the quantity that enters the LRG likelihood calculation
+     ! end BR09 additions
+   end Type CosmoTheory
+ 
+   logical, parameter ::  Old_format  = .false.
+ !wig
+ !  logical, parameter :: write_all_Cls = .false. 
+   logical, parameter :: write_all_Cls = .true.
+ !end wig
+    !if false use CAMB's flat interpolation scheme (lossless if models are flat except near lmax when lensed)
+ 
+ contains
+ 
+ 
+    subroutine WriteModel(i,CMB, T, like, mult)
+     integer i
+     real, intent(in), optional :: mult
+     Type(CosmoTheory) T
+     real like, amult
+     Type(CMBParams) CMB
+     integer j 
+ 
+     if (present(mult)) then
+        amult = mult
+     else
+        amult = 1
+     end if
+     
+     if (Old_format) then
+ 
+       stop 'old not supported'    
+     else
+ 
+     j = 0 !format ID
+     if (write_all_cls) j=1
+     write(i) j
+ 
+     write(i) amult, num_matter_power, lmax, lmax_tensor, num_cls
+ 
+     write(i) T%SN_loglike, T%HST_loglike, T%reserved
+ 
+     write(i) like
+     write(i) CMB
+ 
+     write(i) T%Age, T%r10, T%sigma_8, T%matter_power
+ 
+     if (write_all_cls) then
+      write(i) T%cl(2:lmax,1:num_cls)
+      write(i) T%cl_tensor(2:lmax_tensor,1:num_cls)
+     else
+ 
+        !Use interpolation scheme CAMB uses for flat models
+        !If using significantly non-flat, or increasing interpolation accuracy, save all th cls instead
+         write(i) T%cl(2:20,1:num_cls)
+         do j=30,90,10 
+          write(i) T%cl(j,1:num_cls)
+         end do
+         do j=110,130, 20
+          write(i) T%cl(j,1:num_cls)
+         end do
+         do j=150,lmax, 50
+          write(i) T%cl(j,1:num_cls)
+         end do
+ 
+         if (lmax_tensor /= 0) then
+             if (lmax_tensor<150) stop 'lmax_tensor too small'
+             write(i) T%cl_tensor(2:20,1:num_cls)
+             do j=30,90,10 
+              write(i) T%cl_tensor(j,1:num_cls)
+             end do
+             do j=110,130,20 
+              write(i) T%cl_tensor(j,1:num_cls)
+             end do
+             do j=150,lmax_tensor, 50
+              write(i) T%cl_tensor(j,1:num_cls)
+             end do
+         end if
+     end if
+ 
+     end if 
+ 
+     if (flush_write) call FlushFile(i)
+ 
+    end subroutine WriteModel
+ 
+    
+  subroutine ReadModel(i,CMB, T, mult, like, error)
+     integer, intent(in) :: i
+     integer, intent(out) :: error
+     real, intent(out) :: mult
+     Type(CosmoTheory) T
+     real like
+     Type(CMBParams) CMB
+     real icl(lmax,1:num_cls)
+     integer allcl,j,ind, ix(lmax)
+     integer almax,almaxtensor, anumpowers, anumcls
+    
+     error = 0
+ 
+     if (old_format) then
+ 
+        stop 'old not supported'
+ 
+     else
+ 
+         read(i,end=100,err=100) allcl
+ 
+         if (allcl/=0 .and. allcl/=1) stop 'wrong file format'
+ 
+         read(i,end=100,err=100) mult,anumpowers,almax, almaxtensor, anumcls
+         if (almax > lmax) stop 'reading file with larger lmax'
+         if (anumcls > num_cls) stop 'reading file with more Cls (polarization)'
+ 
+         read(i) T%SN_loglike, T%HST_loglike,T%reserved
+    
+         read(i,end = 100, err=100) like
+         read(i) CMB
+ 
+     
+         read(i) T%Age, T%r10, T%sigma_8, T%matter_power(1:anumpowers,1:matter_power_lnzsteps)
+         T%cl = 0
+         T%cl_tensor = 0
+          
+         if(allcl==1) then  
+          read(i) T%cl(2:almax,1:anumcls)
+          read(i) T%cl_tensor(2:almaxtensor,1:anumcls)
+         else
+ 
+             read(i) icl(1:19,1:num_cls)
+             ind =1
+             do j =2,20 
+                ix(ind)=j
+                ind=ind+1
+             end do
+             do j=30,90,10 
+              read(i) icl(ind,1:num_cls)
+              ix(ind) = j
+              ind = ind + 1
+             end do
+              do j=110,130,20 
+              read(i) icl(ind,1:num_cls)
+              ix(ind) = j
+              ind = ind + 1
+             end do
+             do j=150,almax, 50
+              read(i) icl(ind,1:num_cls)
+              ix(ind) = j
+              ind = ind+1
+             end do
+             ind = ind-1
+  
+            call InterpCls(ix,icl, T%cl, ind, almax, num_Cls)
+ 
+            if (almaxtensor /= 0) then     
+             read(i) icl(1:19,1:num_cls)
+             ind =1
+             do j =2,20 
+                ix(ind)=j
+                ind=ind+1
+             end do
+             do j=30,90,10 
+              read(i) icl(ind,1:num_cls)
+              ix(ind) = j
+              ind = ind + 1
+             end do
+             do j=110,130,20 
+              read(i) icl(ind,1:num_cls)
+              ix(ind) = j
+              ind = ind + 1
+             end do
+             do j=150,almaxtensor, 50
+              read(i) icl(ind,1:num_cls)
+              ix(ind) = j
+              ind = ind+1
+             end do
+             ind = ind-1
+             call InterpCls(ix,icl, T%cl_tensor, ind, almaxtensor,num_cls)
+            end if
+         
+         end if 
+ 
+         return
+     100 error = 1
+ 
+     end if
+ 
+    end subroutine ReadModel
+ 
+       subroutine InterpCls(l,iCl, all_Cl, n, almax, ncls)
+       integer, intent(in) :: n, almax,ncls
+    
+       real, intent(in) :: iCl(lmax,1:num_cls)
+       integer l(n),p
+       real all_Cl(:,:)
+    
+       integer il,llo,lhi,xi
+       real xl(n),  ddCl(n)
+ 
+       real a0,b0,ho
+       real inCl(n)
+ 
+     
+       do p =1, ncls
+ 
+         do il=1,n
+            inCl(il) = iCl(il,p)*l(il)**2
+         end do
+ 
+         xl = l
+         call spline_real(xl,inCl,n,ddCl)
+      
+         llo=1
+         do il=2,l(n)
+            xi=il
+            if ((xi > l(llo+1)).and.(llo < n)) then
+               llo=llo+1
+            end if
+            lhi=llo+1
+            ho=l(lhi)-l(llo)
+            a0=(xl(lhi)-xi)/ho
+            b0=(xi-xl(llo))/ho
+           
+            all_Cl(il,p) = (a0*inCl(llo)+ b0*inCl(lhi)+((a0**3-a0)* ddCl(llo) &
+                    +(b0**3-b0)*ddCl(lhi))*ho**2/6)/il**2
+   
+         end do
+ 
+       end do
+ 
+       all_Cl(l(n)+1:almax,:) = 0
+        
+ 
+       end subroutine InterpCls
+   
+   
+    subroutine ClsFromTheoryData(T, CMB, Cls)
+      Type(CosmoTheory) T
+      Type(CMBParams) CMB
+      real Cls(lmax,num_cls)
+      integer i
+      
+      Cls(2:lmax,1:num_clsS) =T%cl(2:lmax,1:num_clsS)    !CMB%norm(norm_As)*T%cl(2:lmax,1:num_clsS)
+      if (num_cls>3 .and. num_ClsS==3) Cls(2:lmax,num_cls)=0
+ 
+      i = norm_amp_ratio !this convolution is to avoid compile-time bounds-check errors on CMB%norm
+      if (CMB%norm(i) /= 0) then
+         Cls(2:lmax_tensor,:) =  Cls(2:lmax_tensor,:)+ T%cl_tensor(2:lmax_tensor,:) 
+          !CMB%norm(norm_As)*CMB%norm(norm_amp_ratio)*T%cl_tensor(2:lmax_tensor,:)
+      end if 
+ 
+    end subroutine ClsFromTheoryData
+ 
+    subroutine WriteTextCls(aname,T, CMB)
+      Type(CosmoTheory) T
+      Type(CMBParams) CMB
+      character (LEN=*), intent(in) :: aname
+      integer l
+      real Cls(lmax,num_cls)
+ 
+      call ClsFromTheoryData(T,CMB,Cls)
+      open(unit = tmp_file_unit, file = aname, form='formatted', status = 'replace')
+      do l=2, lmax
+         write (tmp_file_unit,*) l, Cls(l,:)*l*(l+1)/(2*pi)
+      end do
+      close(tmp_file_unit)
+ 
+    end subroutine WriteTextCls
+ 
+    function MatterPowerAt(T,kh)
+      !get matter power spectrum today at kh = k/h by interpolation from stored values
+      real, intent(in) :: kh
+      Type(CosmoTheory) T
+      real MatterPowerAt
+      real x, d
+      integer i
+    
+      x = log(kh/matter_power_minkh) / matter_power_dlnkh
+      if (x < 0 .or. x >= num_matter_power-1) then
+         write (*,*) ' k/h out of bounds in MatterPowerAt (',kh,')'
+         stop 
+      end if
+      i = int(x)
+      d = x - i
+      MatterPowerAt = exp(log(T%matter_power(i+1,1))*(1-d) &
+        + log(T%matter_power(i+2,1))*d)
+      !Just do linear interpolation in logs for now..
+      !(since we already cublic-spline interpolated to get the stored values)
+      !Assume matter_power_lnzsteps is at redshift zero
+    end function
+ 
+ 
+ 
+ !BR09 this function is just a copy of the one above but with LRG theory put in instead of linear theory
+    function LRGPowerAt(T,kh)
+      !get LRG matter power spectrum today at kh = k/h by interpolation from stored values
+      real, intent(in) :: kh
+      Type(CosmoTheory) T
+      real LRGPowerAt
+      real x, d
+      integer i
+    
+      x = log(kh/matter_power_minkh) / matter_power_dlnkh
+      if (x < 0 .or. x >= num_matter_power-1) then
+         write (*,*) ' k/h out of bounds in MatterPowerAt (',kh,')'
+         stop 
+      end if
+      i = int(x)
+      d = x - i
+      LRGPowerAt = exp(log(T%finalLRGtheoryPk(i+1))*(1-d) + log(T%finalLRGtheoryPk(i+2))*d)
+      !Just do linear interpolation in logs for now..
+      !(since we already cublic-spline interpolated to get the stored values)
+    end function
+ !!BRO09 addition end
+ 
+    function MatterPowerAt_Z(T,kh,z)
+      !get matter power spectrum at z at kh = k/h by interpolation from stored values
+ 
+      real, intent(in) :: kh
+      Type(CosmoTheory) T
+      real MatterPowerAt_Z
+      real x, d, z, y, dz, mup, mdn
+      real matter_power_dlnz
+      integer i, iz
+    
+      matter_power_dlnz = log(matter_power_maxz+1) / (matter_power_lnzsteps -1 + 1e-13)
+      y = log(1.+ z) / matter_power_dlnz 
+ 
+      if (z > matter_power_maxz ) then
+         write (*,*) ' z out of bounds in MatterPowerAt_Z (',z,')'
+         stop
+      end if
+      x = log(kh/matter_power_minkh) / matter_power_dlnkh
+      if (x < 0 .or. x >= num_matter_power-1) then
+         write (*,*) ' k/h out of bounds in MatterPowerAt_Z (',kh,')'
+         stop
+      end if
+ 
+      iz = int(y*0.99999999)
+      dz = y - iz
+ 
+      i = int(x)
+      d = x - i
+ 
+      mup = log(T%matter_power(i+1,iz+2))*(1-d) + log(T%matter_power(i+2,iz+2))*d
+      mdn = log(T%matter_power(i+1,iz+1))*(1-d) + log(T%matter_power(i+2,iz+1))*d
+ 
+      MatterPowerAt_Z = exp(mdn*(1-dz) + mup*dz)
+ 
+    end function MatterPowerAt_Z
+ 
+ 
+ 
+ 
+ end module cmbtypes
diff -r -c -b -N cosmomc/source/driver.F90 cosmomc_wiggles/source/driver.F90
*** cosmomc/source/driver.F90	2009-10-20 15:24:17.000000000 +0200
--- cosmomc_wiggles/source/driver.F90	2009-10-28 16:36:09.328216523 +0100
***************
*** 58,68 ****
  
  #ifdef MPI 
  
!         if (instance /= 0) call DoStop('With MPI should not have second parameter')
          call mpi_comm_rank(mpi_comm_world,MPIrank,ierror)
  
          instance = MPIrank +1 !start at 1 for chains                                             
!         write (numstr,*) instance
          rand_inst = instance
          if (ierror/=MPI_SUCCESS) call DoStop('MPI fail')
  
--- 58,77 ----
  
  #ifdef MPI 
  
! !wig
! !        if (instance /= 0) call DoStop('With MPI should not have second parameter')
!         instance_shift=instance
!         write(*,*)'MPI file names shifted by: ',instance_shift
! !end wig
          call mpi_comm_rank(mpi_comm_world,MPIrank,ierror)
    
          instance = MPIrank +1 !start at 1 for chains                                             
! !wig
! !        write (numstr,*) instance
! 	instance_shift = MPIrank + instance_shift
! 	write (numstr,*) instance_shift
! !end wig
! 
          rand_inst = instance
          if (ierror/=MPI_SUCCESS) call DoStop('MPI fail')
  
***************
*** 85,90 ****
--- 94,106 ----
  
          propose_scale = Ini_Read_Real('propose_scale',2.4)
          AccuracyLevel = Ini_Read_Real('accuracy_level',1.)
+ !wig
+         lAccuracyLevel = Ini_Read_Real('l_accuracy_level',1.)
+         lSampleLevel = Ini_Read_Real('l_sample_level',1.)
+         kSrcSampleLevel = Ini_Read_Real('k_src_sample_level',1.)
+         kIntSampleLevel = Ini_Read_Real('k_int_sample_level',1.)
+         CambComputeEveryl = Ini_Read_Logical('camb_compute_every_l',.false.)
+ !end wig
  
          checkpoint = Ini_Read_Logical('checkpoint',.false.)
          if (checkpoint) flush_write = .true.
diff -r -c -b -N cosmomc/source/GetDist.f90 cosmomc_wiggles/source/GetDist.f90
*** cosmomc/source/GetDist.f90	2009-10-27 16:41:31.000000000 +0100
--- cosmomc_wiggles/source/GetDist.f90	2009-10-28 16:36:22.056208166 +0100
***************
*** 89,94 ****
--- 89,97 ----
          logical force_twotail 
          logical smoothing, plot_meanlikes, plot_NDcontours
          logical shade_meanlikes, make_single_samples
+ !addon
+         logical td_meanlikes
+ !end addon
          integer single_thin,cust2DPlots(max_cols**2)
          integer ix_min(max_cols),ix_max(max_cols)
          real center(max_cols),width(max_cols)   
***************
*** 1339,1351 ****
   !             write (aunit,fmt) (/(I, I=ix_min(j), ix_max(j), 1)/)*width(j)+center(j)
                if (DoShade) then
                if (shade_meanlikes) then
                  write (aunit,'(a)') "load (fullfile('"//trim(plot_data_dir)//"','" // trim(plotfile) //'_likes''));'
                  write (aunit,*) 'contourf(x1,x2,'//trim(plotfile)//'_likes,64);'
                else     
                  write (aunit,*) 'contourf(x1,x2,pts,64);'
                end if
! 
                write (aunit,*) 'set(gca,''climmode'',''manual''); shading flat; hold on;'
                end if
                
             
--- 1342,1370 ----
   !             write (aunit,fmt) (/(I, I=ix_min(j), ix_max(j), 1)/)*width(j)+center(j)
                if (DoShade) then
                if (shade_meanlikes) then
+ !addon
                  write (aunit,'(a)') "load (fullfile('"//trim(plot_data_dir)//"','" // trim(plotfile) //'_likes''));'
+                  if (td_meanlikes) then
+                     write (aunit,*) 'surfl(x1,x2,'//trim(plotfile)//'_likes);'
+                  else
                  write (aunit,*) 'contourf(x1,x2,'//trim(plotfile)//'_likes,64);'
+                  endif
+ !end addon
+ 
                else     
                  write (aunit,*) 'contourf(x1,x2,pts,64);'
                end if
! !addon
! !              write (aunit,*) 'set(gca,''climmode'',''manual''); shading flat; hold on;'
!               if (td_meanlikes) then
!                  write (aunit,*) 'set(gca,''climmode'',''manual'');shading interp; hold on;'
!                  write (aunit,*) 'grid off;box on;camproj(''perspective'');'
!                  write (aunit,*) 'set(gca,''climmode'',''manual'');alpha(0.9);axis tight'
!               else
                write (aunit,*) 'set(gca,''climmode'',''manual''); shading flat; hold on;'
+               endif
+ !end addon
+ 
                end if
                
             
***************
*** 1762,1767 ****
--- 1781,1789 ----
            write (*,*) 'WARNING: Mapping params - .covmat file is new params.'
  
      shade_meanlikes = Ini_Read_Logical('shade_meanlikes',.false.)   
+ !addon
+     td_meanlikes = Ini_Read_Logical('td_meanlikes',.false.)  
+ !end addon
  
      matlab_col = Ini_Read_String('matlab_colscheme')
      smoothing = Ini_Read_Logical('smoothing',.true.)
***************
*** 2435,2441 ****
--- 2457,2470 ----
                write (51,*)  'set(gcf, ''PaperUnits'',''inches'');'
       
                if (plot_row < plot_col .and. plot_row*plot_col>9) then
+ !addon
+ !            write (51,*) 'set(gcf, ''PaperPosition'',[ 0 0 8 10]);';
+          if (td_meanlikes) then
+             write (51,*) 'set(gcf, ''PaperPosition'',[ 0 0 7 9]);';
+          else
                  write (51,*) 'set(gcf, ''PaperPosition'',[ 0 0 8 10]);';
+          endif
+ !end addon           
                else
                  write (51,*) 'set(gcf, ''PaperPosition'',[ 0 0 8 8]);';
                end if
diff -r -c -b -N cosmomc/source/lrggettheory.f90 cosmomc_wiggles/source/lrggettheory.f90
*** cosmomc/source/lrggettheory.f90	2009-10-27 00:48:14.000000000 +0100
--- cosmomc_wiggles/source/lrggettheory.f90	1970-01-01 01:00:00.000000000 +0100
***************
*** 1,189 ****
- !!! moved this routine from modules.f90 in CAMB here by preference of Antony.
- !!! needs to be separate from Pktheory.f90 because that file depends on CMB_Cls somehow
- 
- module lrggettheory
- use precision
- use Transfer
- implicit none
- 
- real(dl), parameter ::  aNEAR = 0.809717d0, aMID = 0.745156d0, aFAR = 0.70373d0
- real(dl), parameter ::  z0 = 0.0d0, zNEAR = 0.235d0, zMID = 0.342d0, zFAR = 0.421d0
- real(dl), parameter ::  sigma2BAONEAR = 86.9988, sigma2BAOMID = 85.1374, sigma2BAOFAR = 84.5958
- real(dl), parameter :: zeffDR7 = 0.312782  !! effective redshift of the LRG sample
- real(dl), dimension(4) :: transferscalefid  !! this is set in LRGinfo_init
- real(dl), dimension(4) :: powerscaletoz0  
-  !! this is to scale the amplitude of the redshift slices power spectra to the z=0 amplitude; 
-  !this is the assumption of the model.
- real(dl), parameter :: kmindata = 0.02 
-  !! in h/Mpc.  they are needed for normalizing nowiggs power spectrum. 
- ! Hard coded for the SDSS DR7 values.
- integer :: iz0lrg, izNEARlrg, izMIDlrg, izFARlrg
- logical :: use_dr7lrg = .false.  
- 
- contains
- 
-         subroutine Transfer_GetMatterPowerAndNW(MTrans,outpower, itf, in, minkh, dlnkh, &
-                 npoints, kmindata, getabstransferscale, outpowernw, outpowerrationwhalofit)
- 
-           !Allows for non-smooth priordial spectra
-           !if CP%Nonlinear/ = NonLinear_none includes non-linear evolution
-           !Get total matter power spectrum at logarithmically equal intervals dlnkh of k/h starting at minkh
-           !in units of (h Mpc^{-1})^3.
-           !Here there definition is < Delta^2(x) > = 1/(2 pi)^3 int d^3k P_k(k)
-           !We are assuming that Cls are generated so any baryonic wiggles are well sampled and that matter power
-           !sepctrum is generated to beyond the CMB k_max
-           Type(MatterTransferData), intent(in) :: MTrans
-           Type(MatterPowerData) :: PKnw
- 
-           integer, intent(in) :: itf, in, npoints 
-           real, intent(out) :: outpower(npoints)
-           real, intent(out) :: outpowernw(npoints), outpowerrationwhalofit(npoints)
-           real, intent(in) :: minkh, dlnkh
-           real(dl), intent(in) :: kmindata
-           real(dl), intent(out) :: getabstransferscale
-           real(dl), parameter :: cllo=1.e30_dl,clhi=1.e30_dl
-           integer ik, llo,il,lhi,lastix
-           real(dl) matpower(MTrans%num_q_trans), kh, kvals(MTrans%num_q_trans), ddmat(MTrans%num_q_trans)
-           real(dl) atransfer,xi, a0, b0, ho, logmink,k, h, fbaryon,omegam
-           real(dl) matpowernw(MTrans%num_q_trans), matpowernwhalofit(MTrans%num_q_trans), &
-           & atransfernw, atransfernwhalofit, &
-           &ddmatnw(MTrans%num_q_trans), ddmatnwhalofit(MTrans%num_q_trans)
- 
-           !!added for splining.
-           real(dl) :: mykvals(MTrans%num_q_trans),mylnpklinear(MTrans%num_q_trans),mylnpksmooth(MTrans%num_q_trans)
- 
-           integer :: nwi,tempi, setabs
-           Type(MatterTransferData) :: MTransnw
- 
-           MTransnw%num_q_trans = MTrans%num_q_trans
-           allocate(MTransnw%q_trans(MTransnw%num_q_trans))
-           allocate(MTransnw%TransferData(Transfer_max,MTransnw%num_q_trans,CP%Transfer%num_redshifts))
-           allocate(MTransnw%sigma_8(CP%Transfer%num_redshifts, CP%InitPower%nn))
- 
- 
-           h = CP%H0/100
-           do nwi = 1, MTransnw%num_q_trans
-             MTransnw%q_trans(nwi) = MTrans%q_trans(nwi)  !! not ever referenced.
-             MTransnw%TransferData(Transfer_kh,nwi,1) = MTrans%TransferData(Transfer_kh,nwi,1)
-             kh = MTrans%TransferData(Transfer_kh,nwi,1)
-             k = kh*h
-             do tempi=2,Transfer_tot
-               MTransnw%TransferData(tempi,nwi,1) = 0.0d0
-             end do
-             mykvals(nwi) = k
-             atransfer=MTrans%TransferData(transfer_power_var,nwi,itf)
-             mylnpklinear(nwi) = log(atransfer**2*k*pi*twopi*h**3*ScalarPower(k,in))
-           end do
- 
- #ifdef DR71RG
-           call dopksmoothbspline(mykvals,mylnpklinear,mylnpksmooth, MTrans%num_q_trans)
- #else
-          call MpiStop('mpk: edit makefile to have "EXTDATA = LRG" to inlude LRGs')
- #endif
-           setabs = 0
-           do nwi = 1, MTransnw%num_q_trans
-             kh = MTrans%TransferData(Transfer_kh,nwi,1)
-             if(kh > kmindata .and. setabs == 0) then
-               getabstransferscale = sqrt(exp(mylnpklinear(nwi)))
-               setabs = 1
-            end if
-             k = kh*h
-             MTransnw%TransferData(transfer_power_var,nwi,1) = sqrt(exp(mylnpksmooth(nwi))/(k*pi*twopi*h**3*ScalarPower(k,in)))
-           end do
-           if (npoints < 2) stop 'Need at least 2 points in Transfer_GetMatterPower'
-           if (minkh*exp((npoints-1)*dlnkh) > MTrans%TransferData(Transfer_kh,MTrans%num_q_trans,itf) &
-                 .and. FeedbackLevel > 0 ) &
-                     write(*,*) 'Warning: extrapolating matter power in Transfer_GetMatterPower'
- 
-           !! get nonlinear on Pnw
-           call Transfer_GetMatterPowerData(MTransnw,PKnw, in, 1)
-           Pknw%redshifts(1) = CP%Transfer%Redshifts(itf)
-           call NonLinear_GetRatios(Pknw)
- 
-           h = CP%H0/100
-           logmink = log(minkh)
-           do ik=1,MTrans%num_q_trans
-              kh = MTrans%TransferData(Transfer_kh,ik,itf)
-              k = kh*h
-              kvals(ik) = log(kh)
-              atransfer=MTrans%TransferData(transfer_power_var,ik,itf)
-              atransfernw=MTransnw%TransferData(transfer_power_var,ik,1)
-              atransfernwhalofit=MTransnw%TransferData(transfer_power_var,ik,1)
-              atransfernwhalofit = atransfernwhalofit * PKnw%nonlin_ratio(ik,1)
-              matpower(ik) = log(atransfer**2*k*pi*twopi*h**3)
-                  !Put in power spectrum later: transfer functions should be smooth, initial power may not be
- 
-              matpowernw(ik) = log(atransfernw**2*k*pi*twopi*h**3)
-              matpowernwhalofit(ik) = log(atransfernwhalofit**2*k*pi*twopi*h**3)
-           end do
-           call spline(kvals,matpower,MTrans%num_q_trans,cllo,clhi,ddmat)
-           call spline(kvals,matpowernw,MTrans%num_q_trans,cllo,clhi,ddmatnw)
-           call spline(kvals,matpowernwhalofit,MTrans%num_q_trans,cllo,clhi,ddmatnwhalofit)
- 
- 
-             llo=1
-             lastix = npoints + 1
-             do il=1, npoints
-                xi=logmink + dlnkh*(il-1)
-                if (xi < kvals(1)) then
-                  outpower(il)=-30.
-                  outpowernw(il)=-30.
-                  outpowerrationwhalofit(il)=-30.
-                  cycle
-                end if
-                do while ((xi > kvals(llo+1)).and.(llo < MTrans%num_q_trans))
-                   llo=llo+1
-                   if (llo >= MTrans%num_q_trans) exit
-                end do
-                if (llo == MTrans%num_q_trans) then
-                    lastix = il
-                    exit
-                end if
-                lhi=llo+1
-                ho=kvals(lhi)-kvals(llo)
-                a0=(kvals(lhi)-xi)/ho
-                b0=(xi-kvals(llo))/ho
- 
-                outpower(il) = a0*matpower(llo)+ b0*matpower(lhi)+((a0**3-a0)* ddmat(llo) &
-                        +(b0**3-b0)*ddmat(lhi))*ho**2/6
-                outpowernw(il) = a0*matpowernw(llo)+ b0*matpowernw(lhi)+((a0**3-a0)* ddmatnw(llo) &
-                        +(b0**3-b0)*ddmatnw(lhi))*ho**2/6
-                outpowerrationwhalofit(il) = a0*matpowernwhalofit(llo)+ b0*matpowernwhalofit(lhi)+((a0**3-a0)* ddmatnwhalofit(llo) &
-                        +(b0**3-b0)*ddmatnwhalofit(lhi))*ho**2/6
- 
-             end do
- 
-             do while (lastix <= npoints)
-                !Do linear extrapolation in the log
-                !Obviouly inaccurate, non-linear etc, but OK if only using in tails of window functions
-                outpower(lastix) = 2*outpower(lastix-1) - outpower(lastix-2)
-                outpowernw(lastix) = 2*outpowernw(lastix-1) - outpowernw(lastix-2)
-                outpowerrationwhalofit(lastix) = 2*outpowerrationwhalofit(lastix-1)&
-                          - outpowerrationwhalofit(lastix-2)
-                lastix = lastix+1
-             end do
- 
-             outpower = exp(max(-30.,outpower))
-             outpowernw = exp(max(-30.,outpowernw))
-             outpowerrationwhalofit = exp(max(-30.,outpowerrationwhalofit))
- 
- 
-             do il = 1, npoints
-                k = exp(logmink + dlnkh*(il-1))*h
-                outpower(il) = outpower(il) * ScalarPower(k,in)
-                outpowerrationwhalofit(il) = outpowerrationwhalofit(il)/outpowernw(il) 
-                 !! do this first because the ScalarPower calls cancel.
-                outpowernw(il) = outpowernw(il) * ScalarPower(k,in)
-                !print *,k/h,outpower(il),outpowernw(il)
-                !print *,k/h,outpowerrationwhalofit(il)
-             end do
- 
-           call MatterPowerdata_Free(PKnw)
-           deallocate(MTransnw%q_trans)
-           deallocate(MTransnw%TransferData)
-           deallocate(MTransnw%sigma_8)
- 
-         end subroutine Transfer_GetMatterPowerAndNW
- 
- 
- end module
--- 0 ----
diff -r -c -b -N cosmomc/source/lrggettheory.F90 cosmomc_wiggles/source/lrggettheory.F90
*** cosmomc/source/lrggettheory.F90	1970-01-01 01:00:00.000000000 +0100
--- cosmomc_wiggles/source/lrggettheory.F90	2009-10-28 16:49:31.960218537 +0100
***************
*** 0 ****
--- 1,189 ----
+ !!! moved this routine from modules.f90 in CAMB here by preference of Antony.
+ !!! needs to be separate from Pktheory.f90 because that file depends on CMB_Cls somehow
+ 
+ module lrggettheory
+ use precision
+ use Transfer
+ implicit none
+ 
+ real(dl), parameter ::  aNEAR = 0.809717d0, aMID = 0.745156d0, aFAR = 0.70373d0
+ real(dl), parameter ::  z0 = 0.0d0, zNEAR = 0.235d0, zMID = 0.342d0, zFAR = 0.421d0
+ real(dl), parameter ::  sigma2BAONEAR = 86.9988, sigma2BAOMID = 85.1374, sigma2BAOFAR = 84.5958
+ real(dl), parameter :: zeffDR7 = 0.312782  !! effective redshift of the LRG sample
+ real(dl), dimension(4) :: transferscalefid  !! this is set in LRGinfo_init
+ real(dl), dimension(4) :: powerscaletoz0  
+  !! this is to scale the amplitude of the redshift slices power spectra to the z=0 amplitude; 
+  !this is the assumption of the model.
+ real(dl), parameter :: kmindata = 0.02 
+  !! in h/Mpc.  they are needed for normalizing nowiggs power spectrum. 
+ ! Hard coded for the SDSS DR7 values.
+ integer :: iz0lrg, izNEARlrg, izMIDlrg, izFARlrg
+ logical :: use_dr7lrg = .false.  
+ 
+ contains
+ 
+         subroutine Transfer_GetMatterPowerAndNW(MTrans,outpower, itf, in, minkh, dlnkh, &
+                 npoints, kmindata, getabstransferscale, outpowernw, outpowerrationwhalofit)
+ 
+           !Allows for non-smooth priordial spectra
+           !if CP%Nonlinear/ = NonLinear_none includes non-linear evolution
+           !Get total matter power spectrum at logarithmically equal intervals dlnkh of k/h starting at minkh
+           !in units of (h Mpc^{-1})^3.
+           !Here there definition is < Delta^2(x) > = 1/(2 pi)^3 int d^3k P_k(k)
+           !We are assuming that Cls are generated so any baryonic wiggles are well sampled and that matter power
+           !sepctrum is generated to beyond the CMB k_max
+           Type(MatterTransferData), intent(in) :: MTrans
+           Type(MatterPowerData) :: PKnw
+ 
+           integer, intent(in) :: itf, in, npoints 
+           real, intent(out) :: outpower(npoints)
+           real, intent(out) :: outpowernw(npoints), outpowerrationwhalofit(npoints)
+           real, intent(in) :: minkh, dlnkh
+           real(dl), intent(in) :: kmindata
+           real(dl), intent(out) :: getabstransferscale
+           real(dl), parameter :: cllo=1.e30_dl,clhi=1.e30_dl
+           integer ik, llo,il,lhi,lastix
+           real(dl) matpower(MTrans%num_q_trans), kh, kvals(MTrans%num_q_trans), ddmat(MTrans%num_q_trans)
+           real(dl) atransfer,xi, a0, b0, ho, logmink,k, h, fbaryon,omegam
+           real(dl) matpowernw(MTrans%num_q_trans), matpowernwhalofit(MTrans%num_q_trans), &
+           & atransfernw, atransfernwhalofit, &
+           &ddmatnw(MTrans%num_q_trans), ddmatnwhalofit(MTrans%num_q_trans)
+ 
+           !!added for splining.
+           real(dl) :: mykvals(MTrans%num_q_trans),mylnpklinear(MTrans%num_q_trans),mylnpksmooth(MTrans%num_q_trans)
+ 
+           integer :: nwi,tempi, setabs
+           Type(MatterTransferData) :: MTransnw
+ 
+           MTransnw%num_q_trans = MTrans%num_q_trans
+           allocate(MTransnw%q_trans(MTransnw%num_q_trans))
+           allocate(MTransnw%TransferData(Transfer_max,MTransnw%num_q_trans,CP%Transfer%num_redshifts))
+           allocate(MTransnw%sigma_8(CP%Transfer%num_redshifts, CP%InitPower%nn))
+ 
+ 
+           h = CP%H0/100
+           do nwi = 1, MTransnw%num_q_trans
+             MTransnw%q_trans(nwi) = MTrans%q_trans(nwi)  !! not ever referenced.
+             MTransnw%TransferData(Transfer_kh,nwi,1) = MTrans%TransferData(Transfer_kh,nwi,1)
+             kh = MTrans%TransferData(Transfer_kh,nwi,1)
+             k = kh*h
+             do tempi=2,Transfer_tot
+               MTransnw%TransferData(tempi,nwi,1) = 0.0d0
+             end do
+             mykvals(nwi) = k
+             atransfer=MTrans%TransferData(transfer_power_var,nwi,itf)
+             mylnpklinear(nwi) = log(atransfer**2*k*pi*twopi*h**3*ScalarPower(k,in))
+           end do
+ 
+ #ifdef DR71RG
+           call dopksmoothbspline(mykvals,mylnpklinear,mylnpksmooth, MTrans%num_q_trans)
+ #else
+          call MpiStop('mpk: edit makefile to have "EXTDATA = LRG" to inlude LRGs')
+ #endif
+           setabs = 0
+           do nwi = 1, MTransnw%num_q_trans
+             kh = MTrans%TransferData(Transfer_kh,nwi,1)
+             if(kh > kmindata .and. setabs == 0) then
+               getabstransferscale = sqrt(exp(mylnpklinear(nwi)))
+               setabs = 1
+            end if
+             k = kh*h
+             MTransnw%TransferData(transfer_power_var,nwi,1) = sqrt(exp(mylnpksmooth(nwi))/(k*pi*twopi*h**3*ScalarPower(k,in)))
+           end do
+           if (npoints < 2) stop 'Need at least 2 points in Transfer_GetMatterPower'
+           if (minkh*exp((npoints-1)*dlnkh) > MTrans%TransferData(Transfer_kh,MTrans%num_q_trans,itf) &
+                 .and. FeedbackLevel > 0 ) &
+                     write(*,*) 'Warning: extrapolating matter power in Transfer_GetMatterPower'
+ 
+           !! get nonlinear on Pnw
+           call Transfer_GetMatterPowerData(MTransnw,PKnw, in, 1)
+           Pknw%redshifts(1) = CP%Transfer%Redshifts(itf)
+           call NonLinear_GetRatios(Pknw)
+ 
+           h = CP%H0/100
+           logmink = log(minkh)
+           do ik=1,MTrans%num_q_trans
+              kh = MTrans%TransferData(Transfer_kh,ik,itf)
+              k = kh*h
+              kvals(ik) = log(kh)
+              atransfer=MTrans%TransferData(transfer_power_var,ik,itf)
+              atransfernw=MTransnw%TransferData(transfer_power_var,ik,1)
+              atransfernwhalofit=MTransnw%TransferData(transfer_power_var,ik,1)
+              atransfernwhalofit = atransfernwhalofit * PKnw%nonlin_ratio(ik,1)
+              matpower(ik) = log(atransfer**2*k*pi*twopi*h**3)
+                  !Put in power spectrum later: transfer functions should be smooth, initial power may not be
+ 
+              matpowernw(ik) = log(atransfernw**2*k*pi*twopi*h**3)
+              matpowernwhalofit(ik) = log(atransfernwhalofit**2*k*pi*twopi*h**3)
+           end do
+           call spline(kvals,matpower,MTrans%num_q_trans,cllo,clhi,ddmat)
+           call spline(kvals,matpowernw,MTrans%num_q_trans,cllo,clhi,ddmatnw)
+           call spline(kvals,matpowernwhalofit,MTrans%num_q_trans,cllo,clhi,ddmatnwhalofit)
+ 
+ 
+             llo=1
+             lastix = npoints + 1
+             do il=1, npoints
+                xi=logmink + dlnkh*(il-1)
+                if (xi < kvals(1)) then
+                  outpower(il)=-30.
+                  outpowernw(il)=-30.
+                  outpowerrationwhalofit(il)=-30.
+                  cycle
+                end if
+                do while ((xi > kvals(llo+1)).and.(llo < MTrans%num_q_trans))
+                   llo=llo+1
+                   if (llo >= MTrans%num_q_trans) exit
+                end do
+                if (llo == MTrans%num_q_trans) then
+                    lastix = il
+                    exit
+                end if
+                lhi=llo+1
+                ho=kvals(lhi)-kvals(llo)
+                a0=(kvals(lhi)-xi)/ho
+                b0=(xi-kvals(llo))/ho
+ 
+                outpower(il) = a0*matpower(llo)+ b0*matpower(lhi)+((a0**3-a0)* ddmat(llo) &
+                        +(b0**3-b0)*ddmat(lhi))*ho**2/6
+                outpowernw(il) = a0*matpowernw(llo)+ b0*matpowernw(lhi)+((a0**3-a0)* ddmatnw(llo) &
+                        +(b0**3-b0)*ddmatnw(lhi))*ho**2/6
+                outpowerrationwhalofit(il) = a0*matpowernwhalofit(llo)+ b0*matpowernwhalofit(lhi)+((a0**3-a0)* ddmatnwhalofit(llo) &
+                        +(b0**3-b0)*ddmatnwhalofit(lhi))*ho**2/6
+ 
+             end do
+ 
+             do while (lastix <= npoints)
+                !Do linear extrapolation in the log
+                !Obviouly inaccurate, non-linear etc, but OK if only using in tails of window functions
+                outpower(lastix) = 2*outpower(lastix-1) - outpower(lastix-2)
+                outpowernw(lastix) = 2*outpowernw(lastix-1) - outpowernw(lastix-2)
+                outpowerrationwhalofit(lastix) = 2*outpowerrationwhalofit(lastix-1)&
+                          - outpowerrationwhalofit(lastix-2)
+                lastix = lastix+1
+             end do
+ 
+             outpower = exp(max(-30.,outpower))
+             outpowernw = exp(max(-30.,outpowernw))
+             outpowerrationwhalofit = exp(max(-30.,outpowerrationwhalofit))
+ 
+ 
+             do il = 1, npoints
+                k = exp(logmink + dlnkh*(il-1))*h
+                outpower(il) = outpower(il) * ScalarPower(k,in)
+                outpowerrationwhalofit(il) = outpowerrationwhalofit(il)/outpowernw(il) 
+                 !! do this first because the ScalarPower calls cancel.
+                outpowernw(il) = outpowernw(il) * ScalarPower(k,in)
+                !print *,k/h,outpower(il),outpowernw(il)
+                !print *,k/h,outpowerrationwhalofit(il)
+             end do
+ 
+           call MatterPowerdata_Free(PKnw)
+           deallocate(MTransnw%q_trans)
+           deallocate(MTransnw%TransferData)
+           deallocate(MTransnw%sigma_8)
+ 
+         end subroutine Transfer_GetMatterPowerAndNW
+ 
+ 
+ end module
diff -r -c -b -N cosmomc/source/Makefile cosmomc_wiggles/source/Makefile
*** cosmomc/source/Makefile	2009-10-27 16:42:57.000000000 +0100
--- cosmomc_wiggles/source/Makefile	2009-10-28 16:38:13.540218595 +0100
***************
*** 1,118 ****
! #You may need to edit the library paths for MKL for Intel
! #Beware of using optmizations that lose accuracy - may give errors when running
  
! ##Uncomment the next line to include dr7 LRG
! EXTDATA = 
! #EXTDATA = LRG
  
! #set WMAP empty not to compile with WMAP
! WMAP = /home/aml1005/WMAP5/likelihood_v3
  
! #Only needed for WMAP
! cfitsio = /usr/local/cfitsio/intel10/64/3.040
  
- #GSL only needed for DR7 LRG
- GSLPATH = /home/aml1005/libs/gsl
  
- IFLAG = -I
- INCLUDE=
- 
- #Intel MPI (assuming mpif77 set to point to ifort)
- #change -lmkl_ia32 to -lmkl_p3 for MKL versions earlier than 6 (6 needed for ifc 8+) 
- F90C     = mpif90
- FFLAGS = -O2 -ip -W0 -WB -openmp -fpp -DMPI -vec_report0
- LAPACKL = -L/usr/local/intel/mkl/9.1/lib/em64t -lmkl_lapack -lmkl_em64t -lguide -lpthread
- 
- #HPCF settings. Use Inteal 9 or 10.1+, not 10.0
- #F90C     = mpif90
- #FFLAGS = -O2 -Vaxlib -W0 -WB -openmp -fpp -DMPI -vec_report0
- #LAPACKL = -L/usr/local/Cluster-Apps/intel/mkl/10.2.2.025/lib/em64t -lmkl_lapack -lmkl -lguide -lpthread
- #GSLPATH = /usr/local/Cluster-Apps/gsl/1.9
- #cfitsio = /usr/local/Cluster-Users/cpac/cmb/2.1.0/cfitsio
- #INCLUDE=
- 
- #COSMOS: use "module load cosmolib" "module load latest" "module load gsl.stable"
- #use "runCosmomc" (globally installed) to run, defining required memory usage
- ifeq ($(COSMOHOST),cosmos)
- F90C = ifort
- FFLAGS = -openmp -O3 -w -fpp2 -DMPI
- LAPACKL = -mkl=sequential -lmkl_lapack -lmpi
- cfitsio = $(CFITSIO)
- WMAP = /home/cosmos/share-ia64/likelihood_v3
- GSLPATH = $(GSL_ROOT)
- endif
- 
- #Intel fortran 8, check you have the latest update from the Intel web pages
- #See Makefile_intel for ifc 7.1 or lower (some versions have problems)
- #F90C     = ifort
- #FFLAGS = -O2 -Vaxlib -ip -W0 -WB -openmp -fpp
- #LAPACKL = -L/opt/intel/mkl61/lib/32 -lmkl_lapack -lmkl_ia32 -lguide -lpthread
- 
- #G95; make sure LAPACK and MPI libs also compiled with g95
- #F90C     = mpif90
- #FFLAGS = -O2 -DMPI
- #LAPACKL = /LAPACK/lapack_LINUX.a /LAPACK/blas_LINUX.a
- 
- #GFortran: if pre v4.3 add -D__GFORTRAN__
- #F90C     = gfortran
- #FFLAGS = -O2 -ffree-form -x f95-cpp-input 
- #LAPACKL = -Wl,-framework -Wl,accelerate
- 
- #SGI, -mp toggles multi-processor. Use -O2 if -Ofast gives problems.
- #Not various versions of the compiler are buggy giving erroneous seg faults with -mp.
- #Version 7.3 is OK, currently version 7.4 is bugged, as are some earlier versions.
- #F90C     = f90
- #LAPACKL = -lcomplib.sgimath
- #FFLAGS  = -Ofast -mp
- 
- #Digital/Compaq fortran, -omp toggles multi-processor
- #F90C    = f90
- #FFLAGS  = -omp -O4 -arch host -math_library fast -tune host -fpe1
- #LAPACKL  = -lcxml
- 
- #Absoft ProFortran, single processor, set -cpu:[type] for your local system 
- #F90C     = f95
- #FFLAGS = -O2 -s -cpu:athlon -lU77 -w -YEXT_NAMES="LCS" -YEXT_SFX="_" 
- #LAPACKL =  -llapack -lblas -lg2c 
- #IFLAG = -p
- 
- #NAGF95, single processor:
- #F90C     = f95
- #FFLAGS = -DNAGF95 -O3
- #LAPACKL = -llapack -lblas -lg2c
- 
- #PGF90
- #F90C = pgf90
- #FFLAGS = -O2 -DESCAPEBACKSLASH
- #LAPACKL = -llapack -lblas
- 
- 
- #Sun, single processor:
- #F90C     = f90
- #FFLAGS = -fast -ftrap=%none
- #LAPACKL = -lsunperf -lfsu
- #LAPACKL = -lsunperf -lfsu -lsocket -lm
- #IFLAG = -M
- 
- #Sun MPI
- #F90C = mpf90
- #FFLAGS =  -O4 -openmp -ftrap=%none -dalign -DMPI
- #LAPACKL = -lsunperf -lfsu -lmpi_mt
- #IFLAG = -M
- 
- #Sun parallel enterprise:
- #F90C     = f95
- #FFLAGS =  -O4 -xarch=native64 -openmp -ftrap=%none
- #LAPACKL = -lsunperf -lfsu
- #IFLAG = -M
- 
- 
- #IBM XL Fortran, multi-processor (run "module load lapack" then run "gmake")
- # See also http://cosmocoffee.info/viewtopic.php?t=326
- #F90C     = xlf90_r $(LAPACK)
- #FFLAGS  = -WF,-DIBMXL -qsmp=omp -qsuffix=f=f90:cpp=F90 -O3 -qstrict -qarch=pwr3 -qtune=pwr3
- #INCLUDE = -lessl
- #LAPACKL =
  
  PROPOSE = propose.o
  CLSFILE = CMB_Cls_simple.o
--- 1,84 ----
! # >>> DESIGNED FOR GMAKE <<<
  
! # Unified Systems makefile for COSMOMC
! # Add FLAGS -DMPI for using MPI
! 
! ext=$(shell uname | cut -c1-3)
! 
! CC=cc
! 
! ifeq ($(ext),IRI)
! F90C= f90
! FFLAGS= -Ofast -mp -n32 -LANG:recursive=ON -lmpi -DMPI
! WMAPFLAGS= $(FFLAGS)
! LAPACKL = -lcomplib.sgimath_mp
! INCLUDE = -I../camb
! CFITSIODIR = /people/ringeval/usr
! GSLDIR =
! endif
! 
! ifeq ($(ext),Lin)
! F90C=gfortran
! FFLAGS= -O -fopenmp
! WMAPFLAGS= -O
! LAPACKL = -llapack
! INCLUDE = -I../camb 
! CFITSIODIR = /opt/gnu/cfitsio
! GSLDIR = /usr
! endif 
  
! ifeq ($(ext),OSF)
! F90C=f90
! FFLAGS= -omp -O -arch host -math_library fast -tune host -fpe1
! WMAPFLAGS= $(FFLAGS)
! LAPACKL  = -lcxml
! INCLUDE = -I../camb 
! CFITSIODIR = 
! GSLDIR =
! endif
  
! ifeq ($(ext),Sun)
! F90C=f90
! FFLAGS= -O4 -xarch=native64 -openmp -ftrap=%none
! WMAPFLAGS= $(FFLAGS)
! LAPACKL = -lsunperf -lfsu
! INCLUDE = -I../camb -M../camb
! CFITSIODIR = 
! GSLDIR =
! endif
! 
! ifeq ($(ext),AIX)
! F90C    = mpxlf90_r
! FFLAGS  = -O4 -WF,-DIBMXL,-DMPI -qstrict -qsmp=omp -qmaxmem=-1 -qsuffix=f=f90:cpp=F90
! WMAPFLAGS= $(FFLAGS)
! LAPACKL = -lessl
! INCLUDE = -I../camb
! CFITSIODIR = 
! GSLDIR =
! endif
! 
! #EXTDATA = LRG
! EXTDATA = LRG
! EXTINCLUDE = -I$(GSLDIR)/include
! EXTOBJS = bsplinepk.o
! 
! 
! WMAPDIR = ../WMAP
! WMAPINCLUDE = -I$(CFITSIODIR)/include
! WMAPOBJS = read_archive_map.o \
! 	read_fits.o \
! 	healpix_types.o \
! 	br_mod_dist.o \
! 	WMAP_5yr_options.o \
! 	WMAP_5yr_util.o \
! 	WMAP_5yr_gibbs.o \
! 	WMAP_5yr_tt_pixlike.o \
! 	WMAP_5yr_tt_beam_ptsrc_chisq.o \
! 	WMAP_5yr_teeebb_pixlike.o \
! 	WMAP_5yr_tetbeebbeb_pixlike.o \
! 	WMAP_5yr_likelihood.o
  
  
  
  PROPOSE = propose.o
  CLSFILE = CMB_Cls_simple.o
***************
*** 120,163 ****
  #Can use params_H if you prefer more generic parameters
  PARAMETERIZATION = params_CMB.o
  
- F90FLAGS = -DMATRIX_SINGLE $(FFLAGS) $(IFLAG)../camb $(INCLUDE)
  LINKFLAGS = -L../camb -lcamb $(LAPACKL) 
  
  DISTFILES = utils.o ParamNames.o Matrix_utils.o settings.o GetDist.o
  
! OBJFILES= utils.o ParamNames.o Matrix_utils.o settings.o IO.o cmbtypes.o Planck_like.o \
! 	cmbdata.o WeakLen.o bao.o lrggettheory.o mpk.o supernovae.o HST.o SDSSLy-a-v3.o \
  	$(CLSFILE) paramdef.o $(PROPOSE) $(PARAMETERIZATION) calclike.o \
  	conjgrad_wrapper.o EstCovmat.o postprocess.o MCMC.o driver.o
  
- 
  ifeq ($(EXTDATA),LRG)
  F90FLAGS += -DDR71RG
! OBJFILES += bsplinepk.o
! GSLINC = -I$(GSLPATH)/include
! LINKFLAGS += -L$(GSLPATH)/lib -lgsl -lgslcblas
! endif
! 
! ifneq ($(WMAP),)
! F90FLAGS += $(IFLAG)$(cfitsio)/include $(IFLAG)$(WMAP)
! LINKFLAGS +=  -L$(cfitsio)/lib -L$(WMAP) -lcfitsio
! OBJFILES += $(WMAP)/read_archive_map.o \
! 	$(WMAP)/read_fits.o \
! 	$(WMAP)/WMAP_5yr_options.o \
! 	$(WMAP)/WMAP_5yr_util.o \
! 	$(WMAP)/WMAP_5yr_tt_pixlike.o \
! 	$(WMAP)/WMAP_5yr_teeebb_pixlike.o \
! 	$(WMAP)/WMAP_5yr_likelihood.o \
! 	$(WMAP)/WMAP_5yr_gibbs.o \
! 	$(WMAP)/WMAP_5yr_tt_beam_ptsrc_chisq.o \
! 	$(WMAP)/br_mod_dist.o 
  else
  F90FLAGS += -DNOWMAP
  endif
  
- default: cosmomc
  
! all : cosmomc getdist
  
  utils.o: ../camb/libcamb.a
  settings.o: utils.o
--- 86,124 ----
  #Can use params_H if you prefer more generic parameters
  PARAMETERIZATION = params_CMB.o
  
  LINKFLAGS =  -L../camb -lcamb  $(LAPACKL)
  
+ F90FLAGS = -DMATRIX_SINGLE $(FFLAGS) $(INCLUDE)
+ 
  DISTFILES = utils.o ParamNames.o Matrix_utils.o settings.o GetDist.o
  
! 
! 
! OBJFILES = utils.o ParamNames.o Matrix_utils.o settings.o IO.o cmbtypes.o Planck_like.o \
! 	cmbdata.o WeakLen.o bao.o lrggettheory.o mpk.o supernovae.o HST.o SDSSLy-a-v3.o\
  	$(CLSFILE) paramdef.o $(PROPOSE) $(PARAMETERIZATION) calclike.o \
  	conjgrad_wrapper.o EstCovmat.o postprocess.o MCMC.o driver.o
  
  ifeq ($(EXTDATA),LRG)	
  F90FLAGS += -DDR71RG
! EXTINCLUDE = -I$(GSLDIR)/include
! LINKFLAGS += -lgsl -lgslcblas
! OBJFILES += $(EXTOBJS)
! endif	
! 
! ifneq ($(WMAPDIR),)
! F90FLAGS += $(WMAPINCLUDE)
! LINKFLAGS += -L$(CFITSIODIR)/lib -lcfitsio
! OBJFILES += $(WMAPOBJS)
  else
  F90FLAGS += -DNOWMAP
  endif
  
  
! 
! default: cosmomc.$(ext)
! 
! all : cosmomc.$(ext) getdist.$(ext)
  
  utils.o: ../camb/libcamb.a
  settings.o: utils.o
***************
*** 181,194 ****
  MCMC.o: calclike.o
  driver.o: MCMC.o
  
- export FFLAGS
- export F90C
- 
  .f.o:
  	f77 $(F90FLAGS) -c $<
  
  %.o: %.c
! 	$(CC) $(GSLINC) -c $*.c
  
  %.o: %.f90
  	$(F90C) $(F90FLAGS) -c $*.f90
--- 142,159 ----
  MCMC.o: calclike.o
  driver.o: MCMC.o
  
  .f.o:
  	f77 $(F90FLAGS) -c $<
  
  %.o: %.c
! 	$(CC) $(EXTINCLUDE) -c $*.c
! 
! %.o: $(WMAPDIR)/%.f90
! 	$(F90C) $(WMAPFLAGS) $(WMAPINCLUDE) -c $<
! 
! %.o: $(WMAPDIR)/%.F90
! 	$(F90C) $(WMAPFLAGS) $(WMAPINCLUDE) -c $<
! 
  
  %.o: %.f90
  	$(F90C) $(F90FLAGS) -c $*.f90
***************
*** 197,215 ****
  	$(F90C) $(F90FLAGS) -c $*.F90
  
  
! cosmomc: camb $(OBJFILES)
! 	$(F90C) -o ../cosmomc $(OBJFILES) $(LINKFLAGS) $(F90FLAGS)
  
  
! clean: cleancosmomc
! 	rm -f ../camb/*.o ../camb/*.obj
! 
! cleancosmomc:
  	rm -f *.o *.mod *.d *.pc *.obj ../core 
  
! 
! getdist: camb $(DISTFILES)
! 	$(F90C) -o ../getdist $(DISTFILES) $(LINKFLAGS) $(F90FLAGS) 
! 
! camb:
! 	cd ../camb && $(MAKE) --file=Makefile_main libcamb.a
--- 162,173 ----
  	$(F90C) $(F90FLAGS) -c $*.F90
  
  
! cosmomc.$(ext): $(OBJFILES) ../camb/libcamb.a 	
! 	$(F90C) -o ../$@ $(OBJFILES) $(LINKFLAGS) $(F90FLAGS)
  
  
! clean:
  	rm -f *.o *.mod *.d *.pc *.obj ../core
  
! getdist.$(ext): $(DISTFILES)
! 	$(F90C) -o ../$@ $(DISTFILES) $(LINKFLAGS) $(F90FLAGS) 
diff -r -c -b -N cosmomc/source/MCMC.f90 cosmomc_wiggles/source/MCMC.f90
*** cosmomc/source/MCMC.f90	2009-09-18 18:32:05.000000000 +0200
--- cosmomc_wiggles/source/MCMC.f90	2009-10-28 16:36:32.936208260 +0100
***************
*** 215,221 ****
    
      Like = GetLogLike(grid(r))
     
!     if (Feedback > 1) write (*,*) r, 'Likelihood: ', Like, 'Current Like:', CurLike
  
      if ((Like /= logZero) .and. (CurLike > Like .or. randexp1() > Like - CurLike)) then
      !Accept
--- 215,221 ----
    
      Like = GetLogLike(grid(r))
     
!     if (Feedback > 1) write (*,*) r, 'Likelihood: ', Like, 'Current Like:', CurLike, 'Rank', MPIRank
  
      if ((Like /= logZero) .and. (CurLike > Like .or. randexp1() > Like - CurLike)) then
      !Accept
***************
*** 501,507 ****
             output_lines = output_lines +1
             call WriteParams(CurParams, real(mult), CurLike)
            end if
!           if (Feedback > 1) write (*,*) instance, 'Slicing, Current Like:', CurLike
            mult = 1
            if (num_slow /=0) call SliceSampleSlowParam(CurParams, CurLike) 
            if (num_fast /=0) call SliceSampleFastParams(CurParams, CurLike) 
--- 501,507 ----
             output_lines = output_lines +1
             call WriteParams(CurParams, real(mult), CurLike)
            end if
!           if (Feedback > 1) write (*,*) instance, 'Slicing, Current Like:', CurLike, 'Rank', MPIRank
            mult = 1
            if (num_slow /=0) call SliceSampleSlowParam(CurParams, CurLike) 
            if (num_fast /=0) call SliceSampleFastParams(CurParams, CurLike) 
***************
*** 536,542 ****
  
      Like = GetLogLike(Trial) 
  
!     if (Feedback > 1) write (*,*) 'Likelihood: ', Like, 'Current Like:', CurLike
  
  !!!    accpt = (Like /= logZero) .and. (CurLike > Like .or. randexp1() > Like - CurLike) 
               !Include the min() so that compilers not doing optimal compilation don't complain
--- 536,542 ----
  
      Like = GetLogLike(Trial) 
  
!     if (Feedback > 1) write (*,*) 'Likelihood: ', Like, 'Current Like:', CurLike,  'Rank', MPIRank
  
  !!!    accpt = (Like /= logZero) .and. (CurLike > Like .or. randexp1() > Like - CurLike) 
               !Include the min() so that compilers not doing optimal compilation don't complain
diff -r -c -b -N cosmomc/source/mpk.f90 cosmomc_wiggles/source/mpk.f90
*** cosmomc/source/mpk.f90	2009-10-23 10:48:04.000000000 +0200
--- cosmomc_wiggles/source/mpk.f90	1970-01-01 01:00:00.000000000 +0100
***************
*** 1,1148 ****
- !Module storing observed matter power spectrum datasets, their points and window functions
- !and routines for computing the likelihood
- 
- !This code is based on that in cmbdata.f90 
- !and on Sam Leach's incorporation of Max Tegmark's SDSS code
- !
- !Originally SLB Sept 2004
- !AL April 2006: added covariance matrix support (following 2df 2005)
- !LV_06 : incorporation of LRG DR4 from Tegmark et al . astroph/0608632 
- !AL: modified LV SDSS to do Q and b^2 or b^2*Q marge internally as for 2df
- !BR09: added model LRG power spectrum.
- !AL Oct 20: switch to Ini_Read_xxx_File; fortran compatibility changes
- 
- 
- module LRGinfo
- use settings
- use cmbtypes
- use Precision
- use lrggettheory
- 
- !use CMB_Cls
- 
- implicit none
- 
- !! these are the LRG redshift subsample weights.
- real(dl), parameter :: w0 = 0.0d0, wNEAR = 0.395d0, wMID = 0.355d0, wFAR = 0.250d0
- 
- !in CAMB: 4=now (z=0), 3=NEAR, 2=MID, 1=FAR; opposite order in matter_power
- !! now generalized indices iz0lrg, izNEARlrg, izMIDlrg, izFARlrg
- real(dl), dimension(4) :: zeval, zweight, sigma2BAOfid, sigma2BAO
- 
- real(dl) om_val, ol_val, ok_check, wval  ! passed in from CMBparams CMB
- 
- ! power spectra evaluated at fiducial cosmological theory (WMAP5 recommended values)
- real, allocatable :: ratio_power_nw_nl_fid(:,:)
- !real,dimension(num_matter_power,matter_power_lnzsteps) :: ratio_power_nw_nl_fid
- !make allocatable to avoid compile-time range errors when matter_power_lnzsteps<4
- 
- contains
- 
- subroutine LRGinfo_init()
-   integer :: iopb, i, ios
-   real(dl) :: omegakdummy,omegavdummy,wdummy,getabstransferscalefiddummy
-   real(dl) :: kval, plin, psmooth, rationwhalofit
- 
-   !!BR09 only needed for LRGs, so only 4 redshifts no matter what matter_power_lnzsteps is
-   allocate(ratio_power_nw_nl_fid(num_matter_power,4))
- 
-   sigma2BAOfid(1) = 1.0e-5  !! don't do any smearing at z=0; this won't be used anyway.
-   sigma2BAOfid(2) = sigma2BAONEAR
-   sigma2BAOfid(3) = sigma2BAOMID
-   sigma2BAOfid(4) = sigma2BAOFAR
- 
-   zeval(1) = z0
-   zeval(2) = zNEAR
-   zeval(3) = zMID
-   zeval(4) = zFAR
- 
-   zweight(1) = w0
-   zweight(2) = wNEAR
-   zweight(3) = wMID
-   zweight(4) = wFAR
- 
-   !! first read in everything needed from the CAMB output files.
-   iopb = 0 !! check later if there was an error
- 
-   open(unit=tmp_file_unit,file=trim(DataDir)//'lrgdr7fiducialmodel_matterpowerzNEAR.dat',form='formatted',err=500, iostat=ios)
-   read (tmp_file_unit,*,iostat=iopb) getabstransferscalefiddummy, omegakdummy,omegavdummy,wdummy
-   do i = 1, num_matter_power
-     read (tmp_file_unit,*,iostat=iopb) kval, plin, psmooth, rationwhalofit
-     ratio_power_nw_nl_fid(i,2) = rationwhalofit
-   end do
-   close(tmp_file_unit)
- 
-   open(unit=tmp_file_unit,file=trim(DataDir)//'lrgdr7fiducialmodel_matterpowerzMID.dat',form='formatted',err=500, iostat=ios)
-   read (tmp_file_unit,*,iostat=iopb) getabstransferscalefiddummy,omegakdummy,omegavdummy,wdummy
-   do i = 1, num_matter_power
-     read (tmp_file_unit,*,iostat=iopb) kval, plin, psmooth, rationwhalofit
-     ratio_power_nw_nl_fid(i,3) = rationwhalofit
-   end do
-   close(tmp_file_unit)
- 
-   open(unit=tmp_file_unit,file=trim(DataDir)//'lrgdr7fiducialmodel_matterpowerzFAR.dat',form='formatted',err=500,iostat=ios)
-   read (tmp_file_unit,*,iostat=iopb) getabstransferscalefiddummy,omegakdummy,omegavdummy,wdummy
-   do i = 1, num_matter_power
-     read (tmp_file_unit,*,iostat=iopb) kval, plin, psmooth, rationwhalofit
-     ratio_power_nw_nl_fid(i,4) = rationwhalofit
-   end do
-   close(tmp_file_unit)
- 
- 500 if(ios .ne. 0) stop 'Unable to open file'
-   if(iopb .ne. 0) stop 'Error reading model or fiducial theory files.'
- end subroutine LRGinfo_init
- 
- ! HARD CODING OF POLYNOMIAL FITS TO NEAR, MID, FAR SUBSAMPLES.
- subroutine LRGtoICsmooth(k,fidpolys)
-   real(dl), intent(in) :: k
-   real(dl) :: fidNEAR, fidMID, fidFAR
-   real(dl), dimension(2:4), intent(out) :: fidpolys
- 
-   if(k < 0.194055d0) then !!this is where the two polynomials are equal
-     fidNEAR = (1.0d0 - 0.680886d0*k + 6.48151d0*k**2)
-   else
-     fidNEAR = (1.0d0 - 2.13627d0*k + 21.0537d0*k**2 - 50.1167d0*k**3 + 36.8155d0*k**4)*1.04482d0
-   end if
- 
-   if(k < 0.19431) then
-     fidMID = (1.0d0 - 0.530799d0*k + 6.31822d0*k**2)
-   else
-     fidMID = (1.0d0 - 1.97873d0*k + 20.8551d0*k**2 - 50.0376d0*k**3 + 36.4056d0*k**4)*1.04384
-   end if
- 
-   if(k < 0.19148) then
-     fidFAR = (1.0d0 - 0.475028d0*k + 6.69004d0*k**2)
-   else
-     fidFAR = (1.0d0 - 1.84891d0*k + 21.3479d0*k**2 - 52.4846d0*k**3 + 38.9541d0*k**4)*1.03753
-   end if
-   fidpolys(2) = fidNEAR
-   fidpolys(3) = fidMID
-   fidpolys(4) = fidFAR
- end subroutine LRGtoICsmooth
- 
- subroutine fill_LRGTheory(Theory, minkh, dlnkh)
-   Type(CosmoTheory) Theory
-   real, intent(in) :: minkh, dlnkh
-   real(dl) :: logmink, xi, kval, expval, psmear, nlrat
-   real(dl), dimension(2:4) :: fidpolys, holdval
- 
-   integer :: iz, ik, matterpowerindx
- 
-   do iz = 1, 4
-     sigma2BAO(iz) = sigma2BAOfid(iz)
-   end do
- 
-   logmink = log(minkh)
-   do ik=1,num_matter_power
-    xi = logmink + dlnkh*(ik-1)
-    kval = exp(xi)
-    Theory%finalLRGtheoryPk(ik) = 0.
-    do iz = 2,4
-     if(iz == 2) matterpowerindx = izNEARlrg
-     if(iz == 3) matterpowerindx = izMIDlrg
-     if(iz == 4) matterpowerindx = izFARlrg
-     expval = exp(-kval**2*sigma2BAO(iz)*0.5)
-     psmear = (Theory%matter_power(ik,matterpowerindx))*expval + (Theory%mpk_nw(ik,matterpowerindx))*(1.0-expval)
-     psmear = psmear*powerscaletoz0(iz)
-     nlrat = (Theory%mpkrat_nw_nl(ik,matterpowerindx))/(ratio_power_nw_nl_fid(ik,matterpowerindx))
-     call LRGtoICsmooth(kval,fidpolys)
-     holdval(iz) = zweight(iz)*psmear*nlrat*fidpolys(iz)
-     Theory%finalLRGtheoryPk(ik) = Theory%finalLRGtheoryPk(ik) + holdval(iz)
-    end do
- 
-   end do
- 
- end subroutine fill_LRGTheory
- 
- end module
- 
- module mpk
- use precision
- use settings
- use cmbtypes
- use LRGinfo
- implicit none
- 
-  Type mpkdataset
-     logical :: use_set
-     integer :: num_mpk_points_use ! total number of points used (ie. max-min+1)
-     integer :: num_mpk_kbands_use ! total number of kbands used (ie. max-min+1)
-     character(LEN=20) :: name
-     real, pointer, dimension(:,:) :: N_inv
-     real, pointer, dimension(:,:) :: mpk_W, mpk_invcov
-     real, pointer, dimension(:) :: mpk_P, mpk_sdev, mpk_k
-     real, pointer, dimension(:) :: mpk_zerowindowfxn
-     real, pointer, dimension(:) :: mpk_zerowindowfxnsubtractdat
-     real :: mpk_zerowindowfxnsubdatnorm !!the 0th entry in windowfxnsubtract file
-     logical :: use_scaling !as SDSS_lrgDR3
-    !for Q and A see e.g. astro-ph/0501174, astro-ph/0604335
-     logical :: Q_marge, Q_flat
-     real :: Q_mid, Q_sigma, Ag
-    end Type mpkdataset
- 
-  integer :: num_mpk_datasets = 0
-  Type(mpkdataset) mpkdatasets(10)
- 
-  !Note all units are in k/h here
-  
-   integer, parameter :: mpk_d = kind(1.d0)
-  
-   logical :: use_mpk = .false.
-   
-   ! constants describing the allowed a1,a2 regions.
-   ! must check the functions below before changing these, because the shape of the space may change!
- 
-   integer, parameter :: wp = selected_real_kind(11,99)
- 
-   !!these are the 'nonconservative' nuisance parameter bounds
-   !!real(dl), parameter :: k1 = 0.1d0, k2 = 0.2d0, s1 = 0.02d0, s2 = 0.05d0, a1maxval = 0.5741d0
-   real(dl), parameter :: k1 = 0.1d0, k2 = 0.2d0, s1 = 0.04d0, s2 = 0.10d0, a1maxval = 1.1482d0
-   integer, parameter :: nptsa1 = 41, nptsa2 = 41, nptstot = 325  
-   !! but total number of points to evaluate is much smaller than 41**2 because lots of the space 
-   !is not allowed by the s1,s2 constraints.
-  
-    ! only want to compute these once. 
-    real(dl), dimension(nptstot) :: a1list, a2list
- 
- contains 
- 
-   subroutine mpk_SetTransferRedshifts(redshifts)
-    real, intent(inout) :: redshifts(*)
-    !input is default log z spacing; can change here; check for consistency with other (e.g. lya)
-          
-    !Note internal ordering in CAMB is the opposite to that used in cosmomc transfer arrays (as here)
-    !first index here must be redshift zero
-    
-       if(use_dr7lrg .and. matter_power_lnzsteps < 4) &
-        call MpiStop('For LRGs matter_power_lnzsteps should be set to at least 4 (hardcoded in cmbtypes)')
- 
-        if (matter_power_lnzsteps==1 .or. .not. use_dr7lrg) return
- 
-        !! assigning indices to LRG NEAR, MID, FAR.  If you want to reorder redshifts, just change here.
-        iz0lrg = 1  !! we use the z=0 output to normalize things; this is already assumed index 1 elsewhere 
-                     !(like in calculation of sigma8).
-        izNEARlrg = 2
-        izMIDlrg = 3
-        izFARlrg = 4
-        redshifts(izNEARlrg) = zNEAR
-        redshifts(izMIDlrg) = zMID
-        redshifts(izFARlrg) = zFAR  
-        if(iz0lrg /= 1) then
-           redshifts(iz0lrg) = 0.0d0
-        else
-           if(redshifts(1) > 0.001) call MpiStop('redshifts(1) should be at z=0!')
-        endif
-          
-   end subroutine mpk_SetTransferRedshifts
-  
-   subroutine ReadmpkDataset(gname)   
-     use MatrixUtils
-     character(LEN=*), intent(IN) :: gname
-     character(LEN=Ini_max_string_len) :: kbands_file, measurements_file, windows_file, cov_file
-     !! added for the LRG window function subtraction
-     character(LEN=Ini_max_string_len) :: zerowindowfxn_file, zerowindowfxnsubtractdat_file
- 
-     Type (mpkdataset) :: mset
- 
-     integer i,iopb
-     real keff,klo,khi,beff
-     integer :: num_mpk_points_full ! actual number of bandpowers in the infile
-     integer :: num_mpk_kbands_full ! actual number of k positions " in the infile
-     integer :: max_mpk_points_use ! in case you don't want the smallest scale modes (eg. sdss)
-     integer :: min_mpk_points_use ! in case you don't want the largest scale modes
-     integer :: max_mpk_kbands_use ! in case you don't want to calc P(k) on the smallest scales (will truncate P(k) to zero here!)
-     integer :: min_mpk_kbands_use ! in case you don't want to calc P(k) on the largest scales (will truncate P(k) to zero here!)
-     real, dimension(:,:), allocatable :: mpk_Wfull, mpk_covfull
-     real, dimension(:), allocatable :: mpk_kfull, mpk_fiducial
- 
-     real, dimension(:), allocatable :: mpk_zerowindowfxnfull
-     real, dimension(:), allocatable :: mpk_zerowindowfxnsubfull
- 
-     character(80) :: dummychar
-     logical bad
-     Type(TIniFile) :: Ini
-     integer file_unit
-     
-  
-     num_mpk_datasets = num_mpk_datasets + 1
-     if (num_mpk_datasets > 10) stop 'too many datasets'
-     file_unit = new_file_unit()
-     call Ini_Open_File(Ini, gname, file_unit, bad, .false.)
-     if (bad) then
-       write (*,*)  'Error opening dataset file '//trim(gname)
-       stop
-     end if
- 
-     mset%name = Ini_Read_String_File(Ini,'name') 
-     Ini_fail_on_not_found = .false.
-     mset%use_set =.true.
-     if (Feedback > 0) write (*,*) 'reading: '//trim(mset%name)
-     num_mpk_points_full = Ini_Read_Int_File(Ini,'num_mpk_points_full',0)
-     if (num_mpk_points_full.eq.0) write(*,*) ' ERROR: parameter num_mpk_points_full not set'
-     num_mpk_kbands_full = Ini_Read_Int_File(Ini,'num_mpk_kbands_full',0)
-     if (num_mpk_kbands_full.eq.0) write(*,*) ' ERROR: parameter num_mpk_kbands_full not set'
-     min_mpk_points_use = Ini_Read_Int_File(Ini,'min_mpk_points_use',1)
-     min_mpk_kbands_use = Ini_Read_Int_File(Ini,'min_mpk_kbands_use',1)
-     max_mpk_points_use = Ini_Read_Int_File(Ini,'max_mpk_points_use',num_mpk_points_full)
-     max_mpk_kbands_use = Ini_Read_Int_File(Ini,'max_mpk_kbands_use',num_mpk_kbands_full)
-     mset%num_mpk_points_use = max_mpk_points_use - min_mpk_points_use +1
-     mset%num_mpk_kbands_use = max_mpk_kbands_use - min_mpk_kbands_use +1
- 
-     allocate(mpk_Wfull(num_mpk_points_full,num_mpk_kbands_full))
-     allocate(mpk_kfull(num_mpk_kbands_full))
-     allocate(mset%mpk_P(mset%num_mpk_points_use))
-     allocate(mset%mpk_sdev(mset%num_mpk_points_use))  ! will need to replace with the covmat
-     allocate(mset%mpk_k(mset%num_mpk_kbands_use))
-     allocate(mset%mpk_W(mset%num_mpk_points_use,mset%num_mpk_kbands_use))
-     allocate(mset%mpk_zerowindowfxn(mset%num_mpk_kbands_use))
-     allocate(mset%mpk_zerowindowfxnsubtractdat(mset%num_mpk_points_use))
-     allocate(mpk_fiducial(mset%num_mpk_points_use))
-     allocate(mpk_zerowindowfxnsubfull(num_mpk_points_full+1)) 
-       !!need to add 1 to get the normalization held in the first (really zeroth) entry
-     allocate(mpk_zerowindowfxnfull(num_mpk_kbands_full))
- 
-     kbands_file  = ReadIniFileName(Ini,'kbands_file')
-     call ReadVector(kbands_file,mpk_kfull,num_mpk_kbands_full)
-     mset%mpk_k(1:mset%num_mpk_kbands_use)=mpk_kfull(min_mpk_kbands_use:max_mpk_kbands_use) 
-     if (Feedback > 1) then 
-        write(*,*) 'reading: ',mset%name,' data'
-        write(*,*) 'Using kbands windows between',mset%mpk_k(1),' < k/h < ',mset%mpk_k(mset%num_mpk_kbands_use)      
-     endif
-     if  (mset%mpk_k(1) < matter_power_minkh) then
-        write (*,*) 'WARNING: k_min in '//trim(mset%name)//'less than setting in cmbtypes.f90'
-        write (*,*) 'all k<matter_power_minkh will be set to matter_power_minkh' 
-     end if
- 
-     measurements_file  = ReadIniFileName(Ini,'measurements_file')
-     call OpenTxtFile(measurements_file, tmp_file_unit)
-     mset%mpk_P=0.
-     read (tmp_file_unit,*) dummychar
-     read (tmp_file_unit,*) dummychar
-     do i= 1, (min_mpk_points_use-1)
-        read (tmp_file_unit,*, iostat=iopb) keff,klo,khi,beff,beff,beff
-     end do
-     if (Feedback > 1 .and. min_mpk_points_use>1) write(*,*) 'Not using bands with keff=  ',keff,' or below'
-     do i =1, mset%num_mpk_points_use
-        read (tmp_file_unit,*, iostat=iopb) keff,klo,khi,mset%mpk_P(i),mset%mpk_sdev(i),mpk_fiducial(i)
-     end do
-     close(tmp_file_unit) 
-     if (Feedback > 1) write(*,*) 'bands truncated at keff=  ',keff
-     
-     windows_file  = ReadIniFileName(Ini,'windows_file')
-     if (windows_file.eq.'') write(*,*) 'ERROR: mpk windows_file not specified'
-     call ReadMatrix(windows_file,mpk_Wfull,num_mpk_points_full,num_mpk_kbands_full)
-     mset%mpk_W(1:mset%num_mpk_points_use,1:mset%num_mpk_kbands_use)= &
-        mpk_Wfull(min_mpk_points_use:max_mpk_points_use,min_mpk_kbands_use:max_mpk_kbands_use)
-     
-  
-     if (mset%name == 'lrg_2009') then
- #ifndef DR71RG
-         call MpiStop('mpk: edit makefile to have "EXTDATA = LRG" to inlude LRGs')
- #else
-         use_dr7lrg = .true.
-         zerowindowfxn_file  = ReadIniFileName(Ini,'zerowindowfxn_file')
-     
-         print *, 'trying to read this many points', num_mpk_kbands_full
-         if (zerowindowfxn_file.eq.'') write(*,*) 'ERROR: mpk zerowindowfxn_file not specified'
-         call ReadVector(zerowindowfxn_file,mpk_zerowindowfxnfull,num_mpk_kbands_full)
-         mset%mpk_zerowindowfxn(1:mset%num_mpk_kbands_use) = mpk_zerowindowfxnfull(min_mpk_kbands_use:max_mpk_kbands_use)
-         zerowindowfxnsubtractdat_file  = ReadIniFileName(Ini,'zerowindowfxnsubtractdat_file')
-         if (zerowindowfxnsubtractdat_file.eq.'') write(*,*) 'ERROR: mpk zerowindowfxnsubtractdat_file not specified'
-         call ReadVector(zerowindowfxnsubtractdat_file,mpk_zerowindowfxnsubfull,num_mpk_points_full+1)
-         mset%mpk_zerowindowfxnsubtractdat(1:mset%num_mpk_points_use) = &
-          mpk_zerowindowfxnsubfull(min_mpk_points_use+1:max_mpk_points_use+1)
-         mset%mpk_zerowindowfxnsubdatnorm = mpk_zerowindowfxnsubfull(1)
- #endif
-     end if
- 
-     cov_file  = ReadIniFileName(Ini,'cov_file')
-     if (cov_file /= '') then
-      allocate(mpk_covfull(num_mpk_points_full,num_mpk_points_full))
-      call ReadMatrix(cov_file,mpk_covfull,num_mpk_points_full,num_mpk_points_full)
-      allocate(mset%mpk_invcov(mset%num_mpk_points_use,mset%num_mpk_points_use))
-      mset%mpk_invcov=  mpk_covfull(min_mpk_points_use:max_mpk_points_use,min_mpk_points_use:max_mpk_points_use)
-      call Matrix_Inverse(mset%mpk_invcov)
-      deallocate(mpk_covfull)
-     else
-      nullify(mset%mpk_invcov)
-     end if
- 
-     mset%use_scaling = Ini_Read_Logical_File(Ini,'use_scaling',.false.)
- 
-     mset%Q_marge = Ini_Read_Logical_File(Ini,'Q_marge',.false.)
-     if (mset%Q_marge) then
-      mset%Q_flat = Ini_Read_Logical_File(Ini,'Q_flat',.false.)
-      if (.not. mset%Q_flat) then
-       !gaussian prior on Q
-       mset%Q_mid = Ini_Read_Real_File(Ini,'Q_mid')
-       mset%Q_sigma = Ini_Read_Real_File(Ini,'Q_sigma')
-      end if
-      mset%Ag = Ini_Read_Real_File(Ini,'Ag', 1.4)
-     end if 
-     if (iopb.ne.0) then
-        stop 'Error reading mpk file'
-     endif
-  
-    call Ini_Close_File(Ini)
-    call ClearFileUnit(file_unit)
-    
-    deallocate(mpk_Wfull, mpk_kfull,mpk_fiducial)
- 
-    mpkdatasets(num_mpk_datasets) = mset
-  
-   if (mset%name == 'lrg_2009') call LSS_LRG_mpklike_init()
- 
-   end subroutine ReadmpkDataset
- 
-  
-   function LSS_mpklike(Theory,mset,CMB) result(LnLike) ! LV_06 added CMB here
-    Type (mpkdataset) :: mset
-    Type (CosmoTheory) Theory
-    Type(CMBparams) CMB     !LV_06 added for LRGDR4
-    real LnLike
-    real, dimension(:), allocatable :: mpk_Pth, mpk_k2,mpk_lin,k_scaled !LV_06 added for LRGDR4
-    real, dimension(:), allocatable :: w
-    real, dimension(:), allocatable :: mpk_WPth, mpk_WPth_k2
-    real :: covdat(mset%num_mpk_points_use), covth(mset%num_mpk_points_use),  covth_k2(mset%num_mpk_points_use)
-    real :: normV, Q, minchisq
-    real :: a_scl  !LV_06 added for LRGDR4
-    integer :: i, iQ
-    logical :: do_marge
-    integer, parameter :: nQ=6
-    real :: tmp, dQ = 0.4
-    real chisq(-nQ:nQ)
-    real calweights(-nQ:nQ)
-    real vec2(2),Mat(2,2)
-    
-    allocate(mpk_lin(mset%num_mpk_kbands_use) ,mpk_Pth(mset%num_mpk_kbands_use))
-    allocate(mpk_WPth(mset%num_mpk_points_use))
-    allocate(k_scaled(mset%num_mpk_kbands_use))!LV_06 added for LRGDR4
-    allocate(w(mset%num_mpk_points_use))
- 
-    chisq = 0
- 
-    if (.not. mset%use_set) then
-       LnLike = 0
-       return
-    end if
- 
-    ! won't actually want to do this multiple times for multiple galaxy pk data sets?..
- 
-    IF(mset%use_scaling) then   
-       call compute_scaling_factor(dble(CMB%omk),dble(CMB%omv),dble(CMB%w),a_scl)      
-    else
-      a_scl = 1
-    end if
-       
- 
-    do i=1, mset%num_mpk_kbands_use 
-      !Errors from using matter_power_minkh at lower end should be negligible
-          k_scaled(i)=max(matter_power_minkh,a_scl*mset%mpk_k(i))
-          mpk_lin(i)=MatterPowerAt(Theory,k_scaled(i))/a_scl**3
-    end do
-       
-       
-     do_marge = mset%Q_Marge
-     if (do_marge .and. mset%Q_flat) then
-         !Marginalize analytically with flat prior on b^2 and b^2*Q
-         !as recommended by Max Tegmark for SDSS
-           allocate(mpk_k2(mset%num_mpk_kbands_use))
-           allocate(mpk_WPth_k2(mset%num_mpk_points_use))
-  
-           mpk_Pth=mpk_lin/(1+mset%Ag*k_scaled)
-           mpk_k2=mpk_Pth*k_scaled**2
-           mpk_WPth = matmul(mset%mpk_W,mpk_Pth)
-           mpk_WPth_k2 = matmul(mset%mpk_W,mpk_k2)
- 
-           if (associated(mset%mpk_invcov)) then
-             covdat = matmul(mset%mpk_invcov,mset%mpk_P)
-             covth = matmul(mset%mpk_invcov,mpk_WPth)
-             covth_k2 = matmul(mset%mpk_invcov,mpk_WPth_k2)
-           else
-             w=1/(mset%mpk_sdev**2)
-             covdat = mset%mpk_P*w
-             covth = mpk_WPth*w
-             covth_k2 = mpk_WPth_k2*w     
-           end if
-            
-           Mat(1,1) = sum(covth*mpk_WPth)
-           Mat(2,2) = sum(covth_k2*mpk_WPth_k2)
-           Mat(1,2) = sum(covth*mpk_WPth_k2)
-           Mat(2,1) = Mat(1,2)
-           LnLike = log( Mat(1,1)*Mat(2,2)-Mat(1,2)**2)
-           call inv_mat22(Mat)
-           vec2(1) = sum(covdat*mpk_WPth)
-           vec2(2) = sum(covdat*mpk_WPth_k2)
-           LnLike = (sum(mset%mpk_P*covdat) - sum(vec2*matmul(Mat,vec2)) + LnLike ) /2
- 
-           deallocate(mpk_k2,mpk_WPth_k2)      
-     else
- 
-       if (mset%Q_sigma==0) do_marge = .false.
-       
-       do iQ=-nQ,nQ
-          Q = mset%Q_mid +iQ*mset%Q_sigma*dQ 
-          
-          if (mset%Q_marge) then
-             mpk_Pth=mpk_lin*(1+Q*k_scaled**2)/(1+mset%Ag*k_scaled)
-          else 
-             mpk_Pth = mpk_lin
-          end if
-          
-          mpk_WPth = matmul(mset%mpk_W,mpk_Pth)
-          
-          !with analytic marginalization over normalization nuisance (flat prior on b^2)
-          !See appendix F of cosmomc paper
-          
-          if (associated(mset%mpk_invcov)) then
-             covdat = matmul(mset%mpk_invcov,mset%mpk_P)
-             covth = matmul(mset%mpk_invcov,mpk_WPth)
-             normV = sum(mpk_WPth*covth)
-             chisq(iQ) = sum(mset%mpk_P*covdat)  - sum(mpk_WPth*covdat)**2/normV  + log(normV)  
-             
-          else
-       
-             !with analytic marginalization over normalization nuisance (flat prior on b^2)
-             w=1/(mset%mpk_sdev**2)
-             normV = sum(mpk_WPth*mpk_WPth*w)
-             tmp=sum(mpk_WPth*mset%mpk_P*w)/normV ! avoid subtracting one large number from another
-             chisq(iQ) = sum(mset%mpk_P*(mset%mpk_P - mpk_WPth*tmp)*w)  + log(normV)
-          end if
-          
-          if (do_marge) then
-             calweights(iQ) = exp(-(iQ*dQ)**2/2)
-          else 
-             LnLike = chisq(iQ)/2
-             exit
-          end if
-    
-       end do
-       
-       !without analytic marginalization
-       !! chisq = sum((mset%mpk_P(:) - mpk_WPth(:))**2*w) ! uncommented for debugging purposes
-       
-       if (do_marge) then
-          minchisq=minval(chisq)
-          LnLike = sum(exp(-(chisq-minchisq)/2)*calweights)/sum(calweights)
-          if (LnLike == 0) then
-             LnLike = LogZero
-          else
-             LnLike =  -log(LnLike) + minchisq/2
-          end if
-       end if
- 
-    end if !not analytic over Q
-       
-    if (Feedback>1) write(*,*) 'mpk chi-sq:', LnLike*2
-    
-    if (LnLike > 1e8) then
-       write(*,*) 'Chisq is huge, maybe there is a problem? chisq=',chisq
-    end if
-    
-    deallocate(mpk_Pth,mpk_lin)
-    deallocate(mpk_WPth,k_scaled,w)
-    
-  end function LSS_mpklike
- 
- 
-  function LSSLnLike(CMB, Theory)
-    Type (CMBParams) CMB
-    Type (CosmoTheory) Theory
-    real LSSLnLike
-    integer i
-    real tot(num_mpk_datasets)
- 
-   do i=1, num_mpk_datasets
-      if (mpkdatasets(i)%name == 'twodf') then
-         stop 'twodf no longer supported - use data/2df_2005.dataset'
-      else if (mpkdatasets(i)%name == 'lrg_2009') then
-         tot(i) = LSS_LRG_mpklike(Theory,mpkdatasets(i),CMB)
-      else
-       tot(i) = LSS_mpklike(Theory,mpkdatasets(i),CMB) !LV_06 added CMB here
-      end if
-   end do
-   LSSLnLike = SUM(tot) 
-   
-  end function LSSLnLike
- 
-  subroutine inv_mat22(M)
-     real M(2,2), Minv(2,2), det
- 
-     det = M(1,1)*M(2,2)-M(1,2)*M(2,1)
-     Minv(1,1)=M(2,2)
-     Minv(2,2) = M(1,1)
-     Minv(1,2) = - M(2,1)
-     Minv(2,1) = - M(1,2)
-     M = Minv/det
- 
-  end subroutine inv_mat22
- 
- !-----------------------------------------------------------------------------
- !LV added to include lrg DR4
- 
- subroutine compute_scaling_factor(Ok,Ol,w,a)
-   ! a = dV for z=0.35 relative to its value for flat Om=0.25 model.
-   ! This is the factor by which the P(k) measurement would shift 
-   ! sideways relative to what we got for this fiducial flat model.
-   ! * a = (a_angular**2 * a_radial)**(1/3)
-   ! * a_angular = comoving distance to z=0.35 in Mpc/h relative to its value for flat Om=0.25 model
-   !     dA = (c/H)*eta = (2997.92458 Mpc/h)*eta, so we care only about 
-   !     eta scaling, not h scaling.
-   !     For flat w=-1 models, a ~ (Om/0.25)**(-0.065)
-   !     For the LRG mean redshift z=0.35, the power law fit 
-   !    dA(z,Om= 0.3253 (Om/0.25)^{-0.065}c H_0^{-1} is quite good within 
-   !    our range of interest,
-   !     accurate to within about 0.1% for 0.2<Om<0.3.
-   ! * a_radial = 1/H(z) relative to its value for flat Om=0.25 model
-   implicit none
-   real(mpk_d) Or, Om, Ok, Ol, w, Ok0, Om0, Ol0, w0, z, eta, eta0, Hrelinv, Hrelinv0, tmp
-   real(mpk_d) a_radial, a_angular
-   real a
-   !Or= 0.0000415996*(T_0/2.726)**4 / h**2
-   Or= 0! Radiation density totally negligible at  z < 0.35
-   Om= 1-Ok-Ol-Or
-   !!!z  = 0.35  !!edited by Beth 21-11-08: change to zeff of Will's LRG sample.
-   z = zeffDR7
-   Hrelinv= 1/sqrt(Ol*(1+z)**(3*(1+w)) + Ok*(1+z)**2 + Om*(1+z)**3 + Or*(1+z)**4)
- !  write(*,*) Ok,Ol,w  
- call compute_z_eta(Or,Ok,Ol,w,z,eta)
-   tmp = sqrt(abs(Ok))
-   if (Ok.lt.-1.d-6) eta = sin(tmp*eta)/tmp
-   if (Ok.gt.1d-6)   eta = (exp(tmp*eta)-exp(-tmp*eta))/(2*tmp) ! sinh(tmp*eta)/tmp
-   Ok0= 0
-   Ol0= 0.75
-   w0= -1
-   Om0= 1-Ok0-Ol0-Or
-   call compute_z_eta(Or,Ok0,Ol0,w0,z,eta0)
-   Hrelinv0= 1/sqrt(Ol0*(1+z)**(3*(1+w0)) + Ok0*(1+z)**2 + Om0*(1+z)**3 + Or*(1+z)**4)
-   !a_angular = (Om/0.25)**(-0.065) * (-w*Otot)**0.14 ! Approximation based on Taylor expansion
-   a_angular = eta/eta0
-   a_radial= Hrelinv/Hrelinv0
-   a=  (a_angular**2 * a_radial)**(1/3.d0)
-   !write(*,'(9f10.5)') Ok,Ol,w,a,a_radial,a_angular,(Om/0.25)**(-0.065) * (-w*(1-Ok))**0.14
-   !write(*,'(9f10.5)') Ok,Ol,w,a,a_radial**(2/3.d0),a_angular**(4/3.d0),((Om/0.25)**(-0.065) * (-w*(1-Ok))**0.14)**(4/3.d0)
-   !!! BR09 -- in previous version, scale factor is applied in the wrong direction!  So take a = 1/a to fix it.
-   a = 1.0/a
- end subroutine compute_scaling_factor
- 
- subroutine eta_demo
-   implicit none
-   real(mpk_d) Or, Ok, Ol, w, h, z, eta
-   h  = 0.7
-   Ok = 0
-   Ol = 0.7
-   Or = 0.0000416/h**2 
-   w  = -1
-   z  = 1090
-   call compute_z_eta(Or,Ok,Ol,w,z,eta)
- !  print *,'eta.............',eta
- !  print *,'dlss in Gpc.....',(2.99792458/h)*eta
- end subroutine eta_demo
- 
- !INTERFACE
- logical function nobigbang2(Ok,Ol,w)
-   ! Test if we're in the forbidden zone where the integrand blows up
-   ! (where's H^2 < 0 for some a between 0 and 1).
-   ! The function f(a) = Omega_m + Omega_k*a + Omega_l*a**(-3*w)
-   ! can have at most one local minimum, at (Ok/(3*w*Ol))**(-1/(1+3*w)), 
-   ! so simply check if f(a)<0 there or at the endpoints a=0, a=1.
-   ! g(0) = Omega_m - Omega_l*a**(-3*w) < 0 if w > 0 & Omega_k > 1
-   !                                     or if w = 0 & Omega_l < 1       
-   ! g(1) = Omega_m + Omega_k + Omega_l = 1 > 0
-   implicit none
-   real(mpk_d) Ok, Ol, w, Om, tmp, a, epsilon
-   integer failure
-   failure = 0
-   epsilon = 0
-   !epsilon = 0.04  ! Numerical integration fails even before H^2 goes negative.
-   Om = 1.d0 - Ok - Ol
-   if (w*Ol.ne.0) then
-      tmp = Ok/(3*w*Ol)
-      if ((tmp.gt.0).and.(1+3*w.ne.0)) then ! f'(0)=0 for some a>0
-         a = tmp**(-1/(1+3*w))
-         if (a.lt.1) then
-            if (Om + Ok*a + Ol*a**(-3*w).lt.epsilon) failure = 1
-         end if
-      end if
-   end if
-   if ((w.eq.0).and.(Ok.gt.1)) failure = 2
-   if ((w.gt.0).and.(Ol.lt.0)) failure = 3
-   nobigbang2 = (failure.gt.0)
-   if (failure.gt.0) print *,'Big Bang failure mode ',failure
-   return
- end function nobigbang2
- !END INTERFACE
- 
- real(mpk_d) function eta_integrand(a)
-   implicit none
-   real(mpk_d) Or, Ok, Ox, w
-   common/eta/Or, Ok, Ox, w
-   real(mpk_d) a, Om
-   ! eta = int (H0/H)dz = int (H0/H)(1+z)dln(1+z) = int (H0/H)/a dlna = int (H0/H)/a^2 da = 
-   ! Integrand = (H0/H)/a^2
-   ! (H/H0)**2 = Ox*a**(-3*(1+w)) + Ok/a**2 + Om/a**3 + Or/a**4 
-   if (a.eq.0.d0) then 
-      eta_integrand = 0.d0
-   else
-      Om = 1.d0 - Or - Ok - Ox
-      eta_integrand = 1.d0/sqrt(Ox*a**(1-3*w) + Ok*a**2 + Om*a + Or)
-   end if
-   return
- end function eta_integrand
- 
- subroutine eta_z_integral(Omega_r,Omega_k,Omega_x,w_eos,z,eta)
-   ! Computes eta as a function
-   ! of the curvature Omega_k, the dark energy density Omega_x
-   ! and its equation of state w.
-   implicit none
-   real(mpk_d) Or, Ok, Ox, w
-   common/eta/Or, Ok, Ox, w
-   real(mpk_d) Omega_r, Omega_k,Omega_x,w_eos, z, eta, epsabs, epsrel, amin, amax!, eta_integrand
-   Or = Omega_r
-   Ok = Omega_k
-   Ox = Omega_x
-   w  = w_eos
-   epsabs  = 0
-   epsrel  = 1.d-10
-   amin= 1/(1+z)
-   amax= 1
-   call qromb2(eta_integrand,amin,amax,epsabs,epsrel,eta)
-   return
- end subroutine eta_z_integral
- 
- subroutine compute_z_eta(Or,Ok,Ox,w,z,eta)
-   ! Computes the conformal distance eta(z)
-   implicit none
-   real(mpk_d) Or, Ok, Ox, w, z, eta
- !  logical nobigbang2
-   if (nobigbang2(Ok,Ox,w)) then
-      print *,'No big bang, so eta undefined if z>zmax.'
-      eta = 99 
-   else
-      call eta_z_integral(Or,Ok,Ox,w,z,eta) 
-      ! print *,'Or, Ok, Ox, w, z, H_0 t_0...',Or, Ok, Ox, w, eta
-   end if
-   return
- end subroutine compute_z_eta
- 
- 
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- !!!! num rec routines
- !!!!!!!!!!!!!!!!!!!!!!!!!!
- SUBROUTINE qromb2(func,a,b,epsabs,epsrel,ss)
- ! The numerical recipes routine, but modified so that is decides
- ! it's done when either the relative OR the absolute accuracy has been attained.
- ! The old version used relative errors only, so it always failed when
- ! when the integrand was near zero.
- ! epsabs = epsrel = 1e-6 are canonical choices.
-   INTEGER JMAX,JMAXP,K,KM
-   real(mpk_d) a,b,func,ss,epsabs,epsrel
-   EXTERNAL func
-   PARAMETER (JMAX=20, JMAXP=JMAX+1, K=5, KM=K-1)
-                                 !    USES polint,trapzd
-   INTEGER j
-   real(mpk_d) dss,h(JMAXP),s(JMAXP)
-   h(1)=1.d0
-   do j=1,JMAX
-      call trapzd(func,a,b,s(j),j)
-      if (j.ge.K) then
-         call polint(h(j-KM),s(j-KM),K,0.d0,ss,dss)
-         if (abs(dss).le.epsrel*abs(ss)) return
-         if (abs(dss).le.epsabs) return
-      endif
-      s(j+1)=s(j)
-      h(j+1)=0.25d0*h(j)
-   ENDDO
-   print *,'Too many steps in qromb'
-       
-   RETURN 
- END SUBROUTINE qromb2
-   
- SUBROUTINE polint(xa,ya,n,x,y,dy) ! From Numerical Recipes
-   INTEGER n,NMAX
-   real(mpk_d) dy,x,y,xa(n),ya(n)
-   PARAMETER (NMAX=10)
-   INTEGER i,m,ns
-   real(mpk_d) den,dif,dift,ho,hp,w,c(NMAX),d(NMAX)
-   ns=1
-   dif=abs(x-xa(1))
-   do  i=1,n
-      dift=abs(x-xa(i))
-      if (dift.lt.dif) then
-         ns=i
-         dif=dift
-      endif
-      c(i)=ya(i)
-      d(i)=ya(i)
-   enddo
-   y=ya(ns)
-   ns=ns-1
-   do  m=1,n-1
-      do  i=1,n-m
-         ho=xa(i)-x
-         hp=xa(i+m)-x
-         w=c(i+1)-d(i)
-         den=ho-hp
-         if(den.eq.0.) then
-            print*, 'failure in polint'
-            stop
-         endif
-         den=w/den
-         d(i)=hp*den
-         c(i)=ho*den
-      enddo
-      if (2*ns.lt.n-m)then
-         dy=c(ns+1)
-      else
-         dy=d(ns)
-         ns=ns-1
-      endif
-      y=y+dy
-   enddo
-   return
- END SUBROUTINE polint
-       
- SUBROUTINE trapzd(func,a,b,s,n) ! From Numerical Recipes
-   INTEGER n
-   real(mpk_d) a,b,s,func
-   EXTERNAL func
-   INTEGER it,j
-   real(mpk_d) del,sum,tnm,x
-   if (n.eq.1) then
-      s=0.5*(b-a)*(func(a)+func(b))
-   else
-      it=2**(n-2)
-      tnm=it
-      del=(b-a)/tnm
-      x=a+0.5*del
-      sum=0.
-      do  j=1,it
-         sum=sum+func(x)
-         x=x+del
-      enddo
-      s=0.5*(s+(b-a)*sum/tnm)
-   endif
-   return
- END SUBROUTINE trapzd
-             
- 
- 
- !! added by Beth Reid for LRG P(k) analysis
- 
-   function a2maxpos(a1val) result(a2max)
-     real(dl), intent(in) :: a1val
-     real(dl) a2max
-     a2max = -1.0d0
-     if (a1val <= min(s1/k1,s2/k2)) then
-       a2max = min(s1/k1**2 - a1val/k1, s2/k2**2 - a1val/k2)
-     end if
-   end function a2maxpos
- 
-   function a2min1pos(a1val) result(a2min1)
-     real(dl), intent(in) :: a1val
-     real(dl) a2min1
-     a2min1 = 0.0d0
-     if(a1val <= 0.0d0) then
-       a2min1 = max(-s1/k1**2 - a1val/k1, -s2/k2**2 - a1val/k2, 0.0d0)
-     end if
-   end function a2min1pos
- 
-   function a2min2pos(a1val) result(a2min2)
-     real(dl), intent(in) :: a1val
-     real(dl) a2min2
-     a2min2 = 0.0d0
-     if(abs(a1val) >= 2.0d0*s1/k1 .and. a1val <= 0.0d0)  then
-       a2min2 = a1val**2/s1*0.25d0
-     end if
-   end function a2min2pos
- 
-   function a2min3pos(a1val) result(a2min3)
-     real(dl), intent(in) :: a1val
-     real(dl) a2min3
-     a2min3 = 0.0d0
-     if(abs(a1val) >= 2.0d0*s2/k2 .and. a1val <= 0.0d0)  then
-       a2min3 = a1val**2/s2*0.25d0
-     end if
-   end function a2min3pos
- 
-   function a2minfinalpos(a1val) result(a2minpos)
-     real(dl), intent(in) :: a1val
-     real(dl) a2minpos
-     a2minpos = max(a2min1pos(a1val),a2min2pos(a1val),a2min3pos(a1val))
-   end function a2minfinalpos
- 
-   function a2minneg(a1val) result(a2min)
-     real(dl), intent(in) :: a1val
-     real(dl) a2min
-     if (a1val >= max(-s1/k1,-s2/k2)) then
-       a2min = max(-s1/k1**2 - a1val/k1, -s2/k2**2 - a1val/k2)
-     else
-       a2min = 1.0d0
-     end if
-   end function a2minneg
- 
-   function a2max1neg(a1val) result(a2max1)
-     real(dl), intent(in) :: a1val
-     real(dl) a2max1
-     if(a1val >= 0.0d0) then
-       a2max1 = min(s1/k1**2 - a1val/k1, s2/k2**2 - a1val/k2, 0.0d0)
-     else
-       a2max1 = 0.0d0
-     end if
-   end function a2max1neg
- 
-   function a2max2neg(a1val) result(a2max2)
-     real(dl), intent(in) :: a1val
-     real(dl) a2max2
-     a2max2 = 0.0d0
-     if(abs(a1val) >= 2.0d0*s1/k1 .and. a1val >= 0.0d0)  then
-       a2max2 = -a1val**2/s1*0.25d0
-     end if
-   end function a2max2neg
- 
-   function a2max3neg(a1val) result(a2max3)
-     real(dl), intent(in) :: a1val
-     real(dl) a2max3
-     a2max3 = 0.0d0
-     if(abs(a1val) >= 2.0d0*s2/k2 .and. a1val >= 0.0d0)  then
-       a2max3 = -a1val**2/s2*0.25d0
-     end if
-   end function a2max3neg
- 
-   function a2maxfinalneg(a1val) result(a2maxneg)
-     real(dl), intent(in) :: a1val
-     real(dl) a2maxneg
-     a2maxneg = min(a2max1neg(a1val),a2max2neg(a1val),a2max3neg(a1val))
-   end function a2maxfinalneg
- 
- 
- function testa1a2(a1val, a2val) result(testresult)
-     real(dl), intent(in) :: a1val,a2val
-     logical :: testresult
- 
-     real(dl) :: kext, diffval
-     testresult = .true.
- 
-     ! check if there's an extremum; either a1val or a2val has to be negative, not both
-     kext = -a1val/2.0d0/a2val
-     diffval = abs(a1val*kext + a2val*kext**2)
-     if(kext > 0.0d0 .and. kext <= k1 .and. diffval > s1) testresult = .false.
-     if(kext > 0.0d0 .and. kext <= k2 .and. diffval > s2) testresult = .false.
- 
-     if (abs(a1val*k1 + a2val*k1**2) > s1) testresult = .false.
-     if (abs(a1val*k2 + a2val*k2**2) > s2) testresult = .false.
- 
- end function testa1a2
- 
- !! copying LSS_mpklike above.
- !! points_use is how many points to use in the likelihood calculation; 
- !!kbands_use is how many points you need to have a theory for in order to convolve the theory with the window function.
- 
- 
- ! this subroutine fills in the a1 and a2 values only once.
-  subroutine LSS_LRG_mpklike_init()
-    real(dl) :: a1val, a2val
-    real(dl) :: da1, da2  ! spacing of numerical integral over nuisance params.
-    integer :: countcheck = 0
-    integer :: i, j
-    !! this is just for checking the 'theory' curves for fiducial model
-    real :: fidLnLike
-    type(CosmoTheory) :: temptheory
-    type(CMBparams) :: tempCMB
- 
-    da1 = a1maxval/(nptsa1/2)
-    da2 = a2maxpos(-a1maxval)/(nptsa2/2)
-    do i = -nptsa1/2, nptsa1/2
-       do j = -nptsa2/2, nptsa2/2
-          a1val = da1*i
-          a2val = da2*j
- 
-          if ((a2val >= 0.0d0 .and. a2val <= a2maxpos(a1val) .and. a2val >= a2minfinalpos(a1val)) .or. &
-      & (a2val <= 0.0d0 .and. a2val <= a2maxfinalneg(a1val) .and. a2val >= a2minneg(a1val))) then
-             if(testa1a2(a1val,a2val) .eqv. .false.)  then
-                print *,'Failed a1, a2: ',a1val,a2val
-                if (a2val >= 0.0d0) print *,'pos', a2maxpos(a1val), a2minfinalpos(a1val)
-                if (a2val <= 0.0d0) print *,'neg', a2maxfinalneg(a1val), a2minneg(a1val)
-                stop
-             end if
-             countcheck = countcheck + 1
-             if(countcheck > nptstot) then
-                print *, 'countcheck > nptstot failure.'
-                stop
-             end if
-             a1list(countcheck) = a1val
-             a2list(countcheck) = a2val
-             !print *, countcheck, a1list(countcheck), a2list(countcheck)
-          end if
-       end do
-    end do
-    if(countcheck .ne. nptstot) then
-      print *, 'countcheck issue', countcheck, nptstot
-      stop
-    end if
-   call LRGinfo_init()
-  end subroutine LSS_LRG_mpklike_init
- 
- 
-  function LSS_LRG_mpklike(Theory,mset,CMB) result(LnLike)  ! LV_06 added CMB here
-    Type (mpkdataset) :: mset
-    Type (CosmoTheory) Theory
-    Type(CMBparams) CMB                  !LV_06 added for LRGDR4
-    real LnLike
-    integer :: i
-    real, dimension(:), allocatable :: mpk_raw, mpk_Pth, mpk_Pth_k, mpk_Pth_k2, k_scaled
-    real, dimension(:), allocatable :: mpk_WPth, mpk_WPth_k, mpk_WPth_k2
-    real :: covdat(mset%num_mpk_points_use), covth(mset%num_mpk_points_use), &
-           & covth_k(mset%num_mpk_points_use), covth_k2(mset%num_mpk_points_use), & 
-           & covth_zerowin(mset%num_mpk_points_use)
- 
-    real, dimension(nptstot) :: chisq, chisqmarg  !! minus log likelihood list
-    real :: minchisq,maxchisq,deltaL
- 
-    real(dl) :: a1val, a2val, zerowinsub
-    real :: sumDD, sumDT, sumDT_k, sumDT_k2, sumTT,& 
-      &  sumTT_k, sumTT_k2, sumTT_k_k, sumTT_k_k2, sumTT_k2_k2, &
-      &  sumDT_tot, sumTT_tot, &
-      &  sumDT_zerowin, sumTT_zerowin, sumTT_k_zerowin, sumTT_k2_zerowin, sumTT_zerowin_zerowin
- 
-    real :: sumzerow_Pth, sumzerow_Pth_k, sumzerow_Pth_k2
- 
-    real :: a_scl      !LV_06 added for LRGDR4
- 
-    real(wp) :: temp1,temp2,temp3
-    real :: temp4
- 
-    !! added for no marg
-    integer :: myminchisqindx
-    real :: currminchisq, currminchisqmarg, minchisqtheoryamp, chisqnonuis
-    real :: minchisqtheoryampnonuis, minchisqtheoryampminnuis
-    real(dl), dimension(2) :: myerfval
- 
-    call fill_LRGTheory(Theory,matter_power_minkh,matter_power_dlnkh)
-    allocate(mpk_raw(mset%num_mpk_kbands_use) ,mpk_Pth(mset%num_mpk_kbands_use))
-    allocate(mpk_Pth_k(mset%num_mpk_kbands_use) ,mpk_Pth_k2(mset%num_mpk_kbands_use))
-    allocate(mpk_WPth(mset%num_mpk_points_use),mpk_WPth_k(mset%num_mpk_points_use),mpk_WPth_k2(mset%num_mpk_points_use))
-    allocate(k_scaled(mset%num_mpk_kbands_use))!LV_06 added for LRGDR4
- 
-    chisq = 0
- 
-    if (.not. mset%use_set) then
-       LnLike = 0
-       return
-    end if
- 
-    IF(mset%use_scaling) then
-       call compute_scaling_factor(dble(CMB%omk),dble(CMB%omv),dble(CMB%w),a_scl)
-       !! this step now applied in compute_scaling_factor
-       !! this fixes the bug most easily !!
-       !!a_scl = 1.0d0/a_scl
-    else
-      a_scl = 1
-      stop 'use_scaling should be set to true for the LRGs!'
-    end if
- 
-    do i=1, mset%num_mpk_kbands_use
-          k_scaled(i)=max(matter_power_minkh,a_scl*mset%mpk_k(i))
-          mpk_raw(i)=LRGPowerAt(Theory,k_scaled(i))/a_scl**3
-    end do
- 
-    mpk_Pth = mpk_raw
- 
-    mpk_Pth_k = mpk_Pth*k_scaled
-    mpk_Pth_k2 = mpk_Pth*k_scaled**2
-    mpk_WPth = matmul(mset%mpk_W,mpk_Pth)
-    mpk_WPth_k = matmul(mset%mpk_W,mpk_Pth_k)
-    mpk_WPth_k2 = matmul(mset%mpk_W,mpk_Pth_k2)
- 
-    sumzerow_Pth = sum(mset%mpk_zerowindowfxn*mpk_Pth)/mset%mpk_zerowindowfxnsubdatnorm
-    sumzerow_Pth_k = sum(mset%mpk_zerowindowfxn*mpk_Pth_k)/mset%mpk_zerowindowfxnsubdatnorm
-    sumzerow_Pth_k2 = sum(mset%mpk_zerowindowfxn*mpk_Pth_k2)/mset%mpk_zerowindowfxnsubdatnorm
- 
- 
-    covdat = matmul(mset%mpk_invcov,mset%mpk_P)
-    covth = matmul(mset%mpk_invcov,mpk_WPth)
-    covth_k = matmul(mset%mpk_invcov,mpk_WPth_k)
-    covth_k2 = matmul(mset%mpk_invcov,mpk_WPth_k2)
-    covth_zerowin = matmul(mset%mpk_invcov,mset%mpk_zerowindowfxnsubtractdat)
- 
-    sumDD = sum(mset%mpk_P*covdat)
-    sumDT = sum(mset%mpk_P*covth)
-    sumDT_k = sum(mset%mpk_P*covth_k)
-    sumDT_k2 = sum(mset%mpk_P*covth_k2)
-    sumDT_zerowin = sum(mset%mpk_P*covth_zerowin)
- 
-    sumTT = sum(mpk_WPth*covth)
-    sumTT_k = sum(mpk_WPth*covth_k)
-    sumTT_k2 = sum(mpk_WPth*covth_k2)
-    sumTT_k_k = sum(mpk_WPth_k*covth_k)
-    sumTT_k_k2 = sum(mpk_WPth_k*covth_k2)
-    sumTT_k2_k2 = sum(mpk_WPth_k2*covth_k2)
-    sumTT_zerowin = sum(mpk_WPth*covth_zerowin)
-    sumTT_k_zerowin = sum(mpk_WPth_k*covth_zerowin)
-    sumTT_k2_zerowin = sum(mpk_WPth_k2*covth_zerowin)
-    sumTT_zerowin_zerowin = sum(mset%mpk_zerowindowfxnsubtractdat*covth_zerowin)
- 
-    currminchisq = 1000.0d0
-    do i=1,nptstot
-      a1val = a1list(i)
-      a2val = a2list(i)
-      zerowinsub = -(sumzerow_Pth + a1val*sumzerow_Pth_k + a2val*sumzerow_Pth_k2)
- 
-      sumDT_tot = sumDT + a1val*sumDT_k + a2val*sumDT_k2 + zerowinsub*sumDT_zerowin
-      sumTT_tot = sumTT + a1val**2.0d0*sumTT_k_k + a2val**2.0d0*sumTT_k2_k2 + & 
-                  & zerowinsub**2.0d0*sumTT_zerowin_zerowin &
-        & + 2.0d0*a1val*sumTT_k + 2.0d0*a2val*sumTT_k2 + 2.0d0*a1val*a2val*sumTT_k_k2 &
-        & + 2.0d0*zerowinsub*sumTT_zerowin + 2.0d0*zerowinsub*a1val*sumTT_k_zerowin &
-        & + 2.0d0*zerowinsub*a2val*sumTT_k2_zerowin
-      minchisqtheoryamp = sumDT_tot/sumTT_tot
-      chisq(i) = sumDD - 2.0d0*minchisqtheoryamp*sumDT_tot + minchisqtheoryamp**2.0d0*sumTT_tot
- #ifdef DR71RG
-      myerfval(1) = sumDT_tot/2.0d0/sqrt(sumTT_tot)
-      call geterf(myerfval)
-      chisqmarg(i) = sumDD - sumDT_tot**2.0d0/sumTT_tot & 
-          & + log(sumTT_tot) &
-          & - 2.0*log(1.0d0 + myerfval(2))
- #else
-      !!leave out the erf term, just to get it to compile.  This should never run.
-      chisqmarg(i) = sumDD - sumDT_tot**2.0d0/sumTT_tot & 
-          & + log(sumTT_tot)
-      if(0 .eq. 0) stop 'Logic problem.  Shouldnt be here.'
- #endif
- !this should always be here, but we're using gsl to call erf, so this function is only available if gsl is installed.
-      if(i == 1 .or. chisq(i) < currminchisq) then
-         myminchisqindx = i
-         currminchisq = chisq(i)
-         currminchisqmarg = chisqmarg(i)
-         minchisqtheoryampminnuis = minchisqtheoryamp
-      end if
-      if(i == int(nptstot/2)+1) then
-         chisqnonuis = chisq(i)
-         minchisqtheoryampnonuis = minchisqtheoryamp
-         if(abs(a1val) > 0.001 .or. abs(a2val) > 0.001) then
-            print *, 'ahhhh! violation!!', a1val, a2val
-         end if
-      end if
- 
-    end do
- 
- ! numerically marginalize over a1,a2 now using values stored in chisq
-    minchisq = minval(chisqmarg)
-    maxchisq = maxval(chisqmarg)
- 
-    LnLike = sum(exp(-(chisqmarg-minchisq)/2.0d0)/(nptstot*1.0d0))
-    if(LnLike == 0) then
-      LnLike = LogZero
-    else
-      LnLike = -log(LnLike) + minchisq/2.0d0
-    end if
-  deltaL = (maxchisq - minchisq)*0.5
-  if(Feedback > 1) print *,'LRG P(k) LnLike = ',LnLike
- 
-    deallocate(mpk_raw, mpk_Pth)
-    deallocate(mpk_Pth_k, mpk_Pth_k2)
-    deallocate(mpk_WPth, mpk_WPth_k, mpk_WPth_k2)
-    deallocate(k_scaled) 
- 
-  end function LSS_LRG_mpklike
- 
- end module
--- 0 ----
diff -r -c -b -N cosmomc/source/mpk.F90 cosmomc_wiggles/source/mpk.F90
*** cosmomc/source/mpk.F90	1970-01-01 01:00:00.000000000 +0100
--- cosmomc_wiggles/source/mpk.F90	2009-10-28 16:49:31.964217860 +0100
***************
*** 0 ****
--- 1,1148 ----
+ !Module storing observed matter power spectrum datasets, their points and window functions
+ !and routines for computing the likelihood
+ 
+ !This code is based on that in cmbdata.f90 
+ !and on Sam Leach's incorporation of Max Tegmark's SDSS code
+ !
+ !Originally SLB Sept 2004
+ !AL April 2006: added covariance matrix support (following 2df 2005)
+ !LV_06 : incorporation of LRG DR4 from Tegmark et al . astroph/0608632 
+ !AL: modified LV SDSS to do Q and b^2 or b^2*Q marge internally as for 2df
+ !BR09: added model LRG power spectrum.
+ !AL Oct 20: switch to Ini_Read_xxx_File; fortran compatibility changes
+ 
+ 
+ module LRGinfo
+ use settings
+ use cmbtypes
+ use Precision
+ use lrggettheory
+ 
+ !use CMB_Cls
+ 
+ implicit none
+ 
+ !! these are the LRG redshift subsample weights.
+ real(dl), parameter :: w0 = 0.0d0, wNEAR = 0.395d0, wMID = 0.355d0, wFAR = 0.250d0
+ 
+ !in CAMB: 4=now (z=0), 3=NEAR, 2=MID, 1=FAR; opposite order in matter_power
+ !! now generalized indices iz0lrg, izNEARlrg, izMIDlrg, izFARlrg
+ real(dl), dimension(4) :: zeval, zweight, sigma2BAOfid, sigma2BAO
+ 
+ real(dl) om_val, ol_val, ok_check, wval  ! passed in from CMBparams CMB
+ 
+ ! power spectra evaluated at fiducial cosmological theory (WMAP5 recommended values)
+ real, allocatable :: ratio_power_nw_nl_fid(:,:)
+ !real,dimension(num_matter_power,matter_power_lnzsteps) :: ratio_power_nw_nl_fid
+ !make allocatable to avoid compile-time range errors when matter_power_lnzsteps<4
+ 
+ contains
+ 
+ subroutine LRGinfo_init()
+   integer :: iopb, i, ios
+   real(dl) :: omegakdummy,omegavdummy,wdummy,getabstransferscalefiddummy
+   real(dl) :: kval, plin, psmooth, rationwhalofit
+ 
+   !!BR09 only needed for LRGs, so only 4 redshifts no matter what matter_power_lnzsteps is
+   allocate(ratio_power_nw_nl_fid(num_matter_power,4))
+ 
+   sigma2BAOfid(1) = 1.0e-5  !! don't do any smearing at z=0; this won't be used anyway.
+   sigma2BAOfid(2) = sigma2BAONEAR
+   sigma2BAOfid(3) = sigma2BAOMID
+   sigma2BAOfid(4) = sigma2BAOFAR
+ 
+   zeval(1) = z0
+   zeval(2) = zNEAR
+   zeval(3) = zMID
+   zeval(4) = zFAR
+ 
+   zweight(1) = w0
+   zweight(2) = wNEAR
+   zweight(3) = wMID
+   zweight(4) = wFAR
+ 
+   !! first read in everything needed from the CAMB output files.
+   iopb = 0 !! check later if there was an error
+ 
+   open(unit=tmp_file_unit,file=trim(DataDir)//'lrgdr7fiducialmodel_matterpowerzNEAR.dat',form='formatted',err=500, iostat=ios)
+   read (tmp_file_unit,*,iostat=iopb) getabstransferscalefiddummy, omegakdummy,omegavdummy,wdummy
+   do i = 1, num_matter_power
+     read (tmp_file_unit,*,iostat=iopb) kval, plin, psmooth, rationwhalofit
+     ratio_power_nw_nl_fid(i,2) = rationwhalofit
+   end do
+   close(tmp_file_unit)
+ 
+   open(unit=tmp_file_unit,file=trim(DataDir)//'lrgdr7fiducialmodel_matterpowerzMID.dat',form='formatted',err=500, iostat=ios)
+   read (tmp_file_unit,*,iostat=iopb) getabstransferscalefiddummy,omegakdummy,omegavdummy,wdummy
+   do i = 1, num_matter_power
+     read (tmp_file_unit,*,iostat=iopb) kval, plin, psmooth, rationwhalofit
+     ratio_power_nw_nl_fid(i,3) = rationwhalofit
+   end do
+   close(tmp_file_unit)
+ 
+   open(unit=tmp_file_unit,file=trim(DataDir)//'lrgdr7fiducialmodel_matterpowerzFAR.dat',form='formatted',err=500,iostat=ios)
+   read (tmp_file_unit,*,iostat=iopb) getabstransferscalefiddummy,omegakdummy,omegavdummy,wdummy
+   do i = 1, num_matter_power
+     read (tmp_file_unit,*,iostat=iopb) kval, plin, psmooth, rationwhalofit
+     ratio_power_nw_nl_fid(i,4) = rationwhalofit
+   end do
+   close(tmp_file_unit)
+ 
+ 500 if(ios .ne. 0) stop 'Unable to open file'
+   if(iopb .ne. 0) stop 'Error reading model or fiducial theory files.'
+ end subroutine LRGinfo_init
+ 
+ ! HARD CODING OF POLYNOMIAL FITS TO NEAR, MID, FAR SUBSAMPLES.
+ subroutine LRGtoICsmooth(k,fidpolys)
+   real(dl), intent(in) :: k
+   real(dl) :: fidNEAR, fidMID, fidFAR
+   real(dl), dimension(2:4), intent(out) :: fidpolys
+ 
+   if(k < 0.194055d0) then !!this is where the two polynomials are equal
+     fidNEAR = (1.0d0 - 0.680886d0*k + 6.48151d0*k**2)
+   else
+     fidNEAR = (1.0d0 - 2.13627d0*k + 21.0537d0*k**2 - 50.1167d0*k**3 + 36.8155d0*k**4)*1.04482d0
+   end if
+ 
+   if(k < 0.19431) then
+     fidMID = (1.0d0 - 0.530799d0*k + 6.31822d0*k**2)
+   else
+     fidMID = (1.0d0 - 1.97873d0*k + 20.8551d0*k**2 - 50.0376d0*k**3 + 36.4056d0*k**4)*1.04384
+   end if
+ 
+   if(k < 0.19148) then
+     fidFAR = (1.0d0 - 0.475028d0*k + 6.69004d0*k**2)
+   else
+     fidFAR = (1.0d0 - 1.84891d0*k + 21.3479d0*k**2 - 52.4846d0*k**3 + 38.9541d0*k**4)*1.03753
+   end if
+   fidpolys(2) = fidNEAR
+   fidpolys(3) = fidMID
+   fidpolys(4) = fidFAR
+ end subroutine LRGtoICsmooth
+ 
+ subroutine fill_LRGTheory(Theory, minkh, dlnkh)
+   Type(CosmoTheory) Theory
+   real, intent(in) :: minkh, dlnkh
+   real(dl) :: logmink, xi, kval, expval, psmear, nlrat
+   real(dl), dimension(2:4) :: fidpolys, holdval
+ 
+   integer :: iz, ik, matterpowerindx
+ 
+   do iz = 1, 4
+     sigma2BAO(iz) = sigma2BAOfid(iz)
+   end do
+ 
+   logmink = log(minkh)
+   do ik=1,num_matter_power
+    xi = logmink + dlnkh*(ik-1)
+    kval = exp(xi)
+    Theory%finalLRGtheoryPk(ik) = 0.
+    do iz = 2,4
+     if(iz == 2) matterpowerindx = izNEARlrg
+     if(iz == 3) matterpowerindx = izMIDlrg
+     if(iz == 4) matterpowerindx = izFARlrg
+     expval = exp(-kval**2*sigma2BAO(iz)*0.5)
+     psmear = (Theory%matter_power(ik,matterpowerindx))*expval + (Theory%mpk_nw(ik,matterpowerindx))*(1.0-expval)
+     psmear = psmear*powerscaletoz0(iz)
+     nlrat = (Theory%mpkrat_nw_nl(ik,matterpowerindx))/(ratio_power_nw_nl_fid(ik,matterpowerindx))
+     call LRGtoICsmooth(kval,fidpolys)
+     holdval(iz) = zweight(iz)*psmear*nlrat*fidpolys(iz)
+     Theory%finalLRGtheoryPk(ik) = Theory%finalLRGtheoryPk(ik) + holdval(iz)
+    end do
+ 
+   end do
+ 
+ end subroutine fill_LRGTheory
+ 
+ end module
+ 
+ module mpk
+ use precision
+ use settings
+ use cmbtypes
+ use LRGinfo
+ implicit none
+ 
+  Type mpkdataset
+     logical :: use_set
+     integer :: num_mpk_points_use ! total number of points used (ie. max-min+1)
+     integer :: num_mpk_kbands_use ! total number of kbands used (ie. max-min+1)
+     character(LEN=20) :: name
+     real, pointer, dimension(:,:) :: N_inv
+     real, pointer, dimension(:,:) :: mpk_W, mpk_invcov
+     real, pointer, dimension(:) :: mpk_P, mpk_sdev, mpk_k
+     real, pointer, dimension(:) :: mpk_zerowindowfxn
+     real, pointer, dimension(:) :: mpk_zerowindowfxnsubtractdat
+     real :: mpk_zerowindowfxnsubdatnorm !!the 0th entry in windowfxnsubtract file
+     logical :: use_scaling !as SDSS_lrgDR3
+    !for Q and A see e.g. astro-ph/0501174, astro-ph/0604335
+     logical :: Q_marge, Q_flat
+     real :: Q_mid, Q_sigma, Ag
+    end Type mpkdataset
+ 
+  integer :: num_mpk_datasets = 0
+  Type(mpkdataset) mpkdatasets(10)
+ 
+  !Note all units are in k/h here
+  
+   integer, parameter :: mpk_d = kind(1.d0)
+  
+   logical :: use_mpk = .false.
+   
+   ! constants describing the allowed a1,a2 regions.
+   ! must check the functions below before changing these, because the shape of the space may change!
+ 
+   integer, parameter :: wp = selected_real_kind(11,99)
+ 
+   !!these are the 'nonconservative' nuisance parameter bounds
+   !!real(dl), parameter :: k1 = 0.1d0, k2 = 0.2d0, s1 = 0.02d0, s2 = 0.05d0, a1maxval = 0.5741d0
+   real(dl), parameter :: k1 = 0.1d0, k2 = 0.2d0, s1 = 0.04d0, s2 = 0.10d0, a1maxval = 1.1482d0
+   integer, parameter :: nptsa1 = 41, nptsa2 = 41, nptstot = 325  
+   !! but total number of points to evaluate is much smaller than 41**2 because lots of the space 
+   !is not allowed by the s1,s2 constraints.
+  
+    ! only want to compute these once. 
+    real(dl), dimension(nptstot) :: a1list, a2list
+ 
+ contains 
+ 
+   subroutine mpk_SetTransferRedshifts(redshifts)
+    real, intent(inout) :: redshifts(*)
+    !input is default log z spacing; can change here; check for consistency with other (e.g. lya)
+          
+    !Note internal ordering in CAMB is the opposite to that used in cosmomc transfer arrays (as here)
+    !first index here must be redshift zero
+    
+       if(use_dr7lrg .and. matter_power_lnzsteps < 4) &
+        call MpiStop('For LRGs matter_power_lnzsteps should be set to at least 4 (hardcoded in cmbtypes)')
+ 
+        if (matter_power_lnzsteps==1 .or. .not. use_dr7lrg) return
+ 
+        !! assigning indices to LRG NEAR, MID, FAR.  If you want to reorder redshifts, just change here.
+        iz0lrg = 1  !! we use the z=0 output to normalize things; this is already assumed index 1 elsewhere 
+                     !(like in calculation of sigma8).
+        izNEARlrg = 2
+        izMIDlrg = 3
+        izFARlrg = 4
+        redshifts(izNEARlrg) = zNEAR
+        redshifts(izMIDlrg) = zMID
+        redshifts(izFARlrg) = zFAR  
+        if(iz0lrg /= 1) then
+           redshifts(iz0lrg) = 0.0d0
+        else
+           if(redshifts(1) > 0.001) call MpiStop('redshifts(1) should be at z=0!')
+        endif
+          
+   end subroutine mpk_SetTransferRedshifts
+  
+   subroutine ReadmpkDataset(gname)   
+     use MatrixUtils
+     character(LEN=*), intent(IN) :: gname
+     character(LEN=Ini_max_string_len) :: kbands_file, measurements_file, windows_file, cov_file
+     !! added for the LRG window function subtraction
+     character(LEN=Ini_max_string_len) :: zerowindowfxn_file, zerowindowfxnsubtractdat_file
+ 
+     Type (mpkdataset) :: mset
+ 
+     integer i,iopb
+     real keff,klo,khi,beff
+     integer :: num_mpk_points_full ! actual number of bandpowers in the infile
+     integer :: num_mpk_kbands_full ! actual number of k positions " in the infile
+     integer :: max_mpk_points_use ! in case you don't want the smallest scale modes (eg. sdss)
+     integer :: min_mpk_points_use ! in case you don't want the largest scale modes
+     integer :: max_mpk_kbands_use ! in case you don't want to calc P(k) on the smallest scales (will truncate P(k) to zero here!)
+     integer :: min_mpk_kbands_use ! in case you don't want to calc P(k) on the largest scales (will truncate P(k) to zero here!)
+     real, dimension(:,:), allocatable :: mpk_Wfull, mpk_covfull
+     real, dimension(:), allocatable :: mpk_kfull, mpk_fiducial
+ 
+     real, dimension(:), allocatable :: mpk_zerowindowfxnfull
+     real, dimension(:), allocatable :: mpk_zerowindowfxnsubfull
+ 
+     character(80) :: dummychar
+     logical bad
+     Type(TIniFile) :: Ini
+     integer file_unit
+     
+  
+     num_mpk_datasets = num_mpk_datasets + 1
+     if (num_mpk_datasets > 10) stop 'too many datasets'
+     file_unit = new_file_unit()
+     call Ini_Open_File(Ini, gname, file_unit, bad, .false.)
+     if (bad) then
+       write (*,*)  'Error opening dataset file '//trim(gname)
+       stop
+     end if
+ 
+     mset%name = Ini_Read_String_File(Ini,'name') 
+     Ini_fail_on_not_found = .false.
+     mset%use_set =.true.
+     if (Feedback > 0) write (*,*) 'reading: '//trim(mset%name)
+     num_mpk_points_full = Ini_Read_Int_File(Ini,'num_mpk_points_full',0)
+     if (num_mpk_points_full.eq.0) write(*,*) ' ERROR: parameter num_mpk_points_full not set'
+     num_mpk_kbands_full = Ini_Read_Int_File(Ini,'num_mpk_kbands_full',0)
+     if (num_mpk_kbands_full.eq.0) write(*,*) ' ERROR: parameter num_mpk_kbands_full not set'
+     min_mpk_points_use = Ini_Read_Int_File(Ini,'min_mpk_points_use',1)
+     min_mpk_kbands_use = Ini_Read_Int_File(Ini,'min_mpk_kbands_use',1)
+     max_mpk_points_use = Ini_Read_Int_File(Ini,'max_mpk_points_use',num_mpk_points_full)
+     max_mpk_kbands_use = Ini_Read_Int_File(Ini,'max_mpk_kbands_use',num_mpk_kbands_full)
+     mset%num_mpk_points_use = max_mpk_points_use - min_mpk_points_use +1
+     mset%num_mpk_kbands_use = max_mpk_kbands_use - min_mpk_kbands_use +1
+ 
+     allocate(mpk_Wfull(num_mpk_points_full,num_mpk_kbands_full))
+     allocate(mpk_kfull(num_mpk_kbands_full))
+     allocate(mset%mpk_P(mset%num_mpk_points_use))
+     allocate(mset%mpk_sdev(mset%num_mpk_points_use))  ! will need to replace with the covmat
+     allocate(mset%mpk_k(mset%num_mpk_kbands_use))
+     allocate(mset%mpk_W(mset%num_mpk_points_use,mset%num_mpk_kbands_use))
+     allocate(mset%mpk_zerowindowfxn(mset%num_mpk_kbands_use))
+     allocate(mset%mpk_zerowindowfxnsubtractdat(mset%num_mpk_points_use))
+     allocate(mpk_fiducial(mset%num_mpk_points_use))
+     allocate(mpk_zerowindowfxnsubfull(num_mpk_points_full+1)) 
+       !!need to add 1 to get the normalization held in the first (really zeroth) entry
+     allocate(mpk_zerowindowfxnfull(num_mpk_kbands_full))
+ 
+     kbands_file  = ReadIniFileName(Ini,'kbands_file')
+     call ReadVector(kbands_file,mpk_kfull,num_mpk_kbands_full)
+     mset%mpk_k(1:mset%num_mpk_kbands_use)=mpk_kfull(min_mpk_kbands_use:max_mpk_kbands_use) 
+     if (Feedback > 1) then 
+        write(*,*) 'reading: ',mset%name,' data'
+        write(*,*) 'Using kbands windows between',mset%mpk_k(1),' < k/h < ',mset%mpk_k(mset%num_mpk_kbands_use)      
+     endif
+     if  (mset%mpk_k(1) < matter_power_minkh) then
+        write (*,*) 'WARNING: k_min in '//trim(mset%name)//'less than setting in cmbtypes.f90'
+        write (*,*) 'all k<matter_power_minkh will be set to matter_power_minkh' 
+     end if
+ 
+     measurements_file  = ReadIniFileName(Ini,'measurements_file')
+     call OpenTxtFile(measurements_file, tmp_file_unit)
+     mset%mpk_P=0.
+     read (tmp_file_unit,*) dummychar
+     read (tmp_file_unit,*) dummychar
+     do i= 1, (min_mpk_points_use-1)
+        read (tmp_file_unit,*, iostat=iopb) keff,klo,khi,beff,beff,beff
+     end do
+     if (Feedback > 1 .and. min_mpk_points_use>1) write(*,*) 'Not using bands with keff=  ',keff,' or below'
+     do i =1, mset%num_mpk_points_use
+        read (tmp_file_unit,*, iostat=iopb) keff,klo,khi,mset%mpk_P(i),mset%mpk_sdev(i),mpk_fiducial(i)
+     end do
+     close(tmp_file_unit) 
+     if (Feedback > 1) write(*,*) 'bands truncated at keff=  ',keff
+     
+     windows_file  = ReadIniFileName(Ini,'windows_file')
+     if (windows_file.eq.'') write(*,*) 'ERROR: mpk windows_file not specified'
+     call ReadMatrix(windows_file,mpk_Wfull,num_mpk_points_full,num_mpk_kbands_full)
+     mset%mpk_W(1:mset%num_mpk_points_use,1:mset%num_mpk_kbands_use)= &
+        mpk_Wfull(min_mpk_points_use:max_mpk_points_use,min_mpk_kbands_use:max_mpk_kbands_use)
+     
+  
+     if (mset%name == 'lrg_2009') then
+ #ifndef DR71RG
+         call MpiStop('mpk: edit makefile to have "EXTDATA = LRG" to inlude LRGs')
+ #else
+         use_dr7lrg = .true.
+         zerowindowfxn_file  = ReadIniFileName(Ini,'zerowindowfxn_file')
+     
+         print *, 'trying to read this many points', num_mpk_kbands_full
+         if (zerowindowfxn_file.eq.'') write(*,*) 'ERROR: mpk zerowindowfxn_file not specified'
+         call ReadVector(zerowindowfxn_file,mpk_zerowindowfxnfull,num_mpk_kbands_full)
+         mset%mpk_zerowindowfxn(1:mset%num_mpk_kbands_use) = mpk_zerowindowfxnfull(min_mpk_kbands_use:max_mpk_kbands_use)
+         zerowindowfxnsubtractdat_file  = ReadIniFileName(Ini,'zerowindowfxnsubtractdat_file')
+         if (zerowindowfxnsubtractdat_file.eq.'') write(*,*) 'ERROR: mpk zerowindowfxnsubtractdat_file not specified'
+         call ReadVector(zerowindowfxnsubtractdat_file,mpk_zerowindowfxnsubfull,num_mpk_points_full+1)
+         mset%mpk_zerowindowfxnsubtractdat(1:mset%num_mpk_points_use) = &
+          mpk_zerowindowfxnsubfull(min_mpk_points_use+1:max_mpk_points_use+1)
+         mset%mpk_zerowindowfxnsubdatnorm = mpk_zerowindowfxnsubfull(1)
+ #endif
+     end if
+ 
+     cov_file  = ReadIniFileName(Ini,'cov_file')
+     if (cov_file /= '') then
+      allocate(mpk_covfull(num_mpk_points_full,num_mpk_points_full))
+      call ReadMatrix(cov_file,mpk_covfull,num_mpk_points_full,num_mpk_points_full)
+      allocate(mset%mpk_invcov(mset%num_mpk_points_use,mset%num_mpk_points_use))
+      mset%mpk_invcov=  mpk_covfull(min_mpk_points_use:max_mpk_points_use,min_mpk_points_use:max_mpk_points_use)
+      call Matrix_Inverse(mset%mpk_invcov)
+      deallocate(mpk_covfull)
+     else
+      nullify(mset%mpk_invcov)
+     end if
+ 
+     mset%use_scaling = Ini_Read_Logical_File(Ini,'use_scaling',.false.)
+ 
+     mset%Q_marge = Ini_Read_Logical_File(Ini,'Q_marge',.false.)
+     if (mset%Q_marge) then
+      mset%Q_flat = Ini_Read_Logical_File(Ini,'Q_flat',.false.)
+      if (.not. mset%Q_flat) then
+       !gaussian prior on Q
+       mset%Q_mid = Ini_Read_Real_File(Ini,'Q_mid')
+       mset%Q_sigma = Ini_Read_Real_File(Ini,'Q_sigma')
+      end if
+      mset%Ag = Ini_Read_Real_File(Ini,'Ag', 1.4)
+     end if 
+     if (iopb.ne.0) then
+        stop 'Error reading mpk file'
+     endif
+  
+    call Ini_Close_File(Ini)
+    call ClearFileUnit(file_unit)
+    
+    deallocate(mpk_Wfull, mpk_kfull,mpk_fiducial)
+ 
+    mpkdatasets(num_mpk_datasets) = mset
+  
+   if (mset%name == 'lrg_2009') call LSS_LRG_mpklike_init()
+ 
+   end subroutine ReadmpkDataset
+ 
+  
+   function LSS_mpklike(Theory,mset,CMB) result(LnLike) ! LV_06 added CMB here
+    Type (mpkdataset) :: mset
+    Type (CosmoTheory) Theory
+    Type(CMBparams) CMB     !LV_06 added for LRGDR4
+    real LnLike
+    real, dimension(:), allocatable :: mpk_Pth, mpk_k2,mpk_lin,k_scaled !LV_06 added for LRGDR4
+    real, dimension(:), allocatable :: w
+    real, dimension(:), allocatable :: mpk_WPth, mpk_WPth_k2
+    real :: covdat(mset%num_mpk_points_use), covth(mset%num_mpk_points_use),  covth_k2(mset%num_mpk_points_use)
+    real :: normV, Q, minchisq
+    real :: a_scl  !LV_06 added for LRGDR4
+    integer :: i, iQ
+    logical :: do_marge
+    integer, parameter :: nQ=6
+    real :: tmp, dQ = 0.4
+    real chisq(-nQ:nQ)
+    real calweights(-nQ:nQ)
+    real vec2(2),Mat(2,2)
+    
+    allocate(mpk_lin(mset%num_mpk_kbands_use) ,mpk_Pth(mset%num_mpk_kbands_use))
+    allocate(mpk_WPth(mset%num_mpk_points_use))
+    allocate(k_scaled(mset%num_mpk_kbands_use))!LV_06 added for LRGDR4
+    allocate(w(mset%num_mpk_points_use))
+ 
+    chisq = 0
+ 
+    if (.not. mset%use_set) then
+       LnLike = 0
+       return
+    end if
+ 
+    ! won't actually want to do this multiple times for multiple galaxy pk data sets?..
+ 
+    IF(mset%use_scaling) then   
+       call compute_scaling_factor(dble(CMB%omk),dble(CMB%omv),dble(CMB%w),a_scl)      
+    else
+      a_scl = 1
+    end if
+       
+ 
+    do i=1, mset%num_mpk_kbands_use 
+      !Errors from using matter_power_minkh at lower end should be negligible
+          k_scaled(i)=max(matter_power_minkh,a_scl*mset%mpk_k(i))
+          mpk_lin(i)=MatterPowerAt(Theory,k_scaled(i))/a_scl**3
+    end do
+       
+       
+     do_marge = mset%Q_Marge
+     if (do_marge .and. mset%Q_flat) then
+         !Marginalize analytically with flat prior on b^2 and b^2*Q
+         !as recommended by Max Tegmark for SDSS
+           allocate(mpk_k2(mset%num_mpk_kbands_use))
+           allocate(mpk_WPth_k2(mset%num_mpk_points_use))
+  
+           mpk_Pth=mpk_lin/(1+mset%Ag*k_scaled)
+           mpk_k2=mpk_Pth*k_scaled**2
+           mpk_WPth = matmul(mset%mpk_W,mpk_Pth)
+           mpk_WPth_k2 = matmul(mset%mpk_W,mpk_k2)
+ 
+           if (associated(mset%mpk_invcov)) then
+             covdat = matmul(mset%mpk_invcov,mset%mpk_P)
+             covth = matmul(mset%mpk_invcov,mpk_WPth)
+             covth_k2 = matmul(mset%mpk_invcov,mpk_WPth_k2)
+           else
+             w=1/(mset%mpk_sdev**2)
+             covdat = mset%mpk_P*w
+             covth = mpk_WPth*w
+             covth_k2 = mpk_WPth_k2*w     
+           end if
+            
+           Mat(1,1) = sum(covth*mpk_WPth)
+           Mat(2,2) = sum(covth_k2*mpk_WPth_k2)
+           Mat(1,2) = sum(covth*mpk_WPth_k2)
+           Mat(2,1) = Mat(1,2)
+           LnLike = log( Mat(1,1)*Mat(2,2)-Mat(1,2)**2)
+           call inv_mat22(Mat)
+           vec2(1) = sum(covdat*mpk_WPth)
+           vec2(2) = sum(covdat*mpk_WPth_k2)
+           LnLike = (sum(mset%mpk_P*covdat) - sum(vec2*matmul(Mat,vec2)) + LnLike ) /2
+ 
+           deallocate(mpk_k2,mpk_WPth_k2)      
+     else
+ 
+       if (mset%Q_sigma==0) do_marge = .false.
+       
+       do iQ=-nQ,nQ
+          Q = mset%Q_mid +iQ*mset%Q_sigma*dQ 
+          
+          if (mset%Q_marge) then
+             mpk_Pth=mpk_lin*(1+Q*k_scaled**2)/(1+mset%Ag*k_scaled)
+          else 
+             mpk_Pth = mpk_lin
+          end if
+          
+          mpk_WPth = matmul(mset%mpk_W,mpk_Pth)
+          
+          !with analytic marginalization over normalization nuisance (flat prior on b^2)
+          !See appendix F of cosmomc paper
+          
+          if (associated(mset%mpk_invcov)) then
+             covdat = matmul(mset%mpk_invcov,mset%mpk_P)
+             covth = matmul(mset%mpk_invcov,mpk_WPth)
+             normV = sum(mpk_WPth*covth)
+             chisq(iQ) = sum(mset%mpk_P*covdat)  - sum(mpk_WPth*covdat)**2/normV  + log(normV)  
+             
+          else
+       
+             !with analytic marginalization over normalization nuisance (flat prior on b^2)
+             w=1/(mset%mpk_sdev**2)
+             normV = sum(mpk_WPth*mpk_WPth*w)
+             tmp=sum(mpk_WPth*mset%mpk_P*w)/normV ! avoid subtracting one large number from another
+             chisq(iQ) = sum(mset%mpk_P*(mset%mpk_P - mpk_WPth*tmp)*w)  + log(normV)
+          end if
+          
+          if (do_marge) then
+             calweights(iQ) = exp(-(iQ*dQ)**2/2)
+          else 
+             LnLike = chisq(iQ)/2
+             exit
+          end if
+    
+       end do
+       
+       !without analytic marginalization
+       !! chisq = sum((mset%mpk_P(:) - mpk_WPth(:))**2*w) ! uncommented for debugging purposes
+       
+       if (do_marge) then
+          minchisq=minval(chisq)
+          LnLike = sum(exp(-(chisq-minchisq)/2)*calweights)/sum(calweights)
+          if (LnLike == 0) then
+             LnLike = LogZero
+          else
+             LnLike =  -log(LnLike) + minchisq/2
+          end if
+       end if
+ 
+    end if !not analytic over Q
+       
+    if (Feedback>1) write(*,*) 'mpk chi-sq:', LnLike*2
+    
+    if (LnLike > 1e8) then
+       write(*,*) 'Chisq is huge, maybe there is a problem? chisq=',chisq
+    end if
+    
+    deallocate(mpk_Pth,mpk_lin)
+    deallocate(mpk_WPth,k_scaled,w)
+    
+  end function LSS_mpklike
+ 
+ 
+  function LSSLnLike(CMB, Theory)
+    Type (CMBParams) CMB
+    Type (CosmoTheory) Theory
+    real LSSLnLike
+    integer i
+    real tot(num_mpk_datasets)
+ 
+   do i=1, num_mpk_datasets
+      if (mpkdatasets(i)%name == 'twodf') then
+         stop 'twodf no longer supported - use data/2df_2005.dataset'
+      else if (mpkdatasets(i)%name == 'lrg_2009') then
+         tot(i) = LSS_LRG_mpklike(Theory,mpkdatasets(i),CMB)
+      else
+       tot(i) = LSS_mpklike(Theory,mpkdatasets(i),CMB) !LV_06 added CMB here
+      end if
+   end do
+   LSSLnLike = SUM(tot) 
+   
+  end function LSSLnLike
+ 
+  subroutine inv_mat22(M)
+     real M(2,2), Minv(2,2), det
+ 
+     det = M(1,1)*M(2,2)-M(1,2)*M(2,1)
+     Minv(1,1)=M(2,2)
+     Minv(2,2) = M(1,1)
+     Minv(1,2) = - M(2,1)
+     Minv(2,1) = - M(1,2)
+     M = Minv/det
+ 
+  end subroutine inv_mat22
+ 
+ !-----------------------------------------------------------------------------
+ !LV added to include lrg DR4
+ 
+ subroutine compute_scaling_factor(Ok,Ol,w,a)
+   ! a = dV for z=0.35 relative to its value for flat Om=0.25 model.
+   ! This is the factor by which the P(k) measurement would shift 
+   ! sideways relative to what we got for this fiducial flat model.
+   ! * a = (a_angular**2 * a_radial)**(1/3)
+   ! * a_angular = comoving distance to z=0.35 in Mpc/h relative to its value for flat Om=0.25 model
+   !     dA = (c/H)*eta = (2997.92458 Mpc/h)*eta, so we care only about 
+   !     eta scaling, not h scaling.
+   !     For flat w=-1 models, a ~ (Om/0.25)**(-0.065)
+   !     For the LRG mean redshift z=0.35, the power law fit 
+   !    dA(z,Om= 0.3253 (Om/0.25)^{-0.065}c H_0^{-1} is quite good within 
+   !    our range of interest,
+   !     accurate to within about 0.1% for 0.2<Om<0.3.
+   ! * a_radial = 1/H(z) relative to its value for flat Om=0.25 model
+   implicit none
+   real(mpk_d) Or, Om, Ok, Ol, w, Ok0, Om0, Ol0, w0, z, eta, eta0, Hrelinv, Hrelinv0, tmp
+   real(mpk_d) a_radial, a_angular
+   real a
+   !Or= 0.0000415996*(T_0/2.726)**4 / h**2
+   Or= 0! Radiation density totally negligible at  z < 0.35
+   Om= 1-Ok-Ol-Or
+   !!!z  = 0.35  !!edited by Beth 21-11-08: change to zeff of Will's LRG sample.
+   z = zeffDR7
+   Hrelinv= 1/sqrt(Ol*(1+z)**(3*(1+w)) + Ok*(1+z)**2 + Om*(1+z)**3 + Or*(1+z)**4)
+ !  write(*,*) Ok,Ol,w  
+ call compute_z_eta(Or,Ok,Ol,w,z,eta)
+   tmp = sqrt(abs(Ok))
+   if (Ok.lt.-1.d-6) eta = sin(tmp*eta)/tmp
+   if (Ok.gt.1d-6)   eta = (exp(tmp*eta)-exp(-tmp*eta))/(2*tmp) ! sinh(tmp*eta)/tmp
+   Ok0= 0
+   Ol0= 0.75
+   w0= -1
+   Om0= 1-Ok0-Ol0-Or
+   call compute_z_eta(Or,Ok0,Ol0,w0,z,eta0)
+   Hrelinv0= 1/sqrt(Ol0*(1+z)**(3*(1+w0)) + Ok0*(1+z)**2 + Om0*(1+z)**3 + Or*(1+z)**4)
+   !a_angular = (Om/0.25)**(-0.065) * (-w*Otot)**0.14 ! Approximation based on Taylor expansion
+   a_angular = eta/eta0
+   a_radial= Hrelinv/Hrelinv0
+   a=  (a_angular**2 * a_radial)**(1/3.d0)
+   !write(*,'(9f10.5)') Ok,Ol,w,a,a_radial,a_angular,(Om/0.25)**(-0.065) * (-w*(1-Ok))**0.14
+   !write(*,'(9f10.5)') Ok,Ol,w,a,a_radial**(2/3.d0),a_angular**(4/3.d0),((Om/0.25)**(-0.065) * (-w*(1-Ok))**0.14)**(4/3.d0)
+   !!! BR09 -- in previous version, scale factor is applied in the wrong direction!  So take a = 1/a to fix it.
+   a = 1.0/a
+ end subroutine compute_scaling_factor
+ 
+ subroutine eta_demo
+   implicit none
+   real(mpk_d) Or, Ok, Ol, w, h, z, eta
+   h  = 0.7
+   Ok = 0
+   Ol = 0.7
+   Or = 0.0000416/h**2 
+   w  = -1
+   z  = 1090
+   call compute_z_eta(Or,Ok,Ol,w,z,eta)
+ !  print *,'eta.............',eta
+ !  print *,'dlss in Gpc.....',(2.99792458/h)*eta
+ end subroutine eta_demo
+ 
+ !INTERFACE
+ logical function nobigbang2(Ok,Ol,w)
+   ! Test if we're in the forbidden zone where the integrand blows up
+   ! (where's H^2 < 0 for some a between 0 and 1).
+   ! The function f(a) = Omega_m + Omega_k*a + Omega_l*a**(-3*w)
+   ! can have at most one local minimum, at (Ok/(3*w*Ol))**(-1/(1+3*w)), 
+   ! so simply check if f(a)<0 there or at the endpoints a=0, a=1.
+   ! g(0) = Omega_m - Omega_l*a**(-3*w) < 0 if w > 0 & Omega_k > 1
+   !                                     or if w = 0 & Omega_l < 1       
+   ! g(1) = Omega_m + Omega_k + Omega_l = 1 > 0
+   implicit none
+   real(mpk_d) Ok, Ol, w, Om, tmp, a, epsilon
+   integer failure
+   failure = 0
+   epsilon = 0
+   !epsilon = 0.04  ! Numerical integration fails even before H^2 goes negative.
+   Om = 1.d0 - Ok - Ol
+   if (w*Ol.ne.0) then
+      tmp = Ok/(3*w*Ol)
+      if ((tmp.gt.0).and.(1+3*w.ne.0)) then ! f'(0)=0 for some a>0
+         a = tmp**(-1/(1+3*w))
+         if (a.lt.1) then
+            if (Om + Ok*a + Ol*a**(-3*w).lt.epsilon) failure = 1
+         end if
+      end if
+   end if
+   if ((w.eq.0).and.(Ok.gt.1)) failure = 2
+   if ((w.gt.0).and.(Ol.lt.0)) failure = 3
+   nobigbang2 = (failure.gt.0)
+   if (failure.gt.0) print *,'Big Bang failure mode ',failure
+   return
+ end function nobigbang2
+ !END INTERFACE
+ 
+ real(mpk_d) function eta_integrand(a)
+   implicit none
+   real(mpk_d) Or, Ok, Ox, w
+   common/eta/Or, Ok, Ox, w
+   real(mpk_d) a, Om
+   ! eta = int (H0/H)dz = int (H0/H)(1+z)dln(1+z) = int (H0/H)/a dlna = int (H0/H)/a^2 da = 
+   ! Integrand = (H0/H)/a^2
+   ! (H/H0)**2 = Ox*a**(-3*(1+w)) + Ok/a**2 + Om/a**3 + Or/a**4 
+   if (a.eq.0.d0) then 
+      eta_integrand = 0.d0
+   else
+      Om = 1.d0 - Or - Ok - Ox
+      eta_integrand = 1.d0/sqrt(Ox*a**(1-3*w) + Ok*a**2 + Om*a + Or)
+   end if
+   return
+ end function eta_integrand
+ 
+ subroutine eta_z_integral(Omega_r,Omega_k,Omega_x,w_eos,z,eta)
+   ! Computes eta as a function
+   ! of the curvature Omega_k, the dark energy density Omega_x
+   ! and its equation of state w.
+   implicit none
+   real(mpk_d) Or, Ok, Ox, w
+   common/eta/Or, Ok, Ox, w
+   real(mpk_d) Omega_r, Omega_k,Omega_x,w_eos, z, eta, epsabs, epsrel, amin, amax!, eta_integrand
+   Or = Omega_r
+   Ok = Omega_k
+   Ox = Omega_x
+   w  = w_eos
+   epsabs  = 0
+   epsrel  = 1.d-10
+   amin= 1/(1+z)
+   amax= 1
+   call qromb2(eta_integrand,amin,amax,epsabs,epsrel,eta)
+   return
+ end subroutine eta_z_integral
+ 
+ subroutine compute_z_eta(Or,Ok,Ox,w,z,eta)
+   ! Computes the conformal distance eta(z)
+   implicit none
+   real(mpk_d) Or, Ok, Ox, w, z, eta
+ !  logical nobigbang2
+   if (nobigbang2(Ok,Ox,w)) then
+      print *,'No big bang, so eta undefined if z>zmax.'
+      eta = 99 
+   else
+      call eta_z_integral(Or,Ok,Ox,w,z,eta) 
+      ! print *,'Or, Ok, Ox, w, z, H_0 t_0...',Or, Ok, Ox, w, eta
+   end if
+   return
+ end subroutine compute_z_eta
+ 
+ 
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+ !!!! num rec routines
+ !!!!!!!!!!!!!!!!!!!!!!!!!!
+ SUBROUTINE qromb2(func,a,b,epsabs,epsrel,ss)
+ ! The numerical recipes routine, but modified so that is decides
+ ! it's done when either the relative OR the absolute accuracy has been attained.
+ ! The old version used relative errors only, so it always failed when
+ ! when the integrand was near zero.
+ ! epsabs = epsrel = 1e-6 are canonical choices.
+   INTEGER JMAX,JMAXP,K,KM
+   real(mpk_d) a,b,func,ss,epsabs,epsrel
+   EXTERNAL func
+   PARAMETER (JMAX=20, JMAXP=JMAX+1, K=5, KM=K-1)
+                                 !    USES polint,trapzd
+   INTEGER j
+   real(mpk_d) dss,h(JMAXP),s(JMAXP)
+   h(1)=1.d0
+   do j=1,JMAX
+      call trapzd(func,a,b,s(j),j)
+      if (j.ge.K) then
+         call polint(h(j-KM),s(j-KM),K,0.d0,ss,dss)
+         if (abs(dss).le.epsrel*abs(ss)) return
+         if (abs(dss).le.epsabs) return
+      endif
+      s(j+1)=s(j)
+      h(j+1)=0.25d0*h(j)
+   ENDDO
+   print *,'Too many steps in qromb'
+       
+   RETURN 
+ END SUBROUTINE qromb2
+   
+ SUBROUTINE polint(xa,ya,n,x,y,dy) ! From Numerical Recipes
+   INTEGER n,NMAX
+   real(mpk_d) dy,x,y,xa(n),ya(n)
+   PARAMETER (NMAX=10)
+   INTEGER i,m,ns
+   real(mpk_d) den,dif,dift,ho,hp,w,c(NMAX),d(NMAX)
+   ns=1
+   dif=abs(x-xa(1))
+   do  i=1,n
+      dift=abs(x-xa(i))
+      if (dift.lt.dif) then
+         ns=i
+         dif=dift
+      endif
+      c(i)=ya(i)
+      d(i)=ya(i)
+   enddo
+   y=ya(ns)
+   ns=ns-1
+   do  m=1,n-1
+      do  i=1,n-m
+         ho=xa(i)-x
+         hp=xa(i+m)-x
+         w=c(i+1)-d(i)
+         den=ho-hp
+         if(den.eq.0.) then
+            print*, 'failure in polint'
+            stop
+         endif
+         den=w/den
+         d(i)=hp*den
+         c(i)=ho*den
+      enddo
+      if (2*ns.lt.n-m)then
+         dy=c(ns+1)
+      else
+         dy=d(ns)
+         ns=ns-1
+      endif
+      y=y+dy
+   enddo
+   return
+ END SUBROUTINE polint
+       
+ SUBROUTINE trapzd(func,a,b,s,n) ! From Numerical Recipes
+   INTEGER n
+   real(mpk_d) a,b,s,func
+   EXTERNAL func
+   INTEGER it,j
+   real(mpk_d) del,sum,tnm,x
+   if (n.eq.1) then
+      s=0.5*(b-a)*(func(a)+func(b))
+   else
+      it=2**(n-2)
+      tnm=it
+      del=(b-a)/tnm
+      x=a+0.5*del
+      sum=0.
+      do  j=1,it
+         sum=sum+func(x)
+         x=x+del
+      enddo
+      s=0.5*(s+(b-a)*sum/tnm)
+   endif
+   return
+ END SUBROUTINE trapzd
+             
+ 
+ 
+ !! added by Beth Reid for LRG P(k) analysis
+ 
+   function a2maxpos(a1val) result(a2max)
+     real(dl), intent(in) :: a1val
+     real(dl) a2max
+     a2max = -1.0d0
+     if (a1val <= min(s1/k1,s2/k2)) then
+       a2max = min(s1/k1**2 - a1val/k1, s2/k2**2 - a1val/k2)
+     end if
+   end function a2maxpos
+ 
+   function a2min1pos(a1val) result(a2min1)
+     real(dl), intent(in) :: a1val
+     real(dl) a2min1
+     a2min1 = 0.0d0
+     if(a1val <= 0.0d0) then
+       a2min1 = max(-s1/k1**2 - a1val/k1, -s2/k2**2 - a1val/k2, 0.0d0)
+     end if
+   end function a2min1pos
+ 
+   function a2min2pos(a1val) result(a2min2)
+     real(dl), intent(in) :: a1val
+     real(dl) a2min2
+     a2min2 = 0.0d0
+     if(abs(a1val) >= 2.0d0*s1/k1 .and. a1val <= 0.0d0)  then
+       a2min2 = a1val**2/s1*0.25d0
+     end if
+   end function a2min2pos
+ 
+   function a2min3pos(a1val) result(a2min3)
+     real(dl), intent(in) :: a1val
+     real(dl) a2min3
+     a2min3 = 0.0d0
+     if(abs(a1val) >= 2.0d0*s2/k2 .and. a1val <= 0.0d0)  then
+       a2min3 = a1val**2/s2*0.25d0
+     end if
+   end function a2min3pos
+ 
+   function a2minfinalpos(a1val) result(a2minpos)
+     real(dl), intent(in) :: a1val
+     real(dl) a2minpos
+     a2minpos = max(a2min1pos(a1val),a2min2pos(a1val),a2min3pos(a1val))
+   end function a2minfinalpos
+ 
+   function a2minneg(a1val) result(a2min)
+     real(dl), intent(in) :: a1val
+     real(dl) a2min
+     if (a1val >= max(-s1/k1,-s2/k2)) then
+       a2min = max(-s1/k1**2 - a1val/k1, -s2/k2**2 - a1val/k2)
+     else
+       a2min = 1.0d0
+     end if
+   end function a2minneg
+ 
+   function a2max1neg(a1val) result(a2max1)
+     real(dl), intent(in) :: a1val
+     real(dl) a2max1
+     if(a1val >= 0.0d0) then
+       a2max1 = min(s1/k1**2 - a1val/k1, s2/k2**2 - a1val/k2, 0.0d0)
+     else
+       a2max1 = 0.0d0
+     end if
+   end function a2max1neg
+ 
+   function a2max2neg(a1val) result(a2max2)
+     real(dl), intent(in) :: a1val
+     real(dl) a2max2
+     a2max2 = 0.0d0
+     if(abs(a1val) >= 2.0d0*s1/k1 .and. a1val >= 0.0d0)  then
+       a2max2 = -a1val**2/s1*0.25d0
+     end if
+   end function a2max2neg
+ 
+   function a2max3neg(a1val) result(a2max3)
+     real(dl), intent(in) :: a1val
+     real(dl) a2max3
+     a2max3 = 0.0d0
+     if(abs(a1val) >= 2.0d0*s2/k2 .and. a1val >= 0.0d0)  then
+       a2max3 = -a1val**2/s2*0.25d0
+     end if
+   end function a2max3neg
+ 
+   function a2maxfinalneg(a1val) result(a2maxneg)
+     real(dl), intent(in) :: a1val
+     real(dl) a2maxneg
+     a2maxneg = min(a2max1neg(a1val),a2max2neg(a1val),a2max3neg(a1val))
+   end function a2maxfinalneg
+ 
+ 
+ function testa1a2(a1val, a2val) result(testresult)
+     real(dl), intent(in) :: a1val,a2val
+     logical :: testresult
+ 
+     real(dl) :: kext, diffval
+     testresult = .true.
+ 
+     ! check if there's an extremum; either a1val or a2val has to be negative, not both
+     kext = -a1val/2.0d0/a2val
+     diffval = abs(a1val*kext + a2val*kext**2)
+     if(kext > 0.0d0 .and. kext <= k1 .and. diffval > s1) testresult = .false.
+     if(kext > 0.0d0 .and. kext <= k2 .and. diffval > s2) testresult = .false.
+ 
+     if (abs(a1val*k1 + a2val*k1**2) > s1) testresult = .false.
+     if (abs(a1val*k2 + a2val*k2**2) > s2) testresult = .false.
+ 
+ end function testa1a2
+ 
+ !! copying LSS_mpklike above.
+ !! points_use is how many points to use in the likelihood calculation; 
+ !!kbands_use is how many points you need to have a theory for in order to convolve the theory with the window function.
+ 
+ 
+ ! this subroutine fills in the a1 and a2 values only once.
+  subroutine LSS_LRG_mpklike_init()
+    real(dl) :: a1val, a2val
+    real(dl) :: da1, da2  ! spacing of numerical integral over nuisance params.
+    integer :: countcheck = 0
+    integer :: i, j
+    !! this is just for checking the 'theory' curves for fiducial model
+    real :: fidLnLike
+    type(CosmoTheory) :: temptheory
+    type(CMBparams) :: tempCMB
+ 
+    da1 = a1maxval/(nptsa1/2)
+    da2 = a2maxpos(-a1maxval)/(nptsa2/2)
+    do i = -nptsa1/2, nptsa1/2
+       do j = -nptsa2/2, nptsa2/2
+          a1val = da1*i
+          a2val = da2*j
+ 
+          if ((a2val >= 0.0d0 .and. a2val <= a2maxpos(a1val) .and. a2val >= a2minfinalpos(a1val)) .or. &
+      & (a2val <= 0.0d0 .and. a2val <= a2maxfinalneg(a1val) .and. a2val >= a2minneg(a1val))) then
+             if(testa1a2(a1val,a2val) .eqv. .false.)  then
+                print *,'Failed a1, a2: ',a1val,a2val
+                if (a2val >= 0.0d0) print *,'pos', a2maxpos(a1val), a2minfinalpos(a1val)
+                if (a2val <= 0.0d0) print *,'neg', a2maxfinalneg(a1val), a2minneg(a1val)
+                stop
+             end if
+             countcheck = countcheck + 1
+             if(countcheck > nptstot) then
+                print *, 'countcheck > nptstot failure.'
+                stop
+             end if
+             a1list(countcheck) = a1val
+             a2list(countcheck) = a2val
+             !print *, countcheck, a1list(countcheck), a2list(countcheck)
+          end if
+       end do
+    end do
+    if(countcheck .ne. nptstot) then
+      print *, 'countcheck issue', countcheck, nptstot
+      stop
+    end if
+   call LRGinfo_init()
+  end subroutine LSS_LRG_mpklike_init
+ 
+ 
+  function LSS_LRG_mpklike(Theory,mset,CMB) result(LnLike)  ! LV_06 added CMB here
+    Type (mpkdataset) :: mset
+    Type (CosmoTheory) Theory
+    Type(CMBparams) CMB                  !LV_06 added for LRGDR4
+    real LnLike
+    integer :: i
+    real, dimension(:), allocatable :: mpk_raw, mpk_Pth, mpk_Pth_k, mpk_Pth_k2, k_scaled
+    real, dimension(:), allocatable :: mpk_WPth, mpk_WPth_k, mpk_WPth_k2
+    real :: covdat(mset%num_mpk_points_use), covth(mset%num_mpk_points_use), &
+           & covth_k(mset%num_mpk_points_use), covth_k2(mset%num_mpk_points_use), & 
+           & covth_zerowin(mset%num_mpk_points_use)
+ 
+    real, dimension(nptstot) :: chisq, chisqmarg  !! minus log likelihood list
+    real :: minchisq,maxchisq,deltaL
+ 
+    real(dl) :: a1val, a2val, zerowinsub
+    real :: sumDD, sumDT, sumDT_k, sumDT_k2, sumTT,& 
+      &  sumTT_k, sumTT_k2, sumTT_k_k, sumTT_k_k2, sumTT_k2_k2, &
+      &  sumDT_tot, sumTT_tot, &
+      &  sumDT_zerowin, sumTT_zerowin, sumTT_k_zerowin, sumTT_k2_zerowin, sumTT_zerowin_zerowin
+ 
+    real :: sumzerow_Pth, sumzerow_Pth_k, sumzerow_Pth_k2
+ 
+    real :: a_scl      !LV_06 added for LRGDR4
+ 
+    real(wp) :: temp1,temp2,temp3
+    real :: temp4
+ 
+    !! added for no marg
+    integer :: myminchisqindx
+    real :: currminchisq, currminchisqmarg, minchisqtheoryamp, chisqnonuis
+    real :: minchisqtheoryampnonuis, minchisqtheoryampminnuis
+    real(dl), dimension(2) :: myerfval
+ 
+    call fill_LRGTheory(Theory,matter_power_minkh,matter_power_dlnkh)
+    allocate(mpk_raw(mset%num_mpk_kbands_use) ,mpk_Pth(mset%num_mpk_kbands_use))
+    allocate(mpk_Pth_k(mset%num_mpk_kbands_use) ,mpk_Pth_k2(mset%num_mpk_kbands_use))
+    allocate(mpk_WPth(mset%num_mpk_points_use),mpk_WPth_k(mset%num_mpk_points_use),mpk_WPth_k2(mset%num_mpk_points_use))
+    allocate(k_scaled(mset%num_mpk_kbands_use))!LV_06 added for LRGDR4
+ 
+    chisq = 0
+ 
+    if (.not. mset%use_set) then
+       LnLike = 0
+       return
+    end if
+ 
+    IF(mset%use_scaling) then
+       call compute_scaling_factor(dble(CMB%omk),dble(CMB%omv),dble(CMB%w),a_scl)
+       !! this step now applied in compute_scaling_factor
+       !! this fixes the bug most easily !!
+       !!a_scl = 1.0d0/a_scl
+    else
+      a_scl = 1
+      stop 'use_scaling should be set to true for the LRGs!'
+    end if
+ 
+    do i=1, mset%num_mpk_kbands_use
+          k_scaled(i)=max(matter_power_minkh,a_scl*mset%mpk_k(i))
+          mpk_raw(i)=LRGPowerAt(Theory,k_scaled(i))/a_scl**3
+    end do
+ 
+    mpk_Pth = mpk_raw
+ 
+    mpk_Pth_k = mpk_Pth*k_scaled
+    mpk_Pth_k2 = mpk_Pth*k_scaled**2
+    mpk_WPth = matmul(mset%mpk_W,mpk_Pth)
+    mpk_WPth_k = matmul(mset%mpk_W,mpk_Pth_k)
+    mpk_WPth_k2 = matmul(mset%mpk_W,mpk_Pth_k2)
+ 
+    sumzerow_Pth = sum(mset%mpk_zerowindowfxn*mpk_Pth)/mset%mpk_zerowindowfxnsubdatnorm
+    sumzerow_Pth_k = sum(mset%mpk_zerowindowfxn*mpk_Pth_k)/mset%mpk_zerowindowfxnsubdatnorm
+    sumzerow_Pth_k2 = sum(mset%mpk_zerowindowfxn*mpk_Pth_k2)/mset%mpk_zerowindowfxnsubdatnorm
+ 
+ 
+    covdat = matmul(mset%mpk_invcov,mset%mpk_P)
+    covth = matmul(mset%mpk_invcov,mpk_WPth)
+    covth_k = matmul(mset%mpk_invcov,mpk_WPth_k)
+    covth_k2 = matmul(mset%mpk_invcov,mpk_WPth_k2)
+    covth_zerowin = matmul(mset%mpk_invcov,mset%mpk_zerowindowfxnsubtractdat)
+ 
+    sumDD = sum(mset%mpk_P*covdat)
+    sumDT = sum(mset%mpk_P*covth)
+    sumDT_k = sum(mset%mpk_P*covth_k)
+    sumDT_k2 = sum(mset%mpk_P*covth_k2)
+    sumDT_zerowin = sum(mset%mpk_P*covth_zerowin)
+ 
+    sumTT = sum(mpk_WPth*covth)
+    sumTT_k = sum(mpk_WPth*covth_k)
+    sumTT_k2 = sum(mpk_WPth*covth_k2)
+    sumTT_k_k = sum(mpk_WPth_k*covth_k)
+    sumTT_k_k2 = sum(mpk_WPth_k*covth_k2)
+    sumTT_k2_k2 = sum(mpk_WPth_k2*covth_k2)
+    sumTT_zerowin = sum(mpk_WPth*covth_zerowin)
+    sumTT_k_zerowin = sum(mpk_WPth_k*covth_zerowin)
+    sumTT_k2_zerowin = sum(mpk_WPth_k2*covth_zerowin)
+    sumTT_zerowin_zerowin = sum(mset%mpk_zerowindowfxnsubtractdat*covth_zerowin)
+ 
+    currminchisq = 1000.0d0
+    do i=1,nptstot
+      a1val = a1list(i)
+      a2val = a2list(i)
+      zerowinsub = -(sumzerow_Pth + a1val*sumzerow_Pth_k + a2val*sumzerow_Pth_k2)
+ 
+      sumDT_tot = sumDT + a1val*sumDT_k + a2val*sumDT_k2 + zerowinsub*sumDT_zerowin
+      sumTT_tot = sumTT + a1val**2.0d0*sumTT_k_k + a2val**2.0d0*sumTT_k2_k2 + & 
+                  & zerowinsub**2.0d0*sumTT_zerowin_zerowin &
+        & + 2.0d0*a1val*sumTT_k + 2.0d0*a2val*sumTT_k2 + 2.0d0*a1val*a2val*sumTT_k_k2 &
+        & + 2.0d0*zerowinsub*sumTT_zerowin + 2.0d0*zerowinsub*a1val*sumTT_k_zerowin &
+        & + 2.0d0*zerowinsub*a2val*sumTT_k2_zerowin
+      minchisqtheoryamp = sumDT_tot/sumTT_tot
+      chisq(i) = sumDD - 2.0d0*minchisqtheoryamp*sumDT_tot + minchisqtheoryamp**2.0d0*sumTT_tot
+ #ifdef DR71RG
+      myerfval(1) = sumDT_tot/2.0d0/sqrt(sumTT_tot)
+      call geterf(myerfval)
+      chisqmarg(i) = sumDD - sumDT_tot**2.0d0/sumTT_tot & 
+          & + log(sumTT_tot) &
+          & - 2.0*log(1.0d0 + myerfval(2))
+ #else
+      !!leave out the erf term, just to get it to compile.  This should never run.
+      chisqmarg(i) = sumDD - sumDT_tot**2.0d0/sumTT_tot & 
+          & + log(sumTT_tot)
+      if(0 .eq. 0) stop 'Logic problem.  Shouldnt be here.'
+ #endif
+ !this should always be here, but we're using gsl to call erf, so this function is only available if gsl is installed.
+      if(i == 1 .or. chisq(i) < currminchisq) then
+         myminchisqindx = i
+         currminchisq = chisq(i)
+         currminchisqmarg = chisqmarg(i)
+         minchisqtheoryampminnuis = minchisqtheoryamp
+      end if
+      if(i == int(nptstot/2)+1) then
+         chisqnonuis = chisq(i)
+         minchisqtheoryampnonuis = minchisqtheoryamp
+         if(abs(a1val) > 0.001 .or. abs(a2val) > 0.001) then
+            print *, 'ahhhh! violation!!', a1val, a2val
+         end if
+      end if
+ 
+    end do
+ 
+ ! numerically marginalize over a1,a2 now using values stored in chisq
+    minchisq = minval(chisqmarg)
+    maxchisq = maxval(chisqmarg)
+ 
+    LnLike = sum(exp(-(chisqmarg-minchisq)/2.0d0)/(nptstot*1.0d0))
+    if(LnLike == 0) then
+      LnLike = LogZero
+    else
+      LnLike = -log(LnLike) + minchisq/2.0d0
+    end if
+  deltaL = (maxchisq - minchisq)*0.5
+  if(Feedback > 1) print *,'LRG P(k) LnLike = ',LnLike
+ 
+    deallocate(mpk_raw, mpk_Pth)
+    deallocate(mpk_Pth_k, mpk_Pth_k2)
+    deallocate(mpk_WPth, mpk_WPth_k, mpk_WPth_k2)
+    deallocate(k_scaled) 
+ 
+  end function LSS_LRG_mpklike
+ 
+ end module
diff -r -c -b -N cosmomc/source/params_CMB.f90 cosmomc_wiggles/source/params_CMB.f90
*** cosmomc/source/params_CMB.f90	2009-10-02 12:16:48.000000000 +0200
--- cosmomc_wiggles/source/params_CMB.f90	2009-10-28 16:36:42.824459670 +0100
***************
*** 57,80 ****
         integer, intent(in) :: in
  
  
         if (Power_Name == 'power_tilt') then
  
-        P%InitPower%k_0_scalar = pivot_k
-        P%InitPower%k_0_tensor = pivot_k
- 
         P%InitPower%ScalarPowerAmp(in) = cl_norm*CMB%norm(norm_As)
         P%InitPower%rat(in) = CMB%norm(norm_amp_ratio)
          
         P%InitPower%an(in) = CMB%InitPower(1)
         P%InitPower%ant(in) = CMB%InitPower(2)
         P%InitPower%n_run(in) = CMB%InitPower(3)
!        if (inflation_consistency) then
!          P%InitPower%ant(in) = - CMB%norm(norm_amp_ratio)/8.
!           !note input n_T is ignored, so should be fixed (to anything)
!        end if
         else
           stop 'params_CMB:Wrong initial power spectrum'
         end if
  
      end subroutine SetCAMBInitPower
   
--- 57,118 ----
         integer, intent(in) :: in
  
  
+ !wig
         if (Power_Name == 'power_tilt') then
  
         P%InitPower%ScalarPowerAmp(in) = cl_norm*CMB%norm(norm_As)
         P%InitPower%rat(in) = CMB%norm(norm_amp_ratio)
          
         P%InitPower%an(in) = CMB%InitPower(1)
         P%InitPower%ant(in) = CMB%InitPower(2)
         P%InitPower%n_run(in) = CMB%InitPower(3)
! 
!           P%InitPower%logomega(in) = CMB%InitPower(5)
!           P%InitPower%omega(in) = 10**(P%InitPower%logomega(in))
!           P%InitPower%amposc(in) = CMB%InitPower(6)          
!           P%InitPower%phase(in) = CMB%InitPower(7)
!           P%InitPower%logpowosc(in) = CMB%InitPower(8)
!           P%InitPower%powosc(in) = 10**(P%InitPower%logpowosc(in))
!           
!        elseif (Power_Name == 'power_sr') then
!          
!           P%InitPower%P_scalar =  cl_norm*CMB%norm(norm_As)
!           P%InitPower%eps1(in) =  CMB%InitPower(1)
!           P%InitPower%eps2(in) =  CMB%InitPower(2)
!           P%InitPower%eps3(in) =  CMB%InitPower(3)
! 
!           P%InitPower%logeps1invsigma0(in) = CMB%InitPower(5)
!           P%InitPower%loginvsigma0(in) = P%InitPower%logeps1invsigma0(in) &
!                - log10(P%InitPower%eps1(in))
!           P%InitPower%invsigma0(in) = 10**(P%InitPower%loginvsigma0(in))
!           P%InitPower%Xsigma0(in) = CMB%InitPower(6)
!           P%InitPower%varphi(in)  = CMB%InitPower(7)
!           !CMB%InitPower(7) unused.
!           CMB%InitPower(7) = 1./P%InitPower%invsigma0(in)
! 
!        elseif (Power_Name == 'power_dbi') then
! 
!           P%InitPower%P_scalar =  cl_norm*CMB%norm(norm_As)
!           P%InitPower%eps1(in) =  10.**(CMB%InitPower(4))
!           P%InitPower%del1(in) = P%InitPower%eps1(in) - CMB%InitPower(1)
!           P%InitPower%eps2(in) =  CMB%InitPower(2) - P%InitPower%del1(in)
!           P%InitPower%eps3(in) =  CMB%InitPower(3)
! 
!           P%InitPower%logeps1invsigma0(in) = CMB%InitPower(5)
!           P%InitPower%loginvsigma0(in) = P%InitPower%logeps1invsigma0(in) &
!                - log10(P%InitPower%eps1(in))
!           P%InitPower%invsigma0(in) = 10**(P%InitPower%loginvsigma0(in))
!           P%InitPower%Xsigma0(in) = CMB%InitPower(6)
!           P%InitPower%varphi(in)  = CMB%InitPower(7)
!           !CMB%InitPower(7) unused.
!           CMB%InitPower(7) = 1./P%InitPower%invsigma0(in)
! 
!           P%InitPower%gam(in) = 10**(-CMB%InitPower(8))*P%InitPower%eps1(in)
! 
         else
           stop 'params_CMB:Wrong initial power spectrum'
         end if
+ !end wig
  
      end subroutine SetCAMBInitPower
   
diff -r -c -b -N cosmomc/source/settings.f90 cosmomc_wiggles/source/settings.f90
*** cosmomc/source/settings.f90	2009-10-22 11:33:09.000000000 +0200
--- cosmomc_wiggles/source/settings.f90	2009-10-28 16:37:31.028218127 +0100
***************
*** 6,11 ****
--- 6,18 ----
    implicit none
  
    real :: AccuracyLevel = 1.
+ !wig
+   real :: lAccuracyLevel = 1.
+   real :: lSampleLevel = 1.
+   real :: kSrcSampleLevel = 1.
+   real :: kIntSampleLevel = 1.
+   logical :: CambComputeEveryl = .false.
+ !end wig
    !Set to >1 to use CAMB etc on higher accuracy settings. 
    !Does not affect MCMC (except making it all slower)
  
***************
*** 19,25 ****
  !                    (e.g. beam uncertainty modes, etc, specific to dataset)
  
    integer, parameter :: num_hard =7 
!   integer, parameter :: num_initpower = 3 
    integer, parameter :: num_freq_params = 1
    integer, parameter :: num_norm = 2 + num_freq_params 
    integer, parameter :: num_nuisance_params= 0
--- 26,35 ----
  !                    (e.g. beam uncertainty modes, etc, specific to dataset)
  
    integer, parameter :: num_hard =7 
! !wig
! !  integer, parameter :: num_initpower = 3 
!   integer, parameter :: num_initpower = 8
! !end wig
    integer, parameter :: num_freq_params = 1
    integer, parameter :: num_norm = 2 + num_freq_params 
    integer, parameter :: num_nuisance_params= 0
***************
*** 61,66 ****
--- 71,79 ----
  
    integer :: num_threads = 0
    integer :: instance = 0
+ !wig
+   integer :: instance_shift = 0
+ !end wig
    integer :: MPIchains = 1, MPIrank = 0
  
    logical :: Use_LSS = .true.
