Photonuclear reactions in 40Ca and 16O leading to excited states of the residual nuclei