3D version of lw1.f90 (in 2D rev_lw1.f90)