clc;clear all;close all

% file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_Wind_202407.nc'
% file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_Wind_20240713.nc'
% file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_Wind_20240731.nc'
% file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_Wind_202408.nc'
% file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_Wind_20240819.nc'
% file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_Wind_20240904.nc'
file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_Wind_20240906.nc'
Uwind=ncread(file, 'Uwind');
Vwind=ncread(file, 'Vwind');
time=ncread(file,'wind_time');

number=3; %smooth hours

% Initialize
n = round(size(Uwind,3) / number);    % 每組3筆資料，結果有 n 組
Uwind_avg = zeros(200,260, n);
Vwind_avg = zeros(200,260, n);
Uwind_new = zeros(200,260,length(time));
Vwind_new = zeros(200,260,length(time));

for i = 1:n
    start_idx = (i-1)*number + 1; % start
    end_idx = i*number;           % End
    Uwind_avg(:,:,i) = nanmean(Uwind(:,:,start_idx:end_idx),3);
    Vwind_avg(:,:,i) = nanmean(Vwind(:,:,start_idx:end_idx),3); 
%-----------%    
Uwind_new(:, :, start_idx:end_idx) = repmat(Uwind_avg(:,:,i), [1, 1, number]);
Vwind_new(:, :, start_idx:end_idx) = repmat(Vwind_avg(:,:,i), [1, 1, number]);
end


% file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/3hrs/Bitou_Wind_202407_3hrs.nc'
% file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/6hrs/Bitou_Wind_202407_6hrs.nc'
% file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/3hrs/Bitou_Wind_20240713_3hrs.nc'
% file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/3hrs/Bitou_Wind_20240731_3hrs.nc'
% file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/3hrs/Bitou_Wind_202408_3hrs.nc'
% file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/3hrs/Bitou_Wind_20240819_3hrs.nc'
% file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/3hrs/Bitou_Wind_20240904_3hrs.nc'
file='/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/3hrs/Bitou_Wind_20240906_3hrs.nc'
ncwrite(file, 'Uwind', Uwind_new);
ncwrite(file, 'Vwind', Vwind_new);










