function plot_OPC_v3(fyar, fmon, fday)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modified for function call by Tom Legbandt 26 July, 2021
% Input from OPC_Data.bat
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load GRIMM-OPC measured PM2.5, PM1 and PM10
% Load Concentratio
% Last updated by Yonghua WU, July 15, 2020
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% clear all;   close all; clc;

% data file path in the local computer
fl_path='C:\Monitor_Grimm_OPC\';
logPath='C:\Monitor_Grimm_OPC\OPC_Logs\';
numDate=strcat(fyar,fmon,fday);
strDate=datestr(now, 'dd/mm/yy-HH:MM:ss');
% 20210804_OPC_logfile
logFile = strcat(numDate, '_OPC_logfile.txt')
fid = fopen(strcat(logPath, logFile),'a+');
	fprintf(fid, '%s %s %s %s %s\n', '* Processing Started on:', ' ', strDate, ' using:', ' ' );
	fclose(fid);
	
% data file name
%fl_nm1='OPC_2021-07-21-M.dat';
fl_nm1=strcat('OPC_',fyar,'-',fmon,'-',fday,'-M.dat')
%fl_nm2='OPC_2021-07-21-C.dat';
fl_nm2=strcat('OPC_',fyar,'-',fmon,'-',fday,'-C.dat')

filename=[fl_path fl_nm1]
ddate1=fl_nm1(5:14);
%No headline in the datafile (not work for OPC_2020-07-09-M.dat)
try,
	[A,delimiterOut]=importdata(filename);
	% With the headerline
	% headerlinesIn = 14;
	% [A,delimiterOut]=importdata(filename);
	PMdata=A.data; % PM10[ug/m3]	PM2.5	PM1	Inhalable	Thoracic	Alveolic 
	mdate0=A.textdata; %7/10/2020 11:52:45 PM
	tmp0=datevec(mdate0); % year, mon, day, hr, min, sec
	timehr=tmp0(:,4)+ (tmp0(:,5)+tmp0(:,6)/60)/60;  %hour
	PM25=smooth(PMdata(:,2),10);
	PM1=smooth(PMdata(:,3),10);
	PM10=smooth(PMdata(:,1),10);
catch,
	erreport(lasterror,logPath,logFile),
end

% set various constants, plot parameters, etc
set(0,'DefaultTextFontSize',[15]);   % change font size on all plots
set(0,'DefaultAxesFontSize',[15]);

ymax=max(PM10);

figure(1); hold on;
plot(timehr, PM1,'r--'); hold on;
plot(timehr, PM25,'g-'); hold on;
plot(timehr, PM10,'b--'); hold on;
legend('PM1','PM2.5','PM10',1);
legend boxoff
ymin=0;
ymax=ceil(max(PMdata(:,1)))+1.0;
xmin=floor(min(timehr));
xmax=ceil(max(timehr));
xlim([xmin xmax]);
%ylim([ymin ymax]);
ylim([0 ymax]);
box on;
%xlabel('Time (hour EDT)');
xlabel('Time (hour local)');
ylabel('PM mass (\mug/m^3)');
mt=[ddate1 ' PM-concentration by GRIMM-OPC at CCNY'];
title(mt);
set(gca, 'xtick',xmin:2:xmax);
set(gca,'TickDir','out');
set(gca,'XMinorTick', 'on');
set(gca,'TickDir','out'); 
ddate2=[fl_nm1(5:8) fl_nm1(10:11) fl_nm1(13:14)];
fig_fl_nm=[ddate2 '_OPC_PM'];
fig = gcf;
fig.PaperUnits = 'inches';
fig.PaperPosition = [0 0 7 6];
print(gcf,'-djpeg', fig_fl_nm, '-r300');
%  saveas(gcf,fig_fl_nm,'fig');
fprintf('\n Output file: %s.jpg\n', fig_fl_nm);
	fid = fopen(strcat(logPath, logFile),'a+');
	fprintf(fid, '%s, %s\n', '* * Image saved: ', fig_fl_nm);
	fclose(fid);

filename2=[fl_path fl_nm2];
%No headline in the datafile (not work for OPC_2020-07-09-M.dat
try,
	[A,delimiterOut]=importdata(filename2);
	Cdata=A.data; 
	mdate0=A.textdata; %7/10/2020 11:52:45 PM
	tmp0=datevec(mdate0); % year, mon, day, hr, min, sec
	timehr2=tmp0(:,4)+ (tmp0(:,5)+tmp0(:,6)/60)/60;  %hour
catch,
	erreport(lasterror,logPath,logFile),
end	

dbin=[0.25 0.28	0.3	0.35	0.4	0.45	0.5	0.58	0.65	0.7	0.8	1.0 ...
      1.3	1.6	2	2.5	3	3.5	4	5	6.5	7.5	8.5	10	12.5	15	17.5 ...
      20	25	30	32];
dbin=dbin';
id=1:3:20;
sid=num2str(dbin(id));
Cdata2=Cdata(:,id);

figure(2); hold on;
plot(timehr2, Cdata2); hold on;
set(gca,'yscale','log');
ymin=0;
ymax=ceil(max(Cdata(:,1)))+10.0;
xmin=floor(min(timehr));
xmax=ceil(max(timehr));
xlim([xmin xmax]);
if(ymax<(1e+6));
  ylim([1 2e+6]);
else
  ylim([1 ymax]);
end    
box on;
xlabel('Time (hour local)');
ylabel('Number density (counts/liter)');
mt=[ddate1 ' Particle number density by GRIMM-OPC at CCNY'];
title(mt);
set(gca, 'xtick',xmin:2:xmax);
set(gca,'TickDir','out');
set(gca,'XMinorTick', 'on');
set(gca,'TickDir','out'); 

%legend(sid,'location','NorthEastOutside');    
legend(sid); % set it by default position. 
legend boxoff
 ddate2=[fl_nm1(5:8) fl_nm1(10:11) fl_nm1(13:14)];
 fig_fl_nm=[ddate2 '_OPC_C'];
 fig = gcf;
 fig.PaperUnits = 'inches';
 fig.PaperPosition = [0 0 7 6];
 print(gcf,'-djpeg', fig_fl_nm, '-r300');
 %  saveas(gcf,fig_fl_nm,'fig');
fprintf('\n Output file: %s.jpg\n', fig_fl_nm);
	fid = fopen(strcat(logPath, logFile),'a+');
	fprintf(fid, '%s, %s\n', '* * Image saved: ', fig_fl_nm);
	strDate = datestr(now, 'dd/mm/yy-HH:MM:ss');
	fprintf(fid, '%s %s %s %s\n', '* Processing Ended', ' ', strDate, ' ');	
	fprintf(fid, '%s\n ', '* System call envoked on OPC_Yesterday_FTP.ftp');
	fclose(fid);

% Execute the file transfer program since Task Scheduler will reject it as not a valid Windows application
pause(5)
system('start OPC_Yesterday_FTP.ftp');

% Exit and release memory
close all;
clearvars –global;
quit force;
% End of main function

	function erreport(err,logPath,logFile);
		% openfile
		fid = fopen(strcat(logPath, logFile),'a+');
%		fid = fopen('mwrlogfile.txt','a+'),
		% err = lasterror,
		% write the error to a logfile
		% first line: 
		fprintf(fid,'s%\n','* Errors found:');
		fprintf(fid,'%s\n',err.message),
		txt = err.identifier,
		% following lines: stack
		for e=1:length(err.stack)
			fprintf(fid,'%s in %s at line %i\n',txt,err.stack(e).name,err.stack(e).line),
			% or fprintff(fid,'%s',err.getReport('extended','hyperlinks','off'));
		end
		str = ['* aborted ' fl_nm1 ],
		fprintf(fid,'%s\n',str),
		% close logfile
		fclose(fid),
		close all,
		quit force,
	end
end % end of function