%%%%%%%%计算1991—2020年 南北纬30度的20°气候态等温线深度
clear
clc

depth_std=ncread('../../CAS_v1_temp_monthly_netCDF_GTSPP/CAS_v1_2000m_year_1955_month_01.nc','depth_std');
lat=ncread('../../CAS_v1_temp_monthly_netCDF_GTSPP/CAS_v1_2000m_year_1955_month_01.nc','lat');
lon=ncread('../../CAS_v1_temp_monthly_netCDF_GTSPP/CAS_v1_2000m_year_1955_month_01.nc','lon');

for month=1:12
    month
    m=1;
    temp_all=NaN(30,41,360,180);
    for year=1991:2020
        year
        filename=['../../CAS_v1_temp_monthly_netCDF_GTSPP/CAS_v1_2000m_year_',num2str(year),'_month_',num2str(month,'%02d'),'.nc'];
        temp=ncread(filename,'temp');
        temp_all(m,:,:,:)=temp;
        m=m+1;
    end
    COMS_temp_clim_monthly=squeeze(nanmean(temp_all,1));
    
    %%%%%%%%%计算20度等温线
    depth_20degree_clim=NaN(360,180);
    for i=1:360
        i
        for j=1:180
            data=squeeze(COMS_temp_clim_monthly(:,i,j));
            if(all(isnan(data)))
                continue
            end
            %%%%%找到大于20度和小于20度的深度和温度值
            index1=find(data>20);  %如果为空，证明没有20度等温线
            index2=find(data<20);
            if(isempty(index2) || isempty(index1))
                continue
            end
            
            temp_small=[data(index1(end)),data(index2(1))];
            depth_small=[depth_std(index1(end)),depth_std(index2(1))];
            
            %做线性插值
            depth_20degree_clim(i,j)=interp1(temp_small,depth_small,[20]);
        end
    end
    
    eval(['save ./CAS_1991_2020_20degree_depth_clim_month',num2str(month),'.mat depth_20degree_clim lat lon'])
    clear depth_20degree
end