function[] = CMS_WIS_to_txt_for_spectragen_v2()
%This function creates the .txt file used in the spectra generator of 
% CMS Wave.  It prompts the user to select all the desired WIS output files,
% specify the depth of the station, and whether or not to average the data
% series.  The data and user inputs are manipulated into the format used by
% CMS Wave file import for spectra generation.  Currently the output file
% is saved in the active directory of matlab.  This can be changed by
% adding in desired destination in the definition of variable 'outfile' on
% line 58.
% 
% 
% Created: 26 JAN 2012
% Author: Kevin Hodgens, USACE, SAJ, kevin.c.hodgens@usace.army.mil
% Modified: 02 FEB 2012 by Kevin Hodgens
% Notes: Lines 26 and 27 were added to ensure that the time series is in
% order when multiple files are selected for the dataset.

[filename,path,filter] = uigetfile('C:\*.onlns','Select the WIS files to include','multiselect','on');

Data = [];
for i = 1:length(filename(1,:))
    [time,sta,Lat,Long,ws,wdir,non1,non2,non3,Hs,Tp,foo2,foo3,non4,Dp,dir2] = textread([path filename{i}],'%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n','delimiter',' ');
    Data = [Data;[time,Dp,Hs,Tp]];
end
depth = input('What depth is the WIS station? (Remember CMS uses positive values for depth)');
[s,si] = sort(Data(:,1));
Data = Data(si,:);
% save('WIS_63422_1980to1999.mat','Data','depth') Turn this on if you wish
% to archive the data in a matlab data file

avg_on = input('Would you like to average the time series? 1 = Yes');
if avg_on
    int = input('How many hours in the interval? ex. "6"');
    count = 0;
    for i = 1:int:length(Data(:,1))
        count = count +1;
        mD(count,1) = Data(i+5,1);
        mD(count,2:4) = mean(Data(i:i+5,2:4));
    end
    mind = [1:length(mD)]';
else
    mD = Data;
    mind = 1:length(Data(:,1));
end

Gamma = 3.3*ones(mind(end),1);%Set Gamma to 3.3 as advised by Lihwa Lin
nn = 50*ones(mind(end),1);%Set nn to 50 as advised by Lihwa Lin

%Set up output matrix
matrix(1:mind(end),[1,16]) = zeros(mind(end),2);
matrix(1:mind(end),2) = -1*ones(mind(end),1);
matrix(1:mind(end),[3,9:14,17:18]) = 999*ones(mind(end),9);
matrix(1:mind(end),4) = mind;
matrix(1:length(mD(:,1)),[5:8,15]) = [mD(:,2:4),Gamma,nn];
matrix(1:mind(end),19) = depth*ones(mind(end),1);
text(1,1:20) = {' ',     'METHOD', 'OPTION',        'TIME',       'INDEX',    'ANGLE(1)',       'HS(1)',       'TP(1)',    'GAMMA(1)',    'ANGLE(2)',       'HS(2)',       'TP(2)',    'GAMMA(2)',        'WIND',       'FETCH',       'NN(1)',       'NN(2)',   'STDDEV(1)',   'STDDEV(2)',       'DEPTH'};

outfile = 'CMS_spectra_import_file.txt';%Add in specific directory if you please...
fid = fopen(outfile,'w');

fprintf(fid,'%s\r\n','SPECTRAL TABLE');
fclose('all');
fid = fopen(outfile,'a');
for j = 1:20
    if j == 20
        fprintf(fid,'%s\r\n',text{1,j});
    else
        fprintf(fid,'%s\t',text{1,j});
    end
end

for i = 1:mind(end)
    for j = 1:20
        if j == 1
            fprintf(fid,'%s\t','');
        elseif j == 20
            fprintf(fid,'%f\r\n',matrix(i,j-1));
        else
            fprintf(fid,'%f\t',matrix(i,j-1));
        end
    end
end
fclose('all');

%For Flagler CMS work, period one is 8/1/84 to 1/15/87; period 2 is 1/15/87
%to 7/31/93.  Data is reduced before line 30 with a break point.
% % f = find(Data(:,1)>19840731235900);
% % f1 = find(Data(:,1)<19870115000000);
% % f = find(Data(:,1)>=19870115000000);
% % f1 = find(Data(:,1)<19930731235900);