  subroutine samewave(nopr,iout,&
  nlines,wcm,wavelength,&
  nlinesr,wcmr,wavelengthr,rndat,&
  nlinesi,wcmi,wavelengthi,ridat)

! *************************
  use netcdf

! Has max number of output wavelengths (or indices)
  include 'param1.inc'

! See rdindices.f90
  character(len=100) :: lset
  integer :: nlinesr,nlinesi,nlines
  real :: wcm(nwavemax),wavelength(nwavemax)
  real :: rndat(nwavemax),ridat(nwavemax)

! For cases in which the number of real and imaginary indices differ
  real :: rndat2(nwavemax),ridat2(nwavemax)
  real :: wcmr(nwavemax),wavelengthr(nwavemax)
  real :: wcmi(nwavemax),wavelengthi(nwavemax)

! *************************
! Put values into temporary rndat2 and ridat2 arrays
    do i=1,nlinesr
     rndat2(i)=rndat(i)
    end do

    do i=1,nlinesi
     ridat2(i)=ridat(i)
    end do

! *************************
    if (nlinesi .lt. nlinesr) then 

! **
      nlines=nlinesi

! **
! The imaginary values are the primary ones to work with
     do i=1,nlines 
      ridat(i)=ridat2(i)
      wcm(i)=wcmi(i)
      wavelength(i)=wavelengthi(i)
     end do

! **
! Find first match
      i=1
      diffmin=1.0e6
      istart=-99
     do ii=1,nlinesr 
      diff=abs(wavelengthr(ii)-wavelength(i))
      if (diff .lt. diffmin) then
       istart=ii
       diffmin=diff
      end if
     end do

      iwrsp=0
     if (iwrsp .eq. 1) then
      write(iout,fmt=5000) i,istart,diffmin,&
      wavelengthr(istart),wavelength(i)
      5000 format(2x,"samewave: i,istart,diffmin ",&
      2(1x,i4),1p,1(1x,e10.3),/,&
      2x,"samewave: wavelengthr(istart),wavelength(i) ",2(1x,e10.3),/,&
      2x,"samewave: i,ii,rndat(i) ")
     end if

! **
! Specify the rndat values
      idiff=5
      ii=istart
     do i=1,nlines 

       j1=ii-idiff
       j2=ii+idiff
      if (j1 .lt. 1) then
       j1=1
      end if
      if (j2 .ge. nlinesr) then
       j2=nlinesr
      end if
 
       diffmin=1.0e6
       iuse=-99
      do j=j1,j2 
       diff=abs(wavelengthr(j)-wavelength(i))
       if (diff .lt. diffmin) then
        iuse=j
        ii=j
        diffmin=diff
       end if
      end do

       rndat(i)=0.0
      if ((iuse .ge. 1) .and. (iuse .le. nlinesr)) then 
       rndat(i)=rndat2(iuse)
      end if 

      if (iwrsp .eq. 1) then 
       write(iout,fmt=5010) i,iuse,rndat(i)
       5010 format(2x,2(1x,i4),1p,1(1x,e10.3))
      endif

      end do

    end if

! *************************
    if (nlinesr .lt. nlinesi) then

! **
      nlines=nlinesr

! **
! The real values are the primary ones to work with
     do i=1,nlines
      rndat(i)=rndat2(i)
      wcm(i)=wcmi(i)
      wavelength(i)=wavelengthi(i)
     end do

! **
! Find first match
      diffmin=1.0e6
      i=1
     do ii=1,nlinesr
      diff=abs(wavelengthr(ii)-wavelength(i))
      if (diff .lt. diffmin) then
       istart=ii
       diffmin=diff
      end if
     end do

! **
! Specify the rndat values
      idiff=5
      ii=istart
     do i=1,nlines

       j1=ii-idiff
       j2=ii+idiff
      if (j1 .lt. 1) then 
       j1=1
      end if
      if (j2 .ge. nlinesi) then
       j2=nlinesi
      end if
 
       iuse=-99
       diffmin=1.0e6
      do j=j1,j2 
       diff=abs(wavelengthi(j)-wavelength(i))
       if (diff .lt. diffmin) then
        iuse=j
        ii=j
        diffmin=diff
       end if
      end do

       ridat(i)=0.0
      if ((iuse .ge. 1) .and. (iuse .le. nlinesi)) then 
       ridat(i)=ridat2(iuse)
      end if

      end do

    end if

! *************************
! Write out the results
    write(iout,fmt=500) nlines,nlinesr,nlinesi
    500 format(/,&
    2x,"samewave: nlines,nlinesr,nlinesi ",/,&
    2x,3(2x,i4),/,&
    2x,"samewave: i,wavelength(i),wcm(i),rndat(i),ridat(i)")
   do i=1,nlines 
    write(iout,fmt=510) i,wavelength(i),wcm(i),rndat(i),ridat(i)
    510 format(2x,i4,2x,2(1x,f10.4),1p,2(1x,e10.3))
   end do

! *************************
  return
  end
