clear all
close all

ap_files = dir('*ap.txt');
ap_name = strvcat(ap_files.name);
ap_name = sortrows(ap_name);

cp_files = dir('*cp.txt');
cp_name = strvcat(cp_files.name);
cp_name = sortrows(cp_name);

ap = [];
cp = [];
cpEr = [];
cp_info = cell(1,8);
ap_info = cell(1,8);
cpwl2 = 400:2:755;


for y = 1:length(ap_files)
% ap data file
fid = fopen(strtrim(ap_name(y,:)));
header = textscan(fid,'%s','delimiter','\n');
fseek(fid,0,'bof');
C = textscan(fid,['%f%f%s%s%s%s' char(repmat(uint8('%f'),1,88))],'headerlines',1);
fclose all;
apwl = eval(['[' header{1}{1}(74:end) ']']);
data = cell2mat(C(:,9:end));
nan_col = all(isnan(data));
data(:,nan_col) = [];
missing = find(data == -9999);
data(missing) = NaN;
ap_info = [{[ap_info{1,1}; C{1}]} {[ap_info{1,2}; C{2}]} {[ap_info{1,3}; C{3}]} {[ap_info{1,4}; C{4}]} {[ap_info{1,5}; C{5}]} {[ap_info{1,6}; C{6}]} {[ap_info{1,7}; C{7}]} {[ap_info{1,8}; C{8}]}];

ap = [ap; interp1(apwl,data',[650 676 715],'linear')'];
end
for y = 1:length(cp_files)
% cp data file
fid = fopen(strtrim(cp_name(y,:)));
header = textscan(fid,'%s','delimiter','\n');
fseek(fid,0,'bof');
C = textscan(fid,['%f%f%s%s%s%s' char(repmat(uint8('%f'),1,88))],'headerlines',1);
fclose all;
cpwl = eval(['[' header{1}{1}(74:end) ']']);
data = cell2mat(C(:,9:end));
nan_col = all(isnan(data));
data(:,nan_col) = [];
missing = find(data == -9999);
data(missing) = NaN;
cp_info = [{[cp_info{1,1}; C{1}]} {[cp_info{1,2}; C{2}]} {[cp_info{1,3}; C{3}]} {[cp_info{1,4}; C{4}]} {[cp_info{1,5}; C{5}]} {[cp_info{1,6}; C{6}]} {[cp_info{1,7}; C{7}]} {[cp_info{1,8}; C{8}]}];

cp = [cp; interp1(cpwl,data',cpwl2,'linear')'];

%cp error file
j = findstr('.txt',cp_name(y,:));
fid = fopen([cp_name(y,1:(j-1)) '_uncertainty.txt']);
C = textscan(fid,['%f%f%f%s%f%s' char(repmat(uint8('%f'),1,88))],'headerlines',1);
fclose all;
data = cell2mat(C(:,9:end));
nan_col = all(isnan(data));
data(:,nan_col) = [];
missing = find(data == -9999);
data(missing) = NaN;
 
cpEr = [cpEr; interp1(cpwl,data',cpwl2,'linear')'];

fclose all;
end

ap_date_in = datenum(strcat(ap_info{1,3},ap_info{1,4}),'yyyymmddHH:MM:SS');
cp_date_in = datenum(strcat(cp_info{1,3},cp_info{1,4}),'yyyymmddHH:MM:SS');
ap_date_out = datenum(strcat(ap_info{1,5},ap_info{1,6}),'yyyymmddHH:MM:SS');
cp_date_out = datenum(strcat(cp_info{1,5},cp_info{1,6}),'yyyymmddHH:MM:SS');

cd('/Volumes/LEEUW/ap_gamma');

cp660 = interp1(cpwl2,cp',660,'linear')';

POC1 = cp660.*575;
POC2 = -cp660.*(242.*cp660-562);

idx = any(isnan(cp),1);

cp = cp(:,~idx);
cpEr = cpEr(:,~idx);
cpwl2 = cpwl2(~idx);

[cp0,gammav,fiterr] = FitSpectra_HM(cpwl2,cpEr,cp);

chl = (ap(:,2)-(39/65*ap(:,1)+26/65*ap(:,3)))/.0164;

cp_data = [cp_info{1} cp_info{2} cp_date_in cp_date_out cp_info{7} cp_info{8} nan(length(cp_date_out),1) POC1 POC2 gammav];
AllData = [ap_info{1} ap_info{2} ap_date_in ap_date_out ap_info{7} ap_info{8} chl nan(length(ap_date_out),3)];
AllData = sortrows(AllData,3);
cp_data = sortrows(cp_data,3);

for n = 1:length(cp_date_out)
[r,c] = find(AllData(:,3) == cp_data(n,3));
    if isempty(r)
        [r,c] = find(AllData(:,3)<cp_data(n,3));
        AllData = [AllData(1:r(end),:);cp_data(n,:);AllData(r(end)+1:end,:)];
    else
        AllData(r,8:10) = cp_data(n,8:10);
    end
    
end
idx = isnan(AllData);
AllData(idx) = -9999;
 
fid =  fopen(['/Volumes/LEEUW/ap_gamma/NewGrid_2013/ChlPOCGamma_1km.txt'],'wt');
fprintf(fid,'%s\n','#spectra Bin_Idx Date_In Date_Out Temperature Salinity Chlorophyll POC1 POC2 Gamma');
for p = 1:length(AllData(:,1))
    fprintf(fid,'%.0f %.0f %f %f %.2f %.2f %.3f %.2f %.2f %.2f \n',AllData(p,:));
end
fclose all;






