1

私はまだpythonを使いこなそうとしていますが、この問題は私の知識を超えています:

トピック: 流体力学的後処理: 配列への油圧ソフトウェアの csv 出力、タイムステップの分割

ここにデータと、私が作業コードをどこまで持ってきたかがあります:

入力ファイル (以下を参照):

最初の行: 結果ノードの数

2 行目: ヘッダー

3 行目: timestep @ time=

以下: このタイムステップのすべての結果 (このファイル: 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 タイムステップを配列に読み込むループが必要です。

結果は次のようになります: 各タイムステップの配列: この場合、それぞれ 13541 要素の 27 タイムステップ。

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

多分誰かが私を助けることができます、ありがとう!!!

4

2 に答える 2

1

np.genfromtxt()次のような 3 次元配列を取得するために使用できます。

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)

wherea[i]は timestep での出力を表しますi

于 2014-06-15T10:06:26.723 に答える
0

あなたの問題に対する私の見解は、ファイル全体を配列に読み取って配列を処理するのではなく、行ごとに読み取り、データが読み取られるときに配列を作成することです。

ファイルに記述されているようにタイム ステップごとの行数と列数を読み取り、読み取ったタイム ステップごとに新しい配列を作成し (それをリストに追加)、読み取ったデータを入力します。

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()

それが役に立てば幸い!

于 2014-06-14T22:49:54.670 に答える