function [rafosJulianDay] = dmy2rd(pDay, pMonth, pYear)
%DMY2RD Converts the given parameters to rafos julian day.
%     converts (day, month, year) to RAFOS JULIAN day (rjul)
%     RAFOS JULIAN day = julian day (ref. to 24 nov 4713BC) - 2440000
%
%          translated from dates.c from GSO RAFOS processing code
%          which was taken from Collected Algorithms from ACM, #199
%                   "astronomically correct"
%          and converted to RAFOS JULIAN day   20.03.91 kst
%          This routine is valid forever ....
%
%   NOTE:   this RAFOS JULIAN day = old ARGOS day - 6718 + 1
%
%   NOTE:   The rafos day has been redefined, to shorten numbers.
%           It has been:
%           rafosJulianDay = rafosJulianDay - 2440000
%           but is now:
%           rafosJulianDay = rafosJulianDay - 2451545
%
%   Parameters:
%       pDay (int)      The day.
%       pMonth (int)    The month.
%       pYear (int)     The year.
%
%   Returns:
%       rafosJulianDay (int)        The converted rafos julian day.

%% Parameter check

if (nargin == 1)
  d = pDay;
  pDay = d(:,1);
  pMonth = d(:,2);
  pYear = d(:,3);
end

%% Setup variables
pDay = pDay(:);
pMonth = pMonth(:);
pYear = pYear(:);

%% Convert
gttwo = find( pMonth > 2 );
letwo = find( pMonth < 3 );
pMonth(gttwo) = pMonth(gttwo) - 3;
pMonth(letwo) = pMonth(letwo) + 9;
pYear(letwo) = pYear(letwo) - 1;

ic = floor( pYear / 100 );
iy = pYear - 100 * ic;
rafosJulianDay = floor((146097 * ic) / 4) ...
    + floor((1461 * iy) / 4) ...
    + floor((153 * pMonth + 2) / 5) ...
    + pDay ...
    + 1721119;

rafosJulianDay = rafosJulianDay - 2451545;