added code to write out csv files from the sampling code.
merged.