function [ c, w, s ] = jed_to_gps ( jed )

%*****************************************************************************80
%
%% jed_to_gps() converts a JED to a GPS date.
%
%  Discussion:
%
%    The GPS time keeping is in terms of seconds, weeks, and cycles
%    of 1024 weeks.  The weeks and cycles begin numbering at 0.
%
%    The computation is only valid for dates after the GPS epoch,
%    that is, after 6 January 1980.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    10 June 2012
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real JED, the Julian Ephemeris Date.
%
%  Output:
%
%    integer C, W, real S, the corresponding GPS cycles/weeks/seconds date.
%
  jed_epoch = epoch_to_jed_gps ( );

  d = jed - jed_epoch;

  if ( d < 0.0 )
    s = -1.0;
    w = -1;
    c = -1;
    return
  end

  w = floor ( d / 7 );
  d = d - 7 * w;

  c = floor ( w / 1024 );
  w = w - 1024 * c;

  s = d * 24.0 * 60.0 * 60.0;

  return
end
