function ymd=yr2ymd(Time)

% yr2ymd converts decimal year Time 
%            to   string  yyyy/mm/dd truncated.
%   
%        yymmdd = yr2ymd(Time)
%
%     this will NOT handle a vector of Times.
%
%   started 2 May 1999 by Paul Hemenway
%

% $Id: yr2ymd.m,v 1.1 1999/05/20 18:21:40 paul Exp $
%

% $Log: yr2ymd.m,v $
% Revision 1.1  1999/05/20 18:21:40  paul
% Initial version
%

year=floor(Time);
daysinyear = 365;
if (rem(year,4) == 0 & rem(year,100)~=0) | ...
   rem(year,400)==0;
  daysinyear = 366;                % This is a leapyear.
end

normalmonths=[31 29 31 30 31 30 31 31 30 31 30 31];

leapmonths = [31 28 31 30 31 30 31 31 30 31 30 31];

% the starting day number within the year of each month
%   remember that month.0 is day 1 of the month.
startnm=[1 32 60 91 121 152 182 213 244 274 305 335];
startlm=[1 32 61 92 122 153 183 214 245 275 306 336];

startm=(startnm-1)/365;
if daysinyear==366;
   startm=(startlm-1)/366;
end

dec=Time-year;

% the following is clever, and not good programming practice.
mdx=find(startm<=dec);

mm=sprintf('%02d',length(mdx));

yyyy=sprintf('%04d',year);

% note:  we need to add one to days to bring it in line with startm
%        and one to bring it "over the top"
days=dec * daysinyear + 1 + 1;

day = days-startnm(length(mdx));
if daysinyear==366
  day = days-startlm(length(mdx));
end

dd=sprintf('%02d',floor(day));

ymd=[yyyy '/' mm '/' dd];

