A numerical algorithm for geophysical and astrophysical inhomogeneous fluid flows