day_sta.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <<<<<<< HEAD
  2. <<<<<<< HEAD
  3. __author__ = 'lmstack'
  4. # 每日指标统计函数
  5. import CONFIGURE.PathSetting as PathSetting
  6. import sys
  7. sys.path.append(PathSetting.backend_path)
  8. sys.path.append(PathSetting.middle_path)
  9. import DBManager
  10. import Tools
  11. import DataPreProcess
  12. import IndexStaByPeriod
  13. import Log
  14. import IndexStaByPeriod
  15. import importlib
  16. import datetime
  17. import os
  18. import pandas as pd
  19. import time
  20. importlib.reload(IndexStaByPeriod)
  21. dbManager = DBManager.DBManager()
  22. dataPrePro = DataPreProcess.DataPreProcess()
  23. indexPerSta = IndexStaByPeriod.IndexStaByPeriod()
  24. # log 文件配置
  25. myLog = Log.Mylog('day_sta')
  26. myLog.set_file_hl(file_name=r'D:\Platform\platform\FRONTEND\day_sta\day_sta.log', log_level='info')
  27. myLog.set_stream_hl(log_level='info')
  28. logger = myLog.get_logger()
  29. logger.info(str(os.getpid()))
  30. # sn文件读取
  31. sn_list = list(pd.read_excel('D:\Platform\platform\苏州电池列表.xlsx')['sn'])
  32. sn_list.extend(list(pd.read_excel('D:\Platform\platform\骑享北京6040电池包统计更新20210407.xlsx')['SN号']))
  33. sn = sn_list[0]
  34. # 字段设置及结果文件生成
  35. columns = ['sn', 'time', 'sumDriveTime', 'sumDriveSoc', 'sumDriveAh', 'sumDriveEnergy']
  36. st = datetime.datetime.strptime('00:00:00', '%H:%M:%S')
  37. for i in range(96):
  38. et = st + datetime.timedelta(minutes=15)
  39. columns.append(st.strftime('%H:%M') + '-' + et.strftime('%H:%M'))
  40. st = et
  41. result_path = r'D:\Platform\platform\FRONTEND\day_sta\result.csv'
  42. df_res = pd.DataFrame(columns=columns)
  43. if not os.path.exists(result_path):
  44. df_res.to_csv(result_path, index=False)
  45. # 时间范围设置
  46. start_time = '{} 00:00:00'.format('2020-01-01')
  47. end_time = '{} 00:00:00'.format('2021-06-01')
  48. sta_days = (datetime.datetime.strptime(end_time, '%Y-%m-%d %H:%M:%S') - datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S')).days
  49. count= 0
  50. sn_result = {}
  51. for sn in sn_list[:]:
  52. count += 1
  53. logger.info('{} start, {}/{} '.format(sn, str(count), str(len(sn_list))))
  54. if sn[2:5] == '500':
  55. cap = 40
  56. elif sn[2:5] == '504':
  57. cap = 55
  58. else:
  59. logger.info('{} cap error'.format(sn))
  60. cap = None
  61. continue
  62. sn_result.update({'sn':sn})
  63. logger.info('{} :{} to {} start'.format(sn, str(start_time), str(end_time)))
  64. # 获取数据
  65. df_bms, df_gps = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, gps_switch=True, mode=0)
  66. if df_bms.empty:
  67. continue
  68. # 数据预处理
  69. # 时间完全相同的数据仅保留一行
  70. df_bms_pro, df_gps_pro = dataPrePro.time_filter(df_bms, df_gps)
  71. # bms数据按照电流和状态分段, 然后在状态分段内部,根据时间跳变继续分段(解决段内数据丢失)
  72. df_bms_pro = dataPrePro.data_split_by_status(df_bms_pro)
  73. df_bms_pro = dataPrePro.data_split_by_time(df_bms_pro)
  74. # bms数据将两次充电间的状态合并
  75. df_bms_pro = dataPrePro.combine_drive_stand(df_bms_pro)
  76. # bms 数据计算行车和充电开始前后的静置时间
  77. df_bms_pro = dataPrePro.cal_stand_time(df_bms_pro)
  78. # gps 数据可靠性判断, 并增加里程和速度至gps数据(根据未合并的数据段判断)
  79. df_bms_pro, df_gps_pro, res_record= dataPrePro.gps_data_judge(df_bms_pro, df_gps_pro)
  80. # gps 数据可靠性判断, 并增加里程和速度至gps数据(根据已合并的数据段判断)
  81. df_bms_pro, df_gps_pro, res_record= dataPrePro.data_gps_judge_after_combine(df_bms_pro, df_gps_pro)
  82. for sta_day in range(sta_days):
  83. try:
  84. st_ = datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(days=sta_day)
  85. et_ =datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(days=sta_day+1)
  86. # 按天统计指标
  87. sn_result.update({'time':st_.strftime('%Y-%m-%d')})
  88. df_bms_period = df_bms_pro[(df_bms_pro['时间戳'] > st_.strftime('%Y-%m-%d %H:%M:%S')) & (df_bms_pro['时间戳'] <= et_.strftime('%Y-%m-%d %H:%M:%S'))]
  89. #df_gps_period = df_gps_pro[(df_gps_pro['时间戳'] > st_.strftime('%Y-%m-%d %H:%M:%S')) & (df_gps_pro['时间戳'] <= et_.strftime('%Y-%m-%d %H:%M:%S'))]
  90. sn_result.update({'sumDriveTime':[indexPerSta.drive_time_sta(df_bms_period)]})
  91. sn_result.update({'sumDriveSoc':[indexPerSta.drive_soc_sta(df_bms_period)]})
  92. sn_result.update({'sumDriveAh':[indexPerSta.drive_capacity_sta(cap, df_bms_period)]})
  93. sn_result.update({'sumDriveEnergy':[indexPerSta.drive_energy_sta(cap, df_bms_period)]})
  94. # 每天间隔15分钟 统计一次
  95. for i in range(96):
  96. cur_result = []
  97. st__ = st_ + datetime.timedelta(minutes=15 * i)
  98. et__ = st_ + datetime.timedelta(minutes=15 * (i+1))
  99. df_bms_period = df_bms_pro[(df_bms_pro['时间戳'] > st__.strftime('%Y-%m-%d %H:%M:%S')) & (df_bms_pro['时间戳'] <= et__.strftime('%Y-%m-%d %H:%M:%S'))]
  100. #df_gps_period = df_gps_pro[(df_gps_pro['时间戳'] > st__.strftime('%Y-%m-%d %H:%M:%S')) & (df_gps_pro['时间戳'] <= et__.strftime('%Y-%m-%d %H:%M:%S'))]
  101. cur_result.append(indexPerSta.drive_time_sta(df_bms_period))
  102. cur_result.append(indexPerSta.drive_soc_sta(df_bms_period))
  103. cur_result.append(indexPerSta.drive_capacity_sta(cap, df_bms_period))
  104. cur_result.append(indexPerSta.drive_energy_sta(cap, df_bms_period))
  105. key = st__.strftime('%H:%M') + '-' + et__.strftime('%H:%M')
  106. sn_result.update({key:[cur_result]})
  107. df_cur_res = pd.DataFrame(sn_result)
  108. df_cur_res = df_cur_res[columns]
  109. # 防止写入结果时,结果文件被打开
  110. write_flag = False
  111. while not write_flag:
  112. try:
  113. df_cur_res.to_csv(result_path, mode='a+', index=False, header=False)
  114. except PermissionError as e:
  115. logger.info('{} error:{}'.format(sn, str(e)))
  116. time.sleep(10)
  117. continue
  118. else:
  119. write_flag = True
  120. except Exception as e:
  121. logger.info('{} {}_{} error: {}'.format(sn, st_.strftime('%Y-%m-%d %H:%M:%S'), et_.strftime('%Y-%m-%d %H:%M:%S'), str(e)))
  122. continue
  123. else:
  124. continue
  125. logger.info('{} done, {}/{} '.format(sn, str(count), str(len(sn_list))))
  126. =======
  127. __author__ = 'Wang Liming'
  128. =======
  129. __author__ = 'lmstack'
  130. >>>>>>> 0fdacae7667a378900d95748e2f53901ada95b8c
  131. # 每日指标统计函数
  132. # import CONFIGURE.PathSetting as PathSetting
  133. # import sys
  134. # sys.path.append(PathSetting.backend_path)
  135. # sys.path.append(PathSetting.middle_path)
  136. from LIB.BACKEND import DBManager, Log, DataPreProcess
  137. from LIB.MIDDLE import IndexStaByPeriod, IndexStaBOneCycle
  138. from LIB.BACKEND import DBManager
  139. from LIB.BACKEND import DBManager
  140. from LIB.BACKEND import DBManager
  141. from LIB.BACKEND import DBManager
  142. import importlib
  143. import datetime
  144. import os
  145. import pandas as pd
  146. import time
  147. importlib.reload(IndexStaByPeriod)
  148. dbManager = DBManager.DBManager()
  149. dataPrePro = DataPreProcess.DataPreProcess()
  150. indexPerSta = IndexStaByPeriod.IndexStaByPeriod()
  151. # log 文件配置
  152. myLog = Log.Mylog('day_sta')
  153. myLog.set_file_hl(file_name=r'D:\Platform\platform\FRONTEND\day_sta\day_sta.log', log_level='info')
  154. myLog.set_stream_hl(log_level='info')
  155. logger = myLog.get_logger()
  156. logger.info(str(os.getpid()))
  157. # sn文件读取
  158. sn_list = list(pd.read_excel('D:\Platform\platform\苏州电池列表.xlsx')['sn'])
  159. sn_list.extend(list(pd.read_excel('D:\Platform\platform\骑享北京6040电池包统计更新20210407.xlsx')['SN号']))
  160. sn = sn_list[0]
  161. # 字段设置及结果文件生成
  162. columns = ['sn', 'time', 'sumDriveTime', 'sumDriveSoc', 'sumDriveAh', 'sumDriveEnergy']
  163. st = datetime.datetime.strptime('00:00:00', '%H:%M:%S')
  164. for i in range(96):
  165. et = st + datetime.timedelta(minutes=15)
  166. columns.append(st.strftime('%H:%M') + '-' + et.strftime('%H:%M'))
  167. st = et
  168. result_path = r'D:\Platform\platform\FRONTEND\day_sta\result.csv'
  169. df_res = pd.DataFrame(columns=columns)
  170. if not os.path.exists(result_path):
  171. df_res.to_csv(result_path, index=False)
  172. # 时间范围设置
  173. start_time = '{} 00:00:00'.format('2020-01-01')
  174. end_time = '{} 00:00:00'.format('2021-06-01')
  175. sta_days = (datetime.datetime.strptime(end_time, '%Y-%m-%d %H:%M:%S') - datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S')).days
  176. count= 0
  177. sn_result = {}
  178. for sn in sn_list[:]:
  179. count += 1
  180. logger.info('{} start, {}/{} '.format(sn, str(count), str(len(sn_list))))
  181. if sn[2:5] == '500':
  182. cap = 40
  183. elif sn[2:5] == '504':
  184. cap = 55
  185. else:
  186. logger.info('{} cap error'.format(sn))
  187. cap = None
  188. continue
  189. sn_result.update({'sn':sn})
  190. logger.info('{} :{} to {} start'.format(sn, str(start_time), str(end_time)))
  191. # 获取数据
  192. df_bms, df_gps = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, gps_switch=True, mode=0)
  193. if df_bms.empty:
  194. continue
  195. # 数据预处理
  196. # 时间完全相同的数据仅保留一行
  197. df_bms_pro, df_gps_pro = dataPrePro.time_filter(df_bms, df_gps)
  198. # bms数据按照电流和状态分段, 然后在状态分段内部,根据时间跳变继续分段(解决段内数据丢失)
  199. df_bms_pro = dataPrePro.data_split_by_status(df_bms_pro)
  200. df_bms_pro = dataPrePro.data_split_by_time(df_bms_pro)
  201. # bms数据将两次充电间的状态合并
  202. df_bms_pro = dataPrePro.combine_drive_stand(df_bms_pro)
  203. # bms 数据计算行车和充电开始前后的静置时间
  204. df_bms_pro = dataPrePro.cal_stand_time(df_bms_pro)
  205. # gps 数据可靠性判断, 并增加里程和速度至gps数据(根据未合并的数据段判断)
  206. df_bms_pro, df_gps_pro, res_record= dataPrePro.gps_data_judge(df_bms_pro, df_gps_pro)
  207. # gps 数据可靠性判断, 并增加里程和速度至gps数据(根据已合并的数据段判断)
  208. df_bms_pro, df_gps_pro, res_record= dataPrePro.data_gps_judge_after_combine(df_bms_pro, df_gps_pro)
  209. for sta_day in range(sta_days):
  210. try:
  211. st_ = datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(days=sta_day)
  212. et_ =datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(days=sta_day+1)
  213. # 按天统计指标
  214. sn_result.update({'time':st_.strftime('%Y-%m-%d')})
  215. df_bms_period = df_bms_pro[(df_bms_pro['时间戳'] > st_.strftime('%Y-%m-%d %H:%M:%S')) & (df_bms_pro['时间戳'] <= et_.strftime('%Y-%m-%d %H:%M:%S'))]
  216. #df_gps_period = df_gps_pro[(df_gps_pro['时间戳'] > st_.strftime('%Y-%m-%d %H:%M:%S')) & (df_gps_pro['时间戳'] <= et_.strftime('%Y-%m-%d %H:%M:%S'))]
  217. sn_result.update({'sumDriveTime':[indexPerSta.drive_time_sta(df_bms_period)]})
  218. sn_result.update({'sumDriveSoc':[indexPerSta.drive_soc_sta(df_bms_period)]})
  219. sn_result.update({'sumDriveAh':[indexPerSta.drive_capacity_sta(cap, df_bms_period)]})
  220. sn_result.update({'sumDriveEnergy':[indexPerSta.drive_energy_sta(cap, df_bms_period)]})
  221. # 每天间隔15分钟 统计一次
  222. for i in range(96):
  223. cur_result = []
  224. st__ = st_ + datetime.timedelta(minutes=15 * i)
  225. et__ = st_ + datetime.timedelta(minutes=15 * (i+1))
  226. df_bms_period = df_bms_pro[(df_bms_pro['时间戳'] > st__.strftime('%Y-%m-%d %H:%M:%S')) & (df_bms_pro['时间戳'] <= et__.strftime('%Y-%m-%d %H:%M:%S'))]
  227. #df_gps_period = df_gps_pro[(df_gps_pro['时间戳'] > st__.strftime('%Y-%m-%d %H:%M:%S')) & (df_gps_pro['时间戳'] <= et__.strftime('%Y-%m-%d %H:%M:%S'))]
  228. cur_result.append(indexPerSta.drive_time_sta(df_bms_period))
  229. cur_result.append(indexPerSta.drive_soc_sta(df_bms_period))
  230. cur_result.append(indexPerSta.drive_capacity_sta(cap, df_bms_period))
  231. cur_result.append(indexPerSta.drive_energy_sta(cap, df_bms_period))
  232. key = st__.strftime('%H:%M') + '-' + et__.strftime('%H:%M')
  233. sn_result.update({key:[cur_result]})
  234. df_cur_res = pd.DataFrame(sn_result)
  235. df_cur_res = df_cur_res[columns]
  236. # 防止写入结果时,结果文件被打开
  237. write_flag = False
  238. while not write_flag:
  239. try:
  240. df_cur_res.to_csv(result_path, mode='a+', index=False, header=False)
  241. except PermissionError as e:
  242. logger.info('{} error:{}'.format(sn, str(e)))
  243. time.sleep(10)
  244. continue
  245. else:
  246. write_flag = True
  247. except Exception as e:
  248. logger.info('{} {}_{} error: {}'.format(sn, st_.strftime('%Y-%m-%d %H:%M:%S'), et_.strftime('%Y-%m-%d %H:%M:%S'), str(e)))
  249. continue
  250. else:
  251. continue
  252. logger.info('{} done, {}/{} '.format(sn, str(count), str(len(sn_list))))
  253. >>>>>>> 65a87ae16013552e359df047df19f46fc4e6eb08