PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/scripts/generate_png.py

http://github.com/mmal/fpde
Python | 83 lines | 76 code | 7 blank | 0 comment | 3 complexity | 18b7a1caac5fe6c4ed3b2b9e4e296d5a MD5 | raw file
  1. from argparse import ArgumentParser
  2. import os
  3. import re
  4. import shutil
  5. import sys
  6. from fpde import *
  7. if __name__ == "__main__":
  8. parser = ArgumentParser()
  9. add_default_arguments(parser)
  10. parser.add_argument("-s", "--scripts", dest="scripts",
  11. help="Set path to fpde/scripts",
  12. default="../fpde/scripts")
  13. parser.add_argument("-m", "--movie", dest="movie",
  14. help="Generate movie", action="store_true")
  15. parser.add_argument("-r", "--recreate", dest="recreate",
  16. help="Force recreation of png files"
  17. " and movie.avi", action="store_true", default = False)
  18. parser.add_argument("-e", "--every", dest="every",
  19. help="Take only every N-th file",
  20. default = 1, type=int, metavar='N')
  21. parser.set_defaults(plot_title = "t={t}")
  22. options = parser.parse_args()
  23. if not os.path.exists(
  24. os.path.join(
  25. options.scripts,"make_movie.sh")):
  26. print "'make_movie.sh' not found in '", options.scripts,"'"
  27. print "Consider setting the correct path using '-s'"
  28. sys.exit()
  29. if(options.file == ""):
  30. file = find_latest_data_dir(options.dir)
  31. file = os.path.join(file,"module_print_data")
  32. else:
  33. file = options.file
  34. if(options.recreate):
  35. [os.remove(f) for f in list_dat(file,".png")]
  36. out = open(options.out, 'w')
  37. files = list_dat(file)[::options.every]
  38. n = len(files)
  39. print "Files to process: {0}".format(n)
  40. options_fix_using(options)
  41. out.write("set terminal png\n")
  42. for i, filename in enumerate(files):
  43. png_filename = filename + ".png"
  44. if not os.path.exists(png_filename) \
  45. or options.recreate:
  46. # this will forece the recreation of movie.avi if there
  47. # are new png files
  48. options.recreate = True
  49. data_file = open(filename, 'r')
  50. dict = get_vars(data_file.read())
  51. out.write('print "generating {0}/{1} : {name}"\n'.\
  52. format(i,n, name=png_filename))
  53. out.write('set output "{0}"\n'.format(png_filename))
  54. generate_plot_cmd(out, options, dict, filename)
  55. data_file.close()
  56. out.close()
  57. os.system("gnuplot {0}".format(options.out))
  58. if options.movie:
  59. movie_file = os.path.join(file,"movie.avi")
  60. # play a movie if it does not exist
  61. if os.path.exists(movie_file) and not options.recreate:
  62. os.system("mplayer -loop 0 {file}".\
  63. format(file=movie_file))
  64. # else generate a movie
  65. else:
  66. os.system("{scripts}/make_movie.sh {dir}".format(
  67. dir=file,scripts=options.scripts))