In [1]:
    
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import pkg_resources as pkg
    
In [2]:
    
filename = pkg.resource_filename('pivpy','data/PIV_Challenge/B00001.txt')
    
In [3]:
    
with open(filename) as f:
    print(f.readline()) #header
    print(f.readline()) #with commas
    print(f.readline().replace(',','.')) #replace commas by dots
    
    
In [4]:
    
c = lambda x: float(x.decode().replace(',','.') or -999)
tmp = np.genfromtxt(filename,skip_header=1,converters = {0:c, 1:c, 2:c, 3:c})
x,y,u,v = tmp[:,0],tmp[:,1],tmp[:,2],tmp[:,3]
    
In [5]:
    
plt.quiver(x,y,u,v)
    
    Out[5]:
    
the following cell only explains what happened here:
In [6]:
    
from locale import *
setlocale(LC_NUMERIC, '') # set to your default locale; for me this is
# 'English_Canada.1252'. Or you could explicitly specify a locale in which floats
# are formatted the way that you describe, if that's not how your locale works :)
atof('123,456') # 123456.0
# To demonstrate, let's explicitly try a locale in which the comma is a
# decimal point:
# setlocale(LC_NUMERIC, 'French_Canada.1252')
atof('123,456') # 123.456
    
    Out[6]:
In [7]:
    
from pivpy import io, pivpy, graphics
    
In [8]:
    
rows = np.unique(y).shape[0]
cols = np.unique(x).shape[0]
    
In [9]:
    
x1 = x.reshape(rows,cols)
y1 = y.reshape(rows,cols)
u1 = u.reshape(rows,cols)
v1 = v.reshape(rows,cols)
    
In [10]:
    
plt.quiver(x1,y1,u1,v1)
    
    Out[10]:
    
In [11]:
    
d = io.from_arrays(x1,y1,u1,v1,np.ones_like(u1))
    
In [12]:
    
graphics.quiver(d.isel(t=0));
    
    
In [13]:
    
def parse_header_davis816(filename):
    
    with open(filename) as f:
        header = f.readline() #header
        
    indp = header.find('"position"')+10
    indv = header.find('"velocity"')+10
    ind1 = header[indp:].find('"')
    ind2 = header[indp+ind1+1:].find('"')
    lUnits = header[indp+ind1+1:indp+ind1+ind2+1]
    ind1 = header[indv:].find('"')
    ind2 = header[indv+ind1+1:].find('"')
    velUnits = header[indv+ind1+1:indv+ind1+ind2+1]
    
    return (lUnits, velUnits)
    
In [14]:
    
len_units,vel_units = parse_header_davis816(filename)
    
In [15]:
    
print(len_units,vel_units)
    
    
In [16]:
    
d.attrs['units'] = [len_units, len_units, vel_units, vel_units]
    
In [17]:
    
graphics.quiver(d)
    
    
    Out[17]:
    
In [18]:
    
convert = lambda x: float(x.decode().replace(',','.') or -999)
def load_txt_davis816(filename, frame = 0):
    tmp = np.genfromtxt(filename,skip_header=1,converters = {0:convert, 1:convert, 
                                                             2:convert, 3:convert})
    x,y,u,v = tmp[:,0],tmp[:,1],tmp[:,2],tmp[:,3]
    rows = np.unique(y).shape[0]
    cols = np.unique(x).shape[0]
    x1 = x.reshape(rows,cols)
    y1 = y.reshape(rows,cols)
    u1 = u.reshape(rows,cols)
    v1 = v.reshape(rows,cols)
    d = io.from_arrays(x1,y1,u1,v1,np.ones_like(u1))
    len_units,vel_units = parse_header_davis816(filename)
    d.attrs['units'] = [len_units, len_units, vel_units, vel_units]
    # set frame
    d['t'] += frame
    return d
    
In [19]:
    
from glob import glob
import os
def load_directory_davis816(path, basename='*',ext='.txt', soft='davis816'):
    """ 
    load_directory (path,basename='*', ext='*.txt')
    Loads all the files with the chosen sextension in the directory into a single
    xarray Dataset with variables and units added as attributes
    Input: 
        directory : path to the directory with .vec, .txt or .VC7 files
        basename  : for directories with different sets of runs, add some string, 'B00*'
        ext : string, with a dot: '.txt'
        soft : default is None, optional ['openpiv','davis','davis816']
        
    Output:
        data : xarray DataSet with dimensions: x,y,t and 
               data arrays of u,v,
               attributes of variables and units
    See more: load_vec
    """
    files  = sorted(glob(os.path.join(path,basename+ext)))
    data = []
    if ext == '.vec':
        variables, units, rows, cols, dt, frame = parse_header(files[0])
        for i,f in enumerate(files):
            data.append(load_vec(f,rows,cols,variables,units,dt,frame+i-1))
        if len(data) > 0:
            combined = xr.concat(data, dim='t')
            combined.attrs['variables'] = data[0].attrs['variables']
            combined.attrs['units'] = data[0].attrs['units']
            combined.attrs['dt'] = data[0].attrs['dt']
            combined.attrs['files'] = files
    elif ext.lower() == '.vc7':
        frame = 1
        for i,f in enumerate(files):
            if basename=='B*':
                time=int(f[-9:-4])-1
            else:
                time=i
            data.append(load_vc7(f,time))
        if len(data) > 0:
            combined = xr.concat(data, dim='t')
            combined.attrs = data[-1].attrs
            
    elif ext.lower() == '.txt' and soft.lower() == 'davis816':
        frame = 1
        for i,f in enumerate(files):
            data.append(load_txt_davis816(f,i))
        if len(data) > 0:
            combined = xr.concat(data, dim='t')
            combined.attrs = data[-1].attrs
    return combined
    
In [20]:
    
d = load_txt_davis816(filename,25)
    
In [21]:
    
d
    
    Out[21]:
In [22]:
    
ds = load_directory_davis816(path = pkg.resource_filename('pivpy','data/PIV_Challenge'), basename='B*',ext='.txt', soft='davis816')
    
In [23]:
    
graphics.quiver(ds.isel(t=1))
    
    Out[23]:
    
In [ ]: