function format_imp_plasma_3(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 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
format long e;
ct    = 1;          % initialize counter
ii    = 1;          % initialize input file counter.
mm    = 1;          % initialize counter
where = 0;          % initiallize month in while loop
months = strvcat('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
                    % strings of the months
daysofmonths  = [31 28 31 30 31 30 31 31 30 31 30 31];      % days total for each month
daysofmonths2 = [31 29 31 30 31 30 31 31 30 31 30 31];      % damn leap year
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load in data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%for yy=1974:1974,         % ambitious hope to do multiple years
	yy=flag;                % input year into program
	outputtxt = [' Working on year: ', num2str(yy)];        % construct input file name
	disp(outputtxt);                                
	yyyy=num2str(yy);                                       % convert to string	
	ssone = ['plasma_',yyyy,'.dat'];                        % indicate if correct
	outputtxt = [' Input file: ', ssone];
	disp(outputtxt);
	[fid,message] = fopen(ssone,'r');                       % open input file
	if fid == -1                                            % check if opened
		disp(message);
	end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % skip first 31 lines   (header)
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	for jj=1:30,
		line = fgetl(fid);
	end
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % get first fucking data line
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	year(ii)  = fscanf(fid,'%d',1);
	mon(ii)   = fscanf(fid,'%d',1);
	day(ii)   = fscanf(fid,'%d',1);
	hr(ii)    = fscanf(fid,'%d',1);
	min(ii)   = fscanf(fid,'%d',1);
    monthdoing = mon(ii);
	sec(ii)   = fscanf(fid,'%d',1);
	msec(ii)  = fscanf(fid,'%d',1);
	Mtime(ii) = datenum(year(ii),mon(ii),day(ii),hr(ii),min(ii),sec(ii));

	xgsm(ii)    = fscanf(fid,'%f',1);
	dummy       = fscanf(fid,'%f',1);
    dummy       = fscanf(fid,'%f',1);
    ygsm(ii)    = fscanf(fid,'%f',1);
    zgsm(ii)    = fscanf(fid,'%f',1);
    dummy       = fscanf(fid,'%f',1);
    vxgse(ii)      = fscanf(fid,'%f',1);
    vygse(ii)      = fscanf(fid,'%f',1);
    vzgse(ii)      = fscanf(fid,'%f',1);
	vtherm(ii)  = fscanf(fid,'%f',1);
	ionden(ii)  = fscanf(fid,'%f',1);
	EW_ang(ii)  = fscanf(fid,'%f',1);
    NS_ang(ii)  = fscanf(fid,'%f',1);
%keyboard	
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   % get the rest of the data while loop and open output file
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   %for hhh=1:1:3,
   while( (monthdoing <= 12) & (feof(fid) == 0) ), %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        where = mon(ii);
	    monthdoing = mon(ii);
        outputtxt = [' Working on month: ', months(monthdoing,:)];
        disp(outputtxt);
	    %%where = strmatch(monstr,months);
        ssout = ['imp8plasma',yyyy(3:4),num2str(monthdoing,'%2.2d'),'.dat'];
        ssouttwo = ['imp8plasma',yyyy(3:4),num2str(monthdoing,'%2.2d'),'.mat'];
	    outputtxt = [' Output file: ', ssout];
        disp(outputtxt);
        [fidout,message] = fopen(ssout,'w');
        if fidout == -1
                disp(message);
        end

        ii=ii+1;
        %for ii=2:30,
        %keyboard
        while( (mon(ii-1) == monthdoing) & (feof(fid) == 0) ),
		    temp  = fscanf(fid,'%d',1);
                error       = feof(fid);
                if (error ~= 0),
                       break;
                end
		    year(ii)  = temp;
        	    mon(ii)   = fscanf(fid,'%d',1);
        	    day(ii)   = fscanf(fid,'%d',1);
        	    hr(ii)    = fscanf(fid,'%d',1);
        	    min(ii)   = fscanf(fid,'%d',1);
        	    sec(ii)   = fscanf(fid,'%d',1);
        	    msec(ii)  = fscanf(fid,'%d',1);
		        Mtime(ii) = datenum(year(ii),mon(ii),day(ii),hr(ii),min(ii),sec(ii));

        	    xgsm(ii)  = fscanf(fid,'%f',1);
        	    dummy     = fscanf(fid,'%f',1);
        	    dummy     = fscanf(fid,'%f',1);
        	    ygsm(ii)  = fscanf(fid,'%f',1);
        	    zgsm(ii)  = fscanf(fid,'%f',1);
        	    dummy     = fscanf(fid,'%f',1);
        	    vxgse(ii)    = fscanf(fid,'%f',1);
        	    vygse(ii)    = fscanf(fid,'%f',1);
        	    vzgse(ii)    = fscanf(fid,'%f',1);
        	    vtherm(ii)  = fscanf(fid,'%f',1);
        	    ionden(ii)  = fscanf(fid,'%f',1);
        	    EW_ang(ii)  = fscanf(fid,'%f',1);
        	    NS_ang(ii)  = fscanf(fid,'%f',1);

		        outputtxt = ['YY MM DD HH MM SS: ',num2str(year(ii)),' ',num2str(mon(ii)),' ',num2str(day(ii)),' ',...
                    num2str(hr(ii)),' ',num2str(min(ii)),' ',num2str(sec(ii)),' Sample: ',num2str(ii),' Vx: ',num2str(vxgse(ii-1)) ];
		        disp(outputtxt);
                ii = ii+1;
   	    end
        atxt = ['Exited while loop. Logic: ', num2str((mon(ii-1) == monthdoing)),' ',num2str((feof(fid) == 0))];
	    disp(atxt);
        lllarray = length(Mtime);
	
	   
	    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Convert Vgse to Vgsm
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        disp('Convert Velocity GSE to velocity GSM');
        %RF = [1 2 3; 4 5 6; 7 8 9];
        for jj=1:1:lllarray,
            [RF,RI] = gse2gsm(Mtime(jj));
            %keyboard
            vector = [vxgse(jj),vygse(jj),vzgse(jj)];
            %vecin = vector';
            %vecout = RF*vecin;
            vecout = mat9vec3(RF,vector);
            vxgsm(jj) = vecout(1);
            vygsm(jj) = vecout(2);
            vzgsm(jj) = vecout(3);
        end
        disp('Convertion from GSE to GSM done');
        %keyboard
        
	    disp('Linearly interpolating to value on the minute (Resampling). ');
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %% starthhmmss needs to be changed to 00:00:00.000
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	    startyyymmddhhmmss = ['01','-',months(monthdoing,:),'-',yyyy(1:4),' ','00:00:00'];
        startend(1) = datenum(startyyymmddhhmmss);
    
        lyear = num2str(yyyy(1:4));
    %%keyboard
        if ( (lyear==1976) | (lyear==1980) | (lyear==1984) | (lyear==1988) | (lyear==1992) | (lyear==1996) | (lyear==2000)  ),
             endyyymmddhhmmss = [num2str(daysofmonths2(monthdoing)),'-',months(monthdoing,:),'-',yyyy(1:4),' ','23:59:00.00'];
        else
             endyyymmddhhmmss = [num2str(daysofmonths(monthdoing)),'-',months(monthdoing,:),'-',yyyy(1:4),' ','23:59:00.00'];
        end
        startend(2) = datenum(endyyymmddhhmmss);
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % did month increment? (do after get start and end date and time)
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %keyboard
        if (mon(ii-1) ~= monthdoing),
		        monthdoing = mon(ii-1);
	    end
    
    %keyboard
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Arrays must be turned for bob's degap program
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        nxgsm   = xgsm';
        nygsm   = ygsm';
        nzgsm   = zgsm';
        nvxgsm     = vxgsm';
        nvygsm     = vygsm';
        nvzgsm     = vzgsm';
        nvtherm = vtherm';
        nionden = ionden';
        nMtime  = Mtime';
        nEW_ang = EW_ang';
        nNS_ang = NS_ang';
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Save first value of the next month then clear arrays to save mem
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        disp('Saving first value of next month');
	    tempMatime = Mtime(ii-1);
        tempmon = mon(ii-1);
        tempday = day(ii-1);
        temphr  = hr(ii-1);
        tempyear = year(ii-1);
        tempmin = min(ii-1);
        tempsec = sec(ii-1);
        tempmsec = msec(ii-1);
	    tempvth    = vtherm(ii-1);
	    tempden    = ionden(ii-1);
	    tempvx     = vxgsm(ii-1);
	    tempvy     = vygsm(ii-1);
	    tempvz     = vzgsm(ii-1);
	    tempxgsm   = xgsm(ii-1);
	    tempygsm   = ygsm(ii-1);
	    tempzgsm   = zgsm(ii-1);
        tempew_ang = EW_ang(ii-1);
        tempns_ang = NS_ang(ii-1);
        disp('Clearing entered arrays and interpolated arrays.');
        clear Mtime vtherm ionden vx vy vz xgsm ygsm zgsm NS_ang EW_ang hr min sec msec year mon day;
    
       disp('Degap and interpolate running');
       inarray = [nMtime,nxgsm,nygsm,nzgsm,nvxgsm,nvygsm,nvzgsm,nvtherm,nionden,nEW_ang,nNS_ang];
       %keyboard
       outarray=degapint(inarray,1.0e25,datenum('00:01:00'),startend,datenum('00:10:00'));
       [maxval,lll] = max(outarray(:,1));
    %keyboard
        nMtime   = outarray(:,1);
        nxgsm    = outarray(:,2);
        nygsm    = outarray(:,3);
        nzgsm    = outarray(:,4);
        nvxgsm      = outarray(:,5);
        nvygsm      = outarray(:,6);
        nvzgsm      = outarray(:,7);
        nvtherm  = outarray(:,8);
        nionden  = outarray(:,9);
        nEW_ang  = outarray(:,10);
        nNS_ang  = outarray(:,11);

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % read out shitty ascii file
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	    ctout = 1;
	    disp('Reading out values to file.');
	    while((ctout) <= lll),%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %while( ((ctout) <= 100) & (error ~= 0)),
             [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
		     count = fprintf(fidout,'%2.2d %2.2d %4.4d %2.2d %2.2d %06.3f %12.8e %12.8e %12.8e %12.8e %12.8e %12.8e %12.8e %12.8e\n',...
                        newday(ctout),newmonth(ctout),newyear(ctout),newhr(ctout),newmin(ctout),newsec(ctout),...
		        nvtherm(ctout),nionden(ctout),nxgsm(ctout),nygsm(ctout),nzgsm(ctout),...
                        nvxgsm(ctout),nvygsm(ctout),nvzgsm(ctout));
             atxt = ['length: ',num2str(lll),' ctout: ',num2str(ctout),' HHMMSS: ',num2str(newhr(ctout)),' ',num2str(newmin(ctout)),' ',num2str(newsec(ctout)) ];
		     disp(atxt);
             ctout = ctout+1;
        end   
	    disp('Closing output file.');
	    status = fclose (fidout);	
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % creating a mat file
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        disp('Creating MAT file.');
        %if (error ~= 0),
	        save(ssouttwo,'nMtime','nvtherm','nionden','nxgsm','nygsm','nzgsm','nvxgsm','nvygsm','nvzgsm');
        %end
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % plot out raw and interpolated to check
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	    %figure;
	    %plot(Mtime,vx,'b',NewMtime,nvxgsm,'k*');

    
        disp('Clearing interpolated arrays.');
        clear newyear newday newmonth newhr newmin newsec newvxgsm newvygsm newvzgsm newxgsm newygsm newzgsm;
    
	    Mtime(1)  = tempMatime;
        year(1) = tempyear;
        mon(1) = tempmon;
        day(1) = tempday;
        hr(1) = temphr;
        min(1) = tempmin;
        sec(1) = tempsec;
        msec(1) = tempmsec;
	    vtherm(1) = tempvth;
	    ionden(1) = tempden;
	    bxgsm(1)  = tempvx;
	    bygsm(1)  = tempvy;
	    bzgsm(1)  = tempvz;
	    xgsm(1)   = tempxgsm;
	    ygsm(1)   = tempygsm;
	    zgsm(1)   = tempzgsm;
        EW_ang(1) = tempew_ang;
        NS_ang(1) = tempns_ang;
	    ii = 1;
	    ct = 1;
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %% end of the big while loop loop on the months
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  end
    disp('Closing input file. DONE!!!!!!!!!!!!!!!!!!');
	status = fclose (fid);
%%end
