gpt4 book ai didi

Python:从 csv 读取时间步长到数组:使用 numpy 后处理模型数据;

转载 作者:太空宇宙 更新时间:2023-11-03 18:20:40 25 4
gpt4 key购买 nike

我仍在尝试使用 python,但这个问题超出了我的知识范围:

主题:流体动力学后处理:液压软件的 csv 输出到数组、分割时间步

这是数据以及我的工作代码的进展情况:

输入文件(见下文):

第一行:结果节点的数量

第二行:标题

第三行:时间步@时间=

以下:此时间步的所有结果(在此文件中:13541 个节点,变量)....下一个时间步骤再次相同。

# Number of Nodes: 13541
#X Y Z depth wse
# Output at t = 0
5603.7598 4474.4902 37.470001 0 37.470001
5610.5 4461.6001 36.020001 0 36.020001
5617.25 4448.71 35.130001 0 35.130001
5623.9902 4435.8198 35.07 0 35.07
5630.7402 4422.9199 35.07 0 35.07
5761.5801 4402.79 35.369999 0 35.369999
COMMENT:....................13541 timesteps...........
# Output at t = 120.04446
5603.7598 4474.4902 37.470001 3.6977223 41.167724
5610.5 4461.6001 36.020001 4.1377293 40.15773
5617.25 4448.71 35.130001 3.9119012 39.041902
5623.9902 4435.8198 35.07 3.7923947 38.862394
5630.7402 4422.9199 35.07 3.998436 39.068436
5761.5801 4402.79 35.369999 3.9750571 39.345056
COMMENT:....................13541 timesteps...........
# Output at t = 240.06036
5603.7598 4474.4902 37.470001 11.131587 48.601588
5610.5 4461.6001 36.020001 12.564266 48.584266
5617.25 4448.71 35.130001 13.498463 48.628464
5623.9902 4435.8198 35.07 13.443041 48.513041
5630.7402 4422.9199 35.07 11.625824 46.695824
5761.5801 4402.79 35.369999 19.49551 54.865508

问题: 我需要一个循环,将 n 个时间步读入数组。

结果应该是:每个时间步长的数组:在本例中为 27 个时间步长,每个时间步长有 13541 个元素。

timestep_1=[此时间步长的所有元素:shape=13541,5]

timestep_2=[]

timestep_3[]

......

timestep_n=[]

到目前为止我的代码:

 import numpy as np
import csv
from numpy import *
import itertools

#read file to big array
array=np.array([row for row in csv.reader(open("ascii-full.csv", "rb"), delimiter='\t')])
firstRow=array[0]
secondRow=array[1]

# find out how many nodes
strfirstRow=' '.join(map(str,firstRow))
first=strfirstRow.split()
print first[4]
nodes=first[4]
nodes=float(nodes)

#count timesteps
temp=(len(array)-3)/nodes
timesteps=int(temp)+1

#split array into timesteps:
# X Y Z h(t1) h(t2) h(tn)

ts1=array[3:nodes+3]#13541
#print ts1

ts2=array[nodes+4:nodes*2+4]
#print ts2


.......
read ts3 to last timestep to arrays with loop....

也许有人可以帮助我,谢谢!

最佳答案

您可以使用np.genfromtxt()来获取3D数组,例如:

import numpy as np

gen = (a for a in open('test.txt') if not a[0] in ['#', 'C'])
a = np.genfromtxt(gen).reshape(-1, 6, 5)

其中a[i]将表示时间步i的输出。

关于Python:从 csv 读取时间步长到数组:使用 numpy 后处理模型数据;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24222708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com