% datadir = '\\sosiknas1\Instruments\SUNA\data\20190414_AR34B\LTER_SUNA\converted_raw_data';
% datadir = 'C:\Users\emily\Desktop\SUNA\data\20190404_AR34\20190404_AR34A\LTER_SUNA_data\converted_raw_data';
% datadir = 'C:\Users\emily\Desktop\SUNA\data\20190414_AR34B\LTER_SUNA1227\converted_raw_data';
% datadir = 'C:\Users\emily\Desktop\SUNA\data\20190414_AR34B\OOI_SUNA0662\converted_raw_data';
% datadir = 'C:\Users\emily\Desktop\SUNA\data\20190512_RB1904\converted_raw_data';
% datadir = 'C:\Users\emily\Desktop\SUNA\data\20190705_TN368\converted_raw_data';
datadir = 'C:\Users\emily\Desktop\SUNA\data\20190705_TN368\REMUS_SUNA1178\converted_raw_dataUCIver2';
files = dir([datadir '\*SUNA*.csv']);
files = {files.name}';

answer = input('Would you only like to plot the most recent file? (y/n)','s');
if strmatch(lower(answer),'y')
    files = files(end);
else
    answer2 = input('Would you like to plot all of the files? (y/n)','s');
end
if strmatch(lower(answer2),'y')
    data = nan(200000,12);
    matdate = nan(20000000,1);
else
    answer3 = input('Would you like to plot a subset of the files? (y/n)','s');
    if strmatch(lower(answer3),'y')
    for n=1:length(files), fprintf('%2d  %s\n',n,char(files(n))),end
    i = input ('Enter number(s) corresponding to file(s) you want to plot. \n It can be an individual file (ex. 1), \n a range of files files (ex. [1:7]), \n or a list of specific files (ex. [1 4 7]) \n');
    files = files(i);
    data = nan(200000,12);
    matdate = nan(20000000,1);
    end
end
clear answer* i

cast_time_duration = nan(50,1); 
batt_stats=nan(50,8);
whennewfile = zeros(20000000,1);
lastrow = 0;
files2=cell(17,1);
file_count=1;
%'start_time','end_time','clock_dur','start_v','end_v','v_drop','start_amp','end_amp'
header_battstats={'start_time(min)','end_time','lamptime_dur(min)','start_v','end_v','v_drop','start_amp','end_amp'};
for count = 1:length(files)
    filename = [datadir '\' char(files(count))];
    files2(file_count) =  files(count);
    disp(filename)
    header = importSUNA_converted_raw_header(filename);
    if length(header)~=288, error('Check that imported file was convereted with all data columns possible. Should be 288.'), end
    if ~strmatch(header(2),'DATE(UTC+00:00)'), error('Check which column is date in SUNA file. We assumed column 2 but header not matching up.'); end
    if ~strmatch(header(3),'TIME(UTC+00:00)'), error('Check which column is time in SUNA file. We assumed column 3 but header not matching up.'); end
    if ~strmatch(header(276),'CURRENT'), error('Check which column is CURRENT in SUNA file. We assumed column 276 but header not matching up.'); end
    [Index,DATEUTC0000,TIMEUTC0000,NITRATE_UM,DARK_AVG,T_INT,T_SPEC,T_LAMP,LAMP_TIME,HUMIDITY,VOLT_MAIN,VOLT_12,VOLT_5,CURRENT] = import_SUNA_converted_newdate_wlamptime(filename);
    temp = [Index, NITRATE_UM, DARK_AVG, T_INT, T_SPEC, T_LAMP, HUMIDITY, VOLT_MAIN, VOLT_12, VOLT_5, CURRENT, LAMP_TIME];
    tempmatdate= datenum([cell2mat(DATEUTC0000) repmat(' ',length(DATEUTC0000),1) cell2mat(TIMEUTC0000)]);
    matdate(lastrow+1:lastrow+size(temp,1)) = tempmatdate;
    cast_time_duration(count) = (LAMP_TIME(end)-LAMP_TIME(1))/60;
    batt_stats(count,:) = [tempmatdate(1) tempmatdate(end) cast_time_duration(count) VOLT_MAIN(1) VOLT_MAIN(end) (VOLT_MAIN(end)-VOLT_MAIN(1)) CURRENT(1) CURRENT(end)];
    data(lastrow+1:lastrow+size(temp,1),:) = temp;
    whennewfile(lastrow+1) = 1;
    if strcmp(files(count),'A0000038-SUNA1227.csv')
        file_count=file_count+1;
        files2(file_count) =  files(count);
        ind1 = find(diff(tempmatdate)>0.014);
        whennewfile(lastrow+ind1) = 1;
        files2 = [files2; files(count)];
        clear ind1
    end
    lastrow = lastrow+size(temp,1);
    file_count=file_count+1;
end
clear Index count *UTC* NITRATE_UM DARK_AVG T_INT T_SPEC T_LAMP HUMIDITY VOLT_MAIN VOLT_12 VOLT_5 CURRENT lastrow n temp
data = data(~isnan(data(:,1)),:);
matdate = matdate(~isnan(data(:,1)),:);
whennewfile = whennewfile(~isnan(data(:,1)),:);
batt_stats = batt_stats(~isnan(batt_stats(:,1)),:);
cast_time_duration = cast_time_duration(~isnan(cast_time_duration));
header = header([1 4 10 268:270 272:276]);
units = importSUNA_converted_raw_header(filename,4,4);
units = units([1 4 10 268:270 272:276]);

%Taylor manually entered CTD file start time and last bottle fired time to
%match up with cast number and SUNA file names
load SUNA_AR34B_ctd_info
starttime = datenum([cell2mat(SUNAfiles(:,5)) repmat(' ',length(SUNAfiles),1) cell2mat(SUNAfiles(:,6))]);
endtime = datenum([cell2mat(SUNAfiles(:,5)) repmat(' ',length(SUNAfiles),1) cell2mat(SUNAfiles(:,9))]);

%use ind to exclude dark counts 
ind = find(data(:,3)~=0);
newfileindex=find(whennewfile==1);


%try to toatl the number of hours of use over all of the files
a=matdate(2:end)-matdate(1:end-1);
b=a*24*60*60;
realtimecounts = find(b<10);
counted_time = sum(b(realtimecounts))/60/60;



%plot internal temp and humidity
answer = input('Would you like to plot temperature and humidity? (y/n)','s');
if strmatch(lower(answer),'y')
figure
[ax,h1,h2]=plotyy(matdate,data(:,4),matdate,data(:,7));
set(h1,'color','r','marker','.','linestyle','none')
set(h2,'color','b','marker','.','linestyle','none')
ax(1).YLabel.String = 'Temperature (decC)'; ax(1).YColor = 'r';
ax(2).YLabel.String = 'Humidity (%)'; ax(2).YColor = 'b';
legend('Temp','Humd') %T_INT
datetick('keeplimits')
grid on
title('SUNA Temp & Humd (H>30 bad!)')
end

%voltages
answer = input('Would you like to plot the 3 voltage metrics? (y/n)','s');
if strmatch(lower(answer),'y')
figure
title('SUNA Voltage Supplies')
subplot(311)
plot(matdate(ind),data(ind,8),'b.') %main voltage
ylabel('Volts'); legend('Main Voltage'); title('SUNA Voltage Supplies'); grid on
datetick('keeplimits')
subplot(312)
plot(matdate(ind),data(ind,9),'r.') %12V voltage
ylabel('Volts'); legend('12V Voltage'); set(gca,'ylim',[11 14]); grid on
datetick('keeplimits')
subplot(313)
plot(matdate(ind),data(ind,10),'.k') %5V voltage
ylabel('Volts'); legend('5V Voltage'); set(gca,'ylim',[4 6]); grid on
datetick('keeplimits')
end

%current
answer = input('Would you like to plot the power supply current? (y/n)','s');
if strmatch(lower(answer),'y')
figure
plot(matdate(ind),data(ind,11),'.') %Current
ylabel('Current (mA)','fontweight','bold'); datetick('x','keeplimits'); title('SUNA current - EXCLUDE DARK CTS');
end

%plot lamp temp and internal temp of SUNA
answer = input('Would you like to plot the lamp and internal instrument temperatures? (y/n)','s');
if strmatch(lower(answer),'y')
answer = input('Plot, 1) over time, or 2) over count. (answer 1 or 2)');
if answer == 1
    figure
    plot(matdate,data(:,6),'.') %T_LAMP
    hold on
    plot(matdate,data(:,4),'.')
    datetick('keeplimits')
elseif answer == 2
    figure
    plot(data(:,6),'.') %T_LAMP
    hold on
    plot(data(:,4),'.')
end
legend('Lamp Temp','Internal Temp') %T_INT
xlabel('Date','fontweight','bold'); ylabel('Temperature (degC)','fontweight','bold');
title('SUNA internal lamp and instrument temp')
end

%raw estimated nitrate 
answer = input('Would you like to plot the raw estimated nitrate? (y/n)','s');
if strmatch(lower(answer),'y')
figure
plot(matdate,data(:,2),'.') %NITRATE_UM
set(gca,'ylim',[-10 40])
datetick('keeplimits')
xlabel('Date','fontweight','bold'); ylabel('est. Nitrate (uM)','fontweight','bold');
title('SUNA raw estimated Nitrate in uM')
end

