function a=doit(spec,file,printdir,j,q,filename,DD)
%abs(X) replaced by max(X,0)
BEGIN=1;
WIN=80;
YY=wavread(file);
Q=q;
J=j;
T=2^(j/q);
clf
beginindex=BEGIN;
windowsnb=WIN;
[Y,ff]=wavread(file);
size(Y);
Y=Y(:,1);
Y=Y/norm(Y);
TT=floor(length(Y)/(2^15+windowsnb*2^15));
SC111=[];
SC121=[];
mkdir(['/NAS3/SABIOD/public_data/' printdir '/']);
totaltime=0;
nombrecachalot=0;
wavsumsquare=0;
energietotale11=0;
energietotale12=0;
II=0;
for JJJ=1:TT
II=II+1;
endindex=beginindex+2^15+2^15*windowsnb;
YY=Y(beginindex:(beginindex+2^15+windowsnb*2^15));
length(YY)/2^15
[SC11,begintime,endtime,enersc11]=humpbackw2(1,j,q,YY);
%[SC12,begintime,endtime,enersc12]=humpbackw2(beginindex,windowsnb,2,j,q,file,printdir);
SC111=[SC111 SC11];
%SC121=[SC121 SC12];
energietotale11=energietotale11+enersc11;
%energietotale12=energietotale12+enersc12;




SC11=SC11(1:end-1,:);
SC11(:,1)=0;
SC11(:,end)=0;
MAX=(median(sum(SC11(10:end-25,:))))*1.35;
b=find(sum(SC11(10:end-25,:))>MAX);
y=zeros(1,length(SC11));
x=1:length(SC11);
energies=zeros(1,length(SC11));
for i=1:length(y)
if sum(SC11(10:end-25,i))>(MAX)
[n,m]=max(SC11(10:end-26,i));
y(i)=m+26;
energies(i)=n;
end
end
y(1)=0;
y(end)=0;
EE=energies;
seconds=length(YY)/ff;
seconds=seconds/length(SC11);
xticksd=x(find(diff(y>0.0)==1));
xticksf=x(find(diff(y>0.0)==-1));
m=y(xticksd+1);
energies=energies(xticksd+1);
binpos=xticksd;
xticksd=seconds.*xticksd;
xticksf=seconds.*xticksf;





YY=Y(beginindex:(beginindex+2^15+windowsnb*2^15));
totaltime=length(YY);
wavsumsquare=sum(YY.^2);



line11=1+15*sum(rect(SC11(2:30,:)));
line11=line11/max(line11);

seuil=line11>max(line11)/2;
compte=sum(diff(seuil)==1);
clf



BB=SC11;

for i=10:length(line11)-10
if line11(i)>1.8*mean(line11)
SC11(31:end,i-5:i+5)=0;
end
end

SC11(2:30,:)=SC11(2:30,:)/max(max(SC11(2:30,:)));
SC11(35:60,:)=SC11(35:60,:)/max(max(SC11(35:60,:)));
SC11(67:end,:)=SC11(67:end,:)/max(max(SC11(67:end,:)));

line12=1+15*sum(rect(SC11(35:60,:)));
line13=1+15*sum(rect(SC11(63:end,:)));
line12=line12/max(line12);
line13=line13/max(line13);
line12=conv(line12,ones(1,4));
line13=conv(line13,ones(1,4));



bruit=sum(sum(rect(SC11(80:end,:))));


v = SC11( 35:60,:);
sv = sum(v) ;
sv=conv(sv,ones(1,4));
ms2= sv > (max(sv)/2);
%cb2=sum(diff(ms2)==1);%supprime toutes les detections consecutives
[ICIms2, cb2] = fici(ms2);

ms3= sv > (max(sv)/3);
[ICIms3, cb3] = fici(ms3);
%cb3=sum(diff(ms3)==1);

Std=std(sum(SC11(:,:)'));




v2 = SC11(67:end,:);
sv2 = sum(v2) ;
sv2=conv(sv2,ones(1,4));
ms22= sv2 > (max(sv2)/1.5);
%cb22=sum(diff(ms22)==1);
[ICIms22, cb22] = fici(ms22);

ms32= sv2 > (max(sv2)/2.75);
[ICIms32, cb32] = fici(ms32);
%cb32=sum(diff(ms32)==1);



figure()
subplot(6,1,1)
hold on
plot(10*YY)
xlim([1 length(YY)])
title({['BALESTRIERO R. & GLOTIN H.  SABIOD.ORG - MI CNRS - Univ. Toulon  Scalogram representation on bioacoustic data [ScatNet] using a Gabor mother wavelet, Antares DATASET (' filename ') : Start at : ' num2str(beginindex)];'   ';'Waveform representation'},'fontsize',25,'Interpreter','none')

%subplot(7,1,2)
%hold on
%plot(ms2,'-g','linewidth',1)
%plot(ms32,'-r','linewidth',1)
%xlim([1 length(ms22)])
%title('Sperm Whale detector (max/3) ','fontsize',25)

subplot(6,1,3)
hold on
imagesc(flipud(BB(2:end,:)))
set(gca,'YDir','normal')
xlim([1 length(sum(abs(SC11(2:end-2,:))))])
title(['Full scalogram, T' num2str(T) ', Q' num2str(q) ', J' num2str(j) ' energy captured : ' num2str(enersc11)],'fontsize',25)

subplot(6,1,4)
hold on
imagesc(flipud(SC11(2:30,:)))
set(gca,'YDir','normal')
xlim([1 length(sum(abs(SC11(2:end-2,:))))])
title(['Top part of the scalogram for ping detection : (2:30), T' num2str(T) ', Q' num2str(q) ', J' num2str(j) ],'fontsize',25)


subplot(6,1,5)
hold on
imagesc(flipud(SC11(35:60,:)))
set(gca,'YDir','normal')
xlim([1 length(sum(abs(SC11(58:end-4,:))))])
title('Middle part of the scalogram used as a Ziphius detector (35:60)','fontsize',25)

subplot(6,1,6)
hold on
imagesc(flipud(SC11(60:end,:)))
set(gca,'YDir','normal')
xlim([1 length(sum(abs(SC11(2:end-2,:))))])
title(['Bottom part of the scalogram used as a Sperm Whale detector (60:end), T' num2str(T) ', Q' num2str(q) ', J' num2str(j) ],'fontsize',25)


subplot(6,1,2)
specgram(YY,spec,ff,[],spec/2);
title(['Spectrogram coeffs (specgram func): window=' num2str(spec) ', noverlap=' num2str(spec/2)],'fontsize',25)
a=13
if windowsnb>1
%a=2*windowsnb*3.5
a=2*windowsnb
end
set(gcf,'PaperUnits','inches','PaperPosition',[0 0 a 36])
print(['/NAS3/SABIOD/public_data/' printdir '/' DD '_' filename '_part' num2str(JJJ) '_windowsnb' num2str(windowsnb) '.png'],'-dpng','-r100')
beginindex=endindex;
%wavwrite(YY,ff,['/NAS3/SABIOD/public_data/' printdir '/' DD '_' filename '_part' num2str(JJJ) '.wav'])


%W=find(diff(ms3)==1);
%W=[W,zeros(1,200)];
%if length(W)>200
%W=W(1:200);
%end
%pp=JJJ;
%aa=str2num(filename(1:end-4))
%filename(1:end-4)
%for i=1:199
%aa=[aa str2num(filename(1:end-4))];
%pp=[pp JJJ];
%end
%W=[aa' pp' W']
W=[binpos',xticksd',xticksf',m',energies'];
save(['/NAS3/SABIOD/public_data/' printdir '/' DD '_' filename '_DETECTIONS_part' num2str(JJJ) '.txt'],'W','-ascii')


fid=fopen(['/NAS3/SABIOD/public_data/' printdir '/' DD '_' filename '_README_part' num2str(JJJ) '.txt'],'w');
tt=['Total description of the part ' num2str(JJJ) ' wav ' DD '_' filename ' :' num2str(totaltime) ' bins, ' num2str(ff) ' Hz, so about ' num2str(totaltime/ff) ' sec., sum of the wav squared : ' num2str(wavsumsquare) ', average energy captured for the scalo 1 L1: ' num2str(enersc11) ', standard deviation of the lines sum (for ping tracking) : ' num2str(Std) ', Sperm Whale detector : ms1.5 ' num2str(cb22) ' detection with a median separation of ' num2str(ICIms22) ' bins, ms2.5 ' num2str(cb32) ' detection with a median separation of ' num2str(ICIms32) ' bins, ' num2str((totaltime/ff)/length(SC11)) ' sec. per bin , ' num2str(compte) ' ping(s) detected , noise is at ' num2str(bruit) ];
fprintf(fid,'%s\r\n',tt);
fclose(fid);


end



filename11=strcat('',DD,'_',filename,'_T',num2str(T),'_Q',num2str(Q),'_J',num2str(J),'_scalo1_L1');
name11=strcat(['/NAS3/SABIOD/public_data/' printdir '/'],filename11);
name11=strcat(name11,'.dat');
csvwrite(name11,SC111);

end

function [ici, card] = fici(x)
[v, d] = find(x == 1);
dd = diff(d);
dd = dd(dd > 10);%on supprime les differences issues de pulses plats
card = length(dd);
ici = median(dd);
end

function rc= rect(x)
rc = max(x,0);
end

