2013-05-20 74 views
-1

我正在嘗試使用以下腳本將數據寫入HDF5文件。將數據寫入HDF5文件

我正在運行代碼的終端窗口給出正確的輸出,並且輸出HDF5文件被創建,但輸出文件的表中沒有數據。

任何提示?


import tables 
import datetime 
import time 
import csv 
from tables import openFile, IsDescription, Float64Col, Int16Col 
from scipy import array 
import numpy as np 
import os 

# 
output_filename='events_per_hour_output.h5' 
# 

if __name__ == '__main__': 
    # Read data from file 
    input_file='data_s20002_20121101_20121105.h5' 
#Find station ID, start date and stop date from filename 
    print "input_file: ",str(input_file) 
    stationID=str(input_file[6:11]) 
    print "stationID: ",stationID 
    start_year=str(input_file[12:16]) 
    print "start_year: ", start_year 
    start_month=str(input_file[16:18]) 
    print "start_month: ", start_month 
    start_day=str(input_file[18:20]) 
    print "start_day",start_day 
    stop_year=str(input_file[21:25]) 
    print "stop_year ",stop_year 
    stop_month=str(input_file[25:27]) 
    print "stop_month ",stop_month 
    stop_day=str(input_file[27:29]) 
    print "stop_day ",stop_day 
    print '' 

    with tables.openFile(str(input_file), 'r') as datafile: 
      data = [(x['timestamp'], x['nanoseconds'], x['ext_timestamp'], x['pulseheights']) for 
        x in datafile.root.s20002.events] 


# 
class variable_01(IsDescription): 
unix_timestamp = Float64Col() 
events = Float64Col() 
GMT = Float64Col() 
step = Float64Col() 
# 
# 
start_date=datetime.datetime(int(start_year),int(start_month),int(start_day)) 
print "start_date: ",start_date 
#start_date=time.gmtime(int(start_year),int(start_month),int(start_day)) 
stop_date=datetime.datetime(int(stop_year),int(stop_month),int(stop_day)) 
print "stop_date: ",stop_date 
print"start_date.timetuple(): ",start_date.timetuple() 
start_unix_time=time.mktime(start_date.timetuple()) 
#start_unix_time=time.gmtime(start_date.timetuple()) 
stop_unix_time=time.mktime(stop_date.timetuple()) 
step_length=3600# 3600 seconds = 1 hour 
total_length=0 
# 
with openFile(output_filename, 'w') as data_splice: 
      group_variable_01 = data_splice.createGroup("/", 'hisparc_vantage') 
      table_variable_01 = data_splice.createTable(group_variable_01, 'table_01', variable_01) 
      dummy_01 = table_variable_01.row 

# 
for hour_step in range(int(start_unix_time),int(stop_unix_time),step_length): 
dummy_01['step']=1 
dummy_01.append 

result = [] 
for row in data: 
    if (hour_step <= row[0]) and (row[0] < hour_step+step_length):# 
     result.append(row) 
print "UTC start time: ",time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(hour_step)) 
print "len(result) : ", len(result) 
# 
# 

dummy_01['unix_timestamp'] = hour_step 
dummy_01['events']=len(result) 
print "dummy_01['events']=",dummy_01['events'] 
print "dummy_01['unix_timestamp'] =", dummy_01['unix_timestamp'] 
dummy_01.append() 
print '' 
table_variable_01.flush 
print "Done." 
+0

如果這不是你的另一個問題的重複[Python的 - 問題數據寫入HDF5(http://stackoverflow.com/questions/16639813/python-problems-寫入數據到hdf5)已關閉,你最好解釋一下。 –

回答

1

你應該看看h5py。該模塊提供了讀取/寫入HDF5文件的非常簡單的功能。如果您讀取圖像或柵格等數據字段,則可以直接作爲numpy數組使用。

0
import h5py 

# Create random data 
import numpy as np 
data_matrix = np.random.uniform(-1, 1, size=(10, 3)) 

# Write data to HDF5 
data_file = h5py.File('file.hdf5', 'w') 
data_file.create_dataset('group_name', data=data_matrix) 
data_file.close() 

參見:How to read HDF5 files in Python