function format_imp_mag_0(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
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	% line = fgetl(fid);	
	% get the first line and construct the start Matlab time.
	yyymmdd = fscanf(fid,'%s',1);
        hhmmss = fscanf(fid,'%s',1);
	yrstr = yyymmdd(1:4);
	daystr=yyymmdd(10:11);
	monstr = yyymmdd(6:8);
        yyymmddhhmmss = [daystr,'-',monstr,'-',yrstr,' ',hhmmss];1
        Mtime(ii) = datenum(yyymmddhhmmss);

	% get the input data
        bxgsm(ii) = fscanf(fid,'%f',1);
        bygsm(ii) = fscanf(fid,'%f',1);
        bzgsm(ii) = fscanf(fid,'%f',1);
        dummy = fscanf(fid,'%f',1);
        xgsm(ii) = fscanf(fid,'%f',1);
        ygsm(ii) = fscanf(fid,'%f',1);
        zgsm(ii) = fscanf(fid,'%f',1);
        dummy = fscanf(fid,'%f',1);
        dummy = fscanf(fid,'%f',1);
        dummy = fscanf(fid,'%f',1);
        dummy = fscanf(fid,'%f',1);
        ii = ii+1;

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % start month loop  (Used to enter multiple months)
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  while( (where <= 12) & (feof(fid) == 0) ),		% do all months or until end of file
	monthdoing = monstr;
        outputtxt = [' Working on month: ', monthdoing];
        disp(outputtxt);
        % determine output file name
	where = strmatch(monstr,months);
        ssout = ['imp8mag',yyyy(3:4),num2str(where,'%2.2d'),'.dat'];
        ssouttwo = ['imp8mag',yyyy(3:4),num2str(where,'%2.2d'),'.mat'];
	outputtxt = [' Output file: ', ssout];
        disp(outputtxt);
        % open output file
        [fidout,message] = fopen(ssout,'w');
        if fidout == -1
                disp(message);
        end
        %% starthhmmss needs to be changed to 00:00:00.000
	startmin = str2num(hhmmss(4:5)) + 1;
	starthhmmss = strcat(hhmmss(1:3),num2str(startmin),':00');
	startyyymmddhhmmss = [daystr,'-',monstr,'-',yrstr,' ',starthhmmss];
	NewMtime(ct) =datenum(startyyymmddhhmmss);
	[newyear(ct),newmonth(ct),newday(ct),newhr(ct),newmin(ct),newsec(ct)] = datevec(NewMtime(ct));
    %for ii=2:9,
   	while( (monstr == monthdoing) & (feof(fid) == 0) ),
		[yyymmdd,error]       = fscanf(fid,'%s',1);
        if (error == 0),		% must doe for EOF
                break;
        end
	% construct the matlab time
        hhmmss        = fscanf(fid,'%s',1);
	yrstr         = yyymmdd(1:4);
        daystr        = yyymmdd(10:11);
        monstr        = yyymmdd(6:8);
        yyymmddhhmmss = [daystr,'-',monstr,'-',yrstr,' ',hhmmss];
	Mtime(ii) = datenum(yyymmddhhmmss);

	% get the data
		bxgsm(ii) = fscanf(fid,'%f',1);
		bygsm(ii) = fscanf(fid,'%f',1);
		bzgsm(ii) = fscanf(fid,'%f',1);
		dummy     = fscanf(fid,'%f',1);
		xgsm(ii)  = fscanf(fid,'%f',1);
		ygsm(ii)  = fscanf(fid,'%f',1);
		zgsm(ii)  = fscanf(fid,'%f',1);
		dummy     = fscanf(fid,'%f',1);
		dummy     = fscanf(fid,'%f',1);
		dummy     = fscanf(fid,'%f',1);
		dummy     = fscanf(fid,'%f',1);
		ii        = ii + 1;
		% Check the input
		outputtxt = ['MM DD HH MM SS: ',monstr,' ',daystr,' ',hhmmss,' Sample: ',num2str(ii),' Bxgsm: ',num2str(bxgsm(ii-1)) ];
		disp(outputtxt);
   	end		% end of the while loop for monstr
        %keyboard
	disp('Exited while loop. ');
	endMtime = max(Mtime);
	

	disp('Linearly interpolating to value on the minute (Resampling). ');
        startend(1) = NewMtime(1);
        lyear = num2str(yrstr);		% 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(where)),'-',monthdoing,'-',yrstr,' ','23:59:00.00'];
        else
             endyyymmddhhmmss = [num2str(daysofmonths(where)),'-',monthdoing,'-',yrstr,' ','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];

	% save the last entry of the file for the next month.
	% this never happens now.
        tempMatime = Mtime(ii-1);
        tempbxgsm = bxgsm(ii-1);
        tempbygsm = bygsm(ii-1);
        tempbzgsm = bzgsm(ii-1);
        tempxgsm = xgsm(ii-1);
        tempygsm = ygsm(ii-1);
        tempzgsm = zgsm(ii-1);
	%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)]);
   
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Check the month (but pointless now).
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if (monstr ~= monthdoing),
		monthdoing = monstr;
    end

    % 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;
    Mtime(1)= tempMatime;
    bxgsm(1)= tempbxgsm;
    bygsm(1)= tempbygsm;
    bzgsm(1)= tempbzgsm;
    xgsm(1) = tempxgsm;
    ygsm(1) = tempygsm;
    zgsm(1) = tempzgsm;
    ii      = 1;        % reset counters
    ct      = 1;        % reset counters
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % end of while loop for the months (never do more than one month these days.
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  end	% end of while loop for the months (never do more than one month these days.

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