function format_imp_plasma_5(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;
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];
doyn = [1,32,60,91,121,152,182,213,244,274,305,335];
doyl = [1,32,61,92,122,153,183,214,245,275,306,336];
k_b = 1.380622e-23;
m_p =1.672e-27;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load in data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%for yy=1974:1974,	% this will never work.
	yy=flag;
	outputtxt = [' Working on year: ', num2str(yy)];
	    disp(outputtxt);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Determine file name and open input file.
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	yyyy=num2str(yy);	
	ssone = ['plasma_',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 %d %d %f %f %f %f %f %f %f %f %f %f %f %f %f\n'],[20,inf]);
	% get all the data at once. This saves time.
    if (length(alldata)==0), 	% if file empty, then make flag file
            %keyboard
            yrstr = num2str(yyyy);
            yrs(1:2) = str2num(yrstr(1:4));
            mons(1:2) = str2num(yrstr(5:6));
            days(1:2) = 15;
            hrs(1:2) = 0;
            mins(1:2) = 0;
            secs(1:2) = 0;
            xgse(1:2) = 1.0e+25;
            ygsm(1:2) = 1.0e+25;
            zgsm(1:2) = 1.0e+25;
            vxgse(1:2) = 1.0e+25;
            vygse(1:2) = 1.0e+25;
            vzgse(1:2) = 1.0e+25;
            temp(1:2) = 1.0e+25;
            density(1:2) = 1.0e+25;
     else			% if files has data sort the data
	    yrs      = alldata(1,:);
	    mons     = alldata(2,:);
	    days     = alldata(3,:);
	    hrs      = alldata(4,:);
	    mins     = alldata(5,:);
	    secs     = alldata(6,:);
	    msec     = alldata(7,:);
	    xgse     = alldata(8,:);
	    ygse     = alldata(9,:);
	    zgse     = alldata(10,:);
	    ygsm     = alldata(11,:);
	    zgsm     = alldata(12,:);
	    speed    = alldata(13,:);
	    vxgse   = alldata(14,:);
	    vygse   = alldata(15,:);
	    vzgse   = alldata(16,:);
	    temp = alldata(17,:);
	    density = alldata(18,:);
	    EW_ang  = alldata(19,:);
	    NS_ang  = alldata(20,:);
    end 
        Mtime = datenum(yrs,mons,days,hrs,mins,secs);	%construct Matlab time.
	len = length(yrs)	% how long are the arries.
	lyear = yrs(1);		% what year do we really have.
	clear alldata;		% free some RAM
        
	monthdoing = mons(1);	% what month are we really doing
        outputtxt = [' Working on month: ', num2str(monthdoing)];	
        disp(outputtxt);	% what month are we really doing display to screen 
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Convert Vgse to Vgsm
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        disp('Convert Velocity SC and position GSE to velocity and position GSM');
        %RF = [1 2 3; 4 5 6; 7 8 9];	example of what it looks like
        for jj=1:1:len
            [RF,RI] = gse2gsm(Mtime(jj));		% builds the matrix
            vector = [vxgse(jj),vygse(jj),vzgse(jj)];	% make a vector
            vecout = mat9vec3(RF,vector);		% transfor from gse to gsm
            vxgsm(jj) = vecout(1);			% sort our vector components
            vygsm(jj) = vecout(2);			% sort our vector components
            vzgsm(jj) = vecout(3);			% sort our vector components
        end
        disp('Convertion from GSE to GSM done');	% status report.
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % determine output file name
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        ssout    = ['imp8plasma',num2str(yrs(1)),num2str(monthdoing,'%2.2d'),'.dat'];
        ssouttwo = ['imp8plasma',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(2),'-',months(monthdoing,:),'-',num2str(yrs(1)),' ','23:59:00.00'];
             endyyymmddhhmmss = [num2str(daysofmonths(monthdoing)),'-',months(monthdoing,:),'-',num2str(yrs(1)),' ','23:59:00.00'];
        end
        startend(2) = datenum(endyyymmddhhmmss);
	% degap requires a specifiv input
	ntemp = temp';
	ndensity = density';
        nMtime = Mtime';
        nvxgsm = vxgsm';
        nvygsm = vygsm';
        nvzgsm = vzgsm';
        nxgsm  = xgse';	% xgse = xgsm
        nygsm  = ygsm';
        nzgsm  = zgsm';
        inarray = [nMtime,ntemp,ndensity,nvxgsm,nvygsm,nvzgsm,nxgsm,nygsm,nzgsm];
%keyboard
	% this never happens now.
	howlong = length(yrs);
	%free up some RAM.
        %clear Mtime bxgsm bygsm bzgsm xgsm ygsm zgsm;
        clear vygsm vzgsm 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);
	ntemp  = outarray(:,2);
	ndensity = outarray(:,3);
        nvxgsm = outarray(:,4);
        nvygsm = outarray(:,5);
        nvzgsm = outarray(:,6);
        nxgsm  = outarray(:,7);
        nygsm  = outarray(:,8);
        nzgsm  = outarray(:,9);
	%free up some RAM.
	%clear outarray;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Plot out to check
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    figure;					% make fig window
    plot(Mtime,vxgsm,'b',nMtime,nvxgsm,'k*');	% plot original and interpolated 
 %keyboard
    if (min(vxgsm) == max(vxgsm)),
        ylim([0,1]);		% set limits to avoid plotting flags
    else
        ylim([min(vxgsm),max(vxgsm)]);		% set limits to avoid plotting flags
    end
    clear Mtime vxgsm;				% free memory
    %plot(nMtime,nbxgsm,'k*');
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % read out to an ascii file
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	ctout = 1;				% set flag
	disp('Fixing rounding error in Time.');
	while( (ctout) <= lll),
	%while( (ctout) <= 1000),
		% fix the rounding error problem.
		[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
        	atxt = ['length: ',num2str(lll),' ctout: ',num2str(ctout),' day: ', num2str(newday(ctout)),' hr: ',num2str(newhr(ctout))];	% text out to check
        	disp(atxt);
        	ctout = ctout + 1;		% incriment the counter
        end
	disp('Making output array');
	% contruct output array
	outputarray = [newday',newmonth',newyear',newhr',newmin',newsec',ntemp, ndensity, nxgsm,nygsm,nzgsm,nvxgsm,nvygsm,nvzgsm]; 
	% export output array to file.
	ctout = 1;
	while( (ctout) <= lll),
	count = fprintf(fidout,'%2.2d %2.2d %4.4d %2.2d %2.2d %06.3f  %12.6e %12.6e %12.6e %12.6e %12.6e %12.6e %12.6e %12.6e\n',outputarray(ctout,:));
	ctout = ctout + 1;              % incriment the counter
	end
	%keyboard
    disp('Closing output file.');
    status = fclose (fidout);	
    
    % read out to matlab binary file.
    save(ssouttwo,'nMtime','ntemp','ndensity','nvxgsm','nvygsm','nvzgsm','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 nxgsm nygsm nzgsm;

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