%[names,keys,deltimlm] = setup;   % ###Get processing info from user.
[r,c] = size(names);
[mfiles,n] = size(keys);       % mfiles is number of files
units = fill;
cmt   = fill;
datax = [];     %###
start = [];     %###
stop  = [];     %###
ii=[];jj=[];jl=[];
goodvals = [];
statdata = [];
k = 1;                  % variable sequence number

% This "for" loop reads reads the data into matrix "datax" and checks 
% the time data from each input file for good values (not NaN or infinity).
% Row indices into matrix "datax" for good times are written into matrix 
% "goodvals".If there are no good times, the entire file is flagged as 
% invalid in matrix "statdata", column 3.

for i = 1:mfiles              % i is file sequence number
	
	maxvars  = keys(i,2);
	filename = stripnam(names(keys(i,1),:));         
	start    = keys(i,3:8) ;                
	stop     = keys(i,9:14);
	units    = fill([' '],units);
	v1 = [];
	jd = [];

	for j = 1 : maxvars     % j is variable index
					
		varposn = keys(i,1) + j;
		varnam = stripnam(names(varposn,:));                 
		
%               if j ~= i + 1                
%                       [v1,jd] = getts(filename,varnam,start,stop);
%               else        

disp('keyboard #-1');
keyboard;
			[v1,jd,longnam,yewnits,lat,lon,idepth,wdepth]=...
			  getts(filename,varnam,start,stop);
			units = fill(yewnits,units);
%               end     %if j ~= i + 1 

%disp('keyboard #0');
%keyboard;              
		datax(1:length(jd),varposn) = v1';
		statdata(varposn,6) = k;

		k = k + 1;                

	end     % for j = 1 : maxvars

	datax(1:length(jd),keys(i,1)) = jd;
%disp('keyboard #1');
%keyboard;               

%       datax = bdx812;                 %###
%       jd = datax(:,keys(i,1));        %###

	ljd = length(jd);

	%Check time for good values (not (NaN or infinity))

	fin = find(finite(jd) & abs(jd)>2414989); 
				% fin is vector of row indices into "datax"
				% of "good" times in Julian days.

%       goodvals = zeros(r,ljd+1);

%disp('keyboard #1.1');
%keyboard;

	if isempty(fin) == 1
		statdata(keys(i,1),3) = 0;
		zfill = zeros(1,ljd + 1);
		goodvals(keys(i,1),1:ljd + 1) = zfill;
	else
		statdata(keys(i,1),3) = 1;
		goodvals(keys(i,1),1) = length(fin);
		goodvals(keys(i,1),2:length(fin)+1) = fin';
		statdata(keys(i,1),7) = ljd - length(fin);
	end     %if isempty(fin) == 1

	i = i + 1;

end     %for i = 1:mfiles---i is file sequence number

%disp('keyboard #2');
%keyboard;

% This "for" loop linearly interpolates incoming data wherever there are
% bad points (except end points for which extrapolation would be necessary).
% First, every variable not already disqualified on the
% basis of invalid times is examined for good data (not NaN or infinity) and 
% all outliers among the remaining points are determined. If there are no 
% good data for a given variable, the variable is flagged as invalid in 
% matrix "statdata", column 3.

for i = 1:mfiles        % i is file sequence number

	maxvars = keys(i,2);

	if statdata(keys(i,1),3) == 1   % There are good times

		goodcols = goodvals(keys(i,1),1);    % number of good times

		for j = 1 : maxvars     % j is variable sequence    
						% no. for file i only

			% Write data for current variable into first column
			% of matrix origvrbl. Corresponding datax indices 
			% go into second column.

			varposn = keys(i,1) + j;
			origvrbl = [];

			origvrbl(:,1) = datax(goodvals(keys(i,1),...
					2:goodcols + 1),varposn);
			origvrbl(:,2) = (goodvals(keys(i,1),2:goodcols+1))';
			lenorig  = length(origvrbl(:,1));

			% Find indices of good data entries (not(NaN or 
			% infinity)) and store in jj  
	
			ii = finite(origvrbl(:,1));
			jj = find(ii);  % These are row indices in origvrbl
					% for rows having finite variable
					% values in col 1 and corresponding
					% datax indices in col 2.

%disp('keyboard #2.01');
%keyboard;
			if isempty(jj) == 0

				jl = length(jj);
%disp('keyboard #2.1');
%keyboard;
				numbad = lenorig - jl;
				statdata(varposn,8) = numbad;

				% Check for outliers

				mv    = mean(origvrbl(jj,1));
				sigma =  std(origvrbl(jj,1));
				e     = ones(jl,1);
				dist  = abs(origvrbl(jj,1)-e*mv); 
				outliers = dist>3*e*sigma; 
				
				% "outliers" is a column vector of 0's and
				% 1's which is jl=length(jj) elements long. 
				% "outliers(1)" refers to element jj(1) 
				% in origvrbl, etc. Find the elements of 
				% "outliers" which are in fact outliers;i.e. 
				% which equal 1, and then find the index into 
				% origvrbl.
       
				kk = find(outliers);    % kk are indices 
							% into jj

				if isempty(kk) == 0
					lkk=length(kk);
					statdata(varposn,9) = lkk;
				else
					statdata(varposn,9) = 0;
				end     % if isempty(kk) == 0

				%jj(kk) are indices of outliers into origvrbl.
				% Therefore must delete all elements kk of jj. 

				jj(kk) = [];

%disp('keyboard #2.2');
%keyboard;

				% jj now contains only indices into origvrbl 
				% which correspond 
				% to good data that are not outliers.
				% Recalculate the length of jj

				jlnew = length(jj);

				% Define matrix goodpts which has only good 
				% times in first column, good data with no 
				% outliers for current variable in second 
				% column, and the common datax row indices 
				% for both in the third column.

				goodvals(varposn,1) = jlnew;
				goodvals(varposn,2:jlnew + 1) =...
					(origvrbl(jj,2))';
				goodpts = [];

				% Write times.

				goodpts(:,1) =... 
				datax(goodvals(varposn,2:jlnew + 1),...
					keys(i,1));
		
				% Write variable

				goodpts(:,2) =... 
				datax(goodvals(varposn,2:jlnew + 1),varposn);

%disp('keyboard #2.21');
%keyboard;
				% Write time/data indices

				goodpts(:,3) =...
				(goodvals(varposn,2:jlnew + 1))';

%disp('keyboard #2.3');
%keyboard;
				% Define vector "timeind" holding datax
				% indices to good times and "varind" holding
				%datax indices to good variables.

				numtims = goodvals(keys(i,1),1);
				numvars = goodvals(varposn,1);
				timeind = goodvals(keys(i,1),2:numtims+1);
				varind  = goodvals(varposn,2:numvars+1);

				% Delete elements of timeind having values
				% which lie outside the range of indices for
				% the current variable.

				ends = find((timeind < varind(1)) |...
					    (timeind > varind(numvars)));

				if isempty(ends) == 0
					timeind(ends) = [];
				end     % if isempty(ends) == 0

				% Determine time indices from matrix goodpts
				% pointing to successive times that are
				% separated by a time interval greater than
				% the interpolation limit deltimlm specified 
				% by the user. Put the lower and upper time
				% indices in elements (rr,1) and (rr,2) of
				% matrix "d".

				h = []; d = [];
				h = find(diff(goodpts(:,1)) >  deltimlm);
%disp('keyboard #2.31');
%keyboard;              

				if isempty(h) == 0  %There are time intervals
						    %to be excluded from the
						    %interpolation.
					d = [goodpts(h,3)  goodpts(h+1,3)]; 
					
					% Record time interval boundaries in 
					% matrix "interval" with row indices 
					% like "goodvals" and "statdata." 
					% First column is number of intervals 
					% (limit pairs) to follow in that row 
					% in successive columns.

					[rd,cd] = size(d);
					rid = [];
					interval(varposn,1) = rd;
					interval(varposn,2:rd*cd + 1) =...
						reshape(d',1,rd*cd);
%disp('keyboard #2.32');
%keyboard;              

					% Determine elements of timeind 
					% holding indices to times that are 
					% within intervals in which interpo-
					% lation is not to be done.

					% Prepare mask "discard"
					discard = zeros(1,length(timeind)); 

					for q = 1:rd

						rid = find(timeind>d(q,1) &...
							timeind<d(q,2));

						if isempty(rid) == 0
							discard(rid) = ...
							    discard(rid)+1;
						end   % if isempty(rid) == 0

					end     % for q = 1:rd
%disp('keyboard #2.33');
%keyboard;              

					% Delete elements of timeind holding 
					% indices to times that are within 
					% intervals in which interpolation is 
					% not to be done.

					timeind(discard) = [];

				else

					interval(varposn,1) = 0;

				end     %if isempty(h) == 0

				% Create time vector for times desired from
				% MATLAB interpolation function "table1".

				filltim = datax(timeind,keys(i,1));
%disp('keyboard #2.34');
%keyboard;              

				% Store start and stop times in columns 1 & 2 
				% respectively of matrix "statdata" 
				% for later use.
       
				lfill = length(filltim);
				statdata(varposn,1) = filltim(1);
				statdata(varposn,2) = filltim(lfill); 
				interped = lfill - jlnew;
				statdata(varposn,7) = interped;

%disp('keyboard #2.4');
%keyboard;
				% Do the linear interpolation. Note that there 
				% is no attempt to extrapolate beyond the set 
				% of good points.

				if lfill > 2

					%*** FIRST OF TWO LINEAR INTERPOLATIONS
					% IN THE SCRIPT - THIS ONE ON INCOMING
					% DATA ***

					tabled = table1(goodpts(:,1:2),...
							filltim);
					statdata(varposn,3) = 1;

%disp('keyboard #2.5');
%keyboard;
					% Write revised datax matrix with 
					% interpolated points replacing 
					% spurious and outlying data. 
					 
					datax(timeind,varposn) = tabled;

					% Revise list of indices of good 
					% variables in "goodvals" to include 
					% interpolated points. Note that this 
					% adds to indices of original data 
					% points upon which the interpolation 
					% was based.

					[ms,ns] = size(goodvals);
					ciphers = zeros(1,ns-1);
					goodvals(varposn,1) = lfill;
					goodvals(varposn,2:ns) = ciphers;
					goodvals(varposn,2:lfill + 1) =...
						timeind;

				else
					fprintf(['\nInsufficient good '],...
					['data for variable %g in file %g']...
						,j,i) ;
					statdata(varposn,3) = 0;
				end     %if lfill > 2

			else    % No finite value. Can't interpolate.

				statdata(varposn,3) = 0;
				statdata(varposn,8) = lenorig;

			end     %if isempty(jj) == 0

		end     % for j = 1 : maxvars 
		
		i = i + 1;

	else    %No good times;therefore, linear interpolation is not done.
		% Write a zero in time flag column of statdata 

		statdata(varposn,3) = 0;
		
		i = i + 1;

	end     % if statdata(keys(i,1),3) == 1 --- There are good times

end     % for i = 1:mfiles --- i is file sequence number

%disp('keyboard #3');
%keyboard;

% Data have now been adjusted for spurious values and outliers. Now must
% issue a report to enable user to choose interpolation parameters.

repeat = 'y';

while repeat == 'y'

	ans = input(['Do you want a report before entering desired ',...
		'lineup time requirements? y/n:'],'s');

	if ans ~= 'y' & ans ~= 'n'
		fprintf(['\nMust answer ''y'' or ''n'',',...
		'observing case. Retry.']);
		repeat = 'y';
	else
		repeat = 'n';
	end     %if ans ~= 'y' & fileans ~= 'n'

end     %while repeat == 'y'

if ans == 'y'

	repeat = 'y';

	while repeat == 'y'

		kt = 0;         % Initialize line counter for display.

		for i = 1:mfiles

			filename = stripnam(names(keys(i,1),:));

			if statdata(keys(i,1),3) == 1

				maxvars = keys(i,2);
				badtims = statdata(keys(i,1),7);
				kt=kt+1;if kt>24;kt=0;pause;end;
				fprintf('\n');
				kt=kt+1;if kt>24;kt=0;pause;end;
				disp(['FILE: ' filename]);
				kt=kt+2;if kt>24;kt=0;pause;end;
				fprintf(' Number of bad times: %g\n\n',...
					badtims);

				for j = 1:maxvars

					varposn = keys(i,1) + j;
					varnam = stripnam(names(varposn,:));
					kt=kt+1;if kt>24;kt=0;pause;end;
					disp(['  VARIABLE: ' varnam]);

					if statdata(varposn,3) == 1

						startim = gregorian...
						 (statdata(varposn,1));
						stoptim = gregorian...
						 (statdata(varposn,2));
						nonfinit =... 
						 statdata(varposn,8);
						outliers =... 
						 statdata(varposn,9);
						interped =... 
						 statdata(varposn,7);
						kt=kt+1;if kt>24;kt=0;...
						 pause;end;
						disp(['    start time ',...
						 'after initial edit:']);          
						kt=kt+2;if kt>24;kt=0;...
						 pause;end;
						disp(startim);
						kt=kt+1;if kt>24;kt=0;...
						 pause;end;
						disp(['    stop time ',...
						 'after initial edit:']);          
						kt=kt+2;if kt>24;kt=0;...
						 pause;end;
						disp(stoptim);
						kt=kt+2;if kt>24;kt=0;...
						 pause;end;
						fprintf(['    nonfinite ',...
						'points:%g ','    outlie',...
						'rs:%g ','    total inte',...
						'rpolated ','points:',...
						'%g \n\n'],nonfinit,...
						outliers,interped);

					else

						kt=kt+1;if kt>24;kt=0;...
						 pause;end;
						disp(['  INSUFFICIENT ',...
						 'GOOD DATA FOR THIS ',...
						 'VARIABLE']);

					end     % if statdata(varposn,3) == 1

				end     % for j = 1:maxvars

			else

				kt=kt+1;if kt>24;kt=0;pause;end;
				disp(['  INSUFFICIENT GOOD TIMES FOR ',...
				 'THIS FILE']);

			end     % if statdata(keys(i,1)) == 1

		end     % for i = 1:mfiles

		rpt = 'y';

		while rpt == 'y'

			kt=kt+1;if kt>24;kt=0;pause;end;
			repeat = input(['Do you want to review this ',...
			'report? y/n: '],'s');

			if repeat ~= 'y' & repeat ~= 'n'
				fprintf(['\nMust answer ''y'' or ''n'',',...
				'observing case. Retry.']);
				rpt = 'y';
			else
				rpt = 'n';
			end     % if repeat ~= 'y' & repeat ~= 'n'

		end     % while rpt == 'y'

	end     % while repeat == 'y'

end     % if ans == 'y'

% Ask user to enter overall start and stop times, and desired time interval.

askagain = 'y';

while askagain == 'y'

	repeat = 'y';

	while repeat == 'y'

		repeat = 'n';

		start = input(['Type start time [yyyy mm dd hr mi sc]',...
		10,'  for output file: ']);

		if length(start) ~= 6
			disp('Retry---'); 
			repeat = 'y';
		end     %if length(start) ~= 6

	end     %while repeat == 'y'

	repeat = 'y';

	while repeat == 'y'

		repeat = 'n';

		stop = input(['Type stop time [yyyy mm dd hr mi sc]',...
		10,'  for output file: ']);

		if length(stop) ~= 6
			disp('Retry---'); 
			repeat = 'y';
		end     %if length(stop) ~= 6

	end     %while repeat == 'y'

	if julian(stop) <= julian(start)
		disp('Stop time must be after start time. Retry---');
		askagain = 'y';
	else
		askagain = 'n';
	end     % if julian(stop) <= julian(start)

end     % while askagain == 'y'

repeat = 'y';

while repeat == 'y'

	repeat = 'n';

	deltat = input('Type time interval in hours: ');

	if isempty(deltat) == 1
		disp('Must enter time interval in hours. Retry...'); 
		repeat = 'y';
	end     % if isempty(deltat) == 1

end     %while repeat == 'y'

%disp('keyboard #3.01');
%keyboard;              

% Start and stop times are stored in "start" and "stop" respectively in 
% [yyyy mm dd hr mn sc] format. Desired time interval in hours is stored
% in deltat. The interpolation will be done over the span of "good" data 
% for each variable. 
% Start and stop times for each variable are stored in matrix "statdata",
% whose columns are similar to matrix "names",  so that the first two
% columns of "keys" can be used to identify the row indices.
% These start and stop times are stored in columns 1 and 2 of statdata
% respectively. They are in Julian days.

% Construct a vector of desired times. Each variable will generally
% be interpolated over a subset of these times, but all data will be
% referenced to this same time scale. 

jdstart = julian(start);
jdstop  = julian(stop) ;
deltim  = deltat/24;
numdel  = fix((jdstop-jdstart)/deltim); 

% "timswant" is vector of desired times

timswant= (jdstart:deltim:(jdstart+numdel*deltim));

%disp('keyboard #3.02');
%keyboard;              

% The following "for" loop determines the times in "timswant" for which 
% linear interpolation will be done.

for i = 1:mfiles        % i is file sequence number

	maxvars  = keys(i,2);

	if statdata(keys(i,1),3) == 1   % There are good times

	    for j = 1 : maxvars     % j is variable sequence no.
				    % for file i only

		  varposn  = keys(i,1) + j;

		  if statdata(varposn,3) == 1 % There are good variable values

			% Get start and stop times for variable k in file i
							
			starttim = statdata(varposn,1);
			stoptim  = statdata(varposn,2);
			lentwant = length(timswant);

			% Determine timswant indices for timswant values
			% which will be disqualified for interpolation either
			% because they lie outside the interval bounded by
			% starttim and stoptim or because they lie within
			% blocked-out time intervals defined by the user.

			rd = interval(varposn,1);  % number of user-blocked 
						   % intervals

			keep = ones(1,lentwant);   % Prepare mask 
			rid = [];
%disp('keyboard #3.03');
%keyboard;              

			if rd ~= 0      % There are user-blocked intervals

				for q = 1:rd

					dlower = datax...
						(interval(varposn,2*q),...
								keys(i,1));
					dupper = datax...
						(interval(varposn,2*q+1),...
								keys(i,1));
					rid = find...
					 ((timswant<starttim | ...
					   timswant>stoptim) | ...
					  (timswant>dlower   & ...
					   timswant<dupper )); 
%disp('keyboard #3.031');
%keyboard;
					if isempty(rid) == 0

						keep(rid) = 0*keep(rid);

					end     % if isempty(rid) == 0                     
					
				end     % for q = 1:rd

				statdata(varposn,3) = 1;
				statdata(varposn,4) = min(find(keep));
				statdata(varposn,5) = max(find(keep));

				% Store indices "keep" in matrix twant with 
				% same organization as "goodvals." 

				ltwkeep = length(keep);
				twant(varposn,1) = ltwkeep;
				twant(varposn,2:ltwkeep+1) = keep;
%disp('keyboard #3.04');
%keyboard;              
			else

				rid = find(timswant<starttim | ...
					   timswant>stoptim);
				  
				if isempty(rid) == 0

					keep(rid) = 0*keep(rid);

				end     % if isempty(rid) == 0 

				statdata(varposn,3) = 1;
				statdata(varposn,4) = min(find(keep));
				statdata(varposn,5) = max(find(keep));

				% Store indices "keep" in matrix twant with 
				% same organization as "goodvals." 

				ltwkeep = length(keep);
				twant(varposn,1) = ltwkeep;
				twant(varposn,2:ltwkeep+1) = keep; 
				  
			end     % if rd ~= 0 -There are user-blocked intervals
%disp('keyboard #3.05');
%keyboard;              

			if isempty(find(keep)) == 1 
				fprintf(['Desired times are not within ',...
					 'available time interval for ',...
					 'variable %g in file %g'],j,i);
				% Store "0" flag in 3rd col of matrix 
				% statdata to indicate invalid time

				statdata(varposn,3) = 0;

			end     % if isempty(find(keep)) == 1

		  end     % if statdata(varposn,3) == 1

	    end     % for j = 1 : maxvars

	end     % if statdata(keys(i,1),3) == 1   % There are good times

	i = i + 1;

end     %for i = 1:mfiles

%disp('keyboard #3.1');
%keyboard;

% Will now construct output array "dataout" of interpolated data.
% First col will contain maximum span of desired times achieved for
% all variables, all files. Successive columns will contain the
% corresponding data for all variables for which data could be
% extracted. Sequence numbers for all variables appear
% in array "statdata", col 6. These can be used to identify 
% correspondence between columns and variables in "dataout".
  
% Determine overall achievable min & max indices of timswant vectors  

vald = statdata(:,3); % vector of 0's & 1's indicating valid files & variables

% Set valid time flag for files to zero.

for i = 1:mfiles;
	vald(keys(i,1)) = 0;  
end     %for i = 1:mfiles;

validind = find(vald);  % validind is vector of rows of statdata which 
			% have variables with achievable start & stop times
%disp('keyboard #4');
%keyboard;

indstart = min(statdata(validind,4));   % minimum start time index
indstop  = max(statdata(validind,5));   % maximum start time index
disp(' OVERALL START TIME:')
disp(gregorian(timswant(indstart)));
disp(' OVERALL STOP TIME:')
disp(gregorian(timswant(indstop)));

% Initialize output matrix dataout.

dataout = [];

% Find number of output points (same for all variables)

numgudpt = indstop - indstart + 1;

%disp('keyboard #4.1')
%keyboard;

% Linearly interpolate all variables pointed to by vector validind
% First, put maximum span of wanted & achievable times in column 1 of output 
% matrix "dataout"

ltwant = length(timswant);
dataout(1:ltwant,1) = timswant';

k = 0;  % Initialize variable sequence number for output matrix 

for i = 1:mfiles

	if i <= mfiles - 1

		% Define vector "ival" to have qualifying row indices for
		% variables associated with the current file.("validind" has
		% all rows of statdata with achievable start & stop times.)

		ival = find(validind > keys(i,1) &... 
			    validind <= keys(i,1) + keys(i,2));
	else
		ival = find(validind > keys(i,1));
	end     % if i <= mfiles - 1

	if isempty(ival) == 0

		timecol = keys(i,1);
		valrows = validind(ival);
		lvalrows = length(valrows);

%disp('keyboard #4.11')
%keyboard;
		for j = 1:lvalrows

			thisrow = valrows(j);
			nvals = goodvals(thisrow,1);
			
			tablein = [];

			tablein(1:nvals,1) =... 
				datax(goodvals(thisrow,2:nvals + 1),timecol);
			tablein(1:nvals,2) =...
				datax(goodvals(thisrow,2:nvals + 1),thisrow);

			k = k + 1;

			%  Write NaN's for all locations in current column of 
			% output matrix "dataout." Valid values will 
			% overwrite them as appropriate.

			dataout(1:ltwant,k+1) =... 
				NaN * ones(ltwant,1);
%disp('keyboard #4.2');
%keyboard;
			numwant = twant(thisrow,1);
			keep = twant(thisrow,2:numwant+1);
			timeterp = timswant(keep);

			%*** SECOND OF TWO LINEAR INTERPOLATIONS IN THE 
			% SCRIPT: THIS ONE OVER DESIRED TIMES *** 
			
			dataout(keep,1+k) = table1(tablein,timeterp');

		end     % for j = 1:length(valrows)

	end     % if isempty(ival) == 0

end     % for i = 1:mfiles

%disp('keyboard #5');
%keyboard;

% Delete rows of "dataout" which are out of the range of overall
% achievable start and stop times.

[dor,doc] = size(dataout);

if dor ~= ltwant
	disp('!!! LOGICAL ERROR 1 IN FORMING DATAOUT');
	pause;
end     % if dor ~= ltwant

if indstop < indstart
	disp('!!! LOGICAL ERROR 2 IN FORMING DATAOUT');
	pause;
end     % if indstop < indstart

%if indstop <= ltwant - 1
%       dataout(indstop+1:ltwant,:) = [];
%end     % if indstop <= ltwant - 1

%if indstart > 1
%       dataout(1:indstart-1,:) = [];
%end     % if indstart > 1

seq = 0;
outnames = names;        

% "delv" will be a vector of rows of "outnames" and "units" to delete.
% Rows of "delv" are initially organized like "names."
% Since only valid variables are wanted, all rows corresponding to files will
% be deleted, as well as unavailable variables associated with good files.

delv = [];
	
for i = 1:mfiles
		
	seq = seq + 1;
	delv(seq,:) = keys(i,1);
	maxvars = keys(i,2);    
		
	if statdata(keys(i,1),3) == 1

		for j = 1:maxvars
				
			varposn = keys(i,1) + j;

			if statdata(varposn,3) == 1
				startout = gregorian...
					(timswant(indstart)); 
			else
				seq = seq + 1;
				delv(seq,:) = varposn;
			end     % if statdata(varposn,3) == 1

		end     % for j = 1:maxvars

	else
			
		for j = 1:maxvars
			varposn = keys(i,1) + j;
			seq = seq + 1;
			delv(seq,:) = varposn;
		end     % for j = 1:maxvars          
		
	end     % if statdata(keys(i,1)) == 1
	
end     % for i = 1:mfiles

outnames(delv,:) = [];
units(delv,:)    = [];
tmout = deltat*3600;
%cmt = ['Comment handling to be determined - Gerry Martineau'];
[kr,kc] = size(dataout);
dout = dataout(:,2:kc);
cdfnam =input ('Type a name without quotes for the netCDF output file:','s');

disp(' Enter comment text after ''text'' prompt (Don''t enclose in quotes).'); 
disp([' Hit ''Enter'' before end of each line to continue or end input,',...
	10,'  but don''t start a new line with a blank.']); 

moretext = 'y';
%cmt = fill([' '],cmt);

while moretext == 'y'

	words = input('text:','s');
	cmt = fill(words,cmt);
	repeat = 'y';

	while repeat == 'y'

		moretext = input(['More text? y/n:'],'s');

		if moretext ~= 'y' & moretext ~= 'n'
			fprintf(['\nMust answer ''y'' or ''n'',',...
			'observing case. Retry.']);
			repeat = 'y';
		else
			repeat = 'n';
		end     %if ans ~= 'y' & fileans ~= 'n'

	end     %while repeat == 'y'

end     % while moretext == 'y'

%disp('outkb #1');
%keyboard;

create_ts(cdfnam,dout,outnames,lat,lon,startout,tmout,...
	wdepth,idepth,units,cmt');

disp('End of ''lineup'' script.');
