function kinetic_intensity
% exctraction automatique d'une cinetique a partir des spectres
% d'un dossier, sur intensitintégrée
% normalisation automatique / nb de scan et amplitude de modulation
% zoom possible sur spectres experimentaux
% exp 2D peuvent etre traitees
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% temps pour un spectre = milieu de l'acquisition %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% EE juin 2016
% EE sept 2020: spectre derive visible, selection spectre sur courbe
% cinetique, bug 1D corrige 
%EE jan 2021 bug  time si plusieurs scan corrige
%EE june2021  pas de menu
%EE sept2021 possible sur plusieurs jours, semaines, mois, annees
%sauvegarde figure en tif plutot que bmp
%EE jan2022 bug selection sur cinetique et time(1) non nul pour 1D
%EE janv 2022: error saving 2D fixed
%EE mar2024: time unit choice
%EE avr2024: loading spectra from magnettech (xml avec compression des donnees (1024 points/spectre)) + chargement données pas
%seulement dans current directory + Easyspin 6.0.0 (rescale=> rescaledata)
%EE oct2025 compatibilite avec Matlab2025

clear;close;clc;

%linetype={'Derivative';'Positive';'Negative'}; 
%linetypeval=1; %default
timeunit={'[s]';'[min]';'[h]'};

polydeginit=3; %degre du polynome pour ligne de base

%version easyspin
try
    easyinfo=easyspin; %seult a partir de  easyspin 6.0.0
    easyversion=str2double(easyinfo.Version(1));
catch 
    easyversion=5; %avant easyspin 6.0.0
end

%handle d'initialisation
versionyear=version('-date');
versionyear=str2double(versionyear(end-4:end)); %annee version matlab
if versionyear>=2014
        hinit=gobjects; %handle=structure
else
        hinit=NaN; %handle=double
end

if versionyear>=2025
    tickNaN=[];
else
    tickNaN=NaN;
end

hkincurve=hinit; %handle courbe cinetique
hcurrent=hinit; %handle du point rouge du point de la courbe  correspondant au spectre courant
hLF=hinit; %handle rectangle bruit bas champ
hHF=hinit; %handle rectangle bruit haut champ
%hsig=hinit; %handle rectangle signal
%h_linesup=hinit; %handle ligne superieure
%h_lineinf=hinit; %handle ligne inferieure
%hlinetype=hinit; %handle popup menu
hspec=hinit; %handle courbe absorption
hspec0=hinit; %handle spectre exp
hprimcorr=hinit; %handle courbe abs corrigee
hbaseline=hinit; %handle ligne de base

tdelay=0; %init delay (s)
data1Dloaded=[];%init

disp('The time associated to a spectrum is the time when its mid point is recorded.');

%% chargement des donnees
reload=false; %par defaut nouveau chargement

choice = questdlg('What kind of data do you want to load?','kinetic: data type','Existing kinetic','From Bruker','From Magnettech','Existing kinetic');

if isempty(choice) %si cancel
    return
else
    switch choice
        case 'Existing kinetic'
            [file2load,chemin]=uigetfile('*.mat','Select kinetic to Open');
            if isnumeric(file2load) % cancel;
                return
            else
                load(fullfile(chemin,file2load));
                reload=true;
                
                nbspec=numel(data);
                if ~isduration([data.time]) %sauvegarde avec vieille version
                    for kk=1:nbspec
                        tempt=seconds(data(kk).time);
                        data(kk).time=duration(tempt,'format','dd:hh:mm:ss'); %type duration!!!
                    end
                end
                disp([file2load,' loaded.']);
            end
            
        case 'From Bruker'
            choiceB=questdlg('What kind of Bruker data do you want to load?','kinetic: Bruker','2D','1D','2D');
            if isempty(choiceB) %si cancel
                return
            end
            switch choiceB
                case '2D'
                    [file2load,chemin]=uigetfile('*.YGF','Select Bruker 2D file to Open');
                    if isnumeric(file2load) % cancel;
                        return
                    else
                        disp(['Load ',file2load]);
                        data=load2D(fullfile(chemin,file2load));
                        nbspec=numel(data);
                        data1Dloaded=0;%2D
                    end
                case '1D'
                    [file2load_all,chemin]=uigetfile('*.DSC','Select Bruker 1D files to Open','MultiSelect','on');
                    if isnumeric(file2load_all) % cancel;
                        return
                    else
                        dir_structYGF=dir(fullfile(chemin,'*.YGF')); % structure contenant les noms de fichier.YGF (exp 2D)
                        nb2D=numel(dir_structYGF);
                        nbDSC=numel(file2load_all);
                        indDSC=1:nbDSC;
                        if nb2D>0 %suppression des DSC relatifs à 2D si selectionnes
                            ind2supr=[]; %init des indices de file2load a supprimer car YGF
                          for k2D=1:nb2D
                              nameDSC2supr=dir_structYGF(k2D).name;
                              nameDSC2supr=[nameDSC2supr(1:end-3),'DSC'];
                              nn = find(strcmp(file2load_all,nameDSC2supr));
                              if ~isempty(nn)
                                 disp(['The file ',nameDSC2supr,' is gnored because part of a 2D experiment.']); 
                                 ind2supr=[ind2supr,nn];
                              end
                          end
                          if ~isempty(ind2supr)
                            indDSC(ind2supr)=[];
                            file2load=file2load_all(indDSC);
                          else
                            file2load=file2load_all;
                          end
                        else
                          file2load=file2load_all;
                        end
                        nbspec=numel(file2load);
                        data=load1D;
                        data1Dloaded=1;%1D
                    end
            end
            
         case 'From Magnettech'
            [file2load,chemin]=uigetfile('*.xml','Select Magnettech files to Open','MultiSelect','on');
            if isnumeric(file2load) % cancel;
                return
            else
                nbspec=numel(file2load);
                data=load1Dmagnettech;
                data1Dloaded=1;%1D
            end 
    end     
end

if exist('data','var')
    %%%sauvegarde smooth avec ancienne version kinetic data.specbrut (smooth) en data.secbrut2(smooth)
    if isfield(data,'specbrut')
        for kk=1:size(data,2)
            data(kk).specbrut2=data(kk).specbrut;
        end
        data=rmfield(data,'specbrut');
        denoisingvalue=2;
        nbptsmooth=str2double(nbptsmooth);%ancienne version string sauvegardee
    end

if not(isempty(data)) %no cancel;

%% figure 
hsatmain=figure(1);
scrszpix = get(0,'ScreenSize'); %taille ecran [left, bottom, width, height] en pixels
%scrszcm=scrszpix/get(0,'screenpixelsperinch')*2.54; %1inch=2.54cm  taille ecran en cm

set(hsatmain,'Units','pixels','Outerposition',...    
    [scrszpix(3)/3 0.2*scrszpix(4) 1000 scrszpix(4)*0.8],...
    'Name','Kinetic_intensity','Menubar', 'none', 'Toolbar', 'none',...%'resize','off',...
    'NumberTitle','off',...'DoubleBuffer','on',...'CloseRequestFcn',@closeSimLabel,...
    'color',get(0,'DefaultUicontrolBackgroundColor'));

%         %%%% pour ne garder que les outils zoomin, zoomout et main
% hToolbar = findall(hsatmain,'tag','FigureToolBar'); %handle toolbar
% halltoolbar=allchild(hToolbar);%tous les handles de la toolbar
% hzoomin=findall(halltoolbar,'TooltipString','Zoom In');
% hzoomout=findall(halltoolbar,'TooltipString','Zoom Out');
% hhand=findall(halltoolbar,'TooltipString','Pan');
% halltoolbar(halltoolbar==hzoomin)=[];%supprime 1 handle a conserver
% halltoolbar(halltoolbar==hzoomout)=[];%supprime 1 handle a conserver
% halltoolbar(halltoolbar==hhand)=[];%supprime 1 handle a conserver
% set(halltoolbar,'visible','off')

%%% graphes spectres exp %%%
hspecaxes=axes('parent',hsatmain,'Position',[0.01 0.33 0.58 0.64],...
                'Box','on');
% hslidsup=uicontrol('Style','slider','Units','Normalized','pos',[0.595 0.84 0.02 0.13],'BackgroundColor',[0.9 0.9 0.9],...
%     'parent',hsatmain,'String',num2str(0),'Callback', @slidsup,'TooltipString','Adjust up line for amplitude extraction');%'fontunits','pixels','fontsize',ftsz,...
%    % 'Min',-20,'Max',+20);%,'Value',0,'SliderStep',[0.1/40 1/40]);
%    
% hslidinf=uicontrol('Style','slider','Units','Normalized','pos',[0.595 0.33 0.02 0.13],'BackgroundColor',[0.9 0.9 0.9],...
%     'parent',hsatmain,'String',num2str(0),'Callback', @slidinf,'TooltipString','Adjust bottom line for amplitude extraction');
% 


%%% graphes courbe de saturation => cinetique %%%
hsataxes=axes('parent',hsatmain,'Position',[0.64 0.5 0.35 0.47],...
                'Ytick',tickNaN,'Box','on');%,'Xtick',tickNaN);
htimeunit=uicontrol(...
        'Parent',hsatmain,...
        'FontUnits','pixels',...
        'FontSize',13,...
        'BackgroundColor','w',...
        'Units','normalized',...
        'Position',[0.84 0.32 0.07 0.15],...[0.835 0.315 0.05 0.15]
        'String',timeunit,...
        'Value',1,...
        'Callback',@dispsat,...
        'Style','popupmenu');%,... %

   
%%% liste des spectres experimentaux
% hlist=uicontrol('Style','listbox','Units','Normalized','pos',[0.64 0.01 0.35 0.38],...
%     'parent',hsatmain,'max',1,'min',0,'Callback',{@dispexp,'new'},'fontunits','pixels',...'fontsize',ftsz,...
%     'backgroundcolor','w','string',{data(:).name},'value',1,'fontsize',13);
% hpath=uicontrol('Style','text','Units','Normalized','pos',[0.64 0.40 0.35 0.02],...
%     'parent',hsatmain,'String',chemin,'HorizontalAlignment','center');

hlist=uicontrol('Style','listbox','Units','Normalized','pos',[0.64 0.01 0.35 0.38],...
    'parent',hsatmain,'max',1,'min',0,'Callback',{@dispexp,'new'},'fontunits','pixels',...'fontsize',ftsz,...
    'backgroundcolor','w','string',{data(:).name},'value',1,'fontsize',13);

if ~isempty(data1Dloaded) %nouvelles dnnees
    if data1Dloaded
        out=regexp(chemin,filesep,'split');%pour separer les disques et dossiers du chemin et n'afficher que le dernier
        str2disp=['Directory of 1D data = ',out{end-1}];
    else
        str2disp=['2D data = ',file2load];
    end
else %anciennes donnees rechargees
    %out=regexp(chemin,filesep,'split');
    str2disp=['Previously saved data = ',file2load];
end

out=regexp(chemin,filesep,'split');%pour separer les disques et dossiers du chemin et n'afficher que le dernier
hpath=uicontrol('Style','text','Units','Normalized','pos',[0.64 0.40 0.35 0.02],...
    'parent',hsatmain,'String',str2disp,'HorizontalAlignment','center','tooltipstring',chemin);

        
%%% panel baseline (noise dans kinetic)
noisestrLF=sprintf('Range of magnetic field for baseline extraction (low field).');
noisestrHF=sprintf('Range of magnetic field for baseline extraction (high field).');
hpanelnoise = uipanel('Parent',hsatmain,'FontWeight','bold','Title','Baseline',...
        'Position',[0.02 0.12 0.61 0.15],'Units','Normalized',...
        'FontUnits','pixels','FontSize',16,'FontWeight','bold');
    %low field
uicontrol('Style','text','Units','Normalized','pos',[0 0.7 0.2 0.25],...'BackgroundColor','w',....
    'parent',hpanelnoise,'String','Low Field: from','HorizontalAlignment','right','fontsize',11);
hB1min=uicontrol('Parent',hpanelnoise,...
        'Units','normalized',...
        'FontUnits','pixels',...
        'BackgroundColor','w',...
        'Callback',@dispnoiserange,...
        'FontSize',13,'foregroundcolor','b',...
        'Position',[0.21 0.69 0.15 0.21],...
        'Style','edit',...
    'TooltipString',noisestrLF);
uicontrol('Style','text','Units','Normalized','pos',[0.37 0.7 0.1 0.25],...'BackgroundColor','w',....
    'parent',hpanelnoise,'String','mT to','HorizontalAlignment','center','fontsize',11);    
hB1max=uicontrol('Parent',hpanelnoise,...
        'Units','normalized',...
        'FontUnits','pixels',...
        'BackgroundColor','w',...
        'Callback',@dispnoiserange,...
        'FontSize',13,'foregroundcolor','b',...
        'Position',[0.48 0.69 0.15 0.21],...
        'Style','edit',...
        'TooltipString',noisestrLF);
uicontrol('Style','text','Units','Normalized','pos',[0.64 0.7 0.05 0.25],...'BackgroundColor','w',....
    'parent',hpanelnoise,'String','mT','HorizontalAlignment','left','fontsize',11);    

    %high field
uicontrol('Style','text','Units','Normalized','pos',[0. 0.25 0.2 0.25],...'BackgroundColor','w',....
    'parent',hpanelnoise,'String','High Field: from','HorizontalAlignment','right','fontsize',11);
hB2min=uicontrol('Parent',hpanelnoise,...
        'Units','normalized',...
        'FontUnits','pixels',...
        'BackgroundColor','w',...
        'Callback',@dispnoiserange,...
        'FontSize',13,'foregroundcolor','b',...
        'Position',[0.21 0.24 0.15 0.21],...
        'Style','edit',...
    'TooltipString',noisestrHF);
uicontrol('Style','text','Units','Normalized','pos',[0.37 0.25 0.1 0.25],...'BackgroundColor','w',....
    'parent',hpanelnoise,'String','mT to','HorizontalAlignment','center','fontsize',11);    
hB2max=uicontrol('Parent',hpanelnoise,...
        'Units','normalized',...
        'FontUnits','pixels',...
        'BackgroundColor','w',...
        'Callback',@dispnoiserange,...
        'FontSize',13,'foregroundcolor','b',...
        'Position',[0.48 0.24 0.15 0.21],...
        'Style','edit',...
        'TooltipString',noisestrHF);
uicontrol('Style','text','Units','Normalized','pos',[0.64 0.25 0.1 0.25],...'BackgroundColor','w',....
    'parent',hpanelnoise,'String','mT','HorizontalAlignment','left','fontsize',11);  


% %% panel signal
% hpanelsignal = uipanel('Parent',hsatmain,'FontWeight','bold','Title','Baseline degree',...
%         'Position',[0.38 0.12 0.25 0.15],'Units','Normalized',...
%         'FontUnits','pixels','FontSize',16,'FontWeight','bold');

uicontrol('Style','text','Units','Normalized','pos',[0.73 0.67 0.15 0.25],...'BackgroundColor','w',....
    'parent',hpanelnoise,'String','degree =','HorizontalAlignment','right','fontsize',11,'tooltipstring','Degree of the polynomial baseline');
hdegree=uicontrol('Parent',hpanelnoise,...
        'Units','normalized',...
        'FontUnits','pixels',...
        'BackgroundColor','w',...
        'Callback',@setpolydeg,...
        'FontSize',13,'foregroundcolor','b',...
        'Position',[0.89 0.67 0.1 0.25],...
        'Style','edit','tooltipstring','Degree of the polynomial baseline',...
        'string',num2str(polydeginit));
    
    
 hchkintarea=uicontrol('Style','checkbox','String','Reduce int. area','Units','Normalized','HorizontalAlignment','center',...
    'pos',[0.75 0.1 0.25 0.2],'parent',hpanelnoise,'Callback', @integration_area,'value',1,...
    'fontunits','pixels','fontsize',12,'tooltipstring','If checked, the 2nd integration occurs between low field maximum and high field minimum.');



% uicontrol('Style','text','Units','Normalized','pos',[0.49 0.7 0.2 0.2],...'BackgroundColor','w',....
%     'parent',hpanelsignal,'String','mT to','HorizontalAlignment','center','fontsize',11);    
% hBsmax=uicontrol('Parent',hpanelsignal,...
%         'Units','normalized',...
%         'FontUnits','pixels',...
%         'BackgroundColor','w',...
%         'Callback',@dispnoiserange,...
%         'FontSize',13,'foregroundcolor',[0 0.7 0],...
%         'Position',[0.69 0.69 0.15 0.21],...
%         'Style','edit');
% uicontrol('Style','text','Units','Normalized','pos',[0.84 0.7 0.1 0.2],...'BackgroundColor','w',....
%     'parent',hpanelsignal,'String','mT','HorizontalAlignment','center','fontsize',11);    


% hlinetype=uicontrol(...
%         'Parent',hpanelsignal,...
%         'FontUnits','pixels',...
%         'FontSize',13,...
%         'BackgroundColor','w',...
%         'Units','normalized',...
%         'Callback',@setlinetype,...
%         'Position',[0.2 0.25 0.35 0.15],...
%         'String',linetype,...
%         'Style','popupmenu',...
%         'Value',1);
% 
% uicontrol('Style','text','Units','Normalized','pos',[0.55 0.19 0.1 0.2],...'BackgroundColor','w',....
%     'parent',hpanelsignal,'String','line','HorizontalAlignment','center','fontsize',11); 


uicontrol('Parent',hsatmain,...
        'Units','normalized',...
        'FontUnits','pixels',...
        'BackgroundColor','w',...
        'Callback',@considerdelay,...
        'FontSize',13,...
        'Position',[0.44 0.06 0.06 0.03],...
        'Style','edit',...
        'String',num2str(tdelay),...
    'TooltipString','Delay between experiment and first record (s)');

uicontrol('Style','text','Units','Normalized','pos',[0.39 0.06 0.05 0.03],...'BackgroundColor','w',....
    'parent',hsatmain,'String','delay=','HorizontalAlignment','center','fontsize',11,...
    'TooltipString','Delay between experiment and start of first record');    
uicontrol('Style','text','Units','Normalized','pos',[0.5 0.06 0.02 0.03],...'BackgroundColor','w',....
    'parent',hsatmain,'String','s','HorizontalAlignment','center','fontsize',11,...
    'TooltipString','Delay between experiment and start of first record (s)'); 

uicontrol('Style','pushbutton','String','Send to all spectra','Units','Normalized','HorizontalAlignment','center',...
    'pos',[0.02 0.05 0.1 0.05],'parent',hsatmain,'Callback', @sent,...
    'fontunits','pixels','fontsize',11);

uicontrol('Style','pushbutton','String','Save Kinetic','Units','Normalized','HorizontalAlignment','center',...
    'pos',[0.2 0.05 0.1 0.05],'parent',hsatmain,'Callback', @savesat,...
    'fontunits','pixels','fontsize',11);

hchkder=uicontrol('Style','checkbox','String','Show derivative line','Units','Normalized','HorizontalAlignment','center',...
    'pos',[0.39 0.01 0.2 0.05],'parent',hsatmain,'Callback', @dispderivative,'value',1,...
    'fontunits','pixels','fontsize',12);

%%% + et -

uicontrol('Style', 'pushbutton', 'String', 'Down','Units','Normalized','Position', [0.59 0.01 0.05 0.05],...
    'parent',hsatmain,'Callback',{@plus_or_minus_fctn,'+'},'tooltipstring','Change spectrum');
%     uicontrol(plusbutton);%set(plusbutton,'selected','off'); %focus par défaut sur bouton +
uicontrol('Style', 'pushbutton', 'String', 'Up','Units','Normalized','Position', [0.59 0.06 0.05 0.05],...
    'parent',hsatmain,'Callback', {@plus_or_minus_fctn,'-'},'tooltipstring','Change spectrum');

%%%%%%%%%%%%%%% init %%%%%%%%%%%%%%%%
% if data(1).amp==0
%     alpha1initnum=max(data(1).spec)-min(data(1).spec);
% else
%     alpha1initnum=data(1).amp;
% end
% alpha1=log10(alpha1initnum/data(1).sqrtpuissance); %init facteur de propportionalite pour diag sur sat curve
% alpha2=0;
%     set(hslidinf,'value',alpha2);

dispexp(hlist,[],'new')

% set(hchkintarea,'value',1);
end
end
%%
function data1D=load1D()
    if nbspec>1
        for k=1:nbspec
            data1D(k).name=file2load{k};%dir_structDSC(k,1).name;%nom du fichier
            [B,spec,par]=eprload(fullfile(chemin,data1D(k).name));
            if iscell(B)
               error(['Kinetic is not able to deal with 1D Data present in ',chemin,'. Please, check 1D data compatibility.']);
            end
            data1D(k).B=B/10;%B en mT
            MA=par.B0MA;%amplitude de modulation (T)
            nbsc=par.AVGS;%nb de scan
            data1D(k).spec0=spec/MA/nbsc;%normalisation du signal par nb de scan et amplitude de modulation
            acqtime(k)=str2double(par.SweepTime(1:end-2))*nbsc; %s temps d'acquisition du spectre
          
            data1D(k).tempsh=datestr(datenum(par.TIME)-datenum(datestr(acqtime(k)/2/86400,'HH:MM:SS')),'HH:MM:SS');% heure d'enregistrement (hh:mm:ss): heure - 1/2 acquisition
            data1D(k).date=par.DATE;% date d'enregistrement (MM/DD/YY)

            data1D(k).timenum=datetime([data1D(k).date,' ',data1D(k).tempsh],'InputFormat','MM/dd/yy HH:mm:ss'); %date et heure associee au spectre version matlab
            
            data1D(k).B1min=data1D(k).B(1);%floor(data1D(k).B(1))+1;%plage de champ pour tenir compte du bruit (bas champ)
            data1D(k).B1max=data1D(k).B(1)+0.2*par.XWID/10;%floor(data1D(k).B(1))+1; 


            [data1D(k).imin1,data1D(k).imax1]=getindex(data1D(k).B,[data1D(k).B1min data1D(k).B1max]); %indice pour plage bas champ 

            data1D(k).B2min=data1D(k).B(end)-0.2*par.XWID/10;%floor(data1D(k).B(end));%plage de champ pour tenir compte du bruit (haut champ)
            data1D(k).B2max=data1D(k).B(end);%floor(data1D(k).B(end));
            [data1D(k).imin2,data1D(k).imax2]=getindex(data1D(k).B,[data1D(k).B2min data1D(k).B2max]); %indice pour plage haut champ 

            data1D(k).polydeg=polydeginit;
            prim=cumtrapz(data1D(k).B,data1D(k).spec0);
            data1D(k).spec=prim;
            data1D(k).baseline=extractbaseline(data1D(k));%zeros(1,numel(data1D(k).spec));
            data1D(k).primcorr=prim-data1D(k).baseline;%zeros(numel(data1D(k).spec));%primitive corrigée
   
            data1D(k).amp=trapz(data1D(k).B(data1D(k).imax1:data1D(k).imin2),data1D(k).primcorr(data1D(k).imax1:data1D(k).imin2));

            
            %data1D(k).amp=trapz(data1D(k).B,data1D(k).primcorr);%data2D(k).ysup-data2D(k).yinf;

            
            
            clear B spec par MA nbsc
        end
        
        [~,order]=sort([data1D(:).timenum]); %ordonner selon temps croissants
        data1D=data1D(order);
        
        for k=1:nbspec
            data1D(k).time=duration(data1D(k).timenum-data1D(1).timenum,'format','dd:hh:mm:ss')+seconds(acqtime(1)/2); %type duration!!!      
        end
    else
                error('No data...');
    end

end

function data1D=load1Dmagnettech()

        for k=1:nbspec
            msg=fprintf(['loading and compressing data... (',num2str(k/nbspec*100,'%2.f'),'%%)']);pause(0.0001)
            data1D(k).name=file2load{k};%dir_structDSC(k,1).name;%nom du fichier
            [B,spec,par]=eprload(fullfile(chemin,data1D(k).name));
            if iscell(B)
               error(['Kinetic is not able to deal with 1D Data present in ',chemin,'. Please, check 1D data compatibility.']);
            end
            %%%%%% xml (>22000pts avec param standard d'enregistrement)=> interpolation à 1024 points
            if strcmp(data1D(k).name([end-3:end]),'.xml')
                
                B_=linspace(B(1),B(end),1024);
                spec_=interp1(B,spec,B_,'spline');
                clear B spec
                B=B_;
                spec=spec_;
            end


            data1D(k).B=B;%/10;%B en mT  deja en mT avec magnettech
            MA=par.Modulation;%amplitude de modulation (T)
            nbsc=par.Accumulations;%nb de scan  (normalisation faite par logiciel spectro)
            data1D(k).spec0=spec/MA;%/nbsc;%normalisation du signal par  amplitude de modulation
            
   
            acqtime(k)=par.SweepTime*nbsc; %s temps d'acquisition du spectre

            timestamp=par.Timestamp; %forme:'2024-04-03T18:12:58.5654129+02:00'  debut du scan
            data1D(k).date=timestamp(1:10);% date d'enregistrement (YY-MM-DD)
            timeinit=[timestamp(12:17),num2str(round(str2double(timestamp(18:25))))];% heure d'enregistrement (HH:MM:SS)
            data1D(k).tempsh=datestr(datetime(timeinit,'inputformat','HH:mm:ss')+seconds(acqtime(k)/2),'HH:MM:SS');
            data1D(k).timenum=datetime([data1D(k).date,' ',data1D(k).tempsh],'InputFormat','yy-MM-dd HH:mm:ss'); %date et heure associee au spectre version matlab

            
            Bwidth=B(end)-B(1);
             data1D(k).B1min=data1D(k).B(1);%floor(data1D(k).B(1))+1;%plage de champ pour tenir compte du bruit (bas champ)
            data1D(k).B1max=data1D(k).B(1)+0.2*Bwidth;%floor(data1D(k).B(1))+1; 
            [data1D(k).imin1,data1D(k).imax1]=getindex(data1D(k).B,[data1D(k).B1min data1D(k).B1max]); %indice pour plage bas champ 

            data1D(k).B2min=data1D(k).B(end)-0.2*Bwidth;%floor(data1D(k).B(end));%plage de champ pour tenir compte du bruit (haut champ)
            data1D(k).B2max=data1D(k).B(end);%floor(data1D(k).B(end));
            [data1D(k).imin2,data1D(k).imax2]=getindex(data1D(k).B,[data1D(k).B2min data1D(k).B2max]); %indice pour plage haut champ 

            
            data1D(k).polydeg=polydeginit;
            prim=cumtrapz(data1D(k).B,data1D(k).spec0);
            data1D(k).spec=prim;
            data1D(k).baseline=extractbaseline(data1D(k));%zeros(1,numel(data1D(k).spec));
            data1D(k).primcorr=prim-data1D(k).baseline;%zeros(numel(data1D(k).spec));%primitive corrigée
   
            data1D(k).amp=trapz(data1D(k).B(data1D(k).imax1:data1D(k).imin2),data1D(k).primcorr(data1D(k).imax1:data1D(k).imin2));

            
            clear B spec par MA nbsc 
            fprintf(repmat('\b', 1,msg)); %supprime la ligne 'loading...'
        end
        
        [~,order]=sort([data1D(:).timenum]); %ordonner selon temps croissants
        data1D=data1D(order);
        

        for k=1:nbspec
           
            data1D(k).time=duration(data1D(k).timenum-data1D(1).timenum,'format','dd:hh:mm:ss')+seconds(acqtime(1)/2); %type duration!!!
        
        end

end




%%
function data2D=load2D(file)
    [x,spec,par]=eprload([file(1:end-3),'DSC']); %chargement de l'experience 2D
    nbspc=size(spec,2); %nb de spectres a considerer
    starttime=x{1,2}; %s temps de debut d'acquisition de chaque spectre
    nbsc=par.AVGS;%nb de scan
    while nbsc==0 || isnan(nbsc)  %si manip arretee avant la fin AVGS=0
       nbsc=str2double(inputdlg(sprintf('Enter the scan number per spectrum:\n(Data not available, because stop before end of experiment)'),'',1,{'1'}));
    end
    acqtime=str2double(par.SweepTime(1:end-2)); %s temps d'acquisition d'un spectre
    delai=starttime(2)-starttime(1)-acqtime; %s delai entre 2 acquisitions
    MA=par.B0MA;%amplitude de modulation (T)
    
    n=1;
    for k=1:nbspc
        if ~isequal(spec(:,k),zeros(size(spec,1),1)) %si  exp 2D arretee avant la fin
            ind2keep(n)=k;
            n=n+1;
        end
    end
   
    for k=ind2keep 
        data2D(k).name=['spectre ',num2str(k)];
        data2D(k).B=x{1,1}/10; %mT champ magnetique experimental en gauss
        data2D(k).spec0=spec(:,k)/MA;%/nbsc normalisation du signal par amplitude de modulation
        timetemp=seconds(starttime(k)+acqtime/2);
        data2D(k).time=duration(timetemp,'format','dd:hh:mm:ss'); %type duration!!!

        data2D(k).tempsh=datestr(datenum(par.TIME)-datenum(datestr(((2*nbspc+1-2*k)/2*acqtime+(nbspc-k)*delai)/86400,'HH:MM:SS')),'HH:MM:SS');% heure d'enregistrement (hh:mm:ss)
        
        data2D(k).B1min=data2D(k).B(1);%floor(data2D(k).B(1))+1;%plage de champ pour tenir compte du bruit (bas champ)
        data2D(k).B1max=data2D(k).B(1)+0.2*par.XWID/10;%floor(data2D(k).B(1))+1;            
        [data2D(k).imin1,data2D(k).imax1]=getindex(data2D(k).B,[data2D(k).B1min data2D(k).B1max]); %indice pour plage bas champ 
        %data2D(k).B1max=data2D(k).B(data2D(k).imax1);
        
        data2D(k).B2min=data2D(k).B(end)-0.2*par.XWID/10;%floor(data2D(k).B(end));%plage de champ pour tenir compte du bruit (haut champ)
        data2D(k).B2max=data2D(k).B(end);%floor(data2D(k).B(end));
        [data2D(k).imin2,data2D(k).imax2]=getindex(data2D(k).B,[data2D(k).B2min data2D(k).B2max]); %indice pour plage haut champ 
        %data2D(k).B2min=data2D(k).B(data2D(k).imin2);
        
%         data2D(k).Bsmin=floor(data2D(k).B(1)+(data2D(k).B(end)-data2D(k).B(1))/4);%plage de champ du signal
%         data2D(k).Bsmax=floor(data2D(k).B(1)+3*(data2D(k).B(end)-data2D(k).B(1))/4);
%         [data2D(k).imins,data2D(k).imaxs]=getindex(data2D(k).B,[data2D(k).Bsmin data2D(k).Bsmax]); %indice pour plage du signal 

        %data2D(k).ysup=posline(data2D(k),'sup'); %ordonnee ligne superieure
        %data2D(k).yinf=posline(data2D(k),'inf'); %ordonnee ligne inferieure
        data2D(k).polydeg=polydeginit;
        prim=cumtrapz(data2D(k).B,data2D(k).spec0);
        data2D(k).spec=prim;
        data2D(k).baseline=extractbaseline(data2D(k));%zeros(1,numel(data2D(k).spec));
        data2D(k).primcorr=prim-data2D(k).baseline';
        data2D(k).amp=trapz(data2D(k).B(data2D(k).imax1:data2D(k).imin2),data2D(k).primcorr(data2D(k).imax1:data2D(k).imin2));
        %data2D(k).amp=trapz(data2D(k).B,data2D(k).primcorr);%data2D(k).ysup-data2D(k).yinf;
        

        
      
    end
    [~,order]=sort([data2D(:).time]); %ordonner selon temps croissants
    data2D=data2D(order);
    
end

function baseline=extractbaseline(data)
    x=data.B([data.imin1:data.imax1,data.imin2:data.imax2]);
    y=data.spec([data.imin1:data.imax1,data.imin2:data.imax2]);
    if ~isequal(size(x),size(y))
       x=x'; 
    end
    [pol,S,MU]=polyfit(x,y,data.polydeg);
    baseline=polyval(pol,data.B,S,MU);


end


function dispexp(hobject,~,status) %affichage spectre  
    rang=get(hobject,'value'); %numero du spectre dans la liste
    
    if strcmp(status,'new')
        h2del=get(hspecaxes,'children');
        delete(h2del);


        set(hB1min,'string',num2str(data(rang).B1min));
        set(hB1max,'string',num2str(data(rang).B1max));
        set(hB2min,'string',num2str(data(rang).B2min));
        set(hB2max,'string',num2str(data(rang).B2max));
        set(hdegree,'string',num2str(data(rang).polydeg));
        if ~isfield(data(rang),'integtype') || isempty(data(rang).integtype)
            data(rang).integtype=0;%anciennes donnees integration sur plage totale
        end
        
        set(hchkintarea,'value',data(rang).integtype)  
        
%         set(hBsmin,'string',num2str(data(rang).Bsmin));
%         set(hBsmax,'string',num2str(data(rang).Bsmax));
        %set(hlinetype,'value',data(rang).linetypeval);

        hold(hspecaxes,'on');

        
        
        hspec=plot(hspecaxes,data(rang).B,data(rang).spec,'k','linewidth',2);

        plot(hspecaxes,[data(rang).B(1),data(rang).B(end)],[0 0],'k','linewidth',0.75);hold(hspecaxes,'on');
        set(hspecaxes,'xlim',[data(rang).B(1) data(rang).B(end)],'Ytick',tickNaN,...
        'ylim',1.1*[min(data(rang).spec) max(data(rang).spec)]);
        xlabel(hspecaxes,'Magnetic Field [mT]');

        % correction ligne de base
        data(rang).baseline=extractbaseline(data(rang));
        x=data(rang).spec;
        y=data(rang).baseline;
        if ~isequal(size(x),size(y))
           x=x'; 
        end
        data(rang).primcorr=x-y;
        if get(hchkintarea,'value')
            imin_maxB1=data(rang).imax1;
            imax_minB2=data(rang).imin2;
        else
            imin_maxB1=1;
            imax_minB2=numel(data(rang).B);
        end
        
        data(rang).amp=trapz(data(rang).B(imin_maxB1:imax_minB2),data(rang).primcorr(imin_maxB1:imax_minB2));


        hprimcorr=plot(hspecaxes,data(rang).B,data(rang).primcorr,'m');hold(hspecaxes,'on');
        hbaseline=plot(hspecaxes,data(rang).B,data(rang).baseline,'b');hold(hspecaxes,'on');
        
        if get(hchkder,'value')
            dispy=get(hspecaxes,'ylim');
            if easyversion>=6 %easyspin 6.0.0 et ulterieur
                plot(hspecaxes,data(rang).B,0.4*(dispy(2)-dispy(1))+0.3*rescaledata(data(rang).spec0,data(rang).spec,'maxabs'),'color','w','linewidth',4);
                hspec0=plot(hspecaxes,data(rang).B,0.4*(dispy(2)-dispy(1))+0.3*rescaledata(data(rang).spec0,data(rang).spec,'maxabs'),'color',0.8*[1 1 1],'linewidth',2);

            else
               plot(hspecaxes,data(rang).B,0.4*(dispy(2)-dispy(1))+0.3*rescale(data(rang).spec0,data(rang).spec,'maxabs'),'color','w','linewidth',4);
               hspec0=plot(hspecaxes,data(rang).B,0.4*(dispy(2)-dispy(1))+0.3*rescale(data(rang).spec0,data(rang).spec,'maxabs'),'color',0.8*[1 1 1],'linewidth',2);
            end


        end
         
        if ishandle(hspec0)
            legend(hspecaxes,[hspec,hprimcorr,hbaseline,hspec0],'exp. absorp.','corr. absorp.','baseline','exp. deriv.')
        else
            legend(hspecaxes,[hspec,hprimcorr,hbaseline],'exp. absorp.','corr. absorp.','baseline')
        end

%         %rectangles pour bruit et signal
        hLF=disprect('B1',rang); %noise low field
        hHF=disprect('B2',rang); %noise high field
        
        

        time2disp=char(data(rang).time+seconds(tdelay));

        nb22points=numel(strfind(time2disp,':'));
        if nb22points==2 % hh:mm:ss
            title(hspecaxes,[time2disp,' (hh:mm:ss) / Amplitude = ',num2str(data(rang).amp,'%3.2e')]);
        elseif nb22points==3% dd:hh:mm:ss
           title(hspecaxes,[time2disp,' (dd:hh:mm:ss) / Amplitude = ',num2str(data(rang).amp,'%3.2e')]);
        end
            
%        h_lineinf=line([data(rang).B(imins) data(rang).B(imaxs)],data(rang).yinf*[1 1],'color','m','parent',hspecaxes);

    end

    dispsat() %affichage courbe de saturation => cinetique
end

function dispsat(~,~) %affichage courbe de saturation => cinetique
    if ishandle(hkincurve)
        
        delete(hkincurve)
    end
    if ishandle(hcurrent)
        delete(hcurrent)
    end
%     if ishandle(hdiag)
%         delete(hdiag)
%     end

    timeunitnum=get(htimeunit,'value'); %1:s / 2:min / 3:h
    rang=get(hlist,'value'); % point du spectre courant en rouge
    if timeunitnum==1
        time2plot=seconds([data(:).time])+tdelay;
        timecurrent=seconds(data(rang).time)+tdelay;
    end
    if timeunitnum==2
        time2plot=minutes([data(:).time])+tdelay/60;
        timecurrent=minutes(data(rang).time)+tdelay/60;
    end
    if timeunitnum==3
        time2plot=hours([data(:).time])+tdelay/3600;
        timecurrent=hours(data(rang).time)+tdelay/3600;
    end


    hkincurve=plot(time2plot,[data(:).amp],'ob','parent',hsataxes);hold on
    set(hkincurve,'ButtonDownFcn',@MouseleftclicksatCllbck);
    xlabel(hsataxes,'Time');
    ylabel(hsataxes,'EPR intensity [a.u.]');
    title(hsataxes,'Kinetic on Intensity');    
    % point du spectre courant en rouge
    rang=get(hlist,'value');
    hcurrent=plot(timecurrent,data(rang).amp,'ob','MarkerFaceColor','m','parent',hsataxes);
    

end

function [imin,imax]=getindex(B,rangeB) %recherche des indices de la plage de champ à considérer 
    i=1;

    while B(i)<rangeB(1)
        i=i+1;
    end
    imin=i;
    while B(i)<rangeB(2) && i<numel(B)
        i=i+1;

            
    end
    imax=i-1;
    if imax>numel(B)
        imax=numel(B);
    end
end

function dispnoiserange(~,~) %callback des edit des plages de champ pour bruit et signal
    if ishandle(hLF)
        delete(hLF)
    end
    if ishandle(hHF)
        delete(hHF)
    end
%     if ishandle(hsig)
%         delete(hsig)
%     end
%     if ishandle(h_linesup)
%         delete(h_linesup)
%     end
%     if ishandle(h_lineinf)
%         delete(h_lineinf)
%     end
    rang=get(hlist,'value');
    data(rang).B1min=str2double(get(hB1min,'string'));%plage de champ pour tenir compte du bruit (bas champ)
    data(rang).B1max=str2double(get(hB1max,'string'));
    [data(rang).imin1,data(rang).imax1]=getindex(data(rang).B,[data(rang).B1min data(rang).B1max]); %indice pour plage bas champ 

    data(rang).B2min=str2double(get(hB2min,'string'));%plage de champ pour tenir compte du bruit (haut champ)
    data(rang).B2max=str2double(get(hB2max,'string'));
    [data(rang).imin2,data(rang).imax2]=getindex(data(rang).B,[data(rang).B2min data(rang).B2max]); %indice pour plage haut champ 
   
%     data(rang).Bsmin=str2double(get(hBsmin,'string'));%plage de champ du signal
%     data(rang).Bsmax=str2double(get(hBsmax,'string'));
%    [data(rang).imins,data(rang).imaxs]=getindex(data(rang).B,[data(rang).Bsmin data(rang).Bsmax]); %indice pour plage du signal 

     %rectangles pour bruit et signal
    hLF=disprect('B1',rang); %noise low field
    hHF=disprect('B2',rang); %noise high field
%     hsig=disprect('Bs',rang); %signal


    %maj
    delete([hprimcorr,hbaseline]);
    data(rang).baseline=extractbaseline(data(rang));
        x=data(rang).spec;
        y=data(rang).baseline;
        if ~isequal(size(x),size(y))
           x=x'; 
        end
        data(rang).primcorr=x-y;
        if get(hchkintarea,'value')
            imin_maxB1=data(rang).imax1;
            imax_minB2=data(rang).imin2;
        else
            imin_maxB1=1;
            imax_minB2=numel(data(rang).B);
        end
        
        data(rang).amp=trapz(data(rang).B(imin_maxB1:imax_minB2),data(rang).primcorr(imin_maxB1:imax_minB2));
        hprimcorr=plot(hspecaxes,data(rang).B,data(rang).primcorr,'m');hold(hspecaxes,'on');
        hbaseline=plot(hspecaxes,data(rang).B,data(rang).baseline,'b');hold(hspecaxes,'on');
    
        if get(hchkder,'value')
            dispy=get(hspecaxes,'ylim');
            if easyversion>=6 %easyspin 6.0.0 et ulterieur
                plot(hspecaxes,data(rang).B,0.4*(dispy(2)-dispy(1))+0.3*rescaledata(data(rang).spec0,data(rang).spec,'maxabs'),'color','w','linewidth',4);
                hspec0=plot(hspecaxes,data(rang).B,0.4*(dispy(2)-dispy(1))+0.3*rescaledata(data(rang).spec0,data(rang).spec,'maxabs'),'color',0.8*[1 1 1],'linewidth',2);
            else
                plot(hspecaxes,data(rang).B,0.4*(dispy(2)-dispy(1))+0.3*rescale(data(rang).spec0,data(rang).spec,'maxabs'),'color','w','linewidth',4);
                hspec0=plot(hspecaxes,data(rang).B,0.4*(dispy(2)-dispy(1))+0.3*rescale(data(rang).spec0,data(rang).spec,'maxabs'),'color',0.8*[1 1 1],'linewidth',2);
            end
           
        end
       if ishandle(hspec0)
            legend(hspecaxes,[hspec,hprimcorr,hbaseline,hspec0],'exp. absorp.','corr. absorp.','baseline','exp. deriv.')
        else
            legend(hspecaxes,[hspec,hprimcorr,hbaseline],'exp. absorp.','corr. absorp.','baseline')
        end
    
    
    
    
    
%     data(rang).ysup=posline(data(rang),'sup'); %ordonnee ligne superieure
%     data(rang).yinf=posline(data(rang),'inf'); %ordonnee ligne inferieure
%     data(rang).amp=data(rang).ysup-data(rang).yinf;
        
    %lines pour extraction amplitude
%     [imins,imaxs]=getindex(data(rang).B,[data(rang).Bsmin data(rang).Bsmax]);
%     h_linesup=line([data(rang).B(imins) data(rang).B(imaxs)],data(rang).ysup*[1 1],'color','m','parent',hspecaxes);
%     h_lineinf=line([data(rang).B(imins) data(rang).B(imaxs)],data(rang).yinf*[1 1],'color','m','parent',hspecaxes);
%     % maj sliders
%     set(hslidsup,'value',data(rang).ysup);
%     set(hslidinf,'value',data(rang).yinf);
    
    
    dispsat() %affichage courbe de saturation => cinetique
end

function h=disprect(fieldB,rang) %affichage des rectangles pour visu des plages
    %[imin,imax]=getindex(data(rang).B,[data(rang).([fieldB,'min']) data(rang).([fieldB,'max'])]);
    
    rangey=get(hspecaxes,'ylim');
    
%     if imin==imax
%         amp=max(data(rang).spec)-min(data(rang).spec);
%         pos=[data(rang).([fieldB,'min']) -amp/10 0.01 amp/5];
%     else
       % amp=max(data(rang).spec(imin:imax))-min(data(rang).spec(imin:imax));
%         pos=[data(rang).([fieldB,'min']) min(data(rang).spec(imin:imax))...
%              data(rang).([fieldB,'max'])-data(rang).([fieldB,'min'])...
%              amp];
        pos=[data(rang).([fieldB,'min']) rangey(1)...
             data(rang).([fieldB,'max'])-data(rang).([fieldB,'min'])...
             rangey(2)-rangey(1)];
%    end
%     switch fieldB
%         case 'B1'
%             color='b';
%         case 'B2'
%             color='b';
%         case 'Bs'
%             color=[0 0.7 0];
%             graphe_lim=get(hspecaxes,'Ylim');
%             pos([2 4])= [0.95*graphe_lim(1) 0.95*(graphe_lim(2)-graphe_lim(1))];
        
%    end

    h=rectangle('parent',hspecaxes,'position',pos,'Edgecolor','w','facecolor',[0.8*[1 1 1],0.7]);%[187 210 255]/255);
    uistack(h,'bottom');
end

function y=posline(datatemp,arg) %ordonnee de  line arg='sup' ou 'inf'
%         amp_bruit1=NaN; %amplitude bruit bas champ
%         std1=NaN; %deviation standard bruit bas champ
%         amp_bruit2=NaN; %amplitude bruit haut champ
%         std2=NaN; %deviation standard bruit haut champ
%         if datatemp.B1min~=datatemp.B1max %noise a bas champ
%             maxbruit1=max(datatemp.spec(datatemp.imin1:datatemp.imax1));
%             minbruit1=min(datatemp.spec(datatemp.imin1:datatemp.imax1));
%             amp_bruit1=maxbruit1-minbruit1;
%             std1=std(datatemp.spec(datatemp.imin1:datatemp.imax1));
%         end
%         if datatemp.B2min~=datatemp.B2max %noise a haut champ
%             maxbruit2=max(datatemp.spec(datatemp.imin2:datatemp.imax2));
%             minbruit2=min(datatemp.spec(datatemp.imin2:datatemp.imax2));
%             amp_bruit2=maxbruit2-minbruit2;
%             std2=std(datatemp.spec(datatemp.imin2:datatemp.imax2));
%         end
%         if ishandle(hlinetype)
%             linetypeval=get(hlinetype,'value');
%         else 
%             linetypeval=1; %par defaut derivative line
%         end
%         switch arg
%             case 'sup' %ordonnee superieur
%                 if linetypeval==3 %raie negative
%                     y=0;
%                 else
%                     maxspec=max(datatemp.spec(datatemp.imins:datatemp.imaxs));
%                     % amplitude moyenne bruit
%                     amp_bruit_all=[amp_bruit1 amp_bruit2];
%                     amp_bruit_all(isnan(amp_bruit_all))=[]; %supprime les NaN
%                     amp_bruit=mean(amp_bruit_all); %NaN si isempty(amp_bruit)
%                     if isnan(amp_bruit) 
%                         amp_bruit=0;
%                     end
%                     % deviation standard moyenne bruit
%                     std_bruit_all=[std1 std2];
%                     std_bruit_all(isnan(std_bruit_all))=[]; %supprime les NaN
%                     std_bruit=mean(std_bruit_all); %NaN si isempty(std_bruit)
%                     if isnan(std_bruit)
%                         std_bruit=0;
%                     end
%                      y=maxspec-mean([amp_bruit/2 std_bruit]);
%                 end
%                 
%             case 'inf' %ordonnee inferieur
%                 if linetypeval==2 %raie positive
%                     y=0;
%                 else
%                     minspec=min(datatemp.spec(datatemp.imins:datatemp.imaxs));
%                     % amplitude moyenne bruit
%                     amp_bruit_all=[amp_bruit1 amp_bruit2];
%                     amp_bruit_all(isnan(amp_bruit_all))=[]; %supprime les NaN
%                     amp_bruit=mean(amp_bruit_all); %NaN si isempty(amp_bruit)
%                     if isnan(amp_bruit) 
%                         amp_bruit=0;
%                     end
%                     % deviation standard moyenne bruit
%                     std_bruit_all=[std1 std2];
%                     std_bruit_all(isnan(std_bruit_all))=[]; %supprime les NaN
%                     std_bruit=mean(std_bruit_all); %NaN si isempty(std_bruit)
%                     if isnan(std_bruit)
%                         std_bruit=0;
%                     end
%                      y=minspec+mean([amp_bruit/2 std_bruit]);
%                
%                 end
%                 
%         end
                
end

% function slidsup(hObject,~)
% %     rang=get(hlist,'value');
% %     data(rang).ysup=get(hObject,'value');
% %     data(rang).amp=data(rang).ysup-data(rang).yinf;
% %     dispexp(hlist,[],'old')
% end
% 
% function slidinf(hObject,~)
% %     rang=get(hlist,'value');
% %     data(rang).yinf=get(hObject,'value');
% %     data(rang).amp=data(rang).ysup-data(rang).yinf;
% %     dispexp(hlist,[],'old')
% end

% function slidalpha(~,~)
%     alpha2=get(hslidalpha,'value');
%     dispsat()
% end

function sent(~,~)
    rang=get(hlist,'value');
    for n=1:nbspec
        data(n).B1min=data(rang).B1min;%plage de champ pour tenir compte du bruit (bas champ)
        data(n).B1max=data(rang).B1max;
        data(n).imin1=data(rang).imin1;
        data(n).imax1=data(rang).imax1; %indice pour plage bas champ 

        data(n).B2min=data(rang).B2min;%plage de champ pour tenir compte du bruit (haut champ)
        data(n).B2max=data(rang).B2max;
        data(n).imin2=data(rang).imin2;
        data(n).imax2=data(rang).imax2; %indice pour plage haut champ 
        
        data(n).polydeg=data(rang).polydeg;
        data(n).baseline=extractbaseline(data(n));
        x=data(n).spec;
        y=data(n).baseline;
        if ~isequal(size(x),size(y))
           x=x'; 
        end
        data(n).primcorr=x-y;
        data(n).integtype=data(rang).integtype;
         if get(hchkintarea,'value')
            imin_maxB1=data(n).imax1;
            imax_minB2=data(n).imin2;
        else
            imin_maxB1=1;
            imax_minB2=numel(data(n).B);
        end
        
        data(n).amp=trapz(data(n).B(imin_maxB1:imax_minB2),data(n).primcorr(imin_maxB1:imax_minB2));
        

%         data(n).Bsmin=data(rang).Bsmin;%plage de champ du signal
%         data(n).Bsmax=data(rang).Bsmax;
%         data(n).imins=data(rang).imins;
%         data(n).imaxs=data(rang).imaxs; %indice pour plage du signal 
% 
%         data(n).ysup=posline(data(n),'sup'); %ordonnee ligne superieure
%         data(n).yinf=posline(data(n),'inf'); %ordonnee ligne inferieure
%         data(n).amp=data(n).ysup-data(n).yinf;
    end
    
    %dispsat()
    dispexp(hlist,[],'old')
end

function savesat(~,~)
    %%% sauvegarde fichier txt
    [fnametxt,path2save]=uiputfile(fullfile(chemin,'*.txt'));
    if ~(isnumeric(fnametxt)) %no cancel
        fid=fopen(fullfile(path2save,fnametxt),'w');
        if isfield(data,'timenum') %from 1D
            fprintf(fid,'%22s\t','Rec. time');
        end
        fprintf(fid,'%12s\t','time');
        fprintf(fid,'%12s\t','time');
        fprintf(fid,'%12s\t','Amplitude');
        fprintf(fid,'%12s\n','File name');
        if isfield(data,'timenum') %from 1D
            fprintf(fid,'%22s\t','[ date  hh:mm:ss]');
        end
        time2disp=char(data(end).time+seconds(tdelay));
        nb22points=numel(strfind(time2disp,':'));
        if nb22points==2 % hh:mm:ss
            fprintf(fid,'%12s\t','[hh:mm:ss]');
        else
            fprintf(fid,'%12s\t','[dd:hh:mm:ss]');
        end
        fprintf(fid,'%12s\t','[s]');
        fprintf(fid,'%12s\t','[ ]');
        fprintf(fid,'%12s\n','[ ]');
        for i=1:nbspec
            taillename(i)=numel(data(i).name);
        end
        maxname=max(taillename);
        for i=1:nbspec
            if isfield(data,'timenum') %from 1D
                fprintf(fid,'%22s\t',char(data(i).timenum)); %heure d'enregistrement
            end
            fprintf(fid,'%12s\t',char(data(i).time+seconds(tdelay))); %t+t0
            fprintf(fid,'%12.0f\t',char(seconds(data(i).time)+tdelay)); %t+t0 en s
            fprintf(fid,'%12.5e\t',data(i).amp);
            fprintf(fid,['%',num2str(maxname),'s\n'],data(i).name);
        end
        fclose(fid);

        % save plot
        figure2save=figure();%max(findobj('type','figure'))+1); %pour ne pas superposer des figures 
        set(figure2save,'Units','pixels','Name','Kinetic','NumberTitle','on','visible','on',...
                'Position',[300 300 400 400],'Menubar', 'figure','Toolbar', 'figure');
        hnew=copyobj(hsataxes,figure2save);%graphe(=axes)+legend copier dans nouvelle figure
        set(hnew,'units','normalized','position',[0.15 0.12 0.8 0.8])%,...
        title(hnew,['Kinetic: ',fnametxt(1:end-4)],'interpreter','none');

        timeunitnum=get(htimeunit,'value'); %1:s / 2:min / 3:h
        if timeunitnum==1
            xlabel('Time [s]');
        end
        if timeunitnum==2
           xlabel('Time [min]');
        end
        if timeunitnum==3
          xlabel('Time [h]');
        end
        
        set(findobj(hnew,'MarkerFaceColor','m'),'MarkerFaceColor','none');
        set(figure2save,'visible','on');
        saveas(figure2save,fullfile(path2save,[fnametxt(1:end-3),'tif']));
        saveas(figure2save,fullfile(path2save,[fnametxt(1:end-3),'fig']));
        delete(figure2save);

        %save data
        save(fullfile(path2save,[fnametxt(1:end-3),'mat']),'data','tdelay');
    end
end

function considerdelay(hObject,~)
     t0=str2double(get(hObject,'string'));  
     if ~isnan(t0)
         tdelay=t0;
     else
         set(hObject,'string',num2str(tdelay))
     end   
     dispexp(hlist,[],'old')
end

% function setlinetype(~,~)
%       for n=1:nbspec
%           data(n).ysup=posline(data(n),'sup'); %ordonnee ligne superieure
%           data(n).yinf=posline(data(n),'inf'); %ordonnee ligne inferieure
%           data(n).amp=data(n).ysup-data(n).yinf;
%       end
%       dispexp(hlist,[],'old')
% 
% end
function setpolydeg(~,~)
    rang=get(hlist,'value');
    data(rang).polydeg=int8(str2num(get(hdegree,'string')));
    set(hdegree,'string',num2str(data(rang).polydeg));
    dispexp(hlist,[],'new')
end

function plus_or_minus_fctn(~,~,strsign) %strsign='+' ou '-'
    rang=get(hlist,'value');
   switch strsign
       case '+'
           if rang~=nbspec
               rang=rang+1;
           end         
           
       case '-'
           if rang~=1
               rang=rang-1;
           end   
   end
   set(hlist,'value',rang);
   
   dispexp(hlist,[],'new')    

end

function MouseleftclicksatCllbck(~,~) %init
    delete(hcurrent)
    p = get(hsataxes,'CurrentPoint');
    p = p(1,1:2);

    timeunitnum=get(htimeunit,'value'); %1:s / 2:min / 3:h
    if timeunitnum==1
        time2search=abs((seconds([data.time])+tdelay-p(1))./(seconds([data.time])+tdelay));
    end
    if timeunitnum==2
       time2search=abs((minutes([data.time])+tdelay/60-p(1))./(minutes([data.time])+tdelay/60));
    end
    if timeunitnum==3
       time2search=abs((hours([data.time])+tdelay/3600-p(1))./(hours([data.time])+tdelay/3600));
    end
    
    minsearch=[time2search;abs(([data.amp]-p(2))./[data.amp])];
    
    [~,rang]=min(sum(minsearch,1));%min(abs([data.time]+tdelay-p(1)));%recherche du rang avec valeur de temps la plus proche

    if timeunitnum==1
        tcurrent=seconds(data(rang).time)+tdelay;
    end
    if timeunitnum==2
        tcurrent=minutes(data(rang).time)+tdelay/60;
    end
    if timeunitnum==3
        tcurrent=hours(data(rang).time)+tdelay/3600;
    end
    
    
    hcurrent=plot(tcurrent,data(rang).amp,'ob','MarkerFaceColor','m','parent',hsataxes);

    set(hlist,'value',rang);
    dispexp(hlist,[],'new');
end


function dispderivative(~,~)
    dispexp(hlist,[],'new');
end

function integration_area(hobject,~)
    rang=get(hlist,'value');
    data(rang).integtype=get(hobject,'value');
    dispexp(hlist,[],'new');
end

end