function format_imp_mag_2(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;
ct    = 1;
ii    = 1;
mm    = 1;
where = 0;
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);	
	ssone = ['IMP8_',yyyy,'.dat'];
	outputtxt = [' Input file: ', ssone];
	disp(outputtxt);
	[fid,message] = fopen(ssone,'r');
	if fid == -1
		disp(message);
	end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Enter the first few lines of the input file
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	alldata = fscanf(fid,['%d %d %d %d %d %f %f %f %f %f %f %f %f %f %f %f %f\n'],[17,inf]);
	% line = fgetl(fid);	
	% get the first line and construct the start Matlab time.
	yrs = alldata(1,:);
	mons = alldata(2,:);
	days = alldata(3,:);
	hrs = alldata(4,:);
	mins = alldata(5,:);
	secs = alldata(6,:);
        Mtime = datenum(yrs,mons,days,hrs,mins,secs);

	% get the input data
        bxgsm = alldata(7,:);  
        bygsm = alldata(8,:); 
        bzgsm = alldata(9,:);
%        dummy = fscanf(fid,'%f',1);
        xgsm = alldata(10,:);
        ygsm = alldata(11,:);
        zgsm = alldata(12,:);
%        dummy = fscanf(fid,'%f',1);
%        dummy = fscanf(fid,'%f',1);
%        dummy = fscanf(fid,'%f',1);
%        dummy = fscanf(fid,'%f',1);
%        ii = ii+1;
	clear alldata;
	monthdoing = mons(1);
        outputtxt = [' Working on month: ', num2str(monthdoing)];
        disp(outputtxt);
        % determine output file name
        ssout    = ['imp8mag',num2str(yrs(1)),num2str(monthdoing,'%2.2d'),'.dat'];
        ssouttwo = ['imp8mag',num2str(yrs(1)),num2str(monthdoing,'%2.2d'),'.mat'];
	outputtxt = [' Output file: ', ssout];
        disp(outputtxt);
        % open output file
        [fidout,message] = fopen(ssout,'w');
        if fidout == -1
                disp(message);
        end

	disp('Linearly interpolating to value on the minute (Resampling). ');
	startyyymmddhhmmss = [num2str(1,'%2.2d'),'-',months(monthdoing,:),'-',num2str(yrs(1)),' ','00:00:00.00'];
        startend(1) = datenum(startyyymmddhhmmss);
        lyear = yrs(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(yrs(1)),' ','23:59:00.00'];
        else
             endyyymmddhhmmss = [num2str(daysofmonths(monthdoing)),'-',months(monthdoing,:),'-',num2str(yrs(1)),' ','23:59:00.00'];
        end
        startend(2) = datenum(endyyymmddhhmmss);
	% degap requires a specifiv input

        nMtime = Mtime';
        nbxgsm = bxgsm';
        nbygsm = bygsm';
        nbzgsm = bzgsm';
        nxgsm  = xgsm';
        nygsm  = ygsm';
        nzgsm  = zgsm';
        inarray = [nMtime,nbxgsm,nbygsm,nbzgsm,nxgsm,nygsm,nzgsm];

	% this never happens now.
	howlong = length(yrs);
	%free up some RAM.
        clear Mtime bxgsm bygsm bzgsm xgsm ygsm zgsm;

	% run the degap program which also interpolates to the minute
        outarray=degapint(inarray,1.0e25,datenum('00:01:00'),startend,datenum('00:10:00'));
        [maxval,lll] = max(outarray(:,1));
 keyboard
        nMtime = outarray(:,1);
        nbxgsm = outarray(:,2);
        nbygsm = outarray(:,3);
        nbzgsm = outarray(:,4);
        nxgsm  = outarray(:,5);
        nygsm  = outarray(:,6);
        nzgsm  = outarray(:,7);
	%free up some RAM.
	%clear outarray;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % read out to an ascii file
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	ctout = 1;
	disp('Reading out values to file.');
	while( (ctout) <= lll),
		[newyear(ctout),newmonth(ctout),newday(ctout),newhr(ctout),newmin(ctout),newsec(ctout)]=datevec(outarray(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
		count = fprintf(fidout,'%2.2d %2.2d %4.4d %2.2d %2.2d %06.3f %12.8f %12.8f %12.8f %12.8f %12.8f %12.8f\n',...
                newday(ctout),newmonth(ctout),newyear(ctout),newhr(ctout),newmin(ctout),newsec(ctout),...
                nbxgsm(ctout),nbygsm(ctout),nbzgsm(ctout),nxgsm(ctout),nygsm(ctout),nzgsm(ctout));
           
                atxt = ['length: ',num2str(lll),' ctout: ',num2str(ctout),' HHMMSS: ',num2str(newhr(ctout)),' ',num2str(newmin(ctout)),' ',num2str(newsec(ctout)) ];
        disp(atxt);
        %keyboard
        ctout = ctout + 1;
        end
    %keyboard
    disp('Closing output file.');
    status = fclose (fidout);	
    
    % read out to matlab binary file.
    save(ssouttwo,'nMtime','nbxgsm','nbygsm','nbzgsm','nxgsm','nygsm','nzgsm');
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Plot out to check
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    figure;
    plot(Mtime,bxgsm,'b',nMtime,nbxgsm,'k*');
    %xlim([min(Mtime),max(Mtime)]);
    %ylim([min(bxgsm),max(bxgsm)]);

    % 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 nbxgsm nbygsm nbzgsm nxgsm nygsm nzgsm;
    ii      = 1;        % reset counters
    ct      = 1;        % reset counters

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