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
12 - def __init__(self,
13 args=None,
14 **kwargs):
15 description="""\
16 Takes a plain file with column-oriented data and converts it to a
17 csv-file. If more than one file are specified, they are joined
18 according to the first column.
19
20 Note: the first file determines the resolution of the time-axis
21 """
22 PyFoamApplication.__init__(self,
23 args=args,
24 description=description,
25 usage="%prog <source> ... <dest.csv>",
26 interspersed=True,
27 changeVersion=False,
28 nr=2,
29 exactNr=False,
30 **kwargs)
31
33 data=OptionGroup(self.parser,
34 "Data",
35 "Specification on the data that is read in")
36 self.parser.add_option_group(data)
37 data.add_option("--time-name",
38 action="store",
39 dest="time",
40 default=None,
41 help="Name of the time column")
42 data.add_option("--column-names",
43 action="append",
44 default=[],
45 dest="columns",
46 help="The columns (names) which should be copied to the CSV. All if unset")
47
48 how=OptionGroup(self.parser,
49 "How",
50 "How the data should be joined")
51 self.parser.add_option_group(how)
52
53 how.add_option("--force",
54 action="store_true",
55 dest="force",
56 default=False,
57 help="Overwrite the destination csv if it already exists")
58 how.add_option("--extend-data",
59 action="store_true",
60 dest="extendData",
61 default=False,
62 help="Extend the time range if other files exceed the range of the first file")
63 how.add_option("--delimiter",
64 action="store",
65 dest="delimiter",
66 default=',',
67 help="Delimiter to be used between the values. Default: %default")
68
70 dest=self.parser.getArgs()[-1]
71 if path.exists(dest) and not self.opts.force:
72 self.error("CSV-file",dest,"exists already. Use --force to overwrite")
73 sources=self.parser.getArgs()[0:-1]
74
75 data=SpreadsheetData(txtName=sources[0],
76 timeName=self.opts.time,
77 validData=self.opts.columns,
78 title=path.splitext(path.basename(sources[0]))[0])
79
80 if self.opts.time==None:
81 self.opts.time=data.names()[0]
82
83 for s in sources[1:]:
84 addition=path.splitext(path.basename(s))[0]
85 sData=SpreadsheetData(txtName=s)
86 for n in sData.names():
87 if n!=self.opts.time and (self.opts.columns==[] or n in self.opts.columns):
88 d=data.resample(sData,
89 n,
90 time=self.opts.time,
91 extendData=self.opts.extendData)
92 data.append(addition+" "+n,d)
93
94 data.writeCSV(dest,
95 delimiter=self.opts.delimiter)
96
97
98