%% apmsis3.m is a modified version of apmsis2.m. It calculates the
%% current 3-hourly Ap, the mean of the current plus seven previous
%% 3-hourly Ap and the minimum of the current and next seven 3-hourly 
%% Ap.
%% Each row is for a 3 hour period, with the columns being day number,
%% UT, and the Ap values. The files are saved every year, with the 
%% naming format being apm# where # = year.
%% 

clear

years=input('Starting File Year? ');
yearf=input('Finishing File Year? ');

nop=yearf-years+1;                           %number of files to be converted
ap16=zeros(1,16);                            %next 8 3-hourly Ap, current Ap and last 7 3-hour Ap values

for i=1:nop
 if i==1
  yeari=years;                          %year index
 else
  yeari=yeari+1;                        %increment year index
 end 
 
 eval(['load a',int2str(yeari)])        %load in i'th year data file
 ap3h=ap3h';                            %take transpose of 3-hour Ap matrix data
  
 dn=0;                                  %current day number
 h3i=0;                                 %current 3-hour index
 jmax=8*length(apv);                    %number of 3-hour periods in current year
 apmat=zeros(jmax,5);                   %results matrix for i'th year

 for j=1:jmax                           %loop through each day of i'th year
  if rem(j-1,8)==0                      %check if first 3-hour period of new day
   dn=dn+1;                             %update day number
   timut=0;                             %reset UT
  else 
   timut=timut+3;                       %update new UT
  end
   
  ap16(2:16)=ap16(1:15);                %move 3-hour Ap values back one time interval
  h3i=h3i+1;                            %increment h3i by one
  ap16(1)=ap3h(h3i);                    %j'th 3-hour Ap increment
  
  %Record the j'th Ap values
  apmat(j,:)=[dn timut ap16(9) mean(ap16(9:16)) min(ap16(2:9))];   
   
 end
 
 %Save the results for the i'th year
 eval(['apm',int2str(yeari),'=apmat;'])
 eval(['save apm',int2str(yeari),' apm',int2str(yeari)])  
 eval(['clear apm',int2str(yeari)]) 
end 

%% Now need to loop through and correct data which is offset by one day
for i=1:nop
 if i~=1                                %update year if necessary
  yeari=yeari-1;
 end
 
 %Load the results for the i'th year
 eval(['load apm',int2str(yeari),' apm',int2str(yeari)])  
 eval(['nopd=size(apm',int2str(yeari),',1)/8;'])
 
 %Remove first day of data, which belongs as last day from the previous year
 eval(['apmco=apm',int2str(yeari),'(1:8,:);'])
 eval(['apm',int2str(yeari),'(1:8,:)=[];'])
 
 %Update the day number
 eval(['apm',int2str(yeari),'(:,1)=apm',int2str(yeari),'(:,1)-1;'])
 
 %Update the last day with its correct value
 if i==1
  apmco2=[nopd*ones(8,1) zeros(8,4)];
  eval(['apm',int2str(yeari),'=[apm',int2str(yeari),';apmco2];'])
 else
  apmco2(:,1)=nopd*ones(8,1);
  eval(['apm',int2str(yeari),'=[apm',int2str(yeari),';apmco2];'])
 end 
 
 %Update ampco
 apmco2=apmco;
 
 %Save modified files
 eval(['save apm',int2str(yeari),' apm',int2str(yeari)])  
 eval(['clear apm',int2str(yeari)])   
end 