%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Written by Joel Brito - 14/Aug/2014 %
% %
%This routine reads the TEOM ascii file and plots daily values of fine%
% and coarse particles for a quick look. It saves a .mat 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
clc
close all
[tmp,mydir]=fileparts(pwd);

correct_clock=0;

T0_T2=0;
%%
if strcmp(mydir,'A_T2')
Site='T2';
count=0;
fl_dir='CPC3772\raw\';
fl=dir([fl_dir,'*CPC*.csv']);
T0_T2=1;
correct_clock=1;
elseif strcmp(mydir,'A_T0_ATTO')
station=' -T0a';
Site='T0a';
fid = fopen('SMPS_TSI\Raw\Full_Data_ATTO_SMPS.txt');
T0_T2=1;
end;

if T0_T2==1
%%
if min(size(fl))>0
for fl_number=1:max(size(fl))

fl_dir,fl(fl_number).name

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

if n_delimiters>12
if max(size(str2num(TextLine(1:idx_delimiter(1)-1))))==1
try
dummy_time_CPC=datenum(TextLine(idx_delimiter(1)+1:idx_delimiter(3)-1),'mm/dd/yy,HH:MM:SS');
time_error=0;
catch error_message
time_error=1;
end

if time_error==0
avg_time=str2num(TextLine(idx_delimiter(4)+1:idx_delimiter(5)-1));
for i=12:n_delimiters
if i<n_delimiters
dummy_CPC=str2num(TextLine(idx_delimiter(i)+1:idx_delimiter(i+1)-1));
else
dummy_CPC=str2num(TextLine(idx_delimiter(i)+1:end));
end;
if max(size(dummy_CPC))==1
count=count+1;
CPC(count)=dummy_CPC;
dummy_time_CPC=dummy_time_CPC + avg_time./(60.*24.*60);
time_CPC(count)=dummy_time_CPC;
% clc
% CPC(count)
% datestr(time_CPC(count))
end;
end;

end;
end
end;
% get next line of text
TextLine = fgetl(fid);

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

fl=dir([fl_dir,'*.dat']);

if min(size(fl))>0
for fl_number=1:max(size(fl))

fl_dir,fl(fl_number).name

fid = fopen([fl_dir,fl(fl_number).name]);
total_lines=0;
TextLine = fgetl(fid);

while ~feof(fid)
total_lines=total_lines+1;
n_delimiters=0;
size_text=max(size(TextLine));
i=2;
while i<size_text
if strcmp(TextLine(i),',')
n_delimiters=n_delimiters+1;
idx_delimiter(n_delimiters)=i;
end;
i=i+1;
end

if total_lines==2
dummy_time_CPC=datenum(TextLine(idx_delimiter(1)+6:end),'mmm dd HH:MM:SS yyyy');
end;
if total_lines==3
time_step=str2num(TextLine(1));
end;

if n_delimiters==4 && total_lines>4
dummy_time_CPC=dummy_time_CPC+time_step./(24.*60.*60);
dummy_CPC=str2num(TextLine(idx_delimiter(1)+1:idx_delimiter(2)-1));
if max(size(dummy_CPC))==1
count=count+1;
CPC(count)=dummy_CPC;
time_CPC(count)=dummy_time_CPC;
end;
end;
% get next line of text
TextLine = fgetl(fid);

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

else
%%
station=' - T3';
Site='T3';
fdir='CPC3772/raw/';
f=dir([fdir,'*.cdf']);
CPC=[];
time_CPC=[];

for i=1:max(size(f));
disp(f(i).name);

base_time=ncread([fdir,f(i).name],'base_time'); %units = 'seconds since 1970-1-1 0:00:00 0:00'
time_offset=ncread([fdir,f(i).name],'time_offset'); %units = 'seconds since 2014-01-26 00:00:00 0:00'
CPC=[CPC;double(ncread([fdir,f(i).name],'concentration'))]; %Total concentration
time_CPC=[time_CPC;datenum(1970,1,1)+double(base_time)./(60*60*24) + time_offset./(60*60*24)]; %#ok<AGROW>
end;

end;
%%
[time_CPC,idx]=unique(time_CPC);
CPC=CPC(idx);

if correct_clock==1
[a,idx]=min(abs(time_CPC-datenum(2014,07,01)));
time_CPC(1:idx)=time_CPC(1:idx)+175./(24*60);
time_CPC(idx+1:end)=time_CPC(idx+1:end)+120./(24*60);

[a,idx]=min(abs(time_CPC-datenum(2014,03,12)));
time_CPC(1:idx)=time_CPC(1:idx)-5./(24*60);
end;

%% Apply level 1 filter
orig_size=max(size(time_CPC));

time_CPC(CPC<50|CPC>1e4)=[];
CPC(CPC<50|CPC>1e4)=[];

%------------------------------------------------------------
%% Read Silica Gel change times

if T0_T2==1
fl_dir='Logbook\';
count=0;
clear idx_delimiter
fid = fopen([fl_dir,'LogBook_Troca_Silica_SMPS_CPC.csv']);
while ~feof(fid)

TextLine = fgetl(fid);
delimiters=0;
size_text=max(size(TextLine));
i=1;
while i<size_text
if strcmp(TextLine(i),',')
delimiters=delimiters+1;
idx_delimiter(delimiters)=i;
end
i=i+1;
end

if delimiters==2
try
dummy_time_st=datenum(TextLine(1:idx_delimiter(1)-1),'dd/mm/yyyy HH:MM');
dummy_time_end=datenum(TextLine(idx_delimiter(1)+1:idx_delimiter(2)-1),'dd/mm/yyyy HH:MM');
time_error=0;
catch error_message
time_error=1;
end

if time_error==0;
count=count+1;
time_Silica_st(count)=dummy_time_st;
time_Silica_end(count)=dummy_time_end;
end;
end
end;
status=fclose(fid);

for i=1:max(size(time_Silica_st));
CPC(time_CPC>time_Silica_st(i)&time_CPC<time_Silica_end(i))=[];
time_CPC(time_CPC>time_Silica_st(i)&time_CPC<time_Silica_end(i))=[];
end;
end

%% Exporting data
Instrument='CPC';
Model='3772';
serial_num='N/A';
Val_level='1';
Proc_vers='1.0';

labels{1}='Total concentration';

units{1}='(cm-3)';

export_LFA_GoAmazon(labels,units,Instrument,Model,serial_num,Site,Val_level,Proc_vers,orig_size,time_CPC,CPC)

eval(['save mat-files/Level1/',Instrument,'_',Model,'_L',Val_level,'.mat'])

%process_CPC_L2

%process_CPC_L3