def _write(self): """ Write a file (overrided) """ f = FortranFile(self.filename, mode='w') # Default omnivor binary header f.writeInts(self.data['MK'], 'i') f.writeInts(self.data['itime'], 'i') f.writeString(self.data['version']) f.writeInts(self.data['file_id'], 'i') f.writeString(self.data['sversion']) # Velocity field f.writeString(self.data['stype']) f.writeInts(self.data['is_grid'], 'i') f.writeInts(self.data['nCPs'], 'i') if self.data['MK'] == 8: real_char = 'd' else: real_char = 'f' if self.data['is_grid']: f.writeInts(self.data['n1'], 'i') f.writeInts(self.data['n2'], 'i') f.writeInts(self.data['n3'], 'i') f.writeInts(self.data['is_straight'], 'i') f.writeReals(self.data['v1'], real_char) f.writeReals(self.data['v2'], real_char) f.writeReals(self.data['v3'], real_char) CPs = self.data['CPs'].flatten(order='F') Utot = self.data['Utot'].flatten(order='F') f.writeReals(CPs, real_char) f.writeReals(Utot, real_char)
def _read(self): """ Read the file (overrided) """ with open(self.filename, 'r') as f: # self.title = f.read() # initializng data dictionary print('Opening: %s' % self.filename) self.data = {} f = FortranFile(self.filename) # Default omnivor binary header self.data['MK'] = f.readInts('i') self.data['itime'] = f.readInts('i') self.data['version'] = f.readString() self.data['file_id'] = f.readInts('i') self.data['sversion'] = f.readString() # Velocity field self.data['stype'] = f.readString() self.data['is_grid'] = f.readInts('i') nCPs = f.readInts('i') self.data['nCPs'] = nCPs if self.data['MK'] == 8: real_char = 'd' else: real_char = 'f' if self.data['is_grid']: #print('File is a velocity grid file') n1 = f.readInts('i') n2 = f.readInts('i') n3 = f.readInts('i') self.data['n1'] = n1 self.data['n2'] = n2 self.data['n3'] = n3 self.data['is_straight'] = f.readInts('i') self.data['v1'] = f.readReals(real_char) self.data['v2'] = f.readReals(real_char) self.data['v3'] = f.readReals(real_char) CPs_raw = f.readReals(real_char) Utot_raw = f.readReals(real_char) CPs = np.reshape(CPs_raw, (3, nCPs), order='F') Utot = np.reshape(Utot_raw, (3, nCPs), order='F') acc = -1 CPsTab = np.zeros((3, n1, n2, n3)) UtotTab = np.zeros((3, n1, n2, n3)) # Reshaping the nasty way (this is natural order). for i in range(0, n1): for j in range(0, n2): for k in range(0, n3): acc = acc + 1 CPsTab[0:3, i, j, k] = CPs[0:3, acc] UtotTab[0:3, i, j, k] = Utot[0:3, acc] self.data['CPs'] = CPs self.data['CPsTab'] = CPsTab self.data['Utot'] = Utot self.data['UtotTab'] = UtotTab self.plot()
def _write(self): """ Write a file (overrided) """ f = FortranFile(self.filename,mode='w') # Default omnivor binary header f.writeInts ( self.data['MK'] , 'i' ) f.writeInts ( self.data['itime'] , 'i' ) f.writeString ( self.data['version'] ) f.writeInts ( self.data['file_id'] , 'i' ) f.writeString ( self.data['sversion'] ) # Velocity field f.writeString ( self.data['stype'] ) f.writeInts ( self.data['is_grid'] , 'i' ) f.writeInts ( self.data['nCPs'] , 'i' ) if self.data['MK'] == 8: real_char='d' else: real_char='f' if self.data['is_grid']: f.writeInts ( self.data['n1'] , 'i' ) f.writeInts ( self.data['n2'] , 'i' ) f.writeInts ( self.data['n3'] , 'i' ) f.writeInts ( self.data['is_straight'] , 'i' ) f.writeReals ( self.data['v1'] , real_char ) f.writeReals ( self.data['v2'] , real_char ) f.writeReals ( self.data['v3'] , real_char ) CPs = self.data['CPs'].flatten(order = 'F') Utot = self.data['Utot'].flatten(order = 'F') f.writeReals(CPs,real_char) f.writeReals(Utot,real_char)
def _read(self): """ Read the file (overrided) """ # initializng data dictionary self.data={} f = FortranFile(self.filename) # Default omnivor binary header self.data['MK'] = f.readInts('i') self.data['itime'] = f.readInts('i') self.data['version'] = f.readString() self.data['file_id'] = f.readInts('i') self.data['sversion'] = f.readString() # Velocity field self.data['stype'] = f.readString() self.data['is_grid'] = f.readInts('i') nCPs = f.readInts('i') self.data['nCPs'] = nCPs if self.data['MK'] == 8: real_char='d' else: real_char='f' if self.data['is_grid']: #print('File is a velocity grid file') n1 = f.readInts('i') n2 = f.readInts('i') n3 = f.readInts('i') self.data['n1'] = n1 self.data['n2'] = n2 self.data['n3'] = n3 self.data['is_straight'] = f.readInts('i') self.data['v1'] = f.readReals(real_char) self.data['v2'] = f.readReals(real_char) self.data['v3'] = f.readReals(real_char) CPs_raw = f.readReals(real_char) Utot_raw = f.readReals(real_char) CPs = np.reshape(CPs_raw,(3,nCPs),order = 'F') Utot = np.reshape(Utot_raw,(3,nCPs),order = 'F') acc=-1 CPsTab = np.zeros((3, n1,n2,n3)) UtotTab = np.zeros((3, n1,n2,n3)) # Reshaping the nasty way (this is natural order). for i in range(0,n1): for j in range(0,n2): for k in range(0,n3): acc=acc+1 CPsTab[0:3,i,j,k] = CPs[0:3,acc] UtotTab[0:3,i,j,k] = Utot[0:3,acc] self.data['CPs'] = CPs self.data['CPsTab'] = CPsTab self.data['Utot'] = Utot self.data['UtotTab'] = UtotTab
#!/usr/bin/python2 import numpy as np from fortran_file import FortranFile f = FortranFile('FCC.dat') reals = f.readReals(prec='d') matr = reals.reshape((6369/3, 3)) np.savetxt('out.mat', matr) #>>> f = FortranFile('tetrahedron.dat','@','i','w') #>>> f.writeReals([1,-1,-1,1,1,-1,1,-1,1,1,-1,-1],'d') #>>> f.close()