video_filename = 'ROTI_S4_Map_Visualization_MovingCircles_4.avi';
video_writer = VideoWriter(video_filename, 'Motion JPEG AVI');
video_writer.FrameRate = 30;
start_date = datetime(2023, 4, 23, 0, 0, 0);
end_date = datetime(2023, 4, 23, 23, 59, 59);
figure('Position', [100, 100, 1200, 800]);
    filename_s4 = sprintf('Station%d.xlsx', station_num);
        data_s4 = readmatrix(filename_s4, 'OutputType', 'string');
        station_data(station_num).data = data_s4;
        disp(['File ', filename_s4, ' not found. Skipping.']);
fileDates = {'20230423'};
for d = 1:length(fileDates)
    time_bins = start_date:minutes(5):end_date;
    for t = 1:length(time_bins)-1
        ax = worldmap(latlim, lonlim);
        setm(ax, 'FFaceColor', [0.5 0.7 0.9]);
        geoshow(coastlat, coastlon, 'DisplayType', 'polygon', 'FaceColor', [0.6 0.8 0.6]);
        geoshow('landareas.shp', 'FaceColor', [0.8 0.8 0.8]);
        current_time = time_bins(t);
        hour_str = datestr(current_time, 'HH');
        minute_str = datestr(current_time, 'MM');
        filename_vtec = sprintf('C:\\Users\\I7-2600K\\Desktop\\data\\%s_%s%s00_roti.dat', ...
                                 date_str, hour_str, minute_str);
            data_ROTI = readmatrix(filename_vtec);
            if size(data_ROTI, 2) >= 3
                lon_ROTI = data_ROTI(:, 1);
                lat_ROTI = data_ROTI(:, 2);
                scatterm(lat_ROTI, lon_ROTI, 30, ROTI, 'filled');
                cb_roti = colorbar('southoutside');
                cb_roti.Label.String = 'ROTI';
                title(['ROTI Map - ', datestr(current_time)]);
            disp(['ROTI file not found: ', filename_vtec]);
            if isfield(station_data(station_num), 'data')
                data_s4 = station_data(station_num).data;
                time_str_s4 = data_s4(:, 1);
                S4 = str2double(data_s4(:, 3));
                Lon_IPP = str2double(data_s4(:, 15));
                Lat_IPP = str2double(data_s4(:, 16));
                elevation = str2double(data_s4(:, 5));
                time_dt_s4 = datetime(time_str_s4, 'InputFormat', 'yyyy-MM-dd''T''HH:mm:ss.SSS');
                valid_idx = (time_dt_s4 >= time_bins(t)) & ...
                            (time_dt_s4 < time_bins(t+1)) & ...
                    S4_filtered = S4(valid_idx);
                    Lon_IPP_filtered = Lon_IPP(valid_idx);
                    Lat_IPP_filtered = Lat_IPP(valid_idx);
                    for i = 1:length(S4_filtered)
                        scatterm(Lat_IPP_filtered(i), Lon_IPP_filtered(i), ...
                                 100, color, 'o', 'filled', ...
                                 'MarkerEdgeColor', 'k', 'LineWidth', 0.5);
        cb_s4 = colorbar('eastoutside');
        cb_s4.Label.String = 'S4';
        writeVideo(video_writer, frame);
disp(['Video saved to ', video_filename]);