Refactor calcemit.py