year=1948:2014;
month=1:length(year)*12;
lon=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/SST/dat/air.mon.mean.nc','lon');
lat=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/SST/dat/air.mon.mean.nc','lat');

SST_temp=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/SST/dat/air.mon.mean.nc','air');

OCNFRAC=ncread('/Users/yangyang/Aerosol_Variaiton/IRUN/SST/dat/land.nc','land');

for i=1:length(lon)
    for j=1:length(lat)
        if OCNFRAC(i,j)==1
            SST_temp(i,j,:)=-999;
        end
    end
end

SST_temp(:,1:13,:)=-999;
SST_temp(:,61:73,:)=-999;


SST=SST_temp(:,:,1:(2014-1948)*12+12);


SST_MONTH=zeros(144,73,length(month));
% Change size
SST_temp=zeros(144,73,12,length(year));

for i=1:144
    for j=1:73
        n=0;
        for yearID=1:length(year)
            for monthID=1:12
                n=n+1;
                SST_temp(i,j,monthID,yearID)=SST(i,j,n);
            end
        end
    end
end


% Deseasonal
for i=1:144
    for j=1:73
        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:144
    for j=1:73
        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


%Set fillValues
for i=1:144
    for j=1:73
        if OCNFRAC(i,j)==1
            SST_MONTH(i,j,:)=-999;
        end
    end
end




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

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