function yearly_tmin_correction(filenameout)

% this program is design to adjust the daily Tmin generated by WG using
% new yearly Tmin series produced by FFT.

% load the yearly averaged Tmin after FFT
load('Pnew_tmin');
tmin_FFT=Pnew_tmin';

% load WG generated daily Tmin
load(filenameout);
[n,m]=size(gTmin);
gTmin=gTmin';
tmin_WG=reshape(gTmin,[],1);

n=length(tmin_FFT); % years
m=length(tmin_WG);  % days

% calculate yearly Tmin generated by WG, namely (Y)
j=1;
Z=zeros(n,1);
for i=1:365:m
    Z(j,1)=mean(tmin_WG(i:i+365-1,1));
    j=j+1;
end

% calculate the ratio of yearly Tmin between those after FFT and
% generated by weather generator
for i=1:n
    tmin_ratio(i,1)=tmin_FFT(i,1)-Z(i,1);
end

% extend the yearly Tmin ratio to daily scale,the data in each
% year are the same
tmin_extent=zeros(m,1);
j=1;
for i=1:365:m
    tmin_extent(i:i+365-1,1)=tmin_ratio(j,1);
    j=j+1;
end

% adjust the daily Tmin generated by WG using above ratios
tmin_adjust=zeros(size(tmin_WG));
for i=1:m
    tmin_adjust(i,1)=tmin_WG(i,1)+tmin_extent(i,1);
end
yearly_corrected_tmin=tmin_adjust;
save('yearly_corrected_tmin','yearly_corrected_tmin')