
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Written by Joel Brito - 17/Aug/2014                                 %
%                                                                    %
%This routine reads the Org ascii file and plots daily aerosol conc. %
%values for a quick look. It saves a .mat with read data.            %
%                                                                    %
%Latest changes:                                                     %
%To improve processing time now the routine reads the previous mat   %
%file (if existing) and imports only updated ascii files. The same is%
%valid for making new plots                                          %
%                                                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


clear
close all
clc
[tmp,mydir]=fileparts(pwd);


if strcmp(mydir,'A_T2')
    station=' - T2 (Tiwa)';
else
    station=[' - ' mydir];    
end;

fl_dir='ACSM/';

%fl_dir='CE_nova_densidade/';

disp('ACSM')

%-------------------------------
%{
if exist('mat-files/ACSM.mat')>0
    load mat-files/ACSM.mat
    count_rem=0;
    clear rem_idx
    fl=dir([fl_dir,'ACSM_April_to_august_2014.csv']);

    for i=1:max(size(fl_old))
        for j=1:max(size(fl))
            if strcmp(fl(j).name,fl_old(i).name)
                if fl(j).bytes==fl_old(i).bytes
                    count_rem=count_rem+1;
                    rem_idx(count_rem)=j;
                end;
            end;
        end;
    end;

    if count_rem>0
        fl(rem_idx)=[];
        fl_old=[fl_old;fl];
    end;

else
%}
    %fl=dir([fl_dir,'ACSM_April_to_august_2014.csv']);
    %fl=dir([fl_dir,'ACSM_T2.csv']);
    fl=dir([fl_dir,'ACSM_*After*.csv']);
    count=0;
    count_old=1;
    time_ACSM_avg=[];
    Org_avg=[];
    fl_old=fl;
%end;



if size(fl,1)==0 && count==0
    return
elseif min(size(fl))>0

    for fl_number=1:max(size(fl))

        fid = fopen([fl_dir,fl(fl_number).name]);
        while ~feof(fid)
            TextLine = fgetl(fid);
            delimiters=0;
            date_delim=0;
            hour_delim=0;
            size_text=max(size(TextLine));
            i=2;
             while i<size_text
                if strcmp(TextLine(i),';')
                    delimiters=delimiters+1;
                    idx_delimiter(delimiters)=i;  
                end
                i=i+1;
            end

            if delimiters==8
                if max(size(TextLine(1:idx_delimiter(1))))>7
                    dummy_Org=str2num(TextLine(idx_delimiter(1)+1:idx_delimiter(2)-1));
                    dummy_SO4=str2num(TextLine(idx_delimiter(2)+1:idx_delimiter(3)-1));
                    dummy_NO3=str2num(TextLine(idx_delimiter(3)+1:idx_delimiter(4)-1));
                    dummy_NH4=str2num(TextLine(idx_delimiter(4)+1:idx_delimiter(5)-1));
                    dummy_Chl=str2num(TextLine(idx_delimiter(5)+1:idx_delimiter(6)-1));
                    dummy_f44=str2num(TextLine(idx_delimiter(6)+1:idx_delimiter(7)-1));
                    dummy_f43=str2num(TextLine(idx_delimiter(7)+1:idx_delimiter(8)-1));
                    dummy_f60=str2num(TextLine(idx_delimiter(8)+1:end));
                    i=1;
                    try
                        dummy_time=datenum(TextLine(1:idx_delimiter(1)-1),'dd/mm/yyyy HH:MM');
                        time_error=0;
                    catch error_message
                        time_error=1;
                    end
                    if max(size(dummy_Org))==1 && max(size(dummy_Chl))==1 && max(size(dummy_SO4))==1 &&  time_error==0;
                            count=count+1; 
                            Org(count)=dummy_Org;
                            SO4(count)=dummy_SO4;
                            NO3(count)=dummy_NO3;
                            NH4(count)=dummy_NH4;
                            Chl(count)=dummy_Chl;
                            time_ACSM(count)=dummy_time;
                            if max(size(dummy_f43))==1 && max(size(dummy_f44))==1
                                f44(count)=dummy_f44;
                                f43(count)=dummy_f43;
                                f60(count)=dummy_f60;
                            else
                                f44(count)=NaN;
                                f43(count)=NaN;
                                f60(count)=NaN;
                            end;
                            
                    end;
                end;
            elseif delimiters==12
                if max(size(TextLine(1:idx_delimiter(1))))>7
                    dummy_Org=str2num(TextLine(idx_delimiter(1)+1:idx_delimiter(2)-1));
                    dummy_SO4=str2num(TextLine(idx_delimiter(2)+1:idx_delimiter(3)-1));
                    dummy_NO3=str2num(TextLine(idx_delimiter(3)+1:idx_delimiter(4)-1));
                    dummy_NH4=str2num(TextLine(idx_delimiter(4)+1:idx_delimiter(5)-1));
                    dummy_Chl=str2num(TextLine(idx_delimiter(5)+1:idx_delimiter(6)-1));
                    dummy_f44=str2num(TextLine(idx_delimiter(6)+1:idx_delimiter(7)-1));
                    dummy_f43=str2num(TextLine(idx_delimiter(7)+1:idx_delimiter(8)-1));
                    dummy_m16=str2num(TextLine(idx_delimiter(8)+1:idx_delimiter(9)-1));
                    dummy_m17=str2num(TextLine(idx_delimiter(9)+1:idx_delimiter(10)-1));
                    dummy_m18=str2num(TextLine(idx_delimiter(10)+1:idx_delimiter(11)-1));
                    dummy_m19=str2num(TextLine(idx_delimiter(11)+1:idx_delimiter(12)-1));
                    dummy_m20=str2num(TextLine(idx_delimiter(12)+1:end));
                    
                    i=1;
                    try
                        dummy_time=datenum(TextLine(1:idx_delimiter(1)-1),'dd/mm/yyyy HH:MM');
                        time_error=0;
                    catch error_message
                        time_error=1;
                    end
                    if max(size(dummy_Org))==1 && max(size(dummy_Chl))==1 && max(size(dummy_SO4))==1 &&  time_error==0;
                            count=count+1; 
                            Org(count)=dummy_Org;
                            SO4(count)=dummy_SO4;
                            NO3(count)=dummy_NO3;
                            NH4(count)=dummy_NH4;
                            Chl(count)=dummy_Chl;
                            time_ACSM(count)=dummy_time;
                            mWater(count)=dummy_m16+dummy_m17+dummy_m18+dummy_m19+dummy_m20;
                            if max(size(dummy_f43))==1 && max(size(dummy_f44))==1
                                f44(count)=dummy_f44;
                                f43(count)=dummy_f43;
                            else
                                f44(count)=NaN;
                                f43(count)=NaN;
                            end;
                    end;
                end;
            

            elseif delimiters==5
                if max(size(TextLine(1:idx_delimiter(1))))>7
                    dummy_Org=str2num(TextLine(idx_delimiter(1)+1:idx_delimiter(2)-1));
                    dummy_SO4=str2num(TextLine(idx_delimiter(2)+1:idx_delimiter(3)-1));
                    dummy_NO3=str2num(TextLine(idx_delimiter(3)+1:idx_delimiter(4)-1));
                    dummy_NH4=str2num(TextLine(idx_delimiter(4)+1:idx_delimiter(5)-1));
                    dummy_Chl=str2num(TextLine(idx_delimiter(5)+1:end));
                    i=1;
                    try
                        dummy_time=datenum(TextLine(1:idx_delimiter(1)-1),'dd/mm/yyyy HH:MM');
                        time_error=0;
                    catch error_message
                        time_error=1;
                    end
                    if max(size(dummy_Org))==1 && max(size(dummy_Chl))==1 && max(size(dummy_SO4))==1 &&  time_error==0;
                            count=count+1; 
                            Org(count)=dummy_Org;
                            SO4(count)=dummy_SO4;
                            NO3(count)=dummy_NO3;
                            NH4(count)=dummy_NH4;
                            Chl(count)=dummy_Chl;
                            time_ACSM(count)=dummy_time;
                    end;
                end;
            end   

        end;
        status=fclose(fid);
    end;
end;
    %--------------------------------

    
[time_ACSM,idx]=unique(time_ACSM);
Org=Org(idx);
SO4=SO4(idx);
NO3=NO3(idx);
NH4=NH4(idx);
Chl=Chl(idx);
%mWater=mWater(idx);


label_ACSM='Mass concentriton (ug m-3)';
title_ACSM='ACSM';

if exist('f44','var')
    f43=f43(idx);
    f44=f44(idx);
    f60=f60(idx);
    O_C=0.0382.*100.*f44+0.0794;
end

%[avg_daily_O_C,std_daily_O_C,avg_WE_O_C,std_WE_O_C,avg_WD_O_C,std_WD_O_C]=Diurnal_variation(O_C,time_ACSM-4./24,'TT34 - ','O:C','O_C','O:C','');
        
%[avg_daily_Org,std_daily_Org,avg_WE_Org,std_WE_Org,avg_WD_Org,std_WD_Org]=Diurnal_variation(Org,time_ACSM,'TT34 - ',label_ACSM,'Org','Organics','');
        

save mat-files/ACSM_after_IOP2.mat

return


%{
if exist('mat-files/flag_OFR.mat')
    load mat-files/flag_OFR.mat
   
    count_OFR=0;
    clear idx_ACSM_OFR
    if min(size(time_OFR))>0
        for i=1:max(size(time_OFR))
            [diff,idx]=min(abs(time_ACSM-time_OFR(i)));
            [a,idx_end]=min(abs(time_ACSM-(time_OFR(i)+3./(24.*60))));
            if diff<1./(60.*24) && valve_status(i)==1
              count_OFR=count_OFR+1;
              idx_ACSM_OFR(count_OFR)=idx;
              count_OFR=count_OFR+1;
              idx_ACSM_OFR(count_OFR)=idx_end;
            end;
        end

        idx_ACSM_OFR=unique(idx_ACSM_OFR);
        Org_OFR=Org(idx_ACSM_OFR);
        SO4_OFR=SO4(idx_ACSM_OFR);
        NO3_OFR=NO3(idx_ACSM_OFR);
        NH4_OFR=NH4(idx_ACSM_OFR);
        Chl_OFR=Chl(idx_ACSM_OFR);
        f43_OFR=f43(idx_ACSM_OFR);
        f44_OFR=f44(idx_ACSM_OFR);
        time_ACSM_OFR=time_ACSM(idx_ACSM_OFR);
        count=count-max(size(time_ACSM(idx_ACSM_OFR)));
        Org(idx_ACSM_OFR)=[];
        SO4(idx_ACSM_OFR)=[];
        NO3(idx_ACSM_OFR)=[];
        NH4(idx_ACSM_OFR)=[];
        Chl(idx_ACSM_OFR)=[];
        f43(idx_ACSM_OFR)=[];
        f44(idx_ACSM_OFR)=[];
        time_ACSM(idx_ACSM_OFR)=[];
    end;
end
%}


if isunix
    fig1 = figure('visible','off');
else
    fig1=figure;
end;

set(fig1,'InvertHardcopy','on');
set(gca, 'FontSize', 12, 'LineWidth', 2); 
hold on
plot(time_ACSM,Org,'g*')
plot(time_ACSM,SO4,'r*')
plot(time_ACSM,NO3,'*')
plot(time_ACSM,NH4,'y*')
plot(time_ACSM,Chl,'m*')
title([title_ACSM,station])
xlabel('Date')
ylabel(label_ACSM)
ylim([0 40])
box on
dynamicDateTicks([], [], 'dd/mm');

set(gca,'Units','normalized','Position',[0.13 0.11 0.775 0.515]);
nome=['fig/ACSM_' mydir '_Time_series'];

if isunix
    print(fig1,'-depsc',[nome,'.eps']);
    eval(['!convert -density 300 ',nome,'.eps ',nome,'.png'])
    eval(['delete ',nome,'.eps'])
else
    eval(['export_fig ',nome,' -png -transparent'])
end;

%Plot each day

days_ACSM=unique(floor(time_ACSM));

count_days=0;
if exist('days_ACSM_OK','var')==1
    if min(size(days_ACSM_OK))>0
        days_ACSM_OK=unique(days_ACSM_OK);
        for i=1:max(size(days_ACSM))
            if max(days_ACSM(i)==days_ACSM_OK)>0
                count_days=count_days+1;
                rem_days(count_days)=i;
            end;
        end;
        days_ACSM(rem_days)=[];
    end;
else
    days_ACSM_OK=[];
end;

if min(size(days_ACSM))>0
    for i=1:max(size(days_ACSM))

         if days_ACSM(i)<=datenum(2014,04,01)
            fig_name=['fig/IOP1/ACSM_',mydir,'_',datestr(days_ACSM(i),29)];
        elseif days_ACSM(i)>datenum(2014,04,01) && days_ACSM(i)<datenum(2014,08,15)
            fig_name=['fig/April_to_Aug_2014/ACSM_',mydir,'_',datestr(days_ACSM(i),29)];
        elseif days_ACSM(i)>=datenum(2014,08,15) && days_ACSM(i)<=datenum(2014,10,15)
            fig_name=['fig/IOP2/ACSM_',mydir,'_',datestr(days_ACSM(i),29)];
         end

        clear fig1;

        quick_time_ACSM=(time_ACSM-days_ACSM(i)).*24;

        [diff_st,idx_st]=min(abs(quick_time_ACSM));
        [diff_end,idx_end]=min(abs(quick_time_ACSM - 24));


        if diff_st<1/24 && diff_end<1/24
            days_ACSM_OK=[days_ACSM_OK days_ACSM];
        end;

        %Calculate sunrise/sunset
        [rs,t,d,z,a,r]=suncycle(-3.07,60,i);

        rs=rs+8;

        if idx_end>idx_st+30

            if isunix
                fig1 = figure('visible','off');            
            else
                fig1=figure;
            end;
            set(fig1,'InvertHardcopy','on');
            set(gca,'FontSize', 12, 'LineWidth', 2); 

            axis('off');
            axes1 = axes('Parent',fig1,...
            'XTickLabel',{'0','2','4','6','8','10','12','14','16','18','20','22','24'},...
            'XTick',[0 2 4 6 8 10 12 14 16 18 20 22 24]);


            hold on
    %            rectangle('Parent',axes1,'Position',[0,-1000,rs(1),2000],'FaceColor',[0.95 0.95 0.95],'EdgeColor',[0.9 0.9 0.9])
    %            rectangle('Parent',axes1,'Position',[rs(2),-1000,24,2000],'FaceColor',[0.95 0.95 0.95],'EdgeColor',[0.9 0.9 0.9])
            rectangle('Parent',axes1,'Position',[rs(1),-1000,rs(2)-rs(1),200000],'FaceColor',[1 1 0.7],'EdgeColor',[1 1 0.7])
            plot(quick_time_ACSM(idx_st:idx_end),Org(idx_st:idx_end),'g')
            plot(quick_time_ACSM(idx_st:idx_end),SO4(idx_st:idx_end),'r')
            plot(quick_time_ACSM(idx_st:idx_end),NO3(idx_st:idx_end))
            plot(quick_time_ACSM(idx_st:idx_end),NH4(idx_st:idx_end),'y')
            plot(quick_time_ACSM(idx_st:idx_end),Chl(idx_st:idx_end),'m')
            ylim(axes1,[0 max(Org(idx_st:idx_end))])
            xlim(axes1,[0 24])

            title([title_ACSM,station,' - ',datestr(days_ACSM(i),1)])
            xlabel('Time (UTC)')
            ylabel(label_ACSM)
            box on

            set(gca,'Units','normalized','Position',[0.13 0.11 0.775 0.515]);

            if isunix
                print(fig1,'-depsc',[fig_name,'.eps']);
                eval(['!convert -density 300 ',fig_name,'.eps ',fig_name,'.png'])
                eval(['delete ',fig_name,'.eps '])
            else
                eval(['export_fig ',fig_name,' -png -transparent'])
            end;
        end;
    end;
end;


save mat-files/ACSM_T2.mat

%Calc_ACSM_CE