%%
[hdr,data] = basic_load_arena('20230202_214323_snow_digrx0_0031.dat');

[hdr2,data2] = basic_load_arena('20230202_214323_snow_digrx0_0047.dat');

% Sounding the McMurdo Ice Shelf:
% [hdr2,data2] = basic_load_arena('20230202_214323_snow_digrx0_0108.dat');

%%

dd = lp(fir_dec(fft(echo_filt(data{1},5)),11));
dd2 = lp(fir_dec(fft(echo_filt(data2{1},5)),11));

%%

fs = 1e9/16;
dt = 1/fs;
Nt = size(dd,1);
T = Nt*dt;
df = 1/T;
% freq = df * [-floor(Nt/2):floor((Nt-1)/2)]
freq = fftshift(df * [-floor(Nt/2):floor((Nt-1)/2)])
Tpd = 180e-6;
% (267.857142857143 - 107.142857142857/2)*56
% (267.857142857143 + 107.142857142857/2)*56
BW = 107.142857142857e6*56

alpha = BW / Tpd;

td = freq / alpha;
good_mask = td>0;
td = td - 48e-9;

vel = 4;
x = (hdr{1}.rel_time_cntr_latch - hdr{1}.rel_time_cntr_latch(1))/10e6 * vel;
x2 = (hdr2{1}.rel_time_cntr_latch - hdr2{1}.rel_time_cntr_latch(1))/10e6 * vel;

% er_ice = 1.53;
er_ice = 2;
% er_ice = 3.15;

figure(1); clf;
imagesc(x,td(good_mask)*3e8/2/sqrt(er_ice),dd(good_mask,:));
% imagesc(x,td(good_mask),dd(good_mask,:));
caxis([0 103]);
colormap(1-gray(256));
xlabel('Along-track (m)');
ylabel(sprintf('Depth (m), {\\epsilon}_r=%g',er_ice));

figure(2); clf;
imagesc(x2,td(good_mask)*3e8/2/sqrt(er_ice),dd2(good_mask,:));
% imagesc(x2,td(good_mask),dd2(good_mask,:));
caxis([0 103]);
colormap(1-gray(256));
xlabel('Along-track (m)');
ylabel(sprintf('Depth (m), {\\epsilon}_r=%g',er_ice));

link_figures([1 2]);

ylim([-1 30]);
% ylim([-1 55]);

return;

%%

saveas(1,'~/snow_example1.jpg');
saveas(2,'~/snow_example2.jpg');

saveas(1,'~/snow_example1.fig');
saveas(2,'~/snow_example2.fig');

%%

saveas(2,'~/snow_example3.jpg');
saveas(2,'~/snow_example3.fig');

