function [interp_data, lon, lat, jd] = prof_interp ( ptrfile, variable, depth_vector )
% PROF_INTERP:  Interpolates a variable along depth_vector from a pointer file.
%
% USAGE:  [interp_data, lon, lat, jd] = prof_interp ( ptrfile, variable, depth_vector );
%
% PARAMETERS:  
%   ptrfile:
%      pointer file of Netcdf files
%   variable:
%      The name of the variable to interpolate.
%   depth_vector:
%      Interpolate the variable at these depth values.  NaNs will be returned
%      in all depth slots greater than the max depth in each file.
%   interp_data:
%      The interpolated data.
%   lon, lat:
%      Geographic coords of profiles.
%   jd:
%      julian datestamps of the profiles.

if ( nargin ~= 3 )
    help prof_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(depth_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
	[depth, status] = mexcdf ( 'varget', ncid, 'depth', [0], [-1] );
	if ( status == -1 )
	    fprintf ( 2, 'Could not retrieve depth from %s.\n', cdfname );
	    return;
	end

	[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

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

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

	jd(count) = time + time2/8.64e7;

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

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


    end


    mexcdf ( 'close', ncid );

    cdfname = fgetl ( afid );

end


interp_data = interp_data(:,1:count);

lat = lat(:);
lon = lon(:);
jd = jd(:);




