1
Fork 0

PA readswash black reformat

This commit is contained in:
Edgar P. Burkhart 2022-03-02 14:42:09 +01:00
parent f27f44bbb6
commit 2092789aa2
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227

View file

@ -1,83 +1,111 @@
import numpy as np
def readswash(filename,xlen,nlay):
with open('test.txt', 'r') as f:
content = f.readlines()
fsurf = []
klay = np.zeros((7203,11,300))
brkp = []
Vxfield = np.zeros((7203,11,300))
Vyfield = np.zeros((7203,11,300))
Vzfield = np.zeros((7203,11,300))
ts = []
t = 0
for i in range(len(content)):
if content[i][33:93] == 'Layer interface k=00' :
layint0 = content[i+7].split('.')
layint1 = content[i+12].split('.')
surftemp = [float(layint0[0].split()[1])]+layint0[1:-1]+[float(layint1[0].split()[1])]+layint1[1:-1]
j = 0
while j < len(surftemp):
surftemp[j] = float(surftemp[j])
j+=1
fsurf.append(np.asarray(surftemp)/10)
if content[i][33:56] == 'Flow velocity per layer' :
k = int(content[i][83:85])
layint0 = content[i+7].split('.')
layint1 = content[i+12].split('.')
vktemp = [float(layint0[0].split()[1])]+layint0[1:-1]+[float(layint1[0].split()[1])]+layint1[1:-1]
j = 0
while j < len(vktemp):
try :
vktemp[j] = float(vktemp[j])
except :
#vktemp[j] = float(vktemp[j][-5:])
vktemp[j]=0
j+=1
if content[i][87:88] == 'X':
Vxfield[t,k,:] = np.asarray(vktemp)/1000
else:
Vyfield[t,k,:] = np.asarray(vktemp)/1000
if content[i][33:56] == 'Velocity in z-direction' :
k = int(content[i][91:93])
layint0 = content[i+7].split('.')
layint1 = content[i+12].split('.')
vktemp = [float(layint0[0].split()[1])]+layint0[1:-1]+[float(layint1[0].split()[1])]+layint1[1:-1]
j = 0
while j < len(vktemp):
try:
vktemp[j] = float(vktemp[j])
except:
vktemp[j] = 0.0
j+=1
Vzfield[t,k,:len(vktemp)] = np.asarray(vktemp)/1000
Vzfield[t,k,len(vktemp):] = np.zeros(300-len(vktemp))
if content[i][33:48] == 'Layer interface' :
k = int(content[i][91:93])
layint0 = content[i+7].split('.')
layint1 = content[i+12].split('.')
layktemp = [float(layint0[0].split()[1])]+layint0[1:-1]+[float(layint1[0].split()[1])]+layint1[1:-1]
j = 0
while j < len(layktemp):
layktemp[j] = float(layktemp[j])
j+=1
klay[t,k,:] = np.asarray(layktemp)/10
if content[i][33:52] == 'wave breaking point' :
br0 = content[i+7].split('.')
br1 = content[i+12].split('.')
brtemp = [float(br0[0].split()[1])]+br0[1:-1]+[float(br1[0].split()[1])]+br1[1:-1]
j = 0
while j < len(brtemp):
brtemp[j] = float(brtemp[j])
j+=1
brkp.append(np.asarray(brtemp))
if content[i][33:68] == 'Time in seconds from reference time' :
ts.append(float(content[i+1][7:16]))
t+=1
def readswash(filename, nx, nlay):
with open(filename, "r") as f:
content = f.readlines()
fsurf = []
klay = np.zeros((7203, nlay+1, nx))
brkp = []
Vxfield = np.zeros((7203, nlay+1, nx))
Vyfield = np.zeros((7203, nlay+1, nx))
Vzfield = np.zeros((7203, nlay+1, nx))
ts = []
t = 0
for i in range(len(content)):
if (
content[i][33:93]
== "Layer interface k=00"
):
layint0 = content[i + 7].split(".")
layint1 = content[i + 12].split(".")
surftemp = (
[float(layint0[0].split()[1])]
+ layint0[1:-1]
+ [float(layint1[0].split()[1])]
+ layint1[1:-1]
)
j = 0
while j < len(surftemp):
surftemp[j] = float(surftemp[j])
j += 1
fsurf.append(np.asarray(surftemp) / 10)
if content[i][33:56] == "Flow velocity per layer":
k = int(content[i][83:85])
layint0 = content[i + 7].split(".")
layint1 = content[i + 12].split(".")
vktemp = (
[float(layint0[0].split()[1])]
+ layint0[1:-1]
+ [float(layint1[0].split()[1])]
+ layint1[1:-1]
)
j = 0
while j < len(vktemp):
try:
vktemp[j] = float(vktemp[j])
except:
# vktemp[j] = float(vktemp[j][-5:])
vktemp[j] = 0
j += 1
if content[i][87:88] == "X":
Vxfield[t, k, :] = np.asarray(vktemp) / 1000
else:
Vyfield[t, k, :] = np.asarray(vktemp) / 1000
if content[i][33:56] == "Velocity in z-direction":
k = int(content[i][91:93])
layint0 = content[i + 7].split(".")
layint1 = content[i + 12].split(".")
vktemp = (
[float(layint0[0].split()[1])]
+ layint0[1:-1]
+ [float(layint1[0].split()[1])]
+ layint1[1:-1]
)
j = 0
while j < len(vktemp):
try:
vktemp[j] = float(vktemp[j])
except:
vktemp[j] = 0.0
j += 1
Vzfield[t, k, : len(vktemp)] = np.asarray(vktemp) / 1000
Vzfield[t, k, len(vktemp) :] = np.zeros(nx - len(vktemp))
if content[i][33:48] == "Layer interface":
k = int(content[i][91:93])
layint0 = content[i + 7].split(".")
layint1 = content[i + 12].split(".")
layktemp = (
[float(layint0[0].split()[1])]
+ layint0[1:-1]
+ [float(layint1[0].split()[1])]
+ layint1[1:-1]
)
j = 0
while j < len(layktemp):
layktemp[j] = float(layktemp[j])
j += 1
klay[t, k, :] = np.asarray(layktemp) / 10
if content[i][33:52] == "wave breaking point":
br0 = content[i + 7].split(".")
br1 = content[i + 12].split(".")
brtemp = (
[float(br0[0].split()[1])]
+ br0[1:-1]
+ [float(br1[0].split()[1])]
+ br1[1:-1]
)
j = 0
while j < len(brtemp):
brtemp[j] = float(brtemp[j])
j += 1
brkp.append(np.asarray(brtemp))
if content[i][33:68] == "Time in seconds from reference time":
ts.append(float(content[i + 1][7:16]))
t += 1
return fsurf, Vxfield, Vyfield, Vzfield, brkp, klay
return fsurf, Vxfield, Vyfield, Vzfield, brkp, klay
if __name__ == "__main__":
print(readswash("test.txt", 1250, 10))