function writeBAS6(basename,bas)
% writeBAS6(basename,bas)
% TO 070630


% Copyright 2009 Theo Olsthoorn, TU-Delft and Waternet, without any warranty
% under free software foundation GNU license version 3 or later

fid=fopen([basename,'.',bas.ext],'wt');

%0.
fprintf(fid,'# MATLAB  writeBAS6 %s\n',datestr(now));
fprintf(    '# MODFLOW writeBAS6 %s\n',datestr(now));

%1.  optional words XSECTION CHTOCH FREE PRINTTIME SHOWPROGRESS
OPTIONS='';  % OPTIONS='FREE';0)

fprintf(fid,'%s\n',OPTIONS);

%2.  if <0 constant head if > 0 compute head if 0 inactive (or lake in lake package)
for ilay=1:bas.NLAY
    warray(fid,bas.IBOUND(:,:,ilay),bas.unit,'(25I3)',sprintf('IBOUND{%d}',ilay));
end

%3. value used for inactive cells
fprintf(fid,'%10g     HNOFLO\n',bas.HNOFLO);

%4. initial heads
for ilay=1:bas.NLAY
    warray(fid,bas.STRTHD(:,:,ilay),bas.unit,'(10E12.3)',sprintf('STRTHD{%d}',ilay));
end

fclose(fid);
