function scl_intp (ncid,ncname,flxnam,timnam,ptx,pty,outfile);
%
% scl_intp (ncid,ncname,flxnam,timnam,ptx,pty,outfile);
%
% This function interpolates the scalar field to the requested point.

%-------------------------------------------------------------------------------
% Compute timing conversion (Julian day to yearday)
%-------------------------------------------------------------------------------

timeoff = jul2yd (ncid,timnam);

%-------------------------------------------------------------------------------
% Create output file.
%-------------------------------------------------------------------------------

[fid,mess] = fopen (outfile,'w');

if (fid<0)
   disp (' ');
   disp ('***Error:  SCL_INTP - unable to open output file:');
   disp (['          ',setstr(34),outfile,setstr(34)]);
   disp (['          ',setstr(34),mess,setstr(34)]);
   disp (' ');
   return;
end;

%-------------------------------------------------------------------------------
% Determine number of time levels in input file.
%-------------------------------------------------------------------------------

ntim = length(ncid(timnam));

if isempty(ntim)
   disp (' ');
   disp ('***Error:  SCL_INTP - unable to read PE_forcing netCDF file:');
   disp (['          ',setstr(34),ncname,setstr(34)]);
   disp (' ');
   return;
end;

nchk = round( (0.1:0.1:1.1).*ntim );

%-------------------------------------------------------------------------------
% Loop over all times, interpolating fluxes.
%-------------------------------------------------------------------------------

for n = 1:ntim

   %--------------------------
   %--- Read current time. ---
   %--------------------------

   time = squeeze(ncid{timnam}(n)) - timeoff;
   if isempty(time)
      disp (' ');
      disp (['***Error:  SCL_INTP - unable to read ',setstr(34),timnam, ...
            setstr(34),'(',num2str(n),')',' in netCDF file:']);
      disp (['          ',setstr(34),ncname,setstr(34)]);
      disp (' ');
      return;
   end;

   %--------------------------
   %--- Read current flux. ---
   %--------------------------

   flx    = squeeze(ncid{flxnam}(n,:,:));
   if isempty(flx)
      disp (' ');
      disp (['***Error:  SCL_INTP - unable to read ',setstr(34),flxnam, ...
            setstr(34),'(',num2str(n),')', ...
            ' in netCDF file:']);
      disp (['          ',setstr(34),ncname,setstr(34)]);
      disp (' ');
      return;
   end;

   %---------------------------
   %--- Interpolate fluxes. ---
   %---------------------------

   fpt = interp2 (flx,ptx,pty,'*cubic');

   clear flx;

   %---------------------
   %--- Write fluxes. ---
   %---------------------

   fprintf (fid,'%12.8f   %14.8f\n',[time fpt]);
   [mess errnum] = ferror (fid);
   if (errnum~=0)
      disp (' ');
      disp (['***Error:  SCL_INTP - unable to write time ',num2str(n), ...
             ' data to output file:']);
      disp (['          ',setstr(34),outfile,setstr(34)]);
      disp (['          ',setstr(34),mess,setstr(34)]);
      disp (' ');
      return;
   end;

   clear errnum mess time fpt;

   %-------------------------
   %--- Report on status. ---
   %-------------------------

   if (n==nchk(1))
      nchk(1) = [];
      disp (['      interpolated ',num2str(n),' of ',num2str(ntim),' fields']);
   end;

end;

clear n nchk ntim timeoff;

%-------------------------------------------------------------------------------
% Close output file.
%-------------------------------------------------------------------------------

status = fclose (fid);

if (status<0)
   disp (' ');
   disp ('***Error:  SCL_INTP - unable to close output file:');
   disp (['          ',setstr(34),outfile,setstr(34)]);
   disp (' ');
   return;
end;

clear fid status;
