function dmc_regen(bin_spec)

% dmc_regen.m
% 
% SYNTAX:       dmc_regen(bin_spec)
%               dmc_regen(1)
%
% INPUTS:       bin_spec                -   eg. 1 (minutes per bin for summary stats)
%
% Regenerate all files from a single RAW.txt file.
%
% Date Created:  June 11, 2004
% Date Modified: June 22, 2004
%
% Brandon Sackmann
% University of Maine
% School of Marine Sciences & 
% Ira C. Darling Marine Center
% 193 Clark's Cove Road
% Walpole, Maine 04573
%
% Tel: (207) 563-3146 x343
% Fax: (207) 563-3119
% Email: brandon.sackmann@maine.edu

[filename, DIRECTORY_NAME_LOCAL, filterindex] = uigetfile('*.txt', 'Where have the data been saved...');
cd(DIRECTORY_NAME_LOCAL)

ARCHIVE_PREFIX = filename(1:end-22);
ARCHIVE_NUM    = filename(end-20:end-8);
BIN_SPEC  = bin_spec;
ASCII_FID = fopen([ARCHIVE_PREFIX '_' ARCHIVE_NUM '_RAW.txt'],'r');

count = 1;
while 1
    if mod(count,100) == 0
        disp(count)
    end
    data = fgetl(ASCII_FID);
    if ~ischar(data), break, end
    
    if length(data)>0
        ARCHIVE_TIME(count,1)   = datenum(data(1:20));
        
        [signal(1),signal(2),signal(3),signal(4),signal(5),signal(6)] = strread(data(43:end),'%f%f%f%f%f%f','delimiter',' ');  % eco-bb2f
        %[signal(1),signal(2),signal(3),signal(4),signal(5),signal(6)] = strread(data(21:end),'%f%f%f%f%f%f','delimiter',' ');  % hockey-puck
        ARCHIVE_SIGNAL(count,:) = signal;
    
        count = count+1;
    end
end
eval(['save ' ARCHIVE_PREFIX '_' ARCHIVE_NUM '_RAW.mat ARCHIVE_TIME ARCHIVE_SIGNAL -V6']);
fclose(ASCII_FID);

% *************************************************************
% GENEATE STATISTICS FOR THE DATA. MAKE SURE TO USE THE CODE AS  
% CURRENTLY IMPLEMENTED IN 'SERIALTRACE_vX_X.m'
% *************************************************************

time_beg = ARCHIVE_TIME(1);
time_end = ARCHIVE_TIME(end);    
time_bin = time_beg:(BIN_SPEC/60/24):time_end;
start_pos = 0;
       
% CALCULATE STATISTICS - FOR EACH ARCHIVE
for i = 1:length(time_bin)
    if i<length(time_bin)
        dat_indices = find(ARCHIVE_TIME>=time_bin(i) & ARCHIVE_TIME<(time_bin(i+1)));
    else
        dat_indices = find(ARCHIVE_TIME>=time_bin(i) & ARCHIVE_TIME<(time_bin(i)+(BIN_SPEC/60/24)));
    end
    if dat_indices
        ARCHIVE_TIME_BIN(start_pos+i,1)     = time_bin(i);
        ARCHIVE_SIGNAL_MEAN(start_pos+i,:)  = mean(ARCHIVE_SIGNAL(dat_indices,:),1);
        ARCHIVE_SIGNAL_STD(start_pos+i,:)   = std(ARCHIVE_SIGNAL(dat_indices,:),0,1);
        ARCHIVE_SIGNAL_MED(start_pos+i,:)   = median(ARCHIVE_SIGNAL(dat_indices,:),1);
        if length(dat_indices)==1
            ARCHIVE_SIGNAL_16PER(start_pos+i,:) = median(ARCHIVE_SIGNAL(dat_indices,:),1);
            ARCHIVE_SIGNAL_84PER(start_pos+i,:) = median(ARCHIVE_SIGNAL(dat_indices,:),1);
        else
            ARCHIVE_SIGNAL_16PER(start_pos+i,:) = prctile(ARCHIVE_SIGNAL(dat_indices,:),16);
            ARCHIVE_SIGNAL_84PER(start_pos+i,:) = prctile(ARCHIVE_SIGNAL(dat_indices,:),84);
        end
        ARCHIVE_SIGNAL_MIN(start_pos+i,:)   = min(ARCHIVE_SIGNAL(dat_indices,:),[],1);
        ARCHIVE_SIGNAL_MAX(start_pos+i,:)   = max(ARCHIVE_SIGNAL(dat_indices,:),[],1);
        ARCHIVE_SIGNAL_N(start_pos+i,1)     = length(dat_indices);
    end
end
           
eval(['save ' ARCHIVE_PREFIX '_' ARCHIVE_NUM '_STATS.mat ARCHIVE_TIME_* ARCHIVE_SIGNAL_* -V6']);