function vec_intp (ncid,ncname,flxnam,timnam,ptx,pty,outfile);
%
% vec_intp (ncid,ncname,flxnam,timnam,ptx,pty,outfile);
%
% This function interpolates the vector 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:  VEC_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:  VEC_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:  VEC_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 components. ---
   %-------------------------------------

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

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

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

   upt = interp2 (u,ptx,pty,'*cubic');
   vpt = interp2 (v,ptx,pty,'*cubic');

   clear u v;

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

   fprintf (fid,'%12.8f   %12.8f   %12.8f\n',[time upt vpt]);
   [mess errnum] = ferror (fid);
   if (errnum~=0)
      disp (' ');
      disp (['***Error:  VEC_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 upt vpt;

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

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

end;

clear n nchk ntim timeoff vecind;

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

status = fclose (fid);

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

clear fid status;
