Context. The chemically peculiar stars of the upper main sequence represent a natural laboratory for the study of rare-earth elements (REE). Aims. We want to check the reliability of the energy levels and atomic line parameters for the second REE ions currently available in the literature, and obtained by means of experiments and theoretical calculations. Methods. We have obtained a UVES spectrum of a slowly rotating strongly magnetic Ap star, HD 144897, that exhibits very large overabundances of rare-earth elements. Here we present a detailed spectral analysis of this object, taking effects of non-uniform vertical distribution (stratification) of chemical elements into account. Results. We determined the photospheric abundances of 40 ions. For seven elements (Mg, Si, Ca, Ti, Cr, Mn, Fe), we obtained a stratification model that allows us to produce a satisfactory fit to the observed profiles of spectral lines of various strengths. All the stratified elements but Cr show a steep decrease in concentration toward the upper atmospheric layers; for Cr the transition from high to low concentration regions appears smoother than for the other elements. The REEs abundances, which have been determined for the first time from the lines of the first and second ions, have been found typically four dex higher than solar abundances. Our analysis of REE spectral lines provides strong support for the laboratory line classification and determination of the atomic parameters. The only remarkable exception is Nd iii, for which spectral synthesis was found to be inconsistent with the observations. We therefore performed a revision of the Nd iii classification. We confirmed the energies for 11 out of 24 odd energy levels that were classified previously, and derived the energies for additional 24 levels of Nd iii, thereby substantially increasing the number of classified Nd iii lines with corrected wavelengths and atomic parameters.