function [n,code,location] = FIXDATA(file,varargin)
% FIXDATA to fix or edit NEWFIT format binary data
%
% [n,code,location] = FIXDATA(file) is the initial call to FIXDATA
% which processes the file named "file" and returns the number of
% stations found, "n", and a list of their names, "code", and binary
% offsets, "location".
%
% [n,code,location] = FIXDATA(file,n,code,location) are subsequent
% calls to FIXDATA.
%
% ASSUMPTIONS: Data for a given station are contiguous and in
%              increasing-time order.

if length(varargin)
   n=varargin{1};
   code=varargin{2};
   location=varargin{3};
else
   fprintf('\n...Please wait for initialization procedures...');
   [n,code,location]=setfixdata(file);
   fprintf('Done...\n');
end
fprintf('\n...Type h for help...\n\n');
sresponse='a';
while sresponse ~= 'q'
   sresponse=input('station> ','s');
   if ~ isempty(sresponse)
      switch sresponse
         case 'l'
            fprintf('\n...Station list:\n');
            cseq=strcat(cellstr(repmat('(',n,1))',cellstr(num2str((1:n)'))',cellstr(repmat(') ',n,1))');
            ccom=cellstr(repmat(',',n,1))';
            ccom{n}=' ';
            nful=floor(n/7);
            nrem=mod(n,7);
            for j=1:nful;
               cblk=[cseq(7*(j-1)+1:7*j);code(7*(j-1)+1:7*j);ccom(7*(j-1)+1:7*j)];
               fprintf('\n%s %s%s %s %s%s %s %s%s %s %s%s %s %s%s %s %s%s %s %s%s\n',cblk{1:3,1:7});
            end
            if nrem
               cblk=[cseq(7*nful+1:n);code(7*nful+1:n);ccom(7*nful+1:n)];
               fprintf('\n%s %s%s %s %s%s %s %s%s %s %s%s %s %s%s %s %s%s %s %s%s\n',cblk{1:3,1:nrem});
            end
            fprintf('\n\n');
         case 'p'
            slbl={'y';'n'};
            fprintf('\n...Process a contiguous range of stations? (y/n)\n\n');
            sresponse='a';
            while ~ sum(strcmp(sresponse,slbl))
               sresponse=input('station-range> ','s');
               if ~ isempty(sresponse)
                  switch sresponse;
                     case 'y'
                        xnam=0;
                        while ~ xnam
                           jnam=0;
                           while ~ jnam
                              fprintf('\n...Select first station name...\n\n');
                              rnam=[];
                              while isempty(rnam)
                                 rnam=input('first-name> ','s');
                              end
                              jnam=find(strcmp(deblank(rnam),code(1:n)));
                              if isempty(jnam)
                                 jnam=0;
                                 fprintf('\n...No location found for station: %s...\n\n',deblank(rnam));
                              end
                           end
                           jnxt=0;
                           while ~ jnxt
                              fprintf('\n...Select last station name...\n\n');
                              rnam=[];
                              while isempty(rnam)
                                 rnam=input('last-name> ','s');
                              end
                              jnxt=find(strcmp(deblank(rnam),code(1:n)));
                              if isempty(jnxt)
                                 jnxt=0;
                                 fprintf('\n...No location found for station: %s...\n\n',deblank(rnam));
                              end
                           end
                           xnam=jnxt-jnam;
                           if xnam <= 0
                              xnam=0;
                              fprintf('\n...First station must be before last station on list...\n\n');
                           end
                        end
                        tnam(1:xnam+1)=code(jnam:jnxt);
                        pnam=[tnam{1},' -> ',tnam{xnam+1}];
                     case 'n'
                        jnam=0;
                        while ~ jnam
                           fprintf('\n...Select single station name...\n\n');
                           rnam=[];
                           while isempty(rnam)
                              rnam=input('single-name> ','s');
                           end
                           jnam=find(strcmp(deblank(rnam),code(1:n)));
                           if isempty(jnam)
                              jnam=0;
                              fprintf('\n...No location found for station: %s...\n\n',deblank(rnam));
                           end
                        end
                        xnam=0;
                        tnam{1}=rnam;
                        pnam=tnam{1};
                     otherwise
                        switch sresponse
                           case 'h'
                              fprintf('\n');
                           otherwise
                              fprintf('\n...Command not recognized...\n\n');
                        end
                        fprintf('...Commands:  y  -> yes\n');
                        fprintf('              n  -> no\n');
                  end
               else
                  sresponse='a';
               end
            end
            fprintf('\n...Select beginning year, month, day, millisec in brackets as [yyyy mm dd mmmmmmmm]...\n\n');
            tb=[];
            while isempty(tb)
               tb=input('time-begin> ');
            end
            fprintf('\n...Select ending year, month, day, millisec in brackets as [yyyy mm dd mmmmmmmm]...\n\n');
            te=[];
            while isempty(te)
               te=input('time-end> ');
            end
            fprintf('\n...Select quality code of inliers to process...\n\n');
            qual=[];
            while isempty(qual)
               qual=input('quality-inliers> ');
            end
            fprintf('\n...Select quality code of outliers to assign...\n\n');
            qout=[];
            while isempty(qout)
               qout=input('quality-outliers> ');
            end
            qmap=[qual,qout,1];
            rlbl={'y';'n'};
            fprintf('\n...Reprocess old outliers? (y/n)\n\n');
            rresponse='a';
            while ~ sum(strcmp(rresponse,rlbl))
               rresponse=input('reprocess-outliers> ','s');
               if ~ isempty(rresponse)
                  switch rresponse;
                     case 'y'
                        qget=[qual,qout];
                     case 'n'
                        qget=qual;
                     otherwise
                        switch rresponse
                           case 'h'
                              fprintf('\n');
                           otherwise
                              fprintf('\n...Command not recognized...\n\n');
                        end
                        fprintf('...Commands:  y  -> yes\n');
                        fprintf('              n  -> no\n');
                  end
               else
                  rresponse='a';
               end
            end
            albl={'y';'n'};
            wlbl={'y';'n'};
            elbl={'y';'n'};
            olbl={'o';'i'};
            mlbl={'length';'max';'min';'mean';'median';'std'};
            plbl={'poly';'filter';'gain';'bias'};
            ulbl={'y';'n'};
            clbl={'X';'Y';'Z';'Xt';'Yt';'Zt';'D';'I';'H';'B';'Dt';'It';'Ht';'Bt'};
            cval=[1:1:14];
            [mjdb,dayofyearb]=ymdtomjd(tb(1),tb(2),tb(3));
            [mjde,dayofyeare]=ymdtomjd(te(1),te(2),te(3));
            mjdfb=mjdb+tb(4)/86400000;
            mjdfe=mjde+te(4)/86400000;
            cnum=1;
            chot=1;
            bplt=0;
            bnew=0;
            bund=0;
            blev=0;
            bpar=ones(1,14);
            brkv(1)=mjdfb;
            tlev=1;
            tbl(tlev)=mjdfb;
            tel(tlev)=mjdfe;
            k=zeros(1,14);
            t=[];
            b=[];
            q=[];
            for j=1:xnam+1
               fprintf('\n...Loading data for station %s between mjd''s %f and %f...',tnam{j},tbl(tlev),tel(tlev));
               ksav=k;
               [jget,k,t,b,q]=getfixdata(file,tnam{j},tb,te,qget,k,t,b,q,n,code,location);
               fprintf('Done...\n\n');
               baux(j)=0;
               bidf(j)=0;
               bidn{j}=tnam{j};
               bidx(j)=0;
               bidy(j)=0;
               bmef(j)=0;
               bmen{j}='mean';
               bmex(j)=0;
               bmey(j)=0;
               if jget > 0
                  if max(k-ksav) == 0
                     fprintf('\n...No data found for station %s between mjd''s %f and %f...\n\n',tnam{j},tbl(tlev),tel(tlev));
                     fresponse='q';
                     break;
                  else
                     first=1;
                     fresponse='a';
                     [kmax,ctry]=max(k);
                     clev(cnum)=ctry;
                     if j > 1
                        knoz=find((ksav > 0) & (k > ksav));
                        tbot=max(t(sub2ind(size(t),ksav(knoz),knoz)));
                        ttop=min(t(sub2ind(size(t),ksav(knoz)+1,knoz)));
                        if (ttop > tbot)
                           blev=blev+1;
                           brkv(blev+1)=(ttop+tbot)/2.0;
                        else
                           fprintf('\n...Data times for station %s overlap previous station...\n\n',tnam{j});
                           fresponse='q';
                           break;
                        end
                     end
                  end
               else
                  fresponse='q';
                  break;
               end
            end
            if fresponse ~= 'q'
               brkv(blev+2)=mjdfe;
               fprintf('\n...Begin processing...type h for help...\n\n');
            end
            while fresponse ~= 'q'
               fresponse=input('fix> ','s');
               if ~ isempty(fresponse)
                  switch fresponse
                     case 'c'
                        cnum=0;
                        chot=1;
                        mresponse='a';
                        while mresponse ~= 'n'
                           fprintf('\n...Select component to process...\n\n');
                           cresponse='a';
                           while ~ sum(strcmp(cresponse,clbl(:,1)))
                              cresponse=input('component> ','s');
                              if ~ isempty(cresponse)
                                 switch sum(strcmp(cresponse,clbl(:,1)));
                                    case 1
                                       ctry=cval(find(strcmp(cresponse,clbl(:,1))));
                                       if k(ctry) == 0
                                          cresponse='a';
                                          fprintf('\n...No data for this component...\n\n');
                                       else
                                          cnum=cnum+1;
                                          clev(cnum)=ctry;
                                          h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf, ...
                                                        bidn,bidx,bidy,clbl,tbl,tel,brkv,k,t,b,q);
                                       end
                                    otherwise
                                       switch cresponse
                                          case 'h'
                                             fprintf('\n');
                                          otherwise
                                             fprintf('\n...Command not recognized...\n\n');
                                       end
                                       fprintf('...Commands:  X  -> X component\n');
                                       fprintf('              Y  -> Y component\n');
                                       fprintf('              Z  -> Z component\n');
                                       fprintf('              D  -> D component\n');
                                       fprintf('              I  -> I component\n');
                                       fprintf('              H  -> H component\n');
                                       fprintf('              B  -> B component\n');
                                       fprintf('              Xt -> dX/dt component\n');
                                       fprintf('              Yt -> dY/dt component\n');
                                       fprintf('              Zt -> dZ/dt component\n');
                                       fprintf('              Dt -> dD/dt component\n');
                                       fprintf('              It -> dI/dt component\n');
                                       fprintf('              Ht -> dH/dt component\n');
                                       fprintf('              Bt -> dB/dt component\n');
                                 end
                              else
                                 cresponse='a';
                              end
                           end
                           fprintf('\n...Process another component simultaneously? (y/n)\n\n');
                           mresponse='a';
                           while ~ sum(strcmp(mresponse,albl))
                              mresponse=input('multiple-process> ','s');
                              if ~ isempty(mresponse)
                                 switch mresponse;
                                    case 'y'
                                    case 'n'
                                    otherwise
                                       switch mresponse
                                          case 'h'
                                             fprintf('\n');
                                          otherwise
                                             fprintf('\n...Command not recognized...\n\n');
                                       end
                                       fprintf('...Commands:  y  -> yes\n');
                                       fprintf('              n  -> no\n');
                                 end
                              else
                                 mresponse='a';
                              end
                           end
                        end
                     case 'd'
                        bplt=1-bplt;
                        h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy,clbl, ...
                                      tbl,tel,brkv,k,t,b,q);
                     case 'g'
                        qmap(3)=1-qmap(3);
                        h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy,clbl, ...
                                      tbl,tel,brkv,k,t,b,q);
                     case 'f'
                        h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy,clbl, ...
                                      tbl,tel,brkv,k,t,b,q);
                     case 'b'
                        if tlev > 1
                           tlev=tlev-1;
                        else
                           fprintf('\n...Cannot zoom out...\n\n');
                        end
                        h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy,clbl, ...
                                      tbl,tel,brkv,k,t,b,q);
                     case 'z'
                        if first
                           first=0;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        fprintf('\n...Create zoom bracket with left mouse clicks...\n\n');
                        xi(1,1)=0;
                        xi(2,1)=0;
                        while xi(1,1) == xi(2,1)
                           [xi,yi]=ginput(2);
                           if xi(1,1) == xi(2,1)
                              fprintf('\n...First click must be different than second click...\n');
                           end
                        end
                        tlev=tlev+1;
                        tbl(tlev)=min([xi(1,1),xi(2,1)]);
                        tel(tlev)=max([xi(1,1),xi(2,1)]);
                        h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy,clbl, ...
                                      tbl,tel,brkv,k,t,b,q);
                     case 'a'
                        if first
                           first=0;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        if cnum > 1
                           fprintf('\n...Left mouse click on plot of component to be affected...\n\n');
                           [xi,yi]=ginput(1);
                           chot=find(gca == h(1:cnum));
                        else
                           chot=1;
                        end
                        fprintf('\n...Right mouse click on final partition to process for outliers, otherwise left...\n\n');
                        butt=1;
                        while butt == 1 
                           [xi,yi,butt]=ginput(1);
                           beoi=find(xi >= brkv(1:blev+1) & xi < brkv(2:blev+2));
                           if isempty(beoi)
                              if xi >= brkv(blev+2)
                                 beoi=blev+1;
                              else
                                 beoi=1;
                              end
                           end
                           fprintf('\n...Specify excessive outlier magnitude for this data partition...\n\n');
                           omag=-1;
                           while omag < 0
                              omag=input('outlier-magnitude> ');
                              if ~ isempty(omag)
                                 if omag < 0
                                    fprintf('\n...Outlier magnitude must be non-negative...\n\n');
                                 end
                              else
                                 omag=-1;
                              end
                           end
                           tmap=find(t(1:k(clev(chot)),clev(chot)) >= brkv(beoi) & ...
                                     t(1:k(clev(chot)),clev(chot)) <= brkv(beoi+1));
                           tout=find(abs(b(tmap,clev(chot),bpar(clev(chot)))) >= omag);
                           fprintf('\n...Assign as outliers (o) or inliers (i)?\n\n');
                           oresponse='a';
                           while ~ sum(strcmp(oresponse,olbl))
                              oresponse=input('outliers> ','s');
                              if ~ isempty(oresponse)
                                 switch oresponse;
                                    case 'o'
                                       qass=qout;
                                    case 'i'
                                       qass=qual;
                                    otherwise
                                       switch oresponse
                                          case 'h'
                                             fprintf('\n');
                                          otherwise
                                             fprintf('\n...Command not recognized...\n\n');
                                       end
                                       fprintf('...Commands:  o  -> outliers\n');
                                       fprintf('              i  -> inliers\n');
                                 end
                              else
                                 oresponse='a';
                              end
                           end
                           if ~ isempty(tout)
                              q(tmap(tout),clev(chot))=qass;
                           end
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                     case 'o'
                        if first
                           first=0;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        if cnum > 1
                           fprintf('\n...Left mouse click on plot of component to be affected...\n\n');
                           [xi,yi]=ginput(1);
                           chot=find(gca == h(1:cnum));
                        else
                           chot=1;
                        end
                        fprintf('\n...Create outlier bracket with left mouse clicks...\n\n');
                        xi(1,1)=0;
                        xi(2,1)=0;
                        while xi(1,1) == xi(2,1)
                           [xi,yi]=ginput(2);
                           if xi(1,1) == xi(2,1)
                              fprintf('\n...First click must be different than second click...\n');
                           end
                        end
                        tout=find(t(1:k(clev(chot)),clev(chot)) >= min([xi(1,1),xi(2,1)]) & ...
                                  t(1:k(clev(chot)),clev(chot)) <= max([xi(1,1),xi(2,1)]));
                        fprintf('\n...Assign as outliers (o) or inliers (i)?\n\n');
                        oresponse='a';
                        while ~ sum(strcmp(oresponse,olbl))
                           oresponse=input('outliers> ','s');
                           if ~ isempty(oresponse)
                              switch oresponse;
                                 case 'o'
                                    qass=qout;
                                 case 'i'
                                    qass=qual;
                                 otherwise
                                    switch oresponse
                                       case 'h'
                                          fprintf('\n');
                                       otherwise
                                          fprintf('\n...Command not recognized...\n\n');
                                    end
                                    fprintf('...Commands:  o  -> outliers\n');
                                    fprintf('              i  -> inliers\n');
                              end
                           else
                              oresponse='a';
                           end
                        end
                        if ~ isempty(tout)
                           q(tout,clev(chot))=qass;
                        end
                        h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy,clbl, ...
                                      tbl,tel,brkv,k,t,b,q);
                     case 'x'
                        if first
                           first=0;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        fprintf('\n...Left mouse click nearest prospective outlier...\n\n');
                        [xi,yi]=ginput(1);
                        chot=find(gca == h(1:cnum));
                        [xm,tout]=min(abs(xi-t(1:k(clev(chot)),clev(chot))));
                        fprintf('\n...Assign as outlier (o) or inlier (i)?\n\n');
                        oresponse='a';
                        while ~ sum(strcmp(oresponse,olbl))
                           oresponse=input('outliers> ','s');
                           if ~ isempty(oresponse)
                              switch oresponse;
                                 case 'o'
                                    qass=qout;
                                 case 'i'
                                    qass=qual;
                                 otherwise
                                    switch oresponse
                                       case 'h'
                                          fprintf('\n');
                                       otherwise
                                          fprintf('\n...Command not recognized...\n\n');
                                    end
                                    fprintf('...Commands:  o  -> outliers\n');
                                    fprintf('              i  -> inliers\n');
                              end
                           else
                              oresponse='a';
                           end
                        end
                        if ~ isempty(tout)
                           q(tout,clev(chot))=qass;
                        end
                        h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy,clbl, ...
                                      tbl,tel,brkv,k,t,b,q);
                     case 's'
                        if first
                           first=0;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        fprintf('\n...Select break with one left mouse click...\n\n');
                        bind=1;
                        while bind
                           [xi,yi]=ginput(1);
                           bind=sum(xi(1,1) == brkv(1:blev+2));
                           if bind
                              fprintf('\n...Selected break is repeat of existing break...\n');
                           end
                        end
                        blev=blev+1;
                        temp=brkv(1:blev+1);
                        temp(blev+2)=xi(1,1);
                        brkv=sort(temp);
                        bnew=find(xi(1,1) == brkv)-1;
                        temp=bidn(bnew:blev);
                        bidn(bnew)=temp(1);
                        bidn(bnew+1:blev+1)=temp;
                        bidf(blev+1)=0;
                        bmen{blev+1}='mean';
                        bmef(blev+1)=0;
                        baux(blev+1)=0;
                        h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy,clbl, ...
                                      tbl,tel,brkv,k,t,b,q);
                        bnew=0;
                     case 'u'
                        if first
                           first=0;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        if blev > 0
                           fprintf('\n...Left mouse click nearest break to undo...\n\n');
                           [xi,yi]=ginput(1);
                           [xm,bund]=min(abs(xi-brkv(2:blev+1)));
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                           fprintf('\n...Undo indicated break? (y/n)\n\n');
                           uresponse='a';
                           while ~ sum(strcmp(uresponse,ulbl))
                              uresponse=input('undo> ','s');
                              if ~ isempty(uresponse)
                                 switch uresponse;
                                    case 'y'
                                       blev=blev-1;
                                       brkv(bund+1)=[]; 
                                       bidn(bund)=[];
                                       bidf(bund)=[];
                                       bmen(bund)=[];
                                       bmef(bund)=[];
                                       baux(bund)=[];
                                    case 'n'
                                    otherwise
                                       switch uresponse
                                          case 'h'
                                             fprintf('\n');
                                          otherwise
                                             fprintf('\n...Command not recognized...\n\n');
                                       end
                                       fprintf('...Commands:  y  -> yes\n');
                                       fprintf('              n  -> no\n');
                                 end
                              else
                                 uresponse='a';
                              end
                           end
                           bund=0;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        else
                           fprintf('\n...No breaks exist...\n\n');
                        end
                     case 'i'
                        if first
                           first=0;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        if cnum > 1
                           fprintf('\n...Left mouse click on plot of component to be affected...\n\n');
                           [xi,yi]=ginput(1);
                           chot=find(gca == h(1:cnum));
                        else
                           chot=1;
                        end
                        fprintf('\n...Right mouse click on final partition to identify, otherwise left...\n\n');
                        butt=1;
                        while butt == 1 
                           [xi,yi,butt]=ginput(1);
                           bidi=find(xi >= brkv(1:blev+1) & xi < brkv(2:blev+2));
                           if isempty(bidi)
                              if xi >= brkv(blev+2)
                                 bidi=blev+1;
                              else
                                 bidi=1;
                              end
                           end
                           bidf(bidi)=1;
                           bidx(bidi)=xi;
                           bidy(bidi)=yi;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        bidf(:)=0;
                     case 'n'
                        if first
                           first=0;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        if cnum > 1
                           fprintf('\n...Left mouse click on plot of component to be affected...\n\n');
                           [xi,yi]=ginput(1);
                           chot=find(gca == h(1:cnum));
                        else
                           chot=1;
                        end
                        fprintf('\n...Right mouse click on final partition to rename, otherwise left...\n\n');
                        butt=1;
                        while butt == 1 
                           [xi,yi,butt]=ginput(1);
                           bidi=find(xi >= brkv(1:blev+1) & xi < brkv(2:blev+2));
                           if isempty(bidi)
                              if xi >= brkv(blev+2)
                                 bidi=blev+1;
                              else
                                 bidi=1;
                              end
                           end
                           bidf(bidi)=1;
                           bidx(bidi)=xi;
                           bidy(bidi)=yi;
                           fprintf('\n...Enter new name of current partition (up to 15 characters)...\n\n');
                           nresponse='                ';
                           while length(nresponse) > 15
                              nresponse=input('rename> ','s');
                              if ~ isempty(nresponse)
                                 if length(nresponse) > 15
                                    fprintf('\n...Name must be 15 characters or less...\n\n');
                                 else
                                    bidn{bidi}=nresponse;
                                 end
                              else
                                 nresponse='                ';
                              end
                           end
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        bidf(:)=0;
                     case 'm'
                        if first
                           first=0;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        if cnum > 1
                           fprintf('\n...Left mouse click on plot of component to be affected...\n\n');
                           [xi,yi]=ginput(1);
                           chot=find(gca == h(1:cnum));
                        else
                           chot=1;
                        end
                        fprintf('\n...Right mouse click on final partition and measure, otherwise left...\n\n');
                        butt=1;
                        while butt == 1 
                           [xi,yi,butt]=ginput(1);
                           bmei=find(xi >= brkv(1:blev+1) & xi < brkv(2:blev+2));
                           if isempty(bmei)
                              if xi >= brkv(blev+2)
                                 bmei=blev+1;
                              else
                                 bmei=1;
                              end
                           end
                           bmef(bmei)=1;
                           bmex(bmei)=xi;
                           bmey(bmei)=yi;
                           fprintf('\n...Enter name of measure for current partition...\n\n');
                           mresponse='a';
                           while ~ sum(strcmp(mresponse,mlbl))
                              mresponse=input('measure> ','s');
                              if ~ isempty(mresponse)
                                 switch sum(strcmp(mresponse,mlbl));
                                    case 1
                                       mlev=find(strcmp(mresponse,mlbl));
                                       tmap=find(t(1:k(clev(chot)),clev(chot)) >= max([tbl(tlev),brkv(bmei)]) & ...
                                                 t(1:k(clev(chot)),clev(chot)) <= min([tel(tlev),brkv(bmei+1)]));
                                       tinl=find(q(tmap,clev(chot)) == qual);
                                       if length(tinl) > 0
                                          bmen{bmei}=[mlbl{mlev},' = ',num2str(feval(mlbl{mlev}, ...
                                                      b(tmap(tinl),clev(chot),bpar(clev(chot)))))];
                                       else
                                          bmef(bmei)=0;
                                          bmen{bmei}=' ';
                                          fprintf('\n...No inlier data in visible partition...\n\n');
                                       end
                                    otherwise
                                       switch mresponse
                                          case 'h'
                                             fprintf('\n');
                                          otherwise
                                             fprintf('\n...Command not recognized...\n\n');
                                       end
                                       fprintf('...Commands:  length -> Number of data\n');
                                       fprintf('              max    -> Maximum\n');
                                       fprintf('              min    -> Minimum\n');
                                       fprintf('              mean   -> Mean\n');
                                       fprintf('              median -> Median\n');
                                       fprintf('              std    -> Standard deviation\n');
                                 end
                              else
                                 mresponse='a';
                              end
                           end
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bmef,bmen,bmex,bmey, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        bmef(:)=0;
                     case 'p'
                        if first
                           first=0;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        if cnum > 1
                           fprintf('\n...Left mouse click on plot of component to be affected...\n\n');
                           [xi,yi]=ginput(1);
                           chot=find(gca == h(1:cnum));
                        else
                           chot=1;
                        end
                        b(1:k(clev(chot)),clev(chot),bpar(clev(chot))+1)=b(1:k(clev(chot)),clev(chot),bpar(clev(chot)));
                        fprintf('\n...Right mouse click on final partition and process, otherwise left...\n\n');
                        butt=1;
                        while butt == 1 
                           [xi,yi,butt]=ginput(1);
                           bpri=find(xi >= brkv(1:blev+1) & xi < brkv(2:blev+2));
                           if isempty(bpri)
                              if xi >= brkv(blev+2)
                                 bpri=blev+1;
                              else
                                 bpri=1;
                              end
                           end
                           baux(bpri)=1;
                           fprintf('\n...Enter name of process for current partition...\n\n');
                           presponse='a';
                           while ~ sum(strcmp(presponse,plbl))
                              presponse=input('process> ','s');
                              if ~ isempty(presponse)
                                 switch sum(strcmp(presponse,plbl));
                                    case 1
                                       tmap=find(t(1:k(clev(chot)),clev(chot)) >= max([tbl(tlev),brkv(bpri)]) & ...
                                                 t(1:k(clev(chot)),clev(chot)) <= min([tel(tlev),brkv(bpri+1)]));
                                       tinl=find(q(tmap,clev(chot)) == qual);
                                       if length(tinl) > 0
                                          plev=find(strcmp(presponse,plbl));
                                          switch presponse 
                                             case 'poly'
                                                fprintf('\n...Enter polynomial degree...\n\n');
                                                np=-1;
                                                while np < 0
                                                   np=input('poly-degree> ');
                                                   if ~ isempty(np)
                                                      if np < 0
                                                         fprintf('\n...Polynomial degree must be non-negative...\n\n');
                                                      end
                                                   else
                                                      np=-1;
                                                   end
                                                end
                                                tfit=(t(tmap,clev(chot))-mean(t(tmap(tinl),clev(chot))))./ ...
                                                     std(t(tmap(tinl),clev(chot)));
                                                b(tmap,clev(chot),bpar(clev(chot))+1)=polyval(polyfit(tfit(tinl), ...
                                                                          b(tmap(tinl),clev(chot),bpar(clev(chot))),np),tfit);
                                             case 'filter'
                                                fprintf('\n...Enter coefficients for explicit data polynomial...\n\n');
                                                ap=[];
                                                while length(ap) == 0
                                                   ap=input('filter-explicit> ');
                                                end
                                                fprintf('\n...Enter coefficients for implicit data polynomial...\n\n');
                                                bp=[];
                                                while length(bp) == 0
                                                   bp=input('filter-implicit> ');
                                                end
                                                b(tmap(tinl),clev(chot),bpar(clev(chot))+1)=filter(bp,ap,b(tmap(tinl), ...
                                                                                            clev(chot),bpar(clev(chot))));
                                             case 'gain'
                                                fprintf('\n...Enter gain...\n\n');
                                                gain=[];
                                                while isempty(gain)
                                                   gain=input('gain> ');
                                                end
                                                b(tmap(tinl),clev(chot),bpar(clev(chot))+1)=b(tmap(tinl),clev(chot), ...
                                                                                            bpar(clev(chot)))*gain;
                                             otherwise
                                                fprintf('\n...Enter bias...\n\n');
                                                bias=[];
                                                while isempty(bias)
                                                   bias=input('bias> ');
                                                end
                                                b(tmap(tinl),clev(chot),bpar(clev(chot))+1)=b(tmap(tinl),clev(chot), ...
                                                                                            bpar(clev(chot)))+bias;
                                          end
                                       else
                                          baux(bpri)=0;
                                          fprintf('\n...No inlier data in visible partition...\n\n');
                                       end
                                    otherwise
                                       switch presponse
                                          case 'h'
                                             fprintf('\n');
                                          otherwise
                                             fprintf('\n...Command not recognized...\n\n');
                                       end
                                       fprintf('...Commands:  poly   -> Polynomial fit\n');
                                       fprintf('              filter -> Tapped delay-line filter\n');
                                       fprintf('              gain   -> Multiply by a gain\n');
                                       fprintf('              bias   -> Add a bias\n');
                                 end
                              else
                                 presponse='a';
                              end
                           end
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        clbl{clev(chot),bpar(clev(chot))+1}=clbl{clev(chot),bpar(clev(chot))};
                        bpar(clev(chot))=bpar(clev(chot))+1;
                        baux(:)=0;
                     case 'l'
                        if first
                           first=0;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        if cnum > 1
                           fprintf('\n...Left mouse click on plot of component to be affected...\n\n');
                           [xi,yi]=ginput(1);
                           chot=find(gca == h(1:cnum));
                        else
                           chot=1;
                        end
                        if bpar(clev(chot)) > 1
                           bpar(clev(chot))=bpar(clev(chot))-1;
                        else
                           fprintf('\n...Cannot go back one level...\n\n');
                        end
                        h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy,clbl, ...
                                      tbl,tel,brkv,k,t,b,q);
                     case 'r'
                        if first
                           first=0;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        if cnum > 1
                           fprintf('\n...Left mouse click on plot of component to be affected...\n\n');
                           [xi,yi]=ginput(1);
                           chot=find(gca == h(1:cnum));
                        else
                           chot=1;
                        end
                        if bpar(clev(chot)) > 1
                           bpar(clev(chot))=bpar(clev(chot))+1;
                           b(:,clev(chot),bpar(clev(chot)))=b(:,clev(chot),bpar(clev(chot))-2)- ...
                                                            b(:,clev(chot),bpar(clev(chot))-1);
                           clbl{clev(chot),bpar(clev(chot))}=['\Delta ',clbl{clev(chot),bpar(clev(chot))-1}];
                        else
                           fprintf('\n...Not enough processing levels...\n\n');
                        end
                        h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy,clbl, ...
                                      tbl,tel,brkv,k,t,b,q);
                     case 'w'
                        if first
                           first=0;
                           h=plotfixdata(pnam,qmap,cnum,chot,clev,tlev,blev,bnew,bund,bplt,bpar,baux,bidf,bidn,bidx,bidy, ...
                                         clbl,tbl,tel,brkv,k,t,b,q);
                        end
                        xbeg=0;
                        jbeg=jnam;
                        if ~ strcmp(code{jnam},bidn{1})
                           fprintf('\n...Extend left-most partition name to data earlier than current window? (y/n)\n\n');
                           eresponse='a';
                           while ~ sum(strcmp(eresponse,elbl))
                              eresponse=input('extend-earlier> ','s');
                              if ~ isempty(eresponse)
                                 switch eresponse;
                                    case 'y'
                                       xbeg=1;
                                       jbeg=jnam-1;
                                    case 'n'
                                    otherwise
                                       switch eresponse
                                          case 'h'
                                             fprintf('\n');
                                          otherwise
                                             fprintf('\n...Command not recognized...\n\n');
                                       end
                                       fprintf('...Commands:  y  -> yes\n');
                                       fprintf('              n  -> no\n');
                                 end
                              else
                                 eresponse='a';
                              end
                           end
                        end
                        xend=0;
                        jend=jnam+xnam;
                        if ~ strcmp(code{jnam+xnam},bidn{blev+1})
                           fprintf('\n...Extend right-most partition name to data later than current window? (y/n)\n\n');
                           eresponse='a';
                           while ~ sum(strcmp(eresponse,elbl))
                              eresponse=input('extend-later> ','s');
                              if ~ isempty(eresponse)
                                 switch eresponse;
                                    case 'y'
                                       xend=1;
                                       jend=jnam+xnam+1;
                                    case 'n'
                                    otherwise
                                       switch eresponse
                                          case 'h'
                                             fprintf('\n');
                                          otherwise
                                             fprintf('\n...Command not recognized...\n\n');
                                       end
                                       fprintf('...Commands:  y  -> yes\n');
                                       fprintf('              n  -> no\n');
                                 end
                              else
                                 eresponse='a';
                              end
                           end
                        end
                        jbad=0;
                        for j=1:blev+1
                           cmap=find(strcmp([code(1:jbeg),bidn(1:blev+1),code(jend:n)],bidn{j}));
                           if length(cmap) > 1
                              if max(diff(cmap)) > 1
                                 jbad=1;
                                 fprintf('\n...Non-contiguous data segments found for station %s...\n\n',bidn{j});
                                 break;
                              end
                           end
                        end
                        if ~ jbad
                           bmap=[];
                           for j=1:xnam+1
                              bmap=[bmap,find(strcmp(bidn(1:blev+1),tnam{j}))];
                           end
                           jchg=0;
                           if (length(bmap) ~= blev+1) | (max(diff(bmap)) > 1)
                              jchg=1;
                           end
                           if jchg & ~ xnam & ~ xbeg & ~ xend
                              jbad=1;
                              fprintf('\n...Non-contiguous data segments found for station %s...\n\n',deblank(tnam{1}));
                           end
                        end
                        if ~ jbad
                           fprintf('\n...Overwrite original with current processing level of all measurement data? (y/n)\n\n');
                           wresponse='a';
                           while ~ sum(strcmp(wresponse,wlbl))
                              wresponse=input('overwrite> ','s');
                              if ~ isempty(wresponse)
                                 switch wresponse;
                                    case 'y'
                                       over=1;
                                    case 'n'
                                       over=0;
                                    otherwise
                                       switch wresponse
                                          case 'h'
                                             fprintf('\n');
                                          otherwise
                                             fprintf('\n...Command not recognized...\n\n');
                                       end
                                       fprintf('...Commands:  y  -> yes\n');
                                       fprintf('              n  -> no\n');
                                 end
                              else
                                 wresponse='a';
                              end
                           end
                           fprintf('\n...Writing data for station(s) %s...',pnam);
                           [jret,n,code,location]=putfixdata(file,tnam,jnam,xnam,qget,xbeg,xend,blev,bidn,brkv,over,bpar, ...
                                                             b,q,n,code,location);
                           fprintf('Done...\n\n');
                           if jchg
                              fprintf('\n...Station name changes detected...processing of current station ending...\n\n');
                              fresponse='q';
                           end
                        end
                     case 'q'
                     otherwise
                        switch fresponse
                           case 'h'
                              fprintf('\n');
                           otherwise
                              fprintf('\n...Command not recognized...\n\n');
                        end
                        fprintf('...Commands:  h -> help\n');
                        fprintf('              c -> select components\n');
                        fprintf('              d -> data symbol plot toggle\n');
                        fprintf('              f -> redraw\n');
                        fprintf('              s -> select break\n');
                        fprintf('              u -> undo break\n');
                        fprintf('              a -> flag or unflag outlier excessive magnitudes for data partition\n');
                        fprintf('              o -> flag or unflag outlier segments\n');
                        fprintf('              x -> flag or unflag outlier individuals\n');
                        fprintf('              z -> zoom in\n');
                        fprintf('              b -> zoom out\n');
                        fprintf('              i -> identify data partitions\n');
                        fprintf('              n -> rename data partitions\n');
                        fprintf('              m -> measure visible inlier data partitions\n');
                        fprintf('              p -> process visible inlier data partitions\n');
                        fprintf('              l -> go back one level of processing\n');
                        fprintf('              r -> residuals of last w/r to current processing level\n');
                        fprintf('              g -> outlier plot toggle\n');
                        fprintf('              w -> save\n');
                        fprintf('              q -> quit\n');
                  end 
               else
                  fresponse='a';
               end
            end
         case 'q'
         otherwise
            switch sresponse
               case 'h'
                  fprintf('\n');
               otherwise
                  fprintf('\n...Command not recognized...\n\n');
            end
            fprintf('...Commands:  p -> process stations\n');
            fprintf('              l -> list stations\n');
            fprintf('              q -> quit\n');
      end
   else
      sresponse='a';
   end
end