OZ renamed zebra.py into h5.py, added reading angles

This commit is contained in:
zaharko 2020-03-19 12:05:45 +01:00
parent 833d0d8db4
commit eed05fe3fe
2 changed files with 91 additions and 0 deletions

69
pyzebra/h5.py Normal file
View File

@ -0,0 +1,69 @@
import h5py
def read_h5meta(filepath):
"""Read and parse content of a h5meta file.
Args:
filepath (str): File path of a h5meta file.
Returns:
dict: A dictionary with section names and their content.
"""
h5meta_content = dict()
with open(filepath, "r") as h5meta_file:
line = h5meta_file.readline()
while line:
if line.startswith("#begin"):
# read section
section = line[7:-1] # len("#begin ") = 7
h5meta_content[section] = []
line = h5meta_file.readline()
while not line.startswith("#end"):
h5meta_content[section].append(line[:-1])
line = h5meta_file.readline()
# read next line after section's end
line = h5meta_file.readline()
return h5meta_content
def read_detector_data(filepath):
"""Read detector data and angles from an h5 file.
Args:
filepath (str): File path of an h5 file.
Returns:
ndarray: A 3D array of data, rot_angle, pol_angle, tilt_angle.
"""
with h5py.File(filepath, "r") as h5f:
detector_data = h5f["/entry1/area_detector2/data"][:]
# reshape data to a correct shape (2006 issue)
n, cols, rows = detector_data.shape
detector_data = detector_data.reshape(n, rows, cols)
rot_angle = h5f["/entry1/area_detector2/rotation_angle"][:]
pol_angle = h5f["/entry1/ZEBRA/area_detector2/polar_angle"][:]
tlt_angle = h5f["/entry1/ZEBRA/area_detector2/tilt_angle"][:]
return detector_data, pol_angle, rot_angle, tlt_angle
def open_h5meta(filepath):
"""Open h5meta file like *.cami
Args:
filepath (str): File path of a h5meta file.
Returns:
dict: A dictionary with h5 names and their detector data and angles.
"""
data = dict()
angles = dict()
h5meta_content = read_h5meta(filepath)
for file in h5meta_content["filelist"]:
data[file] = read_detector_data(file)
angles[file] = read_angle_data(file)
return data, angles

22
pyzebra/xtal.py Normal file
View File

@ -0,0 +1,22 @@
import numpy as np
def z4frgn(wave,ga,nu):
"""CALCULATES DIFFRACTION VECTOR IN LAB SYSTEM FROM GA AND NU
Args:
WAVE,GA,NU
Returns:
Z4
"""
sin = np.sin
cos = np.cos
pir = 180/np.pi
gar = ga/pir
nur = nu/pir
z4 = [0., 0., 0.]
z4[0]=( sin(gar)*cos(nur) )/wave
z4[1]=( cos(gar)*cos(nur)-1. )/wave
z4[2]=( sin(nur) )/wave
return z4