From eed05fe3fe00f31baa503829857cbe0dd6105dd2 Mon Sep 17 00:00:00 2001 From: Oksana Zaharko Date: Thu, 19 Mar 2020 12:05:45 +0100 Subject: [PATCH] OZ renamed zebra.py into h5.py, added reading angles --- pyzebra/h5.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ pyzebra/xtal.py | 22 ++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 pyzebra/h5.py create mode 100644 pyzebra/xtal.py diff --git a/pyzebra/h5.py b/pyzebra/h5.py new file mode 100644 index 0000000..be51205 --- /dev/null +++ b/pyzebra/h5.py @@ -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 diff --git a/pyzebra/xtal.py b/pyzebra/xtal.py new file mode 100644 index 0000000..f6e9db1 --- /dev/null +++ b/pyzebra/xtal.py @@ -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