function fid=openclp(filename)

%OPENCLP    Open EchoPAC clipfile for reading
%
%   fid=OPENCLP(filename)
%
%   Input: filename - string containing NAME or PATH of file to open
%                     The filename parameter is optional,
%                     and a dialog box opens if no filename
%                     is specified or if the specified file or path
%                     is not found on the matlab search path.
%
%   Output: fid     - file identifier, see FOPEN.
%                     If cancel is pressed, fid=0.
% 
%   See also: READECHO, READINFO, FOPEN

% (c) GE Vingmed Ultrasound 1997-99. All rights reserved
% Written by: JK, 23.08.96
% Last update: 06.08.97, JK

v=version;
v=str2num(v(1));
if v<5,
    permission='ieee-be';
else
    permission='ieee-be.l64';
end;

c=computer;
c=c(1:3);
if strcmp(c,'mac'), 
    wildcards='*';
else
    wildcards='*.*';
end;

oldpwd=pwd;
if nargin==1,
    if isstr(filename),
        isdir = 1;
        eval('cd(filename);','isdir=0;');
        cd(oldpwd);

        if isdir,
            cd(filename);
        else
            fid=fopen(filename,'rb',permission);
            if fid>=3,
                return;
            end;
        end;
    end;
end;

[filename, pathname] = uigetfile(wildcards,'Open EchoPAC clip file');
cd(oldpwd);

if ~isstr(filename),
    if filename==0,
        fid=0;
        return;
    end;
end;

if exist([pathname,filename])~=2,
    temppwd=pwd;
    isdir = 1;
    eval('cd([pathname,filename])','isdir=0;');
    cd(temppwd)
    if isdir,
        error('The file chosen is a directory.');
    end;
end;

fid=fopen([pathname,filename],'rb',permission);

if fid <=2,
    error(['Unable to open file: ',pathname,filename]);
end;