added a copy of cosmics.py for optimization