2014-06-14 5 views
1

내가 아직 파이썬으로 돌아올하려고하지만,이 문제는 내 지식 초과 :Python : csv에서 배열로 시간 간격 읽기 : numpy를 사용하여 모델 데이터를 사후 처리합니다.

주제 : 유체 역학적 후 처리 : 배열에 유압 소프트웨어의 CSV 출력을 분할 여기

이 데이터와 얼마나 멀리입니다 시간 단계

입력 파일 (아래 참조) :

첫 번째 행 : 내가 작업 코드와 함께 결과-노드의 수

두 번째 행 : H eader

세 번째 행 :이 시간 단계의 모든 결과 : 다음 시간 단계의 @ 시간 =

(이 파일 : 13,541 노드 변수) .... 다음 타임 스텝에 다시 같은.

# 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 = [이 시간 단계의 모든 요소 : 형상 = 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.... 

어쩌면 누군가 나를 도울 수 있습니다, 감사합니다 !!!

답변

0

필자가 택한 것은 전체 파일을 배열로 읽어 들이고 배열을 처리하는 대신 데이터를 읽을 때 배열을 작성하여 줄 단위로 읽는 것입니다.

파일에 설명 된대로 timestep 당 행 및 열 수를 읽은 다음 읽은 각 시간 단계 (캐쉬 목록에 추가)마다 새 배열을 만든 다음 읽은 데이터로 채 웁니다.

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

희망 하시겠습니까?

+0

고맙습니다. !!!! – user3740877

+0

그것은 이런 식으로 일했습니다 .... 라인별로 작품을 읽는 것이 더 좋게 작동합니다. – user3740877

+0

당신을 환영합니다! 문제가 해결 된 경우 허용 된 답변으로 내 대답을 선택하십시오. – franzwr

1

당신은 같은 3 차원 배열을 얻을 수 np.genfromtxt()를 사용할 수 있습니다 a[i]는 타임 스텝 i에서 출력을 나타냅니다

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) 

.