Package PyFoam :: Package Applications :: Module ConvertToCSV
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.ConvertToCSV

 1  """ 
 2  Application-class that implements pyFoamConvertToCSV.py 
 3  """ 
 4  from optparse import OptionGroup 
 5   
 6  from PyFoamApplication import PyFoamApplication 
 7  from PyFoam.Basics.SpreadsheetData import SpreadsheetData 
 8   
 9  from os import path 
10   
11 -class ConvertToCSV(PyFoamApplication):
12 - def __init__(self,args=None):
13 description=""" 14 Takes a plain file with column-oriented data and converts it to a csv-file. 15 If more than one file are specified, they are joined according to the first 16 column. 17 18 Note: the first file determines the resolution of the time-axis 19 """ 20 PyFoamApplication.__init__(self, 21 args=args, 22 description=description, 23 usage="%prog <source> ... <dest.csv>", 24 interspersed=True, 25 changeVersion=False, 26 nr=2, 27 exactNr=False)
28
29 - def addOptions(self):
30 data=OptionGroup(self.parser, 31 "Data", 32 "Specification on the data that is read in") 33 self.parser.add_option_group(data) 34 data.add_option("--time-name", 35 action="store", 36 dest="time", 37 default=None, 38 help="Name of the time column") 39 40 how=OptionGroup(self.parser, 41 "How", 42 "How the data should be joined") 43 self.parser.add_option_group(how) 44 45 how.add_option("--force", 46 action="store_true", 47 dest="force", 48 default=False, 49 help="Overwrite the destination csv if it already exists") 50 how.add_option("--extend-data", 51 action="store_true", 52 dest="extendData", 53 default=False, 54 help="Extend the time range if other files exceed the range of the first file") 55 how.add_option("--delimiter", 56 action="store", 57 dest="delimiter", 58 default=',', 59 help="Delimiter to be used between the values. Default: %default")
60
61 - def run(self):
62 dest=self.parser.getArgs()[-1] 63 if path.exists(dest) and not self.opts.force: 64 self.error("CSV-file",dest,"exists already. Use --force to overwrite") 65 sources=self.parser.getArgs()[0:-1] 66 67 data=SpreadsheetData(txtName=sources[0], 68 title=path.splitext(path.basename(sources[0]))[0]) 69 if self.opts.time==None: 70 self.opts.time=data.names()[0] 71 72 for s in sources[1:]: 73 addition=path.splitext(path.basename(s))[0] 74 sData=SpreadsheetData(txtName=s) 75 for n in sData.names(): 76 if n!=self.opts.time: 77 d=data.resample(sData, 78 n, 79 time=self.opts.time, 80 extendData=self.opts.extendData) 81 data.append(addition+" "+n,d) 82 83 data.writeCSV(dest, 84 delimiter=self.opts.delimiter)
85