function day_num=day_of_year(JD,mon,year)
% Calculation of day_of_year by calendar date, MJD or serial date
%
%    day_num=day_of_year(JD,mon,year);
%
% JD    - calendar day, modified Julian date MJD or serial date
% mon   - month
% year  - year
%
% day_num - day of year

% 14.06.2003 - S.Khomutov
% 15.01.2005 - add possibility of using MJD
% 03.08.2016 - add possibility of using Serial date (JD)

if nargin==1,	% MJD or serial date
    if JD>100000,  % Julian date
        [year,mon,day]=datevec(JD);
        day_num = JD-datenum(year,1,0);
    else            % MJD
        [year,mon,day]=mjdcdt(JD);
        day_num = JD-cdtmjd(year-1,12,31);
    end
else
%     day_num = datenum(year,mon,JD)-datenum(year(1),1,0);
    day_num = datenum(year,mon,JD)-datenum(year,1,0);
end
