function [time, u ,v ,pg ,nav, amp, pgnum, lbin, lpls, lblnk, nbins, bt_depth] = ...
	rtlditx(pgnum, starttime, endtime)
%***********************************  rtloadit.m  ****************************
%function [time, u ,v ,pg ,nav, amp, pgnum, lbin, lpls, lblnk, nbins, bt_depth] = ...
%	rtlditx(pgnum, starttime, endtime)
%
% This is a new version of loadit_pc that doesn't require entering the
% pingdata file names.  It will find the pingdata files that contain the 
% last n-hours of ADCP data and load the data into mat-files.

% cf, 9/24/94

% Modified to accept ensembles with different depth ranges and number of
% bins.  This is done using a modified version of ping2mat.exe 
% (bnl_p2m.exe) which now includes bin_length, pls_length, blank_length, 
% num_bins, & tr_depth as part of the data retrieved from the pingdata files. 
% cf, 11/98

% <<<<<<<<<<<<<<<<<<<< Begin Editing as needed  <<<<<<<<<<<<<
% Modify the following as they pertain to the current vessel

global disk year

%disk = ['d:\globec\oc333\'];	% disk and sub-directory with the pingdata files
%disk = ' ';
filepart = 'pingdata.';	% first part of the ensemble files with the data

pgn = pgnum;		% starting pingdata file number

% Test times
%starttime = '1998/11/14-17:36:47';
%endtime = '1998/11/15-23:00:00';
%j_end = date2yd([98 11 15 23 0 0]);	
	
j_end = date2yd([str2num(endtime(3:4)) str2num(endtime(6:7)) str2num(endtime(9:10)) ...
	str2num(endtime(12:13)) str2num(endtime(15:16)) str2num(endtime(18:19))]);
%disp(j_end)

matfile = 'out_';			% root of output mat-files (4 chars or less)

% <<<<<<<<<<<<<<<<<<<<<< end editing  <<<<<<<<<<<<<<<<<<<<<<

disp(['Start time: ' starttime])
disp(['End time:  ' endtime])

gotit = 0;			% set the search indicators
ftgot = 0;

% setup null matrices
time1 = ones(0); 
u1 = ones(0); 
v1 = ones(0); 
pg1 = ones(0); 
nav1 = ones(0); 
amp1 = ones(0);
z1 = ones(0);
lbin1 = ones(0);
lpls1 = ones(0);
lblnk1 = ones(0);
nbins1 = ones(0);
tr_depth1 = ones(0);
bt_depth1 = ones(0);
 
% Start search for the desired data
     
if exist([matfile 'time.mat'])
        op_sys = pwd;
	if (op_sys(2) == ':')
	   eval(['!del ' matfile 'time.mat'])
	else
	   eval(['!\rm ' matfile 'time.mat'])
	end
end

while(gotit < 1)

        disp(pgn)
	fnum = num2str(pgn);
	if (length(fnum) == 1)  
		fnum = ['00' fnum];
	end
	if (length(fnum) == 2)
  		fnum = ['0' fnum];
	end

	filename = [disk filepart fnum];

	disp(['Opening ' filename])
	eval(['!bnl_p2m ' filename ' ' starttime ' ' endtime ' ' matfile]);

	eval(['load ' matfile 'time'])

	if (isempty(time))	% no data, pingdata too early
    
		pgn = pgn + 1;

	else			% got at least part of the data

		if (ftgot == 0)	% update the starting pingdata file number
			pgnum = pgn;
			ftgot = 1;
		end

		eval(['load ' matfile 'time']);

		eval(['load ' matfile 'u']);

		eval(['load ' matfile 'v']); 

		eval(['load ' matfile 'pg']); 

		eval(['load ' matfile 'nav']);	% Contains lat & lon

		eval(['load ' matfile 'amp']);

		eval(['load ' matfile 'conf1']);

		eval(['load ' matfile 'bot']);
                lbin = conf1(1,:);
		lpls = conf1(2,:);
		lblnk = conf1(3,:);
		nbins = conf1(4,:);
		tr_depth = conf1(5,:);
		bt_depth = bot(3,:);

 		u = u / 1000; 
		v = v / 1000;

		time1 = [time1, time];	% save what's been retrieved		
		nav1 = [nav1, nav];
       %         zc = lblnk+(lpls-lbin)/2+nbins;

		[no m] = size(u1);  [nn mn] = size(u);
		if (no == nn)
	%		z1 = [z1, zc];
			u1 = [u1, u]; 
			v1 =  [v1, v];
			pg1 = [pg1, pg];
			amp1 = [amp1, amp];
			lbin1 = [lbin1, lbin];
			lpls1 = [lpls1, lpls];
			lblnk1 = [lblnk1, lblnk];
			nbins1 = [nbins1, nbins];
			tr_depth1 = [tr_depth1, tr_depth];
			bt_depth1 = [bt_depth1, bt_depth];
		elseif (no < nn)
	%		z1 = [[z1;nan*ones(nn-no,m)], zc];
			u1 = [[u1;nan*ones(nn-no,m)], u];
			v1 = [[v1;nan*ones(nn-no,m)], v];
			pg1 = [[pg1;nan*ones(nn-no,m)], pg];
			amp1 = [[amp1;nan*ones(nn-no,m)], amp];
			lbin1 = [lbin1, lbin];
			lpls1 = [lpls1, lpls];
			lblnk1 = [lblnk1, lblnk];
			nbins1 = [nbins1, nbins];
			tr_depth1 = [tr_depth1, tr_depth];
			bt_depth1 = [bt_depth1, bt_depth];
		else
	%		z1 = [z1,[zc;nan*ones(no-nn,mn)]];
			u1 = [u1,[u;nan*ones(no-nn,mn)]];
			v1 = [v1,[v;nan*ones(no-nn,mn)]];
			pg1 = [pg1,[pg;nan*ones(no-nn,mn)]];
			amp1 = [amp1,[amp;nan*ones(no-nn,mn)]];
			lbin1 = [lbin1,lbin];
			lpls1 = [lpls1,lpls];
			lblnk1 = [lblnk1,lblnk];
			nbins1 = [nbins1,nbins];
			tr_depth1 = [tr_depth1,tr_depth];
			bt_depth1 = [bt_depth1, bt_depth];
  		end

		NPRFS = length(time);
		disp(['Block start-time: ' num2str(to_date(year,time(1)))])
		disp(['Block end-time: ' num2str(to_date(year,time(NPRFS)))])

		Mx_time = time(NPRFS);
		quit = j_end-15/(60*24);

		if (Mx_time < quit)   		% need to load another file

			pgn = pgn + 1;		% increment the file number
						% for another try

		else				% got all we need

			gotit = 1;

		end
	end
end

time = time1;
%zc = z1+trans_depth;
u = u1;
v = v1;
pg = pg1;
nav = nav1;
amp = amp1;
lbin=lbin1;
lpls=lpls1;
lblnk=lblnk1;
nbins=nbins1;
tr_depth=tr_depth1;
bt_depth=bt_depth1;

clear time1 z1 u1 v1 pg1 nav1 amp1 lbin1 lpls1 lblnk1 nbins1 tr_depth1 bt_depth1


		
