def putLQD(fle,head,vrs,date0,time,xyz): # ~~ Write head ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ core = head from datetime import datetime core.append( "#Date&Time: "+datetime(*date0).strftime("%d/%m/%Y %H:%M:%S") ) names = 'T'; units = 's' if xyz.ndim == 2: for name,unit in vrs: names += ' ' + name.strip().replace(' ','_') units += ' ' + unit.strip().replace(' ','_') core.append(names+'\n'+units) elif xyz.ndim == 3: for ivar in range(len(vrs[0])): for inod in vrs[1]: names += ' ' + vrs[0][ivar][0].strip().replace(' ','_') + '(' + str(inod) + ')' units += ' ' + vrs[0][ivar][1].strip().replace(' ','_') core.append(names+'\n'+units) if xyz.ndim == 2: for itim in range(xyz.shape[1]): line = str(time[itim]) for ivar in range(xyz.shape[0]): line += ' ' + str(xyz[ivar][itim]) core.append(line) elif xyz.ndim == 3: for itim in range(xyz.shape[2]): line = str(time[itim]) for ivar in range(xyz.shape[0]): for inod in range(xyz.shape[1]): line += ' ' + str(xyz[ivar][inod][itim]) core.append(line) # ~~ Put all ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ putFileContent(fle,core) return
def putContent(self,fileName,head=[]): # ~~> file extension processing _,tail = path.splitext(fileName) if tail[1:] != self.fileType: if head != []: head = ['\n'.join(head).replace(':FileType '+self.fileType,':FileType '+tail[1:])] self.fileType = tail[1:] # ~~> write head if head != []: core = head else: core = [':FileType '+self.fileType+' ASCII EnSim 1.0', ':Application BlueKenue', ':Version 3.2.24', ':WrittenBy sebourban', ':CreationDate Thu, Dec 08, 2011 02:47 PM', ':Name ' + path.basename(fileName), #':AttributeName 1 level', #':AttributeType 1 float', #':AttributeUnits 1 m', ':EndHeader' ] # ~~> look for closed lines if self.type == []: for ip in self.poly: if isClose(ip[0][:2],ip[len(ip)-1][:2]): self.type.append(1) else: self.type.append(0) # ~~> fill-up empty attributes if self.atrbut == {}: self.atrbut = {1:['ArbitraryName1']} for _ in self.poly: self.atrbut[1].append(0) self.oatrbut = [1] # ~~> fill-up attribute names if head == []: for i,o in zip(range(len(self.oatrbut)),self.oatrbut): core.insert(-1,':AttributeName '+repr(i+1)+' '+repr(o)) # ~~ Write body ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for i,ip,iv,it in zip(range(len(self.poly)),self.poly,self.vals,self.type): il = len(ip) if il != 0 and not isClose(ip[0],ip[len(ip)-1]): il += it line = repr(il) for o in self.oatrbut: line = line + ' ' + str(self.atrbut[o][i]) core.append(line) if self.fileType == 'i2s': for xyi in ip: core.append(repr(xyi[0])+' '+repr(xyi[1])) if il != len(ip): core.append(repr(ip[0][0])+' '+repr(ip[0][1])) elif self.fileType == 'i3s': if np.shape(iv)[1] == 0: for xyi in ip: core.append(repr(xyi[0])+' '+repr(xyi[1])+' 0.0') if il != len(ip): core.append(repr(ip[0][0])+' '+repr(ip[0][1])+' 0.0') else: for xyi,val in zip(ip,iv): core.append(repr(xyi[0])+' '+repr(xyi[1])+' '+' '.join([ repr(v) for v in val ])) if il != len(ip): core.append(repr(ip[0][0])+' '+repr(ip[0][1])+' '+' '.join([ repr(v) for v in iv[0] ])) # ~~ Put all ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ putFileContent(fileName,core)
def putLQD(fle, head, vrs, date0, time, xyz): # ~~ Write head ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ core = head from datetime import datetime core.append("#Date&Time: " + datetime(*date0).strftime("%d/%m/%Y %H:%M:%S")) names = "T" units = "s" if xyz.ndim == 2: for name, unit in vrs: names += " " + name.strip().replace(" ", "_") units += " " + unit.strip().replace(" ", "_") core.append(names + "\n" + units) elif xyz.ndim == 3: for ivar in range(len(vrs[0])): for inod in vrs[1]: names += " " + vrs[0][ivar][0].strip().replace( " ", "_") + "(" + str(inod) + ")" units += " " + vrs[0][ivar][1].strip().replace(" ", "_") core.append(names + "\n" + units) if xyz.ndim == 2: for itim in range(xyz.shape[1]): line = str(time[itim]) for ivar in range(xyz.shape[0]): line += " " + str(xyz[ivar][itim]) core.append(line) elif xyz.ndim == 3: for itim in range(xyz.shape[2]): line = str(time[itim]) for ivar in range(xyz.shape[0]): for inod in range(xyz.shape[1]): line += " " + str(xyz[ivar][inod][itim]) core.append(line) # ~~ Put all ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ putFileContent(fle, core) return
def putContent(self,fileName): # standard part of the CONLIM core = [] for ifr in range(self.NPTFR): if self.INDEX[ifr] != 0: c = ( ' '.join(['{0[' + repr(i) + ']}' for i in range(len(self.BOR[ifr]))]) ).format(self.BOR[ifr]) if self.NPTIR != {} and self.IFAPAR != {}: c += ' '+repr(self.POR[ifr][0]) c += ' '+repr(float(self.POR[ifr][1])) c += ' '+repr(float(self.POR[ifr][2])) c += ' '+repr(self.POR[ifr][3]) core.append( c ) if self.NPTIR != {} and self.IFAPAR != {}: # parrallel part 1 of the CONLIM -- format with i7 /!\ ntr = self.NPTIR.keys() ntr.sort() core.append(str(len(ntr))) for itr in ntr: c = self.NPTIR[itr] c.sort() c.extend([-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]) s = repr(itr+1).rjust(7) + ' ' + \ repr(c[0]).rjust(7) + ' ' + repr(c[1]).rjust(7) + ' ' + repr(c[2]).rjust(7) + ' ' + \ repr(c[3]).rjust(7) + ' ' + repr(c[4]).rjust(7) + ' ' + repr(c[5]).rjust(7) + ' ' + \ repr(c[6]).rjust(7) + ' ' + repr(c[7]).rjust(7) + ' ' + repr(c[8]).rjust(7) core.append( s ) # parrallel part 2 of the CONLIM -- format with i9 /!\ ntr = self.IFAPAR.keys() ntr.sort() core.append(str(len(ntr))) for itr in ntr: c = self.IFAPAR[itr] s = repr(itr+1).rjust(9) + ' ' + \ repr(c[1]).rjust(9) + ' ' + repr(c[3]).rjust(9) + ' ' + repr(c[5]).rjust(9) + ' ' + \ repr(c[0]+1).rjust(9) + ' ' + repr(c[2]+1).rjust(9) + ' ' + repr(c[4]+1).rjust(9) core.append( s ) core.append("") putFileContent(fileName,core)
def putXYn(file,head,fileType,xyz): # ~~ Write head ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ core = head #if head != []: core = head #<else: core = [':FileType '+fileType+' ASCII EnSim 1.0', # ':Application BlueKenue', ':Version 3.2.24', # ':WrittenBy sebourban', ':CreationDate Thu, Dec 08, 2011 02:47 PM', # ':Name ' + path.basename(file), # ':AttributeUnits 1 m', # ':EndHeader' ] # ~~ Write body ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if fileType == 'xy': for _ in xyz: core.append(str(xyz[0])+' '+str(xyz[1])) elif fileType == 'xyz': for _ in xyz: core.append(str(xyz[0])+' '+str(xyz[1])+' '+str(xyz[2])) # ~~ Put all ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ putFileContent(file,core) return
def putXYn(file, head, fileType, xyz): # ~~ Write head ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ core = head #if head != []: core = head #<else: core = [':FileType '+fileType+' ASCII EnSim 1.0', # ':Application BlueKenue', ':Version 3.2.24', # ':WrittenBy sebourban', ':CreationDate Thu, Dec 08, 2011 02:47 PM', # ':Name ' + path.basename(file), # ':AttributeUnits 1 m', # ':EndHeader' ] # ~~ Write body ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if fileType == 'xy': for _ in xyz: core.append(str(xyz[0]) + ' ' + str(xyz[1])) elif fileType == 'xyz': for _ in xyz: core.append(str(xyz[0]) + ' ' + str(xyz[1]) + ' ' + str(xyz[2])) # ~~ Put all ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ putFileContent(file, core) return
def translateCAS(cas, frgb): casLines = getFileContent(cas) core = [] for i in range(len(casLines)): # ~~> scan through to remove all comments casLines[i] = casLines[i].replace('"""', "'''").replace('"', "'") proc = re.match(key_comment, casLines[i] + '/') head = proc.group('before').strip() core.append(head) casStream = ' '.join(core) frLines = [] gbLines = [] for i in range(len(casLines)): # ~~> split comments casLines[i] = casLines[i].replace('"""', "'''").replace('"', "'") proc = re.match(key_comment, casLines[i] + '/') head = proc.group('before').strip() tail = proc.group('after').rstrip('/').strip() # /!\ is not translated # ~~ special keys starting with '&' p = re.match(key_none, head + ' ') if p: head = '' tail = casLines[i].strip() frline = head gbline = head if head != '' and casStream == '': raise Exception([{ 'name': 'translateCAS', 'msg': 'could not translate this cas file after the line:\n' + head }]) # ~~> this is a must for multiple keywords on one line while casStream != '': proc = re.match(key_equals, casStream) if not proc: raise Exception([{ 'name': 'scanCAS', 'msg': '... hmmm, did not see this one coming ...\n around there :' + casStream[:100] }]) kw = proc.group('key').strip() if kw not in head: break # move on to next line # ~~> translate the keyword head = head.replace(kw, '', 1) if kw.upper() in frgb['GB']: frline = frline.replace(kw, frgb['GB'][kw], 1) if kw.upper() in frgb['FR']: gbline = gbline.replace(kw, frgb['FR'][kw], 1) # ~~> look for less obvious keywords casStream = proc.group('after') # still hold the separator proc = re.match(val_equals, casStream) if not proc: raise Exception([{ 'name': 'translateCAS', 'msg': 'no value to keyword: ' + kw }]) while proc: casStream = proc.group('after') # still hold the separator proc = re.match(val_equals, casStream) # final append if frline != '': frline = ' ' + frline frLines.append(frline + tail) if gbline != '': gbline = ' ' + gbline gbLines.append(gbline + tail) # ~~ print FR and GB versions of the CAS file putFileContent(cas + '.fr', frLines) putFileContent(cas + '.gb', gbLines) return cas + '.fr', cas + '.gb'
def translateCAS(cas, frgb): casLines = getFileContent(cas) core = [] for i in range(len(casLines)): # ~~> scan through to remove all comments casLines[i] = casLines[i].replace('"""', "'''").replace('"', "'") proc = re.match(key_comment, casLines[i] + "/") head = proc.group("before").strip() core.append(head) casStream = " ".join(core) frLines = [] gbLines = [] for i in range(len(casLines)): # ~~> split comments casLines[i] = casLines[i].replace('"""', "'''").replace('"', "'") proc = re.match(key_comment, casLines[i] + "/") head = proc.group("before").strip() tail = proc.group("after").rstrip("/").strip() # /!\ is not translated # ~~ special keys starting with '&' p = re.match(key_none, head + " ") if p: head = "" tail = casLines[i].strip() frline = head gbline = head if head != "" and casStream == "": raise Exception( [{"name": "translateCAS", "msg": "could not translate this cas file after the line:\n" + head}] ) # ~~> this is a must for multiple keywords on one line while casStream != "": proc = re.match(key_equals, casStream) if not proc: raise Exception( [ { "name": "scanCAS", "msg": "... hmmm, did not see this one coming ...\n around there :" + casStream[:100], } ] ) kw = proc.group("key").strip() if kw not in head: break # move on to next line # ~~> translate the keyword head = head.replace(kw, "", 1) if kw.upper() in frgb["GB"]: frline = frline.replace(kw, frgb["GB"][kw], 1) if kw.upper() in frgb["FR"]: gbline = gbline.replace(kw, frgb["FR"][kw], 1) # ~~> look for less obvious keywords casStream = proc.group("after") # still hold the separator proc = re.match(val_equals, casStream) if not proc: raise Exception([{"name": "translateCAS", "msg": "no value to keyword: " + kw}]) while proc: casStream = proc.group("after") # still hold the separator proc = re.match(val_equals, casStream) # final append if frline != "": frline = " " + frline frLines.append(frline + tail) if gbline != "": gbline = " " + gbline gbLines.append(gbline + tail) # ~~ print FR and GB versions of the CAS file putFileContent(cas + ".fr", frLines) putFileContent(cas + ".gb", gbLines) return cas + ".fr", cas + ".gb"
def putContent(self, fileName, head=[]): # ~~> file extension processing _, tail = path.splitext(fileName) if tail[1:] != self.fileType: if head != []: head = [ '\n'.join(head).replace(':FileType ' + self.fileType, ':FileType ' + tail[1:]) ] self.fileType = tail[1:] # ~~> write head if head != []: core = head else: core = [ ':FileType ' + self.fileType + ' ASCII EnSim 1.0', ':Application BlueKenue', ':Version 3.2.24', ':WrittenBy sebourban', ':CreationDate Thu, Dec 08, 2011 02:47 PM', ':Name ' + path.basename(fileName), #':AttributeName 1 level', #':AttributeType 1 float', #':AttributeUnits 1 m', ':EndHeader' ] # ~~> look for closed lines if self.type == []: for ip in self.poly: if isClose(ip[0][:2], ip[len(ip) - 1][:2]): self.type.append(1) else: self.type.append(0) # ~~> fill-up empty attributes if self.atrbut == {}: self.atrbut = {1: ['ArbitraryName1']} for _ in self.poly: self.atrbut[1].append(0) self.oatrbut = [1] # ~~> fill-up attribute names if head == []: for i, o in zip(range(len(self.oatrbut)), self.oatrbut): core.insert(-1, ':AttributeName ' + repr(i + 1) + ' ' + repr(o)) # ~~ Write body ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for i, ip, iv, it in zip(range(len(self.poly)), self.poly, self.vals, self.type): il = len(ip) if il != 0 and not isClose(ip[0], ip[len(ip) - 1]): il += it line = repr(il) for o in self.oatrbut: line = line + ' ' + str(self.atrbut[o][i]) core.append(line) if self.fileType == 'i2s': for xyi in ip: core.append(repr(xyi[0]) + ' ' + repr(xyi[1])) if il != len(ip): core.append(repr(ip[0][0]) + ' ' + repr(ip[0][1])) elif self.fileType == 'i3s': if np.shape(iv)[1] == 0: for xyi in ip: core.append(repr(xyi[0]) + ' ' + repr(xyi[1]) + ' 0.0') if il != len(ip): core.append( repr(ip[0][0]) + ' ' + repr(ip[0][1]) + ' 0.0') else: for xyi, val in zip(ip, iv): core.append( repr(xyi[0]) + ' ' + repr(xyi[1]) + ' ' + ' '.join([repr(v) for v in val])) if il != len(ip): core.append( repr(ip[0][0]) + ' ' + repr(ip[0][1]) + ' ' + ' '.join([repr(v) for v in iv[0]])) # ~~ Put all ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ putFileContent(fileName, core)
def translateCAS(cas,frgb): casLines = getFileContent(cas) core = [] for i in range(len(casLines)): # ~~> scan through to remove all comments casLines[i] = casLines[i].replace('"""',"'''").replace('"',"'") proc = re.match(key_comment,casLines[i]+'/') head = proc.group('before').strip() core.append(head) casStream = ' '.join(core) frLines = []; gbLines = [] for i in range(len(casLines)): # ~~> split comments casLines[i] = casLines[i].replace('"""',"'''").replace('"',"'") proc = re.match(key_comment,casLines[i]+'/') head = proc.group('before').strip() tail = proc.group('after').rstrip('/').strip() # /!\ is not translated # ~~ special keys starting with '&' p = re.match(key_none,head+' ') if p: head = '' tail = casLines[i].strip() frline = head gbline = head if head != '' and casStream == '': raise Exception([{'name':'translateCAS','msg':'could not translate this cas file after the line:\n'+head}]) # ~~> this is a must for multiple keywords on one line while casStream != '': proc = re.match(key_equals,casStream) if not proc: raise Exception([{'name':'scanCAS','msg':'... hmmm, did not see this one coming ...\n around there :'+casStream[:100]}]) kw = proc.group('key').strip() if kw not in head: break # move on to next line # ~~> translate the keyword head = head.replace(kw,'',1) if kw.upper() in frgb['GB']: frline = frline.replace(kw,frgb['GB'][kw],1) if kw.upper() in frgb['FR']: gbline = gbline.replace(kw,frgb['FR'][kw],1) # ~~> look for less obvious keywords casStream = proc.group('after') # still hold the separator proc = re.match(val_equals,casStream) if not proc: raise Exception([{'name':'translateCAS','msg':'no value to keyword: '+kw}]) while proc: casStream = proc.group('after') # still hold the separator proc = re.match(val_equals,casStream) # final append if frline != '': frline = ' ' + frline frLines.append(frline + tail) if gbline != '': gbline = ' ' + gbline gbLines.append(gbline + tail) # ~~ print FR and GB versions of the CAS file putFileContent(cas+'.fr',frLines) putFileContent(cas+'.gb',gbLines) return cas+'.fr',cas+'.gb'