%Enter Directory
fname='DIRECTORY';

filename='wheat_irr.nc';
ncdisp(fullfile(fname,filename));
wheat_irr =ncread(fullfile(fname,filename),'Band1');
filename='wheat_rain.nc';
ncdisp(fullfile(fname,filename));
wheat_rain =ncread(fullfile(fname,filename),'Band1');
wheat_coveragee=wheat_irr+wheat_rain;
wheat_coveragee(wheat_coveragee==0)=NaN;
wheat_coverage=fliplr(wheat_coveragee);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
filename='Wheat.Winter.crop.calendar.nc';
ncdisp(fullfile(fname,filename));
plant_start_winter4320 =ncread(fullfile(fname,filename),'plant.start');
plant_end_winter4320 =ncread(fullfile(fname,filename),'plant.end');
harvest_start_winter4320 =ncread(fullfile(fname,filename),'harvest.start');
harvest_end_winter4320 =ncread(fullfile(fname,filename),'harvest.end');
for i=1:720
    for j=1:360
        plant_start_winter(i,j)=mean(mean(plant_start_winter4320((i-1)*6+1:i*6,(j-1)*6+1:j*6)));
        plant_end_winter(i,j)=mean(mean(plant_end_winter4320((i-1)*6+1:i*6,(j-1)*6+1:j*6)));
        harvest_start_winter(i,j)=mean(mean(harvest_start_winter4320((i-1)*6+1:i*6,(j-1)*6+1:j*6)));
        harvest_end_winter(i,j)=mean(mean(harvest_end_winter4320((i-1)*6+1:i*6,(j-1)*6+1:j*6)));
    end
end
for i=1:720
    for j=1:360
        if isnan(wheat_coverage(i,j))
            plant_start_winter(i,j)=NaN;
            plant_end_winter(i,j)=NaN;
            harvest_start_winter(i,j)=NaN;
            harvest_end_winter(i,j)=NaN;
        end
    end
end
filename='Wheat.crop.calendar.nc';
ncdisp(fullfile(fname,filename));
plant_start_spring4320 =ncread(fullfile(fname,filename),'plant.start');
plant_end_spring4320 =ncread(fullfile(fname,filename),'plant.end');
harvest_start_spring4320 =ncread(fullfile(fname,filename),'harvest.start');
harvest_end_spring4320 =ncread(fullfile(fname,filename),'harvest.end');
for i=1:720
    for j=1:360
        plant_start_spring(i,j)=mean(mean(plant_start_spring4320((i-1)*6+1:i*6,(j-1)*6+1:j*6)));
        plant_end_spring(i,j)=mean(mean(plant_end_spring4320((i-1)*6+1:i*6,(j-1)*6+1:j*6)));
        harvest_start_spring(i,j)=mean(mean(harvest_start_spring4320((i-1)*6+1:i*6,(j-1)*6+1:j*6)));
        harvest_end_spring(i,j)=mean(mean(harvest_end_spring4320((i-1)*6+1:i*6,(j-1)*6+1:j*6)));
    end
end
for i=1:720
    for j=1:360
        if isnan(wheat_coverage(i,j))
            plant_start_spring(i,j)=NaN;
            plant_end_spring(i,j)=NaN;
            harvest_start_spring(i,j)=NaN;
            harvest_end_spring(i,j)=NaN;
        end
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                       %
%                                                       %
%                        1997-1998                      %
%                                                       %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
filename='tmax.1998.nc';
ncdisp(fullfile(fname,filename));
temp_1998 =ncread(fullfile(fname,filename),'tmax');
a1=temp_1998(1:360,:,:);
a2=temp_1998(361:720,:,:);
temp_1998=[a2;a1];
temp_1998(temp_1998<-100)=NaN;
max_temp_1998_winter=nan(720,360);
average_temp_1998_winter=nan(720,360);
clear a1 a2
%%%%%%%%%%%%%%%%%%%%%%%
filename='tmax.1997.nc';
ncdisp(fullfile(fname,filename));
temp_1997 =ncread(fullfile(fname,filename),'tmax');
a1=temp_1997(1:360,:,:);
a2=temp_1997(361:720,:,:);
temp_1997=[a2;a1];
temp_1997(temp_1997<-100)=NaN;
max_temp_1997_winter=nan(720,360);
average_temp_1997_winter=nan(720,360);
clear a1 a2
%%%%%%%%%%%%%%%%%%%%%%%%
filename='tmax.2006.nc';
ncdisp(fullfile(fname,filename));
temp_2006 =ncread(fullfile(fname,filename),'tmax');
a1=temp_2006(1:360,:,:);
a2=temp_2006(361:720,:,:);
temp_2006=[a2;a1];
temp_2006(temp_2006<-100)=NaN;
max_temp_2006_winter=nan(720,360);
average_temp_2006_winter=nan(720,360);
clear a1 a2
%%%%%%%%%%%%%%%%%%%%%%%
filename='tmax.2007.nc';
ncdisp(fullfile(fname,filename));
temp_2007 =ncread(fullfile(fname,filename),'tmax');
a1=temp_2007(1:360,:,:);
a2=temp_2007(361:720,:,:);
temp_2007=[a2;a1];
temp_2007(temp_2007<-100)=NaN;
max_temp_2007_winter=nan(720,360);
average_temp_2007_winter=nan(720,360);
clear a1 a2
%%%%%%%%%%%%%%%%%%%%%%%
number_days_above_30_1998_winter=nan(720,360);
for i=1:720
    for j=1:360
        X1=plant_end_winter(i,j);
        X2=harvest_start_winter(i,j);
        if isnan(X1)||isnan(X2)
            continue
        else
            if X1<X2
                temp_in_growing_season=temp_1998(i,j,fix(X1):fix(X2));
                average_temp_1998_winter(i,j)=nanmean(temp_in_growing_season);
                max_temp_1998_winter(i,j)=nanmax(temp_in_growing_season);
                
                number_days_above_30_1998_winter(i,j)=sum(temp_in_growing_season>25);
            end
            if X1>X2
                temp_in_growing_season1=temp_1997(i,j,fix(X1):end);
                temp_in_growing_season2=temp_1998(i,j,1:fix(X2));
                temp_in_growing_season1=permute(temp_in_growing_season1,[3 1 2])';
                temp_in_growing_season2=permute(temp_in_growing_season2,[3 1 2])';
                temp_in_growing_season=[temp_in_growing_season1,temp_in_growing_season2];
                average_temp_1998_winter(i,j)=nanmean(temp_in_growing_season);
                
                temp_in_growing_season1=temp_1997(i,j,fix(X1):end);
                temp_in_growing_season2=temp_1998(i,j,1:fix(X2));
                temp_in_growing_season1=permute(temp_in_growing_season1,[3 1 2])';
                temp_in_growing_season2=permute(temp_in_growing_season2,[3 1 2])';
                temp_in_growing_season=[temp_in_growing_season1,temp_in_growing_season2];
                max_temp_1998_winter(i,j)=nanmax(temp_in_growing_season);
                number_days_above_30_1998_winter(i,j)=sum(temp_in_growing_season>25);
            end
        end
    end
end

max_temp_1998_spring=nan(720,360);
average_temp_1998_spring=nan(720,360);
number_days_above_30_1998_spring=nan(720,360);
for i=1:720
    for j=1:360
        X1=plant_end_spring(i,j);
        X2=harvest_start_spring(i,j);
        if isnan(X1)||isnan(X2)
            continue
        else
            if X1<X2
                temp_in_growing_season=temp_1998(i,j,fix(X1):fix(X2));
                average_temp_1998_spring(i,j)=nanmean(temp_in_growing_season);
                max_temp_1998_spring(i,j)=nanmax(temp_in_growing_season);
                
                 number_days_above_30_1998_spring(i,j)=sum(temp_in_growing_season>30);
            end
            if X1>X2
                temp_in_growing_season1=temp_1997(i,j,fix(X1):end);
                temp_in_growing_season2=temp_1998(i,j,1:fix(X2));
                temp_in_growing_season1=permute(temp_in_growing_season1,[3 1 2])';
                temp_in_growing_season2=permute(temp_in_growing_season2,[3 1 2])';
                temp_in_growing_season=[temp_in_growing_season1,temp_in_growing_season2];
                average_temp_1998_spring(i,j)=nanmean(temp_in_growing_season);
                
                temp_in_growing_season1=temp_1997(i,j,fix(X1):end);
                temp_in_growing_season2=temp_1998(i,j,1:fix(X2));
                temp_in_growing_season1=permute(temp_in_growing_season1,[3 1 2])';
                temp_in_growing_season2=permute(temp_in_growing_season2,[3 1 2])';
                temp_in_growing_season=[temp_in_growing_season1,temp_in_growing_season2];
                max_temp_1998_spring(i,j)=nanmax(temp_in_growing_season);
                
                 number_days_above_30_1998_spring(i,j)=sum(temp_in_growing_season>25);
            end
        end
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                       %
%                                                       %
%                        2006-2007                      %
%                                                       %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
filename='tmax.2007.nc';
ncdisp(fullfile(fname,filename));
temp_2007 =ncread(fullfile(fname,filename),'tmax');
a1=temp_2007(1:360,:,:);
a2=temp_2007(361:720,:,:);
temp_2007=[a2;a1];
temp_2007(temp_2007<-100)=NaN;
max_temp_2007_winter=nan(720,360);
average_temp_2007_winter=nan(720,360);
clear a1 a2
%%%%%%%%%%%%%%%%%%%%%%%
filename='tmax.2006.nc';
ncdisp(fullfile(fname,filename));
temp_2006 =ncread(fullfile(fname,filename),'tmax');
a1=temp_2006(1:360,:,:);
a2=temp_2006(361:720,:,:);
temp_2006=[a2;a1];
temp_2006(temp_2006<-100)=NaN;
max_temp_2006_winter=nan(720,360);
average_temp_2006_winter=nan(720,360);
clear a1 a2
%%%%%%%%%%%%%%%%%%%%%%%%
filename='tmax.2006.nc';
ncdisp(fullfile(fname,filename));
temp_2006 =ncread(fullfile(fname,filename),'tmax');
a1=temp_2006(1:360,:,:);
a2=temp_2006(361:720,:,:);
temp_2006=[a2;a1];
temp_2006(temp_2006<-100)=NaN;
max_temp_2006_winter=nan(720,360);
average_temp_2006_winter=nan(720,360);
clear a1 a2
%%%%%%%%%%%%%%%%%%%%%%%
filename='tmax.2007.nc';
ncdisp(fullfile(fname,filename));
temp_2007 =ncread(fullfile(fname,filename),'tmax');
a1=temp_2007(1:360,:,:);
a2=temp_2007(361:720,:,:);
temp_2007=[a2;a1];
temp_2007(temp_2007<-100)=NaN;
max_temp_2007_winter=nan(720,360);
average_temp_2007_winter=nan(720,360);
clear a1 a2
%%%%%%%%%%%%%%%%%%%%%%%
number_days_above_30_2007_winter=nan(720,360);
for i=1:720
    for j=1:360
        X1=plant_end_winter(i,j);
        X2=harvest_start_winter(i,j);
        if isnan(X1)||isnan(X2)
            continue
        else
            if X1<X2
                temp_in_growing_season=temp_2007(i,j,fix(X1):fix(X2));
                average_temp_2007_winter(i,j)=nanmean(temp_in_growing_season);
                max_temp_2007_winter(i,j)=nanmax(temp_in_growing_season);
                
                number_days_above_30_2007_winter(i,j)=sum(temp_in_growing_season>30);
            end
            if X1>X2
                temp_in_growing_season1=temp_2006(i,j,fix(X1):end);
                temp_in_growing_season2=temp_2007(i,j,1:fix(X2));
                temp_in_growing_season1=permute(temp_in_growing_season1,[3 1 2])';
                temp_in_growing_season2=permute(temp_in_growing_season2,[3 1 2])';
                temp_in_growing_season=[temp_in_growing_season1,temp_in_growing_season2];
                average_temp_2007_winter(i,j)=nanmean(temp_in_growing_season);
                
                temp_in_growing_season1=temp_2006(i,j,fix(X1):end);
                temp_in_growing_season2=temp_2007(i,j,1:fix(X2));
                temp_in_growing_season1=permute(temp_in_growing_season1,[3 1 2])';
                temp_in_growing_season2=permute(temp_in_growing_season2,[3 1 2])';
                temp_in_growing_season=[temp_in_growing_season1,temp_in_growing_season2];
                max_temp_2007_winter(i,j)=nanmax(temp_in_growing_season);
                
                number_days_above_30_2007_winter(i,j)=sum(temp_in_growing_season>25);
            end
        end
    end
end

max_temp_2007_spring=nan(720,360);
average_temp_2007_spring=nan(720,360);
number_days_above_30_2007_spring=nan(720,360);
for i=1:720
    for j=1:360
        X1=plant_end_spring(i,j);
        X2=harvest_start_spring(i,j);
        if isnan(X1)||isnan(X2)
            continue
        else
            if X1<X2
                temp_in_growing_season=temp_2007(i,j,fix(X1):fix(X2));
                average_temp_2007_spring(i,j)=nanmean(temp_in_growing_season);
                max_temp_2007_spring(i,j)=nanmax(temp_in_growing_season);
                
                number_days_above_30_2007_spring(i,j)=sum(temp_in_growing_season>30);
            end
            if X1>X2
                temp_in_growing_season1=temp_2006(i,j,fix(X1):end);
                temp_in_growing_season2=temp_2007(i,j,1:fix(X2));
                temp_in_growing_season1=permute(temp_in_growing_season1,[3 1 2])';
                temp_in_growing_season2=permute(temp_in_growing_season2,[3 1 2])';
                temp_in_growing_season=[temp_in_growing_season1,temp_in_growing_season2];
                average_temp_2007_spring(i,j)=nanmean(temp_in_growing_season);
                
                temp_in_growing_season1=temp_2006(i,j,fix(X1):end);
                temp_in_growing_season2=temp_2007(i,j,1:fix(X2));
                temp_in_growing_season1=permute(temp_in_growing_season1,[3 1 2])';
                temp_in_growing_season2=permute(temp_in_growing_season2,[3 1 2])';
                temp_in_growing_season=[temp_in_growing_season1,temp_in_growing_season2];
                max_temp_2007_spring(i,j)=nanmax(temp_in_growing_season);
                
                 number_days_above_30_2007_spring(i,j)=sum(temp_in_growing_season>25);
            end
        end
    end
end


dif_number_days_above_30_spring=number_days_above_30_2007_spring-number_days_above_30_1998_spring;
dif_number_days_above_30_winter=number_days_above_30_2007_winter-number_days_above_30_1998_winter;

%Enter Directory
load('DIRECTORY\LAT_COVER.mat');
load('DIRECTORY\LONG_COVER.mat');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Var=dif_number_days_above_30_spring';
Var=flipud(Var);
k=0;
for i=1:360
    for j=1:720
        if isnan(Var(i,j))
            continue
        end
        k=k+1;
        dif_number_days_above_30_spring_rmaping(k,1)=LAT_COVER(i);
        dif_number_days_above_30_spring_rmaping(k,2)=LONG_COVER(j);
        dif_number_days_above_30_spring_rmaping(k,3)=Var(i,j);
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Var=dif_number_days_above_30_winter';
Var=flipud(Var);
k=0;
for i=1:360
    for j=1:720
        if isnan(Var(i,j))
            continue
        end
        k=k+1;
        dif_number_days_above_30_winter_rmaping(k,1)=LAT_COVER(i);
        dif_number_days_above_30_winter_rmaping(k,2)=LONG_COVER(j);
        dif_number_days_above_30_winter_rmaping(k,3)=Var(i,j);
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Var=average_temp_difference_spring';
Var=flipud(Var);
k=0;
for i=1:360
    for j=1:720
        if isnan(Var(i,j))
            continue
        end
        k=k+1;
        average_temp_difference_spring_rmaping(k,1)=LAT_COVER(i);
        average_temp_difference_spring_rmaping(k,2)=LONG_COVER(j);
        average_temp_difference_spring_rmaping(k,3)=Var(i,j);
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Var=average_temp_difference_winter';
Var=flipud(Var);
k=0;
for i=1:360
    for j=1:720
        if isnan(Var(i,j))
            continue
        end
        k=k+1;
        average_temp_difference_winter_rmaping(k,1)=LAT_COVER(i);
        average_temp_difference_winter_rmaping(k,2)=LONG_COVER(j);
        average_temp_difference_winter_rmaping(k,3)=Var(i,j);
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Enter Directory
fname='DIRECTORY';

filename='Input.mat';
save(fullfile(fname,filename),'average_temp_difference_spring_rmaping','average_temp_difference_winter_rmaping'...
,'dif_number_days_above_30_winter_rmaping','dif_number_days_above_30_spring_rmaping');


