%%---synthetic data parameters----
dx=25*0.3048;dt=0.006;
ns=801;ng=801;nt=2000;
isv=801;
n_aperture=401;
xx=(0:ns-1)*dx;
nt0=1500;
tt=(0:nt0-1)*dt;
dat1=zeros(nt,ng);
dat0=zeros(nt,ng);
%%-------------------

%%========read raw data=============
fid=fopen('../data/rawdata/CSG.801','rb');
dat0=fread(fid,'float');
fclose(fid);
dat0=reshape(dat0,[nt,ng]);
figure;colormap(gray);
imagesc(xx,tt,dat0(1:nt0,:),[-0.2 0.2]*max(dat0(:)));
title('Raw data with narrow aperture');
xlabel('X (m)');
ylabel('Time (s)');
pause(0.5);

%%=========Apply time compensate========
for it=1:nt
    dat0(it,:)=dat0(it,:).*it^2;
end

%%=========Generate virtual shot gathers=======
display('Generating virtual shot gather......');
vshot=zeros(nt,ng);
for ig=1:4:isv-1
    ig;
    %% read green's function
    grnfile=['../data/dat_grn/GRN.',num2str(ig)];
    fid=fopen(grnfile,'rb');
    dat1=fread(fid,'float');
    dat1=reshape(dat1,[nt,ng]);
    fclose(fid);

    %%--------generate virtual shot gather--------
    tr1=dat0(:,ig);
    for is=1:isv-1
        if(is>=ig)
            xtr1=xcorr(tr1,dat1(:,is));
            vshot(:,is)=vshot(:,is)+xtr1((end+1)/2:end);
        end
    end
    %%--------generate virtual shto gather--------
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% apply mathching filter-- not finish yet

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

%%%%%normalize the data
vshot=vshot./max(vshot(:,401));
vshot(:,1:401)=dat0(:,1:401)./max(dat0(:,401));
vshot=mute_direct(vshot,dx,dt,801,0);

%%%%%% save the virtual shot gather
mkdir('../data/virtualshot/');
fid=fopen('../data/virtualshot/vshot801.dat','wb');
fwrite(fid,vshot,'float');
fclose(fid);

%%========Display virtual shot gather============
figure;colormap(gray);
imagesc(xx,tt,vshot(1:nt0,:),[-0.5 0.5]*max(vshot(:)));
title('Virtual shot gahter');
xlabel('X (m)');
ylabel('Time (s)');

%%%%% Display the full aperture shot gather
fid=fopen('../data/truedata/CSG.801','rb');
dat1=fread(fid,'float');
fclose(fid);
dat1=reshape(dat1,[nt,ng]);
figure;colormap(gray);
imagesc(xx,tt,dat1(1:nt0,:),[-0.2 0.2]*max(dat1(:)));
title('Ture CSG with full aperture');
xlabel('X (m)');
ylabel('Time (s)');



