function format_imp8_mag_4(flag)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function format_imp_mag_0
%
% purpose: format the imp 8 magnetometer files
% into matlab time format and Bx, By, Bz, (GSM)
% X, Y, Z (GSM RE)
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Given
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Jul 14, 1975 62860 64.19 -0.44839 -1.3184 -3.1557 -3.3912 -2.8834 -0.94964 -1.8924 0.087856
format long e;
k_b = 1.380622e-23;
m_p =1.672e-27;
eflag = 1e+34;
months = strvcat('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
%daysofmonths = strvcat('31', '28', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31');
%daysofmonths2 = strvcat('31', '29', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31');
daysofmonths = [31 28 31 30 31 30 31 31 30 31 30 31];
daysofmonths2 = [31 29 31 30 31 30 31 31 30 31 30 31];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load in data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%for yy=1974:1974,
yy=flag;
outputtxt = [' Working on year: ', num2str(yy)];
disp(outputtxt);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Determine file name and open input file.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yyyy=num2str(yy);
ssthree = ['IMP8_mag_',yyyy,'.dat'];
outputtxt = [' Input file: ', ssthree];
disp(outputtxt);
[fidmag,message] = fopen(ssthree,'r');
if fidmag == -1
disp(message);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Enter the input mag file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Enter the input plasma file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
allmagdata = fscanf(fidmag,['%d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f %f %f\n'],[18,inf]);
%keyboard
myrs = allmagdata(1,:);
doy = allmagdata(2,:);
mmons = allmagdata(3,:);
mdays = allmagdata(4,:);
mhrs = allmagdata(5,:);
mmins = allmagdata(6,:);
msec = allmagdata(7,:);

xgsm = allmagdata(8,:);
ygsm = allmagdata(11,:);
zgsm = allmagdata(12,:);
bxgsm = allmagdata(14,:);
bygsm = allmagdata(17,:);
bzgsm = allmagdata(18,:);
Mmagtime = datenum(myrs,mmons,mdays,mhrs,mmins,msec);
clear allmagdata;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check what month we have
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
monthdoing = mmons(1);
outputtxt = [' Working on month: ', num2str(monthdoing)];
disp(outputtxt);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% determine output file name
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ssoutfour = ['imp8mag',num2str(myrs(1)),num2str(monthdoing,'%2.2d'),'.dat'];
ssoutfive = ['imp8mag',num2str(myrs(1)),num2str(monthdoing,'%2.2d'),'.mat'];
outputtxt = [' Output plasma file: ', ssoutfour];
disp(outputtxt);
% open output file
[fidmagout,message] = fopen(ssoutfour,'w');
if fidmagout == -1
disp(message);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get start and end of the month.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('Linearly interpolating to value on the minute (Resampling). ');
startyyymmddhhmmss = [num2str(1,'%2.2d'),'-',months(monthdoing,:),'-',num2str(myrs(1)),' ','00:00:00.00'];
startend(1) = datenum(startyyymmddhhmmss);
lyear = myrs(1); % Check if we have a leap year.
if ( (lyear==1976) | (lyear==1980) | (lyear==1984) | (lyear==1988) | (lyear==1992) | (lyear==1996) | (lyear==2000) ),
endyyymmddhhmmss = [num2str(daysofmonths2(monthdoing)),'-',months(monthdoing,:),'-',num2str(myrs(1)),' ','23:59:00.00'];
else
endyyymmddhhmmss = [num2str(daysofmonths(monthdoing)),'-',months(monthdoing,:),'-',num2str(myrs(1)),' ','23:59:00.00'];
end
startend(2) = datenum(endyyymmddhhmmss);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% degap requires a specific input construct input array
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nMmagtime = Mmagtime';
nbxgsm = bxgsm';
nbygsm = bygsm';
nbzgsm = bzgsm';
nxgsm = xgsm';
nygsm = ygsm';
nzgsm = zgsm';
inmagarray = [nMmagtime,nbxgsm,nbygsm,nbzgsm,nxgsm,nygsm,nzgsm];
clear bygsm bzgsm xgsm ygsm zgsm;
%keyboard
outmagarray=degapint(inmagarray,eflag,datenum('00:01:00'),startend,datenum('00:10:00'));
[dummy,lllmag] = max(outmagarray(:,1));
nMmagtime = outmagarray(1:lllmag,1);
nbxgsm = outmagarray(1:lllmag,2);
nbygsm = outmagarray(1:lllmag,3);
nbzgsm = outmagarray(1:lllmag,4);
nxgsm = outmagarray(1:lllmag,5);
nygsm = outmagarray(1:lllmag,6);
nzgsm = outmagarray(1:lllmag,7);
is = find((nbxgsm > 2000.0)|(nbxgsm<-2000.0));
nvxgsm(is) = 1e+34;
is = find((nbygsm > 2000.0)|(nbygsm<-2000.0));
nvygsm(is) = 1e+34;
is = find((nbzgsm > 2000.0)|(nbzgsm<-2000.0));
nvzgsm(is) = 1e+34;

is = find((nxgsm > 300.0)|(nxgsm < -300.0));
nvxgsm(is) = 1e+34;
is = find((nygsm > 300.0)|(nygsm < -300.0));
nvygsm(is) = 1e+34;
is = find((nzgsm > 350.0)|(nzgsm < -350.0));
nvzgsm(is) = 1e+34;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% another check
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%if (length(nMmagtime) ~= length(nMtime)),
% disp('Mtime length of two different interpolated arrays does not match!!');
% keyboard;
%end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot out to check
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%figure; % make fig window
%plot(Mmagtime,bxgsm,'b',nMmagtime,nbxgsm,'k*'); % plot original and interpolated
%if (min(bxgsm) == max(bxgsm)),
% ylim([0,1]); % set limits to avoid plotting flags
%else
% ylim([-300,300]); % set limits to avoid plotting flags
%end
%clear Mtime vxgsm bxgsm; % free memory
%plot(nMtime,nbxgsm,'k*');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% read out to an ascii file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ctout = 1; % set flag
disp('Fixing rounding error in Time.');
newyear = zeros(1,lllmag);
newmon = zeros(1,lllmag);
newday = zeros(1,lllmag);
newhr = zeros(1,lllmag);
newmin = zeros(1,lllmag);
newsec = zeros(1,lllmag);
while( (ctout) <= lllmag),
% fix the rounding error problem.
[newyear(ctout),newmonth(ctout),newday(ctout),newhr(ctout),newmin(ctout),newsec(ctout)]=datevec(outmagarray(ctout,1));
if ( abs(newsec(ctout)-60.0) < 1.0), % here due to rounding error.
newsec(ctout) = 0.000;
end % end of while loop for (ctout) <= lll
if (ctout > 1),
if (newmin(ctout) == newmin(ctout-1)),
newmin(ctout) = newmin(ctout) + 1;
if (newmin(ctout) == 60),
newmin(ctout) = 0;
end
end
end
ctout = ctout + 1; % incriment the counter
atxt = ['Plasma 1: length: ',num2str(lllmag),' ctout: ',num2str(ctout)]; % text out to check
disp(atxt);
end
disp('Making output array');
%%keyboard
% contruct output array
outputmagarray = [newday',newmonth',newyear',newhr',newmin',newsec',nbxgsm(1:lllmag),nbygsm(1:lllmag),nbzgsm(1:lllmag),nxgsm(1:lllmag),nygsm(1:lllmag),nzgsm(1:lllmag)];
% export output array to file.
ctout = 1; % set flag
while( (ctout) <= lllmag),
count = fprintf(fidmagout,'%2.2d %2.2d %4.4d %2.2d %2.2d %06.3f %12.6e %12.6e %12.6e %12.6e %12.6e %12.6e\n',outputmagarray(ctout,:));
atxt = ['Mag 2: length: ',num2str(lllmag),' ctout: ',num2str(ctout)];
ctout = ctout + 1; % incriment the counter
end
%keyboard
disp('Closing output file.');
status = fclose (fidmagout);

% read out to matlab binary file.
save(ssoutfive,'nMmagtime','nbxgsm','nbygsm','nbzgsm','nxgsm','nygsm','nzgsm');

% recall we clear the input file varies above
% clear Mtime bxgsm bygsm bzgsm xgsm ygsm zgsm;
% now clear the interpolated data so we can start the next month.
% Recall we will never get the next month.
clear newyear newday newmonth newhr newmin newsec nvxgsm nvygsm nvzgsm;

status = fclose (fidmag); % close input files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% end of for loop for years (Yeah, that was a pipe dream).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%end