function timeoff = jul2yd (ncid,timename);
%
% timeoff = jul2yd (ncid,timename);
%
% This function determines the offset needed to convert modified
% Julian days into year days.

%-------------------------------------------------------------------------------
% Initialize output.
%-------------------------------------------------------------------------------

timeoff = [];

%-------------------------------------------------------------------------------
% Read starting time.
%-------------------------------------------------------------------------------

time0 = ncid{timename}(1);

if isempty(time0)
   disp (' ');
   disp (['***Error:  JUL2YD - unable to read ',setstr(34),timename, ...
          setstr(34),' in netCDF file.']);
   disp (' ');
   return;
end;

%-------------------------------------------------------------------------------
% Read modified Julian date offset.
%-------------------------------------------------------------------------------

jdoff = ncid{timename}.add_offset(:);

if isempty(jdoff)
   disp (' ');
   disp (['***Error:  JUL2YD - unable to read ',setstr(34),timename, ...
          '.add_offset',setstr(34),' in netCDF file.']);
   disp (' ');
   return;
end;

%-------------------------------------------------------------------------------
% Compute conversion offset for modified Julian days.
%-------------------------------------------------------------------------------

%--------------------------------------
%--- Convert to Gregorian calendar. ---
%--------------------------------------

greg = gregorian (time0+jdoff);

clear time0;

%---------------------------------------------
%--- Get Julian date for end of last year. ---
%---------------------------------------------

time_lstyr = julian (greg(1)-1,12,31,0) + 0.5;

clear greg;

%-----------------------
%--- Compute offset. ---
%-----------------------

timeoff = time_lstyr - jdoff;

clear jdoff;
