Commit c37d0e7b authored by Peter Jansweijer's avatar Peter Jansweijer

Added simple script to extract measured delays from any set of measurement directories

parent def1bf75
#!/usr/bin/python
"""
calc.py: Analyses the stat_result files that were output by AnalyzeScopeData.py
and takes the linear correlation results to calculate the final optical to
electrical delay of the photo detector.
-------------------------------------------------------------------------------
Copyright (C) 2017 Peter Jansweijer, Henk Peek
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
-------------------------------------------------------------------------------
Usage:
list_delays.py <root-name> (for example "180928_12_09_18_3_lambda_insitu_alpha_scan")
list_delays.py -h | --help
Options:
-h --help Show this screen.
"""
import os
import sys
import scipy
import numpy
import matplotlib.pyplot as plt
import pdb
# Add parent directory (containing 'lib') to the module search path
lib_path = (os.path.dirname(os.path.abspath(__file__)))
lib_path = os.path.join(lib_path,"..")
sys.path.insert(0,lib_path)
############################################################################
def read_stat_result(dirname,sign="neg"):
"""
read_stat_result
dirname -- pointer to the directory that contains the stat_result.txt file
sign -- default "neg" i.e. stat_result lineair-mean values are negative due
-- to the fact that channel 1 and 3 of the oscilloscope are swapped.
returns:
fail, lin_meas
-- <Bool> fail = True when an error occurred while reading the file
-- <Tuple> lin_meas [0] = lineair-mean,
-- [1] = se,
-- [2] = sem,
"""
if not(os.path.isdir(dirname)):
#print(dirname+": is not a directory.")
return (True, numpy.nan)
stat_result_filename = os.path.join(dirname,"stat_result.txt")
if not(os.path.isfile(stat_result_filename)):
print(stat_result_filename + ": No stat_result.txt file found.")
return (True, numpy.nan)
stat_result_file = open(stat_result_filename,"r")
while 1:
# Lines look like this:
# data/data_181106_1/181106_2 lineair-mean: -6.79100959999e-10 se: 2.16707944778e-14 sem: 4.33415889555e-15 topt: 1 if line == "":
line = stat_result_file.readline()
if line == "":
break #end of file
line_lst = line.strip().split(" ")
if "lineair" in line_lst[1]:
if sign == "neg":
lin_meas = -float(line_lst[2]), float(line_lst[4]), float(line_lst[6])
else:
lin_meas = +float(line_lst[2]), float(line_lst[4]), float(line_lst[6])
return (False,lin_meas)
return (True, lnumpy.nan)
###########################################################################
def write_meas(file_ptr,meas_txt,meas):
"""
Test whether a measurement is present or not
If not then exit with a failure message
file_ptr -- <file> Pointer to output file
meas_txt -- <Str> preceding string
meas -- <Tuple> lin_meas [0] = lineair-mean,
-- [1] = se,
-- [2] = sem,
returns:
"""
file_ptr.write(meas_txt + str(meas[0]) + ", " + str(meas[1]) + ", " + str(meas[2]) + "\n")
return
###########################################################################
#
# If run from commandline, we can test the library
#a_meas[
"""
Usage:
calc.py <root-name> (for example "180928_12_09_18_3_lambda_insitu_alpha_scan")
calc.py -h | --help
Options:
-h --help Show this screen.
"""
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("name", help="parse the directory that contains the measurement files")
args = parser.parse_args()
name = args.name
files = []
# Figure out if the input files exist. Extensions should be:
files_exist = True
if not(os.path.isdir(name)):
print(name+": is not a directory.")
print("Provide the root that contains measurement subdirectories")
sys.exit()
meas_dirs = os.listdir(name)
fail = True
result_file = open(os.path.join(name,"list_result.txt"),"w")
result_file.write("# List of all measurements\n")
result_file.write("# file: " + os.path.join(name,"list_result.txt") + "\n")
for meas_dir in meas_dirs:
#print(os.path.join(name,meas_dir))
fail,lin_meas = read_stat_result(os.path.join(name,meas_dir))
if not fail:
#print(" failed at file: " + os.path.join(name,meas_dir))
#result_file.close()
#sys.exit()
#else:
#pdb.set_trace()
print(meas_dir + ", " + str(lin_meas[0]) + ", " + str(lin_meas[1]) + ", " + str(lin_meas[2]) + "\n")
result_file.write(meas_dir + ", " + str(lin_meas[0]) + ", " + str(lin_meas[1]) + ", " + str(lin_meas[2]) + "\n")
result_file.close()
sys.exit()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment