A General Approach to Domain Adaptation with Applications in Astronomy