clear all
%cd ('C:\Users\Student\Desktop\Linh\Cruise-copy\1607OS Underway data\UWAY\1607pCO2\RAW + MATLAB files')
File=dir('gpsdata.csv'); %from gpsconversion.m

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

    fileID = fopen(File{ia});
    
    %read in data
    data = textscan(fileID,'%f%f%f','delimiter',','); % looks for numbers (%f) and text strings (%s), mostly separated by commas 
    
    fclose(fileID);
    time=data{1};
    lat=data{2};
    lon=data{3};
end

File2=dir('calibrated_ST.csv');
File2={File2.name};
for ib=1:length(File2)

    fileID = fopen(File2{ib});
    
    %read in data
    data = textscan(fileID,'%f%f%f%f%f%f%f%f','delimiter',','); % looks for numbers (%f) and text strings (%s), mostly separated by commas 
    timec=data{1};
    valve=data{2};
    co2=data{3};
    pH=data{4};
    Temp=data{5};
    Sal=data{6};
    latc=data{7};
    lonc=data{8};
end
%%
IDXX=knnsearch(time,timec); %k-nearest neighbor function to link gps to underway
gps=[lat(IDXX) lon(IDXX)];
all=[timec valve co2 pH  Temp Sal gps];

dlmwrite('co2andgps_ST.csv', all, 'delimiter', ',', 'precision', 15); 

%make plots

z = 1:10:length(co2);
sub = ones(length(z),11);
figure; grid on; box on; ylim([29 37]); xlim([-125 -117]); hold;
scatter(gps(:,2),gps(:,1),50,co2,'filled') % plot salinity data
caxis([345 550]); % set the color bar limit
colorbar('eastoutside');
%set(gca,'clim', [340 550]); % set the color bar limit
colormapeditor ; 
print('pCO2','-depsc','-r300')