      subroutine initialize
        use ssd, only: ssd_init
        
        call ssd_init()
      end subroutine


      subroutine ephemerides(t_epoch, elems, obscod, t_eph_uts, n, &
            h_v, g, predictions, exterrcod, has_covar, covar)
        ! use constants_mod, only: wp
        use ssd, only: ssd_compute_ephemerides
        double precision, intent(in) :: t_epoch, elems(6)
        double precision, intent(in) :: covar(21)
        integer, intent(in) :: has_covar
        character (len=3), intent(in) :: obscod
        integer, intent(in) :: n
        double precision, intent(in) :: t_eph_uts(n), h_v, g
        double precision, intent(out) :: predictions(n, 9)
        integer, intent(out) :: exterrcod

        ! Housekeeping.
        double precision :: jpl_elems(6)
        double precision :: null_covar(21) = 0
        double precision :: v_minus_hs(n)
        integer :: valid_covar
        

        ! JPL wants orbital elements in the e, q, tp, node, peri, i oder.
        ! MOPS stores in the q, e, i, node, peri, tp order.
        jpl_elems(1) = elems(2)
        jpl_elems(2) = elems(1)
        jpl_elems(3) = elems(6)
        jpl_elems(4) = elems(4)
        jpl_elems(5) = elems(5)
        jpl_elems(6) = elems(3)

        ! Initialize the output predictions array.
        predictions(:, :) = -1.0d0
        predictions(:, 4) = t_eph_uts

        ! Is the SRC covar valid or not?
        valid_covar = has_covar
        if(any(covar .eq. 0)) then
           valid_covar = 0
        endif

        ! Sort the times in reverse order. We assume that the initial array is 
        ! already sorted.
        call ssd_compute_ephemerides(t_epoch,jpl_elems,covar,obscod, &
             g,t_eph_uts,valid_covar,predictions(:,1),predictions(:,2), &
             predictions(:,5),predictions(:,6),predictions(:,7), &
             predictions(:,8),predictions(:,9),v_minus_hs,exterrcod)

        ! Now fix the mag info in predictions(:, 3)
        predictions(:, 3) = v_minus_hs(:) + h_v
        
        ! And now do the radians -> degrees conversions.
        predictions(:, 1) = predictions(:, 1) * 180.0d0 / 3.1415926535897932384626433832795d0
        predictions(:, 2) = predictions(:, 2) * 180.0d0 / 3.1415926535897932384626433832795d0
      end subroutine ephemerides


      subroutine covar2src(covar, src)
        use ssd, only: compute_src
        double precision, intent(in) :: covar(21)
        double precision, intent(out) :: src(21)
        
        call compute_src(covar, src)
      end subroutine covar2src
