plot_dat_files.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python
  2. """
  3. This is free and unencumbered software released into the public domain.
  4. Anyone is free to copy, modify, publish, use, compile, sell, or
  5. distribute this software, either in source code form or as a compiled
  6. binary, for any purpose, commercial or non-commercial, and by any
  7. means.
  8. In jurisdictions that recognize copyright laws, the author or authors
  9. of this software dedicate any and all copyright interest in the
  10. software to the public domain. We make this dedication for the benefit
  11. of the public at large and to the detriment of our heirs and
  12. successors. We intend this dedication to be an overt act of
  13. relinquishment in perpetuity of all present and future rights to this
  14. software under copyright law.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. For more information, please refer to [http://unlicense.org]
  23. """
  24. import numpy as np
  25. import pandas as pd
  26. import matplotlib.pyplot as plt
  27. import os
  28. import sys
  29. import glob
  30. def load_dat_files(dat_dir):
  31. counts = {}
  32. res = pd.DataFrame
  33. # read all .dat files
  34. dat_files = sorted(glob.glob(os.path.join(dat_dir, "*.dat")))
  35. for file in dat_files:
  36. observable = os.path.splitext(os.path.basename(file))[0]
  37. df = pd.read_csv(file, sep=' ', index_col='time', names=['time', observable])
  38. # use the first data frame as basis and the join with new observables
  39. # to create a single data frame
  40. if res.empty:
  41. res = df
  42. else:
  43. res = res.join(df)
  44. return res
  45. def main():
  46. if len(sys.argv) != 2:
  47. sys.exit("Expecting exactly one argument that is the path to directory with .dat files.")
  48. # load all .dat files in directory passed as the first argument
  49. dat_dir = sys.argv[1]
  50. if not os.path.exists(dat_dir):
  51. sys.exit("Directory " + dat_dir + " does not exist.")
  52. df = load_dat_files(dat_dir)
  53. df.plot(kind='line')
  54. plt.show()
  55. if __name__ == '__main__':
  56. main()