The advance and retreat of the thermocline on the continental shelf