clear

clc
clear

%load Global_Topog1_new2017.mat
%Global_Topog1(181,:)=Global_Topog1(180,:);
%Global_Topog1(:,361)=Global_Topog1(:,360);

load Global_Topog1_new2020.mat mask
Global_Topog1=mask';
Global_Topog1(181,:)=Global_Topog1(180,:);
Global_Topog1(:,361)=Global_Topog1(:,360);


d_check=200;

d_check1=d_check;d_check2=d_check;
%d_check1=1;d_check2=350;


%month_check1=1;month_check2=12;

month_check1=12;month_check2=month_check1;
month_check1=12;month_check2=12;

%%%%%%%   Standard vertical levels

Std_depth(1:99)=1:1:99;
Std_depth(100:220)=100:5:700;
Std_depth(221:350)=710:10:2000;
%Std_depth(351:391)=2000:50:4000;
%Std_depth(392:411)=4100:100:6000;

%Std_depth_model=[1,5,10,20,30,40,50,60,70,80,90,100,120,140,160,180,200,250,300,350,400,450,500,550,600,650,700,750,800,850,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,2000];
Std_depth_model=[1,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,110,120,130,140,150,160,170,180,190,200,220,240,260,280,300,320,340,360,380,400,425,450,475,500,525,550,575,600,625,650,675,700,750,800,850,900,950,1000,1050,1100,1150,1200,1250,1300,1350,1400,1450,1500,1550,1600,1650,1700,1750,1800,1850,1900,1950,2000,2100,2200,2300,2400,2500,2600,2700,2800,2900,3000,3100,3200,3300,3400,3500,3600,3700,3800,3900,4000,4100,4200,4300,4400,4500,4600,4700,4800,4900,5000,5100,5200,5300,5400,5500,5600,5700,5800,5900,6000]

mname=cell(7);
mname{1}='ACCESS-CM2';%r1
mname{2}='ACCESS-ESM1-5';%r1
mname{3}='BCC-CSM2-MR';%r1
mname{4}='BCC-ESM1';%r1
mname{5}='CAMS-CSM1-0';%r1

mname{6}='CanESM5'; %r4
mname{7}='CAS-ESM2-0';%r4
mname{8}='CESM2';%r4
mname{9}='CESM2-FV2';%r4
mname{10}='CESM2-WACCM';%r4
mname{11}='CESM2-WACCM-FV2';%r1

mname{12}='CIESM';%r1
mname{13}='CMCC-CM2-SR5';%r1
mname{14}='CMCC-ESM2'; %r4
mname{15}='CNRM-CM6-1'; %r4
mname{16}='CNRM-ESM2-1'; %r4
mname{17}='E3SM-1-0'; %r4
mname{18}='E3SM-1-1'; %r4
mname{19}='E3SM-1-1-ECA'; %r4
mname{20}='EC-Earth3'; %r4

mname{21}='EC-Earth3-CC';%r1
mname{22}='EC-Earth3-Veg';%r1
mname{23}='EC-Earth3-Veg-LR';%r1
mname{24}='FGOALS-f3-L';%r1
mname{25}='FGOALS-g3';%r1
mname{26}='FIO-ESM-2-0';%r1
mname{27}='GFDL-CM4';%r1
mname{28}='GFDL-ESM4';%r1
mname{29}='GISS-E2-1-G';%r1
mname{30}='GISS-E2-1-H';%r1
mname{31}='HadGEM3-GC31-LL';%r1
mname{32}='HadGEM3-GC31-MM';%r1
mname{33}='INM-CM4-8';%r1
mname{34}='INM-CM5-0';%r1
mname{35}='IPSL-CM5A2-INCA';%r1
mname{36}='IPSL-CM6A-LR';%r1
mname{37}='MCM-UA-1-0';%r1
mname{38}='MIROC6';%r1
mname{39}='MPI-ESM-1-2-HAM';%r1
mname{40}='MPI-ESM1-2-HR';%r1
mname{41}='MPI-ESM1-2-LR';%r1
mname{42}='MRI-ESM2-0';%r1
mname{43}='NESM3';%r1
mname{44}='SAM0-UNICON';%r1
mname{45}='TaiESM1';%r1
mname{46}='UKESM1-0-LL';%r1

model_total_n=46

% mean
obs_test_index=1;

% median
obs_test_index=2;

% median, 3-month
%obs_test_index=3;

%model_total_n=5

month_check=10

eval(['load',' ','./Climatology_2000m_20082012Clim_2022new/','Clim_2000m_20052017Clim_newA3_',num2str(month_check),'.mat',' ','Tclim_mean Tclim_median']);
if(obs_test_index==1)
   Tclim_median=Tclim_mean; 
end
%eval(['save',' ','./Climatology_2000m_20082012Clim/','Clim_2000m_20082012Clim2_',num2str(month_detect),'.mat',' ','Tclim*',' ','NUM*']);
% aa(:,:)=Tclim(d_check,:,:);
%figure
%pcolor(aa);hold on 
  
clear Obs_rec_y
depth_num(1:79)=0;
for i=1:1:79
    ks=find(Std_depth_model(i)==Std_depth);
if(length(ks)>0)
   Obs_rec_y(i,:,:)=Tclim_median(ks,1:180,1:360);
else
    i
end
%depth_num(i)=ks;
end

if(obs_test_index==2 && month_check==11)
  Obs_rec_y(find(Std_depth_model==900),127,233)=NaN;
  Obs_rec_y(find(Std_depth_model==900),127,234)=NaN;  
  Obs_rec_y(find(Std_depth_model==900),116,210)=NaN;  
   Obs_rec_y(find(Std_depth_model==900),127:128,204)=NaN;  
  Obs_rec_y(find(Std_depth_model==900),121,227)=NaN;  
  Obs_rec_y(find(Std_depth_model==900),122,231)=NaN;  
  Obs_rec_y(find(Std_depth_model==900),96,199)=NaN;  
  Obs_rec_y(find(Std_depth_model==950),127,233:235)=NaN;  
  Obs_rec_y(find(Std_depth_model==950),122,230:231)=NaN;  
  Obs_rec_y(find(Std_depth_model==950),122,233)=NaN;  
  Obs_rec_y(find(Std_depth_model==950),116,210)=NaN;  
  Obs_rec_y(find(Std_depth_model==950),73,180)=NaN;  
  Obs_rec_y(find(Std_depth_model==950),74,181)=NaN;  
   Obs_rec_y(79,107,313)=NaN;  
   Obs_rec_y(79,119,274)=NaN; 
   Obs_rec_y(79,119,277)=NaN; 
    Obs_rec_y(79,115,278)=NaN; 
     Obs_rec_y(78,115:123,277:282)=NaN;
   Obs_rec_y(78,123,283)=NaN;
     Obs_rec_y(78,110,325)=NaN;
      Obs_rec_y(78,103,342)=NaN;
      Obs_rec_y(78,105,343)=NaN;
       Obs_rec_y(78,122,308)=NaN;
       Obs_rec_y(78,134,355)=NaN;
       Obs_rec_y(78,95,312)=NaN;
end

if(obs_test_index==2 && month_check==1)
   Obs_rec_y(58,83,120:123)=NaN;  
   Obs_rec_y(58,75,109)=NaN;  
   Obs_rec_y(58,61,115)=NaN;  
   Obs_rec_y(57,75,109)=NaN;  
   Obs_rec_y(57,83,125)=NaN;  
   
end
if(obs_test_index==2 && month_check==2)
   Obs_rec_y(58,123,240)=NaN;  
   Obs_rec_y(58,124,241:242)=NaN;  
end
if(obs_test_index==2 && month_check==3)
   Obs_rec_y(1:50,104,240)=NaN;  
end
if(obs_test_index==2 && month_check==4)
   Obs_rec_y(67:79,102,209)=NaN;  
   Obs_rec_y(67:79,115,299)=NaN;  
   Obs_rec_y(1:79,62,176)=NaN;  
   Obs_rec_y(79,132,290)=NaN; 
   Obs_rec_y(79,139,315)=NaN; 
end
if(obs_test_index==2 && month_check==5)
   Obs_rec_y(79,95,206)=NaN;   
end
if(obs_test_index==2 && month_check==6)
   Obs_rec_y(79,58,153)=NaN;   
   Obs_rec_y(1:19,80,137)=NaN;   
   Obs_rec_y(79:79,118,221)=NaN;   
end
if(obs_test_index==2 && month_check==7)
 Obs_rec_y(45:60,92,235)=NaN;   
 Obs_rec_y(8:20,117,180)=NaN;  
 Obs_rec_y(79:79,109,294)=NaN;   
 Obs_rec_y(79:79,135,302)=NaN;   
 Obs_rec_y(20:40,117,180)=NaN; 
 Obs_rec_y(78,109:111,294)=NaN;
  Obs_rec_y(78,109,295)=NaN;
  Obs_rec_y(78,108,293)=NaN;
   Obs_rec_y(78,135,302)=NaN;
end
if(obs_test_index==2 && month_check==8)
 Obs_rec_y(1:79,101,108)=NaN;   
  Obs_rec_y(57:79,125,240)=NaN;   
  Obs_rec_y(69:79,99,316)=NaN;   
  Obs_rec_y(78:79,107:111,293:294)=NaN;
  Obs_rec_y(78:79,111,295)=NaN;
  Obs_rec_y(78,112,294)=NaN;
  Obs_rec_y(78,107,294)=NaN;
  Obs_rec_y(78:79,91,94)=NaN;
  Obs_rec_y(78,122,303)=NaN;
  Obs_rec_y(78,73,219)=NaN;
  Obs_rec_y(78,73,219)=NaN;
  Obs_rec_y(78,70,210)=NaN;
  Obs_rec_y(78,109:110,295)=NaN;
  Obs_rec_y(79,109:110,295)=NaN;
end
if(obs_test_index==2 && month_check==9)
 Obs_rec_y(20:79,69,95)=NaN;
  Obs_rec_y(78:79,107:111,293:294)=NaN;
  Obs_rec_y(78:79,106,292)=NaN;
  Obs_rec_y(78:79,111,295)=NaN;
  Obs_rec_y(78:79,118,264)=NaN;
    Obs_rec_y(78,116,316)=NaN;
    Obs_rec_y(78,120,292)=NaN;
end
if(obs_test_index==2 && month_check==10)
 Obs_rec_y(62:79,116,245)=NaN;   
 Obs_rec_y(15:32,70,117)=NaN; 
  Obs_rec_y(59:79,99,336)=NaN; 
 Obs_rec_y(77:77,108,338)=NaN;
 Obs_rec_y(79,118,293)=NaN;
  Obs_rec_y(78:79,106,317)=NaN;
  Obs_rec_y(79,118,272)=NaN;
  Obs_rec_y(79,118,277)=NaN;
  Obs_rec_y(78,108:109,293:294)=NaN;
    Obs_rec_y(78,108,295)=NaN;
   Obs_rec_y(78,109,296)=NaN;
      Obs_rec_y(78,118,266)=NaN;
      Obs_rec_y(78,118,269)=NaN;
      Obs_rec_y(78,110,294)=NaN;
   Obs_rec_y(78,129,331)=NaN;
  % Obs_rec_y(78,119,274)=NaN;
  % Obs_rec_y(78,118,272)=NaN;
   %Obs_rec_y(78,118:124,277:282)=NaN;
   %   Obs_rec_y(78:79,130:131,286:289)=NaN;
       Obs_rec_y(78,131:131,290)=NaN;
             Obs_rec_y(78,135:138,299:300)=NaN;
end
if(obs_test_index==2 && month_check==12)
 Obs_rec_y(58:58,129,225)=NaN;   
 Obs_rec_y(58:58,128,223)=NaN;   
 Obs_rec_y(58:58,110,210)=NaN;   
 Obs_rec_y(58:58,106,207)=NaN;   
 Obs_rec_y(58:58,104,206)=NaN;
  Obs_rec_y(58:58,103,205)=NaN;   
 Obs_rec_y(58:58,102,204)=NaN;   
end

   Obs_rec_y(78,123,282)=NaN;
   Obs_rec_y(78,131,288:289)=NaN;
   Obs_rec_y(78:79,133,273)=NaN;
   Obs_rec_y(78:79,129,286)=NaN;
   Obs_rec_y(78:79,136,297)=NaN;
   Obs_rec_y(78:79,132,290)=NaN;
    Obs_rec_y(78:79,119,275)=NaN;
    Obs_rec_y(78:79,118,268)=NaN;
   Obs_rec_y(78:79,134:137,293:298)=NaN;

   Obs_rec_y(1,:,:)=nanmedian(Tclim_median(1:3,1:180,1:360),1);
Tclim_median(1,1:180,1:360)=Obs_rec_y(1,:,:);
Obs_rec_y(1,:,:)=nanmedian(Tclim_median(1:3,1:180,1:360),1);  

for i=78:1:79
  aa(:,:)=Obs_rec_y(i,:,:);figure;pcolor(aa);caxis([-2,30-i/3])
 % aa(:,:)=Obs_rec_y(find(Std_depth_model==950),:,:);pcolor(aa);caxis([-2,10])
 % aa(:,:)=Obs_rec_y(find(Std_depth_model==850),:,:);pcolor(aa);caxis([-2,10])
end

if(0)

    for i=79:1:79
  aa(:,:)=Obs_rec_y(i,:,:);figure;pcolor(aa);caxis([-2,30-i/3])
 % aa(:,:)=Obs_rec_y(find(Std_depth_model==950),:,:);pcolor(aa);caxis([-2,10])
 % aa(:,:)=Obs_rec_y(find(Std_depth_model==850),:,:);pcolor(aa);caxis([-2,10])
    end

    
end
