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

Source Code for Module PyFoam.Applications.ConvertToCSV

  1  """ 
  2  Application-class that implements pyFoamConvertToCSV.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 ConvertToCSV(PyFoamApplication, 14 CommonReadWriteCSV):
15 - def __init__(self, 16 args=None, 17 **kwargs):
18 description="""\ 19 Takes a plain file with column-oriented data and converts it to a 20 csv-file. If more than one file are specified, they are joined 21 according to the first column. 22 23 Note: the first file determines the resolution of the time-axis 24 """ 25 CommonReadWriteCSV.__init__(self) 26 PyFoamApplication.__init__(self, 27 args=args, 28 description=description, 29 usage="%prog <source> ... <dest.csv>", 30 interspersed=True, 31 changeVersion=False, 32 nr=2, 33 exactNr=False, 34 **kwargs)
35
36 - def addOptions(self):
37 CommonReadWriteCSV.addOptions(self) 38 39 how=OptionGroup(self.parser, 40 "How", 41 "How the data should be joined") 42 self.parser.add_option_group(how) 43 44 how.add_option("--force", 45 action="store_true", 46 dest="force", 47 default=False, 48 help="Overwrite the destination csv if it already exists") 49 how.add_option("--extend-data", 50 action="store_true", 51 dest="extendData", 52 default=False, 53 help="Extend the time range if other files exceed the range of the first file") 54 how.add_option("--names-from-filename", 55 action="store_true", 56 dest="namesFromFilename", 57 default=False, 58 help="Read the value names from the file-name (assuming that names are split by _ and the names are in the tail - front is the general filename)")
59
60 - def run(self):
61 dest=self.parser.getArgs()[-1] 62 if path.exists(dest) and not self.opts.force: 63 self.error("CSV-file",dest,"exists already. Use --force to overwrite") 64 sources=self.parser.getArgs()[0:-1] 65 66 diffs=[None] 67 if len(sources)>1: 68 # find differing parts 69 commonStart=1e4 70 commonEnd=1e4 71 for s in sources[1:]: 72 a=path.abspath(sources[0]) 73 b=path.abspath(s) 74 start=0 75 end=0 76 for i in range(min(len(a),len(b))): 77 start=i 78 if a[i]!=b[i]: 79 break 80 commonStart=min(commonStart,start) 81 for i in range(min(len(a),len(b))): 82 end=i 83 if a[-(i+1)]!=b[-(i+1)]: 84 break 85 commonEnd=min(commonEnd,end) 86 diffs=[] 87 for s in sources: 88 b=path.abspath(s) 89 if commonEnd>0: 90 diffs.append(b[commonStart:-(commonEnd)]) 91 else: 92 diffs.append(b[commonStart:]) 93 94 names=None 95 title=path.splitext(path.basename(sources[0]))[0] 96 if self.opts.namesFromFilename: 97 names=path.splitext(path.basename(sources[0]))[0].split("_") 98 title=None 99 100 data=SpreadsheetData(names=names, 101 timeName=self.opts.time, 102 validData=self.opts.columns, 103 validMatchRegexp=self.opts.columnsRegexp, 104 title=title, 105 **self.dataFormatOptions(sources[0])) 106 self.printColumns(sources[0],data) 107 self.recalcColumns(data) 108 self.rawAddColumns(data) 109 110 if self.opts.time==None: 111 self.opts.time=data.names()[0] 112 113 for i,s in enumerate(sources[1:]): 114 names=None 115 title=path.splitext(path.basename(s))[0] 116 if self.opts.namesFromFilename: 117 names=title.split("_") 118 title=None 119 sData=SpreadsheetData(names=names, 120 timeName=self.opts.time, 121 validData=self.opts.columns, 122 validMatchRegexp=self.opts.columnsRegexp, 123 title=title, 124 **self.dataFormatOptions(s)) 125 self.printColumns(s,sData) 126 self.recalcColumns(sData) 127 self.rawAddColumns(sData) 128 129 for n in sData.names(): 130 if n!=self.opts.time and (self.opts.columns==[] or data.validName(n,self.opts.columns,True)): 131 d=data.resample(sData, 132 n, 133 time=self.opts.time, 134 extendData=self.opts.extendData) 135 data.append(diffs[i+1]+" "+n,d) 136 137 self.joinedAddColumns(data) 138 139 if len(sources)>1: 140 self.printColumns("written data",data) 141 142 if self.opts.writeExcel: 143 data.getData().to_excel(dest) 144 else: 145 data.writeCSV(dest, 146 delimiter=self.opts.delimiter)
147 148 # Should work with Python3 and Python2 149