-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexplore.py
executable file
·69 lines (57 loc) · 2.05 KB
/
explore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
import h5py
import numpy as np
from argparse import ArgumentParser
import matplotlib.pyplot as plt
import pprint
def main():
args = parse_args()
explorer = Explorer(args.infile)
explorer.get_all_info()
def parse_args():
description = 'explore a snow melt hdf5 file'
parser = ArgumentParser(description=description)
parser.add_argument('infile', help='location of input hdf5 file')
#parser.add_argument('--day', help='day to plot', default=0, type=int)
return parser.parse_args()
class Explorer(object):
def __init__(self, filename, data_path = None):
self.filename = filename
self.data_path = data_path
self.read()
def read(self):
self.f = h5py.File(self.filename)
if self.data_path:
self.data = self.f[self.data_path]
print "Read dataset with dimensions:", self.data.shape
def plot_day(self, day):
plt.imshow(self.data[day, ...].T)
plt.show()
def extract_regression_data(self, firstday, lastday):
data = self.data[firstday:lastday, ...]
return data.ravel()
def extract_slice(self, day_of_year):
data = self.data[day_of_year, ...]
return data.ravel()
def extract_aggregate_data(self, firstday, lastday, aggfn):
""" Aggregate the data across all days for each pixel.
aggfn can be any aggregation function, like np.mean
"""
data = self.data[firstday:lastday, ...]
aggdata = aggfn(data, axis=0)
return aggdata.ravel()
def get_all_info(self):
def printattributes(attrs, indent=''):
print indent, 'Attributes:'
for key, val in attrs.iteritems():
print indent, key, ':', val
print ''
def printinfo(name, obj):
indent = name.count('/') * ' '
print indent, name
indent += ' '
printattributes(obj.attrs, indent)
printattributes(self.f.attrs)
self.f.visititems(printinfo)
if __name__ == '__main__':
main()