%% f107file4.m loads in the F10.7 data from the file f107day.txt.
%% It calculates and records the 81 day F10.7 flux average centered
%% around the given day, as well as the daily value from 2 days previously,
%% 1 day previously, the current day and the next day. It breaks the data 
%% into year groups and saves them as binary files. 
%% f107file4.m is different from f107file3.m in that it now also records
%% the next day's values.
%% 

clear

load f107day.txt -ascii

%Required vectors
year=f107day(:,1);
month=f107day(:,2);
day=f107day(:,3);
f107=f107day(:,4);
nop=length(year);

data=[];            %data array

%Storage vectors
f3=zeros(81,1);
y3=f3;
m3=f3;
d3=f3;

%Loop through each day of NOAA data
for i=1:nop
 %F107 value for i'th day
 f3(2:81)=f3(1:80);
 f3(1)=f107(i);
 f3av=mean(f3);
 
 %i'th year value
 y3(2:81)=y3(1:80);
 y3(1)=year(i);
 
 %i'th month value
 m3(2:81)=m3(1:80);
 m3(1)=month(i);
 
 %i'th day value
 d3(2:81)=d3(1:80);
 d3(1)=day(i);
 
 %record data for i-41 day
 if d3(41)==0
  dno=0;
 else 
  dno=daynum(d3(41),m3(41),y3(41));
 end 
 
 %Data saved in order of 2 days previously f3(43), 1 day previously f3(42), the current day f3(41),
 %the next day f3(40), and the 81 day average value.
 data=[data;[dno f3(43) f3(42) f3(41) f3(40) f3av]];
 
 %save results if end of year
 if (y3(41)~=y3(40) & y3(41)~=0) | i==nop
  y3(41)
  eval(['f',int2str(y3(41)),'=data;'])
  eval(['save f',int2str(y3(41)),' f',int2str(y3(41))])
  eval(['clear f',int2str(y3(41))])
  data=[];
 end 
end 



