function write_hydrolight(varargin)
% write_hydrolight(filename,depth,adata,cdata,wvl,bbdata,bbwvl,chldata)
% write_hydrolight(filename,depth,adata,cdata,wvl,bbdata,bbwvl)
% write_hydrolight(filename,depth,adata,cdata,wvl,chldata)
% write_hydrolight(filename,depth,adata,cdata,wvl)
% write_hydrolight(filename,depth,chldata)

%% Define inputs
if length(varargin)<3
    error('err:wronginputsize','Not enough inputs')
elseif length(varargin)>8
    error('err:wronginputsize','Too many inputs')
elseif length(varargin)==4
    error('err:wronginputsize','Wrong number of inputs')
elseif length(varargin)==3
    chldata=varargin{3};
else
    adata=varargin{3};
    cdata=varargin{4};
    wvl=varargin{5};
    if length(varargin)>6
        bbdata=varargin{6};
        bbwvl=varargin{7};
        if length(varargin)==8
            chldata=varargin{8};
        end
    elseif length(varargin)==6
        chldata=varargin{6};
    end
end
depth=varargin{2};
filename=varargin{1};

%% Double Check Inputs
if ~isvector(depth)
    error('err:baddepth','Depth must be scalar or vector')
elseif ~iscolumn(depth)
    depth=depth';
end
if ~ischar(filename)
    warning('err:badfilename','Filename must be string')
end
if exist('wvl','var')
    if ~isvector(wvl)
        error('err:badwvl','Wavelength must be scalar or vector')
    elseif iscolumn(wvl)
        wvl=wvl';
    end
end
if exist('bbwvl','var')
    if ~isvector(bbwvl)
        error('err:badbbwvl','Wavelength must be scalar or vector')
    elseif iscolumn(bbwvl)
        bbwvl=bbwvl';
    end
end

%% ACS File
if length(varargin)>4
    if exist(['ACS',filename,'.txt'],'file')
        warning('wrn:exists','filename already exists.  overwriting.')
        delete(['ACS',filename,'.txt'])
    end
acsstr={'Example data file of ac-9 data on the Hydrolight "ac-9 standard format."';                                      
'This file contains ac-9 data binned in 25 cm depth bins, starting with ';
'the first measured depth.  The absorption coefficient at 412 nm is ';
'plotted as the left-hand curve of Fig. 1 in the Hydrolight 4.0 Users Guide.';
'This file will be read by user "ab" routine "abac9"';
'Ten ascii header records (for identification of the users data) are expected. ';
'record 11 contains the number of wavelengths and the nominal wavelengths (free format)';
'records 12 and onward contain the (free-format) ac-9 data ordered as:';
'   depth, a(wavelength 1), ..., a(wavelength n), c(wavelength 1), ..., c(wavelength n)';
'Flag the end of file with a negative depth or an end of file.'};

acsfname = ['ACS',filename,'.txt'];
fid3 = fopen(acsfname, 'wt');
wvlline=[length(wvl) wvl];        

for j=1:10
fprintf(fid3, '%s\n', acsstr{j});
end
fclose(fid3);

dlmwrite(acsfname,wvlline,'delimiter',' ','-append');
dlmwrite(acsfname,[depth adata cdata],'delimiter',' ','-append');
dlmwrite(acsfname,-1.*ones(1,length(wvl).*2+1),'delimiter',' ','-append');
end
%% BB File
if length(varargin)>6
    if exist(['BB',filename,'.txt'],'file')
        warning('wrn:exists','filename already exists.  overwriting.')
        delete(['BB',filename,'.txt'])
    end
  bbstr={'EXAMPLE HYDROSCAT-6 DATA showing the Hydrolight Standard Format for HS-6 data.';
'(These data were taken in conjunction with files AC9_total.txt and AC9_filtered.txt.)';
'NOTE: These bb values are the total (water + particle) backscatter coef b_b, ';
'which is customary for Hydroscat data.  Pure sea water bb values will be removed';
'by Hydrolight to get the particle bb. ';
'The original Hydroscat data were binned in 0.5 meter depth bins, starting with the ';
'first recorded depth; the depths shown here are the average depth of the data in each bin.  ';
'Column headers (depth in m and bb in 1/m):';
 'depth      bb442       bb488       bb532       bb590       bb620       bb671';
'Record 11 gives the number of wavelengths and the wavelengths:  '};

bbfname = ['BB',filename,'.txt'];
fid3 = fopen(bbfname, 'wt');
bbwvlline=[length(bbwvl) bbwvl];        

for j=1:10
fprintf(fid3, '%s\n', bbstr{j});
end
fclose(fid3);

dlmwrite(bbfname,bbwvlline,'delimiter',' ','-append');
dlmwrite(bbfname,[depth bbdata],'delimiter',' ','-append');
dlmwrite(bbfname,-1.*ones(1,length(bbwvl).*2+1),'delimiter',' ','-append');
end

if length(varargin)==3 || length(varargin)==6 || length(varargin)==8
    if exist(['CHL',filename,'.txt'],'file')
        warning('wrn:exists','filename already exists.  overwriting.')
        delete(['CHL',filename,'.txt'])
    end
    chlstr={'Example "chlzdata" file showing the Hydrolight "Chlorophyll-data standard format"';
'These files are read by Hydrolight subroutine "chlzdata" on file ..\maincode\chlzdata.f';
'Ten ascii header records (for identification of the users data) are expected.';
'Record 11 begins with (free format) pairs of depth (in m) and Chl (in mg Chl / m^3) values.';
'The last value is flagged by either an end of file or by negative depth and Chl values';
'Record 6:  unused in this example';
'Record 7:   "';
'Record 8:   "';
'Record 9:   "';
'depth (m)   Chl (mg/m^3)'};
chlfname = ['CHL',filename,'.txt'];
fid3 = fopen(chlfname, 'wt');

for j=1:10
fprintf(fid3, '%s\n', chlstr{j});
end
fclose(fid3);

dlmwrite(chlfname,[depth chldata],'delimiter',' ','-append');
dlmwrite(chlfname,-1.*ones(1,2),'delimiter',' ','-append');
end

    
  