简体   繁体   English

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

[英]Python: read timesteps from csv to arrays: Post-processing model-data with numpy;

I am still trying to come around with python, but this problem exceeds my knowledge: 我仍在尝试使用python,但是这个问题超出了我的知识范围:

Topic: hydrodynamic postprocessing: csv output of hydraulic software to array, split timesteps 主题:流体动力后处理:液压软件的csv输出到阵列,分段时间步

Here is the data and how far i came with a working code: 这是数据,以及有效代码的有效距离:

Input-file (see below): 输入文件(见下文):

First row: Number of result-nodes 第一行:结果节点数

Second row: Header 第二行:标题

Third row: timestep @ time= 第三行:timestep @ time =

Following: all results of this timestep (in this file: 13541 nodes, variable) ....the same again for next timestep. 随后:该时间步的所有结果(在此文件中: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

PROBLEM: I need a loop, which reads in n-timesteps into arrays. 问题:我需要一个循环,将n个时间步读入数组。

The result should be: array for each timestep: in this case 27 timesteps with 13541 elements each. 结果应为:每个时间步的数组:在这种情况下,为27个时间步,每个时间步都有13541个元素。

timestep_1=[all elements of this timestep: shape=13541,5] timestep_1 = [此时间步长的所有元素:shape = 13541,5]

timestep_2=[] timestep_2 = []

timestep_3[] timestep_3 []

........ ........

timestep_n=[] timestep_n = []

My code so far: 到目前为止,我的代码:

 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....

Maybe someone can help me, thanks!!! 也许有人可以帮助我,谢谢!!!

You can use np.genfromtxt() to get a 3-D array like: 您可以使用np.genfromtxt()获得3-D数组,例如:

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)

where a[i] will represent the output at timestep i . 其中a[i]代表时间步长i的输出。

My take on your problem is, instead of reading the whole file into an array and process the array, read it line by line, creating the arrays as the data is read. 我对您的问题的看法是,与其将整个文件读入一个数组并进行处理,不如逐行读取它,而是在读取数据时创建数组。

I read the number of rows and columns per timestep as described in the file, then create a new array for each timestep read (adding it to a list), then populating it with the read data. 我按文件中所述读取每个时间步的行数和列数,然后为每个读取的时间步创建一个新数组(将其添加到列表中),然后使用读取的数据填充它。

import numpy as np

timesteps = []
timestep_results = []

f = open("ascii-full.csv", "rb")

# First line is number of rows (not counting the initial #)
rows = int(f.readline().strip()[1:].split()[-1])
counter = 0

# Second line is number of columns
columns = len(f.readline().strip().split())

# Next lines
for line in f:
    if line.startswith("#"):
        # it's a header: add time to timestep list, begin new array
        timesteps.append( float(line.strip().split("=")[1]) )
        timestep_results.append( np.zeros((rows, columns)) )
        counter = 0
    else:
        # it's data: add to array in appropiate row
        timestep_results[-1][counter] = map(float, line.strip().split())
        counter += 1

f.close()

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM