function ADCPData = ADCP_readAquaVisionTekal(fname);
%ADCP_readAquaVisionTekal   load tekal file into struct
%
%              ADCP_readAquaVisionTekal         % via ADCPview
%   ADCPData = ADCP_readAquaVisionTekal(fname); % command line
%
%See also: 
disp(['Processing: ',fname])
if nargin==0
    [dum,fig]=gcbo;

    [name,pat]=uigetfiles('*.txt','Load stationair AquaVision ADCP file');
    if isempty(name)
        return
    end
else    
    [pat,name,ext]=fileparts(fname);
    name = {[name ext]};
end

%Init waitbar
%wH = waitbar(0,'Loading ADCP data...');

ADCPCount=0;

for ii=1:length(name)
    
    %waitbar(ii/length(name),wH);
    filin=['\' name{ii}];
    tekalData=tekal('open',[pat filin]);
    %disp(filin)
    
    for ij=1:length(tekalData.Field)
        ADCPCount=ADCPCount+1;
        dateID=strmatch('* Date     : ',tekalData.Field(ij).Comments);
        posID=strmatch('* Position : ',tekalData.Field(ij).Comments);
        [dum,dum,dum,ADCPData(ADCPCount).date,ADCPData(ADCPCount).tTime,dum]=strread(char(tekalData.Field(ij).Comments(dateID(end))),'%s%s%s%s%s%s');
        ADCPData(ADCPCount).time=datenum(str2num(ADCPData(ADCPCount).date{1}(1:4)),str2num(ADCPData(ADCPCount).date{1}(5:6)),str2num(ADCPData(ADCPCount).date{1}(7:8)),...
            str2num(ADCPData(ADCPCount).tTime{1}(1:2)),str2num(ADCPData(ADCPCount).tTime{1}(3:4)),str2num(ADCPData(ADCPCount).tTime{1}(5:6)));
        
        [dum,dum,dum,ADCPData(ADCPCount).location(1),ADCPData(ADCPCount).location(2),dum]=strread(char(tekalData.Field(ij).Comments(posID(end))),'%s%s%s%f%f%s');    
        ADCPData(ADCPCount).temp = [];
        ADCPData(ADCPCount).sali = [];
        ADCPData(ADCPCount).sensorHeading = [];
        ADCPData(ADCPCount).sensorPitch = name{ii}; %This will be the original data filename
        ADCPData(ADCPCount).sensorRoll = [];
        ADCPData(ADCPCount).goodness=[];
        ADCPData(ADCPCount).lowestGoodNess=[];
        
        ADCPData(ADCPCount).depths=tekalData.Field(ij).Data(:,1)';
        ADCPData(ADCPCount).curVels=tekalData.Field(ij).Data(:,2)';
        ADCPData(ADCPCount).dirs=tekalData.Field(ij).Data(:,3)';
        
        ADCPData(ADCPCount).bins=[1:length(tekalData.Field(ij).Data(:,1))];
        ADCPData(ADCPCount).err(1:length(ADCPData(ADCPCount).bins))=0;
        ADCPData(ADCPCount).sensorDepth=0;
        ADCPData(ADCPCount).bedLevel=tekalData.Field(ij).Data(end,1)+0.5; %bed level weten we niet, we nemen nu de diepte van laatste bin + 0.5m
        ADCPData(ADCPCount).waterLevel=0; %water level weten we ook niet, dan maar 0...
    end
end

%close(wH);
%Sort the ADCPData in time
ADCPTimes=struct2num(ADCPData,'time');
[dum,sortID]=sort(ADCPTimes);
%Now sort structure
ADCPData=ADCPData(sortID);

%Put in index=1 the name of PSA
ADCPData(1).name='AquaVision ADCP Measurements';

if exist('ADCPData','var') & nargin==0
    set(findobj(fig,'tag','ADCP_loadDataBut'),'userdata',ADCPData);
    %Add the data path to the list, so prepared depthID mat-files can be
    %found
    addpath(pat);
end