function [yd]=ymd2yd(ymd)

% ymd2yd converts a string of the form yyyy/mm/dd
%             to  a string of the form yyyy/ddd
%
% begun 1 april 99 by paul hemenway
%


% $Id: ymd2yd.m,v 1.1 1999/05/20 18:20:52 paul Exp $
%

% $Log: ymd2yd.m,v $
% Revision 1.1  1999/05/20 18:20:52  paul
% Initial version
%

%setups:

normalyear=[31 28 31 30 31 30 31 31 30 31 30 31];
leapyear=[31 29 31 30 31 30 31 31 30 31 30 31];

% various allowed formats:

yr=str2num(ymd(1:4));

if ymd(7)=='/'
  mo=str2num(ymd(6));
else
   mo=str2num(ymd(6:7));
end


lymd =length(ymd);
if lymd==10
   da=str2num(ymd(9:10));
elseif lymd==9
  if ymd(7)=='/'
    da=str2num(ymd(8:9));
  else
    da=str2num(ymd(9));
  end
elseif lymd==8
  da=str2num(ymd(8));
else
  fprintf('you have the wrong input format for this function:\n')
  fprintf('%s\n\n',ymd)
  return
end
   


if mo<1 |mo>12
  fprintf('your month %d is out of range\n',mo)
  return
end
if da<0 | da>31
  fprintf('your day %d is out of range\n',da)
  return
end

% OK, now do it:

year=normalyear;
if isleap(yr)
  year=leapyear;
end

yrday=da;

for i=1:mo-1
  yrday=yrday+year(i);
end

yd=sprintf('%04d/%03d',yr,yrday);
