function eval_2D_energy_scales(lon,lat,FLD,MAX_WID,PL)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  	This function evaluates the "energy" levels linked with different spatial scales 
%  
%  				lon - 2D field
%  				lat - 2D field		
%				FLD - field to be evaluated (units XX)
%  				MAX_WID - maximum scale to be evaluated
%  				PL - plot "spectrum" if = 1		
%  
%  				Ricardo Domingues, AOML/NOAA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
DX = nanmean(diff(lon(1,:)));
WID = 1:2:round(MAX_WID/DX);
[MM,NN] = size(lon);
PERCENT_ENERGY = nan(1,length(WID));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Loops from large scale toward small scales


FLD0 = FLD - nanmean(FLD(:));
MASK = isnan(FLD0);
FLD0(MASK) = nan;

TOTAL_SIGNAL = nansum(abs(FLD0(:)));

FLD_new = FLD0;
FLD_cumul = zeros(MM,NN);

SIGNAL_aux = SIGNAL
for i=1:length(WID);
i
%  	figure
%  	imagesc(FLD_new), colorbar; 
%  	caxis([-60 60])


	FLD_aux = rmean_2D(FLD_new,WID(i));
	FLD_aux(MASK) = nan;

	FLD_eval = FLD_new - FLD_aux;
	FLD_eval(MASK) = nan;

	FLD_cumul = FLD_cumul + FLD_eval; 

%  	figure
%  	imagesc(FLD_aux), colorbar;
%  	caxis([-60 60])

	PERCENT_ENERGY(i) = (nansum(abs(FLD_eval(:)))./TOTAL_SIGNAL)*100;	
	FLD_new = FLD_aux;
	FLD_aux = FLD_aux - nanmean(FLD_aux(:));

%  	figure
%  	imagesc(FLD_eval), colorbar;
%  	caxis([-60 60]) 

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
SCALES = WID.*DX;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

figure
plot(SCALES,PERCENT_ENERGY);
xlabel('\bf Degrees');


nansum(PERCENT_ENERGY)






