1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | import numpy as np |
---|
4 | from scipy import ndimage |
---|
5 | import matplotlib.pyplot as plt |
---|
6 | |
---|
7 | from myplot import getfield |
---|
8 | from netCDF4 import Dataset |
---|
9 | from scipy import ndimage |
---|
10 | |
---|
11 | def disk_structure(n): |
---|
12 | struct = np.zeros((2 * n + 1, 2 * n + 1)) |
---|
13 | x, y = np.indices((2 * n + 1, 2 * n + 1)) |
---|
14 | mask = (x - n)**2 + (y - n)**2 <= n**2 |
---|
15 | struct[mask] = 1 |
---|
16 | return struct.astype(np.bool) |
---|
17 | |
---|
18 | |
---|
19 | def granulometry(data, sizes=None): |
---|
20 | s = max(data.shape) |
---|
21 | if sizes == None: |
---|
22 | sizes = range(1, s/2, 2) |
---|
23 | granulo = [ndimage.binary_opening(data, \ |
---|
24 | structure=disk_structure(n)).sum() for n in sizes] |
---|
25 | return granulo |
---|
26 | |
---|
27 | |
---|
28 | #np.random.seed(1) |
---|
29 | #n = 10 |
---|
30 | #l = 256 |
---|
31 | #im = np.zeros((l, l)) |
---|
32 | #points = l*np.random.random((2, n**2)) |
---|
33 | #im[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1 |
---|
34 | #im = ndimage.gaussian_filter(im, sigma=l/(4.*n)) |
---|
35 | #mask = im > im.mean() |
---|
36 | |
---|
37 | namefile = "/home/aymeric/Big_Data/psfc_f18.nc" |
---|
38 | nc = Dataset(namefile) |
---|
39 | psfc_full=getfield(nc,'PSFC') |
---|
40 | |
---|
41 | #mask = psfc_full[250,100:200,100:200] |
---|
42 | mask = psfc_full[250,:,:] |
---|
43 | mask = ndimage.laplace(mask) |
---|
44 | #mask = mask > 0.3 |
---|
45 | |
---|
46 | print mask.std() |
---|
47 | print mask.mean() |
---|
48 | |
---|
49 | lim = mask.std() * 12. |
---|
50 | |
---|
51 | ind1 = np.where(mask > lim) |
---|
52 | ind2 = np.where(mask < lim) |
---|
53 | mask[ind1] = 1 |
---|
54 | mask[ind2] = 0 |
---|
55 | |
---|
56 | print mask |
---|
57 | |
---|
58 | |
---|
59 | labeled_array, num_features = ndimage.label(mask) |
---|
60 | |
---|
61 | print num_features |
---|
62 | |
---|
63 | plt.pcolor(labeled_array) |
---|
64 | plt.show() |
---|
65 | exit() |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | mask = mask < mask.mean() - 0.3 |
---|
71 | |
---|
72 | #mask = mask > 0.3 |
---|
73 | |
---|
74 | |
---|
75 | #plt.pcolor(mask) |
---|
76 | #plt.show() |
---|
77 | |
---|
78 | granulo = granulometry(mask, sizes=[2,5,20]) |
---|
79 | |
---|
80 | print granulo |
---|
81 | |
---|
82 | plt.figure(figsize=(6, 2.2)) |
---|
83 | |
---|
84 | plt.subplot(121) |
---|
85 | plt.imshow(mask, cmap=plt.cm.gray) |
---|
86 | opened = ndimage.binary_opening(mask, structure=disk_structure(10)) |
---|
87 | opened_more = ndimage.binary_opening(mask, structure=disk_structure(14)) |
---|
88 | plt.contour(opened, [0.5], colors='b', linewidths=2) |
---|
89 | plt.contour(opened_more, [0.5], colors='r', linewidths=2) |
---|
90 | plt.axis('off') |
---|
91 | plt.subplot(122) |
---|
92 | #plt.plot(np.arange(2, 19, 4), granulo, 'ok', ms=8) |
---|
93 | |
---|
94 | |
---|
95 | plt.subplots_adjust(wspace=0.02, hspace=0.15, top=0.95, bottom=0.15, left=0, right=0.95) |
---|
96 | plt.show() |
---|