function plotScalarTimeSeries(data,meta)
%---------------------------------------------------------------------
% General:  Generate time series plot of all scalar sensors - retrieveData.m calls this function
% Input:    meta is the metadata structure
%           data is a structure array with seperate structure for each sensor
% Modify Date/Author:  April 27, 2009 - R. Jenkyns
%                      Sept 10, 2012  - B. Biffard, modified to work with new data/meta structures
%---------------------------------------------------------------------
if nargin<2
  error('Error: Missing variables in plotScalarTimeSeries');   
end

n=length(data); % number of sensor time series to plot

set(gcf,'PaperUnits','points','PaperPosition',[0 0 1600 1200]);
axes('Position',[.1 .1 .82 .82])
fs=20;

if data(1).subsampleType==3
    for i=1:length(data), data(i).dat=data(i).datMaximum; end    
end     

for i=1:n
    subplot(n,1,i)   
    if ~isempty(data(i).dat)
        hold on

        if data(i).subsampleType==3
            plot(data(i).time,data(i).dat, '-','LineWidth',2,'Color',[205/255 51/255 51/255])
            plot(data(i).time,data(i).datMinimum, '-','LineWidth',2,'Color',[70/255 130/255 180/255])
        else
             if length(data(i).dat)>4
                plot(data(i).time,data(i).dat, '-','LineWidth',2,'Color',[70/255 130/255 180/255])
             else
                 plot(data(i).time,data(i).dat, '.','MarkerSize',20,'MarkerFaceColor',[70/255 130/255 180/255],'MarkerEdgeColor',[70/255 130/255 180/255],'Color',[70/255 130/255 180/255])
             end    
        end
        %plot(t,dat, '-','LineWidth',2,'MarkerSize',k,'MarkerFaceColor',[0 0 128/255],'MarkerEdgeColor',[0 0 128/255],'Color',[70/255 130/255 180/255])
        %steel blue, [70/255 130/255 180/255]; light steel blue, [176/255 196/255 222/255]
        datetick
        ylabel([char(data(i).sensorName), ' (',char(data(i).units),')'],'FontSize',fs)
        box on
        
        % determine vertical limits
        ylimits=[nanmin(data(i).dat) nanmax(data(i).dat)];
        if data(i).subsampleType==3
            ylimits(1)=nanmin(data(i).datMinimum);
        end    
        ydiff=ylimits(2)-ylimits(1);
        same=0;
        if ydiff~=0 && ylimits(1)~=0 && abs(ydiff/ylimits(1))<0.00001
            same=1;
        elseif ydiff==0
            same=1;
        end
        if same
            ylimits(2)=ylimits(1)+1;
            ylimits(1)=ylimits(1)-1;
        end   
        ytit=ylimits(2);
        
        %determine horizontal limits
        tlimits=[data(i).time(1) data(i).time(end)];
        if length(data(i).time)==1, tlimits(2)=tlimits(1)+1; end
        xtit=tlimits(2);
         
        if i==1
            tit1 = [meta.deviceDetails(1).deviceName,' (Device ID: ',num2str(meta.deviceDetails(1).deviceID) ')  \bullet  ',data(i).sensorName,' (Sensor ID: ',num2str(data(i).sensorID), ')'];
            if meta.deviceDetails(1).deviceID==11013 %AGO heat probe
                tit2=[num2str(meta.location(1).siteName),'  \bullet  ',num2str(data(i).lat_degrees),'^{\circ} N \bullet  ',num2str(-data(i).lon_degrees),'^{\circ} W  \bullet  ',...
                    num2str(data(i).depth_metres),' m '];
            elseif meta.deviceDetails(1).deviceID==21304 %BHT
                tit2=[num2str(meta.location(1).siteName),'  \bullet  ',num2str(meta.location(1).lat_degrees),'^{\circ} N \bullet  ',num2str(-meta.location(1).lon_degrees),'^{\circ} W  \bullet  ',...
                    num2str(meta.location(1).depth_metres),' m \bullet  ', 'Depth Below Seafloor ',num2str(data(i).sensorBoreholeDepth_metres), ' m'];
            else
                tit2 = [num2str(meta.location(1).siteName),'  \bullet  ',num2str(meta.location(1).lat_degrees),'^{\circ} N \bullet  ',num2str(-meta.location(1).lon_degrees),'^{\circ} W  \bullet  ',...
                     num2str(meta.location(1).depth_metres),' m '];
            end
            title({tit1;tit2},'FontSize',fs,'HorizontalAlignment','right','Position',[xtit,ytit]);   
            if ~isnan(data(i).subsamplePeriod_sec)
                if data(i).subsamplePeriod_sec==3600,per='Hourly ';
                elseif data(i).subsamplePeriod_sec==60,per='Minute ';
                elseif data(i).subsamplePeriod_sec==24*3600,per='Daily ';
                elseif data(i).subsamplePeriod_sec==7*24*3600,per='Weekly ';
                end    
                tit3=[per, data(i).subsampleType,' Data'];
                title({tit1;tit2;tit3},'FontSize',fs,'HorizontalAlignment','right','Position',[xtit,ytit]);
            end
            axis([tlimits ylimits])
            grid on
            set(gca,'FontSize',fs)  
        end
        %text(max(data(i).time),min(data(i).dat),['Plot generated at ',datestr(now),' '],...
        %    'HorizontalAlignment','right','VerticalAlignment','bottom','FontAngle','italic','BackgroundColor','white','FontSize',15);
    else
        display(['There is no data for ' data(i).sensorName ' in this range to plot.'])
    end      
end
xlabel ({'Time (UTC)';[datestr(data(i).time(1)),'  to  ',datestr(data(i).time(end))]},'FontSize',fs)
set(gca,'FontSize',fs)  
set(gcf, 'PaperUnits', 'inches')


