PageRenderTime 53ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/src/csv2movie.py

https://github.com/abhishektiwari/csv2movie
Python | 143 lines | 89 code | 4 blank | 50 comment | 27 complexity | 79065d294d0470369eecd5937c4706c6 MD5 | raw file
  1. """
  2. CSV2Movie takes a csv file as input and creates movies by stitching the plotted images
  3. frame by frame. Each image plot will become a single frame in the movie. CSV2Movie
  4. requires mencoder (http://www.mplayerhq.hu/) or ffmpeg (http://www.ffmpeg.org/) to
  5. create the light weight movies for the csv file data. User needs to provide the
  6. command-line arguments and either short- or long-style flags to specify various options.
  7. On Ubuntu you can install mencoder and ffmpeg using synaptic package manager. For Windows
  8. you have to download the executable files for mencoder and ffmpeg and set the environment
  9. variables. For Mac OS installation details are here
  10. here http://www.macosxhints.com/article.php?story=20061220082125312
  11. usage: python CSV2Movie.py [ -h | -f | -t | -c | -d | -m | -e] [arg] ...
  12. Options and arguments (and corresponding environment variables):
  13. -h : print this help message (also --help)
  14. -f : input CSV file location (also --file)
  15. -t : tab-delimited csv files
  16. -c : comma-separated values (CSV) file (default)
  17. -s : semi colon delimited file
  18. -o : colon delimited file
  19. -m : use ffmpeg to create movie (default)
  20. -e : use mencoder to create movie
  21. -p : frames per second for the movie (also --fps) (defult fps=10)
  22. arg : reference columns for plotting. CSVPlot plots all other columns against
  23. the reference column. By default it uses column 1.
  24. Example use:
  25. 1. For help information,
  26. python CSV2Movie.py --help
  27. OR
  28. python CSV2Movie.py --h
  29. 2. For plotting column x and y of file data.csv,
  30. python CSV2Movie.py -c -m -f data.csv x y
  31. python CSV2Movie.py -t -e --file=data.csv x y
  32. """
  33. '''
  34. Created on 18/02/2010
  35. @author: Abhishek Tiwari
  36. http://www.abhishek-tiwari.com
  37. Permission is hereby granted to use and abuse this document
  38. so long as proper attribution is given.
  39. '''
  40. import os
  41. import sys
  42. import getopt
  43. import numpy as np
  44. import csv
  45. import createmovie as mov
  46. class Usage(Exception):
  47. "Usage() exception class, which we catch in an except clause at the end of main()"
  48. def __init__(self,msg):
  49. "A description of the Usage constructor"
  50. self.msg=msg
  51. def main(argv=None):
  52. "main() function to analyse the command line flags and arguments, and activate further anlysis"
  53. fdelimiter=","
  54. foutput="mencoder"
  55. fargument="0"
  56. fps=10
  57. if argv is None:
  58. argv=sys.argv
  59. try:
  60. if len(argv) <=2:
  61. raise Usage(__doc__)
  62. try:
  63. opts,args= getopt.getopt(sys.argv[1:],"ctsomehp:f:",["help","file=","fps="])
  64. except getopt.error,msg:
  65. raise Usage(msg)
  66. #Option processing
  67. for option, value in opts:
  68. if option in ("-h","--help"):
  69. raise Usage(__doc__)
  70. sys.exit(0)
  71. else:
  72. if option in ("-f","--file"):
  73. fname=value
  74. print "Input CSV File:", fname
  75. fdir=os.path.realpath(os.path.dirname(fname))
  76. #fdir=os.path.dirname(os.path.realpath(fname))
  77. if option in("-t"):
  78. fdelimiter="\t"
  79. elif option in("-c"):
  80. fdelimiter=","
  81. elif option in("-s"):
  82. fdelimiter=";"
  83. elif option in("-o"):
  84. fdelimiter=":"
  85. if option in("-m"):
  86. foutput="ffmpeg"
  87. elif option in ("-e"):
  88. foutput="mencoder"
  89. if option in ("-p","--fps"):
  90. fps=value
  91. #Argument processing
  92. if len(args) !=0:
  93. for argument in args:
  94. argument.strip()
  95. try:
  96. cargument = int(argument)
  97. except:
  98. print 'cannot cast column argument to int'
  99. if isinstance(cargument, int) and cargument>=1:
  100. print "For column-",cargument
  101. fargument=cargument-1
  102. plotter(fname, fdelimiter, foutput, fargument, fdir, fps)
  103. else:
  104. plotter(fname, fdelimiter, foutput, fargument, fdir, fps)
  105. except Usage, err:
  106. print >>sys.stderr, err.msg
  107. print >>sys.stderr, "for help use --help"
  108. return 2
  109. def plotter(fname, fdelimiter, foutput, fargument, fdir,fps):
  110. "This function parse the data in csv file and then calls different plotting functions"
  111. try:
  112. fopen=open(fname,'r')
  113. csvreader = csv.reader(fopen, delimiter=",")
  114. fields = csvreader.next()
  115. except:
  116. print "Can not open input csv file",fname
  117. fopen.close
  118. data=np.loadtxt(fname, delimiter=fdelimiter, unpack=True, skiprows=1)
  119. i=0;
  120. while i<len(fields):
  121. if fargument != i:
  122. xtit=fields[fargument]
  123. ytit=fields[i]
  124. print xtit, ytit
  125. mov.createmovie(xtit, ytit, data[fargument], data[i],foutput, fdir, fps)
  126. os.system("rm *_tmp.png")
  127. i=len(fields);
  128. i=i+1
  129. if __name__ == "__main__":
  130. "The main program collected into function main()"
  131. sys.exit(main())