scientific_comp_projects/CODE/[python]thesis_old_scripts/bulleV0.py
2021-10-29 15:16:40 +02:00

165 lines
5.1 KiB
Python

# -*- coding: utf-8 -*-
"""
This script is made to process images for Quenched-PLIF method in fluid dynamics.
Before using it, you must have already obtained the fit values (a,b,c and d) for the inverse hyperbolic tangent.
Also, the buffers (images from DaVis* Lavision) must already be in ratio (divided).
It is better to have already done the spatial calibration, if not a special function is used to calbirated them,
but you must at least have the mm-pixel conversion.
Finally, the script is divided into three major sections :
-to be continue...
Created on Thu Sep 27 15:09:17 2018
@author: Armando FEMAT ORTIZ
"""
"""
We tried to do this as cleanly as possible so that some PhD student in the future
won't get on a plane at 2:00am and come hunt us down with a crowbar.
"""
import os
import ReadIM
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import glob
from matplotlib.colors import LogNorm
from matplotlib.animation import FFMpegWriter
#from skimage.filters.rank import mean_bilateral
#from skimage.morphology import disk
#import scipy.misc
#%% Here is were we declare all the global variables that we are going to be using along the analysis and data mining
#Fitting function
a = 0.316617
b = 1.26288
c = -7.70744
d = 0.6722
#Numerical to physical equivalences
nappe_laser = 0.250 #Thickness of laser en mm
dimension_pixel = 0.026244 #Equivalence mm/px
#Conversion to SI units
dimension_pixel_m = dimension_pixel * 10**(-3) #Equivalence m/px
nappe_laser_m = nappe_laser * 10**(-3) #Thickness of laser en m
surface_pixel = dimension_pixel_m**2 #Surface equivalence of a pixel (m^2)
#The working directory, where all the images are stored
working_directory = 'D:\\python_processing\\imagesV4\\XG50ppm\\'
plt.ioff()
#%% Here we present the in code functions used to make science
#Fit-inverse function
"In is the function the user specifies a intensity ratio (can be an array) "
"the function return the value of pH associated"
def inverse_tanhfit(y):
return (1/2*np.log((a+y-d)/(a-y+d))-c)/(b)
#Get files from folder
"In this function, the user specifies a folder "
"the function returns all the *.im7 files (DaVis) names"
def get_davis_files (folder):
file_name = glob.glob(folder+ "\\*"+'.im7')
return file_name
#Transform '*.im7' to array
"In this function, the user specifies the file (with extension 'C://...image.im7') *"
"the function return the image as a numpy array "
def get_image_im7 (file):
vbuff, vatts = ReadIM.extra.get_Buffer_andAttributeList(file)
v_array, vbuff = ReadIM.extra.buffer_as_array(vbuff)
array = np.array(v_array[0],dtype = float)
del(vbuff, v_array, vatts)
return array
#Transform the ratio into pH
def ratio_ph(array):
pH = inverse_tanhfit(array.copy())
pH = np.nan_to_num(pH)
return pH
#Transform the ratio into CO2 (g/m^3) concentration par pixel
def ratio_C02(array):
co2 = inverse_tanhfit(array.copy())
co2 = 115467070469*np.exp(-4.63*co2) #Concentration CO2 en g/m^3 (equal to mg/l)(numerical values)
co2 = np.nan_to_num(co2)
co2[co2>50] = 50
return co2
#Transform the ratio into CO2 mass ()
def ratio_CO2mass(array,r):
co2 = inverse_tanhfit(array.copy())
co2 = 115467070469*np.exp(-4.63*co2) #Concentration CO2 en g/m^3 (equal to mg/l)(numerical values)
co2 = np.nan_to_num(co2)
co2[co2>50] = 50
co2 = centre(co2)
co2 = get_half_meanarray(co2)
co2 = (co2) @ r
return (surface_pixel * np.pi * 2 * co2.sum())/nappe_laser_m
#Get the radius for the calculation of CO2 Mass
def get_radius(array):
r = []
radius = dimension_pixel_m
for k in range(len(array[0])//2):
r.append(radius)
radius += dimension_pixel_m
r = np.flipud(r)
return r
#Get the half mean array to apply the axisymetrie to the array.
def get_half_meanarray(array):
cut_image1 = array[0:len(array),0:len(array[0])//2]
cut_image2 = array[0:len(array),len(array[0])//2:len(array[0])]
cut_image2 = np.fliplr(cut_image2[0:len(cut_image1),0:len(cut_image1[0])])
return np.mean(np.array([cut_image1,cut_image2]), axis=0,dtype=np.float64)
#This function count the zeros until the first value
def count_zeroes(array):
return (array.cumsum(axis=1) == 0).sum(axis=1)
#This function centers all the values
def centre(array):
zeroes_front = count_zeroes(array)
zeroes_back = count_zeroes(array[:,::-1])
roll = (zeroes_front + zeroes_back) //2 - zeroes_front
return np.array([np.roll(row, r) for row, r in zip(array, roll)])
'Needs developement for image creation and video'
#
def image(array):
return
#
def video(video_array):
return
#%%
files = get_davis_files(working_directory)
get_rayon = True
mass = []
for i in range(len(files)):
array = get_image_im7(files[i])
if get_rayon:
r = get_radius(array)
get_rayon = False
mass.append(ratio_CO2mass(array,r))
del(get_rayon,r)