1 """
2 Application-class that implements pyFoamJoinCSV.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 Join together two or more CSV-files. Data is resampled to fit the
15 timescale of the the first CSV-file
16 """
17 PyFoamApplication.__init__(self,
18 args=args,
19 description=description,
20 usage="%prog <source1.csv> <source2.csv> ... <dest.csv>",
21 interspersed=True,
22 changeVersion=False,
23 nr=3,
24 exactNr=False)
25
27 data=OptionGroup(self.parser,
28 "Data",
29 "Specification on the data that is read in")
30 self.parser.add_option_group(data)
31 data.add_option("--time-name",
32 action="store",
33 dest="time",
34 default=None,
35 help="Name of the time column")
36
37 how=OptionGroup(self.parser,
38 "How",
39 "How the data should be joined")
40 self.parser.add_option_group(how)
41
42 how.add_option("--force",
43 action="store_true",
44 dest="force",
45 default=False,
46 help="Overwrite the destination csv if it already exists")
47 how.add_option("--extend-data",
48 action="store_true",
49 dest="extendData",
50 default=False,
51 help="Extend the time range if other files exceed the range of the first file")
52 how.add_option("--add-times",
53 action="store_true",
54 dest="addTimes",
55 default=False,
56 help="Actually add the times from the second file instead of interpolating")
57 how.add_option("--interpolate-new-times",
58 action="store_true",
59 dest="interpolateNewTime",
60 default=False,
61 help="Interpolate data if new times are added")
62 how.add_option("--new-data-no-interpolate",
63 action="store_false",
64 dest="newDataInterpolate",
65 default=True,
66 help="Don't interpolate new data fields to the existing times")
67
68 how.add_option("--delimiter",
69 action="store",
70 dest="delimiter",
71 default=',',
72 help="Delimiter to be used between the values. Default: %default")
73
74
76 dest=self.parser.getArgs()[-1]
77 if path.exists(dest) and not self.opts.force:
78 self.error("CSV-file",dest,"exists already. Use --force to overwrite")
79 sources=self.parser.getArgs()[0:-1]
80
81 data=SpreadsheetData(csvName=sources[0],
82 title=path.splitext(path.basename(sources[0]))[0])
83
84 if self.opts.time==None:
85 self.opts.time=data.names()[0]
86
87 for s in sources[1:]:
88 addition=path.splitext(path.basename(s))[0]
89 sData=SpreadsheetData(csvName=s)
90 if self.opts.addTimes:
91 data.addTimes(time=self.opts.time,
92 times=sData.data[self.opts.time],
93 interpolate=self.opts.interpolateNewTime)
94 for n in sData.names():
95 if n!=self.opts.time:
96 d=data.resample(sData,
97 n,
98 time=self.opts.time,
99 extendData=self.opts.extendData,
100 noInterpolation=not self.opts.newDataInterpolate)
101 data.append(addition+" "+n,
102 d,
103 allowDuplicates=True)
104
105 data.writeCSV(dest,
106 delimiter=self.opts.delimiter)
107
108
109