The theory of rare earth eaergy levels and spectra