function wcoupledat(block0,block1,block2,block3,block4,block5,block6)

% Writes COUPLE dat (waveguide) files.
%
% SYNTAX: wcoupledat(block0,block1,block2,block3,block4,block5,block6)

%*******************************************************************************
% Gambelas, Tue Oct 30 13:36:39 WET 2012
% Written by Tordar 
%*******************************************************************************

%*******************************************************************************
%Block0:
thetitle = block0.thetitle;

%*******************************************************************************
%Block1:
hb   = block1.hb  ; % Total thickness of water and sediment layer
freq = block1.freq; % Frequency (Hz)
zs   = block1.zs  ; % Source depth (m)

%*******************************************************************************
%Block2:
m     = block2.m    ; % Number of contributing modes
igeom = block2.igeom; % Geometry (0 => cylindrical, point source, 1 => plane, line source)
irefl = block2.irefl; % Source conditions at r = 0(0 => perfectly cylindrical symmetry, 1 => absorver at negative ranges)

%*******************************************************************************
%Block3:
ndep  = block3.ndep ; % number of receiver depths
zmin  = block3.zmin ; % minimum receiver depth
zinc  = block3.zinc ; % increment between receiver depths

%*******************************************************************************
%Block4:
rmin  = block4.rmin ; % minimum range
rmax  = block4.rmax ; % maximum range
rinc  = block4.rinc ; % increment between ranges

%*******************************************************************************
%Block5:
n    = block5.n    ; % Number of environmental regions
iprt = block5.iprt ; % print flag (see manual)
iout = block5.iout ; % flag for matching conditions (-1 => single scatter, 0 => fully elliptic 2way, 1 => 1way, 2 => adiabatic)

%*******************************************************************************
%Block6:
rang  = block6.rang ; % range at start of each region
irlin = block6.irlin; % number of additional regions
dpth  = block6.dpth ; % water depth at start of each region
npw   = block6.npw  ; % number of points in water
npb   = block6.npb  ; % number of points in bottom

nrang = length( rang );

%*******************************************************************************
% Write input data file: 
 
fid = fopen('couple.dat','w');
fprintf(fid,'%s\n',thetitle);             % Block0
fprintf(fid,'%f %f %f\n',hb,freq,zs);     % Block1
fprintf(fid,'%d %d %d\n',m,igeom,irefl);  % Block2
fprintf(fid,'%d %f %f\n',ndep,zmin,zinc); % Block3
fprintf(fid,'%f %f %f\n',rmin,rmax,rinc); % Block4
fprintf(fid,'%d %d %d\n',n,iprt,iout);    % Block5
for j = 1:nrang                           % Block6
    fprintf(fid,'%f %d %f %d %d\n',rang(j),irlin(j),dpth(j),npw(j),npb(j));
    if npw(j) > 1 
       auxstring = [ 'data = block6.datanpw' num2str(j) ';' ];
       eval( auxstring )
       depw   = data(:,1);
       svpw   = data(:,2);
       dbpwlw = data(:,3);
       rhow   = data(:,4); clear data
       l = length( depw );
       for l = 1:npw(j)
       fprintf(fid,'%f %f %f %f\n',depw(l),svpw(l),dbpwlw(l),rhow(l));
       end
    end
    if npb(j) > 1
       auxstring = [ 'data = block6.datanpb' num2str(j) ';' ];
       eval( auxstring )
       depb   = data(:,1);
       svpb   = data(:,2);
       dbpwlb = data(:,3);
       rhob   = data(:,4); clear data
       l = length( depw );
       for l = 1:npb(j)
       fprintf(fid,'%f %f %f %f\n',depb(l),svpb(l),dbpwlb(l),rhob(l));
       end
    end
end
fclose( fid );

%*******************************************************************************
