function [out_val]=interp_pcord_wrf(in_val,p_in,p_out,nl)


if p_out< p_in(nl); % EXTRAPOLATE ALOFT! if the model pressure surface is less than (e.g. higher in altitude) than the top of sounding
    %disp('extrap aloft');
    w2 = (p_in(nl)-p_out)./(p_in(nl)-p_in(nl-1)); % how many zk levels fit into the excess height
    w1 = 1- w2;
    p_surf=(w1.*(in_val(nl)))+w2.*(in_val(nl-1));
elseif p_out > p_in(1) % EXTRAPOLATE BELOW THE GROUND
    %disp('extrap below'); 
    w2 = (p_in(2)-p_out)./(p_in(2)-p_in(1)); % how many zk levels fit into the excess height
    w1 = 1- w2;
    p_surf=(w1.*(in_val(2)))+w2.*(in_val(1));
else % we need to search through the column to find where the ground resides
    intp='false';
    kp=nl;
    while (strcmp(intp,'false') && kp >=2)
        if (p_in(kp)<= p_out && p_in(kp-1) >= (p_out)) % the ground is between these layers
            w2=(p_out-p_in(kp))./(p_in(kp-1)-p_in(kp));
            w1=1-w2;
            val_surf=w1.*(in_val(kp))+w2.*(in_val(kp-1));
            intp='true';
            %disp('Interp'); 
        end
        kp=kp-1; % step down through the column until we find the ground
    end
end


out_val=val_surf;

end