lon_ECMWF=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/SST/dat/SST_ECMWF.nc','longitude');
lat_ECMWF=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/SST/dat/SST_ECMWF.nc','latitude');
lat=-89.5:1:89.5;
lon=-179.5:1:179.5;
year=1:36;
month=1:432;

SST_ECMWF_temp=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/SST/dat/SST_ECMWF.nc','sst');
SST_ECMWF_temp2=SST_ECMWF_temp;
temp=SST_ECMWF_temp2(1:180,:,:);
SST_ECMWF_temp2(1:180,:,:)=SST_ECMWF_temp2(181:360,:,:);
SST_ECMWF_temp2(181:360,:,:)=temp;
SST_ECMWF_temp2(:,1:181,:)=SST_ECMWF_temp2(:,181:-1:1,:);

SST_ECMWF=zeros(length(lon),length(lat),length(month));
for j=1:180
    SST_ECMWF(:,j,:)=(SST_ECMWF_temp2(:,j,:)+SST_ECMWF_temp2(:,j+1,:))/2;
end

SST_temp=zeros(length(lon),length(lat),12,length(year));

n=0;
for yearID=1:length(year)
    for monthID=1:12
        n=n+1;
        SST_temp(:,:,monthID,yearID)=SST_ECMWF(:,:,n);
    end
end
        



SST_MONTH=zeros(length(lon),length(lat),length(month));

% Deseasonal
for i=1:length(lon)
    for j=1:length(lat)
        for monthID=1:12
            temp=mean(SST_temp(i,j,monthID,:),4);
            for yearID=1:length(year)
                SST_temp(i,j,monthID,yearID)=SST_temp(i,j,monthID,yearID)-temp;
            end
        end
    end
end

% Change size
for i=1:length(lon)
    for j=1:length(lat)
        n=0;
        for yearID=1:length(year)
            for monthID=1:12
                n=n+1;
                SST_MONTH(i,j,n)=SST_temp(i,j,monthID,yearID);
            end
        end
    end
end



save('SST_monthly_ECMWF_1979_2014.mat','SST_MONTH')

nccreate('SST_monthly_ECMWF_1979_2014.nc','Month','Dimensions',{'time',Inf},'Format','classic')
ncwrite('SST_monthly_ECMWF_1979_2014.nc','Month', month)
ncwriteatt('SST_monthly_ECMWF_1979_2014.nc','Month','long_name','Month')
nccreate('SST_monthly_ECMWF_1979_2014.nc','lat','Dimensions',{'lat',length(lat)},'Format','classic')
ncwrite('SST_monthly_ECMWF_1979_2014.nc','lat', lat)
ncwriteatt('SST_monthly_ECMWF_1979_2014.nc','lat','units','degrees_north')
ncwriteatt('SST_monthly_ECMWF_1979_2014.nc','lat','long_name','Latitude')
nccreate('SST_monthly_ECMWF_1979_2014.nc','lon','Dimensions',{'lon',length(lon)},'Format','classic')
ncwrite('SST_monthly_ECMWF_1979_2014.nc','lon', lon)
ncwriteatt('SST_monthly_ECMWF_1979_2014.nc','lon','units','degrees_east')
ncwriteatt('SST_monthly_ECMWF_1979_2014.nc','lon','long_name','Longitude')
nccreate('SST_monthly_ECMWF_1979_2014.nc','SST_MONTH','Dimensions',{'lon',length(lon),'lat',length(lat),'month',length(month)},'Format','classic')
ncwrite('SST_monthly_ECMWF_1979_2014.nc','SST_MONTH', SST_MONTH)
ncdisp('SST_monthly_ECMWF_1979_2014.nc')