function [dyear] = day2year(day, dref)
%
%  Converts a number of days since dref into a decimal year
%  value. dref is the base year.
%
%  Written by Tom Sqouros.
%
% $Id: day2year.m,v 1.5 1998/11/09 20:19:22 dbyrne Exp $


% The preceding empty line is important.

% $Log: day2year.m,v $
% Revision 1.5  1998/11/09 20:19:22  dbyrne
% *** empty log message ***
%
% Revision 1.3  1998/10/23 15:26:02  root
% updating NSCAT 3.0
%
% Revision 1.2  1998/09/13 15:52:38  root
% *** empty log message ***
%
% Revision 1.2  1998/07/13 20:29:28  jimg
% Fixes from the final test of the new build process
%
% Revision 1.1  1998/05/01 21:07:13  jimg
% Deirdre's fixes
%
% Revision 1.2  1997/11/26 19:01:49  jimg
% Changed comment about dref.
%
% Revision 1.1  1997/11/26 18:51:27  jimg
% Added.
%

if any(dref ~= 1)
  i = find(dref ~= 1);
  offset = year2day(dref(i), 1);
  day(i) = day(i) + offset;
end

ndays = [     0   36524   73048  109572  146097  182621 ...
      219145  255669  292194  328718  365242  401766 ...
      438291  474815  511339  547863  584388  620912 ...
      657436  693960  730485];
for j = 1:length(day)
  i = 1;
  while ndays(i) < day(j)
    i = i + 1;
  end
  i = i - 1;
  diff = day(j) - ndays(i);
  nleaps = floor(diff/1461);
  dyear(j) = floor(diff/365);
  diff = (diff-nleaps) - (dyear(j)*365);
  dyear(j) = dyear(j) + 1 + (100*(i-1));
  if isleap(dyear(j))
    dyear(j) = dyear(j) + diff/366.0;
  else
    dyear(j) = dyear(j) + diff/365.0;
  end
end

return

