close all;
clear all;

disp('time series for red line trax event')
%colormap used in meso_trax
%cmap = [ 0,255,0; 0,153,0; 255,255,0; ...
%         255,221,0; 255,170,0; 255,102,0; ...
%         255,51,0; 255,0,0; 153,0,0; ...
%         204,0,51; 255,0,255; 255,255,255];
     %colormap used here
%cmap = [ 0,255,0; 0,153,0; 255,230,0; ...
%         255,190,0; 255,150,0; 255,110,0; ...
%         255,70,0; 255,0,0; 200,0,0; ...
%         150,0,0; 100,0,0; 50,0,0];
%c255 = ones(12,3)*255;
%cmap = cmap ./c255;
%color thresholds used in meso_trax
%ct = [0;6;12;20;28;35.5;45.5;55.5;85.5;115.5;150.5;300];
%color thresholds used here
%ct = [0;12;24;30;36;45;55;65;75;85;100;120];
%ct = [0;6;12;18;24;30;36;45;55;65;75;85];
%cts = num2str(ct,'%3d');
%colormap used in meso_trax
%cmap = [ 0,255,0; 0,153,0; 255,255,0; ...
%         255,221,0; 255,170,0; 255,102,0; ...
%         255,51,0; 255,0,0; 153,0,0; ...
%         204,0,51; 255,0,255; 255,255,255];
     %colormap used here
%cmap = [ 0,255,0; 0,153,0; 255,230,0; ...
%         255,190,0; 255,150,0; 255,110,0; ...
%         255,70,0; 255,0,0; 200,0,0; ...
%         150,0,0; 100,0,0; 50,0,0];
colorlist = ['00FFFF';'00CCFF';'0099FF';'00FF00';'009900';'006600';'FFFF00';'FFE600';'FFCD00';'FF7E00';'FF5000';'FF0000';'990000';'CC0033';'FF00FF']
cmap = hex2rgb(colorlist);
c255 = ones(15,3)*255;
cmap = cmap ./c255;
%color thresholds used in meso_trax
%ct = [0;6;12;20;28;35.5;45.5;55.5;85.5;115.5;150.5;300];
ct = [10,20,30,40,50,60,65,70,75,85,95,105,110,115,500];
cts = num2str(ct,'%3d');
        
        %colortextlist = ['#000000','#000000','#FFFFFF','#000000','#FFFFFF','#FFFFFF','#000000','#000000','#000000','#000000','#000000','#FFFFFF','#FFFFFF','#FFFFFF','#FFFFFF']
       
        %colorminlist = [ 0,10,20,30,40,50,60,65,70,75,85, 95,105,110,115]
        
%key locations on red line
lred = [0,1,2.8,4.4,6.2,9,12.1,14.4,17.8,20.4,23.9,...
    25.5,29.0,32.5,36.9,40.4];
nred = {'HOSP','WASA','1300E','700E','MAIN','1300S',' I80','3300',...
    '4500','5400',' I15','7400','REDW','BANG','DIAG','DAYB'};
    
%data = csvread('trax_comp_jja_2015.csv');
data = csvread('trax_comp.csv');
%columns in the trax_comp_jja_2015.csv file
%complete time string
%all times are local
%01:time
%02:year
%03:month
%04:day
%05:hr
%06:min
%07:lat of trax
%08:trax lon
%09:trax type for day 0 somewhere; 1 red; 2 green; 3 depot 4 no loc info
%10:distance on red
%11:distance on green
%12:whether at depot dps =0
%13:trx oz
%14:naa oz
%15:mtmet oz
%16:hawthorne oz
%17:train speed
%18:dist to naa
%19:dist to mtm
%20:dist to haw
%21:oz diff rel to  naa
%22:oz diff rel to mtmet
%23:oz diff rel to haw
%24:rh of trax envir
%25:tmp of trax envir
%26:trax pm
%train speed (m/s)
%dist to naa (km)
%dist to mtm (km)
%dist to haw (km)

%create time vectors
n= length(data(:,1));
t= datenum(data(:,2),data(:,3),data(:,4),data(:,5),data(:,6),zeros(n,1));
tv= datevec(t);

%define first and last day of event
yr1 = 2015;
mo1 = 8;
dy1 = 26;
yr1 = 2015;
dt1 = datenum(yr1,mo1,dy1,0,0,0);
yr2 = 2015;
mo2 = 8;
dy2 = 27;
yr2 = 2015;
dt2 = datenum(yr2,mo2,dy2,0,0,0);
times = find(t>=dt1&t<=dt2);

%load up times, loc, conc trax, conc haw for event
etm = t(times);
nt = length(etm);
elc = data(times,10);
etr = data(times,13);
eth = data(times,16);

%plot trax concentrations along red line
figure(1)
fh = figure(1);    
set(fh,'PaperPositionMode','manual',...
'PaperPosition',[0 0 10 7.5],'PaperSize',[10 7.5],...
'PaperUnits','inches');
set (fh, 'color', 'white');
ax1 = axes('YDir','reverse','YTick',lred,'YTickLabel',nred,...
    'FontSize',10,'Position',[.075 .25 .85 .65],...
    'XAxisLocation','top','Xlim',[etm(1) etm(nt)],...
    'YLim',[-1 lred(length(lred))],'color','[.93 .93 .93]');
datetick(ax1,'x','mmddhh','keepticks','keeplimits')

%draw lines at key points
for l = 1:length(lred)
line([etm(1) etm(nt)],[lred(l) lred(l)],'LineStyle','-','Color','k','Parent',ax1);
hold on;
end

%plot so more important values on top
p_ord = [8 9 7 10 6 11 5 12 4 13 3 14 2 1];
for nc=1:length(p_ord)
    ncp = p_ord(nc);
    %find between values 
    cvi = find(etr>=ct(ncp)&etr<=ct(ncp+1));
    no_vals = length(cvi);
    if(no_vals>0)
        for n=1:no_vals
            line(etm(cvi(n)),elc(cvi(n)),'LineStyle','none',...
                'Marker','o','MarkerEdgeColor',cmap(ncp,:),...
        'MarkerFaceColor',cmap(ncp,:),'MarkerSize',3.5,'Parent',ax1);
        end
    end
end
%put line on right axis
line([etm(nt) etm(nt)],[lred(1) lred(l)],'LineStyle','-','Color','k','Parent',ax1);


%create colorbar when in depot. 
%dbar = datenum(2015,01,12,0,0,0);
%toff = 2/24;
%dbar = datenum(2015,01,27,1,0,0);
%toff = .5/24;
%for i=1:length(ct)
%loc1 = (i-1)*2.5+2;
%loc2 = loc1+1.25;
%text(dbar,loc,cts(i,:),'Color',cmap(i,:),'Parent',ax1,...
%    'EdgeColor','none','Backgroundcolor',[1 1 1],...
%    'FontWeight','bold');
%text(dbar,loc1,cts(i,:),'Color','k','Parent',ax1,...
%    'EdgeColor','none','Backgroundcolor',[1 1 1],...
%    'FontWeight','bold');
%line(dbar+toff,loc2,'LineStyle','none',...
%            'Marker','o','MarkerEdgeColor',cmap(i,:),...
%    'MarkerFaceColor',cmap(i,:),'MarkerSize',5,'Parent',ax1);
%end

%plot time series of trax and hawthorne at bottom 
ax2 = axes('FontSize',10,'Position',[.075 .05 .85 .18],...
    'XAxisLocation','bottom','Xlim',[etm(1) etm(nt)],...
    'YLim',[0 95],'color','[.93 .93 .93]');
%ax4 = axes('FontSize',9,'Position',[.075 .05 .85 .18],...
%    'XAxisLocation','bottom','Xlim',[etm(1) etm(nt)],...
%    'YLim',[0 100],'YAxisLocation','right','YTickLabel','');
datetick(ax2,'x','mmddhh','keepticks','keeplimits')
line(etm(cvi(n)),elc(cvi(n)),'LineStyle','none',...
            'Marker','o','MarkerEdgeColor',cmap(ncp,:),...
    'MarkerFaceColor',cmap(ncp,:),'MarkerSize',4,'Parent',ax1);
line(etm,etr,'LineStyle','-','Color',[.25 .25 .25],'Parent',ax2,'LineWidth',1.5);
hold on;
line(etm,eth,'LineStyle','-','Color',[0 153/255 0],'Parent',ax2,'LineWidth',1.5);
%text(.10,.65,'TRAX','Color',[.1 .1 .1],'Parent',ax2,'Units','normalized');
%text(.10,.50,'Hawthorne','Color',[0 153/255 0],'Parent',ax2,'Units','normalized');
text(.10,.85,'TRAX','Color',[.1 .1 .1],'Parent',ax2,'Units','normalized');
text(.10,.70,'Hawthorne','Color',[0 153/255 0],'Parent',ax2,'Units','normalized');
line([etm(1) etm(nt)],[70 70 ],'LineStyle','-','Color','k','Parent',ax2);
%put line on right axis
line([etm(nt) etm(nt)],[0 100],'LineStyle','-','Color','k','Parent',ax2);
%save as png
print(gcf,'-dpng','08_26_15trax_event_redline.png');
%print(gcf,'-dpng','trax_event_redline_2.png');