In [13]:
import os
import tables, pandas
import numpy as np

results_dir = "/data/GLM-wkshp/flashsort/results/"
h5name = os.path.join(results_dir, "h5_files/2009/Apr/10/LYLOUT_090410_160000_3600.dat.flash.h5")
h5 = tables.open_file(h5name, 'a')
print h5

table_name = h5.root.flashes._v_children.keys()[0]
print table_name


/data/GLM-wkshp/flashsort/results/h5_files/2009/Apr/10/LYLOUT_090410_160000_3600.dat.flash.h5 (File) 'Flash-sorted New Mexico Tech LMA Data'
Last modif.: 'Mon Sep  7 14:42:38 2015'
Object Tree: 
/ (RootGroup) 'Flash-sorted New Mexico Tech LMA Data'
/NLDN (Group) 'NLDN strokes'
/NLDN/NLDN_090410_160000_600 (Table(0,)) 'NLDN_090410_160000_600'
/events (Group) 'Analyzed detected events'
/events/LMA_090410_160000_3600 (Table(707034,)) 'LMA_090410_160000_3600'
/flashes (Group) 'Sorted LMA flash data'
/flashes/LMA_090410_160000_3600 (Table(119487,)) 'LMA_090410_160000_3600_cg'

LMA_090410_160000_3600

In [14]:
h5.remove_node(h5.root.NLDN, recursive=True)
print h5


/data/GLM-wkshp/flashsort/results/h5_files/2009/Apr/10/LYLOUT_090410_160000_3600.dat.flash.h5 (File) 'Flash-sorted New Mexico Tech LMA Data'
Last modif.: 'Mon Sep  7 14:42:38 2015'
Object Tree: 
/ (RootGroup) 'Flash-sorted New Mexico Tech LMA Data'
/events (Group) 'Analyzed detected events'
/events/LMA_090410_160000_3600 (Table(707034,)) 'LMA_090410_160000_3600'
/flashes (Group) 'Sorted LMA flash data'
/flashes/LMA_090410_160000_3600 (Table(119487,)) 'LMA_090410_160000_3600_cg'


In [15]:
group  = h5.root.flashes
table  = getattr(h5.root.flashes, table_name)
descr  = table.description._v_colobjects

revised_table_title = table.title[:-3]
print revised_table_title

# Create the new description
descr2 = descr.copy()
# remove the CG key from the descriptor dictionary
descr2.pop('CG')

table2 = h5.createTable(group, table_name+'_temp', descr2, revised_table_title)

table.attrs._f_copy(table2)
for i in range(table.nrows):
    table2.row.append()
table2.flush()
for col in descr2:
    getattr(table2.cols, col)[:] = getattr(table.cols, col)[:]
# for value in results[-np.isnan(results)]:
#     for row in table2.where('flash_id == value'):
#         row['CG'] = True
#         row.update()

table.remove()
table2.move('/flashes',table_name)
print 'h5 rewritten'
h5.close()


LMA_090410_160000_3600
h5 rewritten

In [ ]:


In [ ]: