function [B_date_time, E_date_time] = tsBE(sel_filename,station_id)

if ~exist('sel_filename') || isempty(sel_filename)
    error('No filenames specified.');
end
if ~exist('station_id') || isempty(station_id)
    error('No station code specified.');
end

% initialize variables:
B_date_time = [];
E_date_time = [];

% loop through files containing data for the selected ASOS station:
for i=1:length(sel_filename)

    full_filename = sel_filename{i}; % current filename

    fid = fopen( full_filename );
    if fid==-1
        err = errordlg({'The following file could not be opened for reading:',full_filename},'Error reading file');
        uiwait(err); return;
    end
    h = waitbar(0,['File name: ' strrep(strrep(full_filename,'\','\\'),'_','\_')],...
        'Name','Extracting thunderstorm beginning and end times...', 'WindowStyle','modal');
    drawnow;
    file_contents = textscan( fid, '%s', 'whitespace', '', 'delimiter', '\n');
    file_contents = file_contents{1}; 

    tstmbegtimes = zeros(length(file_contents),1);
    tstmendtimes = zeros(length(file_contents),1);

    tsB_codes = {'TB','TB ','TSB','TSB ','T B','T B '}; % codes to indicate thunderstorm beginning
    tsE_codes = {'TE','TE ','TSE','TSE ','T E','T E '}; % codes to indicate thunderstorm end

    countB=0;
    countE=0;
    tswindow = (1/12);% time interval (days) used in testing for repeated reports

    % initialize values for checking for repeated reports:
    last_beg_str = 'CD';
    last_end_str = 'CD';
    last_tstmbegtimes = 0;
    last_tstmendtimes = 0;
    last_tstmbegminstr = 'zero';
    last_tstmendminstr = 'zero';

    for i=1:length(file_contents) % loop through lines of file
        textline = file_contents{i}; % current line
        if length(textline)<22
            continue
        end
        ac = str2num(textline(5:10)); % ASOS station number
        if ac ~= station_id
            continue
        end
        datetimestr=textline(11:22);
        yr=str2num(datetimestr(1:4));
        mo=str2num(datetimestr(5:6));
        dy=str2num(datetimestr(7:8));
        hr=str2num(datetimestr(9:10));
        mn=str2num(datetimestr(11:12));
        linetime=datenum(yr,mo,dy,hr,mn,0);

        for j=1:length(tsB_codes) % loop through codes that indicate thunderstorm beginnings
            tsB_code = tsB_codes{j}; % current code
            k = strfind(textline,tsB_code); % indices of current code on current line
            for ii=1:length(k)
                % check whether the first character following the code is numeric
                if length(textline)<k(ii)+length(tsB_code) || ~ismember(textline(k(ii)+length(tsB_code)),'0123456789')
                    continue; % non-numeric character following code --> skip to next code on line
                end
                tstm_ex = textline(k(ii)+length(tsB_code):length(textline)); % remaining portion of line following tsB_code
                y = min(find(~ismember(tstm_ex,'0123456789'))); % index of first non-numeric character after tsB_code
                if isempty(y)
                    tstmbegminstr=tstm_ex; % numeric characters following tsB_code (rest of line)
                else
                    tstmbegminstr=tstm_ex(1:(y-1)); % numeric characters following tsB_code
                end
                if ismember(length(tstmbegminstr),[2 4])
                    % Check if thunderstorm beginning report is repeated; if not, count it and store in list:
                    if ~((linetime - last_tstmbegtimes < tswindow) & (strcmp(tstmbegminstr,last_tstmbegminstr) | ~ismember(length(tstmbegminstr),[2 4])))
                        countB=countB+1;
                        tstmbegtimes(countB) = case_time(tstmbegminstr, datetimestr);
                        if ishandle(h), waitbar(i/length(file_contents),h); drawnow; end
                        % store temporary values to check for repeated reports:
                        last_tstmbegminstr = tstmbegminstr;
                        last_tstmbegtimes = linetime;
                    elseif strcmp(tstmbegminstr,last_tstmbegminstr)
                        % if thunderstorm beginning report is repeated within 'tswindow', update time of last repeat:
                        last_tstmbegtimes = linetime;
                    end
                end
                % Check if thunderstorm beginning report is followed by an 'E'
                % and a numeric character (a subsequent end report):
                if ~isempty(y) && tstm_ex(y)=='E' && length(tstm_ex)>=y+1 && ismember(tstm_ex(y+1),'0123456789')
                   tstmendstr=tstm_ex(y+1:length(tstm_ex));
                   yy = min(find(~ismember(tstmendstr,'0123456789')));
                   if isempty(yy)
                       tstmendminstr=tstmendstr;
                   else
                       tstmendminstr=tstmendstr(1:(yy-1));                   
                   end
                   if ismember(length(tstmendminstr),[2 4])
                       % Check if thunderstorm end report is repeated; if not, count it and store in list:
                       if ~((linetime - last_tstmendtimes < tswindow) & (strcmp(tstmendminstr,last_tstmendminstr) | ~ismember(length(tstmendminstr),[2 4])))
                           countE=countE+1;
                           tstmendtimes(countE) = case_time(tstmendminstr, datetimestr); 
                           % store temporary values to check for repeated reports:
                           last_tstmendminstr = tstmendminstr;
                           last_tstmendtimes = linetime;
                       elseif strcmp(tstmendminstr,last_tstmendminstr)
                           % if thunderstorm end report is repeated within 'tswindow', update time of last repeat:
                           last_tstmendtimes = linetime;
                       end
                   end
                   % Check if thunderstorm end report is followed by a 'B'
                   % and a numeric character (a subsequent beginning report):
                   if ~isempty(yy) && tstmendstr(yy)=='B' && length(tstmendstr)>=yy+1 && ismember(tstmendstr(yy+1),'0123456789')
                       tstmbegstr = tstmendstr(yy+1:length(tstmendstr));
                       yyy = min(find(~ismember(tstmbegstr,'0123456789')));
                       if isempty(yyy)
                           tstmbegminstr = tstmbegstr;
                       else
                           tstmbegminstr = tstmbegstr(1:(yyy-1));
                       end
                       if ismember(length(tstmbegminstr),[2 4])
                           % Check if thunderstorm beginning report is repeated; if not, count it and store in list:
                           if ~((linetime - last_tstmbegtimes < tswindow) & (strcmp(tstmbegminstr,last_tstmbegminstr) | ~ismember(length(tstmbegminstr),[2 4])))
                               countB=countB+1;
                               tstmbegtimes(countB) = case_time(tstmbegminstr, datetimestr);
                               % store temporary values to check for repeated reports:
                               last_tstmbegminstr = tstmbegminstr;
                               last_tstmbegtimes = linetime;
                           elseif strcmp(tstmbegminstr,last_tstmbegminstr)
                               % if thunderstorm beginning report is repeated within 'tswindow', update time of last repeat:
                               last_tstmbegtimes = linetime;
                           end 
                       end %if ismember(length...
                   end %if ~isempty(yy) ...
                end % if ~isempty(y) ...
            end % for ii=1:length(k)
        end % for j=1:length(tsB_codes)

        for j=1:length(tsE_codes) % loop through codes that indicate thunderstorm ends
            tsE_code = tsE_codes{j}; % current code
            k = strfind(textline,tsE_code); % indices of current code on current line
            for ii=1:length(k)
                % check whether the first character following the code is numeric
                if length(textline)<k(ii)+length(tsE_code) || ~ismember(textline(k(ii)+length(tsE_code)),'0123456789')
                    continue; % non-numeric character following code --> skip to next code on line
                end
                tstm_ex = textline(k(ii)+length(tsE_code):length(textline)); % remaining portion of line following tsE_code
                y = min(find(~ismember(tstm_ex,'0123456789'))); % index of first non-numeric character after tsE_code
                if isempty(y)
                    tstmendminstr=tstm_ex; % numeric characters following tsE_code (rest of line)
                else
                    tstmendminstr=tstm_ex(1:(y-1)); % numeric characters following tsE_code
                end
               
                if ismember(length(tstmendminstr),[2 4])
                    % Check if thunderstorm end report is repeated; if not, count it and store in list:
                    if ~((linetime - last_tstmendtimes < tswindow) & (strcmp(tstmendminstr,last_tstmendminstr) | ~ismember(length(tstmendminstr),[2 4])))
                        countE=countE+1;
                        tstmendtimes(countE) = case_time(tstmendminstr, datetimestr);
                        if ishandle(h), waitbar(i/length(file_contents),h); drawnow; end
                        % store temporary values to check for repeated reports:
                        last_tstmendminstr = tstmendminstr;
                        last_tstmendtimes = linetime;
                    elseif strcmp(tstmendminstr,last_tstmendminstr)
                        % if thunderstorm end report is repeated within 'tswindow', update time of last repeat:
                        last_tstmendtimes = linetime;
                    end
                end
                % Check if thunderstorm end report is followed by a 'B'
                % and a numeric character (a subsequent beginning report):
                if ~isempty(y) && tstm_ex(y)=='B' && length(tstm_ex)>=y+1 && ismember(tstm_ex(y+1),'0123456789')
                   tstmbegstr=tstm_ex(y+1:length(tstm_ex));
                   yy = min(find(~ismember(tstmbegstr,'0123456789')));
                   if isempty(yy)
                       tstmbegminstr=tstmbegstr;
                   else
                       tstmbegminstr=tstmbegstr(1:(yy-1));                   
                   end
                   if ismember(length(tstmbegminstr),[2 4])
                       % Check if thunderstorm beginning report is repeated; if not, count it and store in list:
                       if ~((linetime - last_tstmbegtimes < tswindow) & (strcmp(tstmbegminstr,last_tstmbegminstr) | ~ismember(length(tstmbegminstr),[2 4])))
                           countB=countB+1;
                           tstmbegtimes(countB) = case_time(tstmbegminstr, datetimestr); 
                           % store temporary values to check for repeated reports:
                           last_tstmbegminstr = tstmbegminstr;
                           last_tstmbegtimes = linetime;
                       elseif strcmp(tstmbegminstr,last_tstmbegminstr)
                           % if thunderstorm beginning report is repeated within 'tswindow', update time of last repeat:
                           last_tstmbegtimes = linetime;
                       end 
                   end
                   % Check if thunderstorm beginning report is followed by an 'E'
                   % and a numeric character (a subsequent end report):
                   if ~isempty(yy) && tstmbegstr(yy)=='E' && length(tstmbegstr)>=yy+1 && ismember(tstmbegstr(yy+1),'0123456789')
                       tstmendstr = tstmbegstr(yy+1:length(tstmbegstr));
                       yyy = min(find(~ismember(tstmendstr,'0123456789')));
                       if isempty(yyy)
                           tstmendminstr = tstmendstr;
                       else
                           tstmendminstr = tstmendstr(1:(yyy-1));
                       end
                       if ismember(length(tstmendminstr),[2 4])
                           % Check if thunderstorm end report is repeated; if not, count it and store in list:
                           if ~((linetime - last_tstmendtimes < tswindow) & (strcmp(tstmendminstr,last_tstmendminstr) | ~ismember(length(tstmendminstr),[2 4])))
                               countE=countE+1;
                               tstmendtimes(countE) = case_time(tstmendminstr, datetimestr); 
                               % store temporary values to check for repeated reports:
                               last_tstmendminstr = tstmendminstr;
                               last_tstmendtimes = linetime;
                           elseif strcmp(tstmendminstr,last_tstmendminstr)
                               % if thunderstorm end report is repeated within 'tswindow', update time of last repeat:
                               last_tstmendtimes = linetime;
                           end
                       end %if ismember (length...
                   end %if ~isempty(yy) ...
                end % if ~isempty(y) ...
            end % for ii=1:length(k)
        end % for j=1:length(tsE_codes)
    end

    if ishandle(h), waitbar(1,h); drawnow; close(h); end

    % eliminate trailing values and repeated values (and sort):
    if countB>0
        B_date_time_i = unique(tstmbegtimes(1:countB)); 
    else
        B_date_time_i = [];
    end
    if countE>0
        E_date_time_i = unique(tstmendtimes(1:countE));
    else
        E_date_time_i = [];
    end

    % concatenate results from current file into larger lists:
    B_date_time = [B_date_time; B_date_time_i];
    E_date_time = [E_date_time; E_date_time_i];
end

B_date_time = unique(B_date_time);
E_date_time = unique(E_date_time);


if length(B_date_time)==0 & length(E_date_time)==0 
    wrn = warndlg({'No valid thunderstorm beginning or end reports found in the specified file:',...
        full_filename},'Warning: no thunderstorm beginning or end reports found');
    uiwait(wrn);
end