Tools to produce data file (code by Casey, just restyled)