clc;clear all;close all
%--- daily data ----%
file='/data3/yiwen/cesm2/JGR_ocean/OBS_array/MOVE/2022/OS_MOVE_20000206-20221014_DPR_VOLUMETRANSPORT.nc'
MOVE=ncread(file,'TRANSPORT_TOTAL');
time=ncread(file,'TIME');

%----- 將天數加到基準日期上 -----%
baseDate = datetime(1950, 1, 1);
finalDate = baseDate + days(time);  
disp(finalDate)

%---- [2022/10/14 data] -----%
time=time(1:end-20)  
MOVE=MOVE(1:end-20)

%----- 2000~2022 yrs -----%
year_start = 2000;
year_end = 2022;

mon_days  = [31,28,31,30,31,30,31,31,30,31,30,31];
mon_days2 = [31,29,31,30,31,30,31,31,30,31,30,31];

years = year_start:year_end;

days_per_year = [];
for year = year_start:year_end
    if mod(year, 4) == 0 && (mod(year, 100) ~= 0 || mod(year, 400) == 0)
        days_per_year = [days_per_year, mon_days2]; % 閏年 2 月 29 天
    else
        days_per_year = [days_per_year, mon_days];  % 平年 2 月 28 天
    end
end

% 生成完整的每日索引，並填入原始資料
baseDate = datetime(2000, 2, 5);
new_time = baseDate + days(1:sum(days_per_year)); 
new_MOVE = NaN(1, length(new_time)); 

idx = time - min(time) + 1;
new_MOVE(idx) = MOVE; 

OBS_MOC_mon = [];
OBS_MOC_year = [];
countm = 0;
countmdays = 0;
countydays = 0;

for iyr = 1:length(years)
    % 判断是否闰年
    if mod(years(iyr), 4) == 0 && (mod(years(iyr), 100) ~= 0 || mod(years(iyr), 400) == 0)
        days = mon_days2;
    else
        days = mon_days;
    end
    
    if years(iyr) == 2022
        ms = 1; me = 10;  % 到2018年06月 / 2022年10月
        totaldays = sum(days(ms:me));
    elseif years(iyr) == 2000
        ms = 2; me = 12;  % 2000年:2~12月
        totaldays = sum(days(ms:me));
    else
        ms = 1; me = 12;
        totaldays = sum(days(ms:me));
    end
    
    %----- monthly data -----%
    for im = ms:me
        countm = countm + 1;
        tempA = new_MOVE(countmdays+1:countmdays+days(im));
        
        MOVE_mon(countm) = mean(tempA(~isnan(tempA)));
        countmdays = countmdays + days(im);
    end
    
    %----- yearly data -----%
    tempA = new_MOVE(countydays+1:countydays+totaldays);
    MOVE_year(iyr) = mean(tempA(~isnan(tempA)));
    countydays = countydays + totaldays;
end


patho='./2022/'

readme = {['OBS AMOC at 16N'];
          ['year 2000-2022']
          ['-----------------------------------------------'];
          ['OBS_MOC_mon: unit:SV'];
          ['OBS_MOC_year: unit:SV'];
          ['-----------------------------------------------'];
          [''];};

fn21 = [patho 'MOVE_2022.mat'];
save(fn21,'MOVE_mon','MOVE_year');



























