function [YD] = yeardays(dat)
% YEARDAYS -	returns the day of the year from the given date
%
% USAGE -	[YD]=yeardays(date);
%		date - vector [year month day] eg. [92 3 24]
%		YD - vector [ dayofyear year ] eg. [ 84 ]
%		argument can be columnwise matrix 
%

% PROGRAM - 	MATLAB code by c.m.duncombe rae
%	created mid-90s?
%
% PROG MODS -	
% 2008/02/01 - copied original code from useful scripts. modify original 
%	code to reverse order of output (dayofyear first)

%
% DISCLAIMER -	No responsibility can be accepted for failure or 
%		incorrect operation of this code. No guarantee, express 
%		or implied, is given. 
%

%
%
	modays=[0 31 28 31 30 31 30 31 31 30 31 30 31];
	lydays=[0 31 29 31 30 31 30 31 31 30 31 30 31];
%
	dim=[ 	cumsum(modays);
		cumsum(lydays)
	];
% adjust year to 19?? and find leapyears (LY)
	I=find(dat(:,1)<100);
	dat(I,1)=dat(I,1)+1900;
	yr=rem(dat(:,1),100);
	I=find(yr==0);

	yr(I)=dat(I,1)./100;
	lys=rem(yr,4);
	LY=find(lys==0);
%
	days=dim(1,dat(:,2));
	days(LY)=dim(2,dat(LY,2));
%
	YD=[ days.'+dat(:,3) ];
clear dat modays lydays dim I yr lys days 
return;

