subroutine csf_vector_to_sds(csfs, csf_coeffs, sds, sd_coeffs, ms)
integer(n_int), intent(in) :: csfs(:,:)
real(dp), intent(in) :: csf_coeffs(:)
real(dp), intent(in), optional :: ms
integer(n_int), intent(out), allocatable :: sds(:,:)
real(dp), intent(out), allocatable :: sd_coeffs(:)
real(dp) :: ms_
integer :: n_sds, spin, n_tot, i
integer(n_int), allocatable :: temp_all(:,:), temp_sds(:,:)
real(dp), allocatable :: temp_coeffs(:)
spin = abs(return_ms(csfs(:,1)))
def_default(ms_, ms, spin/2.)
n_sds = 2 ** nSpatorbs
allocate(temp_all(0:GugaBits%len_tot,n_sds), source = 0_n_int)
n_tot = 0
do i = 1, size(csfs,2)
call csf_to_sds_ilut(csfs(:,i), temp_sds, temp_coeffs, ms_, csf_coeffs(i))
call add_guga_lists(n_tot, size(temp_sds,2), temp_all, temp_sds)
end do
allocate(sds(0:GugaBits%len_tot, n_tot), source = temp_all(:,1:n_tot))
allocate(sd_coeffs(n_tot), source = 0.0_dp)
do i = 1, n_tot
sd_coeffs(i) = extract_matrix_element(sds(:,i), 1)
end do
end subroutine csf_vector_to_sds