We developed a modern methodology to retrieve water (H2O) and deuterated water (HDO) in planetary and cometary atmospheres, and constructed an accurate spectral database that combines theoretical and empirical results. On the basis of a greatly expanded set of spectroscopic parameters, we built a full non-resonance cascade fluorescence model and computed fluorescence efficiencies for H2O (500 million lines) and HDO (700 million lines). The new line list was also integrated into an advanced terrestrial radiative transfer code (LBLRTM) and adapted to the CO2 rich atmosphere of Mars, for which we adopted the complex Robert-Bonamy formalism for line shapes. We retrieved water and D/H in the atmospheres of Mars, comet C/2007 W1 (Boattini), and Earth by applying the new formalism to spectra obtained with the high-resolution spectrograph NIRSPEC/Keck II atop Mauna Kea (Hawaii). The new model accurately describes the complex morphology of the water bands and greatly increases the accuracy of the retrieved abundances (and the D/H ratio in water) with respect to previously available models. The new model provides improved agreement of predicted and measured intensities for many H2O lines already identified in comets, and it identifies several unassigned cometary emission lines as new emission lines of H2O. The improved spectral accuracy permits retrieval of more accurate rotational temperatures and production rates for cometary water.