% single file plot l1b, l2

function single_file_plot(file_name1, file_name2, file_name3)

L1b = L1b_importfile(file_name1, 2, Inf);

if nargin >1
    
    if (strcmp(file_name2(12:14), 'csn') ||strcmp(file_name2(12:14),'cso'))
        neut_name = file_name2;
        L2N = l2_cso_importfile(neut_name, 2, Inf);
    end
    if nargin > 2
        ion_name = file_name3;
        L2I = L2_ion_importfile(ion_name, 2, Inf);
    elseif (strcmp(file_name2(12:14),'ion'))
        ion_name = file_name2;
        L2I = L2_ion_importfile(ion_name, 2, Inf);
    end
    
end
% end
% raw plots

cps = L1b.cps_dt_bkgd;
orbit = L1b.orbit(1);
TID = L1b.TID(1);
mass_all = L1b.mass;
fmode = L1b.fmode;
ttid = L1b.t_tid;
alt = L1b.alt;
CS = find(strcmp(fmode, 'csn'));
% massCS = mass_all(CS);
m16 = find(mass_all ==16 & strcmp (fmode, 'csn'));
m166 = find(mass_all ==166 & strcmp (fmode, 'csn'));
m32 = find(mass_all ==32 & strcmp (fmode, 'csn'));
m44 = find(mass_all ==44 & strcmp (fmode, 'csn'));
m194= find(mass_all ==194 & strcmp (fmode, 'csn'));
m190= find(mass_all ==190 & strcmp (fmode, 'csn'));
m22 = find(mass_all ==22 & strcmp (fmode, 'csn'));
m12 = find(mass_all ==12 & strcmp (fmode, 'csn'));
m13 = find(mass_all ==13 & strcmp (fmode, 'csn'));
m40 = find(mass_all ==40 & strcmp (fmode, 'csn'));
m20 = find(mass_all ==20 & strcmp (fmode, 'csn'));
m4  = find(mass_all ==4  & strcmp (fmode, 'csn'));
m28 = find(mass_all ==28 & strcmp (fmode, 'csn'));
m178= find(mass_all ==178 & strcmp (fmode, 'csn'));
m14 = find(mass_all ==14 & strcmp (fmode, 'csn'));



[min_alt, ind_alt] = min(alt(CS));

tca = ttid(CS) - ttid(ind_alt);
% figure(1)
close all
scrsz = get(0,'ScreenSize');
figure('Position',[scrsz(3)/4.1 scrsz(4)/4.3 scrsz(3)/2 scrsz(4)/1.7])
% figure(1)
set(gca,'FontSize',18);

% m44 group
subplot(2,2,1)
semilogy(ttid(m44), cps(m44), ...
    ttid(m194), cps(m194), ...
    ttid(m22), cps(m22),...
    ttid(m12), cps(m12), ...
    ttid(m13), cps(m13) ...
    )
legend('m44', 'm194', 'm22', 'm12', 'm13')%, 'm4', 'm28', 'm14')
xlabel('tca seconds')
ylabel('cps')

%m40 group
subplot(2,2,2)
semilogy(ttid(m40), cps(m40), ...
    ttid(m190), cps(m190), ...
    ttid(m20), cps(m20)...
    )
legend('m40', 'm190', 'm20')%, 'm12', 'm13')%, 'm4', 'm28', 'm14')
xlabel('tca seconds')
ylabel('cps')

%m28 subgroup
subplot(2,2,3)
semilogy(ttid(m28), cps(m28), ...
    ttid(m178), cps(m178), ...
    ttid(m14), cps(m14),...
    ttid(m4), cps(m4)...
    )
legend('m28', 'm178', 'm14', 'm4')%, 'm13')%, 'm4', 'm28', 'm14')
xlabel('tca seconds')
ylabel('cps')

%m16 subgroup
subplot(2,2,4)
semilogy(ttid(m16), cps(m16), ...
    ttid(m32), cps(m32) ...
    )
if ~isempty(m166)
    semilogy(ttid(m166), cps(m166), 'g')
end
legend('m16', 'm166', 'm32')%, 'm12', 'm13')%, 'm4', 'm28', 'm14')
xlabel('tca seconds')
ylabel('cps')
% xlim([-500,500]); grid on; grid(gca,'minor')
name3 = ['osion_',num2str(TID), '_', num2str(orbit), '_cps_tca'];
saveas(gcf, name3, 'fig')
%level 2 plot

cpsN = L2N.cps_dt_bkd;
altN = L2N.alt;
ttidN = L2N.t_tid;
densN = L2N.abundance;
latN = L2N.lat;
longN = L2N.long;
szaN = L2N.sza;
lstN = L2N.lst;
errN = L2N.precision;
massN = L2N.mass;

m40 = find(massN ==40);
m44 = find(massN ==44);
m14 = find(massN ==14);
m4  = find(massN ==4 );
m28 = find(massN ==28 & strcmp(L2N.focusmode, 'csn'));
m32 = find(massN ==32);
m16 = find(massN ==16);
m30 = find(massN ==30);


altN = altN(m40);
ttidN = ttidN(m40);
latN = latN(m40);
longN = longN(m40);
szaN = szaN(m40);
lstN = lstN(m40);

[min_altN, ind_altN] = min(altN);

tcaN = ttidN - ttidN(ind_altN);

IN = find(tcaN <=0);
OUT = find(tcaN > 0);

altIN = altN(IN);
altOUT = altN(OUT);
latIN = latN(IN);
latOUT = latN(OUT);
longIN = longN(IN);
longOUT = longN(OUT);
lstIN = lstN(IN);
lstOUT = lstN(OUT);
szaIN = szaN(IN);
szaOUT = szaN(OUT);

cpsArIN = cpsN(m40(IN));
cpsArOUT = cpsN(m40(OUT));
densArIN = densN(m40(IN));
densArOUT = densN(m40(OUT));
errArIN = errN(m40(IN));
errArOUT = errN(m40(OUT));

cpsCO2IN = cpsN(m44(IN));
cpsCO2OUT = cpsN(m44(OUT));
densCO2IN = densN(m44(IN));
densCO2OUT = densN(m44(OUT));
errCO2IN = errN(m44(IN));
errCO2OUT = errN(m44(OUT));

cpsHeIN = cpsN(m4(IN));
cpsHeOUT = cpsN(m4(OUT));
densHeIN = densN(m4(IN));
densHeOUT = densN(m4(OUT));
errHeIN = errN(m4(IN));
errHeOUT = errN(m4(OUT));

if ~isempty(m28)
cpsCOIN = cpsN(m28(IN));
cpsCOOUT = cpsN(m28(OUT));
densCOIN = densN(m28(IN));
densCOOUT = densN(m28(OUT));
errCOIN = errN(m28(IN));
errCOOUT = errN(m28(OUT));
end

cpsN2IN = cpsN(m14(IN));
cpsN2OUT = cpsN(m14(OUT));
densN2IN = densN(m14(IN));
densN2OUT = densN(m14(OUT));
errN2IN = errN(m14(IN));
errN2OUT = errN(m14(OUT));
if ~isempty(m30)
    cpsOIN = cpsN(m16(IN));
    cpsOOUT = cpsN(m16(OUT));
    densOIN = densN(m16(IN));
    densOOUT = densN(m16(OUT));
    errOIN = errN(m16(IN));
    errOOUT = errN(m16(OUT));
    
    cpsO2IN = cpsN(m32(IN));
    cpsO2OUT = cpsN(m32(OUT));
    densO2IN = densN(m32(IN));
    densO2OUT = densN(m32(OUT));
    errO2IN = errN(m32(IN));
    errO2OUT = errN(m32(OUT));
    
    cpsNOIN = cpsN(m30(IN));
    cpsNOOUT = cpsN(m30(OUT));
    densNOIN = densN(m30(IN));
    densNOOUT = densN(m30(OUT));
    errNOIN = errN(m30(IN));
    errNOOUT = errN(m30(OUT));
end
% figure(2)
scrsz = get(0,'ScreenSize');
figure('Position',[scrsz(3)/4.1 scrsz(4)/4.3 scrsz(3)/2 scrsz(4)/1.7])
% figure(1)
set(gca,'FontSize',18);

% hold on
% inbound desnity Alt
subplot(2,1,1)

semilogx(densArIN, altIN, densCO2IN, altIN, densHeIN, altIN, ...
    densN2IN, altIN)
hold
if ~isempty(m28)
    semilogy(densCOIN, altIN, 'm')
end
if ~isempty(m30)
    semilogx(densO2IN, altIN, densOIN, altIN, densNOIN, altIN)
    %    hold
    %    legend()
end
legend('Ar', 'CO2', 'He', 'N2', 'CO','O2', 'O', 'NO')
xlabel('density')
ylabel('alt inbound')
ylim([0,400]); grid on; grid(gca,'minor')
% hold off
% hold on
subplot(2,1,2)
semilogx(densArOUT, altOUT, densCO2OUT, altOUT, densHeOUT, altOUT, ...
    densN2OUT, altOUT)
hold
if ~isempty(m28)
    semilogy(densCOOUT, altOUT, 'm')
end
% legend('Ar', 'CO2', 'He', 'N2', 'CO')

if ~isempty(m30)
    semilogx(densO2OUT, altOUT, densOOUT, altOUT, densNOOUT, altOUT)
    %    hold
    %    legend('O2', 'O', 'NO')
end
legend('Ar', 'CO2', 'He', 'N2', 'CO','O2', 'O', 'NO')
xlabel('density')
ylabel('alt outbound')
ylim([0,400]); grid on; grid(gca,'minor')
name4 = ['osion_',num2str(TID), '_', num2str(orbit), '_csndensity'];
saveas(gcf, name4, 'fig')
% hold off
if strcmp(file_name2(12:14),'cso')
    return
else


    massI = L2I.ion_mass;
    lstI   = L2I.lst;
    longI = L2I.long;
    latI = L2I.lat;
    altI = L2I.alt;
    cpsI  = L2I.cps_dt;
    ttidI = L2I.t_tid;
    lstI = L2I.lst;
    densI = L2I.abundance;
    errI = L2I.precision;
    szaI= L2I.sza;
    [minaltI, indaltI] = min(altI);
    tcaI = ttidI - ttidI(indaltI);
    
    m32i = find(massI ==32);
    m16i = find(massI ==16);
    m28i = find(massI ==28);
    
    %     figure(3)
    scrsz = get(0,'ScreenSize');
    figure('Position',[scrsz(3)/4.1 scrsz(4)/4.3 scrsz(3)/2 scrsz(4)/1.7])
    % figure(1)
    set(gca,'FontSize',18);
    
    subplot(2,1,1)
    title('ion counts vs time')
    semilogy(ttidI(m32i), cpsI(m32i), ttidI(m16i), cpsI(m16i), ttidI(m28i), cpsI(m28i))
    xlabel('time s')
    ylabel('cps')
    legend('O2+', 'O+', 'CO+')
    
    subplot(2,1,2)
    title('ion density vs time')
    semilogy(tcaI(m16i), densI(m16i,:), 'b')
    hold
    errorbar(tcaI(m16i), densI(m16i,:), errI(m16i,:), 'b.')
    semilogy(tcaI(m28i), densI(m28i,:),'r')
    errorbar(tcaI(m28i), densI(m28i,:), errI(m28i,:), 'r.')
    semilogy(tcaI(m32i), densI(m32i,:), 'g')
    errorbar(tcaI(m32i), densI(m32i,:), errI(m32i,:), 'g.')
    ylim([1e-2,1e6]); grid on; grid(gca,'minor');
    set(gca, 'yscale', 'log')
    xlabel('TCA (s)')
    ylabel('density')
    %     title([num2str(TID), '   ' num2str(orbit), '  density O2+ O+ 28+'])
    legend('O+','','CO+','', 'O2+')
    hold off
    name5 = ['osion_',num2str(TID), '_', num2str(orbit), '_iondensity'];
    saveas(gcf, name5, 'fig')
    %     saveas(gcf, name5, 'jpg')
    
    %     xlabel('time s')
    %     ylabel('part/cc')
    %     legend('O2+', 'O+', 'CO+')
    
    
    
end



