剖模, server desktop分支
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
profiler/awrams.py

769 lines
212 KiB

from dataclasses import dataclass,field
from pathlib import Path
from typing import Any,List,Callable
from enum import Enum
import numpy as np
import struct
import time
import locale
from datetime import datetime, timedelta
from myRamses import RamsesFactoryHandle
from tools.mylogger import log
from myconfig import TOKEN,NEWLINE,DATA_DIR,SAVE_EXT_NAME
from pubsub import pub
TIME_STR = "2022-06-10 16:16:16"
STD_TIME_STR_FMT = "%Y-%m-%d %H:%M:%S" # 小写y是两位年份
CUR_TIME_STR_FMT = "%Y-%m-%d %H:%M:%S"
@dataclass
class InfoFrame(object):
infoBytes:bytes = None
time_str:str= None
time_stamp:int= None
year:str= None
month:str= None
day:str= None
hour:str= None
minute:str= None
second:str= None
Roll: int= None
Pitch:int= None
Yaw:int= None
Hx:int= None
Hy:int= None
lon:int= None
lat:int= None
satelite_num:int= None
PDOP:int= None
HDOP:int= None
VDOP:int= None
Temperature:int= None
Humidity:int = None
Battery:int= None
ErrorCode:int= None
Azimuth:int= None
RunAngle:int= None
MeasureGroupNum:int= None
Tiltx:int= None
Tilty:int= None
Depth:int= None
SN1:str= None
SN2:str= None
SN3:str= None
MeasureInterval :int= None
MeasureRepeat: int= None
def output_info_frame(self, ):
ret = ""
ret = ret + "time : " + self.time_stamp
ret = ret + NEWLINE + "time_stamp : " + self.time_stamp
ret = ret + NEWLINE + "year : " + self.year
ret = ret + NEWLINE + "month : " + self.month
ret = ret + NEWLINE + "day : " + self.day
ret = ret + NEWLINE + "hour : " + self.hour
ret = ret + NEWLINE + "minute : " + self.minute
ret = ret + NEWLINE + "second : " + self.second
ret = ret + NEWLINE + "Roll : " + self.Roll
ret = ret + NEWLINE + "Pitch : " + self.Pitch
ret = ret + NEWLINE + "Yaw : " + self.Yaw
ret = ret + NEWLINE + "Hx : " + self.Hx
ret = ret + NEWLINE + "Hy : " + self.Hy
ret = ret + NEWLINE + "Hz : " + self.Hz
ret = ret + NEWLINE + "lon : " + self.lon
ret = ret + NEWLINE + "lat : " + self.lat
ret = ret + NEWLINE + "PDOP : " + self.PDOP
ret = ret + NEWLINE + "HDOP : " + self.HDOP
ret = ret + NEWLINE + "VDOP : " + self.VDOP
ret = ret + NEWLINE + "Temperature : " + self.Temperature
ret = ret + NEWLINE + "Humidity : " + self.Humidity
ret = ret + NEWLINE + "Battery : " + self.Battery
ret = ret + NEWLINE + "ErrorCode : " + self.ErrorCode
ret = ret + NEWLINE + "Azimuth : " + self.Azimuth
ret = ret + NEWLINE + "RunAngle : " + self.RunAngle
ret = ret + NEWLINE + "MeasureGroupNum : " + self.MeasureGroupNum
ret = ret + NEWLINE + "Tiltx : " + self.Tiltx
ret = ret + NEWLINE + "Tilty : " + self.Tilty
ret = ret + NEWLINE + "Depth : " + self.Depth
ret = ret + NEWLINE + "SN1 : " + self.SN1
ret = ret + NEWLINE + "SN2 : " + self.SN2
ret = ret + NEWLINE + "SN3 : " + self.SN3
ret = ret + NEWLINE + "MeasureInterval : " + self.MeasureInterval
ret = ret + NEWLINE + "MeasureRepeat : " + self.MeasureRepeat
return ret
pass
def set_info_frame(self, info:bytes):
try:
temp = struct.unpack("<BBBBBBHHHHHHIIHHHHHBBBHHIfff \
HHHBHBHHHHH",
info)
except Exception as e:
return
temp = list(temp) # tuple to list
# print(f" ==========={temp} {temp[0]}")
time_ = "20" + f"{str(temp[0])}" + "-" + f"{str(temp[1])}" + "-" + f"{str(temp[2])}" + " " \
+ f"{str(temp[3])}" + ":" + f"{str(temp[4])}" + ":" + f"{str(temp[5])}"
# print(f" ==========={time_}")
self.time_str = time_
self.time_stamp = 0
self.year:str = str(temp[0])
self.month:str = str(temp[1])
self.day:str = str(temp[2])
self.hour:str = str(temp[3])
self.minute:str = str(temp[4])
self.second:str = str(temp[5])
self.Roll: int = temp[6]
self.Pitch:int = temp[7]
self.Yaw:int = temp[8]
self.Hx:int = temp[9]
self.Hy:int = temp[10]
self.Hz:int = temp[11]
self.lon:int = temp[12]
self.lat:int = temp[13]
self.satelite_num:int = temp[14]
self.PDOP:int = temp[15]
self.HDOP:int = temp[16]
self.VDOP:int = temp[17]
self.Temperature:int = temp[18]
self.Humidity:int = temp[19]
self.Battery:int = temp[20]
self.ErrorCode:int = temp[21]
self.Azimuth:int = temp[22]
self.RunAngle:int = temp[23]
self.MeasureGroupNum:int = temp[24]
self.Tiltx:int = temp[25]
self.Tilty:int = temp[26]
self.Depth:int = temp[27]
self.SN1:str = hex(temp[28])[2:].upper()
self.SN2:str = hex(temp[29])[2:].upper()
self.SN3:str = hex(temp[30])[2:].upper()
self.MeasureInterval :int = temp[31]
self.MeasureRepeat: int = temp[32]
# print( self)
pass
@dataclass
class DataFrame(object):
''' AWRAMS 数据无需 remove_mask
多组 : 每组类似 15 + 26 + 576*3
数据接收方式 AWRAMS格式, 从文件读取
'''
deviceID:int = None
measureId: int = None
dataBytes:List[bytes] = None
groupNUm: int = 1
data_group:List[bytes] = None # 多组 每组三个传感器 [ Group1, group2, group3 group4 group5 ]
data_group_senor_bytes:List[List[bytes]] = None# [ [IP_byte, Sennsor1_byte,sensor2_byte,sensor3_byte], ] 先分测量组再传感器
data_sensor_group_bytes:List[List[bytes]] = None# [ [sensor1_grp1,sensor1_grp2,sensor1_grp3,sensor1_grp4,sensor1_grp5], ] 先传感器,再测量组
data_ip: List[List[bytes]] =None # IP数据
wavelength: List = None
spectrum: List = None
def set_data_bytes_list(self, data_frame:List[bytes]):
self.dataBytes = []
self.dataBytes = data_frame
pass
def set_data_bytes(self, byt:bytes):
''' 不含 15个 AA...头 , 26(*) +576(*) '''
self.dataBytes = []
# 判断是否包含IP部分数据
self.dataBytes = [byt]
pass
def set_data_group(self,groups:list):
self.data_group = groups
self.groupNum = len(self.data_group)
pass
def split_by_sensor(self,):
''' 兼容需求,需将 26个Ip信息帧传出去 '''
self.groupNum = len(self.dataBytes)
self.data_sensor_group_bytes =[]
self.data_ip = []
for i in range(3):
sensor = [] # 每组开始,赋予空值
for j in range(self.groupNum):
self.data_ip.append( self.dataBytes[j][:26] )
buf = self.dataBytes[j][26:]
sensor.append ( buf[i*576 : 576+i*576] )
pass
self.data_sensor_group_bytes.append(sensor)
pass
def split_by_measure(self,):
for i in range(self.groupNUm):
buf = self.data_group[i][41:]
measureGroup = [ ]
measureGroup.append( buf[0:576] )
measureGroup.append( buf[576:576+576] )
measureGroup.append( buf[576+576:576+576+576] )
self.data_group_senor_bytes.append(measureGroup)
pass
@dataclass
class AWRAMSData(object):
deviceid:int =None
configSensor:List[str] = None # 序列号 Lsky Esky Lwater
configFunc:List[str] = None
currentPath:Path =None
info_frame:InfoFrame = None
data_frame:DataFrame = None # 数据只到分组成byte结束
ramsesFactoryHandle:List[RamsesFactoryHandle] = None # 多个工厂,每个工厂有自己的 标定文件及参数,工厂标记序列哈,只处理相同序列号的产品数据
data_after_avg:List[int] =None
wavelength:List[int] =None
spectrum:List[int] =None
pass
def __post_init__(self):
assert self.deviceid != None , ">>>> AWRAMSData deviceid is empty"
pass
def build_datafactory_by_configSensor(self,calcfg:dict):
"""
@description : 调用工厂得到整数值
@param : 576字节数据, 数据id(第几个传感器,还是序列号)
@Returns : List[int]
"""
assert self.configSensor != None, ">>>> AWRAMSData configSensor is None"
self.ramsesFactoryHandle = [None,None,None]
for i in range(0, len( self.configSensor)):
cfg = calcfg[self.configFunc[i]]
tmp_ramses_factory_handle = RamsesFactoryHandle( sn= self.configSensor[i], cfg=cfg )
self.ramsesFactoryHandle[i] = tmp_ramses_factory_handle
def set_data_frame( self, data_frame:List[bytes], pth:Path, func ):
"""
@description : data_frame
"""
log.info(f" 处理 data_frame:List[bytes] ",__name__,"set_data_frame")
self.data_frame = DataFrame()
self.currentPath= pth
self.data_frame.set_data_bytes_list(data_frame )
# 对数据进行分组 split_by_sensor(self,)
self.data_frame.split_by_sensor()
self.spectrum =[]
self.wavelength =[]
# 分组后的数据 传给RamsesFactory 的 deal_raw_data_list, 返回平均值
for i in range( len(self.data_frame.data_sensor_group_bytes) ):
log.info( f" {i} ......... {len(self.data_frame.data_sensor_group_bytes[i])}",__name__, "set_data_frame ")
# log.info( f" {i} ......... ",__name__, "set_data_frame ")
self.ramsesFactoryHandle[i].deal_raw_data_list( self.data_frame.data_sensor_group_bytes[i] )
self.ramsesFactoryHandle[i].rf.get_wavelenth()
self.spectrum.append (self.ramsesFactoryHandle[i].rf.data_after_cal)
self.wavelength.append (self.ramsesFactoryHandle[i].rf.Wavelength)
# 补充IP解析数据
# 回调函数
func( self.wavelength, self.spectrum )
def set_info_frame(self, info_frame:bytes):
self.info_frame = InfoFrame()
self.info_frame.set_info_frame(info_frame) #已经解析
pass
class mycfg(object):
sensor_cfg = { 2: {1: {'FUNC': 'Lsky', 'SN': '85B5'}, 2: {'FUNC': 'Esky', 'SN': '50ED'}, 3: {'FUNC': 'Lwater', 'SN': '852F'}} }
cal_cfg = { 2:
{'Lsky': {'SN': '85B5', 'FUNC': 'Lsky', 'TYPE': 'SAM', 'samsn': '85B5', 'inifile': 'SAM_85B5.ini', 'calfile': 'Cal_SAM_85B5.dat', 'calaqfile': 'CalAQ_SAM_85B5.dat', 'backfile': 'Back_SAM_85B5.dat', 'cal': ['+NAN', '+NAN', '+NAN', '0.380824258865919', '0.480956813156357', '0.57671205380196', '0.643896236957154', '0.711054782209677', '0.758265752350778', '0.808854255795615', '0.869466331079482', '0.925321324749243', '1.00927399148091', '1.08699883630787', '1.18347377446848', '1.29079925777138', '1.39953731633192', '1.49536024576308', '1.57053962346542', '1.61051680455964', '1.60891142181803', '1.58014917632373', '1.54292073894688', '1.49782795775685', '1.46270054563491', '1.43754722159869', '1.42339247548624', '1.42249669690879', '1.43289834559721', '1.45081833970432', '1.48383024473758', '1.53350351710437', '1.59283171118034', '1.66682243068085', '1.75523084376357', '1.8490542829721', '1.94827772413977', '2.05297044933559', '2.15300125225733', '2.24489521732098', '2.32484518002751', '2.37710624055378', '2.41032149129472', '2.41401311078449', '2.39196623346607', '2.35386541795861', '2.30358502851024', '2.24350994611066', '2.18566570348128', '2.13007635400607', '2.08048312730197', '2.03943635277338', '1.9990019487276', '1.9542919602901', '1.90697476815729', '1.86132580586718', '1.81936966943287', '1.78948757961567', '1.77036444175303', '1.76067051308416', '1.76079151769139', '1.76865840458355', '1.78533977613825', '1.81054315659671', '1.84219534093402', '1.88262260694772', '1.92633682221336', '1.96919363361994', '2.00638558730966', '2.03543177536163', '2.05645335730185', '2.06889953924594', '2.07668786673546', '2.07746404954558', '2.07708946852551', '2.07239270419962', '2.06329693588947', '2.05235510505223', '2.03788342455449', '2.01750123705209', '1.99316265318269', '1.96344779927853', '1.92810068074762', '1.89032139564187', '1.84840056738145', '1.80188008685176', '1.7531429855323', '1.70184883503381', '1.64693756964496', '1.59639297188055', '1.55743828612696', '1.53081670223502', '1.509066853406', '1.48974595694359', '1.47286758281018', '1.45660325947575', '1.43667408186819', '1.40947906533906', '1.37746325817173', '1.3399910234164', '1.29987620320872', '1.26173586555032', '1.22580130202006', '1.19650560480134', '1.17258287879559', '1.15326969493003', '1.13448708666807', '1.11395465030131', '1.08753684763482', '1.06275254011917', '1.0408908566722', '1.02045255259373', '1.00192289281247', '0.987093337695555', '0.973433543184599', '0.961454156421677', '0.949336048850277', '0.937190359719721', '0.925018119816345', '0.911316167695048', '0.897751565127677', '0.885763756400802', '0.875253674622747', '0.866439225407856', '0.859594015285511', '0.856442344648432', '0.854520610650332', '0.853095084706978', '0.851996897997662', '0.849373999218705', '0.845008414644852', '0.837447706997702', '0.828042280687475', '0.816973957953628', '0.804392597259629', '0.790971383923797', '0.777350907812577', '0.764886212005326', '0.753124860834896', '0.74238339492221', '0.731621293455726', '0.720137856580817', '0.707681887074656', '0.692857487682284', '0.676149155140644', '0.657691286728854', '0.637091724889135', '0.615841105000382', '0.593277961352052', '0.571635565839856', '0.550558744253774', '0.529727893337181', '0.50958610090654', '0.490923786532866', '0.472942835508178', '0.455966760194577', '0.439389399108222', '0.42326262400571', '0.408064966451864', '0.392531100434587', '0.376760145430696', '0.361340837642809', '0.345797349215964', '0.330538900755072', '0.315423852713651', '0.300655326757946', '0.286125136929255', '0.27220666979537', '0.258963801456755', '0.246515726736969', '0.23471243489257', '0.223963260764627', '0.213811227531161', '0.204848303945938', '0.196342813423038', '0.188347519381911', '0.180847277367304', '0.173574321958823', '0.166782572540461', '0.159983659268987', '0.153496471427164', '0.146983276179518', '0.140664487031456', '0.134679615950644', '0.128426075217298', '0.122768803469292', '0.116973531179931', '0.111227699506044', '0.10561644888568', '0.0999165260100575', '0.09399466356358', '0.0884689961099718', '0.
'Esky': {'SN': '50ED', 'FUNC': 'Esky', 'TYPE': 'SAMIP', 'samsn': '859F', 'inifile': 'SAMIP_50ED_ALL.ini', 'calfile': 'Cal_SAM_859F.dat', 'calaqfile': 'CalAQ_SAM_859F.dat', 'backfile': 'Back_SAM_859F.dat', 'cal': ['+NAN', '+NAN', '+NAN', '+NAN', '0.549282530081406', '0.621825635483575', '0.705214779700439', '0.782494923890489', '0.848306874043027', '0.952625287085828', '1.06364941208026', '1.17099618224932', '1.27964867192574', '1.37878597375063', '1.42771820575638', '1.4380590385421', '1.41583530083466', '1.35751030831315', '1.28801896577964', '1.23773169248486', '1.19003370359023', '1.16911032524399', '1.16789019991587', '1.17908641221467', '1.20561721452238', '1.24993325867368', '1.3042108426727', '1.37841324495146', '1.46663415660191', '1.55813913029349', '1.66686169700722', '1.78274771118438', '1.89315285694338', '2.00609300108623', '2.10386062632617', '2.18077321675475', '2.23620955735442', '2.266135432154', '2.26878990322214', '2.25797663538301', '2.22685859649013', '2.17776313289299', '2.1222752364519', '2.06098344374156', '1.99948809108821', '1.94462577242816', '1.8989381397134', '1.8637403074385', '1.84137175128878', '1.82996842025894', '1.82735603324287', '1.82819560800875', '1.82831225824908', '1.82476779554239', '1.82142520789599', '1.82469614734563', '1.83514598035505', '1.85423698694933', '1.88102853388183', '1.91679111910883', '1.95713305118927', '2.0033013266641', '2.05191276620812', '2.10100127704333', '2.15116215495783', '2.19982933350689', '2.24341508467426', '2.28026480621138', '2.30634686430023', '2.31796219508283', '2.31589695847203', '2.29809543158409', '2.2668387918562', '2.22596956700897', '2.17822422289454', '2.1262484698939', '2.07154274835781', '2.01784816285075', '1.9635850159156', '1.90957838150673', '1.85662335754133', '1.80118393109592', '1.74752896056537', '1.69458207285898', '1.64238998731142', '1.59255947952261', '1.55042463825823', '1.52145095482736', '1.50182839545807', '1.48602912306116', '1.47501182897849', '1.46801797625318', '1.46252443118698', '1.4571840542778', '1.44881752975571', '1.433373570625', '1.4106211402034', '1.38310053980989', '1.35343439202405', '1.32469024445085', '1.29921323938482', '1.2757042981125', '1.25356245928469', '1.23230080447944', '1.20776661327865', '1.18172190161938', '1.16211138784545', '1.14836490545677', '1.13698033747357', '1.12673020620722', '1.115430409999', '1.10428562795353', '1.0934141739792', '1.0851095103506', '1.08102444265095', '1.08007408967758', '1.08238869164072', '1.08781099449618', '1.09202496629142', '1.09279743926351', '1.08928198225434', '1.08099296127192', '1.06779395728469', '1.05125732218958', '1.03307124483462', '1.01642561672595', '1.0017478853335', '0.98877464388953', '0.978591382441999', '0.970432000434175', '0.962460675619692', '0.953651636148435', '0.943103023421902', '0.930554197290447', '0.915976069829803', '0.89827602173614', '0.878650755188226', '0.859108524352699', '0.840122962860222', '0.820853477964574', '0.803547895709764', '0.786651104537257', '0.769194844035412', '0.751217544779413', '0.731862968408631', '0.710478767970412', '0.687207420135085', '0.662469528888512', '0.636358233772055', '0.609121332586544', '0.580838486227566', '0.552767035926316', '0.525876935395925', '0.499896413074609', '0.475284681533185', '0.452387885405863', '0.431848904268036', '0.413013739466993', '0.395710805209202', '0.380181587532903', '0.365591896979342', '0.352105459639395', '0.339276260510366', '0.32616389487977', '0.313880254968568', '0.301213081346406', '0.28867892621412', '0.276332017077307', '0.263867205519659', '0.251559483539307', '0.239917175566115', '0.228872841726392', '0.218348838385983', '0.208663588155861', '0.199488791575245', '0.191252859631973', '0.183364916996847', '0.175877803627425', '0.169219899131997', '0.162866423884395', '0.156727192611269', '0.151124000714184', '0.145138407523578', '0.13971586046093', '0.134506608902369', '0.129059486722317', '0.124203168251957', '0.119075347065844', '0.113513221481262', '0.107843336832916', '0.101790305051534', '0.0961960826886135', '0.0914035900672732', '0.0868727278
'Lwater': {'SN': '852F', 'FUNC': 'Lwater', 'TYPE': 'SAM', 'samsn': '852F', 'inifile': 'SAM_852F.ini', 'calfile': 'Cal_SAM_852F.dat', 'calaqfile': 'CalAQ_SAM_852F.dat', 'backfile': 'Back_SAM_852F.dat', 'cal': ['+NAN', '+NAN', '+NAN', '+NAN', '0.099081925071204', '0.107353085872454', '0.113070264290883', '0.114376499819276', '0.116544285861757', '0.1185169696214', '0.121561138979849', '0.126964155564462', '0.134364254054539', '0.144053858633949', '0.154882735476408', '0.16719673162462', '0.181437615712041', '0.194237970806716', '0.20386154720656', '0.211556336359081', '0.211417859085921', '0.208790218500257', '0.20437467341685', '0.198246352046376', '0.192966396099738', '0.18897229193584', '0.186243043953596', '0.18578727224224', '0.187114583931684', '0.189090911319999', '0.193600418301244', '0.200054490546523', '0.208072057884887', '0.218285958582262', '0.230634876630779', '0.243459214546046', '0.257752719558026', '0.272580246630834', '0.286712281491418', '0.300342726342385', '0.312249154496702', '0.321331680226083', '0.327112591435919', '0.329469692698771', '0.327542874308678', '0.322985767437387', '0.316224456423823', '0.308782990589082', '0.301183418606427', '0.293575702556702', '0.286696408065096', '0.280798842600295', '0.275808444038661', '0.271299340181449', '0.266225534078626', '0.260785177597799', '0.255342424416527', '0.250947812402455', '0.24777514930692', '0.246247860756574', '0.246092032695547', '0.247219177075727', '0.249675195006791', '0.253195744885965', '0.257788026175471', '0.26369589320837', '0.270555680751329', '0.277823002163503', '0.284937579021833', '0.291097253127256', '0.296043211270752', '0.299540235735164', '0.301795150475552', '0.303069111244916', '0.30355995799167', '0.303606664702465', '0.303007474849756', '0.302001653949665', '0.300361927400822', '0.297986890885497', '0.295325565249305', '0.291656567474701', '0.287335862742648', '0.282306131698578', '0.276702242286357', '0.270280607979823', '0.263050805729498', '0.255009286552014', '0.247098798503932', '0.2398191835424', '0.23466817932016', '0.230585310303585', '0.227279061892254', '0.224693206022759', '0.222993136629962', '0.221791418806722', '0.220695274517678', '0.219442816624831', '0.217000405185694', '0.213283732282521', '0.208384750595562', '0.202985389753009', '0.197924660371165', '0.193409646692029', '0.189628652821804', '0.186318519164064', '0.183315563032657', '0.180098687261504', '0.176492614557115', '0.172650422408531', '0.168979191040057', '0.166106531649937', '0.163622827054655', '0.161570328667133', '0.159693607722301', '0.157690305409947', '0.15544650006387', '0.153185383511044', '0.150954772341381', '0.148960802743881', '0.147350785081686', '0.146182367433256', '0.145312879880878', '0.144781245193999', '0.14473607505965', '0.144976432177203', '0.145464936657928', '0.145889790468872', '0.146174517595681', '0.14615421098877', '0.145743119998859', '0.144922809795091', '0.143805231938307', '0.142492952453543', '0.141053445832183', '0.139720571628269', '0.138186469198598', '0.136919534961634', '0.135732624874463', '0.134597268963386', '0.133432724955799', '0.13211501546202', '0.130478475600008', '0.128290801143951', '0.125726707508103', '0.122634784100445', '0.119472073398083', '0.116199128230447', '0.112813911251756', '0.109547291781198', '0.106268392530964', '0.10313443150471', '0.100113578226984', '0.097073834113113', '0.0941199411272616', '0.0911853891891621', '0.0882319764053268', '0.085267274530049', '0.0822333903307463', '0.0792187073154596', '0.0760907077820793', '0.0728848234132589', '0.0697048920692252', '0.0665600438164166', '0.0635411651500916', '0.0606319242880778', '0.0578670530064308', '0.0553155735461356', '0.0529193006810867', '0.050722718025932', '0.0486788470977319', '0.0467747209472673', '0.0450423813898501', '0.0434307741189043', '0.041915712424757', '0.0405250313895705', '0.0390895391040532', '0.0377897158697839', '0.0365016922380464', '0.0351788001916396', '0.0338645937424847', '0.0325302009051183', '0.0312848146149282', '0.0300375958148976', '0.0288302935207691', '0.027652203865544', '0.0264621566
## 封装该类的 处理buf hex2int函数
@dataclass
class AWRAMS(object):
deviceid:int = None
configSensor:List[str] =None # 序列号
configFunc:List[str] =None # 功能 Lsky Esky Lwater
calibrationCfg:dict =None
data : AWRAMSData =None
wavelength:List[np.ndarray] =None
spectrum:List[np.ndarray] =None
beginWavelength: float =None
endWavelength: float =None
rowFactor:float =None
wvInterval: float =None
newWavelength:np.ndarray=None
Lsky:np.ndarray =None # 无需记录原始波长,记录插值后的波长
Esky:np.ndarray =None
Lwater:np.ndarray =None
Lw:np.ndarray =None
Rs:np.ndarray =None
afterdeal = []
afterresult =[]
def __post_init__(self):
assert self.deviceid != None
self.data = AWRAMSData(deviceid=self.deviceid )
pass
def set_cfg_calibration(self,calcfg:dict):
assert self.deviceid != None
# assert self.deviceid in calcfg.keys(), f">>>> No calibrations data for the current id {self.deviceid}"
self.calibrationCfg = calcfg
pass
def set_retrieve(self,rtv:dict):
self.beginWavelength = float(rtv["beginWL"])
self.endWavelength = float(rtv["endWL"])
self.wvInterval = float(rtv["interval"])
self.rowFactor = float(rtv["rowFactor"])
self.newWavelength = np.arange(self.beginWavelength,self.endWavelength,self.wvInterval)
pass
def config_awrams(self, cfg:dict):
assert self.deviceid in cfg.keys(), f'>>>> Cannot get the configuration of device id {self.deviceid}'
self.configSensor = [None,None,None]
self.configFunc = [None,None,None]
# Lsky Esky Lwater
# {1: {'FUNC': 'Lsky', 'SN': '85B5'}, 2: {'FUNC': 'Esky', 'SN': '50ED'}, 3: {'FUNC': 'Lwater', 'SN': '852F'}}
for k,v in cfg.items() :
if v["FUNC"] == "Lsky":
self.configSensor[0] = v["SN"]
self.configFunc[0] = v["FUNC"]
if v["FUNC"] == "Esky":
self.configSensor[1] = v["SN"]
self.configFunc[1] = v["FUNC"]
if v["FUNC"] == "Lwater":
self.configSensor[2] = v["SN"]
self.configFunc[2] = v["FUNC"]
# 将传感器配置传给 AWRAMSDATA
self.data = AWRAMSData( deviceid=self.deviceid, configSensor=self.configSensor, configFunc=self.configFunc )
# 将标定配置传给 AWRAMSData 建立工厂
self.data.build_datafactory_by_configSensor( self.calibrationCfg)
pass
def callback( self, wavelength:list, spectrum:list ):
''' 处理返回的数据, 生成Lsky Esky Lwater Lw Rs '''
log.info( " call back for Lw Rs , 插值, 计算, 保存.................. " )
self.wavelength = wavelength
self.spectrum = spectrum
self.Lsky = np.interp( self.newWavelength, self.wavelength[0], self.spectrum[0])
self.Esky = np.interp( self.newWavelength, self.wavelength[1], self.spectrum[1])
self.Lwater = np.interp( self.newWavelength, self.wavelength[2], self.spectrum[2])
self.Lw = self.Lwater - self.rowFactor * self.Lsky
self.Rs = self.Lw/self.Esky
self.save()
# 保存 进一步处理
def save(self, mode=0):
''' 保存在原文件所在目录 '''
pth = self.data.currentPath
fanme = "20"+self.data.info_frame.year+"_"+self.data.info_frame.month+"_" \
+self.data.info_frame.day+"_"+self.data.info_frame.hour+"_" \
+self.data.info_frame.minute+"_"+self.data.info_frame.second \
+".csv"
save_fpath = pth.joinpath(fanme)
ret = ""
ret = ret + str(self.deviceid)+"_"+pth.parts[-1] + TOKEN + self.list2str(list(self.newWavelength))
ret = ret + NEWLINE + "Lsky" + TOKEN + self.list2str( list(self.Lsky) )
ret = ret + NEWLINE + "Esky" + TOKEN + self.list2str( list(self.Esky) )
ret = ret + NEWLINE + "Lwater" + TOKEN + self.list2str( list(self.Lwater) )
ret = ret + NEWLINE + "Lw" + TOKEN + self.list2str( list(self.Lw) )
ret = ret + NEWLINE + "Rs" + TOKEN + self.list2str( list(self.Rs) )
save_fpath.write_text( ret )
pass
def list2str( self, lst:list, token=";" ):
''' 保存在原文件所在目录 '''
ret= ""
for i in range(len(lst)):
if i == 0:
ret = ret + str(lst[i])
else:
ret = ret + TOKEN + str(lst[i])
return ret
pass
def registet_func(self, func:Callable):
self.afterdeal.append(func)
pass
def execute_func(self, ):
for f in self.afterdeal:
f()
pass
@dataclass
class Handheld(object):
deviceid:int = None
configSensor:List[str] =None # 序列号
configFunc:List[str] =None # 功能 Lsky Esky Lwater
calibrationCfg:dict =None
data : AWRAMSData =None
wavelength:List[np.ndarray] =None
spectrum:List[np.ndarray] =None
beginWavelength: float =None
endWavelength: float =None
rowFactor:float =None
wvInterval: float =None
newWavelength:np.ndarray=None
Lsky:np.ndarray =None # 无需记录原始波长,记录插值后的波长
Esky:np.ndarray =None
Lwater:np.ndarray =None
Lw:np.ndarray =None
Rs:np.ndarray =None
afterdeal = []
afterresult =[]
def __post_init__(self):
assert self.deviceid != None
self.data = AWRAMSData(deviceid=self.deviceid )
pass
def set_cfg_calibration(self,calcfg:dict):
assert self.deviceid != None
# assert self.deviceid in calcfg.keys(), f">>>> No calibrations data for the current id {self.deviceid}"
self.calibrationCfg = calcfg
pass
def set_retrieve(self,rtv:dict):
self.beginWavelength = float(rtv["beginWL"])
self.endWavelength = float(rtv["endWL"])
self.wvInterval = float(rtv["interval"])
self.rowFactor = float(rtv["rowFactor"])
self.newWavelength = np.arange(self.beginWavelength,self.endWavelength,self.wvInterval)
pass
def config_awrams(self, cfg:dict):
assert self.deviceid in cfg.keys(), f'>>>> Cannot get the configuration of device id {self.deviceid}'
self.configSensor = [None,None,None]
self.configFunc = [None,None,None]
# Lsky Esky Lwater
# {1: {'FUNC': 'Lsky', 'SN': '85B5'}, 2: {'FUNC': 'Esky', 'SN': '50ED'}, 3: {'FUNC': 'Lwater', 'SN': '852F'}}
for k,v in cfg.items() :
if v["FUNC"] == "Lsky":
self.configSensor[0] = v["SN"]
self.configFunc[0] = v["FUNC"]
if v["FUNC"] == "Esky":
self.configSensor[1] = v["SN"]
self.configFunc[1] = v["FUNC"]
if v["FUNC"] == "Lwater":
self.configSensor[2] = v["SN"]
self.configFunc[2] = v["FUNC"]
# 将传感器配置传给 AWRAMSDATA
self.data = AWRAMSData( deviceid=self.deviceid, configSensor=self.configSensor, configFunc=self.configFunc )
# 将标定配置传给 AWRAMSData 建立工厂
self.data.build_datafactory_by_configSensor( self.calibrationCfg)
pass
def callback( self, wavelength:list, spectrum:list ):
''' 处理返回的数据, 生成Lsky Esky Lwater Lw Rs '''
log.info( " call back for Lw Rs , 插值, 计算, 保存.................. " )
self.wavelength = wavelength
self.spectrum = spectrum
self.Lsky = np.interp( self.newWavelength, self.wavelength[0], self.spectrum[0])
self.Esky = np.interp( self.newWavelength, self.wavelength[1], self.spectrum[1])
self.Lwater = np.interp( self.newWavelength, self.wavelength[2], self.spectrum[2])
self.Lw = self.Lwater - self.rowFactor * self.Lsky
self.Rs = self.Lw/self.Esky
# 展示数据
msg = {"flag":"data", "data":
[ self.meauretime, self.newWavelength, self.Rs ] }
pub.sendMessage('update' , msg=msg)
self.save_handheld()
# 保存 进一步处理
def save(self, mode=0):
''' 保存在原文件所在目录 '''
pth = self.data.currentPath
fanme = "20"+self.data.info_frame.year+"_"+self.data.info_frame.month+"_" \
+self.data.info_frame.day+"_"+self.data.info_frame.hour+"_" \
+self.data.info_frame.minute+"_"+self.data.info_frame.second \
+".csv"
save_fpath = pth.joinpath(fanme)
ret = ""
ret = ret + str(self.deviceid)+"_"+pth.parts[-1] + TOKEN + self.list2str(list(self.newWavelength))
ret = ret + NEWLINE + "Lsky" + TOKEN + self.list2str( list(self.Lsky) )
ret = ret + NEWLINE + "Esky" + TOKEN + self.list2str( list(self.Esky) )
ret = ret + NEWLINE + "Lwater" + TOKEN + self.list2str( list(self.Lwater) )
ret = ret + NEWLINE + "Lw" + TOKEN + self.list2str( list(self.Lw) )
ret = ret + NEWLINE + "Rs" + TOKEN + self.list2str( list(self.Rs) )
save_fpath.write_text( ret )
pass
def save_handheld(self, mode=0):
''' 保存到五个文件 Lsky Esky Lwater Lw Rs '''
pth = DATA_DIR
pth = pth.joinpath("output",
self.data.info_frame.year+"_"+self.data.info_frame.month+"_"
+self.data.info_frame.day+"_"+self.data.info_frame.hour+"_"
+self.data.info_frame.minute+"_"+self.data.info_frame.second
)
if not pth.exists():
pth.mkdir(parents=True)
header = str(self.deviceid) + TOKEN + self.list2str(list(self.newWavelength))
Lsky_fpath = pth.joinpath( "Lsky"+SAVE_EXT_NAME )
Esky_fpath = pth.joinpath( "Esky"+SAVE_EXT_NAME )
Lwater_fpath = pth.joinpath( "Lwater"+SAVE_EXT_NAME )
Lw_fpath = pth.joinpath( "Lw"+SAVE_EXT_NAME )
Rs_fpath = pth.joinpath( "Rs"+SAVE_EXT_NAME )
if not Lsky_fpath.exists():
Lsky_fpath.write_text( header )
if not Esky_fpath.exists():
Esky_fpath.write_text( header )
if not Lwater_fpath.exists():
Lwater_fpath.write_text( header )
if not Lw_fpath.exists():
Lw_fpath.write_text( header )
if not Rs_fpath.exists():
Rs_fpath.write_text( header )
# 添加时间
Lsky = NEWLINE + self.meauretime+ TOKEN + self.list2str( list(self.Lsky) )
Esky = NEWLINE + self.meauretime+ TOKEN + self.list2str( list(self.Esky) )
Lwater = NEWLINE + self.meauretime+ TOKEN + self.list2str( list(self.Lwater) )
Lw = NEWLINE + self.meauretime+ TOKEN + self.list2str( list(self.Lw) )
Rs = NEWLINE + self.meauretime+ TOKEN + self.list2str( list(self.Rs) )
# 追加写入
with open(Lsky_fpath, 'a') as f:
f.write( Lsky )
with open(Esky_fpath, 'a') as f:
f.write( Esky )
with open(Lwater_fpath, 'a') as f:
f.write( Lwater )
with open(Lw_fpath, 'a') as f:
f.write( Lw )
with open(Rs_fpath, 'a') as f:
f.write( Rs )
pass
def list2str( self, lst:list, token=";" ):
''' 保存在原文件所在目录 '''
ret= ""
for i in range(len(lst)):
if i == 0:
ret = ret + str(lst[i])
else:
ret = ret + TOKEN + str(lst[i])
return ret
pass
def registet_func(self, func:Callable):
self.afterdeal.append(func)
pass
def execute_func(self, ):
for f in self.afterdeal:
f()
pass
def deal_sensor_byte(self, byt:bytes):
"""
@description : 处理sensor.bin字节, 1754bytes= 26+3*576
"""
readout = byt
assert len(readout)%1754 == 0, f" wrong bytes number, sensor.bin"
locale.setlocale(locale.LC_ALL, '')
self.meauretime = self.data.info_frame.time_str
bytes_list = []
for i in range( int(len(readout)/1754)):
self.cur_datetime = datetime.strptime(self.meauretime, STD_TIME_STR_FMT)
self.cur_datetime= self.cur_datetime + timedelta( seconds=self.data.info_frame.MeasureInterval) * i
self.meauretime = datetime.strftime(self.cur_datetime, STD_TIME_STR_FMT)
tmp_byte = [ readout[ i*1754 : i*1754+1754 ] ]
self.data.set_data_frame( tmp_byte, None, self.callback )
pass
class AwramsHandle(object):
def __init__(self, deviceid=2, cfg=None, calcfg=None, rtv=None):
self.sensor_cfg = cfg
self.calcfg = calcfg
self.retrieve = rtv
self.device_id = deviceid
self.aw = AWRAMS( deviceid=self.device_id )
self.aw.set_cfg_calibration( self.calcfg )
self.aw.set_retrieve( self.retrieve)
self.aw.config_awrams( self.sensor_cfg )
self.afterdeal = []
pass
def read_one_folder_awrams_online(self, pth:Path):
''' self.data 传数据 info_frame, data_frame'''
log.info(f" 读一个文件夹进行处理 {pth}" ,__name__,"read_one_folder_awrams_online")
bytes_list = []
bin_files = pth.glob('*.bin')
for bf in bin_files:
if bf.name != "info.bin":
bytes_list.append( self.read_bin(bf) )
self.aw.data.set_data_frame( bytes_list, pth, self.aw.callback ) # 目录也要传过去
pass
def read_bin(self,fpath: Path):
assert fpath.exists(), f">>>> not find {fpath} "
ret = b''
with open(fpath, 'rb') as file:
ret = file.read()
return ret
pass
pass
class HandheldHandle(object):
def __init__(self, deviceid=2, cfg=None, calcfg=None, rtv=None):
self.sensor_cfg = cfg
self.calcfg = calcfg
self.retrieve = rtv
self.device_id = deviceid
self.aw = Handheld( deviceid=self.device_id )
self.aw.set_cfg_calibration( self.calcfg )
self.aw.set_retrieve( self.retrieve)
self.aw.config_awrams( self.sensor_cfg )
self.afterdeal = []
pass
def deal_sensor_bin(self, pth:Path):
''' 要分离出每组时间 '''
# print(self.aw.data.info_frame )
byt = self.read_bin(pth)
self.aw.deal_sensor_byte( byt )
pass
def read_folders_from_SD(self, pth:Path):
''' self.data 传数据 info_frame, data_frame'''
log.info(f" 读 SD文件夹进行处理 {pth}" ,__name__,"read_one_folder_awrams_online")
fs = None
filelist = []
fs = pth.glob( "*/*/info/*" )
msg = {"flag":"notice", "data": "开始处理数据 " }
pub.sendMessage('update' , msg=msg)
for f in fs:
if f.stat().st_size == 0:
continue
mypath = f.parent.parent
log.info(f" mypath {mypath}")
sensor_path = mypath.joinpath( 'sensor', f.name )
if not sensor_path.exists() or sensor_path.stat().st_size == 0:
continue
# 处理SD 需要多处理info_frame
self.aw.data.set_info_frame( self.read_bin(f) )
# 按单次测量的处理
self.deal_sensor_bin( sensor_path)
msg = {"flag":"notice", "data": "完成数据处理 " }
pub.sendMessage('update' , msg=msg)
pass
def read_bin(self,fpath: Path):
assert fpath.exists(), f">>>> not find {fpath} "
ret = b''
with open(fpath, 'rb') as file:
ret = file.read()
return ret
pass
pass
class ExternalFun(object):
def __init__(self, aw:AWRAMSData=None):
pass
@staticmethod
def get_par_400_700( aw:AWRAMSData ):
par_400_700 = 0.0
for i in range( 50, 350, 1 ):
par_400_700 = par_400_700 + aw.Esky
return par_400_700
pass
@staticmethod
def get_par_350_950( aw:AWRAMSData):
par_350_950 = 0.0
for i in range( 0, 600, 1 ):
par_350_950 = par_350_950 + aw.Esky
return par_350_950
pass
@staticmethod
def get_chl( aw:AWRAMSData ):
return 0.0
pass
pass
class Acfg:
cfg= {1: {'FUNC': 'Lsky', 'SN': '85C2'}, 2: {'FUNC': 'Esky', 'SN': '50ED'}, 3: {'FUNC': 'Lwater', 'SN': '852F'}}
calcfg= {'Lsky': {'SN': '85C2', 'FUNC': 'Lsky', 'TYPE': 'SAM', 'samsn': '85C2', 'inifile': 'SAM_85C2.ini', 'calfile': 'Cal_SAM_85C2.dat', 'calaqfile': 'CalAQ_SAM_85C2.dat', 'backfile': 'Back_SAM_85C2.dat', 'cal': ['+NAN', '+NAN', '+NAN', '+NAN', '0.63607834406219', '0.718127096538326', '0.812216798598817', '0.914442457893824', '1.03343454996493', '1.15933885373154', '1.29479643420084', '1.43677001665361', '1.56848190839848', '1.67447970580786', '1.73889146263122', '1.73244260903254', '1.69192414835577', '1.61622082709111', '1.5270353751059', '1.46047364317447', '1.41296265347303', '1.39311700938614', '1.39852760194912', '1.42554260762195', '1.4700968507055', '1.53635620441153', '1.62082921439482', '1.7237742274692', '1.84755749123585', '1.98432162640748', '2.12689147788928', '2.27366206436985', '2.41515311346652', '2.54267625633242', '2.65248757975216', '2.73468816910085', '2.77942967914337', '2.79565522877852', '2.77806955157777', '2.73891358506136', '2.69063680629234', '2.6282750012231', '2.56338981635672', '2.49687123158122', '2.4309933291301', '2.36993023507549', '2.32161558853422', '2.28314087424895', '2.25846162084215', '2.24994522727437', '2.25549954861938', '2.27668712518828', '2.30493256690767', '2.33255656715114', '2.35720768052834', '2.37757260164886', '2.40002374613635', '2.42905705371268', '2.4673307146952', '2.51294806887351', '2.56575613007937', '2.62193109349514', '2.67842958533042', '2.73753357518048', '2.7913329207665', '2.8423865946084', '2.89027340109411', '2.93157202353872', '2.96274906831744', '2.98346548286676', '2.98798866816702', '2.97297872291037', '2.93559195933884', '2.87662286787444', '2.80380712732453', '2.72275302648293', '2.6373282860483', '2.55448424591999', '2.47778601018081', '2.40377038673745', '2.33484934711808', '2.2698793292122', '2.20651375086021', '2.14704740380438', '2.08988763493926', '2.0346458333411', '1.98224825426305', '1.93452491480363', '1.89146397238956', '1.8653548650058', '1.84879059497611', '1.8371693859594', '1.83197484046632', '1.83105981302589', '1.83169544364263', '1.83369029033973', '1.82985806236537', '1.81684973319988', '1.7961872241934', '1.76710061502081', '1.73459539138238', '1.70421983997876', '1.67561137148482', '1.65483955955664', '1.64005456402998', '1.63103006074007', '1.62646109446179', '1.62483351776729', '1.61794627041935', '1.60638099649103', '1.59160518167625', '1.57856502215575', '1.56723770329055', '1.55884357172436', '1.55081356970191', '1.5440909800073', '1.53473504989623', '1.52335047710565', '1.51140772671503', '1.49628095199023', '1.48082183121466', '1.4668009209167', '1.45300293604089', '1.43913779397284', '1.42471804569399', '1.41116472805909', '1.39601546893463', '1.3795217760198', '1.36066889187826', '1.3395095955925', '1.31771535033317', '1.29387042583309', '1.26955248066416', '1.24679628762607', '1.22528340309588', '1.20486570624273', '1.18549206002717', '1.16756178865017', '1.15111957559202', '1.13469169498467', '1.11621030173639', '1.09569135455521', '1.07275908862679', '1.04573742076401', '1.01572627731635', '0.98381634710957', '0.950352559222019', '0.915760692099546', '0.881456713194727', '0.848044938315892', '0.816038892625263', '0.785024915273114', '0.754850603543375', '0.725690734641156', '0.697449171016547', '0.669846688269734', '0.643097402076866', '0.615807664216611', '0.589686777991522', '0.563134754809732', '0.537416951715189', '0.512092492411016', '0.487999533569422', '0.464888888064952', '0.442938789447448', '0.422687485052752', '0.403811887358409', '0.386602162003576', '0.370223420179536', '0.355850417036878', '0.342554004485098', '0.330263308885894', '0.318892349787717', '0.308390808825048', '0.29923285616278', '0.290049870831026', '0.280987628676386', '0.272166680353347', '0.263184326446838', '0.254328565772045', '0.245533707259442', '0.236284846332185', '0.227364007848682', '0.218575558150069', '0.209919539318749', '0.201723665364338', '0.193439417628863', '0.185095630044576', '0.176908759167827', '0.168492281717065', '0.15971620196905', '0.151519711134016', '0.143896290701505', '0.137256829327417', '0.13133376173
def my_init():
''' 写个handle 类处理'''
cfg = mycfg.cfg
calcfg = mycfg.cal_cfg
device_id = 2
aw = AWRAMS( deviceid=device_id)
aw.set_cfg_calibration(calcfg)
aw.config_awrams(cfg)
if __name__ == '__main__':
# my_init()
ah =AwramsHandle( 2, Acfg.cfg, Acfg.calcfg)
fpath = Path.cwd()
# t= ("data", "2","2002","8", "17","51", "info.bin")
# t= ("data" )
fpath = fpath.joinpath('data\2\2022\8\17\51\0.bin')
# fpath = fpath.joinpath( *t )
# ah.read_one_folder_awrams_online(fpath)
if fpath.exists():
print(" exsited")
else:
print("not exsited")
# ah.read_one_folder_awrams_online(fpath)
# data\2\2022\8\17\51\0.bin