Move the calculation of input/output weights into commonlib

See https://bugs.launchpad.net/oq-engine/+bug/1370380. The tests are green: https://ci.openquake.org/job/zdevel_oq-commonlib/205