%A 'service' script that bins stratigraphic information.

for trs = 1:3 %ts = transect
load(strcat('outputMeanderWavelengthMeasurementsMarsRivers_Transect_',num2str(trs)))
    l(trs).chm = chm; %l for lambda, i.e. wavelength
    l(trs).rhm = rhm;
    l(trs).mm  = mm;
    l(trs).meanz = meanz;
    l(trs).mls = mls;

%Needed: replace fixed interp rms errors with dynamically loaded errors
%(using ArcGIS-output .TXT files).
for n = 1:length(chm)
if trs == 1 %interpolation rms error
    l(trs).chm(n).interp_rms_error = [20.221 17.523 0 0 0]; %(using ArcGIS-output .TXT files).
elseif trs == 2
    l(trs).chm(n).interp_rms_error = [4.848 3.978 0 0 0]; %(using ArcGIS-output .TXT files).
elseif trs == 3
    l(trs).chm(n).interp_rms_error = [50 50 0 0 0];
end
end

for n = 1:length(rhm)
if trs == 1 %interpolation rms error
    l(trs).rhm(n).interp_rms_error = [20.221 17.523 0 0 0]; %(using ArcGIS-output .TXT files).
elseif trs == 2
    l(trs).rhm(n).interp_rms_error = [4.848 3.978 0 0 0]; %(using ArcGIS-output .TXT files).
elseif trs == 3
    l(trs).rhm(n).interp_rms_error = [50 50 0 0 0];
end
end

disp('Warning: Overriding interp rms with hand coded rms values from rms_data.txt files!')


end


for trs = 1:3
    for irhm = 1:length(l(trs).rhm)
        for iss = 1:5
                l(trs).rhm(irhm).czadjusted(iss) = [l(trs).rhm(irhm).z] ...
                    - [l(trs).rhm(irhm).cz(iss)];
        end
    end
end

for trs = 1:3 %Assign transect numbers (for plotting)
    
    for itrs = 1:length(l(trs).rhm) %Assign transect numbers to reproducible half-meanders
        l(trs).rhm(itrs).trs = trs;
        l(trs).rhm(itrs).dtm = trs+1;
        if trs == 1 & l(trs).rhm(itrs).x < 9e3; %Transect 1 consists of 2 DTMs 
            l(trs).rhm(itrs).dtm = 1;
        end
    end
    
        for itrs = 1:length(l(trs).chm) %Assign transect numbers to channels
        l(trs).chm(itrs).trs = trs;
        l(trs).chm(itrs).dtm = trs+1;
        if trs == 1 & l(trs).chm(itrs).x < 9e3; %Transect 1 consists of 2 DTMs 
            l(trs).chm(itrs).dtm = 1;
        end
    end
    
end

    lallr = [l(1).rhm l(2).rhm l(3).rhm]; %All reproducible half-meanders
    lallc = [l(1).chm l(2).chm l(3).chm]; %All reproducible meandering channels
    
    for m = 1:length(lallr)
        stratz(m) = [lallr(m).czadjusted(iss_for_display)];
    end
    for n = 1:length(lallc)
        stratzc(n) = [lallc(n).z] -[lallc(n).cz(iss_for_display)];
    end
    
    for m = 1:length(lallc)
        lallc(m).zs_for_display = lallc(m).z - lallc(m).cz(iss_for_display);
        lallc(m).zserr_for_display = lallc(m).interp_rms_error(iss_for_display);
    end
    
    for m = 1:length(lallr)
        lallr(m).zs_for_display = lallr(m).z - lallr(m).cz(iss_for_display);
        lallr(m).zserr_for_display = lallr(m).interp_rms_error(iss_for_display);
    end
    
    clear n m d chm rhm itrs iss
    
    