% this script is used to average co2 values for valves 1 and 2, ie atm and
% pco2 readings.
% GET SALINITY DATA
clear all
File=dir('original2016-07-26_081758.txt');

File={File.name};
for ia=1:length(File)

    fileID = fopen(File{ia});
    
    %read in data
    data = textscan(fileID,'%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f','delimiter',','); % looks for numbers (%f) and text strings (%s), mostly separated by commas 
    
    fclose(fileID);
    
    %Extract columns to use:

    raw_doy          = data{1};
    raw_co2          = data{2};
    raw_co2_abs      = data{3};
    raw_h2o          = data{4};
    raw_h2o_abs      = data{5};
    raw_valve        = data{6};
    raw_cell_t       = data{7};
    raw_cell_p       = data{8};
    raw_820          = data{9};
    raw_teq          = data{10};% equilibrator
    raw_peq          = data{11};
    raw_temp1        = data{12};
    raw_ph1          = data{13};
    raw_temp2        = data{14};
    raw_cond2        = data{15};
    raw_sfromc       = data{16};%thermosalinograph
    raw_tsg_t        = data{17};
    raw_tsg_c        = data{18};
    raw_tsg_s        = data{19};
    raw_bs           = data{20};
    raw_bnumber      = data{21};
    raw_cpudate      = data{22};
    raw_cputime      = data{23};
    raw_lat          = data{24};
    raw_lon          = data{25};
    raw_speed        = data{26};
    
end
dn_all = datenum(2016,0,raw_doy,0,0,0);
dv_all = datevec(dn_all);
 X=1:5;
 %%
Fullds=[raw_doy raw_co2 raw_co2_abs raw_h2o raw_h2o_abs raw_valve raw_cell_t raw_cell_p raw_820 raw_teq raw_peq raw_temp1 raw_ph1 raw_temp2 raw_cond2 raw_sfromc raw_tsg_t raw_tsg_c raw_tsg_s raw_bs raw_bnumber raw_cpudate raw_cputime raw_lat raw_lon raw_speed];
V1co2=Fullds(Fullds(:,6)==1,:);
V2atm=Fullds(Fullds(:,6)==2,:);
isample = find(raw_valve == 1);
inewsample = find(diff(isample)~=1);
inewsample=[1;inewsample;length(isample)];
iatm = find(raw_valve == 2); 
inewatm = find(diff(iatm)~=1); 
inewatm=[1;inewatm;length(iatm)];

%extracting atm values, while removing the first 5 values when the valve
%changes
for id = 2:length(inewatm)
   % figuring out how many lines the sample is
    a = (inewatm(id)-inewatm(id-1));
   % getting the all the lines in raw_co2 column
    b = raw_co2(iatm(inewatm(id))-(a):iatm(inewatm(id)));  
    
    % filling up matrix with NaNs up to 400 columns if no co2 value
    raw_atm(id,:) = vec2mat(b,400,[NaN]);
    
     c = dn_all(iatm(inewatm(id))-(a):iatm(inewatm(id)));
    dn(id,:) = vec2mat(c,400,[NaN]);
    
    d = raw_lat(iatm(inewatm(id))-(a):iatm(inewatm(id)));
    cor_lat(id,:) = vec2mat(d,400,[NaN]);
    
    e = raw_lon(iatm(inewatm(id))-(a):iatm(inewatm(id)));
    cor_lon(id,:) = vec2mat(e,400,[NaN]);
    f=raw_ph1(iatm(inewatm(id))-(a):iatm(inewatm(id)));
    pH(id,:) = vec2mat(f,400,[NaN]);
    g=raw_temp1(iatm(inewatm(id))-(a):iatm(inewatm(id)));
    Temp(id,:)=vec2mat(g,400,[NaN]);
    h=raw_tsg_s(iatm(inewatm(id))-(a):iatm(inewatm(id)));
    Sal(id,:)=vec2mat(h,400,[NaN]);
end
% removing the unstable data points, 5 readings after the valve changes
% position
raw_atm(1,:)=[];
raw_atm(:,1:5)=[NaN];
 dn(1,:)=[]; 
dn(:,1:5)=[NaN]; 
cor_lat(1,:)=[]; 
cor_lat(:,1:5)=[NaN]; 
cor_lon(1,:)=[]; 
cor_lon(:,1:5)=[NaN]; 
pH(1,:)=[]; 
pH(:,1:5)=[NaN]; 
Temp(1,:)=[];
Temp(:,1:5)=[NaN];
Sal(1,:)=[];
Sal(:,1:5)=[NaN];
% reshaping and removing NaN's
pH = reshape(pH.',[],1);
pH(logical(sum(pH~=pH,2)),:)=[];

dn = reshape(dn.',[],1);
dn(logical(sum(dn~=dn,2)),:)=[];

cor_lat = reshape(cor_lat.',[],1);
cor_lat(logical(sum(cor_lat~=cor_lat,2)),:)=[];

cor_lon = reshape(cor_lon.',[],1);
cor_lon(logical(sum(cor_lon~=cor_lon,2)),:)=[];

raw_atm = reshape(raw_atm.',[],1);
raw_atm(logical(sum(raw_atm~=raw_atm,2)),:)=[];

Temp = reshape(Temp.',[],1);
Temp(logical(sum(Temp~=Temp,2)),:)=[];

Sal = reshape(Sal.',[],1);
Sal(logical(sum(Sal~=Sal,2)),:)=[];

cor_patm = [dn raw_atm pH Temp Sal cor_lat cor_lon];

%%
%extracting pco2 values, while removing the first 5 values when the valve
%changes
for im = 2:length(inewsample)
   % figuring out how many lines the sample is
    a2 = (inewsample(im)-inewsample(im-1));
   % getting the all the lines in raw_co2 column
    b2 = raw_co2(isample(inewsample(im))-(a2):isample(inewsample(im)));
    % filling up matrix with NaNs up to 400 columns if no co2 value
   v{im}=b2;
    raw_co2_better(im,:) = vec2mat(b2,4118,[NaN]);

     c2 = dn_all(isample(inewsample(im))-(a2):isample(inewsample(im)));
    dnn(im,:) = vec2mat(c2,4118,[NaN]);
    d2 = raw_lat(isample(inewsample(im))-(a2):isample(inewsample(im)));
    cor_latt(im,:) = vec2mat(d2,4118,[NaN]);
    
    e2 = raw_lon(isample(inewsample(im))-(a2):isample(inewsample(im)));
    cor_lonn(im,:) = vec2mat(e2,4118,[NaN]);
    f2 = raw_ph1(isample(inewsample(im))-(a2):isample(inewsample(im)));
    pHH(im,:) = vec2mat(f2,4118,[NaN]);
    
    g2=raw_temp1(isample(inewsample(im))-(a2):isample(inewsample(im)));
    Tempp(im,:) = vec2mat(g2,4118,[NaN]);
    
    h2=raw_tsg_s(isample(inewsample(im))-(a2):isample(inewsample(im)));
    Sall(im,:) = vec2mat(h2,4118,[NaN]);
    
   
end
raw_co2_better(1,:)=[];
raw_co2_better(:,1:5)=[NaN];
Sall(1,:)=[];
Sall(:,1:5)=[NaN];
Tempp(1,:)=[];
Tempp(:,1:5)=[NaN];
 pHH(1,:)=[];
pHH(:,1:5)=[NaN];
cor_latt(1,:)=[]; 
cor_latt(:,1:5)=[NaN]; 
cor_lonn(1,:)=[]; 
cor_lonn(:,1:5)=[NaN]; 
 dnn(1,:)=[]; 
dnn(:,1:5)=[NaN]; 

%Reshaping
pHH = reshape(pHH.',[],1);
pHH(logical(sum(pHH~=pHH,2)),:)=[];

dnn = reshape(dnn.',[],1);
dnn(logical(sum(dnn~=dnn,2)),:)=[];

cor_latt = reshape(cor_latt.',[],1);
cor_latt(logical(sum(cor_latt~=cor_latt,2)),:)=[];

cor_lonn = reshape(cor_lonn.',[],1);
cor_lonn(logical(sum(cor_lonn~=cor_lonn,2)),:)=[];

Tempp = reshape(Tempp.',[],1);
Tempp(logical(sum(Tempp~=Tempp,2)),:)=[];

Sall = reshape(Sall.',[],1);
Sall(logical(sum(Sall~=Sall,2)),:)=[];

raw_co2_better = reshape(raw_co2_better.',[],1);
raw_co2_better(logical(sum(raw_co2_better~=raw_co2_better,2)),:)=[];

cor_ppco2 = [dnn raw_co2_better pHH Tempp Sall cor_latt cor_lonn];


format long
first2 = floor(cor_latt/100);
minutes = rem(cor_latt, 100);
seconds=minutes./60;
LAT=first2+seconds;
first2q=floor(cor_lonn/100);
minutes2q=rem(cor_lonn,100);
seconds2q=minutes2q./60;
LON=first2q+seconds2q;
LON=LON.*-1;


%%
%averaging
dnn_all = datenum(2016,0,dnn,0,0,0);
dv_all = datevec(dn_all);
n = 30; % for n=30, and with a 4 sec interval between readings, averages are taken every 120 secs, or 2 mins
s = size(cor_ppco2);
s2=size(cor_patm);

avgpco2 = squeeze(nanmean(reshape([cor_ppco2;nan(mod(-s(1),n),s(2))],n,[],s(2))));
avgpatm = squeeze(nanmean(reshape([cor_patm;nan(mod(-s2(1),n),s2(2))],n,[],s2(2))));
%avgpatm=avgpatm';
 
[row,col] = size(avgpco2);
[rowi,coli] = size(avgpatm);

% assigning a valve number to pco2 and atm
Valve1=ones(1,length(avgpco2))';
Valve2=ones(1,size(avgpatm,1)).*2;

Valve2=Valve2';

pCO2 = [Valve1 avgpco2];
pATM = [Valve2 avgpatm];

Combined=[pCO2;pATM];

dlmwrite('averagedST_2016-07-26_1.csv', Combined, 'delimiter', ',', 'precision', 15);



