year=2001:2014;
lat=-89.5:1:89.5;
lon=-179.5:1:179.5;
month={'01','02','03','04','05','06','07','08','09','10','11','12'};

OCEANFRC_MONTH_temp=zeros(180,360,length(month),length(year));
OCEANFRC_MONTH=zeros(360,180,length(month),length(year));

OCEANFRC_ANN_temp=zeros(180,360,length(year));
OCEANFRC_ANN=zeros(360,180,length(year));

for yearID=1:length(year)
    for monthID=1:length(month)
        OCEANFRC_MONTH_temp(:,:,monthID,yearID)=hdfread(['/Volumes/WORK5T/MODIS/',num2str(year(yearID)),'/MOD08_M3.A',num2str(year(yearID)),cell2mat(month(monthID)),'.hdf'], '/mod08/Data Fields/Ocean_Fraction_Day_FMean', 'Index', {[1  1],[1  1],[180  360]});
    end
end


for i=1:360
    for j=1:180
        OCEANFRC_MONTH(i,j,:,:)=OCEANFRC_MONTH_temp(181-j,i,:,:);
    end
end


for i=1:360
    for j=1:180
        for yearID=1:length(year)
            
            n=0;
            for monthID=1:length(month)
                if OCEANFRC_MONTH(i,j,monthID,yearID)>0
                    n=n+1;
                    OCEANFRC_ANN(i,j,yearID)=OCEANFRC_ANN(i,j,yearID)+OCEANFRC_MONTH(i,j,monthID,yearID);
                else
                    OCEANFRC_MONTH(i,j,monthID,yearID)=-999;
                end
            end
            if n==0
                OCEANFRC_ANN(i,j,yearID)=-999;
            else
                OCEANFRC_ANN(i,j,yearID)=OCEANFRC_ANN(i,j,yearID)/n;
            end
        
        end
    end
end


save('OCEANFRC_MODIS.mat','OCEANFRC_MONTH','OCEANFRC_ANN','lat','lon','year')

nccreate('OCEANFRC_MODIS.nc','Year','Dimensions',{'time',Inf},'Format','classic')
ncwrite('OCEANFRC_MODIS.nc','Year', year)
nccreate('OCEANFRC_MODIS.nc','lat','Dimensions',{'lat',180},'Format','classic')
ncwrite('OCEANFRC_MODIS.nc','lat', lat)
ncwriteatt('OCEANFRC_MODIS.nc','lat','units','degrees_north')
ncwriteatt('OCEANFRC_MODIS.nc','lat','long_name','Latitude')
nccreate('OCEANFRC_MODIS.nc','lon','Dimensions',{'lon',360},'Format','classic')
ncwrite('OCEANFRC_MODIS.nc','lon', lon)
ncwriteatt('OCEANFRC_MODIS.nc','lon','units','degrees_east')
ncwriteatt('OCEANFRC_MODIS.nc','lon','long_name','Longitude')
nccreate('OCEANFRC_MODIS.nc','OCEANFRC_MONTH','Dimensions',{'lon',360,'lat',180,'month',12,'year',length(year)},'Format','classic')
ncwrite('OCEANFRC_MODIS.nc','OCEANFRC_MONTH', OCEANFRC_MONTH)
nccreate('OCEANFRC_MODIS.nc','OCEANFRC_ANN','Dimensions',{'lon',360,'lat',180,'year',length(year)},'Format','classic')
ncwrite('OCEANFRC_MODIS.nc','OCEANFRC_ANN', OCEANFRC_ANN)
ncdisp('OCEANFRC_MODIS.nc')


