%  TSplot is a simple driver program for time series plotting.
%  It prompts the user for a data file, in one of several formats,
%   reads this file, and initializes the time series plotting interface.
%
%  First a couple of options which are hard-coded here:
decFac = 1; 

%  return full data set (no decimation);scale into 
scale = 1; %if scale == 1 data are returned with units of nT and mV
           %   note: not divided by dipole length!
%  physical units (but no other filter corrections applied)

tUnits = 'Hours';
%   tUnits = initial time units for labeling time series plots
%     other choices are 'Seconds','Minutes','Days'

nTunits = 6;
%   Number of Hours (more generally, tUnits) to plot on one pag3e

%   Assumed sampling rate (used to calculate number of points in
%   the plotting window only ... should recompute after reading file)
dt = 1;

%   initialize structure "plotStart"
[plotStart] = plotSetup(tUnits,nTunits,dt)

%  this program just reads one file, obtained by browsing
%   load one NIMS data  ... 
[filename, pathname, filterindex] =     ...
        uigetfile({'*.bin','NIMS bin files'; ...
	'*.dbn','decimated NIMS bin files';...
        '*.mat', 'mat files'; ...
        '*.asc', 'ascii file with start time'}, ...
        'Data File to Plot');

FileNames{1} = filename;
eval(['cd ' pathname])

if filterindex==4
   [y,dt,clock_zero,data_start] = readTSasc(filename);
   [nch,npts] = size(y);
   t0 = datenum(data_start)-datenum(clock_zero);
   t0 = round(t0*24*3600/dt);
   switch nch
      case 3
         ch = ['Hx';'Hy';'Hz'];
      case 5
         ch = ['Hx';'Hy';'Hz';'Ex';'Ey'];
      otherwise
         ch = [];
         for ich = 1:nch
            ch = [ ch ; 'ch' num2str(ich)];
         end
      end
    temp = strsplit(filename,'.');
    sta = [];
    for k = 1:nch
       sta =  [sta; temp{1}];
    end
    %   set up dummy unit system response
    freq = [1,1e-6]./dt;
    [SysTF] = setUnitSysTF(nch,freq);
else
   %  new version of readTSnims can now handle mat files also ...
   [y,SysTF,dt,t0,ch,sta,error] = readTSnims(FileNames,decFac,scale);
end

%  reset nPw (# of points to plot initially on one page) now
%   that dt has been read from data file  (NOTE: dt returned
%   by readTSnims accounts for subsampling by a factor of decFac)
nPw = fix(plotStart.tFac*plotStart.nTunits/dt);
plotStart.nPw = nPw;

%   Initialize cell arrays for data, time series metadata, 
%     window metadata, etc.
TSc = {};
TSp = {};
TSd = {};
TSw = {};
TSdata = {};
TSfigInd = zeros(100,1);

%   set up TSd
TSd1 = setTSd(y,t0,dt,ch,sta,SysTF);
TSd1.sta = char(TSd1.sta);
TSdata0 = (TSd1.range(:,1)+TSd1.range(:,2))/2;
rng = (TSd1.range(:,2)-TSd1.range(:,1))/2;
rng = plotStart.rangeScale*rng;
TSd1.range(:,1:2) = [ TSdata0-rng TSdata0+rng];

%   are these needed???
TSd1.DataDir = pathname;
TSd1.arrayID = '';

%   copy into cell arrays
iA = 1
TSdata{iA} = y;
TSd{iA} = TSd1;

[hTSw,TSw1] = plotTSwin(TSd{1},plotStart);
TSfigInd(hTSw) = 1;
i0 = TSw1.i0; i1 = TSw1.i1; di = TSw1.di;
yFac = TSw1.yFac;
ddt = TSd{1}.dt/TSw1.tFac;
t0 = (TSd{1}.t0)*(1-TSw1.t0Eq0)/TSw1.tFac;
I = [i0:di:i1];
[TSw1] = plotPage(ddt*I+t0,...
                yFac*TSdata{1}(:,I),...
                TSw1,TSd{1},1);
TSw{1} = TSw1;
