function mcs_act(action,cback_fig)
%
%
% 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.

mcs_global
view_global
%
if nargin<2, cback_fig=gcbf; end
%
if isempty(cback_fig), cback_fig=MBOX.figno; end
hval=findobj(cback_fig,'Tag',action);
hlab=findobj(cback_fig,'Tag',[action,'lab']);
val=get(hval,'Value');
str=get(hval,'String');
tip=get(hval,'TooltipString');
% If Tag has been set to 'HELP' by a previous click on 'HELP', call mcs_help and return:
if strcmp(tip,'help'),
	sty=get(hval,'Style');
% If getting help on a checkbox, value will have toggled, so set it back the way it was:
	if strcmp(sty,'checkbox'), set(hval,'Value',~val); end
	mcs_help(action,cback_fig); return; 
end
%
switch action
case 'AN Frequency Band(s)',
    MCS.AN_freq=str2num(char(str));
    if length(MCS.AN_freq(:))<=1 & ~isempty(FFT) & length(MCS.TL_freq(:))>=2,
        MCS.AN_freq=[FFT.f1,MCS.TL_freq(1); MCS.TL_freq(2),FFT.f2];
        set(hval,'String',sprintf('%1.1f,%1.1f\n',MCS.AN_freq'));
    end    
case 'AN Files',
	if no_files(val,str,'AN')
        return;
    end
	MCS.AN_root=MCS.AN_files{val};
case 'Array Geometry Files';
	if no_files(val,str,'Array Geometry'), return; end	
	MCS.geom_file=MCS.geom_files{val};
	[MCS.xyz_all,MCS.cavg,MCS.nph_geom]=arr_geom([MCS.nav_dir,MCS.geom_file]);
case 'Avg over phone',
	MCS.ii_phavg=val;
case 'Azimuthal Angles',
	MCS.phi_lim=str2num(char(str));
case 'Baseband Freq (0=def)',
    vals=str2num(char(str));
    if length(vals(:))>1, error(['Illegal Baseband Freq: must be single element or []: ',num2str(vals(:)')]); end
    MCS.f_bb=vals;
case 'Batch Mode'
    MCS.batch=val;
case 'Beamformer',
	MCS.iibf_type=val; MCS.iiabf=(MCS.iibf_type>0);
	h0=findobj(cback_fig,'Tag','Exp:+a/-T10dB');
	h1=findobj(cback_fig,'Tag','WN Gain(-3,-6)');
	h2=findobj(cback_fig,'Tag','DMR: D');
	switch val
	case 1,
		set([h0,h1,h2],'Visible','off');
	case 2,
		set(h0,'Visible','on'); set([h1,h2],'Visible','off');
	case 3,
		set([h0,h1],'Visible','on'); set([h2],'Visible','off');
	case 4,
		set([h0,h1,h2],'Visible','on');
	end
case 'BW: nbin/-Hz',
	MCS.rvec_bw=str2num(char(str));
case 'Byte Swap'
    MCS.TAPE.iibyteswap=val;
    MCS.TAPE.byteswap=(MCS.TAPE.iibyteswap-1)*MCS.TAPE.nbyte_sample;
case 'Cal Files',
	MCS.TAPE.cal_root=[MCS.dir MCS.TAPE.cal_files{val}];
case 'Cmin,Cmax',
	MCS.clim=str2num(char(str));
case 'Combine CTS Files',
	if ~over_write(MCS.out_root,'.cts'), return; end
    mcs_save_run([MCS.out_root,'_CTS']);
    cts_files=MCS.comb_files(1:MCS.comb_parms(1));
    new_levels=MCS.comb_levels(2,1:MCS.comb_parms(1));
    t_offsets=MCS.comb_offsets(1:MCS.comb_parms(1));
    cts_combine(cts_files,MCS.out_root,new_levels,t_offsets);
	if MCS.mc_cts2fft,
        cts_root=MCS.out_root; fft_root=cts_root;
		if ~over_write(fft_root,'_fft'), return; end
		[LGRAM,FFT]=cts2fft(cts_root,fft_root,MCS.cts_parms(1),MCS.cts_parms(2), ...
			MCS.cts_parms(3),[],MCS.freq_bands,[],MCS.cts_parms(4),0);
	end	
case 'Compute AN',
    MCS.ii_AN=val;
    gui_box_vis(cback_fig,'AN Frequency Band(s)',MCS.ii_AN);
case 'Compute SL',
    MCS.ii_SL=val;
    gui_box_vis(cback_fig,{'SL Frequency Band(s)','Dropout dB,T_filt (min)'},MCS.ii_SL);
    if MCS.ii_SL,       % Make Compute SL and Compute TL mutually exclusive;
        h1=findobj(cback_fig,'Tag','Compute TL'); set(h1,'Value',0); mcs_act('Compute TL');
    end
case 'Compute TL',
    MCS.ii_TL=val;
    gui_box_vis(cback_fig,{'TL Frequency Band(s)','SL (-1=sim)','Use SL File','SL Files'},MCS.ii_TL);
    if MCS.ii_TL,       % Make Compute SL and Compute TL mutually exclusive;
        h1=findobj(cback_fig,'Tag','Compute SL'); set(h1,'Value',1-MCS.ii_TL); mcs_act('Compute SL');
    end
    if MCS.ii_TL, mcs_act('Use SL File'); end
case 'Copy Data to Mdat.xx',
    MCS.v_opts(2)=val;
case 'Correct for AN',
    MCS.iiAN_corr=val;
    if val, gui_box_vis(cback_fig,'AN Frequency Band(s)',val); end
case 'Create New Fig',
	MCS.v_opts(1)=val;
case 'CTS Files';
	if no_files(val,str,'CTS'), return; end
	MCS.cts_root=MCS.cts_files{val};
	CTS=cts_open([MCS.dir MCS.cts_root]); MCS.nph_all=CTS.nrec;
	MCS.Tlims=CTS.T_lims; %%MCS.Tlims,
	h1=findobj(cback_fig,'Tag','CTS nrec,fs,f_bb');
	str=sprintf('%1i,%1.1f,%1.1f',[CTS.nrec,CTS.fs,CTS.f_bb_shift]); set(h1,'String',str);
	h1=findobj(cback_fig,'Tag','Day,Hour');
	ddhh1=[CTS.Jday CTS.T_lims(1)]'; ddhh2=[CTS.Jday CTS.T_lims(2)]';
	str=sprintf('%3.3i,%3.3f\n',[ddhh1,ddhh2]); set(h1,'String',str);
	mcs_act('Phone Numbers');
case '# CTS Files to Combine',
    nfile=str2num(char(str));
    if length(nfile)>1 | round(nfile)~=nfile | nfile<1 | nfile>MCS.comb_parms(2), 
        error('USER ERROR: # CTS Files to Combine must be a single integer between 1 and 7!!!');
    end
    MCS.comb_parms(1)=nfile;
    for jcomb=1:MCS.comb_parms(2),
        if jcomb<=MCS.comb_parms(1), iivis=1; else, iivis=0; end
        tag1=sprintf(['CTS File ',int2str(jcomb)]); tag2=[tag1,'label'];
        gui_box_vis(cback_fig,{tag1,tag2},iivis);
        if iivis==1, mcs_act(tag1); end
        tag1=sprintf(['CTS Level ',int2str(jcomb)]); tag2=[tag1,'label'];
        gui_box_vis(cback_fig,{tag1,tag2},iivis);
        tag1=sprintf(['CTS New Level ',int2str(jcomb)]); tag2=[tag1,'label'];
        gui_box_vis(cback_fig,{tag1,tag2},iivis);
        tag1=sprintf(['CTS Time Offset ',int2str(jcomb)]); tag2=[tag1,'label'];
        gui_box_vis(cback_fig,{tag1,tag2},iivis);
    end
case 'Data Files',
	if no_files(val,str,'Data'), return; end
	MCS.data_root=MCS.data_files{val};
    %tms
    data_file_update([MCS.dir,MCS.data_root],cback_fig,0);
    if MCS.make_parms(7), mcs_act('Last Data File'); end
case 'Data Word Format'
    MCS.TAPE.iidata_formats=val;
case 'Delay Time Limits',
	MCS.ctau_lim=str2num(char(str));
	if isempty(MCS.tau_lim) & ~isempty(FFT), 
		MCS.ctau_lim=[-FFT.Tfft/2,FFT.Tfft/2];
		set(hval,'String',sprintf('%2.1f,%2.1f',MCS.ctau_lim));
	end
case 'Dim Limits',
	MCS.hdf_dim_lims=cellstr(str);
% Convert each cell to a number:
	for j=1:length(MCS.hdf_dim_lims),
		if isempty(MCS.hdf_dim_lims{j}), MCS.hdf_dim_lims{j}='0'; end
	end
	jset=MCS.hdf_jset; rank=MCS.SD.rank{jset}; nrow=length(MCS.hdf_dim_lims);
	if nrow<rank,
		for j=nrow+1:rank, MCS.hdf_dim_lims{j}='0'; end
	end
case 'DMR: D',
	MCS.abf_parm(2)=str2num(char(str));
case 'Dropout dB,T_filt (min)',
    vals=str2num(char(str)); 
    if length(vals(:))~=2, error('USER ERROR: "Dropout dB,T_filt" must be 2 elements long'); end
    MCS.SL_parms=vals(:)';
case 'Enter Phone Depths',
    MCS.ii_sc2=val;
    gui_box_vis(cback_fig,{'Array Geometry Files'},MCS.ii_sc2);
%
case 'Enveloping';
    MCS.sg_parms(1)=MCS.iienv_codes(val); %% disp(MCS.sg_parms(1));
case 'Exp:+a/-T10dB',
	MCS.abf_parm(6)=str2num(char(str));
case 'f-dep nphi fac (1-3)',
	MCS.fac_var=str2num(char(str));
	MCS.iivar = (MCS.fac_var~=0);
case 'FD [TD]',
	MCS.mc_mcc=val;
case 'FFT Files';
	if no_files(val,str,'FFT'), return; end
	MCS.fft_root=MCS.fft_files{val};
	FFT=fft_read1_open([MCS.dir MCS.fft_root]);
	MCS.fft_parm=[FFT.f1,FFT.f2,FFT.df,1/FFT.df,FFT.nrec,FFT.nseq,FFT.pct_step,FFT.iihann];
	MCS.nph_all=FFT.nrec;
	h1=findobj(cback_fig,'Tag','FFT f1,f2,iihann');
	str=sprintf('%1.0f,%1.0f,%1i',MCS.fft_parm([1:2,8])); set(h1,'String',str);
	h1=findobj(cback_fig,'Tag','FFT df,Tw,Tstep');
	str=sprintf('%3.3f,%2.1f,%2.1f',[MCS.fft_parm(3:4),.01*MCS.fft_parm(7)*MCS.fft_parm(4)]); 
    set(h1,'String',str);
	h1=findobj(cback_fig,'Tag','nrec,nseq,%step');
	str=sprintf('%1i,%1i,%1.1f',MCS.fft_parm(5:7)); set(h1,'String',str);
	h1=findobj(cback_fig,'Tag','Day,Hour');
	ddhh1=[FFT.jday0 FFT.T_lims(1)]'; ddhh2=[FFT.jday0 FFT.T_lims(2)]';
	MCS.fft_Tlims=[ddhh1,ddhh2];
	str=sprintf('%3.3i,%3.3f\n',MCS.fft_Tlims); set(h1,'String',str);
	mcs_act('Phone Numbers');
case 'FFT File 2',
	if no_files(val,str,'FFT'), return; end
	MCS.fft_root2=MCS.fft_files{val};
case 'File Header,Footer Bytes'
    temp=str2num(char(str));
    MCS.TAPE.file_header_bytes=temp(1);
    MCS.TAPE.file_footer_bytes=temp(2);
case 'Find'
    curdir=pwd;
    if isempty(MCS.dir)
        MCS.dir=[curdir '\'];
    end
    cd(MCS.dir);
    dir_prev=MCS.dir;
    [file,path]=uigetfile('*.*','Select Any File in Desired Input Directory');
    cd(curdir);
    if path==0
        MCS.dir=dir_prev;
    else
        MCS.dir=path;
    end
    h1=findobj(cback_fig,'Tag','Input Directory'); set(h1,'String',MCS.dir);
    mcs_name_update(cback_fig);
case 'Freq Chirps',
	MCS.v_10km=str2num(char(str));
case 'Freq phases',
	MCS.mc_phis=str2num(char(str));
case 'Freq Shifts',
	MCS.rm_Df=str2num(char(str));
case 'Frequencies',
	MCS.freq_lim=str2num(char(str));
	if length(MCS.freq_lim)==1, 
		MCS.freq_lim=MCS.fft_parm(1:2);
		set(hval,'String',sprintf('%2.1f,%2.1f',MCS.freq_lim));
	end
    [nr,nc]=size(MCS.freq_lim);
    if nc~=2 & nc~=0, 
        disp('WARNING: Frequency limits should have two columns containing [fmin,fmax]');
    end
	if ~isempty(FFT),
		if MCS.rvec_bw<0,
			MCS.nbin=max(1,round(-MCS.rvec_bw/FFT.df));
		else
			MCS.nbin=round(MCS.rvec_bw);
		end
	end
case 'Frequency Band(s)',
    MCS.freq_bands=str2num(char(str));
    [nr,nc]=size(MCS.freq_bands);
    if nc~=2 & nc~=0, disp('WARNING: Frequency bands should have two columns containing [fmin,fmax]'); end
case 'fs, calfac',
	MCS.make_parms(2:3)=str2num(char(str));
case 'Hann in f-domain',
    MCS.sg_parms(3)=val;
case 'HDF File',
	if strcmp(MCS.hdf_files{val},'None Available'), return; end
	MCS.hdf_root=MCS.hdf_files{val};
    %tms
    [MCS.SD,status]=hdfsd_open([MCS.dir MCS.hdf_root,'.hdf']); if status<0, return; end
	MCS.hdf_nset=MCS.SD.nset;
	MCS.hdf_sets=MCS.SD.data_name;
	h1=findobj(cback_fig,'Tag','HDF Set Names'); jset=get(h1,'Value');
	if jset>MCS.hdf_nset, set(h1,'Value',1); end
	set(h1,'String',MCS.hdf_sets);
	mcs_act('HDF Set Names');
case 'HDF Set Names',
	MCS.hdf_jset=val; jset=val; MCS.hdf_rank=MCS.SD.rank{jset};
	h1=findobj(cback_fig,'Tag','HDF Set Rank'); str2=sprintf('%1i',MCS.hdf_rank);
	set(h1,'String',str2);
	h1=findobj(cback_fig,'Tag','Set Dim Names');
	set(h1,'String',MCS.SD.dim_names{jset});
	h1=findobj(cback_fig,'Tag','Dims [min,max]'); str2=cell(MCS.SD.rank{jset},1);
	for jdim=1:MCS.SD.rank{jset},
		ldim=MCS.SD.dim_sizes{jset}(jdim); scales=MCS.SD.scales{jset}{jdim}; 
        if ldim==1, 
 			str2{jdim}=sprintf('%5i (%1.2f)',[ldim,scales(1)]);
        else, 
            [ldim,scales(1),scales(end)];
			str2{jdim}=sprintf('%5i (%1.2f,%1.2f)',[ldim,scales(1),scales(end)]);
        end
	end
	set(h1,'String',str2);
    h1=findobj(cback_fig,'Tag','Dim Limits');
    ndim=length(MCS.hdf_dim_lims); for jdim=ndim+1:MCS.hdf_rank, MCS.hdf_dim_lims{jdim}='0'; end
    set(h1,'String',MCS.hdf_dim_lims(1:MCS.hdf_rank));
case 'HELP',
	h2=findobj(cback_fig,'Style','pushbutton','Visible','on');
	h3=findobj(cback_fig,'Style','checkbox','Visible','on');
	set([h2; h3],'TooltipString','help','FontAngle','Oblique','FontWeight','Bold','FontSize',9);
	h1=findobj(cback_fig,'Style','text','Visible','on');
	set(h1,'Style','pushbutton','FontAngle','Oblique','FontWeight','Bold','FontSize',9);
	disp(' ')
	disp('Click on any italiced pushbutton or checkbox to obtain help on that subject.')
	disp(' ')
case 'HLAB Endian'
    MCS.TAPE.iihlab_endian=val;
case 'HLA-B fs',
    MCS.hla_fs_ii=val;
    mcs_act('Type of Data');
    MCS.TAPE.fs=str2num(MCS.hla_fs_opts{val});
case 'Init Jday,Jhour',
	MCS.make_parms(4:5)=str2num(char(str));
case 'Input Directory'
    MCS.dir=char(str);
    mcs_name_update(cback_fig);
case 'Input From Tape',
    h1=findobj(cback_fig,'Tag','Input From Tape');MCS.TAPE.isatape=get(h1,'Value');
    h1=findobj(cback_fig,'Tag','Use Fast Positioning'); set(h1,'Value',MCS.TAPE.use_fp);
    gui_box_vis(cback_fig,{'Data Files','Proc Mult Files'},MCS.TAPE.isatape==0); 
    gui_box_vis(cback_fig,{'Tape Address','Read Tape Jday,Jhour'},MCS.TAPE.isatape==1); 
    gui_box_vis(cback_fig,{'Last Data File'},((MCS.TAPE.isatape==0) & (MCS.make_parms(7)==1)));   %tms
    gui_box_vis(cback_fig,{'Use Fast Positioning'},(any(MCS.make_parms(1)==[2,5]) & MCS.TAPE.isatape==1));   %tms
    gui_box_vis(cback_fig,{'Proc Mult Files'},MCS.TAPE.isatape==0);   %tms
    gui_box_vis(cback_fig,{'Last Data File'},((MCS.TAPE.isatape==0) & (MCS.make_parms(7)==1)));   %tms
    if MCS.TAPE.isatape ~= 1
        MCS.TAPE.use_fp=0;
    else
        MCS.make_parms(7)=0;
    end
    mcs_act('re-init')
case 'Julian Day'; % to begin processing
    MCS.make_parms(8)=str2num(char(str));
case 'Last Data File',
	if no_files(val,str,'Data'), return; end
	MCS.data_last=MCS.data_files{val};
    data_file_update([MCS.dir MCS.data_last],cback_fig,1);
case 'Leave Complex [==>dB]',
    MCS.lg_parms(1)=val;
case 'Load _mcs root',
	if no_files(val,str,'_mcs.mat'), return; end
	MCS.mcs_root=MCS.mcs_files{val};
	mcs_gui_load;
case 'Make Cal File',
    mcs_act('AN Files');
	if no_files(val,str,'AN'), return; end
	if ~over_write([MCS.dir,MCS.out_root],'_CAL.mat'), return; end
    mcs_save_run([MCS.out_root,'_CAL']);
    CAL=cal_file_make;          % Make CAL structure;
% Save in directory where input data file resides:
    str=['save ',MCS.dir,MCS.out_root,'_CAL CAL']; disp(str); eval(str);
case 'MAKE REPLICAS',
    disp('NEED FIX HERE');
    return;
	N_up=MCS.rm_parms(1); N_tap=MCS.rm_parms(2); 
	Tfft=MCS.rm_parms(3); fs=MCS.rm_parms(4); T_step=MCS.rm_parms(5);
	iiauto=MCS.rm_parms(6);
	Df=MCS.rm_Df; v_10km=MCS.v_10km;
	if Tfft==0,
		if ~isempty(FFT), 
			Tfft=FFT.Tfft;
		else,
			disp('No FFT file loaded. Select FFT File and retry ...'); return
		end
	end
	if fs==0,
		if ~isempty(FFT), 
			fs=FFT.fs;
		else, 
			disp('No FFT file loaded. Select FFT File and retry ...'); return
		end
	end
	rep_fft_make(rep_root,MCS.out_root,N_up,N_tap,T_step,Tfft,fs, ...
		Df,v_10km,iiauto,0);
case 'Min,Max BF aper',
	MCS.bf_aper=str2num(char(str));
case 'Min,Max XC aper',
	MCS.xc_aper=str2num(char(str));
case 'nrec_avg (0=fix,1=ind)',
    MCS.mc_parms(1)=str2num(char(str));
case 'Number of Channels'
    MCS.TAPE.nchan=str2num(char(str));
case 'Nup,Ntap,Tstep',
	parms=str2num(char(str));
	MCS.rm_parms(1:2)=parms(1:2);
	if length(parms)>2, MCS.rm_parms(5)=parms(3); else, MCS.rm_parms(5)=0; end
case 'Output File Root',
	MCS.out_root=str;
case 'Phase Transform',
    MCS.sg_parms(6)=val;
case 'Phone Numbers',
	MCS.phones=str2num(char(str));
	nph=length(MCS.phones); if nph==0, MCS.phones=0; nph=1; end
	nph_all=MCS.nph_all;
	if MCS.phones(1)==0, 
		MCS.iiph=[1:nph_all]'; 	% Code for all phones;
		set(hval,'String',['[1:',int2str(nph_all),']''']);
	elseif nph>1 & MCS.phones(2)<0,
		MCS.iiph=[MCS.phones(1):-MCS.phones(2):MCS.phones(3)]';
	else,
		MCS.iiph=MCS.phones(:);
	end
	MCS.nrec=length(MCS.iiph);
	phmax=max(MCS.iiph); 
	if MCS.iiprog==3 & phmax>MCS.nph_all, 
		disp('WARNING: phone_max exceeded # phones in array file.  Set to all.');
		keyboard
	end
	if ~isempty(FFT) & phmax>FFT.nrec,
		disp('WARNING: phone_max exceeded # phones in FFT file.  Set to all.'); 
	end
case 'Plot CTS Time Series',
    [DATA]=cts_extract([MCS.dir MCS.cts_root],MCS.cts_parms(1),MCS.Tlims,MCS.iiph(:));
    if MCS.v_opts(2), Mdat.CTS=DATA; end
    view5(DATA);
case 'Plot Measured AN',
    mcs_act('AN Files');
    str=['load ',MCS.dir,MCS.AN_root]; eval(str);
    view5(AN,[2,3,1],1,1,MCS.psize);
case 'Plot Measured TL/SL',
    mcs_act('TLm/SL Files');
    if any(findstr(MCS.TLm_root,'_TLm')),
        view5(TLm,[2,3,1],1,1,MCS.psize);
    else
        view5(SL,[4,3,1],1,1,MCS.psize);
    end
case 'Plot Size [w,h]',
	MCS.psize=str2num(char(str));
case 'Proc Mult Files',
	MCS.make_parms(7)=val;
    gui_box_vis(cback_fig,{'Last Data File','Last Data Filelabel'},val);
case 'Print CTS Details',
    for jcomb=1:MCS.comb_parms(1),
        cts_details(MCS.comb_files{jcomb});
    end
case 'Program',
% Make correct boxes visible:
	MCS.iiprog=MBOX.prog_order(val);
	for jbox=1:MBOX.nboxtot,
		set(MBOX.handle(:,jbox),'Visible','Off');
	end
	iiprog=MCS.iiprog;
	for j=1:MBOX.nbox_prog(iiprog),
		jbox=MBOX.prog(iiprog,j);
		set(MBOX.handle(:,jbox),'Visible','On');
	end
	mcs_name_update(cback_fig);
    mcs_help('Program',cback_fig);
case 'Range Normalize',
    MCS.sg_parms(4)=val;
case 'Read Tape Jday,Jhour',
    data_file_update(MCS.tape_root,cback_fig,0)
case 'Record Header,Footer Bytes'
    temp=str2num(char(str));
    MCS.TAPE.record_header_bytes=temp(1);
    MCS.TAPE.record_footer_bytes=temp(2);
case 're-init',
% Re-initialize _svg files and _adi files available:
	mcs_name_update(cback_fig);
case 'Remove Spikes',
    MCS.spike_parms(1)=val;
    gui_box_vis(cback_fig,'Spike Threshold (dB)',val);
case 'Rep Corr FFT',
	MCS.rep_root=MCS.rep_files{val};
    if strcmp(MCS.rep_root,'None Available'); return; end
	REP=fft_read1_open([MCS.rep_dir,MCS.rep_root]);
    if REP.pri==0, 
        CTS=cts_open([MCS.wf_dir,MCS.src_root]);
        REP.pri=CTS.npt/CTS.fs; disp(['PRI from SRC CTS File = ',num2str(REP.pri)]);
    end
	MCS.rep_parm=[REP.f1,REP.f2,REP.df,1/REP.df,REP.nrec,REP.nseq];
	h1=findobj(cback_fig,'Tag','REP f1,f2,Tw');
	str=sprintf('%1.0f,%1.0f,%1.1f',MCS.rep_parm([1,2,4])); set(h1,'String',str);
	h1=findobj(cback_fig,'Tag','REP nrec,nseq');
	str=sprintf('%1i,%1i',MCS.rep_parm(5:6)); set(h1,'String',str);
case 'Rep Corr iiph',
	MCS.rep_iiph=str2num(char(str));
	if isempty(MCS.rep_iiph) & ~isempty(REP),
		MCS.rep_iiph=[1:REP.nrec];
		set(hval,'String',['[1:',int2str(REP.nrec),']''']);
    end
	if max(MCS.rep_iiph)>REP.nrec | min(MCS.rep_iiph)<1,
		disp('Illegal REP phone requested.  Selecting all.');
		set(hval,'String',''); mcs_act('Rep Corr iiph');
    end
case 'Rep Corr iiseq',
	MCS.rep_iiseq=str2num(char(str));
	if isempty(MCS.rep_iiseq) & ~isempty(REP), 
		MCS.rep_iiseq=[1:REP.nseq];
		set(hval,'String',['[1:',int2str(REP.nseq),']''']);
    end
	if max(MCS.rep_iiseq)>REP.nseq | min(MCS.rep_iiseq)<1,
		disp('Illegal REP sequence requested.  Selecting all.');
		set(hval,'String',''); mcs_act('Rep Corr iiseq');
    end
case 'RGal + Phase Map',
    h1=findobj(cback_fig,'Tag','Enveloping'); set(h1,'Value',2); mcs_act('Enveloping');
	mcs_act('Phone Numbers');
    mcs_save_run('phmap');
	parms=[1,MCS.iihr,MCS.sg_parms([1:4,7])]; tau_lim=[];
    cts_root=[MCS.wf_dir,MCS.src_root];
    [RGRAM,FFT]=rgram2_3d([MCS.dir MCS.fft_root],cts_root,MCS.Tlims,MCS.iiph,MCS.freq_lim, ...
        tau_lim,parms,0,[]); RGRAM.labels{3}='Receiver';
	[RGal]=mseq_align(RGRAM,0,0,MCS.out_root);
    if diff(MCS.tau_lim)<diff(RGal.scales{1}([1,end])) - RGal.dt,
        RGal=data_parse(RGal,1,MCS.tau_lim,0);
    end
    XS=data_avg(RGal,3,[],6,0,0); XS=data_avg(XS,[1 2],[],[1.5 1.5],0,0);
    XS.PH=XS;
    XS.PH.data=angle(XS.data)/pi; XS.PH.dlab='Phase of X-Spectra - Frac of \pi'; XS.PH.iidB=0; view5(XS.PH);
    DIST=phase_map_plot(XS.PH);
case 'RGRAM Align',
	if isempty(RGRAM), disp('Generate RGRAM first!!!'); return; end
	[RGal]=mseq_align(RGRAM,1,0,MCS.out_root);
case 'RGRAM & Align',
	mcs_act('Phone Numbers');
    mcs_save_run('rgram');
	parms=[1,MCS.iihr,MCS.sg_parms([1:4,7])]; tau_lim=[];
    cts_root=[MCS.wf_dir,MCS.src_root];
    %tms
    [RGRAM,FFT]=rgram2_3d([MCS.dir MCS.fft_root],cts_root,MCS.Tlims,MCS.iiph,MCS.freq_lim, ...
        tau_lim,parms,0,[]);
	[RGal]=mseq_align(RGRAM,0,0,MCS.out_root);
    if diff(MCS.tau_lim)<diff(RGal.scales{1}([1,end])) - RGal.dt,
        RGal=data_parse(RGal,1,MCS.tau_lim,0);
    end
    view5(RGal);
case 'RGRAM Map',
	RGmap=Dtau_map0(RGRAM,1,1,0);
case 'RUN CGRAM',
    if MCS.sg_parms(6), T_exp=-1; else, T_exp=0; end
	parms=[T_exp,MCS.iihr,MCS.sg_parms([1,2,3,5])],
    mcs_save_run('cgram');
    xclim=[];
%	xclim=MCS.xclim; if MCS.sg_parms(1)>0, xclim(1)=max(0,xclim(1)); end
    %tms
	[CGRAM,FFT]=cgram_3d([MCS.dir MCS.fft_root],[MCS.dir MCS.fft_root],MCS.Tlims,MCS.ph_pairs,MCS.freq_lim, ...
		MCS.ctau_lim,parms,0,xclim);
	cmap=V.cmap; V.cmap=1-gray;
	if ~isempty(CGRAM), view5(CGRAM); end
	V.cmap=cmap;
case 'RUN CTS2FFT',
    mcs_save_run([MCS.out_root,'_FFT']);
	MCS.mc_cts2fft=val;
    gui_box_vis(cback_fig,{'T_fft,T_step','zpfac,ii_hann'},val);
    if MCS.iiprog==13, gui_box_vis(cback_fig,{'Frequency Band(s)'},val); end

case 'RUN CTS to FFT'
    out_root=MCS.out_root;
	if isempty(MCS.out_root),
		MCS.out_root=MCS.cts_root;
        out_root=MCS.out_root;
		h1=findobj(cback_fig,'Tag','Output File Root'); set(h1,'String',MCS.out_root);
    elseif strcmp(MCS.out_root,'auto'),
        out_root=MCS.cts_root; 'auto worked',
	end
	if ~over_write(out_root,'_fft'), return; end
    mcs_save_run([MCS.out_root,'_FFT']);
    %tms
    if MCS.TAPE.ii_cal
        cal_root=MCS.TAPE.cal_root;
    else
        cal_root=[];
    end
	[LGRAM,FFT]=cts2fft([MCS.dir MCS.cts_root],out_root,MCS.cts_parms(1),MCS.cts_parms(2), ...
		MCS.cts_parms(3),MCS.Tlims,MCS.freq_lim,MCS.iiph(:),MCS.cts_parms(4),0,cal_root);
case 'RUN DTAU',
    mcs_save_run
	if isempty(MCS.out_root),
		MCS.out_root=MCS.fft_root;
		h1=findobj(cback_fig,'Tag','Output File Root'); set(h1,'String',MCS.out_root);
	end
	mcs_act('Phone Numbers');
	mcs_act('SRC CTS File');
    cts_root=[MCS.wf_dir,MCS.src_root];
    parms=[];
    %tms
    Dtau_long_pri([MCS.dir MCS.fft_root],MCS.out_root,cts_root,MCS.Tlims,MCS.iiph,MCS.freq_lim,parms);
case 'RUN fft_make'
    mcs_act('Cal Files');
    if strcmp(MCS.out_root,'auto') | isempty(MCS.out_root), 
        out_root=MCS.data_root; ii=findstr(out_root,'.'); 
        if ~isempty(ii), out_root=out_root(1:ii(1)-1); end
        disp(['Auto Name of Output File Root = ',out_root]);
    else
        out_root=MCS.out_root;
    end
	if ~over_write(out_root,'_fft'), return; end
    mcs_save_run([MCS.out_root,'_FFT']);
    if MCS.TAPE.isatape==1 %tms
        data_root=MCS.tape_root; data_suf=[];
    else
        data_root=[MCS.dir MCS.data_root]; data_suf=[];
    end
    if strcmp(MCS.TAPE.array_type,'SWAMI'), mcs_act('SWAMI calfac'); end
    if strcmp(MCS.TAPE.array_type,'HLAB'), mcs_act('HLA-B fs'); end
    nper=size(MCS.Tlims,1); if nper>1, out_root=[out_root,'_p01']; end
    for jper=1:nper,
        if nper>1, out_root=[out_root(1:end-2),sprintf('%2.2i',jper)]; end
        if jper>1, 
            if MCS.Tlims(jper,1)-MCS.Tlims(jper-1,2)<-12, Jday=Jday+1; end
        end
        Tlims=MCS.Tlims(jper,:);
		if MCS.make_parms(7),       % Multiple data files;
			h1=findobj(cback_fig,'Tag','Data Files'); jfile1=get(h1,'Value');
			h2=findobj(cback_fig,'Tag','Last Data File'); jfile2=get(h2,'Value');
            %tms
            dirs=repmat(MCS.dir,length(MCS.data_files(jfile1:jfile2)),1);
            files=char(MCS.data_files(jfile1:jfile2));
            data_roots=cellstr([dirs files])';
        else
            data_roots=data_root;
        end
        Jday_start=MCS.make_parms(8); % Julian day to begin processing
        ii_app=0;  
        if MCS.batch==0
        	t_hr=fft_make(data_roots,data_suf,[],out_root,MCS.cts_parms(1), ...
        		MCS.cts_parms(2),MCS.cts_parms(3),Tlims,MCS.freq_lim,MCS.iiph, ...
			    MCS.cts_parms(4),ii_app,MCS.spike_parms,MCS.TAPE,Jday_start);
            disp(num2str(t_hr,10))
        else
            if ~exist([matlabroot '\batch'],'dir')
                [SUCCESS,MESSAGE,MESSAGEID] = mkdir(matlabroot,'batch');
                if ~SUCCESS
                    disp(MESSAGE);
                    return;
                end
            end
            save([matlabroot '\batch\fft_make_run_parms.mat'],'data_roots','data_suf','out_root','MCS', ...
        		'Tlims','ii_app','Jday_start');
            isuf=0;
            fft_dir=[matlabroot '\\batch\\'];
            while 1
                isuf=isuf+1;
                fft_script = ['fft_make_run_script' num2str(isuf) '.m'];
                if ~exist([fft_dir fft_script],'file')
                    break;
                end
            end
            bfid = fopen([fft_dir fft_script],'wt');
            fprintf(bfid,['load %s\\batch\\fft_make_run_parms.mat\n'],matlabroot);
            fprintf(bfid,['cd %s\n'],pwd);
            fprintf(bfid,['t_hr=fft_make(data_roots,data_suf,[],out_root,MCS.cts_parms(1), ...\n' ...
        		'MCS.cts_parms(2),MCS.cts_parms(3),Tlims,MCS.freq_lim,MCS.iiph, ...\n' ...
			    'MCS.cts_parms(4),ii_app,MCS.spike_parms,MCS.TAPE,Jday_start);\n']);
            fprintf(bfid,['exit\n']);
            status=fclose(bfid);
            if status ~= 0
                disp('problem writing fft_make batch script');
                return;
            end
            eval(['!start ' matlabroot '\batch\matlab.exe.lnk /r ' fft_script(1:end-2)]);
            delete([fft_dir fft_script]);
        end
    end
case 'RUN FFT to CTS',
	if ~over_write(MCS.out_root,'.cts'), return; end
    mcs_save_run([MCS.out_root,'_CTS']);
    if ~isempty(MCS.f_bb) & MCS.f_bb>0 & length(MCS.freq_lim)==2,
        if prod(MCS.freq_lim-MCS.f_bb)>0, 
            error(['Baseband Freq must be within output band: ',num2str([MCS.f_bb,MCS.freq_lim])]);
        end
    end
    %tms
	fft2cts([MCS.dir MCS.fft_root],MCS.out_root,MCS.Tlims,MCS.iiph,MCS.freq_lim,MCS.cts_head,MCS.f_bb);
case 'RUN LGRAM',
	mcs_act('Phone Numbers');
    mcs_save_run('lgram');
	parms=[1,1,0,1-MCS.lg_parms(1)];
	[LGRAM,FFT]=lgram_3d([MCS.dir MCS.fft_root],MCS.Tlims,MCS.iiph,MCS.freq_lim,parms,0,[]);
	if ~MCS.iihr, LGRAM.z=[1:size(LGRAM.data,3)]; LGRAM.zlab='Sequence Number'; end
	if ~isempty(LGRAM), view5(LGRAM); end
case 'RUN MC',
    mcs_save_run
	if isempty(MCS.out_root),
		MCS.out_root=MCS.fft_root;
		h1=findobj(cback_fig,'Tag','Output File Root'); set(h1,'String',MCS.out_root);
	end
	mcs_act('Phone Numbers');
	mcs_act('SRC CTS File');
    cts_root=[MCS.wf_dir,MCS.src_root];
    if MCS.mc_mcc==0,
        fzpad=2, iifig=0; ii_spline=0; T_filt=20; nrec_avg=MCS.mc_parms(1); T_filt=MCS.mc_parms(4);
        T_sec0=2;
        dr_max=MCS.mc_parms(5); iial=MCS.mc_parms(6),
        parms=[fzpad iifig ii_spline T_filt nrec_avg,T_sec0,dr_max,iial];
        [MC,AN]=mc_bird_gen([MCS.dir MCS.fft_root],MCS.out_root,cts_root,MCS.Tlims,MCS.iiph,MCS.freq_lim,parms);
    else
        CTS=cts_open(cts_root);
        sig_bb=cts_read(CTS,0,inf); npt=length(sig_bb);
        if prod(MCS.freq_lim-CTS.f_bb_shift)>0,
            disp(CTS.f_bb_shift); disp(MCS.freq_lim);
            error('CTS file center frequency not inside MCS.freq_lim: ');
    end
% Find lines in source waveform:    
        df=CTS.fs/npt;
        S_dB=dB(fft(sig_bb)); [S_max,ii_max]=max(S_dB);
% Find 50-dB bandwidth:
        iif=[1:floor(npt/2)]; ii=find(S_dB(iif)>S_max-50); BW=2*df*ii(end);
% Find 10-dB bandwidth:
        ii=find(S_dB(iif)>S_max-10); BW_10dB=2*df*ii(end);
        fac_max=ceil(npt/ii(end)/2); cum_prod=cumprod(factor(npt));
        iin=find(fac_max>cum_prod); fac=cum_prod(iin(end));
        sig_mult=repmat(sig_bb(1:fac:end),[20,1]); npt_mult=length(sig_mult); fs_mult=CTS.fs/fac;
        df_mult=fs_mult/npt_mult; f_ax=[0:npt_mult-1]*df_mult;
        S_dB=dB(fft(sig_mult)); 
        npt_10dB=ceil(BW_10dB/df_mult/2); 
        ii_cent=[npt_mult-npt_10dB+2:npt_mult,1:npt_10dB]; n_cent=length(ii_cent);
        f_cent=f_ax(ii_cent); 
        iineg=find(f_cent>fs_mult/2); f_cent(iineg)=f_cent(iineg)-fs_mult;
        S_cent=S_dB(ii_cent);
        [S_max,ii_max]=max(S_cent); ii=[2:n_cent-1];
        ii_lines=find(S_cent(ii)>S_cent(ii-1) & S_cent(ii)>S_cent(ii+1) & S_cent(ii)>S_max-10);
        f_lines=CTS.f_bb_shift + f_cent(ii_lines+1),
		if length(f_lines)>1, df_win=min(diff(sort(f_lines))), else, df_win=1, end
        %tms
		MC=fft_mcb([MCS.dir MCS.fft_root],MCS.out_root,MCS.Tlims,MCS.iiph,MCS.freq_lim,f_lines, ...
			df_win,MCS.PRI,MCS.mc_parms);
    end
	if MCS.mc_cts2fft,
		cts_root=[MCS.out_root,'_mc']; fft_root=cts_root;
		if ~over_write(cts_root,'_fft'), return; end
		[LGRAM,FFT]=cts2fft(cts_root,fft_root,MCS.cts_parms(1),MCS.cts_parms(2), ...
			MCS.cts_parms(3),[],MCS.freq_lim,[],MCS.cts_parms(4),0);
	end	
case 'RUN MCS',
	mcs_act('Phone Numbers');
	mcs_act('Seqs or Times');
	mcs_act('Frequencies');
    mcs_save_run
	mcs_bform;
case 'RUN RGRAM CTS',
	mcs_act('Phone Numbers');
    mcs_save_run('rgram');
	parms=[1,MCS.iihr,MCS.sg_parms([1:4,7])],
    cts_root=[MCS.wf_dir,MCS.src_root];
    %tms
    [RGRAM,FFT]=rgram2_3d([MCS.dir MCS.fft_root],cts_root,MCS.Tlims,MCS.iiph,MCS.freq_lim, ...
        MCS.tau_lim,parms,0,[]);
	if ~isempty(RGRAM), view5(RGRAM); end
case 'RUN RGRAM FFT',
	mcs_act('Phone Numbers');
    mcs_save_run('rgram');
	parms=[1,MCS.iihr,MCS.sg_parms(1:4),1];
    if isempty(REP), disp('Choose Rep Corr FFT file'); return; end
	[RGRAM,FFT]=rgram_3d([MCS.dir MCS.fft_root],REP,MCS.Tlims,MCS.iiph,MCS.freq_lim, ...
		MCS.tau_lim,parms,MCS.rep_iiph,MCS.rep_iiseq,0,[]);
%%	if ~isempty(RGRAM), view5(RGRAM); end
case 'RUN SGRAM',
	mcs_act('Phone Numbers');
    mcs_save_run('sgram');
	parms=[1,MCS.iihr,MCS.sg_parms(1:5),0];
    %tms
	[SGRAM,FFT]=sgram_3d([MCS.dir MCS.fft_root],MCS.Tlims,MCS.iiph,MCS.freq_lim,MCS.tau_lim, ...
		parms,0,[]);
	if ~isempty(SGRAM), view5(SGRAM); end
case 'RUN TLm_calc',
    iiauto=0;
	if isempty(MCS.out_root),
		MCS.out_root=MCS.fft_root;
		h1=findobj(cback_fig,'Tag','Output File Root'); set(h1,'String',MCS.out_root);
    elseif strcmp(MCS.out_root,'auto'),
        MCS.out_root=MCS.fft_root; iiauto=1;
		h1=findobj(cback_fig,'Tag','Output File Root'); set(h1,'String',MCS.out_root);
	end
    if MCS.ii_sc2,
        if FFT.nrec>MCS.nph_all,
            disp(['Exiting because # phones in FFT file > # phones in array geometry file: ', ...
                    int2str([FFT.nrec,MCS.nph_all])]); return
        end
        geom_file=[MCS.nav_dir,MCS.geom_file];
    else
        geom_file=[];
    end
	if isppc, word_max=5000000; else, word_max=8000000; end
	word_seq=MCS.nrec*FFT.nwords; dT_seq=FFT.T_step/3600;
	n_seq=max(2,word_max/word_seq); dT=n_seq*dT_seq;
	mcs_act('Phone Numbers');
    iiAN_corr=2*MCS.iiAN_corr;
    if MCS.ii_TL,
		if ~over_write(MCS.out_root,'_TLm.mat'), return; end
        mcs_save_run([MCS.out_root,'_TLm']);
        if iiAN_corr & isempty(MCS.AN_freq), 
            disp('WILL USE MEDIAN IN TL BAND TO ESTIMATE AN!!');
            iiAN_corr=1; 
        end;
        if MCS.SL_use,
            %tms
            eval(['load ',MCS.dir MCS.SL_root]);
            %tms
    		TLm=TLm_calc([MCS.dir MCS.fft_root],MCS.out_root,MCS.Tlims,dT,MCS.iiph,MCS.TL_freq, ...
			    MCS.ii_phavg,SL,MCS.SL_parms,iiAN_corr,MCS.AN_freq,geom_file);
        else
            %tms
    		TLm=TLm_calc([MCS.dir MCS.fft_root],MCS.out_root,MCS.Tlims,dT,MCS.iiph,MCS.TL_freq, ...
			    MCS.ii_phavg,MCS.SL,MCS.SL_parms,iiAN_corr,MCS.AN_freq,geom_file);
        end
        MCS.TLm_root=MCS.out_root;
		MCS.TLm_files=gui_pop_update(cback_fig,sufs,lsufs,'TLm/SL Files',MCS.TLm_root,MCS.dir);
		V.size=MCS.psize; view5(TLm);
    elseif MCS.ii_SL,
		if ~over_write(MCS.out_root,'_SL.mat'), return; end
        mcs_save_run([MCS.out_root,'_SL']);
        %tms
		SL=TLm_calc([MCS.dir MCS.fft_root],MCS.out_root,MCS.Tlims,dT,MCS.iiph,MCS.SL_freq, ...
			MCS.ii_phavg,0,MCS.SL_parms,iiAN_corr,[],geom_file);
        MCS.SL_root=MCS.out_root;
		MCS.SL_files=gui_pop_update(cback_fig,'*_SL.mat',4,'SL Files',MCS.SL_root,MCS.dir);
		V.size=MCS.psize; view5(SL);
    end
    if MCS.ii_AN,
		if ~over_write(MCS.out_root,'_AN.mat'), return; end
        mcs_save_run([MCS.out_root,'_AN']);
        %tms
		AN=TLm_calc([MCS.dir MCS.fft_root],MCS.out_root,MCS.Tlims,dT,MCS.iiph,MCS.AN_freq, ...
			MCS.ii_phavg,-999,MCS.SL_parms,iiAN_corr,[],geom_file);
        MCS.AN_root=[MCS.out_root,'_AN'];
		MCS.AN_files=gui_pop_update(cback_fig,'*_AN.mat',4,'AN Files',MCS.AN_root,MCS.dir);
		V.size=MCS.psize; view5(AN);
    end
    if iiauto,
		h1=findobj(cback_fig,'Tag','Output File Root'); set(h1,'String','auto'); MCS.out_root='auto';
    end
case 'RUN TS View'
    [MCS.TAPE] = ts_open(MCS.TAPE);
    if MCS.TAPE.status <= 0
        disp(['RUN TS View:  ts_open returned <= 0 for ' MCS.TAPE.data_file]);
        return;
    end
    [DATA,MCS.TAPE] = ts_view(MCS.TAPE,MCS.TAPE.day,MCS.Tlims(1,:),MCS.ts_view_sample_ith,MCS.iiph,0);
    [MCS.TAPE] = ts_close(MCS.TAPE);
 	if MCS.v_opts(1), iinewfig=1; else, figure(220); clf; iinewfig=0; end
    if MCS.v_opts(2),
        ii=findstr(DATA.dlab,' - '); if isempty(ii), ii=length(DATA.dlab)+1; end
        name=DATA.dlab(1:ii(1)-1); 
        name=strrep(name,' ','_'); name=strrep(name,'(','_'); name=strrep(name,')','_');  
        name=strrep(name,'|','_'); name=strrep(name,'[','_'); name=strrep(name,']','_');
        name=strrep(name,'#','_');
        str=['Mdat.',name,'=DATA;']; 
        eval(str);
    end
    view5(DATA,[],1,iinewfig,MCS.psize,1);
case 'Sample ith'
    MCS.ts_view_sample_ith=str2num(char(str));
case 'Samples per Channel'
    MCS.TAPE.nsamp=str2num(char(str));
case 'Save MCS',
    mcs_save_run;
case 'Seqs or Times',
	MCS.Tlims=str2num(char(str));
%%	if isempty(FFT), disp('No FFT file loaded - cannot interpret Tlims'); return; end
	if isempty(MCS.Tlims) | (length(MCS.Tlims)==1 & MCS.Tlims==0),
		ns=MCS.fft_parm(6);
		switch MCS.tspec,
		case 1, MCS.Tlims=[1,ns];
		case 2,	MCS.Tlims=floor(FFT.hodax([1,ns])*100) + rem(FFT.hodax([1,ns]),1)*60; 
		case 3, MCS.Tlims=FFT.hodax([1,ns]);
		end
		set(hval,'String',sprintf('%3.3f,%3.3f',MCS.Tlims));
	end
	switch MCS.tspec,
	case 1
		if any(round(MCS.Tlims)~=MCS.Tlims), 
			disp('WARNING: Seq #s must be integers!');
		end
		if ~isempty(FFT),
			if any(MCS.Tlims<1), disp('WARNING: Sequence numbers < 1.'); end
			if any(MCS.Tlims>FFT.nseq), disp('WARNING: Sequence numbers > FFT.nseq.'); end
		end
		[nr,nc]=size(MCS.Tlims);
		if nr==1 & nc>2, MCS.Tlims=MCS.Tlims'; end
		if nr==1 & nc==2, MCS.Tlims=[MCS.Tlims(1):MCS.Tlims(2)]'; end
	case 2, 
		MCS.Tlims=floor(MCS.Tlims/100) + rem(MCS.Tlims,100)/100;
	case 3
		if all(round(MCS.Tlims)==MCS.Tlims), 
			disp('WARNING: Times expected as decimal HOD are integers!!');
		end
	end
case '# Seq to Avg (def=1)',
    vals=str2num(str); 
    if length(vals(:))~=1, error('USER ERROR: "#Seq to Avg (def=1)" must have 1 element'); end
	MCS.sg_parms(5)=vals;
case 'Set All to Zero',
    MCS.comb_offsets=zeros(1,MCS.comb_parms(2));
    MCS.comb_levels(2,:)=-999;
    for jcomb=1:MCS.comb_parms(2),
        tag1=sprintf(['CTS New Level ',int2str(jcomb)]);
        h1=findobj(cback_fig,'Tag',tag1); set(h1,'String','-999');
        tag1=sprintf(['CTS Time Offset ',int2str(jcomb)]);         
        h1=findobj(cback_fig,'Tag',tag1); set(h1,'String','0');
    end
case 'Set Files by Times',
    nfiles=length(MCS.data_files);
    file_times=zeros(nfiles,2);
    for jfile=1:length(MCS.data_files),
        data_root=MCS.data_files{jfile};
        data_file_update([MCS.dir,data_root],cback_fig,0);
        file_times(jfile,:)=MCS.make_parms(4) + MCS.make_parms(5:6)/24;
    end
    start_time=MCS.make_parms(8) + MCS.Tlims(1)/24;
    end_time=MCS.make_parms(8) + MCS.Tlims(2)/24;
    ii1=find(file_times(:,1)<=start_time); if isempty(ii1), ii1=1; else, ii1=ii1(end); end
    ii2=find(file_times(:,2)>=end_time); if isempty(ii2), ii2=nfiles; else, ii2=ii2(1); end
    h1=findobj(cback_fig,'Tag','Data Files'); set(h1,'Value',ii1);
    mcs_act('Data Files',cback_fig);
    hmult=findobj(cback_fig,'Tag','Proc Mult Files'); set(hmult,'Value',ii2>ii1);
    mcs_act('Proc Mult Files');
    if ii2>ii1,
        h1=findobj(cback_fig,'Tag','Last Data File'), set(h1,'Value',ii2);
        mcs_act('Last Data File',cback_fig);
    end
    disp('File start, stop times in decimal Julian days: ');
    disp(file_times);
case 'Set WF Dir',
    MCS.wf_dir=adi_dir_set(MCS.wf_dir,'Waveform');
    mcs_name_update(cback_fig);
case 'Sin space [deg]',
	MCS.iisin=str2num(char(str));
case 'SL Files',
	if no_files(val,str,'SL'), return; end
    MCS.SL_root=MCS.SL_files{val};
case 'SL Frequency Band(s)',
    MCS.SL_freq=str2num(char(str));
case 'SL (-1=sim)',
	MCS.SL=str2num(char(str));
	if length(MCS.SL)>1,
		[nr,nc]=size(MCS.SL);
		if nc~=2,
			disp('Illegal SL(t) specification: Must have two columns: t_hr,SL.');
			disp(MCS.SL);
		end
	end
case 'Specify All',
	h_ts=findobj(cback_fig,'Tag','Time Specification'); 
	h_f=findobj(cback_fig,'Tag','Frequencies'); MCS.freq_lim=MCS.fft_parm(1:2);
	h_jd=findobj(cback_fig,'Tag','Julian Day'); 
    switch MCS.iiprog
    case 4,
        if size(MCS.TL_freq,1)==1,
            MCS.AN_freq=MCS.freq_lim; MCS.TL_freq=MCS.freq_lim;
            h_f(1)=findobj(cback_fig,'Tag','TL Frequency Band(s)');
            h_f(2)=findobj(cback_fig,'Tag','AN Frequency Band(s)');
        else
            h_f=[];
        end
    case 6, MCS.freq_lim=max(CTS.f_bb_shift + (CTS.fs/2)*[-1,1],0); 
    case 7, MCS.freq_lim=[0,MCS.make_parms(2)/2]; 
    end
	set(h_f,'String',sprintf('%2.1f,%2.1f',MCS.freq_lim));
	h_seq=findobj(cback_fig,'Tag','Seqs or Times');
    switch MCS.iiprog,
    case {1,2,3,4,5,8,9,10,12}
	    if ~isempty(FFT),
		    if FFT.nseq>10,
			    set(h_ts,'Value',3); MCS.tspec=3; MCS.Tlims=FFT.T_lims;
			    set(h_seq,'String',sprintf('%2.3f,%2.3f',MCS.Tlims));
		    else
			    set(h_ts,'Value',1); MCS.tspec=1; MCS.Tlims=[1:FFT.nseq]';
			    set(h_seq,'String',['[1:',int2str(FFT.nseq),']''']);
            end
        end
    case {6},
		MCS.Tlims=CTS.T_lims;
		set(h_ts,'Value',3); MCS.tspec=3;
		set(h_seq,'String',sprintf('%2.3f,%2.3f',MCS.Tlims));
    case {7,15}
        set(h_ts,'Value',3); MCS.tspec=3; MCS.Tlims=MCS.make_parms(5:6);
        set(h_seq,'String',sprintf('%2.3f,%2.3f',MCS.Tlims));
        set(h_jd,'String',int2str(MCS.make_parms(4)));
        MCS.make_parms(8)=MCS.make_parms(4);
    end
	if any(MCS.iiprog==[2,10,12]),
		MCS.tau_lim=[0,MCS.fft_parm(4)];
		h_tau=findobj(cback_fig,'Tag','[taumin,taumax]');
		set(h_tau,'String',sprintf('%1.1f,%1.1f',MCS.tau_lim));
		if any(MCS.iiprog==[12]) & ~isempty(REP),
			h_seq=findobj(cback_fig,'Tag','Rep Corr iiseq');
			h_rec=findobj(cback_fig,'Tag','Rep Corr iiph');
			MCS.rep_iiseq=[1:REP.nseq]; MCS.rep_iiph=[1:REP.nrec];
			set(h_seq,'String',['[1:',int2str(REP.nseq),']''']);
			set(h_rec,'String',['[1:',int2str(REP.nrec),']''']);
        end
	end
%
	h_rec=findobj(cback_fig,'Tag','Phone Numbers');
	if isfield(MCS.TAPE,'allphones')
        if (MCS.iiprog==7) & ~isempty(MCS.TAPE.allphones),
	        set(h_rec,'String',MCS.TAPE.allphones);
        else
        	set(h_rec,'String','0');
        end
    else
    	set(h_rec,'String','0');
    end
	mcs_act('Phone Numbers');
%	set(h_rec,'String',['1:',int2str(MCS.nph_all)]);
case 'Spike Threshold (dB)',
    MCS.spike_parms(2)=str2num(char(str));
%
case 'SRC CTS File',
	if no_files(val,str,'SRC CTS'), return; end
	MCS.src_root=MCS.src_files{val};
    CTS=cts_open([MCS.wf_dir,MCS.src_root]); fclose(CTS.fid_cts);
    MCS.PRI=CTS.npt/CTS.fs;
    h1=findobj(cback_fig,'Tag','PRI'); set(h1,'String',sprintf('%2.2f',MCS.PRI));
case 'SWAMI calfac',
    MCS.swami_cal_ii=val;
    MCS.make_parms(3)=str2num(MCS.swami_cal_opts{val});
    MCS.TAPE.calfac=MCS.make_parms(3);
case '[taumin,taumax]',
	MCS.tau_lim=str2num(char(str));
	if length(MCS.tau_lim)<=1, 
		MCS.tau_lim=[0,MCS.fft_parm(4)];
		set(hval,'String',sprintf('%2.2f,%2.2f',MCS.tau_lim));
	end
case 'Tape Address',
	if no_files(val,str,'Tape Address'), return; end
	MCS.tape_root=MCS.tape_files{val};
case 'Tfft,fs,iiauto',
	MCS.rm_parms([3,4,6])=str2num(char(str));
case 'T_fft,T_step',
	MCS.cts_parms(1:2)=str2num(char(str));
case 'Tfilt,drmax,iial',
    vals=str2num(char(str)); 
    if length(vals(:))~=3, disp('"Tfilt,drmax,iial" must have 3 entries'); return; end
    MCS.mc_parms(4:6)=vals(:)';
%case 'Fixed array',
%	MCS.mc_parms(1)=val;
case 'Time Specification';
	MCS.tspec=val;
case 'Tlab=HOD(seq)',
	MCS.iihr=val; MCS.iihr,
case 'TL Frequency Band(s)',
    MCS.TL_freq=str2num(char(str));
case 'TLm/SL Files',
	if no_files(val,str,'TLm/SL'), return; end
	MCS.TLm_root=MCS.TLm_files{val};
    eval(['load ',MCS.dir,MCS.TLm_root]);
case 'T_rep_int(0=PRI)',
    MCS.sg_parms(7)=str2num(char(str));
case 'Type of Data',
	MCS.make_parms(1)=val; ii_type=MCS.make_parms(1);
    MCS.TAPE.array_type=char(MCS.make_types(MCS.make_parms(1)));
    gui_box_vis(cback_fig,{'Input From Tape'},1);   %tms
    gui_box_vis(cback_fig,{'Data Files','Proc Mult Files','Set Files by Times'},MCS.TAPE.isatape==0); 
    gui_box_vis(cback_fig,{'Tape Address','Read Tape Jday,Jhour'},MCS.TAPE.isatape==1); 
    h1=findobj(cback_fig,'Tag','Proc Mult Files'); pmf=get(h1,'Value');
    gui_box_vis(cback_fig,{'Last Data File','Last Data Filelabel'},MCS.TAPE.isatape==0 & MCS.make_parms(7));
    gui_box_vis(cback_fig,{'Julian Day','Julian Daylabel'},1);
    atype=MCS.TAPE.array_type;
    h1=findobj(cback_fig,'Tag','Input From Tape'); MCS.TAPE.isatape=get(h1,'Value');
    showlist={'User Defined';'CM';'N/A'};
    gui_box_vis(cback_fig,'fs, calfac',any(strcmp(atype,showlist)));
    showlist={'User Defined';'CM';'SW_OMNI'};
    gui_box_vis(cback_fig,'Init Jday,Jhour',any(strcmp(atype,showlist)));
    showlist={'HLAB'};
    gui_box_vis(cback_fig,{'HLAB Endian';'HLA-B fs'},any(strcmp(atype,showlist)));   %tms
    showlist={'SWAMI'};
    gui_box_vis(cback_fig,{'SWAMI calfac','SWAMI calfaclabel'},any(strcmp(atype,showlist)));
    if (MCS.iiprog==15), gui_box_vis(cback_fig,{'SWAMI calfac','SWAMI calfaclabel'},0); end
    showlist={'SWAMI';'HLAB'};
    gui_box_vis(cback_fig,{'Use Fast Positioning'},(any(strcmp(atype,showlist)))&(MCS.TAPE.isatape==1));   %tms
    gui_box_vis(cback_fig,{'Read Tape Jday,Jhour'},MCS.TAPE.isatape==1);   %tms
    showlist={'SWAMI';'TVLA';'DVLA2002_OMNI';'DVLA2002_DIFAR';'DVLA2002_ALL'};
    gui_box_vis(cback_fig,{'Samples per Channel'},~any(strcmp(atype,showlist)));   %tms
    showlist={'User Defined'};
    gui_box_vis(cback_fig,{'File Header,Footer Bytes'},any(strcmp(atype,showlist)));   %tms
    gui_box_vis(cback_fig,{'Record Header,Footer Bytes'},any(strcmp(atype,showlist)));   %tms
    gui_box_vis(cback_fig,{'Data Word Format'},any(strcmp(atype,showlist)));   %tms
    gui_box_vis(cback_fig,{'Number of Channels'},any(strcmp(atype,showlist)));   %tms
    gui_box_vis(cback_fig,{'Byte Swap'},any(strcmp(atype,showlist)));   %tms
    if MCS.TAPE.isatape==1
        mcs_act('Tape Address');
    else
% 		switch MCS.TAPE.array_type,
%         case 'SWAMI'  % 2
%             sufs={'*.dat'}; lsufs=[0];
% 		otherwise
% 			sufs={'*.bin'}; lsufs=[0];
% 		end
		sufs={'*.bin','*.bin.lnk', '*.dat','*.dat.lnk'}; lsufs=[0 0 0 0];
		MCS.data_files=gui_pop_update(cback_fig,sufs,lsufs,'Data Files',MCS.data_root,MCS.dir);
		MCS.data_files=gui_pop_update(cback_fig,sufs,lsufs,'Last Data File',MCS.data_last,MCS.dir);
        mcs_act('Data Files');
    end
case 'Use Calibration File',
    gui_box_vis(cback_fig,{'Cal Files','Cal Fileslabel'},val);
    MCS.TAPE.ii_cal=val;
    if MCS.TAPE.ii_cal, mcs_act('Cal Files'); end
case 'Use Fast Positioning',
    h1=findobj(cback_fig,'Tag','Use Fast Positioning');MCS.TAPE.use_fp=get(h1,'Value');
    h1=findobj(cback_fig,'Tag','Input From Tape'); set(h1,'Value',MCS.TAPE.isatape);
case 'Use Header from CTS File',
	if no_files(val,str,'CTS'), return; end
	if isfield(MCS,'cts_heads'), MCS.cts_head=MCS.cts_heads{val}; end
%    if ~strcmp(MCS.cts_head,'None'),
%        CTS_head=cts_open(MCS.cts_head);
%        MCS.f_bb=CTS_head.f_bb_shift;
%        h1=findobj(cback_fig,'Tag','Baseband Freq (0=def)'); set(h1,'String',sprintf('%1.4f',MCS.f_bb));
%    end
case 'Use SL File',
    MCS.SL_use=val; ii_leave_lab=1; 
    gui_box_vis(cback_fig,'SL (-1=sim)',1-val,ii_leave_lab);
    gui_box_vis(cback_fig,'SL Files',val,ii_leave_lab);
    mcs_act('SL Files');
case 'View HDF Set',
    if ~isfield(MCS,'SD') | isempty(MCS.SD) | ~exist(MCS.SD.hdf_file,'file'),
        error('HDF not selected or HDF file selected does not exist locally.'); 
    end
	mcs_act('Dim Limits');
	if MCS.v_opts(1), iinewfig=1; else, figure(220); clf; iinewfig=0; end
    for jdim=1:MCS.SD.rank{MCS.hdf_jset}, 
        hdf_dim_lims{jdim}=str2num(MCS.hdf_dim_lims{jdim}); 
    end
    [DATA,status]=hdfsd_read(MCS.SD,hdf_dim_lims,MCS.hdf_jset); if status<0, return; end
    if MCS.v_opts(2),
        ii=findstr(DATA.dlab,' - '); if isempty(ii), ii=length(DATA.dlab)+1; end
        name=DATA.dlab(1:ii(1)-1); 
        name=strrep(name,' ','_'); name=strrep(name,'(','_'); name=strrep(name,')','_');  
        name=strrep(name,'|','_'); name=strrep(name,'[','_'); name=strrep(name,']','_');
        name=strrep(name,'#','_');
        str=['Mdat.',name,'=DATA;']; eval(str);
    end
    if length(MCS.clim)==2, 
        c_diff=diff(MCS.clim); d_diff=diff(DATA.clim); rat=c_diff/max(1.e-10,d_diff);
        if abs(MCS.clim(2)-DATA.clim(2)) < .4*c_diff & rat<3 & rat>1/3, V.clim=MCS.clim; DATA.clim=MCS.clim; end
    end
    view5(DATA,[],1,iinewfig,MCS.psize,1);
case 'Vertical Angles',
	MCS.th_lim=str2num(char(str));
case 'WN Gain(-3,-6)',
	MCS.abf_parm(3)=str2num(char(str));
case 'XCmin,XCmax',
	MCS.xclim=str2num(char(str));
case 'XC Pairs [nx2]',
	MCS.ph_pairs=str2num(char(str)); MCS.ph_pairs,
case 'Zero Pad by 2^N',
    MCS.sg_parms(2)=str2num(str);
case 'zpfac,ii_hann',
	MCS.cts_parms(3:4)=str2num(char(str));
otherwise,
    if length(action)>9 & strcmp(action(1:9),'CTS File '),
    	if no_files(val,str,'CTS Combine File'), return; end
        jcomb=str2num(action(10:end));
        MCS.comb_files{jcomb}=MCS.comb_cts{val};
        CTS=cts_open(MCS.comb_files{jcomb}); fclose(CTS.fid_cts);
        levels=CTS.header(8:min(end,17)); iival=find(levels~=-999);
        if length(iival)==1, 
            MCS.comb_levels(1,jcomb)=levels(iival);
        else
            MCS.comb_levels(1,jcomb)=-999;
        end
        tag1=sprintf(['CTS Level ',int2str(jcomb)]); tag2=[tag1,'label'];
        h1=findobj(cback_fig,'Tag',tag1);
        set(h1,'String',sprintf('%1.1f ',levels(iival)));
    elseif length(action)>14 & strcmp(action(1:14),'CTS New Level '),
        jcomb=str2num(action(15:end));
        MCS.comb_levels(2,jcomb)=str2num(char(str));
    elseif length(action)>14 & strcmp(action(1:16),'CTS Time Offset '),
        jcomb=str2num(action(17:end));
        MCS.comb_offsets(jcomb)=str2num(char(str));
    else
    	disp(['UNKNOWN ACTION in mcs_act: ',action]); 
    end
end
%
%%%
%
function over_wr=over_write(out_root,out_suf)
%
if isempty(out_root) & ~strcmp(out_suf,'_fft'),
	disp('Please fill in output file root before running.'); over_wr=0; return
end
%
out_file=[out_root,out_suf]; over_wr=1;
if exist(out_file,'file') == 2,
	disp(['WARNING: Output file ',out_file,' already exists!!!'])
	inp=input('Press return to overwrite, any other key to abort: ','s');
	if ~isempty(inp), over_wr=0; end
end
%
%%%
%
function iiret=no_files(val,str,file_kind), 
%
iiret=0;
if isempty(val) | val==0 | strcmp(str,'None Available'), 
%%	disp(['No ',file_kind,' files in currect directory!! cd to desired directory']);
	iiret=1;
end

function data_file_update(data_file,cback_fig,ii_last)

mcs_global

if (MCS.TAPE.isatape==1)
    disp(['Reading tape header.  Please wait a few seconds ...']);
end
MCS.TAPE.data_file=data_file;  % have isatape and array_type been set?
[MCS.TAPE] = ts_open(MCS.TAPE);
if MCS.TAPE.status <= 0
    return;
end
[MCS.TAPE] = ts_close(MCS.TAPE);
if MCS.TAPE.status <= 0
    return;
end
MCS.nph_all=MCS.TAPE.nchan;
MCS.make_parms(2)=MCS.TAPE.fs; 
if ii_last==1,
    MCS.make_parms(9)=MCS.TAPE.day;  % set day/hour of last file
    MCS.make_parms(6)=[MCS.TAPE.Dhour_end];
else
    MCS.make_parms(4:6)=[MCS.TAPE.day,MCS.TAPE.Dhour,MCS.TAPE.Dhour_end];
    MCS.make_parms(9)=MCS.TAPE.day;
end
Tlims=[MCS.make_parms(4),MCS.make_parms(5);MCS.make_parms(9),MCS.make_parms(6)]';
str=sprintf('%3.3i,%3.3f\n',Tlims);
h1=findobj(cback_fig,'Tag','Day,Hour');
set(h1,'String',str);
