clc;clear all;close all
% inputFile = '/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_Wind_202407.nc';
% inputFile = '/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_Wind_20240731.nc'
% inputFile = '/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_Wind_202408.nc';
% inputFile = '/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_Wind_20240819.nc';
% inputFile = '/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_Wind_20240904.nc';
inputFile = '/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_Wind_20240906.nc';

ncid_in = netcdf.open(inputFile, 'NC_NOWRITE');
varid_Uwind = netcdf.inqVarID(ncid_in, 'Uwind');
varid_Vwind = netcdf.inqVarID(ncid_in, 'Vwind');
varid_time = netcdf.inqVarID(ncid_in, 'wind_time');

Uwind_data = netcdf.getVar(ncid_in, varid_Uwind);
Vwind_data = netcdf.getVar(ncid_in, varid_Vwind);
Time_data = netcdf.getVar(ncid_in, varid_time);
netcdf.close(ncid_in);

%--------------%
% original_size = size(Uwind_data); % [200, 260, 37]
% new_size = [200, 260, 457]; % Dim : newly
% Uwind_new = zeros(new_size, 'like', Uwind_data);  % value=0
% Vwind_new = zeros(new_size, 'like', Vwind_data);  
% 
% Uwind_new(:, :, 1:original_size(3)) = Uwind_data; % 保留原始數據
% Vwind_new(:, :, 1:original_size(3)) = Vwind_data; 

%-------- read the bitou wind ---------%
% file = '/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_202407.xlsx'
file = '/data3/yiwen/matlab_coawst/Pre-processing/Bitou_wind/Bitou_2024089.xlsx'
[number, text, rawData] = xlsread(file)
%===== need to change ======%
windSpeed = number(866-2:1034-2,2);     % unit (m/s)
windDirection = number(866-2:1034-2,3); % 風向角度 (度, 北=360/0, 東=90)

% 轉換角度到弧度 (因 MATLAB 的 cos 和 sin 接受弧度)
windDirection_rad = deg2rad(90 - windDirection); 

%======== u=V*sind(alpha) / v=V*cosd(alpha) =========%
u = windSpeed .* cos(windDirection_rad); % u  (正東為正)
v = windSpeed .* sin(windDirection_rad); % v  (正北為正)

u(isnan(u))=0;
v(isnan(v))=0;

u_wind=zeros(200,260,length(windSpeed));
v_wind=zeros(200,260,length(windSpeed));

for t=1:size(u_wind,3)
 u_wind(:,:,t)=u(t);   
 v_wind(:,:,t)=v(t);
end

referenceTime = datetime(1858, 11, 17, 0, 0, 0);
data1 = datetime(2024, 9, 06, 0, 0, 0);  
data2 = datetime(2024, 9, 13, 0, 0, 0);   
startTime = days(data2 - data1);

timeInterval = hours(1);
timeSeries = data1:timeInterval:data2;
timeSeries2 = days(timeSeries - referenceTime);

%------ change wind_time Dim ------%
% ---> using NCO first[ncks -O --mk_rec_dmn wind_time input.nc temp.nc]

% 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'
ncwrite(file,'wind_time',timeSeries2)
ncwrite(file, 'Uwind', u_wind);
ncwrite(file, 'Vwind', v_wind);







