1 import numpy as np
2 import pandas as p
3 import Nio
4 import matplotlib.pyplot as plt
5 from matplotlib.dates import num2date, epoch2num
6
7 nc1 = Nio.open_file('10147-precip.nc')
8 nc2 = Nio.open_file('10015-precip.nc')
9
10 time1 = nc1.variables['time'][:]
11 time2 = nc2.variables['time'][:]
12
13 rain1 = nc1.variables['rainfall_rate_hour'][:]
14 rain2 = nc2.variables['rainfall_rate_hour'][:]
15
16
17
18
19
20
21 dates1 = num2date(epoch2num(time1))
22 dates2 = num2date(epoch2num(time2))
23
24
25 ds1 = p.Series(rain1, index = dates1)
26 ds2 = p.Series(rain2, index = dates2)
27
28
29
30
31 ds1 = np.where(ds1<0, nan, ds1)
32 ds2 = np.where(ds2<0, nan, ds2)
33
34
35 ds1.plot()
36 ds2.plot()
37
38
39
40
41 ds1=ds1[ds1==ds1]
42 ds2=ds2[ds2==ds2]
43
44
45 print ds1.shape[0], ds2.shape[0]
46
47
48
49 ds2_nan = ds2.reindex(ds1.index)
50 ds2_backfill = ds2.reindex(ds1.index, method = 'backfill')
51
52
53 print "Max: %.2f Min: %.2f Mean: %.2f Median: %.2f Count: %.2f" % (ds2.max(), ds2.min(), ds2.mean(), ds2.median(), ds2.count())
54
55
56 ds2.cumsum()
57
58
59 df=p.DataFrame({"helgoland":ds2, "hamburg":ds1})
60
61
62 print ds1.fillna(0).count(), ds2.fillna(0).count()
63 print df['hamburg'].fillna(0).count(), df['helgoland'].fillna(0).count()
64
65
66 df.dropIncompleteRows()
67
68
69 df.corr()
70
71
72
73 start=datetime.datetime(2004,5,1)
74 end = datetime.datetime(2007,9,1)
75
76
77
78
79
80
81
82 dfMonth = df.groupby(lambda x: x.month)
83 dfYear = df.groupby(lambda x: x.year)
84
85 dfMonthSum = dfMonth.agg(np.nansum)
86 dfMonthMean = dfMonth.agg(np.mean)
87 dfYearSum = dfYear.agg(p.Series.sum)
88
89
90 dfYearHelgoland = dfYear.agg(np.nansum)['helgoland']
91
92
93 dfYearHelgoland[dfYearHelgoland>800]
94
95
96 dfYear.agg(np.nansum).values