function [interp_data, lon, lat, depth] = prof_interp ( ptrfile, variable, time_vector )
% TS_INTERP:  Interpolates a variable along time_vector from a pointer file.
%
% USAGE:  [interp_data, lon, lat, depth] = ts_interp ( ptrfile, variable, time_vector );
%
% PARAMETERS:  
%   ptrfile:
%      pointer file of Netcdf files
%   variable:
%      The name of the variable to interpolate.
%   time_vector:
%      Interpolate the variable at these time values.  NaNs will be returned
%      in all time slots outside this range.
%   interp_data:
%      The interpolated data.

if ( nargin ~= 3 )
    help ts_interp;
    return;
end

switch ( variable )

    case 'salt'
        variable = 'S_40';
    case 'sal'
        variable = 'S_40';
    case 'salinity'
        variable = 'S_40';

    case 'pressure'
        variable = 'P_4023';
    case 'press'
        variable = 'P_4023';

    case 'temp'
        variable = 'T_20';
    case 'temperature'
        variable = 'T_20';

end


%
% Count the number of files beforehand
afid = fopen ( ptrfile, 'r' );
cdfname = fgetl ( afid );
fcount = 0;
while ( cdfname ~= -1 )
    fcount = fcount+1;
    cdfname = fgetl ( afid );
end


interp_data = NaN * ones(length(time_vector),fcount);


mexcdf ( 'setopts', 0 );

count = 0;

fseek ( afid, 0, 'bof' );
cdfname = fgetl ( afid );
while ( cdfname ~= -1 )

    %
    % open the file, make sure it has the variable in it
    [ncid, status] = mexcdf ( 'open', cdfname, 'nowrite' );
    if ( ncid == -1 )
        fprintf ( 2, 'Could not open %s.\n', cdfname );
        return;
    end

    [varid, status] = mexcdf ( 'varid', ncid, variable );
    if ( varid == -1 )
        ;
    else
	count = count+1;
	
	%
	% OK, get the depth and variable
	[time, status] = mexcdf ( 'varget', ncid, 'time', [0], [-1] );
	if ( status == -1 )
	    fprintf ( 2, 'Could not retrieve time from %s.\n', cdfname );
	    return;
	end
	[time2, status] = mexcdf ( 'varget', ncid, 'time2', [0], [-1] );
	if ( status == -1 )
	    fprintf ( 2, 'Could not retrieve time2 from %s.\n', cdfname );
	    return;
	end

	time = time + time2/8.64e7;


	[val, status] = mexcdf ( 'varget', ncid, variable, [0 0 0 0], [-1 -1 -1 -1] );
	if ( status == -1 )
	    fprintf ( 2, 'Could not retrieve %s from %s.\n', variable, cdfname );
	    return;
	end

	[lon(count), status] = mexcdf ( 'varget', ncid, 'lon', [0], [-1] );
	if ( status == -1 )
	    fprintf ( 2, 'Could not retrieve lon from %s.\n', variable, cdfname );
	    return;
	end

	[lat(count), status] = mexcdf ( 'varget', ncid, 'lat', [0], [-1] );
	if ( status == -1 )
	    fprintf ( 2, 'Could not retrieve lat from %s.\n', variable, cdfname );
	    return;
	end

	[depth(count), status] = mexcdf ( 'varget', ncid, 'depth', [0], [-1] );
	if ( status == -1 )
	    fprintf ( 2, 'Could not retrieve depth from %s.\n', variable, cdfname );
	    return;
	end


	%
	% interpolate along depth vector
	vi = interp1 ( time, val, time_vector );

	interp_data(:,count) = vi(:);


    end


    mexcdf ( 'close', ncid );

    cdfname = fgetl ( afid );

end


interp_data = interp_data(:,1:count);

lat = lat(:);
lon = lon(:);
depth = depth(:);




