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

Source Code for Module PyFoam.Applications.JoinCSV

  1  """ 
  2  Application-class that implements pyFoamJoinCSV.py 
  3  """ 
  4  from optparse import OptionGroup 
  5   
  6  from .PyFoamApplication import PyFoamApplication 
  7  from .CommonReadWriteCSV import CommonReadWriteCSV 
  8   
  9  from PyFoam.Basics.SpreadsheetData import SpreadsheetData 
 10   
 11  from os import path 
 12   
13 -class JoinCSV(PyFoamApplication, 14 CommonReadWriteCSV):
15 - def __init__(self, 16 args=None, 17 **kwargs):
18 description="""\ 19 Join together two or more CSV-files. Data is resampled to fit the 20 timescale of the the first CSV-file 21 """ 22 CommonReadWriteCSV.__init__(self) 23 PyFoamApplication.__init__(self, 24 args=args, 25 description=description, 26 usage="%prog <source1.csv> <source2.csv> ... <dest.csv>", 27 interspersed=True, 28 changeVersion=False, 29 nr=3, 30 exactNr=False, 31 **kwargs)
32
33 - def addOptions(self):
34 CommonReadWriteCSV.addOptions(self) 35 36 how=OptionGroup(self.parser, 37 "How", 38 "How the data should be joined") 39 self.parser.add_option_group(how) 40 41 how.add_option("--force", 42 action="store_true", 43 dest="force", 44 default=False, 45 help="Overwrite the destination csv if it already exists") 46 how.add_option("--extend-data", 47 action="store_true", 48 dest="extendData", 49 default=False, 50 help="Extend the time range if other files exceed the range of the first file") 51 how.add_option("--add-times", 52 action="store_true", 53 dest="addTimes", 54 default=False, 55 help="Actually add the times from the second file instead of interpolating") 56 how.add_option("--interpolate-new-times", 57 action="store_true", 58 dest="interpolateNewTime", 59 default=False, 60 help="Interpolate data if new times are added") 61 how.add_option("--new-data-no-interpolate", 62 action="store_false", 63 dest="newDataInterpolate", 64 default=True, 65 help="Don't interpolate new data fields to the existing times")
66
67 - def run(self):
68 dest=self.parser.getArgs()[-1] 69 if path.exists(dest) and not self.opts.force: 70 self.error("CSV-file",dest,"exists already. Use --force to overwrite") 71 sources=self.parser.getArgs()[0:-1] 72 73 data=SpreadsheetData(title=path.splitext(path.basename(sources[0]))[0], 74 validData=self.opts.columns, 75 validMatchRegexp=self.opts.columnsRegexp, 76 **self.dataFormatOptions(sources[0])) 77 self.printColumns(sources[0],data) 78 self.recalcColumns(data) 79 self.rawAddColumns(data) 80 81 if self.opts.time==None: 82 self.opts.time=data.names()[0] 83 84 for s in sources[1:]: 85 addition=path.splitext(path.basename(s))[0] 86 87 sData=SpreadsheetData(validData=self.opts.columns, 88 validMatchRegexp=self.opts.columnsRegexp, 89 **self.dataFormatOptions(s)) 90 self.printColumns(s,sData) 91 self.recalcColumns(sData) 92 self.rawAddColumns(sData) 93 94 if self.opts.addTimes: 95 data.addTimes(time=self.opts.time, 96 times=sData.data[self.opts.time], 97 interpolate=self.opts.interpolateNewTime) 98 for n in sData.names(): 99 if n!=self.opts.time: 100 d=data.resample(sData, 101 n, 102 time=self.opts.time, 103 extendData=self.opts.extendData, 104 noInterpolation=not self.opts.newDataInterpolate) 105 data.append(addition+" "+n, 106 d, 107 allowDuplicates=True) 108 109 self.joinedAddColumns(data) 110 111 if len(sources)>1: 112 self.printColumns("written data",data) 113 114 if self.opts.writeExcel: 115 data.getData().to_excel(dest) 116 else: 117 data.writeCSV(dest, 118 delimiter=self.opts.delimiter)
119 120 # Should work with Python3 and Python2 121