plot_gdat_file.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. from io import StringIO
  30. def main():
  31. if len(sys.argv) != 2 and len(sys.argv) != 3:
  32. sys.exit("Expecting exactly one argument that is the .gdat "
  33. "file and optionally comma-separated column indices.")
  34. # load all .dat files in directory passed as the first argument
  35. file = sys.argv[1]
  36. if not os.path.exists(file):
  37. sys.exit("File " + file + " does not exist.")
  38. wholefile = open(file).read()
  39. df = pd.read_csv(StringIO(wholefile[1:]), index_col=0, skipinitialspace=True, delim_whitespace=True)
  40. #df = df.set_index('time')
  41. print(df)
  42. col_names = pd.DataFrame
  43. if len(sys.argv) == 3:
  44. sel = [int(i) for i in sys.argv[2].split(',') ]
  45. col_names = df.columns[sel]
  46. print(col_names)
  47. if not col_names.empty:
  48. df_sel = df[col_names]
  49. else:
  50. df_sel = df
  51. df_sel.plot(kind='line')
  52. plt.show()
  53. if __name__ == '__main__':
  54. main()