Skip to content

Commit

Permalink
Merge branch 'v0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
fsmaibrgm committed Dec 8, 2022
2 parents 8dd9a7e + 622f161 commit 3dc022b
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions src/razorback/calibrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""


import os
from pathlib import Path
import re
import numpy as np
import scipy.interpolate
Expand All @@ -15,8 +15,12 @@

METRONIX_DATA_PATH = "metronix_calibration"

METRONIX_CHOPPER_ON_LIMIT = 512.
METRONIX_VERSION_PATTERN = r".*(MFS\d\d).*"
METRONIX_ALPHA_MAP = {'MFS06': 4.0, 'MFS07': 32.0}

def metronix(filename, sampling_rate, chopper_on_limit=512.):

def metronix(filename, sampling_rate, chopper_on_limit=None, version_pattern=None, alpha_map=None):
""" return calibration function for metronix devices
Parameters
Expand All @@ -27,6 +31,13 @@ def metronix(filename, sampling_rate, chopper_on_limit=512.):
sampling_rate: float
chopper_on_limit: float [optional]
threshold for activating chopper
default to METRONIX_CHOPPER_ON_LIMIT
version_pattern: str [optional]
regexp pattern for extracting sensor version identifier from filename
default to METRONIX_VERSION_PATTERN
alpha_map: dict (version->alpha) [optional]
alpha value to use for sensor version
default to METRONIX_ALPHA_MAP
Returns
-------
Expand All @@ -40,6 +51,7 @@ def metronix(filename, sampling_rate, chopper_on_limit=512.):
- sampling_rate > chopper_on_limit -> chopper off
"""

def start_stop(lines, mark):
pattern = r'\s+'.join(re.split(r'\s+', mark.lower()))
start = next(i for (i, l) in enumerate(lines, 1)
Expand All @@ -50,36 +62,30 @@ def start_stop(lines, mark):
return start, stop

def version(filename):
pattern = r".*(MFS\d\d).*"
#
name, _ = os.path.splitext(os.path.basename(filename))
m = re.match(pattern, name)
name = Path(filename).stem
m = re.match(version_pattern, name)
if m is None:
raise ValueError("cannot find version of calibration file '%s'"
% filename)
raise ValueError(f"cannot find version of calibration file '{filename}'")
return m.group(1)

def cal_mp(freq, module, phase):
return freq * module * np.exp(1j * phase * np.pi/180.)

def calibration(table, filename, chopper):
def calibration(table, filename, chopper, alpha_map):
freq = table[:, 0]
calib = cal_mp(freq, table[:, 1], table[:, 2])
tabuled = scipy.interpolate.interp1d(freq, calib, copy=False)

vers = version(filename)
freq_min = freq[0]
mod_min = table[0, 1]
alpha = {
'MFS06': 4.0,
'MFS07': 32.0,
}.get(vers, None)
if alpha is None:
if vers not in alpha_map:
raise ValueError(
"unknown version %r of calibration file %r"
% (vers, filename)
f"unknown version '{vers}' of calibration file '{filename.name}'"
)

alpha = alpha_map[vers]
freq_min = freq[0]
mod_min = table[0, 1]

def calib_func(f):
if f < freq_min and chopper:
phase = np.angle(f + 1j * alpha, deg=True)
Expand All @@ -88,14 +94,21 @@ def calib_func(f):

return calib_func

if chopper_on_limit is None:
chopper_on_limit = METRONIX_CHOPPER_ON_LIMIT
if version_pattern is None:
version_pattern = METRONIX_VERSION_PATTERN
if alpha_map is None:
alpha_map = METRONIX_ALPHA_MAP

filename = get_data_file(filename, METRONIX_DATA_PATH)
with open(filename, 'r') as file:
lines = file.readlines()

chopper = sampling_rate <= chopper_on_limit
mark = 'Chopper On' if chopper else 'Chopper Off'
start, stop = start_stop(lines, mark)
calib = calibration(np.loadtxt(lines[start:stop]), filename, chopper)
calib = calibration(np.loadtxt(lines[start:stop]), filename, chopper, alpha_map)
return calib


Expand Down

0 comments on commit 3dc022b

Please sign in to comment.