添加对python算法的支持
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
try:
|
||||
from .config_reader import config_read_double as ConfigReadDouble
|
||||
except ImportError:
|
||||
from config_reader import config_read_double as ConfigReadDouble
|
||||
|
||||
|
||||
def floatread(filepath, columns):
|
||||
filepath = str(filepath)
|
||||
if not os.path.isfile(filepath):
|
||||
print(f'file ={filepath}')
|
||||
return np.array([])
|
||||
|
||||
try:
|
||||
data = np.loadtxt(filepath)
|
||||
if data.size == 0:
|
||||
return np.array([])
|
||||
if data.ndim == 1:
|
||||
return data
|
||||
if columns == 1 and data.shape[1] == 1:
|
||||
return data.ravel()
|
||||
return data
|
||||
except Exception:
|
||||
try:
|
||||
with open(filepath, 'r') as f:
|
||||
lines = f.readlines()
|
||||
values = []
|
||||
for line in lines:
|
||||
parts = line.strip().split()
|
||||
if parts:
|
||||
row = []
|
||||
for p in parts:
|
||||
try:
|
||||
row.append(float(p))
|
||||
except ValueError:
|
||||
pass
|
||||
if row:
|
||||
values.append(row)
|
||||
if values:
|
||||
return np.array(values)
|
||||
return np.array([])
|
||||
except Exception as e:
|
||||
print(f'file ={filepath}')
|
||||
print(str(e))
|
||||
return np.array([])
|
||||
|
||||
|
||||
def _chkvek(calib_struct, prefix, nom, act, lengthconflict):
|
||||
nom = np.asarray(nom, dtype=np.float64).ravel()
|
||||
act = np.asarray(act, dtype=np.float64).ravel()
|
||||
|
||||
if len(nom) > 0 and len(nom) == len(act):
|
||||
sort_idx = np.argsort(nom)
|
||||
nom = nom[sort_idx]
|
||||
act = act[sort_idx]
|
||||
|
||||
idx = np.where(nom <= 0)[0]
|
||||
if len(idx) > 0:
|
||||
nom = np.delete(nom, idx)
|
||||
act = np.delete(act, idx)
|
||||
|
||||
calib_struct[f'{prefix}nom'] = np.concatenate([[0.0], nom, [1000.0]])
|
||||
calib_struct[f'{prefix}act'] = np.concatenate([[0.0], act, [1000.0]])
|
||||
calib_struct[f'{prefix}err'] = 0
|
||||
elif len(nom) == 0 and len(act) == 0:
|
||||
calib_struct[f'{prefix}nom'] = np.array([])
|
||||
calib_struct[f'{prefix}act'] = np.array([])
|
||||
calib_struct[f'{prefix}err'] = 0
|
||||
else:
|
||||
calib_struct[f'{prefix}nom'] = np.array([])
|
||||
calib_struct[f'{prefix}act'] = np.array([])
|
||||
calib_struct[f'{prefix}err'] = lengthconflict
|
||||
if calib_struct.get('err', 0) == 0:
|
||||
calib_struct['err'] = lengthconflict
|
||||
|
||||
return calib_struct
|
||||
|
||||
|
||||
def read_calibration(filename):
|
||||
lengthconflict = 189
|
||||
doubledefined = 190
|
||||
|
||||
calib_struct = {}
|
||||
|
||||
try:
|
||||
calib_struct['calibrationoff'] = ConfigReadDouble(
|
||||
filename, 'cableType to calibration section link', 'calibrationoff')
|
||||
calib_struct['mincableType'] = ConfigReadDouble(
|
||||
filename, 'cableType to calibration section link', 'mincableType')
|
||||
calib_struct['maxcableType'] = ConfigReadDouble(
|
||||
filename, 'cableType to calibration section link', 'maxcableType')
|
||||
calib_struct['sectionType'] = ConfigReadDouble(
|
||||
filename, 'cableType to calibration section link', 'sectionType')
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
if calib_struct['calibrationoff'] is None or (
|
||||
isinstance(calib_struct['calibrationoff'], (list, np.ndarray)) and
|
||||
len(calib_struct['calibrationoff']) == 0):
|
||||
return {}
|
||||
|
||||
calib_struct['err'] = 0
|
||||
|
||||
try:
|
||||
secType_len = len(calib_struct['sectionType']) if hasattr(calib_struct['sectionType'], '__len__') else 1
|
||||
minc_len = len(calib_struct['mincableType']) if hasattr(calib_struct['mincableType'], '__len__') else 1
|
||||
maxc_len = len(calib_struct['maxcableType']) if hasattr(calib_struct['maxcableType'], '__len__') else 1
|
||||
|
||||
if not (secType_len == minc_len == maxc_len):
|
||||
calib_struct['err'] = lengthconflict
|
||||
except Exception:
|
||||
calib_struct['err'] = lengthconflict
|
||||
|
||||
if calib_struct['err'] != 0:
|
||||
return calib_struct
|
||||
|
||||
try:
|
||||
secType = np.atleast_1d(np.asarray(calib_struct['sectionType'], dtype=np.int32)).ravel()
|
||||
minc = np.atleast_1d(np.asarray(calib_struct['mincableType'], dtype=np.float64)).ravel()
|
||||
maxc = np.atleast_1d(np.asarray(calib_struct['maxcableType'], dtype=np.float64)).ravel()
|
||||
|
||||
sort_idx = np.argsort(secType)
|
||||
secType = secType[sort_idx]
|
||||
minc = minc[sort_idx]
|
||||
maxc = maxc[sort_idx]
|
||||
|
||||
calib_struct['sectionType'] = secType
|
||||
calib_struct['mincableType'] = minc
|
||||
calib_struct['maxcableType'] = maxc
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
calib_struct['sect'] = {}
|
||||
|
||||
try:
|
||||
max_sec = int(np.max(calib_struct['sectionType']))
|
||||
except Exception:
|
||||
return calib_struct
|
||||
|
||||
for ii in range(1, max_sec + 1):
|
||||
calib_struct['sect'][ii] = {}
|
||||
|
||||
try:
|
||||
nom = ConfigReadDouble(filename, f'section{ii}: Diameter calibration', 'Nominal')
|
||||
act = ConfigReadDouble(filename, f'section{ii}: Diameter calibration', 'Actual')
|
||||
calib_struct = _chkvek(calib_struct, f'sect.{ii}.diam.', nom, act, lengthconflict)
|
||||
except Exception:
|
||||
calib_struct[f'sect.{ii}.diam.nom'] = np.array([])
|
||||
calib_struct[f'sect.{ii}.diam.act'] = np.array([])
|
||||
calib_struct[f'sect.{ii}.diam.err'] = 0
|
||||
|
||||
try:
|
||||
nom = ConfigReadDouble(filename, f'section{ii}: Wall thickness calibration', 'Nominal')
|
||||
act = ConfigReadDouble(filename, f'section{ii}: Wall thickness calibration', 'Actual')
|
||||
calib_struct = _chkvek(calib_struct, f'sect.{ii}.wall.', nom, act, lengthconflict)
|
||||
except Exception:
|
||||
calib_struct[f'sect.{ii}.wall.nom'] = np.array([])
|
||||
calib_struct[f'sect.{ii}.wall.act'] = np.array([])
|
||||
calib_struct[f'sect.{ii}.wall.err'] = 0
|
||||
|
||||
try:
|
||||
nom = ConfigReadDouble(filename, f'section{ii}: Outer Layer thickness calibration', 'Nominal')
|
||||
act = ConfigReadDouble(filename, f'section{ii}: Outer Layer thickness calibration', 'Actual')
|
||||
calib_struct = _chkvek(calib_struct, f'sect.{ii}.lay1.', nom, act, lengthconflict)
|
||||
except Exception:
|
||||
calib_struct[f'sect.{ii}.lay1.nom'] = np.array([])
|
||||
calib_struct[f'sect.{ii}.lay1.act'] = np.array([])
|
||||
calib_struct[f'sect.{ii}.lay1.err'] = 0
|
||||
|
||||
try:
|
||||
nom = ConfigReadDouble(filename, f'section{ii}: Second Layer thickness calibration', 'Nominal')
|
||||
act = ConfigReadDouble(filename, f'section{ii}: Second Layer thickness calibration', 'Actual')
|
||||
calib_struct = _chkvek(calib_struct, f'sect.{ii}.lay2.', nom, act, lengthconflict)
|
||||
except Exception:
|
||||
calib_struct[f'sect.{ii}.lay2.nom'] = np.array([])
|
||||
calib_struct[f'sect.{ii}.lay2.act'] = np.array([])
|
||||
calib_struct[f'sect.{ii}.lay2.err'] = 0
|
||||
|
||||
try:
|
||||
nom = ConfigReadDouble(filename, f'section{ii}: Third Layer thickness calibration', 'Nominal')
|
||||
act = ConfigReadDouble(filename, f'section{ii}: Third Layer thickness calibration', 'Actual')
|
||||
calib_struct = _chkvek(calib_struct, f'sect.{ii}.lay3.', nom, act, lengthconflict)
|
||||
except Exception:
|
||||
calib_struct[f'sect.{ii}.lay3.nom'] = np.array([])
|
||||
calib_struct[f'sect.{ii}.lay3.act'] = np.array([])
|
||||
calib_struct[f'sect.{ii}.lay3.err'] = 0
|
||||
|
||||
length1 = len(np.atleast_1d(calib_struct.get(f'sect.{ii}.lay1.nom', np.array([]))))
|
||||
length2 = len(np.atleast_1d(calib_struct.get(f'sect.{ii}.lay2.nom', np.array([]))))
|
||||
length3 = len(np.atleast_1d(calib_struct.get(f'sect.{ii}.lay3.nom', np.array([]))))
|
||||
|
||||
if length1 > 0 and length2 > 0 and length3 > 0:
|
||||
calib_struct[f'sect.{ii}.lay3.err'] = doubledefined
|
||||
if calib_struct.get('err', 0) == 0:
|
||||
calib_struct['err'] = doubledefined
|
||||
|
||||
return calib_struct
|
||||
Reference in New Issue
Block a user