clc 
clear all
close all

% readPath = '/amazon/jkolassa/AMSRE_ASCAT_SM_NN/';
% readYear = 2010;
% readMonth = 4;
% nameStr = 'amsre7H_amsre7V';
% 
% yearStr = num2str(readYear);
% monthStr = ['0' num2str(readMonth)];
% 
% load('cellNr2Coord_Mat')
% lats = cellNrMatrix(:,2);
% lons = cellNrMatrix(:,3);
% 
% load('distance_from_coast.land')
% landIndex = distance_from_coast(:,2)>100;
% 
% smMat = NaN*ones(sum(landIndex),30);
% dateVec = NaN*ones(30,1);
% 
% for d = 1:30
%    
%     if d < 10 
%         dayStr = ['0' num2str(d)];
%     else 
%         dayStr = num2str(d);
%     end
%     
%     readFile = [readPath 'SM_era_' nameStr '_' yearStr monthStr dayStr '.mat'];
%     smData = load(readFile);
%     smMat(:,d) = smData.smData(:,2);
%     
%     dateVec(d) = datenum(readYear,readMonth,d);
%     
% end
% 
% map_plot(lats(landIndex),lons(landIndex),smMat(:,1),[monthStr '-01'],...
%     'soil moisture [m^3 m^{-3}]',[0 0.45])
% map_plot(lats(landIndex),lons(landIndex),smMat(:,15),[monthStr '-15'],...
%     'soil moisture [m^3 m^{-3}]',[0 0.45])
% map_plot(lats(landIndex),lons(landIndex),smMat(:,30),[monthStr '-30'],...
%     'soil moisture [m^3 m^{-3}]',[0 0.45])
% 
% weekSM = nanmean(smMat(:,1:7),2);
% monthSM = nanmean(smMat,2);
% 
% map_plot(lats(landIndex),lons(landIndex),weekSM,'week average',...
%     'soil moisture [m^3 m^{-3}]',[0 0.45])top
% map_plot(lats(landIndex),lons(landIndex),monthSM,'month average',...
%     'soil moisture [m^3 m^{-3}]',[0 0.45])
% 
% %--------------------------------------------------------------------------
% % SET PARAMETERS
% %--------------------------------------------------------------------------
% 
% insituReadPath = '/amazon/jkolassa/ISMN/';
% smReadPath = '/amazon/jkolassa/AMSRE_ASCAT_SM_NN/';
% gpcpReadPath = '/amazon/jkolassa/GPCP/';
% modelReadPath = '/amazon/jkolassa/ERA_Land/';
% wacmosPath = '/amazon/jkolassa/WACMOS/';
% savePath = '~/Projects/AMSRE_ASCAT_retrieval/';
% 
% % insituReadPath = '/Volumes/TOSHIBA/ISMN/Data_standardized/';
% % smReadPath = '/Volumes/TOSHIBA/SM_estimates/SM_estimates_final/';
% % gpcpReadPath = '/Volumes/TOSHIBA/GPCP/transformed/';
% % modelReadPath = '/Volumes/TOSHIBA/ERA_Land/EA25_mat_files/ERA_Land_EA25_mat/';
% % savePath = '/Users/Jana/Documents/Columbia/Projects/SoilMoisture_Retrieval_Daily/statistics_sm/';
% networkNames = {...
%     'AMMA';...
%     'AWDN';...
%     'CALABRIA';...
% 
%     };
% startYear = 2009;
% stopYear = 2011;
% startMonth = 1;
% stopMonth = 9;
% p_threshold = 0.05;
% nameStr = 'amsre7H_amsre7V_amsre11H_amsre11V_amsre37H_amsre37V_amsre7Hdn_amsre7Vdn_amsre11Hdn_amsre11Vdn_amsre37Hdn_amsre37Vdn';
% stationIndices = [6 12 2];
% 
% %--------------------------------------------------------------------------
% % COMPUTATIONS
% %--------------------------------------------------------------------------
% 
% load('cellNr2Coord_Mat')
% lats = cellNrMatrix(:,2);
% lons = cellNrMatrix(:,3);
% 
% 
% % define land pixels
% 
% load('distance_from_coast.land')
% landIndex = distance_from_coast(:,2) > 100;
% 
% % account for data availability
% 
% startMonths = ones(stopYear-startYear+1,1);
% startMonths(1) = startMonth;
% stopMonths = 12*ones(stopYear-startYear+1,1);
% stopMonths(end) = stopMonth;
% totalMonths = sum(stopMonths-startMonths+1);
% 
% % account for leap years
% 
% leapYears = 1992:4:2014;
% monthLength_nly = [31 28 31 30 31 30 31 31 30 31 30 31];
% monthLength_ly = [31 29 31 30 31 30 31 31 30 31 30 31];
% 
% % load mean data 
% 
% meanFile = [savePath 'monthly_mean_and_std_sm_era.mat'];
% meanData = load(meanFile);
% ismnMeanFile = [savePath 'monthly_mean_and_std_ismn.mat'];
% ismnMeanData = load(ismnMeanFile);
% 
% 
% % initialize data matrices
% 
% smMat = NaN*ones(length(stationIndices),totalMonths*31);
% insituMat = NaN*ones(length(stationIndices),totalMonths*31);
% gpcpMat = NaN*ones(length(stationIndices),totalMonths*31);
% modelMat = NaN*ones(length(stationIndices),totalMonths*31);
% wacmosMat = NaN*ones(length(stationIndices),totalMonths*31);
% dateVec = NaN*ones(totalMonths*31,1);
% 
% smMat_orig = NaN*ones(length(stationIndices),totalMonths*31);
% insituMat_orig = NaN*ones(length(stationIndices),totalMonths*31);
% gpcpMat_orig = NaN*ones(length(stationIndices),totalMonths*31);
% modelMat_orig = NaN*ones(length(stationIndices),totalMonths*31);
% wacmosMat_orig = NaN*ones(length(stationIndices),totalMonths*31);
% 
% 
% 
% for n = 1:length(networkNames)
%     
%     networkNames{n}
%     
%     networkName = networkNames{n};
%     files = dir(insituReadPath);
%     files = files(3:end);
%     fileNames = extractfield(files,'name');
%     tmp = strfind(fileNames,networkName);
%     emptyCells = cellfun(@isempty,tmp);
%     fileIndex = find(emptyCells == 0);
%     testFile = [insituReadPath files(fileIndex(1)).name];
%     load(testFile);
%     stationNumber = length(ismnData.latitude);
%     
%     % define cell numbers of in situ stations
%     
%     locationMat = NaN*ones(length(ismnData.latitude),1);
%     
%     for i = 1:length(locationMat)
%         
%         diffVec = sqrt((lats(landIndex)-ismnData.latitude(i)).^2 + ...
%             (lons(landIndex)-ismnData.longitude(i)).^2);
%         minIndex = find(diffVec==min(diffVec));
%         locationMat(i) = minIndex(1);
%         
%     end
%     
%     locationMat = locationMat(stationIndices(n));
%     
%     % initialize matrices
%     
% 
%     pointIndex = 0;
%     
%     
%     for y = startYear:stopYear
%         y
%         
%         yearStr = num2str(y);
%         yearIndex = y - startYear + 1;
%         
%         if any(y==leapYears)
%             monthLength = monthLength_ly;
%         else
%             monthLength = monthLength_nly;
%         end
%         
%         for m = startMonths(yearIndex):stopMonths(yearIndex)
%             
%             
%             if m < 10
%                 monthStr = ['0' num2str(m)];
%             else
%                 monthStr = num2str(m);
%             end
%             
%             for d = 1:monthLength(m)
%                 
%                 pointIndex = pointIndex + 1;
%                 
%                 if d < 10
%                     dayStr = ['0' num2str(d)];
%                 else
%                     dayStr = num2str(d);
%                 end
%                 
%                 smFile = [smReadPath 'SM_era_' nameStr '_' yearStr monthStr dayStr '.mat'];
%                 smData = load(smFile);
%                 smMat(n,pointIndex) = (smData.smData(locationMat,2) -...
%                     meanData.smMeanCell{yearIndex}(locationMat,m))./meanData.smStdCell{yearIndex}(locationMat,m);
%                 smMat_orig(n,pointIndex) = smData.smData(locationMat,2);
%                 
%                 gpcpFile = [gpcpReadPath 'gpcp_daily_' yearStr monthStr dayStr '.mat'];
%                 gpcpData = load(gpcpFile);
%                 gpcpMat(n,pointIndex) = gpcpData.gpcpMat(locationMat,2);
%                 
%                 modelFile = [modelReadPath 'era_sm_' yearStr monthStr ...
%                     dayStr '.mat'];
%                 eraData = era_land_ea25_read(modelFile);
%                 modelSMVec = eraData.soilMoisture(landIndex);
%                 modelMat(n,pointIndex) = (modelSMVec(locationMat) - ...
%                     meanData.modelMeanCell{yearIndex}(locationMat,m))./meanData.modelStdCell{yearIndex}(locationMat,m);
%                 modelMat_orig(n,pointIndex) = modelSMVec(locationMat);
% 
%                 wacmosFile = [wacmosPath 'WACMOS_SM_' yearStr monthStr dayStr '.mat'];
%                 wacmosData = load(wacmosFile);
%                 wacmosVec = wacmosData.soilMoisture;
%                 filterIndex = (wacmosData.flagData == 1) + (wacmosData.flagData==127) ...
%                     + (wacmosData.flagData==3);
%                 filterIndex = filterIndex > 0;
%                 wacmosVec(filterIndex) = NaN;
%                 wacmosVec = wacmosVec(landIndex);
%                 wacmosMat(:,pointIndex) = (wacmosVec(locationMat)-meanData.wacmosMeanCell{yearIndex}(locationMat,m))./...
%                     meanData.wacmosStdCell{yearIndex}(locationMat,m);
%                 wacmosMat_orig(n,pointIndex) = wacmosVec(locationMat);
%                 
%                 insituFile = [insituReadPath networkName '_' yearStr monthStr dayStr '.mat'];
%                 if exist(insituFile)>0
%                     load(insituFile)
%                     insituMat(n,pointIndex) = (ismnData.soilMoisture(stationIndices(n),1) - ...
%                         ismnMeanData.insituMeanCell{yearIndex,n}(stationIndices(n),m))./ismnMeanData.insituStdCell{yearIndex,n}(stationIndices(n),m);
%                     insituMat_orig(n,pointIndex) = ismnData.soilMoisture(stationIndices(n),1);
%                 end
%
%                 dateVec(pointIndex) = datenum(y,m,d);
%
%             end
%         end
%     end
%
%     bufferIndex = isnan(dateVec);
%     dateVec(bufferIndex) = [];
%     smMat(:,bufferIndex) = [];
%     insituMat(:,bufferIndex) = [];
%     gpcpMat(:,bufferIndex) = [];
%     modelMat(:,bufferIndex) = [];
%     wacmosMat(:,bufferIndex) = [];
%     smMat_orig(:,bufferIndex) = [];
%     insituMat_orig(:,bufferIndex) = [];
%     modelMat_orig(:,bufferIndex) = [];
%     wacmosMat_orig(:,bufferIndex) = [];
%
% end
%
% %%
%
% for i = 1:length(networkNames)
%
%     figure(i)
%     [AX,H1,H2]=plotyy(dateVec,[smMat(i,:)' insituMat(i,:)' wacmosMat(i,:)' modelMat(i,:)'],...
%         dateVec,gpcpMat(i,:));
%     set(get(AX(1),'Ylabel'),'String','soil moisture [-]')
%     set(get(AX(2),'Ylabel'),'String','precipitation [mm/day]')
%     set(AX(1),'YLim',[-20 20])
%     set(AX(1),'YTick',-20:10:20)
%     set(AX(2),'YLim',[0 100])
%     set(AX(2),'YTick',0:25:100)
%     xlabel('time [mm-yyyy]')
%     datetick(AX(1),'x','mm-yyyy','keepticks')
%     set(AX(2),'XTick',[])
%     title(networkNames{i})
%     legend([H1;H2],'NN retrieval','ISMN','WACMOS','ERA','GPCP');
%
% end
%
% % figure(2)
% % [AX,H1,H2]=plotyy(dateVec,[smMat_orig(1,:)' insituMat_orig(1,:)' wacmosMat_orig(1,:)' modelMat_orig(1,:)'],...
% %     dateVec,gpcpMat(1,:));
% % set(get(AX(1),'Ylabel'),'String','soil moisture [-]')
% % set(get(AX(2),'Ylabel'),'String','precipitation [mm/day]')
% % set(AX(1),'YLim',[-0.2 0.2])
% % set(AX(1),'YTick',-0.2:0.01:0.2)
% % set(AX(2),'YLim',[0 100])
% % set(AX(2),'YTick',0:25:100)
% % xlabel('time [mm-yyyy]')
% % datetick(AX(1),'x','mm-yyyy','keepticks')
% % set(AX(2),'XTick',[])
% % legend([H1;H2],'NN retrieval','ISMN','WACMOS','ERA','GPCP');
% %
% % figure(3)
% % plot(dateVec,insituMat(1,:))
% % datetick('x','mm-yyyy','keepticks')
%


readPath = '/amazon/jkolassa/AMSRE_raw_climatology/';
savePath = '/amazon/jkolassa/AMSRE_raw_climatology_new/';


load('distance_from_coast.land')
landIndex = distance_from_coast(:,2) > 100;

load('cellNr2Coord_Mat')
lats = cellNrMatrix(:,2);
lons = cellNrMatrix(:,3);



startYear = 2007;
stopYear = 2011;


%--------------------------------------------------------------------------
% COMPUTE SOIL MOISTURE ESTIMATES
%--------------------------------------------------------------------------

% SET GLOBAL PARAMETERS
%--------------------------------------------------------------------------

% account for data availability

startMonths = ones((stopYear-startYear+1),1);
startMonths(1) = 10;
stopMonths = 12*ones((stopYear-startYear+1),1);
stopMonths(end) = 9;

% account for effect of leap year

leapYears = 1992:4:2013;
monthLength_nly = [31 28 31 30 31 30 31 31 30 31 30 31];
monthLength_ly = [31 29 31 30 31 30 31 31 30 31 30 31];

% define land pixels

load('distance_from_coast.land');
landIndex = distance_from_coast(:,2) > 100;
landPixels = sum(landIndex);

yearIndex = 0;

for y = startYear:stopYear
    y
    
    yearIndex = yearIndex + 1;
    yearStr = num2str(y);
    
    if sum(y==leapYears)>0
        monthLength = monthLength_ly;
    else
        monthLength = monthLength_nly;
    end
    
    for m = startMonths(yearIndex):stopMonths(yearIndex)
        m
        
        if m < 10
            monthStr = ['0' num2str(m)];
        else
            monthStr = num2str(m);
        end
        
        
        for d = 1:monthLength(m)
            
            if d < 10
                dayStr = ['0' num2str(d)];
            else
                dayStr = num2str(d);
            end
            
            readFile = [readPath 'AMSR-E_Tb_clim_' yearStr monthStr dayStr '.mat'];
            amsreClimData = load(readFile);
            
            ascatIndex = amsreClimData.ascatAvailabilityIndex==1;
            lats_ascat = lats(ascatIndex);
            lons_ascat = lons(ascatIndex);
            
            if sum(ascatIndex) > 0
                
                amsre_avg_7_h = amsreClimData.amsreData.amsre_avg_7_h;
                amsre_avg_7_h = amsre_avg_7_h(ascatIndex);
                nanIndex = isnan(amsre_avg_7_h);
                if sum(nanIndex)<sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_avg_7_h(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_avg_7_h(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_avg_7_h(amsreClimData.ascatAvailabilityIndex) = amsre_avg_7_h;
                end
                
                amsre_avg_7_v = amsreClimData.amsreData.amsre_avg_7_v;
                amsre_avg_7_v = amsre_avg_7_v(ascatIndex);
                nanIndex = isnan(amsre_avg_7_v);
                if sum(nanIndex)<sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_avg_7_v(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_avg_7_v(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_avg_7_v(amsreClimData.ascatAvailabilityIndex) = amsre_avg_7_v;
                end
                
                amsre_avg_11_h = amsreClimData.amsreData.amsre_avg_11_h;
                amsre_avg_11_h = amsre_avg_11_h(ascatIndex);
                nanIndex = isnan(amsre_avg_11_h);
                if sum(nanIndex)<sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_avg_11_h(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_avg_11_h(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_avg_11_h(amsreClimData.ascatAvailabilityIndex) = amsre_avg_11_h;
                end
                
                amsre_avg_11_v = amsreClimData.amsreData.amsre_avg_11_v;
                amsre_avg_11_v = amsre_avg_11_v(ascatIndex);
                nanIndex = isnan(amsre_avg_11_v);
                if sum(nanIndex)<sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_avg_11_v(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_avg_11_v(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_avg_11_v(amsreClimData.ascatAvailabilityIndex) = amsre_avg_11_v;
                end
                
                amsre_avg_37_h = amsreClimData.amsreData.amsre_avg_37_h;
                amsre_avg_37_h = amsre_avg_37_h(ascatIndex);
                nanIndex = isnan(amsre_avg_37_h);
                if sum(nanIndex)<sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_avg_37_h(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_avg_37_h(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_avg_37_h(amsreClimData.ascatAvailabilityIndex) = amsre_avg_37_h;
                end
                
                amsre_avg_37_v = amsreClimData.amsreData.amsre_avg_37_v;
                amsre_avg_37_v = amsre_avg_37_v(ascatIndex);
                nanIndex = isnan(amsre_avg_37_v);
                if sum(nanIndex)<sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_avg_37_v(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_avg_37_v(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_avg_7_v(amsreClimData.ascatAvailabilityIndex) = amsre_avg_37_v;
                end
                
                amsre_DN_7_h = amsreClimData.amsreData.amsre_DN_7_h;
                amsre_DN_7_h = amsre_DN_7_h(ascatIndex);
                nanIndex = isnan(amsre_DN_7_h);
                if sum(nanIndex)<sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_DN_7_h(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_DN_7_h(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_DN_7_h(amsreClimData.ascatAvailabilityIndex) = amsre_DN_7_h;
                end
                
                amsre_DN_7_v = amsreClimData.amsreData.amsre_DN_7_v;
                amsre_DN_7_v = amsre_DN_7_v(ascatIndex);
                nanIndex = isnan(amsre_DN_7_v);
                if sum(nanIndex)<sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_DN_7_v(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_DN_7_v(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_DN_7_v(amsreClimData.ascatAvailabilityIndex) = amsre_DN_7_v;
                end
                
                amsre_DN_11_h = amsreClimData.amsreData.amsre_DN_11_h;
                amsre_DN_11_h = amsre_DN_11_h(ascatIndex);
                nanIndex = isnan(amsre_DN_11_h);
                if sum(nanIndex) < sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_DN_11_h(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_DN_11_h(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_DN_11_h(amsreClimData.ascatAvailabilityIndex) = amsre_DN_11_h;
                end
                
                amsre_DN_11_v = amsreClimData.amsreData.amsre_DN_11_v;
                amsre_DN_11_v = amsre_DN_11_v(ascatIndex);
                nanIndex = isnan(amsre_DN_11_v);
                if sum(nanIndex)<sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_DN_11_v(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_DN_11_v(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_DN_11_v(amsreClimData.ascatAvailabilityIndex) = amsre_DN_11_v;
                end
                
                amsre_DN_37_h = amsreClimData.amsreData.amsre_DN_37_h;
                amsre_DN_37_h = amsre_DN_37_h(ascatIndex);
                nanIndex = isnan(amsre_DN_37_h);
                if sum(nanIndex)<sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_DN_37_h(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_DN_37_h(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_DN_37_h(amsreClimData.ascatAvailabilityIndex) = amsre_DN_37_h;
                end
                
                amsre_DN_37_v = amsreClimData.amsreData.amsre_DN_37_v;
                amsre_DN_37_v = amsre_DN_37_v(ascatIndex);
                nanIndex = isnan(amsre_DN_37_v);
                if sum(nanIndex)<sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_DN_37_v(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_DN_37_v(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_DN_7_v(amsreClimData.ascatAvailabilityIndex) = amsre_DN_37_v;
                end
                
                amsre_mpdi_7 = amsreClimData.amsreData.amsre_mpdi_7;
                amsre_mpdi_7 = amsre_mpdi_7(ascatIndex);
                nanIndex = isnan(amsre_mpdi_7);
                if sum(nanIndex)<sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_mpdi_7(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_mpdi_7(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_mpdi_7(amsreClimData.ascatAvailabilityIndex) = amsre_mpdi_7;
                end
                
                amsre_mpdi_11 = amsreClimData.amsreData.amsre_mpdi_11;
                amsre_mpdi_11 = amsre_mpdi_11(ascatIndex);
                nanIndex = isnan(amsre_mpdi_11);
                if sum(nanIndex)<sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_mpdi_11(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_mpdi_11(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_mpdi_11(amsreClimData.ascatAvailabilityIndex) = amsre_mpdi_11;
                end
                
                amsre_mpdi_37 = amsreClimData.amsreData.amsre_mpdi_37;
                amsre_mpdi_37 = amsre_mpdi_37(ascatIndex);
                nanIndex = isnan(amsre_mpdi_37);
                if sum(nanIndex)<sum(ascatIndex)
                    intData = griddata(lats_ascat(nanIndex==0),lons_ascat(nanIndex==0),...
                        amsre_mpdi_37(nanIndex==0),lats_ascat(nanIndex),lons_ascat(nanIndex),'cubic');
                    amsre_mpdi_37(nanIndex) = intData;
                    amsreClimData.amsreData.amsre_mpdi_37(amsreClimData.ascatAvailabilityIndex) = amsre_mpdi_37;
                end
                
            end
            
            amsreData = amsreClimData.amsreData;
            ascatAvailabilityIndex = amsreClimData.ascatAvailabilityIndex;
            amsreAvailabilityIndex = amsreClimData.amsreAvailabilityIndex;
            ascat_amsre_AvailabilityIndex = amsreClimData.ascat_amsre_AvailabilityIndex;
            
            saveFile = [savePath 'AMSR-E_Tb_clim_' yearStr monthStr dayStr '.mat'];
            save(saveFile,'amsreData','amsreAvailabilityIndex','ascatAvailabilityIndex',...
                'ascat_amsre_AvailabilityIndex');
            
        end
    end
end









