from tools.mylogger import log
from tools.myexception import MyException
from typing import Tuple,List,Optional,Union,Callable
from pathlib import Path, PurePath
from myconfig import MyConfig,DeviceType,RamsesFunc
from myconfig import FILE_MARK,CURRENT_DIR,CAL_DIR,DATA_DIR,SAVE_EXT_NAME,INTERVAL,SEPARATOR

class ReadCal():
    def __init__(self) -> None:
        pass

    # 数据加时间值
    @staticmethod
    def read_columns_set_by_mark(fpath:Path, mark:list, *column ) ->  Tuple[str,List]  :
    # def read_column_1set_by_mark(fpath:str, column:int, *args ) ->  Tuple[  str, List  ]  :
        '''
        return: time:str List
        mark :  ['Spectrum','DATA']
                default end with "[END]"
        '''
        log.info(f"read_column_1set_by_mark : \
            {fpath} , mark:{mark} , column:{column}", __name__, "", "")

        # 处理mark args[0] = Spectrum
        if len(mark) != 2:
            log.error( f"read_column_1set_by_mark() wrong args ",__name__, "", "" )
            raise MyException( f"read_column_1set_by_mark() wrong args " )

        if len(column) == 0:
            log.error(f"read_column_1set_by_mark(), no column para, pls input column ",__name__, "", "" )
            raise MyException( f"read_column_1set_by_mark() , pls input column " )

        if len( mark ) == 0:
            mark_1 = '[Spectrum]'
            mark_2 = '[DATA]'

        mark_1 = '['+ mark[0] +']'
        mark_2 = '['+ mark[1] +']'
        res = []
        sn = ReadCal.readFileSNbyIDDevice(fpath)  
        # 预备返回数据结构
        # res_time = []
        res_data = []  
        columnLen = len(column)         
        for i in range(columnLen):
            res_data.append( [] )     

        with fpath.open('r') as f_handle:
            rflag = 0
            # res = []
            # res_time = []
            # res_data = []

            for line in f_handle:
                if mark_1 in line   and  rflag == 0:
                    log.debug(f" find {mark_1}", __name__, "", "" )
                    rflag += 1
                    continue

                if mark_2 in line and  rflag > 3 :
                    log.debug(f" find {mark_1}  end ", __name__, "", "" )
                    rflag = 0
                    continue

                # 获取时间
                if rflag == 1 : 
                    data = line.strip('\n').strip(' ').split('=')   
                    if data[0].strip()   == "DateTime" :
                        log.debug(f" find {mark_1} -> DateTime {data[1]} ", __name__, "", "" )
                        time_ =  data[1].strip()     
                        rflag += 1
                        continue

                if rflag == 2 and ( mark_2 in line  ) :
                    log.debug(f" find {mark_2} ->   ", __name__, "", "" )
                    rflag += 1
                    continue

                if rflag == 3 :
                    if mark_2 not in line:
                        # log.debug(f" get data {line}", __name__, "", "" )
                        data = line.strip('\n').strip(' ').split(' ') 
                        # 忽略第一行，第一行0开头，保存积分时间的
                        if  data[0].strip() ==  "0"  :
                            continue   
                        else:
                            # if data[1] != "-NAN" and data[1] != "NAN" and data[1] != "+NAN" :
                            # 处理多列 
                            for i in range(columnLen):
                                # res_time.append( time_ )  
                                res_data[i].append( data[column[i]] )                                                   
                            continue

                if rflag == 3 and ( mark_2 in line  ) :
                    log.debug(f" find {mark_2} end , line: {line}  ", __name__, "", "" )
                    rflag += 1
                    continue    #需要继续让其找到   

                if rflag == 4:   
                    log.debug(f" job done......", __name__, "", "" )
                    res_time = time_                       
                    # res_data.append( res )
                    # 只读一组数据就 break
                    rflag += 1
                    # res = [] 
                    # rflag = 0     

                if rflag == 5:   
                    log.debug(f" job done, break...", __name__, "", "" )     
                    break  
        return ( res_time, res_data)
        pass

    # 数据加时间值
    @staticmethod
    def read_columns_sets_by_mark(fpath:Path, mark:list, *column 
                        ) ->  Tuple[List[str],List[List]]  :
    # def read_column_1set_by_mark(fpath:str, column:int, *args ) ->  Tuple[  str, List  ]  :
        '''
        @desc: 获取多列数据，且为多套数据，非单套数据
        @return: time:List,  List
        @args : ['Spectrum','DATA']
                default end with "[END]"
        '''
        log.info(f"read_column_1set_by_mark : \
            {fpath} , mark:{mark} , column:{column}", __name__, "", "")

        # 处理mark args[0] = Spectrum
        if len(mark) != 2:
            log.error( f"read_columns_sets_by_mark() wrong args ",__name__, "", "" )
            raise MyException( f"read_columns_sets_by_mark() wrong args " )

        if len(column) == 0:
            log.error(f"read_columns_sets_by_mark(), no column para, pls input column ",__name__, "", "" )
            raise MyException( f"read_columns_sets_by_mark() , pls input column " )

        if len( mark ) == 0:
            mark_1 = '[Spectrum]'
            mark_2 = '[DATA]'

        mark_1 = '['+ mark[0] +']'
        mark_2 = '['+ mark[1] +']'
        res = []
        sn = ReadCal.readFileSNbyIDDevice(fpath)  
        # 预备返回数据结构
        res_data = []  
        res_time = []
        res = []
        columnLen = len(column)         
        for i in range(columnLen):
            # res_time.append( [] ) 
            res.append( [] )     
     
        with fpath.open('r') as f_handle:
            rflag = 0

            for line in f_handle:
                if mark_1 in line   and  rflag == 0:
                    log.debug(f" find {mark_1}", __name__, "", "" )
                    rflag += 1
                    continue

                if mark_2 in line and  rflag > 3 :
                    log.debug(f" find {mark_1}  end ", __name__, "", "" )
                    rflag = 0
                    continue

                # 获取时间
                if rflag == 1 : 
                    data = line.strip('\n').strip(' ').split('=')   
                    if data[0].strip()   == "DateTime" :
                        log.debug(f" find {mark_1} -> DateTime {data[1]} ", __name__, "", "" )
                        time_ =  data[1].strip()     
                        rflag += 1
                        continue

                if rflag == 2 and ( mark_2 in line  ) :
                    log.debug(f" find {mark_2} ->   ", __name__, "", "" )
                    rflag += 1
                    continue

                if rflag == 3 :
                    if mark_2 not in line:
                        # log.debug(f" get data {line}", __name__, "", "" )
                        data = line.strip('\n').strip(' ').split(' ') 
                        # 忽略第一行，第一行0开头，保存积分时间的
                        if  data[0].strip() ==  "0"  :
                            continue   
                        else:
                            # if data[1] != "-NAN" and data[1] != "NAN" and data[1] != "+NAN" :
                            # 处理多列 
                            for i in range(columnLen):
                                # res_time.append( time_ )  
                                res[i].append( data[column[i]] )                                                   
                            continue

                if rflag == 3 and ( mark_2 in line  ) :
                    log.debug(f" find {mark_2} end , line: {line}  ", __name__, "", "" )
                    rflag += 1
                    continue    #需要继续让其找到   

                if rflag == 4:   
                    log.debug(f" job done......", __name__, "", "" )
                    res_time.append( time_ )                        
                    res_data.append( res )
                    rflag += 1
                    # res = [] 
                    # rflag = 0     

                if rflag == 5:   
                    log.debug(f" job done, break...", __name__, "", "" )    
                    rflag = 0 
                    continue

        log.debug(f" res.time len  {len(res_time)} , \
                res.data len  {len(res_data)} ,  ", __name__, "", "" )
          
        return ( res_time, res_data)

    # 数据加时间值
    @staticmethod
    def read_columns_sets_by_mark_callback(fpath:Path, mark:list, callback:Callable, *column )   :
        '''
        @desc: 获取多列数据，且为多套数据，非单套数据
        @return: time:List,  List
        @args : ['Spectrum','DATA']
                default end with "[END]"
        '''
        log.debug(f"read_column_1set_by_mark : \
            {fpath} , mark:{mark} , column:{column}", __name__, "", "")

        # 处理mark args[0] = Spectrum
        if len(mark) != 2:
            log.error( f"read_columns_sets_by_mark() wrong args ",__name__, "", "" )
            raise MyException( f"read_columns_sets_by_mark() wrong args " )

        if len(column) == 0:
            log.error(f"read_columns_sets_by_mark(), no column para, pls input column ",__name__, "", "" )
            raise MyException( f"read_columns_sets_by_mark() , pls input column " )

        if len( mark ) == 0:
            mark_1 = '[Spectrum]'
            mark_2 = '[DATA]'

        mark_1 = '['+ mark[0] +']'
        mark_2 = '['+ mark[1] +']'

        # 预备返回数据结构
        res_data = []  
        res_time = []
        res = []
        sn = ReadCal.readFileSNbyIDDevice(fpath)   

        columnLen = len(column)         
        for i in range(columnLen):
            # res_time.append( [] ) 
            res.append( [] )     
     
        with fpath.open('r') as f_handle:
            rflag = 0

            for line in f_handle:
                if mark_1 in line   and  rflag == 0:
                    log.debug(f" find {mark_1}", __name__, "", "" )
                    rflag += 1
                    continue

                if mark_2 in line and  rflag > 3 :
                    log.debug(f" find {mark_1}  end ", __name__, "", "" )
                    rflag = 0
                    continue

                # 获取时间
                if rflag == 1 : 
                    data = line.strip('\n').strip(' ').split('=')   
                    if data[0].strip()   == "DateTime" :
                        log.debug(f" find {mark_1} -> DateTime {data[1]} ", __name__, "", "" )
                        time_ =  data[1].strip()     
                        rflag += 1
                        continue

                if rflag == 2 and ( mark_2 in line  ) :
                    log.debug(f" find {mark_2} ->   ", __name__, "", "" )
                    rflag += 1
                    continue

                if rflag == 3 :
                    if mark_2 not in line:
                        # log.debug(f" get data {line}", __name__, "", "" )
                        data = line.strip('\n').strip(' ').split(' ') 
                        # 忽略第一行，第一行0开头，保存积分时间的
                        if  data[0].strip() ==  "0"  :
                            continue   
                        else:
                            # if data[1] != "-NAN" and data[1] != "NAN" and data[1] != "+NAN" :
                            # 处理多列 
                            for i in range(columnLen):
                                res[i].append( data[column[i]] )                                                   
                            continue

                if rflag == 3 and ( mark_2 in line  ) :
                    log.debug(f" find {mark_2} end , line: {line}  ", __name__, "", "" )
                    rflag += 1
                    continue    #需要继续让其找到   

                if rflag == 4:   
                    log.debug(f" job done......", __name__, "", "" )
                    # callable  , not return。 每读出一组就回调，不建议。频繁读写硬盘
                    # callback(time_, res)  
                    res_time.append( time_ )                        
                    res_data.append( res )
                    rflag += 1
                    # res = [] 
                    # rflag = 0     

                if rflag == 5:   
                    log.debug(f" job done, break...", __name__, "", "" )                        
                    rflag = 0 
                    continue

                # callable  , not return。 一个文件读完直接处理

            callback(sn, res_time, res_data)  

        log.debug(f" res.time len  {len(res_time)} , \
                res.data len  {len(res_data)} ,  ", __name__, "", "" )
          
        # return ( res_time, res_data)

    @staticmethod
    def readDataIPinfo(fpath:Path, mark:list, callback:Callable ):
        '''
        SAMIP sensor ,Inclination  Pressure, InclX InclY Pressure
        '''
        log.debug(f"readDataIPinfo :  {fpath} , mark:{mark} ,  ", __name__, "", "")

        # 处理mark args[0] = Spectrum
        if len(mark) != 2:
            log.error( f"readDataIPinfo() wrong args ",__name__, "", "" )
            raise MyException( f"readDataIPinfo() wrong args " )

        if len( mark ) == 0:
            mark_1 = '[Spectrum]'
            mark_2 = '[DATA]'

        mark_1 = '['+ mark[0] +']'
 
        # 预备返回数据结构
        res_data = []  
        res_time = []
        res = []
        tags = ['InclX' , 'InclY', 'Pressure']
 
        with fpath.open('r') as f_handle:
            rflag = 0
            for line in f_handle:
                if mark_1 in line   and  rflag == 0:
                    log.debug(f" find {mark_1}", __name__, "", "" )
                    rflag += 1
                    continue
                if mark_1 in line   and  rflag > 5:
                    log.debug(f" find {mark_1} end", __name__, "", "" )
                    rflag = 0
                    continue
                # 获取时间
                if rflag == 1 : 
                    data = line.strip('\n').strip(' ').split('=')   
                    if data[0].strip()   == "DateTime" :
                        log.debug(f" find {mark_1} -> DateTime {data[1]} ", __name__, "", "" )
                        time_ =  data[1].strip()     
                        rflag += 1
                        continue
                if rflag == 2:
                    if  "[" not in line :
                        data = line.strip('\n').strip(' ').split('=')
                        # RAMSES 数据通过MethodName 获得传感器序列号
                        if data[0].strip() in  tags :
                            res.append( data[1].strip(' ') )
                            rflag += 1
                if rflag == 5:
                    res_time.append(time_)
                    res_data.append(res)
                    rflag = 0
                    pass
        callback(res_time,res_data)
        return (res_time,res_data)

    @staticmethod
    def readSamSNFromIni( fpath:Path ):
        """
        @description  : 依据文件路径获得samsn, 兼容 SAM SAMIP传感器   
        """

        with fpath.open('r') as f_handle:
            for line in f_handle:
                # IDDevice   = SAM_85AF
                ln = line.strip('\n').strip(' ').split('=')
                if ln[0].strip() == "IDDevice":
                    if ln[1].strip().split('_')[0] == "SAM":
                        return ln[1].strip().split('_')[1]
        return None
        pass

    @staticmethod
    def readIPCalFromIni( fpath:Path ):
        """
        @description  : 依据文件路径获得IPCal IP标定信息
        """
        ipcal = {}
        with fpath.open('r') as f_handle:
            flag = 0
            for line in f_handle:
                # IDDevice   = SAM_85AF
                ln = line.strip('\n').strip(' ').split('=')

                if flag == 0 and ln[0].strip() == "IDDevice":
                    if ln[1].strip().split('_')[0] == "IP":
                        ipcal["IPSN"] = ln[1].strip().split('_')[1]
                        flag += 1
                        continue

                if flag == 1 and "[" in line:
                    flag += 1
                    continue
                
                if flag == 2:
                    if "[" in line:
                        flag = flag + 1
                        
                    if not "[" in line:
                        line_ = line.strip('\n').strip(' ').split('=')
                        ipcal.update({line_[0].strip():line_[1].strip()}) 

                if flag == 3: 
                    break

        return ipcal

    @staticmethod
    def readSAMCalFromIni( fpath:Path ):
        """
        @description  : 依据文件路径获得SAMCal SAM标定信息
        """
        samcal = {}
        with fpath.open('r') as f_handle:
            flag = 0
            for line in f_handle:
                # IDDevice   = SAM_85AF
                line_ = line.strip('\n').strip(' ').split('=')
                if flag == 0 and line_[0].strip() == "IDDevice":
                    if line_[1].strip().split('_')[0] == "SAM":
                        samcal["SAMSN"] = line_[1].strip().split('_')[1]
                        flag += 1
                        continue

                if flag == 1 and "[" in line:
                    flag += 1
                    continue

                if flag == 2:
                    if "[" in line:
                        flag = flag + 1
                        
                    if not "[" in line:
                        line_ = line.strip('\n').strip(' ').split('=')
                        samcal.update({line_[0].strip():line_[1].strip()}) 

                if flag == 3: 
                    break
        return samcal

    @staticmethod
    def readFileSNbyIDDevice(fpath:Path):
        sn_from_file = ''
        with fpath.open('r') as f_handle:
            rflag = 0
            for line in f_handle:
                if '[Spectrum]' in line and '[END]' not in line and rflag == 0:
                    rflag += 1
                pass
                if rflag == 1:
                    data = line.strip('\n').strip(' ').split('=')
                    # RAMSES 数据通过MethodName 获得传感器序列号
                    if data[0].strip() == "IDDevice":
                        sn_from_file = data[1].strip(' ').split('_')[1]
                        break
        return sn_from_file

    @staticmethod
    def readDatafileSNbyMethodName(fpath:Path):
        '''
        SAMIP SAM的数据文件中, MethodName对应的值都是 SAM_{SAMSN}
        读SAMIP的数据,建议用 {SAMSN}的处理
        '''
        sn_from_file = ''
        with fpath.open('r') as f_handle:
            rflag = 0
            for line in f_handle:
                if '[Spectrum]' in line and '[END]' not in line and rflag == 0:
                    rflag += 1
                pass
                if rflag == 1:
                    data = line.strip('\n').strip(' ').split('=')
                    # RAMSES 数据通过MethodName 获得传感器序列号
                    if data[0].strip() == "MethodName":
                        sn_from_file = data[1].strip(' ').split('_')[1]
                        break
        return sn_from_file


if __name__ == "__main__":
    log.info(f"******** main read *********", __name__, "", "")
    
    # path = PurePath()
    # print(path)
 
    pass

    # def read_data_sn(self,fpath):
    #     with open(fpath, 'r') as f_handle:
    #         rflag = 0
    #         for line in f_handle:
    #             if '[Spectrum]' in line and '[END]' not in line and  rflag == 0:
    #                 rflag += 1
    #             pass
    #             if rflag == 1  : 
    #                 data = line.strip('\n').strip(' ').split('=')   
    #                 if data[0].strip()   == "IDDevice" :
    #                     return data[1].strip(' ').split('_')[1]
    #     pass

    # def read_data1(self,fpath):
    #     with open(fpath, 'r') as f_handle:
    #         rflag = 0
    #         res = []
    #         res_time = []
    #         res_data = []

    #         for line in f_handle:
    #             if '[Spectrum]' in line and '[END]' not in line and  rflag == 0:
    #                 rflag += 1
    #                 pass

    #             if '[Spectrum]' in line and '[END]' in line :
    #                 rflag = 0
    #                 pass

    #             # 获取时间
    #             if rflag == 1 : 
    #                 data = line.strip('\n').strip(' ').split('=')   
    #                 if data[0].strip()   == "DateTime" :
    #                     time_ =  data[1].strip()     
    #                     rflag += 1

    #             if rflag == 2   and ( '[DATA]' in line or '[Data]' in line ) :
    #                 rflag += 1

    #             if rflag == 3 :
    #                 data = line.strip('\n').strip(' ').split(' ') 

    #                 if  int(data[0].strip()) ==  0 :
    #                     pass   
    #                 else:
    #                     # if data[1] != "-NAN" and data[1] != "NAN" and data[1] != "+NAN" :
                            
    #                     res.append( float(data[1] ) )   
    #             if rflag == 3 and '[' in line and ']' in line:
    #                 rflag += 1                
    #             if rflag == 4:   
    #                 # 这个地方  
    #                 # self. __save_absorbance_data( res_time, res, sn  )
    #                 res_time.append( time_ )                        
    #                 res_data.append( res )
    #                 # ?? 可以调用函数直接处理
    #                 # DealResult.deal_one_data(time_,res)
    #                 res = [] 
    #                 rflag = 0            
    #     # return [['data']]
    #     pass


    # def read_data_column( self,fpath, column=1 ):
    #     column_ = 1
    #     if int(column_) :
    #         column_ = int(column_)
    #         pass
    #     else:
    #         pass
        
    #     with open(fpath, 'r') as f_handle:
    #         rflag = 0
    #         res = []
    #         res_time = []
    #         res_data = []

    #         for line in f_handle:
    #             if '[Spectrum]' in line and '[END]' not in line and  rflag == 0:
    #                 rflag += 1
    #             pass

    #             if '[Spectrum]' in line and '[END]'  in line :
    #                 rflag = 0
    #             pass

    #             # 获取时间
    #             if rflag == 1 : 
    #                 data = line.strip('\n').strip(' ').split('=')   
    #                 if data[0].strip()   == "DateTime" :
    #                     time_ =  data[1].strip()     
    #                     rflag += 1

    #             if rflag == 2   and ( '[DATA]' in line or '[Data]' in line ) :
    #                 rflag += 1

    #             if rflag == 3 :
    #                 data = line.strip('\n').strip(' ').split(' ') 

    #                 if  int(data[0].strip()) ==  0 :
    #                     pass   
    #                 else:
    #                     res.append( float(data[column_] ) )   
    #             if rflag == 3 and '[' in line and ']' in line:
    #                 rflag += 1                
    #             if rflag == 4:   
    #                 # 这个地方  
    #                 # self. __save_absorbance_data( res_time, res, sn  )
    #                 # res_time.append( time_ )                        
    #                 # res_data.append( res )
    #                 # ?? 可以调用函数直接处理
    #                 return time_, res
          
    #     # return [['data']]
    #     pass

    # def read_data_wl( self, fpath ):
    #     wl = self.read_data_column( fpath, column=0 )
    #     pass

    # def get_cal_file(self,sn):
    #     sn_ = sn
    #     file_ = {}
    #     if open( os.path.join( CAL_PATH, "SAM_"+sn_+"ini" ),"r"):
    #         file_ = { 
    #                 "type": "SAM",
    #                 "back":"Back_SAM_", 
    #                 "cal":"Cal_SAM_",
    #                 "calaq":"CalAQ_SAM_",
    #                 "ini":"SAM_",  #ini
    #                 "immersion":"immersion_factors_Lu.DAT"
    #                 }
    #         return file_
    #     if open( os.path.join( CAL_PATH, "SAMIP_"+ sn_ +"_ALL.ini" ),"r"):
    #         file_ = { 
    #                 "type": "SAMIP",
    #                 # "back":"Back_SAM_" + sn_ + CAL_EXT_NAME, 
    #                 # "cal":"Cal_SAM_" + sn_ + CAL_EXT_NAME,
    #                 # "calaq":"CalAQ_SAM_" + sn_ + CAL_EXT_NAME,
    #                 "ini":"SAMIP_"+sn+"_ALL.ini",  #ini
    #                 "immersion":"immersion_factors_Lu.DAT"
    #                 }
    #     if file_['ini'] :
    #         samsn = self.read_ini(file_['ini'], "SAMSN")
    #         file_.update("cal", "Cal_SAM_" + samsn + CAL_EXT_NAME)
    #         file_.update("calaq", "CalAQ_SAM_" + samsn + CAL_EXT_NAME)
    #         file_.update("back", "Back_SAM_" + samsn + CAL_EXT_NAME)
    #         return file_
    #     return          

    # def read_cal(self,sn):
    #     # SAM SAMIP 不同 
    #     self.cal_data= {}
    #     # 1. 依据序列号读 ini, 判读是否有文件  SAM_8578.ini   SAMIP_50BB_ALL.ini
    #     self.calfile = self.get_cal_file(sn)   # 其中sam的是错误的
        
    #     # 2 读ini 文件错误
                 
    #     cal_data = {}
    #     for key in self.calfile:
    #         if key == "immersion" :
    #             path_ =  os.path.join( CAL_PATH, self.calfile[key] )
    #             content_ = [self.read_data_column( path_, column=0 )[1]]
    #             content_.append( self.read_data_column( path_, column=0 )[1] )
    #             cal_data.update({key:content_})
                
    #         if key == "cal" :
                
    #             path_ =  os.path.join( CAL_PATH, self.calfile[key] )
    #             content_ = self.read_data_column( path_, column=0 )
    #             cal_data.update({key:content_})
                
    #         if key == "back" :
    #             path_ =  os.path.join( CAL_PATH, self.calfile[key] )
    #             content_ = self.read_data_column( path_, column=0 )
    #             cal_data.update({key:content_})    
                
    #         if key == "calaq" :
    #             path_ =  os.path.join( CAL_PATH, self.calfile[key] )
    #             content_ = self.read_data_column( path_, column=0 )
    #             cal_data.update({key:content_})   
                
    #         if key == "ini" :
    #             cal_data.update({ "samcal" : self.read_ini( os.path.join( CAL_PATH, self.calfile[key] ), "SAMCAL" ) })     
    #             cal_data.update({ "ipcal" : self.read_ini( os.path.join( CAL_PATH, self.calfile[key] ), "IPCAL" ) })     
                        
    #         pass
            
    #     DealResult.deal_cal(cal_data)

    #     pass

    # def read_data(self,fname,lst):
    #     """
    #     @description  : 线程守护读取数据文件 ，循环读取
    #                     考虑一次读取多组,只读一列的情况
    #                     sn ,begin,end, 指定读的列
    #                     返回：[  [[第一组时间],[第二组时间] .... ], [ [第一组data],[第二组data] ...]  ]
    #                     RAMSES 要考虑 : InclX = -6.42, InclY = -6.5625 , Pressure = 6.61394049205538
    #                     需要读一组波长出来
    #     """
    #     log.info(  ":::::::::::: Class : %s -> Function :__read_data " % ( __name__,  ) ) 
    #     log.info(  " __read_data : lst   %s" % ( lst,  ) ) 
    #     sn      = lst[0]
    #     begin   = lst[1]
    #     end     = lst[2] 
    #     column  = lst[3]  
    #     samip_sn= lst[4] 

    #     # 检查 sn--- 不检查sn  ，读数据就 成csv

    #     #  读取一组数据的0列作为波长？？？

    #     # if is_sn_ok :
            
    #     log.info(  "正在处理文件  .... " + fname )
    #     with open(fname, 'r') as f_handle:
    #         time_ = ""
    #         rflag = 0
    #         intlx = 0.0
    #         intly = 0.0
    #         pressure = 0.0
    #         res = []
    #         res_time = []
    #         res_data = []

    #         for line in f_handle:
    #             if  rflag == 0 and '[Spectrum]' in line and '[END]' not in line  :
    #                 rflag += 1
    #             pass

    #             if '[Spectrum]' in line and '[END]'  in line :
    #                 rflag = 0
    #             pass

    #             # 获取时间, InclX, InclY ,Pressure
    #             if rflag == 1  : 
    #                 data = line.strip('\n').strip(' ').split('=')   
    #                 if data[0].strip()   == "DateTime" :
    #                     time_ =  data[1].strip() 
    #                     rflag += 1
    #             #   InclX, InclY ,Pressure
                
    #             if rflag == 2:
    #                 data = line.strip('\n').strip(' ').split('=')   
                
    #                 if data[0].strip()   == "InclX" :
    #                     intlx =  data[1].strip()     

    #                 if data[0].strip()   == "InclY" :
    #                     intly =  data[1].strip()     

    #                 if data[0].strip()   == "Pressure" :
    #                     pressure =  data[1].strip()     


    #             # DATA set 结束添加数据
    #             if rflag == 4 and '[DATA]' in line and '[END]' in line:
    #                 # time.sleep(10)
    #                 res_time.append( [time_,intlx,intly,pressure] )                        
    #                 res_data.append( res )
    #                 res = [] 
    #                 rflag += 1  

    #             if rflag == 3 :
    #                 data = line.strip('\n').strip(' ').split(' ') 
    #                 # print("rflag  %s +++++++++++++++++++++++++++++++++ %s" %(rflag,data) )
    #                 if data[0] != "0"   and data[1] != "-NAN" and data[1] != "NAN" and data[1] != "+NAN" :   #去掉第一行 0， 对应的积分时间 2^（n+1）
    #                     # print("append  %s -- %s" %(data[0],data[1]) )
    #                     res.append( float( data[column-1] ) )  
    #                 if data[0] != "0"  and float(data[0]) > 950 :
    #                     # print("> 950...  %s   %s" %(data[0],data[1]) )                            
    #                     rflag += 1

    #             # 处理data 部分
    #             if rflag == 2 and '[DATA]' in line and '[END]' not in line:
    #                 # print("rflag  %s +++++++++++++++++++++++++++++++++ data+1  " %(rflag,) )
    #                 rflag += 1


    #             if rflag == 5:   
    #                 DealResult.deal_ramses_data(time_, [[intlx,intly,pressure], res])
    #                 # 这个地方  
    #                 # self. __save_absorbance_data( res_time, res, sn  )
    #                 # print("rflag  4 ......................")
    #                 # res_time.append( [time_,intlx,intly,pressure] )      
    #                 # print(" append  time  %s "   %res_time)
    #                 # res_data.append( res )
    #                 res = [] 
    #                 rflag = 0 
    #                 intlx  = 0.0
    #                 intly   =0.0
    #                 pressure =0.0           
    #     # else:
    #     #     return []

    #     log.info(   "已经转换完文件，正在保存... " + fname )

    #     # 分析处理数据，还是返回原始数据 或空 ？？
    #     log.info(  " Function :__read_data    %s - %s " % ( res_time[0] ,res_data[0] ) ) 


    #     # # 光强保存文件路径
    #     # fpath = os.path.join(INTENSITY_PATH, samip_sn + SAVE_EXT_NAME )
    #     # fpath_IP = os.path.join(INTENSITY_PATH, samip_sn + "_IP" + SAVE_EXT_NAME )

    #     # for i in range ( len(res_time) ) :
    #     #     # print("1111111111..............%s   %s " %(res_time[i],res_time[i][0])   )
    #     #     # time.sleep(2)
    #     #     ProcessFile.save_time_list(  str(res_time[i][0]) , res_data[i], TOKEN, fpath )
    #     #     # 不是samip 不保存 IP 文件
    #     #     if sn != samip_sn:
    #     #         ProcessFile.save_time_list(  str(res_time[i][0]) , res_time[i][1:], TOKEN, fpath_IP )


    #     # log.info(   "保存结束 ... " + fname )

    #     # 返回 ok  表示文件处理结束
    #     return   
    #     pass  
    
    # def read_ini(self,fname ,type_):
    #     """
    #     @description  : 三个不同任务， SAM　SN， SAM ATTR, IP ATTR
    #                     分别记为 SAMSN  SAMCAL   IPCAL        
    #     """
    #     log.info( " RAMSES  __read_ini   ->  lst ->   %s " %  type_)  
    #     typ = type_
    #     data = []    
            
    #     if typ == "SAMSN" :

    #         with open(fname, 'r') as f_handle:
    #             for line in f_handle:
    #                 # IDDevice   = SAM_85AF
    #                 ln = line.strip('\n').strip(' ').split('=')   
    #                 if ln[0].strip()   == "IDDevice" :
    #                     if ln[1].strip().split('_')[0] =="SAM" :
    #                         return  ln[1].strip().split('_')[1]
    #         pass   

    #     if typ == "SAMCAL" :
    #         samcal= {}
    #         with open(fname, 'r') as f_handle:
    #             flag = 0
    #             for line in f_handle:
    #                 # IDDevice   = SAM_85AF
    #                 line_ = line.strip('\n').strip(' ').split('=')   
    #                 if flag == 0 and line_[0].strip()   == "IDDevice" :
    #                     if line_[1].strip().split('_')[0] =="SAM" :
    #                         samcal["SAMSN"] = line_[1].strip().split('_')[1]
    #                         flag += 1

    #                 if flag == 2 and "[END]" in line:
    #                     flag += 1  

    #                 if flag == 2:
    #                     # print(" 1  %s    %s  %s" % (fname,line_[0],line_[1]))
    #                     samcal[line_[0]] = line_[1]   

    #                 if flag == 1 and  line_[0].strip() == "[ATTRIBUTES]" :
    #                     flag += 1

    #                 if flag == 2 and "[END]" in line:
    #                     flag += 1                   

    #         return samcal
                    
    #         pass   

    #     if typ == "IPCAL" :
    #         ipcal= {}
    #         with open(fname, 'r') as f_handle:
    #             flag = 0
    #             for line in f_handle:
    #                 # IDDevice   = SAM_85AF
    #                 ln = line.strip('\n').strip(' ').split('=')   

    #                 if flag == 0 and ln[0].strip()   == "IDDevice" :
    #                     if ln[1].strip().split('_')[0] =="IP" :
    #                         ipcal["IPSN"] = ln[1].strip().split('_')[1]
    #                         flag += 1

    #                 if flag == 2 and "[END]" in line:
    #                     flag += 1     

    #                 if flag == 2:
    #                     ipcal[ln[0]] = ln[1]  

    #                 if flag == 1 and  ln[0].strip() == "[ATTRIBUTES]" :
    #                     flag += 1

    #         return ipcal 

    #     return                 
    #     pass

    # def read_raw( self, fname , lst, uiraw):
    #     """
    #     @description  : 三个不同任务， SAM　SN， SAM ATTR, IP ATTR
    #                     对波长进行标定处理
    #                     并存入文件 
    #                     λ(N) = C0s + C1s·N + C2s· N2 + C3s·N3      
    #     """

    #     sep = uiraw["1"]
    #     air_water = int(uiraw["2"])

    #     with open(fname, 'r') as f_handle:
    #         sn = ""
    #         wl = ""
    #         is_samip = 0
    #         # data = data[2:]
    #         # cal_data = lst[sn]
    #         wl_cal = []
    #         cal_data = []            
    #         intg_time = 128
    #         ip_cal = []
    #         cal = []
    #         calaq = []
    #         b0 = []
    #         b1 = []

    #         for line in f_handle:
    #             data = line.split(",")
    #             sn_d = data[0]
                   
    #             if sn_d != sn :
    #                 sn = sn_d                    
    #                 intg_time = int(   data[1] )
    #                 cal_data = lst[sn]
    #                 # 判断sam  SAMIP
    #                 if cal_data[0][0] != cal_data[0][1] :
    #                     is_samip = 1

    #                 wl_cal = cal_data[1]
    #                 ip_cal = cal_data[2]

    #                 if air_water == 1 :
    #                     cal = cal_data[3]
    #                 else:
    #                     cal = cal_data[2]

    #                 b0 = cal_data[4]
    #                 b1 = cal_data[5]                    

    #                 for i in range(1,256):
    #                     temp = float(wl_cal["C0s"])  +             \
    #                             float(wl_cal["C1s"]) * i   +        \
    #                             float(wl_cal["C2s"]) * i * i  +     \
    #                             float(wl_cal["C3s"]) * i *i  *i        
    #                     wl.append(temp)  



    #             # 处理数据
    #             data = data[2:]
    #             intensity = []
    #             Cn        = []

    #             for i in range( 255 ):
    #                 # NAN  为 0
    #                 # if cal[i] = "+NAN":
    #                 #     intensity.append(0)
    #                 # else:
    #                 Mn = data[i] / 65535
    #                 Bn = float( b0[i] ) + float( b1[i] ) * intg_time / 8192   #积分时间比
    #                 Cn.append(Bn - Mn)
                    
    #             offset = 0    # DarkPixelStart  = 237 DarkPixelStop = 254的平均值. 数组中236-254
    #             for i in range( int( wl_cal['DarkPixelStart']) -1 , int( wl_cal['DarkPixelStop'])  ):
    #                 offset = offset + float( Cn[i] )
    #             offset = offset/ ( int( wl_cal['DarkPixelStop']) - int( wl_cal['DarkPixelStart']) +1  )
                
    #             for i in range( 255 ):
    #                 Dn = Cn[i] - offset
    #                 En = 8192 * Dn / intg_time  

    #                 # ????  Sn 来自 cal calaq 文件的部分
    #                 Sn = cal[i]                    
    #                 intensity.append( En / Sn )

    #             # 还需要处理传感器的 IP
    #             if is_samip : 
    #                 # ip 数据过来后的格式
    #                 pass

    #             return  intensity

    #             # 数据分别写到文件
