% run_valley_locator_all_lines.m

% ******************************************************************

%  Script to run the valley_locator.m function
%  Configured for a directory containing multiple input RES data files

% *******************************************************************

clear
close all

% **Change this line to insert the correct path for the RES data files**
addpath('/nobackup/fhcr53/greenland_data_files/');

% Sets data directory **change if needed**
Dir = './data/';
Files = dir(fullfile(Dir,'*.csv')); % gets all .csv files in a structure

% EPSG projection number (3413 = NH polar stereographic - matches BedMachine)
epsg = 3413;

% Run valley_locator.m function for each line, extracting valley locations
% and morphometrics
i = 1;
N_counter = zeros(length(Files),1);
for k = 1:length(Files)
  filename = Files(k).name;
  fullFileName = fullfile(Dir, filename);
  fprintf(1, 'Now reading %s\n', filename);
  [date_out, seg_out, X_out, Y_out, Etop_out, Efloor_out, D_out, W_out, R_out, V_out, C_out, N, profiles_out] = valley_locator(fullFileName,epsg);
  date(i:i+N-1,1) = date_out;
  seg(i:i+N-1,1) = seg_out;
  X(i:i+N-1,1) = X_out;
  Y(i:i+N-1,1) = Y_out;
  Etop(i:i+N-1,1) = E_out;
  Efloor(i:i+N-1,1) = E_out;
  D(i:i+N-1,1) = D_out;
  W(i:i+N-1,1) = W_out;
  R(i:i+N-1,1) = R_out;
  V(i:i+N-1,1) = V_out;
  C(i:i+N-1,1) = C_out;
  profiles(:,:,i:i+N-1) = profiles_out;
  i = i+N;
  N_counter(k,1) = N;
end

% Save output as .mat files
save('ValleyMorphFull','date','seg','X','Y','Etop','Efloor','D','W','R','V','C');
save('ValleyProfilesFull','profiles')