function out = get_LmSF_Cl36(in,datadir)

% Interpolates into precalculated scaling factor grids to obtain Lm
% scaling factors for specified Rc and pressure vectors. 
%
% This code ingests an input structure with the following information:
%   cell array sfdata.t contains time vectors
%   cell array sfdata.dipRc contains Rc at corresponding times
%   vector pressure is atmospheric pressure
%
% Adds a cell array containing corresponding vectors of scaling factor
% and returns the structure. 

% Load grid file

load([datadir 'LSDn2015_grid.mat']);

% Load sf file

load([datadir 'Lm2015_all.mat']);
 
% Now do interpolation.    

for a = 1:length(in.tmax)
    % Make pressure vector. This is planning ahead for time-dependent
    % pressure changes. 
    if iscell(in.pressure)
        % Case in.pressure is cell array with time-dependent p
        temp_p = in.pressure{a};
    else
        % Case in.pressure is vector with non-time-dependent p
        % In this case is scalar for each sample, must be expanded to
        % vector that matches time mesh
        temp_p = zeros(size(in.tmax{a})) + in.pressure(a);
    end
    % Catch excessive Rc.
    bigRc = find(in.Rc{a} > 20);
    if ~isempty(bigRc)
        in.Rc{a}(bigRc) = 20 + zeros(size(bigRc));
    end
      
    % Do interpolation.
       
    % assign vector to cell in fields for each pathway
    in.Ksp_Lm{a} = interp2(sfgrid.Rc,sfgrid.p,sf,in.Rc{a},temp_p); 
    % All pathways have same scaling for Lm
    in.Casp_Lm{a} = in.Ksp_Lm{a}; 
    in.Pf0sp_Lm{a} = in.Ksp_Lm{a}; 
    in.Fesp_Lm{a} = in.Ksp_Lm{a};
    in.Tisp_Lm{a} = in.Ksp_Lm{a};
    
end

% Return

out = in;
    
    
    
    
    
    
    
    