function a = process(b)

	% The purpose of this script is to convert the calibrated reflectance data for the 
	% Polarization experiment to single observation polarization parameter data
	% including I, Q, U, Percent Polarization, Chi Angle Plane of Polarization, and
	% Polarization Reflectance.
		
global 	gB1InvStokesM...
	 	gB2InvStokesM...
	 	gB3InvStokesM...
	 	gB4InvStokesM...
	 	gB5InvStokesM...
	 	gB6InvStokesM...
	 	gC1InvStokesM...
	 	gC2InvStokesM...
	 	gC3InvStokesM;
		
global 	gAverageCimelFlag;

Date = 910712;
inputFileName = '916403_0712';
outputFileName = 'm0712.Outputt';
lastObservation = 10000;

InitInvStokesM(Date);

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, ...
	'Obnu\tViewZ\tViewA\tIllumZ\tIllumA\tRelAz\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' );

firstObservation = 1;
lastCode = -101;
lastViewAzimuth = 33.6;
set = 0;

for loop = 1:lastObservation
	
		% Read the next line in to be processed.
		
	obs = GetObservation(rfid);

	if obs == [],
		break,
		end;
	
		% Adjust relative view azimuth for different base for
		% Sunflower observations.  Sky observations do not
		% need to be adjusted.
	
	if obs(2) < 80
		obs(4) = rem(obs(4)+180, 360);
		end;
	
	if loop ~= 1
	
			% Determine if this is the beginning of an angle set.
			
		if lastCode ~= obs(2)
			firstObservation = 1;
			end;
		
		end;
		
	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;
			end;
			
		lastViewAzimuth = obs(4);
		lastCode = obs(2);
		
			% Determine whether set is for Sunflowers or Sky
			
		type = 1;
		if obs(2) >= 81
			type = 2;
			end;
		
			% Determine whether rep is one or two
			
		rep = 1;
		if obs(2) >= 57 & obs(2) <= 62
			rep = 2;
			end;
			
		firstObservation = 0;
		
			% Write the header line for the set and rep.
			
		count = fprintf(wfid, ...
				'Set %d\tRep %d\tCode %d\tScene: %s\n', ...
				set, rep, obs(2), scene(type,:) );
		
		end;	% if firstObservation == 1
			
		% Convert the View Zenith angle to a +- angle
		% based on the view azimuth.
%		
%	plusMinusViewZenith = abs( obs(3) );
%	
%	azimuthDiff = abs(obs(4) - lastViewAzimuth);
%	
%	if (azimuthDiff > 177) & (azimuthDiff < 183)
%		plusMinusViewZenith = -plusMinusViewZenith;
%		end;	% if (azimuthDiff > 177) & ...

		% 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 -180 and 180.
		% Negative values indicate that the sensor is to the left
		% of the sun, Positive indicates that the sensor is to
		% the right of the sun.
	
	obs(7) = rem(obs(4)-obs(6)+360, 360);
	if obs(7) > 180
		obs(7) = obs(7) - 360;
		end;

	B1StokesV = gB1InvStokesM * [obs(26); obs(27); obs(28)];
	B1P = DegOfPolarization(B1StokesV);
	B1Chi = AngOfPolarizedPlane(B1StokesV);
	B1PRefl = B1P * B1StokesV(1) / 100;
	
	B2StokesV = gB2InvStokesM * [obs( 8); obs( 9); obs( 10)];
	B2P = DegOfPolarization(B2StokesV);
	B2Chi = AngOfPolarizedPlane(B2StokesV);
	B2PRefl = B2P * B2StokesV(1) / 100;
	
	B3StokesV = gB3InvStokesM * [obs(22); obs(23); obs(24)];
	B3P = DegOfPolarization(B3StokesV);
	B3Chi = AngOfPolarizedPlane(B3StokesV);
	B3PRefl = B3P * B3StokesV(1) / 100;
	
	B4StokesV = gB4InvStokesM * [obs(15); obs(16); obs(17)];
	B4P = DegOfPolarization(B4StokesV);
	B4Chi = AngOfPolarizedPlane(B4StokesV);
	B4PRefl = B4P * B4StokesV(1) / 100;
	
	B5StokesV = gB5InvStokesM * [obs(19); obs(20); obs(21)];
	B5P = DegOfPolarization(B5StokesV);
	B5Chi = AngOfPolarizedPlane(B5StokesV);
	B5PRefl = B5P * B5StokesV(1) / 100;
	
	B6StokesV = gB6InvStokesM * [obs(12); obs(13); obs(14)];
	B6P = DegOfPolarization(B6StokesV);
	B6Chi = AngOfPolarizedPlane(B6StokesV);
	B6PRefl = B6P * B6StokesV(1) / 100;

	C1StokesV = gC1InvStokesM * [obs(38); obs(39); obs(40)];
	C1P = DegOfPolarization(C1StokesV);
	C1Chi = AngOfPolarizedPlane(C1StokesV);
	C1PRefl = C1P * C1StokesV(1) / 100;
	
	C2StokesV = gC2InvStokesM * [obs(32); obs(33); obs(34)];
	C2P = DegOfPolarization(C2StokesV);
	C2Chi = AngOfPolarizedPlane(C2StokesV);
	C2PRefl = C2P * C2StokesV(1) / 100;
	
	C3StokesV = gC3InvStokesM * [obs(35); obs(36); obs(37)];
	C3P = DegOfPolarization(C3StokesV);
	C3Chi = AngOfPolarizedPlane(C3StokesV);
	C3PRefl = C3P * C3StokesV(1) / 100;
	
	count = fprintf(wfid, ...
				'%d\t%6.1f\t%d\t%d\t%d\t%d\t\t',...
				obs(1), obs(3:7) );
	
	count = fprintf(wfid, ...
				'%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t\t',...
				B1StokesV, B1P, B1Chi, B1PRefl, ...
				B2StokesV, B2P, B2Chi, B2PRefl, ...
				B3StokesV, B3P, B3Chi, B3PRefl, ...
				B4StokesV, B4P, B4Chi, B4PRefl, ...
				B5StokesV, B5P, B5Chi, B5PRefl, ...
				B6StokesV, B6P, B6Chi, B6PRefl, ...
				C1StokesV, C1P, C1Chi, C1PRefl, ...
				C2StokesV, C2P, C2Chi, C2PRefl, ...
				C3StokesV, C3P, C3Chi, C3PRefl );
	
	count = fprintf(wfid, ...
				'%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\n',...
				obs(11), obs(18), obs(25), ...
				obs(29), obs(30), obs(31) );
	
	if rem(loop,10) == 0
		loop
		end;
		
	obsNumber = obsNumber + 1;
	
	end

fclose(rfid);
fclose(wfid);
loop

a = loop;
