Test script to find all locations with large swirl

Aim is to take a velocity field, find all locations with large swirl, and then identify distinct blobs of swirl.

This script makes use of the Source Extraction and Photometry (SEP) library


In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import h5py
from importlib import reload
import sep

In [2]:
f = h5py.File('/Users/Owen/Dropbox/Data/ABL/SBL PIV data/RNV45-RI2.mat')
#list(f.keys())

Swirl = np.asarray(f['Swirl'])
X  = np.asarray(f['X'])
Y  = np.asarray(f['Y'])

X = np.transpose(X,(1,0))
Y = np.transpose(Y,(1,0))
Swirl = np.transpose(Swirl,(2,1,0))

NanLocs = np.isnan(Swirl)

uSize = Swirl.shape

In [3]:
plt.figure(figsize = [8,3])
plt.pcolor(X,Y,Swirl[:,:,1], cmap='RdBu');
plt.clim([-50, 50])
plt.axis('scaled')
plt.xlim([X.min(), X.max()])
plt.ylim([Y.min(), Y.max()])
plt.colorbar()


Out[3]:
<matplotlib.colorbar.Colorbar at 0x11c5c6748>

In [4]:
#Find profile of swirl std
SwirlStd = np.std(np.nanmean(Swirl,axis=2),axis = 1)
plt.plot(SwirlStd,Y[:,1])
plt.ylabel('y(m)')
plt.xlabel('Swirl rms')


Out[4]:
<matplotlib.text.Text at 0x11c7e5320>

In [5]:
Y[1].shape


Out[5]:
(242,)

In [6]:
SwirlStd.shape


Out[6]:
(123,)

In [4]:
#Normalize field by the std of Swirl
Swirl = Swirl/SwirlStd.reshape(uSize[0],1,1) #match the SwirlStd length (123) with the correct index in Swirl (also 123)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-faa142209a1f> in <module>()
      1 #Normalize field by the std of Swirl
----> 2 Swirl = Swirl/SwirlStd.reshape(uSize[0],1,1) #match the SwirlStd length (123) with the correct index in Swirl (also 123)

NameError: name 'SwirlStd' is not defined

In [5]:
plt.figure(figsize = [8,3])
plt.pcolor(X,Y,Swirl[:,:,1], cmap='RdBu');
plt.clim([-200, 200])
plt.axis('scaled')
plt.xlim([X.min(), X.max()])
plt.ylim([Y.min(), Y.max()])
plt.colorbar()


Out[5]:
<matplotlib.colorbar.Colorbar at 0x11df78f60>

In [6]:
Swirl[NanLocs] = 0   #Get rid of nans for now

Estimate background


In [7]:
bkg = sep.Background(np.ascontiguousarray(Swirl[:,:,1]))
bkg_image = bkg.back()
plt.imshow(bkg_image, interpolation='nearest', cmap='gray', origin='lower')
plt.colorbar();



In [8]:
bkg_rms = bkg.rms()
plt.imshow(bkg_rms, interpolation='nearest', cmap='gray', origin='lower')
plt.colorbar();


Now extract objects


In [ ]:
#creat filter kernal
kern = np.array([[1,2,1], [2,4,2], [1,2,1]])      #Basic default kernal

kern = np.array([[1,2,4,2,1],[2,3,5,3,2],[3,6,8,6,3],[2,3,5,3,2],[1,2,4,2,1]])      #Basic default kernal

from scipy.stats import multivariate_normal as mvnorm

x = np.linspace(-5, 5, 100)
y = mvnorm.pdf(x, mean=0, cov=1)
#plt.plot(x,y)
#mvnorm.pdf(

x = np.mgrid[-1:1:.01]
y = x;
r = (x**2+y**2)**0.5
kern = np.empty(x.shape)
#for i in kern.shape[0]
#    kern[i,:] = mvnorm.pdf(r[i,:], mean=0, cov=1)
    
#plt.imshow(kern)

#y = mvnorm.pdf(x, mean=0, cov=1)
#pos = np.empty(x.shape + (2,))l
#pos[:, :, 0] = x; pos[:, :, 1] = y

In [74]:
x = np.mgrid[-10:10:1]
x.shape


Out[74]:
(20,)

In [14]:
objects = sep.extract(np.ascontiguousarray(Swirl[:,:,1]), 1.5, err=bkg.globalrms,filter_kernel=kern)

np.ascontiguousarray(Swirl[:,:,1]).flags how to make array C contiguous


In [52]:
len(objects)


Out[52]:
57

In [15]:
from matplotlib.patches import Ellipse

#fig, ax = plt.subplots()
plt.figure(figsize = [8,3])
plt.pcolor(X,Y,Swirl[:,:,1], cmap='RdBu_r');
ax = plt.gca()
plt.clim([-50, 50])
plt.axis('scaled')
plt.xlim([X.min(), X.max()])
plt.ylim([Y.min(), Y.max()])
plt.colorbar()

scale = (X[1,-1]-X[1,1])/uSize[1]
#plt.plot(objects['x']*scale,objects['y']*scale,'go')

for i in range(len(objects)):
    e = Ellipse(xy=(objects['x'][i]*scale, objects['y'][i]*scale),
                width=6*objects['a'][i]*scale,
                height=6*objects['b'][i]*scale,
                angle=objects['theta'][i] * 180. / np.pi)
    e.set_facecolor('none')
    e.set_edgecolor('red')
    ax.add_artist(e)



In [13]:
#objects['x']
scale = (X[1,-1]-X[1,1])/uSize[1]
objects['x']*scale


Out[13]:
array([ 0.08121172,  0.1628138 ,  0.3071177 ,  0.00181404,  0.00809907,
        0.13279065,  0.29076344,  0.01973563,  0.04861494,  0.21885154,
        0.09020882,  0.22902161,  0.00943356,  0.07659338,  0.27765189,
        0.27051772,  0.25700055,  0.19477567,  0.20198665,  0.15399035,
        0.2940052 ,  0.30083928,  0.06403406,  0.18486947,  0.23868502,
        0.22060494,  0.00578485,  0.00167784,  0.24831657,  0.30750801,
        0.30139386,  0.10858102,  0.10304454,  0.17658349,  0.09649234,
        0.11732664,  0.2828056 ,  0.07716597,  0.26550892,  0.25529876,
        0.02778939,  0.03083588,  0.04007531,  0.22496476,  0.05500456,
        0.06533615,  0.07224873,  0.15289447,  0.08986243,  0.10914521,
        0.28104717,  0.2733653 ,  0.29819921,  0.18953667,  0.19607011,
        0.21106559,  0.13444699,  0.18081771,  0.07195001,  0.16066294,
        0.09451848,  0.24690735,  0.24724393,  0.08241664,  0.25905258,
        0.10267216,  0.10937984,  0.28602864,  0.00127974,  0.19639358,
        0.20174538,  0.20287138,  0.23056224,  0.1863224 ,  0.14393336,
        0.04804137,  0.26458431,  0.14934034,  0.20344475,  0.07405233,
        0.18612277,  0.21319052,  0.21933974,  0.23279476,  0.27621978,
        0.08510074,  0.24082777,  0.17925906,  0.30204657,  0.15331754,
        0.20773158,  0.25252665,  0.07894003,  0.16482995,  0.17139204,
        0.22270296,  0.28816866,  0.26328096,  0.27339512,  0.21336537,
        0.18506526,  0.19085461,  0.19403954,  0.2309163 ,  0.30607609,
        0.15309831,  0.16782342,  0.20326559,  0.06911631,  0.07545066,
        0.23761192,  0.24934181,  0.08874934,  0.22362195,  0.27934378,
        0.27360369,  0.29417324,  0.28916245,  0.02989579,  0.17081237,
        0.2143346 ,  0.20895221,  0.20537191,  0.0598512 ,  0.18532842,
        0.17977972,  0.19935512,  0.29784143,  0.02197759,  0.17014643,
        0.19341673,  0.18563274,  0.30591163,  0.14676191,  0.15303353,
        0.23133442,  0.06936188,  0.24699561,  0.2424906 ,  0.2857116 ,
        0.29057247,  0.10374884,  0.13110018,  0.1782086 ,  0.26314533,
        0.27069496,  0.08147935,  0.1904494 ,  0.04997169,  0.12022386,
        0.        ,  0.00187729,  0.00719141,  0.06252977,  0.13906021,
        0.21361567,  0.21448615,  0.21819722,  0.22364684,  0.2863443 ,
        0.06900371,  0.12562726,  0.20362646,  0.27911029,  0.2961173 ,
        0.30009893,  0.02806842,  0.03223515,  0.02674646,  0.07605105,
        0.17008897,  0.18386869,  0.03795023,  0.04715303,  0.19875331,
        0.25221766,  0.25406083,  0.26133632,  0.26687844,  0.05304286,
        0.05761872,  0.18856044,  0.24086827,  0.06378485,  0.09686105,
        0.09328069,  0.09091937,  0.0921413 ,  0.08895771,  0.10029555,
        0.09862998,  0.10448888,  0.1081744 ,  0.11084602,  0.15779668,
        0.15415326,  0.26290672,  0.2932542 ,  0.29747264,  0.07325996,
        0.22205494,  0.23444066,  0.23239467,  0.27053081,  0.17818451,
        0.172914  ,  0.00848887,  0.08476506,  0.08058559,  0.12613429,
        0.11932452,  0.28171778,  0.02431709,  0.020533  ,  0.03252853,
        0.04666582,  0.06016034,  0.13030208,  0.14630838,  0.14236984,
        0.13840238,  0.13387218,  0.15882495,  0.1839624 ,  0.19235794,
        0.20982737,  0.20575976,  0.24691896,  0.24315287,  0.23813183,
        0.25275789,  0.25967339,  0.30778219])

In [14]:
X[objects['x'],objects['y']]


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-14-1ede2d1d1fd3> in <module>()
----> 1 X[objects['x'],objects['y']]

IndexError: arrays used as indices must be of integer (or boolean) type

In [ ]:
objects['x']

In [ ]: