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
13 description="""\
14 Takes a plain file with column-oriented data and converts it to a
15 csv-file. If more than one file are specified, they are joined
16 according to the first 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
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 data.add_option("--column-names",
40 action="append",
41 default=[],
42 dest="columns",
43 help="The columns (names) which should be copied to the CSV. All if unset")
44
45 how=OptionGroup(self.parser,
46 "How",
47 "How the data should be joined")
48 self.parser.add_option_group(how)
49
50 how.add_option("--force",
51 action="store_true",
52 dest="force",
53 default=False,
54 help="Overwrite the destination csv if it already exists")
55 how.add_option("--extend-data",
56 action="store_true",
57 dest="extendData",
58 default=False,
59 help="Extend the time range if other files exceed the range of the first file")
60 how.add_option("--delimiter",
61 action="store",
62 dest="delimiter",
63 default=',',
64 help="Delimiter to be used between the values. Default: %default")
65
67 dest=self.parser.getArgs()[-1]
68 if path.exists(dest) and not self.opts.force:
69 self.error("CSV-file",dest,"exists already. Use --force to overwrite")
70 sources=self.parser.getArgs()[0:-1]
71
72 data=SpreadsheetData(txtName=sources[0],
73 timeName=self.opts.time,
74 validData=self.opts.columns,
75 title=path.splitext(path.basename(sources[0]))[0])
76
77 if self.opts.time==None:
78 self.opts.time=data.names()[0]
79
80 for s in sources[1:]:
81 addition=path.splitext(path.basename(s))[0]
82 sData=SpreadsheetData(txtName=s)
83 for n in sData.names():
84 if n!=self.opts.time and (self.opts.columns==[] or n in self.opts.columns):
85 d=data.resample(sData,
86 n,
87 time=self.opts.time,
88 extendData=self.opts.extendData)
89 data.append(addition+" "+n,d)
90
91 data.writeCSV(dest,
92 delimiter=self.opts.delimiter)
93