function [DATA,TAPE] = ts_view(TAPE,Jday,tlims,ith,iichan,ii_view);
% View time series in view5
% Call: [DATA,TAPE] = ts_view(TAPE,Jday,tlims,ith,iichan,ii_view);
% TAPE:  from ts_open
% Jday:  doy
% tlims: [start_hour end_hour]
% ith: return every ith sample
% iichan:  channels
% ii_view:  1=view in view5
% DATA: view5 data structure
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

if isempty(iichan)
	iichan=1:TAPE.nchan;
end
nchan=length(iichan);

[TAPE]=ts_tpos(TAPE,Jday,tlims(1));
if (TAPE.status <= 0)
    disp(['ts_tpos error ',num2str(TAPE.status)])    
    return
end
dt=1/TAPE.fs;
nsampout=round( (tlims(2)-tlims(1))*3600/dt/ith );
Dhour_rec = (TAPE.nsamp-1)*dt/3600;
inc=floor(TAPE.nsamp/ith);
hour_scale=linspace(0,Dhour_rec,inc);
pser=zeros(nchan,nsampout);
scales{2}=zeros(nsampout,1);

ndx=1;
while TAPE.Dhour < tlims(2)
    
    switch TAPE.array_type,
    case 'User Defined' % 1
        [pser1,TAPE] = ud_onerec(TAPE);
    case 'SWAMI' % 2
        [pser1,TAPE] = swami_onerec(TAPE);
    case 'SW_OMNI' % 3
    case 'CM' % 4
    case {'HLAB','HLA-B'} % 5
        [pser1,TAPE] = hlab_onerec(TAPE);
    case 'N/A' % 6
        error('tms:  N/A data type no longer implemented')
    case 'MSS Monitor Phone' % 7
        error('tms:  MSS Monitor Phone data type no longer implemented')
    case {'DVLA1'} % 8
    case {'DVLA2'} % 9
    case 'RSP-2001' % 10
    case {'DVLA2002_OMNI';'DVLA2002_DIFAR';'DVLA2002_ALL'} % 11, 12, 13
        [pser1,TAPE] = dvla02_onerec(TAPE);
    case {'TVLA'} % 14
        [pser1,TAPE] = tvla_onerec(TAPE);
    case {'TEAC'}
        [pser1,TAPE] = teac_onerec(TAPE);
    otherwise
        disp(['ts_view:  array type unknown = ' TAPE.array_type])
    end

	try
        pser(1:nchan,ndx:ndx+inc-1)=pser1(iichan,1:ith:TAPE.nsamp);
    catch
        break;
    end
    scales{2}(ndx:ndx+inc-1)=hour_scale+TAPE.Dhour;
    ndx=ndx+inc;
end
pser=pser(:,1:ndx-1);
scales{2}=scales{2}(1:ndx-1);

labels{1}='channel number';
labels{2}=['hour of day - ' num2str(Jday)];
scales{1}=iichan;
dlab='A_D count';
title=TAPE.data_file;
iidB=0;
iifig=0;
DATA=mat2data(pser,labels,scales,dlab,title,iidB,iifig);
if ii_view
    DATA=view5(DATA);
end
