function [Date]=caldate(Julian);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 1999 Rutgers University.                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                           %
% function [Date]=caldate(Julian)                                           %
%                                                                           %
% This function converts Julian day to calendar date.                       %
%                                                                           %
% On Input:                                                                 %
%                                                                           %
%    Julian      Julian day.                                                %
%                                                                           %
% On Output:                                                                %
%                                                                           %
%    Date        Calendar (Gregorian) date (structure array):               %
%                  Date.year  =>  year                                      %
%                  Date.yday  =>  year day                                  %
%                  Date.month =>  month                                     %
%                  Date.day   =>  day of the month                          %
%                  Date.hour  =>  decimal hours                             %
%                  Date.min   =>  minutes                                   %
%                  Date.sec   =>  seconds                                   %
%                                                                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Check fom modified Julian day.  Modified Julian day beggins at 
% Julian Day 2440000 (0000 hours, May 23, 1968).

JulDay=Julian;
if ( JulDay(1) < 2440000 ),
  JulDay=JulDay+2440000;
end,

% Kludge to prevent roundoff error on seconds.

JulDay=JulDay+5.0e-9;

% Convert to gregorain date.

j=floor(JulDay)-1721119;
in=4*j-1;
y=floor(in/146097);
j=in-146097*y;
in=floor(j/4);
in=4*in+3;
j=floor(in/1461);
d=floor(((in-1461*j)+4)/4);
in=5*d-3;
m=floor(in/153);
d=floor(((in-153*m)+5)/5);
y=y*100+j;
mo=m-9;
yr=y+1;
i=(m<10);
mo(i)=m(i)+3;
yr(i)=y(i);

% Get year day.

iyd =[1 32 60 91 121 152 182 213 244 274 305 335 366];
iydl=[1 32 61 92 122 153 183 214 245 275 306 336 367];
nrec=length(JulDay);
for n=1:nrec,
  if ( rem(yr(n),4) == 0 ),
    yday(n)=iydl(mo(n))+d(n)-1;
  else
    yday(n)=iyd(mo(n))+d(n)-1;
  end,
end,

% Extract hour, minutes and seconds.

secs=rem(JulDay,1)*24*3600;
sec=round(secs);
hour=floor(sec/3600);
min=floor(rem(sec,3600)/60);
sec=round(rem(sec,60));

% Build structure array.

Date.year =yr(:);
Date.yday =yday(:);
Date.month=mo(:);
Date.day  =d(:);
Date.hour =hour(:);
Date.min  =min(:);
Date.sec  =sec(:);

return
