function IOPs = RPFG_invert(rrs,wavelength,N);
%
% function IOPs = RP(%_invert(rrs);
% input is RRS(0-) at X wavelengths...outputs are N retrieval params
% N=1, output is chl, acdm(443), bbp(443) with Scdm=0.016 Sbbp=-1
% N=2, output is chl, acdom(443),anap(443), bbp(443) with Scdom=0.02, Snap=0.01, Sbbp = -1
% N=3, output is chl, acdom(443),anap(443), bbpl(443), bbps(443) with % Scdom=0.02, % Snap=0.01, Sbbps/l = -1/0
% R(nspec,nlambda)
% chl, acdom(440), anap(443) and bbpl(440) bbps(443)
%
disp('----------MAKE SURE RRS IS BELOW AIR-WATER INTERFACE--------------')
global rrs_obs wave
wave=wavelength;
options = optimset('fminsearch');
options = optimset(options,'tolx',1e-6,'tolfun',1e-6,'MaxFunEvals',4000,'MaxIter',4000);

%%%%%% FOR EACH SPECTRUM, INVERT USING RP95 TO GIVE IOP's

for i = 1:size(rrs,1), i
   rrs_obs = rrs(i,:)';
   %[X cost(i)] = fminsearch('RP95_cost',[1 1 1 .01 .01]); %
   if N==1,[X cost(i)] = fminsearch('RPFG_cost',[1 1 1 1 1 .01]); 
   elseif N==2, [X cost(i)] = fminsearch('RPFG_cost',[1 1 1 1 1 1 .01]); 
   elseif N==3, [X cost(i)] = fminsearch('RPFG_cost',[1 1 1 1 1 1 .01 .01]);
   end 
   IOPs(i,:) = X;
end


