hurricane='fran';
threshold=0.5;


rep='wavelength analysis';
files=dir(['C:\Documents and Settings\sylvie lorsolo\My Documents\research\',hurricane,'\data\',rep,'\*.mat']); 
for fil=2%:length(files)
    load(['C:\Documents and Settings\sylvie lorsolo\My Documents\research\',hurricane,'\data\',rep,'\',files(fil).name]);
    [li,col]=size(lambda(1).z_data);
    
    for i=1:length(lambda)
        p_to_p_pos=[];
        p_to_p_neg=[];
        for j=1:col
            data=lambda(i).z_data(:,j);
            u_NaN=find(~isnan(data));
            if length(u_NaN)<40 %%%%%%40 for frances,25for isabel
                data(u_NaN)=NaN;
            end
%             data_copy=data;
%             u_tr=find(data < 3 & data > -3);
%             data(u_tr)=NaN;
            
            x=lambda(i).plan_axis2(1,:);
                        
            
            
            maxi=[];
            mini=[];
            extrema=[];
            amplit=[];
            for k=2:length(data)-1
                if data(k)>data(k+1) & data(k)> data(k-1)
                    maxi=[maxi;x(k)];
                    extrema=[extrema;x(k)];
                    amplit=[amplit;data(k)];
                elseif data(k)<data(k+1) & data(k)< data(k-1)
                    mini=[mini;x(k)];
                    extrema=[extrema;x(k)];
                    amplit=[amplit;data(k)];
%                 elseif isnan(data(k))==1 | isnan(data(k+1))==1 | isnan(data(k-1))==1
%                     maxi=[maxi;NaN];
%                     mini=[mini;NaN];
%                     extrema=[extrema;NaN];
%                     amplit=[amplit;NaN];
                end
                
            end
            
            
            %%%%%---------------------THERSHOLDING-------------%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%             
%                         diff_amplit=amplit(2:length(amplit))-amplit(1:length(amplit)-1);
%             
%                         u_threshold=find(abs(diff_amplit) < threshold);
%                         if length(u_threshold) > 1
%                             for ii=1:length(u_threshold)
%                                 u_maxi=find(maxi==extrema(u_threshold(ii)) | maxi==extrema(u_threshold(ii)+1));
%                                 maxi(u_maxi)=Inf;
%                                 u_mini=find(mini==extrema(u_threshold(ii)) | mini==extrema(u_threshold(ii)+1));
%                                 mini(u_mini)=Inf;
% %                                 u_data=find(data_copy==amplit(u_threshold(ii)) |  data_copy==amplit(u_threshold(ii)+1));
% %                                 data_copy(u_data)=NaN;
%                             end
%                         end
%                         u_Inf=find(isinf(maxi));
%                         maxi(u_Inf)=[];        %max(u_Inf)=[] for method 1 and maxi(u_Inf)=NaN; for method 2
%                         u_Inf=find(isinf(mini));
%                         mini(u_Inf)=[];
%                         %%%%%%-------------END THRESHOLDING-------------%%%
                        
                        
            if length(maxi)<=1
                maxi=[maxi;NaN;NaN];
            end
            if length(mini)<=1
                mini=[mini;NaN;NaN];
            end
            
            diff_p=maxi(2:length(maxi))-maxi(1:length(maxi)-1);
            diff_n=mini(2:length(mini))-mini(1:length(mini)-1);
            
            [m_pos,n_pos]=size(p_to_p_pos);
            [m_neg,n_neg]=size(p_to_p_neg);
            if length(diff_p) < m_pos
                add=abs(length(diff_p)-m_pos);
                add=NaN*ones(add,1);
                diff_p=[diff_p;add];
                p_to_p_pos=[p_to_p_pos diff_p];
                
                
            elseif length(diff_p) > m_pos
                add=abs(length(diff_p)-m_pos);
                add=NaN*ones(add,n_pos);
                p_to_p_pos=[p_to_p_pos;add];
                p_to_p_pos=[p_to_p_pos diff_p];
                
                
            elseif length(diff_p) == m_pos
                p_to_p_pos=[p_to_p_pos diff_p];
            end
            
            if length(diff_n) < m_neg
                add=abs(length(diff_n)-m_neg);
                add=NaN*ones(add,1);
                diff_n=[diff_n;add];
                p_to_p_neg=[p_to_p_neg diff_n];
                
                
            elseif length(diff_n) > m_neg
                add=abs(length(diff_n)-m_neg);
                add=NaN*ones(add,n_neg);
                p_to_p_neg=[p_to_p_neg;add];
                p_to_p_neg=[p_to_p_neg diff_n];
                
                
            elseif length(diff_n) == m_neg
                p_to_p_neg=[p_to_p_neg diff_n];
            end
            
end
        %total=[p_to_p_pos;p_to_p_neg];
        wave(i).p_to_p_pos=p_to_p_pos;
        wave(i).p_to_p_neg=p_to_p_neg;
        

        
    end
     %save(['C:\Documents and Settings\sylvie lorsolo\My Documents\research\',hurricane,'\data\',rep,'\extrem\unwrinkled\extrem_',files(fil).name(6:11),'.mat'],'wave');
    %save(['C:\Documents and Settings\sylvie lorsolo\My Documents\research\',hurricane,'\data\',rep,'\extrem\dewrinkled\',num2str(threshold),'\extrem_',files(fil).name(6:11),'.mat'],'wave');
        save(['C:\Documents and Settings\sylvie lorsolo\My Documents\research\',hurricane,'\data\',rep,'\extrem\ess2_',files(fil).name],'wave');

   % clear('wave');
end