PIVpy getting started notebook:

This notebook shows an example of how one can use vecpy in order to load manipulate and display analyzed PIV data.

step 1 - import pivpy and dependencies

here we import the package code so that we can use it next

In [1]:
import os, sys 
# sys.path.append(os.path.abspath('../'))

import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
%matplotlib inline

from pivpy import io, pivpy, graphics

# for the sake of this tutorial, ignore warnings
# import warnings
# warnings.filterwarnings('ignore')

step 2 - load the tests data

In order to load the data, first we need to set up the path to the data directory. Following that we need to get a list of files names that we would like to view/analyze. Finally we very quickly load the data in to a list of vec instances.

In [2]:
# pointer to the directory with the data
path_to_data = os.path.abspath('../../tests/data/')
# list the directory
os.listdir(path_to_data)
Out[2]:
['day2a005001.T000.D000.P003.H001.L.vec',
 'exp1_001_b.txt',
 'Run000001.T000.D000.P000.H001.L.vec',
 '2Cb.VC7',
 'Run000004.T000.D000.P000.H001.L.vec',
 'day2a005003.T000.D000.P003.H001.L.vec',
 '2Ca.VC7',
 'exp1_001_b_noflt.txt',
 'day2a005004.T000.D000.P003.H001.L.vec',
 '3C.VC7',
 'Run000003.T000.D000.P000.H001.L.vec',
 'Run000002.T000.D000.P000.H001.L.vec',
 'exp1_001_b_flt.txt',
 'day2a005005.T000.D000.P003.H001.L.vec',
 'day2a005002.T000.D000.P003.H001.L.vec',
 'Run000005.T000.D000.P000.H001.L.vec',
 'exp1_001_b.vec',
 'day2a005000.T000.D000.P003.H001.L.vec']
In [3]:
# let's read only the files from the Run* 
data = io.load_directory(path_to_data,basename='Run*',ext='.vec')
In [4]:
# let's check if it's read:
data.attrs['files']
Out[4]:
['/Users/alex/Documents/Documents/repos/lior-pivpy/tests/data/Run000001.T000.D000.P000.H001.L.vec',
 '/Users/alex/Documents/Documents/repos/lior-pivpy/tests/data/Run000002.T000.D000.P000.H001.L.vec',
 '/Users/alex/Documents/Documents/repos/lior-pivpy/tests/data/Run000003.T000.D000.P000.H001.L.vec',
 '/Users/alex/Documents/Documents/repos/lior-pivpy/tests/data/Run000004.T000.D000.P000.H001.L.vec',
 '/Users/alex/Documents/Documents/repos/lior-pivpy/tests/data/Run000005.T000.D000.P000.H001.L.vec']

step 3 - plot some arrows

first things first - show a quiver plot

In [5]:
fig, ax = graphics.quiver(data.isel(t=0), nthArr=2, arrScale=20)
In [6]:
fig, ax = graphics.quiver(data, nthArr=3, arrScale=5)
Warning: function for a single frame, using first frame, supply data.isel(t=N)
In [7]:
# we can read also a single file only into a 1 frame dataset
d = io.load_vec('/Users/alex/Documents/OpenPIV/pivpy/tests/data/Run000001.T000.D000.P000.H001.L.vec')
In [8]:
graphics.quiver(d,arrScale=10)
Warning: function for a single frame, using first frame, supply data.isel(t=N)
Out[8]:
(<Figure size 432x288 with 2 Axes>,
 <matplotlib.axes._subplots.AxesSubplot at 0x102137550>)
In [9]:
d.isel(t=0).differentiate(coord='x').differentiate(coord='y')['u'].plot.pcolormesh()
Out[9]:
<matplotlib.collections.QuadMesh at 0x1a160ca630>

and a vorticity map

In [10]:
# prepare vorticity
d.piv.vec2scal(property='curl') # it will appear as d['w'] variable, 'w' for all scalar properties

# plot
fig, ax = graphics.contour_plot(d)
Warning: function for a single frame, using first frame, supply data.isel(t=N)

Also, velocity histograms in x and y directions

In [11]:
fig, ax = graphics.histogram(data, normed = True)

We can also plot a whole list of vec's as subplots:

In [12]:
fig, ax = graphics.quiver(data, nthArr=4, arrScale=10)
fig.set_size_inches(10, 6)
Warning: function for a single frame, using first frame, supply data.isel(t=N)

Last but not least - manipulation

lets create a linear combinatino of our data and then see how to manipulate the coordinate system

Addition and Scalar multiplication

In [13]:
v = (data + 3*data - 2 * data.isel(t=0)) / 3.
graphics.quiver(v, arrScale=10)
Warning: function for a single frame, using first frame, supply data.isel(t=N)
Out[13]:
(<Figure size 432x288 with 2 Axes>,
 <matplotlib.axes._subplots.AxesSubplot at 0x1020a5eb8>)

Crop

In [14]:
v = v.piv.crop([5,15,-5,-15])  #(xmin, xmax, ymin, ymax)
graphics.quiver(v, arrScale=10)
Warning: function for a single frame, using first frame, supply data.isel(t=N)
Out[14]:
(<Figure size 432x288 with 2 Axes>,
 <matplotlib.axes._subplots.AxesSubplot at 0x1a17095c50>)

Rotate

In [15]:
# v.piv.rotate(90) # not implemented

Translation of Coordinate System

In [16]:
# we can also use some default plot from xarray
data.piv.vorticity()
data.isel(t=0)['w'].plot(robust=True)
Out[16]:
<matplotlib.collections.QuadMesh at 0x1a166ac6a0>
In [17]:
# low level quiver
plt.figure(figsize=(8,6))
plt.quiver(data.x,data.y,data.u[:,:,0], data.v[:,:,0] ,data.u[:,:,0]**2 + data.v[:,:,0]**2,scale=.75)
Out[17]:
<matplotlib.quiver.Quiver at 0x1a166182b0>
In [18]:
test = io.create_sample_field(rows=25,cols=5)
In [19]:
graphics.quiver(test,arrScale=5)
Warning: function for a single frame, using first frame, supply data.isel(t=N)
Out[19]:
(<Figure size 432x288 with 2 Axes>,
 <matplotlib.axes._subplots.AxesSubplot at 0x1a1659dc88>)