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
import pkg_resources as pkg
path_to_data  = pkg.resource_filename('pivpy','data')
# list the directory
# os.listdir(path_to_data)

In [3]:
# let's read only the files from the Run* 
data = io.load_directory(os.path.join(path_to_data,'Insight')) # you can add also: basename='day2a*',ext='.vec')

In [4]:
# let's check if it's read:
data.attrs['files']


Out[4]:
['/home/user/.cache/Python-Eggs/pivpy-0.0.7-py3.7.egg-tmp/pivpy/data/Insight/Run000001.T000.D000.P000.H001.L.vec',
 '/home/user/.cache/Python-Eggs/pivpy-0.0.7-py3.7.egg-tmp/pivpy/data/Insight/Run000002.T000.D000.P000.H001.L.vec',
 '/home/user/.cache/Python-Eggs/pivpy-0.0.7-py3.7.egg-tmp/pivpy/data/Insight/Run000003.T000.D000.P000.H001.L.vec',
 '/home/user/.cache/Python-Eggs/pivpy-0.0.7-py3.7.egg-tmp/pivpy/data/Insight/Run000004.T000.D000.P000.H001.L.vec',
 '/home/user/.cache/Python-Eggs/pivpy-0.0.7-py3.7.egg-tmp/pivpy/data/Insight/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.isel(t=0), nthArr=3, arrScale=5)



In [7]:
tmp = data.isel(t=0)
plt.quiver(tmp.x,tmp.y,tmp.u.T,tmp.v.T,scale=1)


Out[7]:
<matplotlib.quiver.Quiver at 0x7f3c024bd5d0>

In [8]:
# we can read also a single file only into a 1 frame dataset
d = io.load_vec(os.path.join(path_to_data,'Insight/Run000001.T000.D000.P000.H001.L.vec'))

In [9]:
graphics.quiver(d.isel(t=0),arrScale=10)


Out[9]:
(<Figure size 432x288 with 1 Axes>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7f3c0254a050>)

In [10]:
d.isel(t=0).differentiate(coord='x').differentiate(coord='y')['u'].plot.pcolormesh()


Out[10]:
<matplotlib.collections.QuadMesh at 0x7f3c02d32f90>

and a vorticity map


In [11]:
# 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 the first frame, supply data.isel(t=N)

Also, velocity histograms in x and y directions


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


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


In [13]:
fig, ax = graphics.quiver(data.isel(t=0), nthArr=4, arrScale=10)
fig.set_size_inches(10, 6)


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 [14]:
v = (data + 3*data - 2 * data.isel(t=0)) / 3.
graphics.quiver(v.isel(t=-1), arrScale=10,units=data.attrs['units'])


Out[14]:
(<Figure size 432x288 with 1 Axes>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7f3c00325110>)

Crop


In [15]:
v = v.piv.crop([5,15,-5,-15])  #(xmin, xmax, ymin, ymax)
graphics.quiver(v.isel(t=-1), arrScale=10,units=data.attrs['units'])


Out[15]:
(<Figure size 432x288 with 1 Axes>,
 <matplotlib.axes._subplots.AxesSubplot at 0x7f3c026343d0>)

Rotate


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

Translation of Coordinate System


In [ ]:
# we can also use some default plot from xarray
data.piv.vorticity()
data.isel(t=0)['w'].plot(robust=True)


Out[ ]:
<matplotlib.collections.QuadMesh at 0x7f3c0237c610>

In [ ]:
# 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)
plt.gca().invert_yaxis()

In [ ]:
test = io.create_sample_field(rows=25,cols=5)

In [ ]:
graphics.quiver(test,arrScale=5,aspectratio='auto')

In [ ]:
data = io.load_vec(os.path.join(path_to_data,'openpiv/exp1_001_b.vec'))

In [ ]:
variables,units,rows,cols, dt, frame = io.parse_header(os.path.join(path_to_data,'openpiv/exp1_001_b.vec'))
variables,units,rows,cols, dt, frame

In [ ]:
data.piv.quiver()

In [ ]:
data = io.load_directory(os.path.join(path_to_data,'urban_canopy/'),ext='.vc7')

In [ ]:
data.piv.quiver(arrScale=40,colbar=True)
plt.gcf().set_size_inches(12,10)

In [ ]: