% Processa dados do monitor CO/CO2/etanol INNOVA 1412i (IAG)
% Experimento Diadema Trailer 2016-2017
% Output: 
%     raw: matdate(utc); CO2(ppm); CO(ppm); Ethanol(ppm); H2O(mg/m3); Pressure(kPa)  
%
% ---------------- Parametros -----------------------
caminho='C:\A\Diadema2016\data_trailer_2016\CO_INNOVA1412i\meas\';
caminho1='C:\A\Diadema2016\processados\CO_INNOVA\';
fuso=0; % fuso horario para plotar hora local
tit='Diadema-2016'; 
Site='Diadema_2016';
filtmanut=true;
corrigirtempo=true; % Corrige horário, de acordo com o logbook.
lv1=false;
%
% inicio e termino de periodos de manutencao (horario em utc): removidos das analises
if(filtmanut)
    C1=xlsread('C:\A\Diadema2016\logbook_trailer_Diadema2016.xlsx','todos','A4:M135');
    %C2=xlsread('C:\A\Diadema2016\logbook_trailer_Diadema2016.xlsx','co','xxx');
    manut(:,1)=datenum(C1(:,1:6));
    manut(:,2)=datenum(C1(:,7:12));
    manut(:,3)=C1(:,13);    
end
%
% inicio e termino de periodo de zoom para fazer grafico
zoom1=[datenum(2016,11,22,0,0,0) datenum(2017,3,15,0,0,0)];
zoom2=[datenum(2017,03,05,0,0,0) datenum(2017,3,15,0,0,0)];%
% ---------------- Fim dos parametros --------------------
%
raw=zeros(1,6);
files=dir(caminho);
disp('-----Reading raw data files')
for ifile=3:size(files,1)
    nome=getfield(files, {ifile,1}, 'name');
    disp(nome)    
    fid=fopen(strcat(caminho,nome));
    % --------- Retrive data from raw file ---------
    header=textscan(fid,'%s',17,'delimiter',';');   % skipping header
    C=textscan(fid,'%s%f%f%f%*s%*s%f%f%s%s%s%s%s%s%s%s%s','delimiter',';');
    fclose(fid);    
    raw_=zeros(size(C{1,1},1),6);
    for j=2:6 % somente numeros
        raw_(:,j)=C{1,j};
    end    
    raw_(:,1)=datenum(C{1,1},'dd-mm-yyyy HH:MM:SS');
    %
    raw=cat(1,raw,raw_);
    clear C raw_    
end
%
raw(1,:)=[];
raw=sortrows(raw,1); % sort data based on day of year
%
% 
if(corrigirtempo) % correção segundo o logbook
    %somar 2h58min em amostras com timestamp até 27-01-2017 10:46:06
    aux1=find(raw(:,1)<=datenum(2017,1,27,10,47,00));    
    raw(aux1,1)=raw(aux1,1)+datenum(0,0,0,2,58,0);    
end
%
save([caminho1,tit,'_CO_level0.mat'],'raw','-mat')
%
% Remove maintanance periods from dataset
if(filtmanut)
    raworig=raw;
    for i=1:size(manut,1)
        aux=find(raw(:,1)>=manut(i,1) & raw(:,1)<=manut(i,2));
        if(manut(i,3)==0)            
            raw(aux,:)=[]; % remove maintanance periods from dataset
% %         else
% %             % detrend baseline (ozone_detrend.m)
% %             if(manut(i,4)==0)
% %                 raw(aux,2)=raw(aux,2)+(-polyval(manut(i,4:5),raw(aux,1)));
% %             else
% %                 raw(aux,2)=raw(aux,2)+(polyval(manut(i,4:5),raw(aux(1),1))-polyval(manut(i,4:5),raw(aux,1)));
% %             end
        end        
    end
    figure,plot(raworig(:,1),raworig(:,3),'r.'),hold on,grid on
    plot(raw(:,1),raw(:,3),'b.')
    dynamicDateTicks
    clear aux   
    save([caminho1,tit,'_CO_clean.mat'],'raw','-mat')
end
%
% ------ Basic plots ---------------------------
if(basicplots)
    figure
    subplot(3,1,1),plot(raw(:,1)+(fuso/24),raw(:,2),'g.')
    title(tit,'FontSize',14)
    ylabel('CO_2 (ppm)','FontSize',14),grid on, axis tight
    dynamicDateTicks
    subplot(3,1,2),plot(raw(:,1)+(fuso/24),raw(:,3),'k.')
    ylabel('CO (ppm)','FontSize',14),grid on, axis tight
    dynamicDateTicks
    subplot(3,1,3),plot(raw(:,1)+(fuso/24),raw(:,4),'b.')
    ylabel('Ethanol (ppm)','FontSize',14),grid on, axis tight
    dynamicDateTicks
    saveas(gcf,strcat(caminho1,tit,'_CO2_CO_ethanol_tseries'),'jpg')
    %
    figure
    subplot(2,1,1),plot(raw(:,1)+(fuso/24),raw(:,5),'c.')
    title(tit,'FontSize',14)
    ylabel('H_2O (mg.m^-^3)','FontSize',14),grid on, axis tight
    dynamicDateTicks
    subplot(2,1,2),plot(raw(:,1)+(fuso/24),raw(:,6),'r.')
    ylabel('Pressure (kPa)','FontSize',14),grid on, axis tight
    dynamicDateTicks
    saveas(gcf,strcat(caminho1,tit,'_H2O_pressure'),'jpg')
    %
    figure
    subplot(1,3,1),plot(mod(raw(:,1)+(fuso/24),1),raw(:,2),'g.')
    title(tit,'FontSize',14)
    ylabel('CO_2 (ppm)','FontSize',14),grid on, axis tight,xlabel('UTC Time','FontSize',14)
    set(gca,'XTick',[0.125 0.25 0.375 0.5 0.625 0.75 0.875 1.0])
    set(gca,'XTickLabel',{'3:00';'6:00';'9:00';'12:00';'15:00';'18:00';'21:00';'0:00'})
    subplot(1,3,2),plot(mod(raw(:,1)+(fuso/24),1),raw(:,3),'k.')
    ylabel('CO (ppm)','FontSize',14),grid on, axis tight,xlabel('UTC Time','FontSize',14)
    set(gca,'XTick',[0.125 0.25 0.375 0.5 0.625 0.75 0.875 1.0])
    set(gca,'XTickLabel',{'3:00';'6:00';'9:00';'12:00';'15:00';'18:00';'21:00';'0:00'})
    subplot(1,3,3),plot(mod(raw(:,1)+(fuso/24),1),raw(:,4),'b.')
    ylabel('Ethanol (ppm)','FontSize',14),grid on, axis tight,xlabel('UTC Time','FontSize',14)
    set(gca,'XTick',[0.125 0.25 0.375 0.5 0.625 0.75 0.875 1.0])
    set(gca,'XTickLabel',{'3:00';'6:00';'9:00';'12:00';'15:00';'18:00';'21:00';'0:00'})
    saveas(gcf,strcat(caminho1,tit,'_CO2_CO_ethanol_dcycle'),'jpg')
    %
    aux=find(raw(:,1)>=zoom1(1) & raw(:,1)<=zoom1(2));
    figure
    subplot(3,1,1),plot(raw(aux,1)+(fuso/24),raw(aux,2),'g.')
    title(tit,'FontSize',14)
    ylabel('CO_2 (ppm)','FontSize',14),grid on, axis tight
    set(gca,'XTick',zoom1(1):2:zoom1(2))
    set(gca,'XTickLabel',datestr(zoom1(1):2:zoom1(2),'dd/mm'))
    set(gca,'XLim',[zoom1(1) zoom1(2)])
    subplot(3,1,2),plot(raw(aux,1)+(fuso/24),raw(aux,3),'k.')
    ylabel('CO (ppm)','FontSize',14),grid on, axis tight
    set(gca,'XTick',zoom1(1):2:zoom1(2))
    set(gca,'XTickLabel',datestr(zoom1(1):2:zoom1(2),'dd/mm'))
    set(gca,'XLim',[zoom1(1) zoom1(2)])
    subplot(3,1,3),plot(raw(aux,1)+(fuso/24),raw(aux,4),'b.')
    ylabel('Ethanol (ppm)','FontSize',14),grid on, axis tight
    set(gca,'XTick',zoom1(1):2:zoom1(2))
    set(gca,'XTickLabel',datestr(zoom1(1):2:zoom1(2),'dd/mm'))
    set(gca,'XLim',[zoom1(1) zoom1(2)])
    saveas(gcf,strcat(caminho1,tit,'_CO2_CO_ethanol_zoom1'),'jpg')
    %
    aux=find(raw(:,1)>=zoom2(1) & raw(:,1)<=zoom2(2));
    figure
    subplot(3,1,1),plot(raw(aux,1)+(fuso/24),raw(aux,2),'g.')
    title(tit,'FontSize',14)
    ylabel('CO_2 (ppm)','FontSize',14),grid on, axis tight
    set(gca,'XTick',zoom2(1):2:zoom2(2))
    set(gca,'XTickLabel',datestr(zoom2(1):2:zoom2(2),'dd/mm'))
    set(gca,'XLim',[zoom2(1) zoom2(2)])
    subplot(3,1,2),plot(raw(aux,1)+(fuso/24),raw(aux,3),'k.')
    ylabel('CO (ppm)','FontSize',14),grid on, axis tight
    set(gca,'XTick',zoom2(1):2:zoom2(2))
    set(gca,'XTickLabel',datestr(zoom2(1):2:zoom2(2),'dd/mm'))
    set(gca,'XLim',[zoom2(1) zoom2(2)])
    subplot(3,1,3),plot(raw(aux,1)+(fuso/24),raw(aux,4),'b.')
    ylabel('Ethanol (ppm)','FontSize',14),grid on, axis tight
    set(gca,'XTick',zoom2(1):2:zoom2(2))
    set(gca,'XTickLabel',datestr(zoom2(1):2:zoom2(2),'dd/mm'))
    set(gca,'XLim',[zoom2(1) zoom2(2)])
    saveas(gcf,strcat(caminho1,tit,'_CO2_CO_ethanol_zoom2'),'jpg')
    %
    % Ciclo diurno médio
    first = 0;   % first averaging time
    last = 23.5/24;  % last averaging time
    delta = 30/60/24;     % 30min
    mdata=cat(2,mod(raw(:,1),1),raw(:,2:5));
    tol=0;
    [stat,~]=fmedia_qualquer_coisa2(mdata,first,last,delta,tol);
    figure
    subplot(2,2,1),errorbar(stat(:,1),stat(:,2),stat(:,3),'g.')
    dynamicDateTicks
    set(gca,'XLim',[0 1])
    ylabel('CO_2 (ppm)','FontSize',14),grid on,xlabel('UTC Time','FontSize',14)
    title([tit,' Avg Diurnal Cycle'],'FontSize',14)
    subplot(2,2,2),errorbar(stat(:,1),stat(:,10),stat(:,11),'k.')
    dynamicDateTicks
    set(gca,'XLim',[0 1])
    ylabel('CO (ppm)','FontSize',14),grid on,xlabel('UTC Time','FontSize',14)
    subplot(2,2,3),errorbar(stat(:,1),stat(:,18),stat(:,19),'b.')
    dynamicDateTicks
    set(gca,'XLim',[0 1])
    ylabel('Ethanol (ppm)','FontSize',14),grid on,xlabel('UTC Time','FontSize',14)
    subplot(2,2,4),errorbar(stat(:,1),stat(:,26),stat(:,27),'c.')
    dynamicDateTicks
    set(gca,'XLim',[0 1])
    ylabel('H_2O (mg.m^-^3)','FontSize',14),grid on,xlabel('UTC Time','FontSize',14)
    saveas(gcf,strcat(caminho1,tit,'NO2_dcycle_level0'),'jpg')
end
%
%
%
% ----------- Level1 ------------------------------------------------
if(lv1)    
    % matdate(utc); CO2(ppm); CO(ppm); Ethanol(ppm); H2O(mg/m3); Pressure(kPa)  
    C=load('C:\A\Diadema2016\processados\CO_INNOVA\Diadema-2016_CO_clean.mat');
    raw_clean=C.raw;
    C=load('C:\A\Diadema2016\processados\CO_INNOVA\Diadema-2016_CO_level0.mat');
    raw_level0=C.raw;
    tit='Diadema-2016';
    %
    raw_clean(:,3)=raw_clean(:,3)*1000; % CO ppm to ppb
    orig_size=size(raw_level0,1);    
    %
    % Save Level 1 data 
    Instrument='INNOVA';
    Model='1412i';
    serial_num='712-073';
    Site=tit;
    Val_level='1';
    Proc_vers='1.0';
    labels={'CO2';'CO';'Ethanol';'H2O'};
    units={'(ppm)';'(ppb)';'(ppm)';'(mg.m-3)'};
    export_LFA_GoAmazon_jan2015(labels,units,Instrument,Model,serial_num,Site,Val_level,Proc_vers,orig_size,raw_clean(:,1),raw_clean(:,2:5))
end
%

