
  %    store all the data in a matrix "y"
    if wctd==1;
      y=[cumdist(1)*ones(length(pmatrix),1) pmatrix(:,1) tmatrix(:,1)  smatrix(:,1) rmatrix(:,1) trmatrix(:,1) fmatrix(:,1)];
      for i=2:ncasts;
        y=[y; cumdist(i)*ones(length(pmatrix),1) pmatrix(:,i) tmatrix(:,i) smatrix(:,i) rmatrix(:,i) trmatrix(:,i) fmatrix(:,i)];
      end; 
    else;  
      y=[cumdist(1)*ones(length(pmatrix),1) -1*pmatrix(:,1) tmatrix(:,1)  smatrix(:,1)];
      for i=2:ncasts;
        y=[y; cumdist(i)*ones(length(pmatrix),1) -1*pmatrix(:,i) tmatrix(:,i)  smatrix(:,i)];
      end; 
    end;

  % rid of all NaN's
    idd=find(isnan(y(:,2))==0);

  % write to an XYZ.dat file for ascii input by SURFER
     yy=y(idd,:);
     save xyz.dat yy -ascii    

  % write a blanking file using posn.dat depth
     a=[min(cumdist2) max(cumdist2) min(maxp) max(maxp)];
     blk=[[ncasts+3;cumdist2(1:ncasts)' ;cumdist2(ncasts);cumdist2(1);cumdist2(1)],...
    -1*[-1;maxp(1:ncasts)';a(4);a(4);maxp(1)]];
     save blk.dat blk -ascii    

  % write a station number label file
     stalab=[cumdist' zeros(ncasts,1) snum];
     eval(['save stalb',pf,'.dat stalab -ascii']);

  % write surface and bottom data to files
     idg=find(isnan(sst)~=1);
     if idg~=[];
       sstf=[q(idg,1) -1*q(idg,2) sst(idg)' btt(idg)' sss(idg)' ...
       bts(idg)' snum(idg) posn(idg,9)];
       save surfbot.dat sstf -ascii
     end;

  % execute a surfer script file
     !c:\winsurf\gsmac.exe /x versec.bas

