function format_imp_plasma_6(flag)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function format_imp_plasma_5
% author: J. Weygand
% creation: Mar 1, 2003
% purpose: format the Wind plasma files
% into matlab time format thermal speed, 
% density vx, vy, vz, (GSM)
% X, Y, Z (GSM RE)
% NOTE:  this is for file type number 2
% which has no vx, vy, vz in gse or gsm.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Given 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
format long e;							% use the long format
months        = strvcat('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
daysofmonths  = [31 28 31 30 31 30 31 31 30 31 30 31];		% number of days per months, normal year
daysofmonths2 = [31 29 31 30 31 30 31 31 30 31 30 31];		% number of days per months, leap year.
doyn          = [1,32,60,91,121,152,182,213,244,274,305,335];	% day of year, normal year
doyl          = [1,32,61,92,122,153,183,214,245,275,306,336];	% day of year, leap year.
k_b           = 1.380622e-23;					% boltzmans const
m_p           = 1.672e-27;					% mass of a proton.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load in data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yy=flag;							% Get year and month
outputtxt = [' Working on year/mm: ', num2str(yy)];		% Double check
	disp(outputtxt);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Determine file name and open input file.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yyyy=num2str(yy);						% Convert year/month to string
ssone = ['wind_swe_2m',yyyy,'.asc'];				% Get file name
outputtxt = [' Input file: ', ssone];				% Check you did it correctly
	disp(outputtxt);		
[fid,message] = fopen(ssone,'r');				% Error test
if fid == -1
	disp(message);
	keyboard;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Enter the first few lines of the input file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
alldata = fscanf(fid,['%d %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f \n'],[53,inf]);
    				% get all the data at once. This saves time.
if (length(alldata)==0), 	% if file empty, then make flag file
	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+34;
	ygsm(1:2) = 1.0e+34;
	zgsm(1:2) = 1.0e+34;
	vxgse(1:2) = 1.0e+34;
	vygse(1:2) = 1.0e+34;
	vzgse(1:2) = 1.0e+34;
	temp(1:2) = 1.0e+34;
	density(1:2) = 1.0e+34;
else				% if files has data sort the data
	yrs  = alldata(1,:);	% Location determined by getting ascii
	doy  = alldata(2,:);	% file header
	xgsm = alldata(49,:);	
	ygsm = alldata(52,:);
	zgsm = alldata(53,:);
	vxgse = alldata(5,:);
	vygse = alldata(7,:);
	vzgse = alldata(9,:);
	temp = alldata(32,:);
	density = alldata(21,:);
end
len  = length(yrs);				% Length of the array

disp(' Getting Days and Months from DOY!'); 	% Bob wants YYYY MM DD, but we have yyyy doy
Mtime = datenum(yrs,1,doy,0,0,0);		% construct Matlab time.
[dummy,mmons,dummy,dummy,dummy,dummy]= datevec(Mtime(1));
clear alldata;					% free some RAM
flag=1.0e+34;					% Flag Bob wants (discussed GEM 03)
index=find((xgsm>=99999.9) | (ygsm>=99999.9) | (zgsm>=99999.9) | (vxgse>=99999.9) | (vygse>=99999.9) | (vzgse>=99999.9) | (density>=99999.9) );
%% eliminate the flags for these. Normally there are very few missing.
%% Removing the flag ones helps prevent corruption of the data on the
%% edges of the flag zones. A 1e34 flag "pulls up" the data on the ends
%% of the flag zones and makes that data worthless. By removing a few
%% flags now it helps to save 3 to 4 data points on each side of the
%% flag. The edges are "pulled up" by the degap program. This program
%% removes bad times, but not bad data.
xgsm(index) = [];
ygsm(index) = [];
zgsm(index) = [];
vxgse(index) = [];
vygse(index) = [];
vzgse(index) = [];
density(index)  = [];
temp(index) = [];
Mtime(index) = [];
len = length(Mtime);
% change the flag. The flag is changed for the temp and not the others
% because there is so many flags for the temp but very little for the
% the others. if vxgse is missing then so usually is vygse and vzgse.
% Denisity is also rarely missing. Temperature has normally 10000's of
% flags so it seemed worth while to sacrifice data points on the ends
% inorder to keep more data in the flaged temp regions.
index=find(temp>=99999.9);			%% Change their flag value
temp(index)=repmat(flag,1,length(index));        
	
monthdoing = mmons(1);				% Check again: What month are we really doing
outputtxt = [' Working on month: ', num2str(monthdoing)];	
	disp(outputtxt);			
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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    = ['windplasma',num2str(yrs(1)),num2str(monthdoing,'%2.2d'),'.dat'];	% Contruct file names
ssouttwo = ['windplasma',num2str(yrs(1)),num2str(monthdoing,'%2.2d'),'.mat'];	% Contruct file names
outputtxt = [' Output file: ', ssout];
	disp(outputtxt);

[fidout,message] = fopen(ssout,'w');			% open output file
if fidout == -1						% error check
	disp(message);
	keyboard;
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);		% Determine start time of month and make string
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);		% Determine end time of month and make string
							% degap requires a specifiv input
ntemp = temp';						% turn row into column
ndensity = density';
nMtime = Mtime';
nvxgsm = vxgsm';
nvygsm = vygsm';
nvzgsm = vzgsm';
nxgsm  = xgsm';						% xgse = xgsm
nygsm  = ygsm';
nzgsm  = zgsm';
inarray = [nMtime,ntemp,ndensity,nvxgsm,nvygsm,nvzgsm,nxgsm,nygsm,nzgsm];

clear temp density vxgse vygse vzgse;			%free up some RAM.
clear vygsm vzgsm xgsm ygsm zgsm;			%free up some RAM.

% run the degap program which also interpolates to the minute
outarray=degapint(inarray,1.0e+34,datenum('00:01:00'),startend,datenum('00:10:00'));
[maxval,lll] = max(outarray(:,1));			% Degap done get new size.
nMtime = outarray(:,1);					% sort out data.
ntemp  = outarray(:,2);					% I understand it better this way.
ndensity = outarray(:,3);
nvxgsm = outarray(:,4);
nvygsm = outarray(:,5);
nvzgsm = outarray(:,6);
nxgsm  = outarray(:,7);
nygsm  = outarray(:,8);
nzgsm  = outarray(:,9);

% Here we are removing spikes in the data. Originally this was
% associated with the "pull" up edges arounded the originally flagged
% regions, but now it just removes spikes.
is = find((nvxgsm > 5000.0)|(nvxgsm<-1300.0));
nvxgsm(is) = 1e+34;
is = find((nvygsm > 6000.0)|(nvygsm<-6000.0));
nvygsm(is) = 1e+34;
is = find((nvzgsm > 6000.0)|(nvzgsm<-6000.0));
nvzgsm(is) = 1e+34;
is = find((ntemp > 3000.0)|(ntemp < 0.0));
ntemp(is) = 1e+34;
is = find((ndensity > 200.0)|(ndensity <= 0.0));
ndensity(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;
    
clear outarray;		%free up some RAM.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot out to check
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure;						% make fig window
plot(Mtime,vxgsm,'b',nMtime,nvxgsm,'k*');	% plot original and interpolated 
datetick('x','dd/mm/yy');			% show date in dd/mm/yy form
if (min(vxgsm) == max(vxgsm)),
	ylim([0,1]);				% set limits to avoid plotting flags
else
	ylim([-1000,0]);			% set limits to avoid plotting flags
end
%keyboard
clear Mtime vxgsm;				% free memory
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% read out to an ascii file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ctout = 1;				% set flag
disp('Fixing rounding error in Time.');
while( (ctout) <= lll),
	% 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 of array: ',num2str(lll),' Position at: ',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;				% Set counter
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
disp('Closing ascii output file.');
status = fclose (fidout);	
    
% read out to matlab binary file.
disp('Making 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
disp('Program complete');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% end of program
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%end 
