The ECMWF implementation of three dimensional variational assimilation. Part 1:Formulation