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 PyFoam.Basics.SpreadsheetData import SpreadsheetData 
  8   
  9  from os import path 
 10   
11 -class JoinCSV(PyFoamApplication):
12 - def __init__(self, 13 args=None, 14 **kwargs):
15 description="""\ 16 Join together two or more CSV-files. Data is resampled to fit the 17 timescale of the the first CSV-file 18 """ 19 PyFoamApplication.__init__(self, 20 args=args, 21 description=description, 22 usage="%prog <source1.csv> <source2.csv> ... <dest.csv>", 23 interspersed=True, 24 changeVersion=False, 25 nr=3, 26 exactNr=False, 27 **kwargs)
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("--add-times", 56 action="store_true", 57 dest="addTimes", 58 default=False, 59 help="Actually add the times from the second file instead of interpolating") 60 how.add_option("--interpolate-new-times", 61 action="store_true", 62 dest="interpolateNewTime", 63 default=False, 64 help="Interpolate data if new times are added") 65 how.add_option("--new-data-no-interpolate", 66 action="store_false", 67 dest="newDataInterpolate", 68 default=True, 69 help="Don't interpolate new data fields to the existing times") 70 71 how.add_option("--delimiter", 72 action="store", 73 dest="delimiter", 74 default=',', 75 help="Delimiter to be used between the values. Default: %default")
76 77
78 - def run(self):
79 dest=self.parser.getArgs()[-1] 80 if path.exists(dest) and not self.opts.force: 81 self.error("CSV-file",dest,"exists already. Use --force to overwrite") 82 sources=self.parser.getArgs()[0:-1] 83 84 data=SpreadsheetData(csvName=sources[0], 85 title=path.splitext(path.basename(sources[0]))[0]) 86 87 if self.opts.time==None: 88 self.opts.time=data.names()[0] 89 90 for s in sources[1:]: 91 addition=path.splitext(path.basename(s))[0] 92 sData=SpreadsheetData(csvName=s) 93 if self.opts.addTimes: 94 data.addTimes(time=self.opts.time, 95 times=sData.data[self.opts.time], 96 interpolate=self.opts.interpolateNewTime) 97 for n in sData.names(): 98 if n!=self.opts.time: 99 d=data.resample(sData, 100 n, 101 time=self.opts.time, 102 extendData=self.opts.extendData, 103 noInterpolation=not self.opts.newDataInterpolate) 104 data.append(addition+" "+n, 105 d, 106 allowDuplicates=True) 107 108 data.writeCSV(dest, 109 delimiter=self.opts.delimiter)
110 111 # Should work with Python3 and Python2 112