% this program calculates the mean series verificatoin scores. 
% read in the file "verificationpost" to get the data as follows:
%enough_3d gridlocations with enough data to attempt a reconstruction
%maskxxxx the spatial masks for the different series
%mont* is the gridbox montecarlo significance estimations. 5 columns 50%, 75%, 90%, 95%, 99%
verispa* is the gridbox RE and CE scores for decadally smoothed data.


maskamo=seamask; % this was added later to do the global SST, remove it to get AMO
maskamo=maskamo([enough_3d]); % just use the gridboxes that were reconstructed
maskextro=maskextro([enough_3d]); % just use the gridboxes that were reconstructed
masktro=masktro([enough_3d]); % just use the gridboxes that were reconstructed
masknino=masknino([enough_3d]); % just use the gridboxes that were reconstructed
maskpdo=maskpdo([enough_3d]); % just use the gridboxes that were reconstructed
masknhland=masknhland([enough_3d]); % just use the gridboxes that were reconstructed
masktamdr=masktamdr([enough_3d]); % just use the gridboxes that were reconstructed


%% apply each mask to the monte carlo and RE and CE scores for each century


amore=verisparesmlagnolut_3d.*repmat(maskamo,19,1);
extrore=verisparesmlagnolut_3d.*repmat(maskextro,19,1);
trore=verisparesmlagnolut_3d.*repmat(masktro,19,1);
nino3re=verisparesmlagnolut_3d.*repmat(masknino,19,1);
pdore=verisparesmlagnolut_3d.*repmat(maskpdo,19,1);
nhlandre=verisparesmlagnolut_3d.*repmat(masknhland,19,1);
tamdrre=verisparesmlagnolut_3d.*repmat(masktamdr,19,1);

% now for CE

amoce=verispacesmlagnolut_3d.*repmat(maskamo,19,1);
extroce=verispacesmlagnolut_3d.*repmat(maskextro,19,1);
troce=verispacesmlagnolut_3d.*repmat(masktro,19,1);
nino3ce=verispacesmlagnolut_3d.*repmat(masknino,19,1);
pdoce=verispacesmlagnolut_3d.*repmat(maskpdo,19,1);
nhlandce=verispacesmlagnolut_3d.*repmat(masknhland,19,1);
tamdrce=verispacesmlagnolut_3d.*repmat(masktamdr,19,1);

% now for the monte carlo RE

monteamore=montreespa_3d.*repmat(maskamo,5,1);
monteextrore=montreespa_3d.*repmat(maskextro,5,1);
montetrore=montreespa_3d.*repmat(masktro,5,1);
montenino3re=montreespa_3d.*repmat(masknino,5,1);
montepdore=montreespa_3d.*repmat(maskpdo,5,1);
montenhlandre=montreespa_3d.*repmat(masknhland,5,1);
montetamdrre=montreespa_3d.*repmat(masktamdr,5,1);

% and the monte carlo CE

monteamoce=montceespa_3d.*repmat(maskamo,5,1);
monteextroce=montceespa_3d.*repmat(maskextro,5,1);
montetroce=montceespa_3d.*repmat(masktro,5,1);
montenino3ce=montceespa_3d.*repmat(masknino,5,1);
montepdoce=montceespa_3d.*repmat(maskpdo,5,1);
montenhlandce=montceespa_3d.*repmat(masknhland,5,1);
montetamdrce=montceespa_3d.*repmat(masktamdr,5,1);


%% now, find the gridboxes that  pass 95% verification

for i=1:19 % loop for each century
	amorepass=find(amore(i,:)>monteamore(4,:));
	amomeanrepass(i)=nanmean(amore(i,[amorepass]));
	amomeanrepass'
end

for i=1:19 % loop for each century
	amocepass=find(amoce(i,:)>monteamoce(4,:));
	amomeancepass(i)=nanmean(amoce(i,[amocepass]));
	amomeancepass'
end
____________________________
for i=1:19 % loop for each century
	extrorepass=find(extrore(i,:)>monteextrore(4,:));
	extromeanrepass(i)=nanmean(extrore(i,[extrorepass]));
end
	extromeanrepass(1:14)'


for i=1:19 % loop for each century
	extrocepass=find(extroce(i,:)>monteextroce(4,:));
	extromeancepass(i)=nanmean(extroce(i,[extrocepass]));
end
	extromeancepass(1:14)'
________________________
for i=1:19 % loop for each century
	trorepass=find(trore(i,:)>montetrore(4,:));
	tromeanrepass(i)=nanmean(trore(i,[trorepass]));
end
	tromeanrepass(1:14)'


for i=1:19 % loop for each century
	trocepass=find(troce(i,:)>montetroce(4,:));
	tromeancepass(i)=nanmean(troce(i,[trocepass]));
end
	tromeancepass(1:14)'
	_______________________
for i=1:19 % loop for each century
	ninorepass=find(nino3re(i,:)>montenino3re(4,:));
	ninomeanrepass(i)=nanmean(nino3re(i,[ninorepass]));
end
		ninomeanrepass(1:14)'


for i=1:19 % loop for each century
	ninocepass=find(nino3ce(i,:)>montenino3ce(4,:));
	ninomeancepass(i)=nanmean(nino3ce(i,[ninocepass]));
end
		ninomeancepass(1:14)'
		
___________________________
for i=1:19 % loop for each century
	pdorepass=find(pdore(i,:)>montepdore(4,:));
	pdomeanrepass(i)=nanmean(pdore(i,[pdorepass]));
end
	pdomeanrepass(1:14)'

for i=1:19 % loop for each century
	pdocepass=find(pdoce(i,:)>montepdoce(4,:));
	pdomeancepass(i)=nanmean(pdoce(i,[pdocepass]));
end
	pdomeancepass(1:14)'

________________________
for i=1:19 % loop for each century
	nhlandrepass=find(nhlandre(i,:)>montenhlandre(4,:));
	nhlandmeanrepass(i)=nanmean(nhlandre(i,[nhlandrepass]));
end
	nhlandmeanrepass(1:14)'

for i=1:19 % loop for each century
	nhlandcepass=find(nhlandce(i,:)>montenhlandce(4,:));
	nhlandmeancepass(i)=nanmean(nhlandce(i,[nhlandcepass]));
end
	nhlandmeancepass(1:14)'
___________________________

for i=1:19 % loop for each century
	tamdrrepass=find(tamdrre(i,:)>montetamdrre(4,:));
	tamdrmeanrepass(i)=nanmean(tamdrre(i,[tamdrrepass]));
end
	tamdrmeanrepass(1:14)'

for i=1:19 % loop for each century
	tamdrcepass=find(tamdrce(i,:)>montetamdrce(4,:));
	tamdrmeancepass(i)=nanmean(tamdrce(i,[tamdrcepass]));
end
	tamdrmeancepass(1:14)'

