clear all;
close all;

read_parameters_stratus
fclose('all');
%%%%%%%%%
tit = cruise;
lt = length(cruise);
tit(lt+1) = '-';
tit((lt+2):lt+5) = year;

%days =input('Input YYMMDD to evaluate (071001, 071002, 071003....): ','s');
fname = '*.csv';

s1=[way_raw_data_aerosol fname];
s=way_raw_data_aerosol;

a=dir(s1);
%%%%%%%%

jd=[];
dday=[];dmonth=[];
c0=[];c1=[];c2=[];c3=[];c4=[];c5=[];fl=[];

for i=1:length(a)
    a(i).name
    day=str2num(a(i).name(5:6));
    month=str2num(a(i).name(3:4));
    chnl0=[];chnl1=[];chnl2=[];chnl3=[];chnl4=[];chnl5=[];flow_rate=[];
    temp=importdata([way_raw_data_aerosol a(i).name],',');
    time1=[];
    for j=2:length(temp.textdata)

        dummy=temp.textdata(j,2);

        dummy=dummy{1};

        hour=str2num(dummy(2:3));
        minute=str2num(dummy(5:6));

        sec=str2num(dummy(8:9));

        time1=[time1 hour*3600+minute*60+sec];
        clear minute sec dummy;
    end
    flow_rate=vertcat(flow_rate,temp.data(:,1));
    chnl0=vertcat(chnl0,temp.data(:,4));
    chnl1=vertcat(chnl1,temp.data(:,6));
    chnl2=vertcat(chnl2,temp.data(:,8));
    chnl3=vertcat(chnl3,temp.data(:,10));
    chnl4=vertcat(chnl4,temp.data(:,12));
    chnl5=vertcat(chnl5,temp.data(:,14));

    clear temp;

    for j=hour*3600+600:600:(hour+1)*3600
        g=find(time1>j-600 & time1<j);
        if length(g)>0
            c0=[c0 nanmean(chnl0(g))];
            c1=[c1 nanmean(chnl1(g))];
            c2=[c2 nanmean(chnl2(g))];
            c3=[c3 nanmean(chnl3(g))];
            c4=[c4 nanmean(chnl4(g))];
            c5=[c5 nanmean(chnl5(g))];
            fl=[fl nanmean(flow_rate(g))];
            jd=[jd datenum(2007,month,day)-datenum(2006,12,31)+time1(g(1))/86400];
            clear g;
        end
    end

    clear chnl* hour;

end

dtt=median(diff(time1))/60;%time interval, min
flow=median(flow_rate);
xminx=floor(min(jd));xmaxx=floor(max(jd))+1;
figure(1)
set(gcf,'defaultaxesfonts',18)
set(gca,'fontname','time','fontsize',18);
%subplot(2,1,1)
semilogy(jd,10*c0./(flow*dtt),'b.')
hold on
semilogy(jd,10*c1./(flow*dtt),'r.')
semilogy(jd,10*c2./(flow*dtt),'g.')
semilogy(jd,10*c3./(flow*dtt),'y.')
semilogy(jd,10*c4./(flow*dtt),'c.')
semilogy(jd,10*c5./(flow*dtt),'k.')
h=legend('0.1 \mum','0.2 \mum','0.3 \mum','0.5 \mum','1 \mum','5 \mum','location','northwest','orientation','horizontal');
set(h,'fontsize',6)
ylabel('No of Particles (cm^{-3})');
xlabel(['Julian Day ' year])
xlim([xminx xmaxx]);ylim([1e-2 1e3]);
title([tit ' PMS 6-sec data'])


print_buffer = [way_images_aerosol cruise '_' year '_np.jpg'];
print('-djpeg90 ', print_buffer);

%set(gca,'xtick',[0:4:24]);

figure(2)
%subplot(2,1,2)
semilogy(jd,10*(c0+c1+c2+c3+c4+c5)./(flow*dtt),'k.');
ylabel('Total No of Particles (cm^{-3})');
xlabel(['Julian Day ' year])
title([tit ' Total No of particles'])
%xlim([286 299])
%set(gca,'xtick',[0:4:24])
xlim([xminx xmaxx]);ylim([1e0 1e3]);
print_buffer = [way_images_aerosol cruise '_' year '_tnp.jpg'];
print('-djpeg90 ', print_buffer);

Ntotal=c0+c1+c2+c3+c4+c5;
stux_all=[jd' Ntotal' c0' c1' c2' c3' c4' c5'];
y=stux_all;
jdav=[];yav=[];yvar=[];
[jdav,yav,yvar]=timave_2_nonan(jd,10,y);
[jdhr,yhr,yvarr]=timave_2_nonan(jd,60,y);

  ss= way_proc_data_aerosol;
    flist1=fopen([ss 'PMS_10mn_' cruise '_' year '.txt'],'w');
    fprintf(flist1,'%12.6f %10.5f %10.5f %5d %10.5f %10.5f \r',yav');
fclose (flist1);
    flist1=fopen([ss 'PMS_hr_' cruise '_' year '.txt'],'w');
    fprintf(flist1,'%12.6f %10.5f %10.5f %5d %10.5f %10.5f \r',yhr');
fclose (flist1);


%save F:\AMMA_2007\RHB\Aerosol\Raw\PMS_10mn_AMMA_07.txt yav -ascii -tabs;

figure(3)
semilogy(jdav,yav(:,3)*10,'.',jdav,yav(:,4)*10,'.',jdav,yav(:,5)*10,'.',jdav,yav(:,6)*10,'.',jdav,yav(:,7)*10,'y.',jdav,yav(:,8)*10,'k.');
set(gca,'fontsize',14);
ylabel('Ave Cts in 6 sec');
xlabel(['Julian Day ' year]);
title([tit ' PMS 10-min data'])
%title([tit ' PMS']);
xlim([xminx xmaxx]);ylim([1e2 1e6]);
print_buffer = [way_images_aerosol cruise '_' year '_tcts.jpg'];
print('-djpeg90 ', print_buffer);

%dt=median(diff(time1))/60;%time interval in min
%flow=2.829e4;%flow rate, cc/min
vol=flow*dtt;
std=jd(1);ent=jd(length(jd));

figure(4)
subplot(2,1,1);semilogy(jdav,yav(:,2)*10/vol,'.');
set(gca,'fontsize',12);
ylabel('Total Conc. (#/cm^3)');
title([tit ' PMS 10-min data']);
xlim([xminx xmaxx]);ylim([1e0 1e3]);


%axis([std ent 10 1e3]);
subplot(2,1,2);semilogy(jdav,yav(:,3)*10/vol,'.',jdav,yav(:,4)*10/vol,'.',jdav,yav(:,5)*10/vol,'.',jdav,yav(:,6)*10/vol,'.',jdav,yav(:,7)*10/vol,'.',jdav,yav(:,8)*10/vol,'.');
set(gca,'fontsize',12);
ylabel('Conc. in Channel (#/cm^3)');
xlabel(['Year Day ' year]);
xlim([xminx xmaxx]);ylim([1e-2 1e3]);
h=legend('0.1 \mum','0.2 \mum','0.3 \mum','0.5 \mum','1 \mum','5 \mum','location','northoutside','orientation','horizontal');
set(h,'fontsize',4)
print_buffer = [way_images_aerosol cruise '_' year '_tconc.jpg'];
print('-djpeg90 ', print_buffer);


%axis([std ent .10 1e3]);


figure
plot(jd,fl)
title([tit '  flow=2.829e4'])
xlabel('Year Day (UTC)')
ylabel('Flow Rate (cc/min)')

print_buffer = [way_images_aerosol cruise '_' year '_tflow.jpg'];
print('-djpeg90 ', print_buffer);


