function a = ProcessToZenBinsR(b)

	% The purpose of this script is to convert the calibrated reflectance data for the 
	% Polarization experiment to polarization parameter data in 12-degree bins that
	% are sampled every 2 degrees.  The polarization parameters include
	% I, Q, U, Percent Polarization, Chi Angle Plane of Polarization, and
	% Polarization Reflectance.
	% The bins are centered on -70, -68, -66, etc degrees.  For those dates which have
	% two scans of the view zenith angles, both scans are included in the output bins.

global 	gB1DataFlag...
		gB2DataFlag...
		gB3DataFlag...
		gB4DataFlag...
		gB5DataFlag...
		gB6DataFlag...
		gC1DataFlag...
		gC2DataFlag...
		gC3DataFlag...
		gB7NPDataFlag...
		gCNPDataFlag;
		
global 	gB1Data...
		gB2Data...
		gB3Data...
		gB4Data...
		gB5Data...
		gB6Data...
		gC1Data...
		gC2Data...
		gC3Data...
		gB7NPData...
		gCNPData;
		
global 	gB1InvStokesM...
	 	gB2InvStokesM...
	 	gB3InvStokesM...
	 	gB4InvStokesM...
	 	gB5InvStokesM...
	 	gB6InvStokesM...
	 	gC1InvStokesM...
	 	gC2InvStokesM...
	 	gC3InvStokesM;
		
global 	gAverageCimelFlag;
		
Date = 910712;
inputFileName = '916403_0712';
outputFileName = 'm0712.BinOutputt';
stopObsNumber = 10000;

InitInvStokesM(Date);

gB1DataFlag = 1;
gB2DataFlag = 1;
gB3DataFlag = 1;
gB4DataFlag = 1;
gB5DataFlag = 1;
gB6DataFlag = 1;
gC1DataFlag = 1;
gC2DataFlag = 1;
gC3DataFlag = 1;
gB7NPDataFlag = 1;
gCNPDataFlag = 1;

gB1Data = zeros(87,6);
gB2Data = zeros(87,6);
gB3Data = zeros(87,6);
gB4Data = zeros(87,6);
gB5Data = zeros(87,6);
gB6Data = zeros(87,6);
gC1Data = zeros(87,6);
gC2Data = zeros(87,6);
gC3Data = zeros(87,6);
gB7NPData = zeros(87,3);
gCNPData = zeros(87,3);

count12DegAveObs = zeros(87,1);
count2DegAveObs = zeros(87,1);

angle12DegBins = zeros(87,1);
angle2DegBins = zeros(87,1);

obs = zeros(67,1);

viewAzimuthM = [360 0];
viewAzimuthP = [360 0];
solarZenith = [360 0];
solarAzimuth = [360 0];
relAzimuthM = [360 -360];
relAzimuthP = [360 -360];

scene = ['Sunflowers';'Sky       '];
type = 1;

rfid = fopen(inputFileName);
wfid = fopen(outputFileName, 'w');

if rfid == -1
	return,
	end;
	
if wfid == -1
	return,
	end;

		% Get Reformating Date.

reformatDate = GetReformatDate(rfid);
if reformatDate == [],
	return,
	end;

		% Write 'date' header line.
		
count = fprintf(wfid, '%s\n', date);

		% Write input data header line.
		
count = fprintf(wfid, 'Input file = %s, Reformatting date = %d\n',...
					inputFileName, reformatDate);

		% Write headers for data columns.
		
count = fprintf(wfid, ...
	'ViewZ\tNumMObs\tNumCObs\tAveMAng\tAveCAng\t\t' );
count = fprintf(wfid, ...
	'B1I\tB1Q\tB1U\tB1P\tB1A\tB1Rp\t\t' );
count = fprintf(wfid, ...
	'B2I\tB2Q\tB2U\tB2P\tB2A\tB2Rp\t\t' );
count = fprintf(wfid, ...
	'B3I\tB3Q\tB3U\tB3P\tB3A\tB3Rp\t\t' );
count = fprintf(wfid, ...
	'B4I\tB4Q\tB4U\tB4P\tB4A\tB4Rp\t\t' );
count = fprintf(wfid, ...
	'B5I\tB5Q\tB5U\tB5P\tB5A\tB5Rp\t\t' );
count = fprintf(wfid, ...
	'B6I\tB6Q\tB6U\tB6P\tB6A\tB6Rp\t\t' );
count = fprintf(wfid, ...
	'C1I\tC1Q\tC1U\tC1P\tC1A\tC1Rp\t\t' );
count = fprintf(wfid, ...
	'C2I\tC2Q\tC2U\tC2P\tC2A\tC2Rp\t\t' );
count = fprintf(wfid, ...
	'C3I\tC3Q\tC3U\tC3P\tC3A\tC3Rp\t\t' );
count = fprintf(wfid, ...
	'B7R1\tB7R2\tB7R3\tC1R\tC2R\tC3R\n' );

lastCode = -101;
obsNumber = 1;
lineReady = 0;
endOfAngleSet = 0;
firstObservation = 1;
skipFlag = 0;

set = 0;

while obsNumber ~= 0

	if lineReady == 0
	
			% Read the next line in to be processed.
			
		obs = GetObservation(rfid);
		
		if obs == [],
			obsNumber
			obsNumber = 0;
			firstObservation = 0;
			
		elseif obs(2) < 80
	
				% Adjust relative view azimuth for different base for
				% Sunflower observations.  Sky observations do not
				% need to be adjusted.
			
			obs(4) = rem(obs(4)+180, 360);
				
		end;	% if obs == [], else...
		
		end;	% if lineReady == 0
		
	if firstObservation == 1
		
			% Determine if this is the start of a new set
			
		if obs(2) == 51 | (lastCode < 80 & obs(2) > 80)
			set = set + 1;
			set
			end;
			
		lastViewAzimuth = obs(4);
		lastCode = obs(2);
		
			% Determine whether set is for Sunflowers or Sky
			
		type = 1;
		crossBin = 39;
		angleLimit = 76;
		numberBins = 77;
		if obs(2) >= 81
			type = 2;
			crossBin = 44;
			angleLimit = 86;
			numberBins = 87;
			end;
		
			% Determine whether rep is one or two
			
		rep = 1;
		if obs(2) >= 57 & obs(2) <= 62
			rep = 2;
			end;
			
		firstObservation = 0;
		
		end;	% if firstObservation == 1
	
	if obsNumber ~= 0
	
			% Determine if this is the end of the angle set.
			
		if lastCode ~= obs(2)
			lineReady = 1;
			endOfAngleSet = 1;
			
		else	% lastCode == code(2)
			
				% Get the view - sun azimuth difference.
				% Set up so that difference reflects whether the
				% view azimuth is to the left or right of the sun
				% azimuth. Adjust so that it is between 0 and 360.
				
			obs(7) = rem(obs(4)-obs(6)+360, 360);
			
				% Convert the View Zenith angle to a +- angle
				% base on the view azimuth.
			
			if type == 1	
				plusMinusViewZenith = abs( obs(3) );
				
			else	% type == 2
				plusMinusViewZenith = abs( obs(3) - 180 );
					
			end;	% if type == 1, else...
			
			azimuthDiff = abs(obs(4) - lastViewAzimuth);
			
			if (azimuthDiff > 177) & (azimuthDiff < 183)
				plusMinusViewZenith = -plusMinusViewZenith;
				end;	% if (azimuthDiff > 177) & ...
	
				% Find view zenith bin for the Cimel data.
				
			bin = fix( (numberBins + plusMinusViewZenith)/2 ) + 1;

				% Find view zenith bins for the MMR data.
				% i.e. allow averaging.
				% The following is for an effective FOV of 12 degrees
				
			binLAngle = 2 * fix(plusMinusViewZenith/2);
			
			if binLAngle == plusMinusViewZenith
				binUAngle = binLAngle;
				
			else	% binLAngle ~= plusMinusViewZenith
				if plusMinusViewZenith < 0
					binUAngle = binLAngle;
					binLAngle = binUAngle - 2;
				
				else	% plusMinusViewZenith >= 0
					binUAngle = binLAngle + 2;
					
				end;	% if plusMinusViewZenith < 0, else...
					
			end;	% if binLAngle..., else
				
			binL = fix( (numberBins + binUAngle - 6)/2 ) + 1;
			binU = fix( (numberBins + binLAngle + 6)/2 ) + 1;
			
			binL = max( [ 1 binL ] );
			binU = min( [ numberBins binU ] );
				
		end;	% if lastCode ~= code(2), else...
		
	else	% obsNumber == 0
			% The end of the input data has been found.  Process the last
			% set of bins.
		
		endOfAngleSet = 1;
		
	end;	% if obsNumber ~= 0, else...
		
	if (endOfAngleSet == 0)
				
			% Check if out of range.
			
		if bin <= 0 | bin > numberBins
			skipFlag = 1;
			end;
		
		if skipFlag == 0
				% Add the data to the correct bin for all requested bands.
			
			AddAllDataToBinsR(bin, binL, binU, obs);

			count2DegAveObs(bin) = count2DegAveObs(bin) + 1;
			angle2DegBins(bin) = angle2DegBins(bin) + plusMinusViewZenith;
			
			for i=binL:binU
				count12DegAveObs(i) = count12DegAveObs(i) + 1;
				angle12DegBins(i) = angle12DegBins(i) + plusMinusViewZenith;
				end;
			
			solarZenith(1) = min(solarZenith(1), obs(5));
			solarZenith(2) = max(solarZenith(2), obs(5));
			
			solarAzimuth(1) = min(solarAzimuth(1), obs(6));
			solarAzimuth(2) = max(solarAzimuth(2), obs(6));
			
			if bin < crossBin
				viewAzimuthM(1) = min(viewAzimuthM(1), obs(4));
				viewAzimuthM(2) = max(viewAzimuthM(2), obs(4));
			
				relAzimuthM(1) = min(relAzimuthM(1), obs(7));
				relAzimuthM(2) = max(relAzimuthM(2), obs(7));
				
			elseif bin > crossBin
				viewAzimuthP(1) = min(viewAzimuthP(1), obs(4));
				viewAzimuthP(2) = max(viewAzimuthP(2), obs(4));
			
				relAzimuthP(1) = min(relAzimuthP(1), obs(7));
				relAzimuthP(2) = max(relAzimuthP(2), obs(7));
				
			end;	% if bin < crossBin, elseif...
		
			obsNumber = obsNumber + 1;
			
		else	% skipFlag ~= 0
			bin
			plusMinusViewZenith
			obsNumber
			obs(1)
			skipFlag = 0;
			
		end;	% if skipFlag == 0, else...
		
			% Indicate that the last line read has been processed.
			
		lineReady = 0;
		
	else	% endOfAngleSet == 1
			% Compute the averages for each available bin.
		
		ComputeAllBinDataR(count2DegAveObs, count12DegAveObs, numberBins);
		angle2DegBins(1:numberBins) = ...
						angle2DegBins(1:numberBins)./count2DegAveObs(1:numberBins);
		angle12DegBins(1:numberBins) = ...
						angle12DegBins(1:numberBins)./count12DegAveObs(1:numberBins);
		
		if relAzimuthP(1) < 360
				% Write the data for this set to the disk file
				% Write the header line for 'plus' zenith.
			
			count = fprintf(wfid, ...
				'Set %d\tRep %d\tCode %d\tScene: %s\tRelative Azimuth: %d to %d\tSolar Zenith: %d to %d\t',...
				set, rep, lastCode, scene(type,:), relAzimuthP, solarZenith );
				
			count = fprintf(wfid, ...
				'Solar Azimuth: %d to %d\tView Azimuth: %d to %d\n',...
				solarAzimuth, viewAzimuthP );
		
			index = crossBin;
			for angle=0:2:angleLimit
				count = fprintf(wfid, ...
					'%d\t%d\t%d\t%4.1f\t%4.1f\t\t',...
					angle, count12DegAveObs(index), count2DegAveObs(index),...
					angle12DegBins(index), angle2DegBins(index) );
				
				WriteAllDataToDisk(index, wfid);
		
				count = fprintf(wfid, '\n');
			
				index = index + 1;
	
				end;	% for angle=0:2:angleLimit
				
			end;	% if relAzimuthP(1) < 360
		
		if relAzimuthM(1) < 360
		
				% Write the data for this set to the disk file
				% Write the header line for 'minus' zenith.
			
			count = fprintf(wfid, ...
				'Set %d\tRep %d\tCode %d\tScene: %s\tRelative Azimuth: %d to %d\tSolar Zenith: %d to %d\t',...
				set, rep, lastCode, scene(type,:), relAzimuthM, solarZenith );
				
			count = fprintf(wfid, ...
				'Solar Azimuth: %d to %d\tView Azimuth: %d to %d\n',...
				solarAzimuth, viewAzimuthM );
		
			index = crossBin;
			for angle=0:2:angleLimit
				if angle12DegBins(index) <= 0
					angle12DegBins(index) = abs( angle12DegBins(index) );
				
				else	% angle12DegBins(index) > 0
					angle12DegBins(index) = -angle12DegBins(index);
				
				end;	% end if angle12DegBins(index) <= 0, else...
				
				if angle2DegBins(index) <= 0
					angle2DegBins(index) = abs( angle2DegBins(index) );
				
				else	% angle2DegBins(index) > 0
					angle2DegBins(index) = -angle2DegBins(index);
				
				end;	% end if angle2DegBins(index) <= 0, else...
				
				count = fprintf(wfid, ...
					'%d\t%d\t%d\t%4.1f\t%4.1f\t\t',...
					angle, count12DegAveObs(index), count2DegAveObs(index),...
					angle12DegBins(index), angle2DegBins(index) );
				
				WriteAllDataToDisk(index, wfid);
		
				count = fprintf(wfid, '\n');
			
				index = index - 1;
	
				end;	% for angle=0:2:angleLimit
				
			end;	% if relAzimuthM(1) < 360
			
			% Initialize values for the next set.
			
		if obsNumber ~= 0	
			lastViewAzimuth = obs(4);
			end;
			
		viewAzimuthP = [360 0];
		viewAzimuthM = [360 0];
		solarZenith = [360 0];
		solarAzimuth = [360 0];
		relAzimuthP = [360 -360];
		relAzimuthM = [360 -360];

		gB1Data = zeros(87,6);
		gB2Data = zeros(87,6);
		gB3Data = zeros(87,6);
		gB4Data = zeros(87,6);
		gB5Data = zeros(87,6);
		gB6Data = zeros(87,6);
		gC1Data = zeros(87,6);
		gC2Data = zeros(87,6);
		gC3Data = zeros(87,6);
		gB7NPData = zeros(87,3);
		gCNPData = zeros(87,3);
		
		count12DegAveObs = zeros(87,1);
		count2DegAveObs = zeros(87,1);

		angle12DegBins = zeros(87,1);
		angle2DegBins = zeros(87,1);
	
		endOfAngleSet = 0;
		firstObservation = 1;
	
	end;	% if endOfAngleSet == 0, else...
		
	if (obsNumber > stopObsNumber)
		break;
		end;
		
	if (obsNumber == 0)
		break;
		end;
	
	if rem(obsNumber,10) == 0
		obs(1)
		end;

	end;	% while obsNumber ~= 0
	
fclose(rfid);
fclose(wfid);
