Abstract. We present a new method to reconstruct solar wind conditions from spacecraft data taken during magnetosheath passages, which can be used to support, e.g., magnetospheric models. The unknown parameters of the solar wind are used as boundary conditions of an MHD (magnetohydrodynamics) magnetosheath model. The boundary conditions are varied until the spacecraft data matches the model predictions. The matching process is performed using a gradientbased minimization of the misfit between data and model. To achieve this time-consuming procedure, we introduce the adjoint of the magnetosheath model, which allows efficient calculation of the gradients. An automatic differentiation tool is used to generate the adjoint source code of the model. The reconstruction method is applied to THEMIS (Time History of Events and Macroscale Interactions during Substorms) data to calculate the solar wind conditions during spacecraft magnetosheath transitions. The results are compared to actual solar wind data. This allows validation of our reconstruction method and indicates the limitations of the MHD magnetosheath model used.