csvread.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import csv
  2. import re
  3. import datetime
  4. import os
  5. # read_dddd_data:读取弹道导弹轨迹
  6. def read_dddd_data(filedir=".", filename="./dddd1.csv"):
  7. filepath = os.path.join(filedir, filename)
  8. data = []
  9. pattern = r"(\d+\s\w+\s\d+\s\d{2}:\d{2}:\d{2}.\d{3})\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)"
  10. # pattern = r'\s+(\d+\s\w+\s\d+\s\d{2}:\d{2}:\d{2}\.\d{3})\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)'
  11. # 打开CSV文件并读取内容
  12. with open(filepath, newline="") as csvfile:
  13. reader = csv.reader(csvfile)
  14. # 跳过CSV文件的第一行,即标题行
  15. next(reader)
  16. next(reader)
  17. next(reader)
  18. next(reader)
  19. next(reader)
  20. next(reader)
  21. next(reader)
  22. # 逐行读取CSV文件内容并添加到data列表中
  23. for row in reader:
  24. match = re.match(pattern, row[0])
  25. split_data = row[0].split(" ")
  26. if match:
  27. # 将匹配的内容存储到result_array中
  28. data.append(
  29. [match.group(1), match.group(2), match.group(3), match.group(4)]
  30. )
  31. return data
  32. # read_satellite_data:读取卫星轨迹
  33. def read_satellite_data(filedir="", filename="./dddd1.csv"):
  34. filepath = os.path.join(filedir, filename)
  35. data = []
  36. # pattern = r'(\d+\s\w+\s\d+\s\d{2}:\d{2}:\d{2}.\d{3})\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)'
  37. pattern = r"\s+(\d+\s\w+\s\d+\s\d{2}:\d{2}:\d{2}\.\d{3})\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)"
  38. # 打开CSV文件并读取内容
  39. with open(filepath, newline="") as csvfile:
  40. reader = csv.reader(csvfile)
  41. # 跳过CSV文件的第一行,即标题行
  42. next(reader)
  43. next(reader)
  44. next(reader)
  45. next(reader)
  46. next(reader)
  47. next(reader)
  48. next(reader)
  49. # 逐行读取CSV文件内容并添加到data列表中
  50. for row in reader:
  51. match = re.match(pattern, row[0])
  52. split_data = row[0].split(" ")
  53. if match:
  54. # 将匹配的内容存储到result_array中
  55. data.append(
  56. [match.group(1), match.group(2), match.group(3), match.group(4)]
  57. )
  58. return data
  59. # read_satellite_data:返回全部轨迹
  60. def get_position_by_time(
  61. starttime, stoptime, dddd_array, satellite_array, timestep, redunit, satellite
  62. ):
  63. # microseconds = ".000"
  64. # starttime = starttime + microseconds
  65. # stoptime = stoptime + microseconds
  66. start_time_obj = datetime.datetime.strptime(starttime, "%d %B %Y %H:%M:%S.%f")
  67. stop_time_obj = datetime.datetime.strptime(stoptime, "%d %B %Y %H:%M:%S.%f")
  68. # 使用timedelta增加时间
  69. current_time = start_time_obj
  70. index = 0
  71. final_data = []
  72. # while current_time + microseconds != stoptime:
  73. while current_time <= stop_time_obj:
  74. obj = {}
  75. for i in range(len(dddd_array)):
  76. if index < len(dddd_array[i]):
  77. item = dddd_array[i][index]
  78. obj["dateTime"] = item[0]
  79. obj[redunit[i].get("name")] = {
  80. "time": item[0],
  81. "x": item[1],
  82. "y": item[2],
  83. "z": item[3],
  84. }
  85. for j in range(len(satellite_array)):
  86. if index < len(satellite_array[j]):
  87. obj["dateTime"] = item[0]
  88. item = satellite_array[j][index]
  89. obj[satellite[j].get("name")] = {
  90. "time": item[0],
  91. "x": item[1],
  92. "y": item[2],
  93. "z": item[3],
  94. }
  95. final_data.append(obj)
  96. current_time = current_time + datetime.timedelta(seconds=int(timestep))
  97. index = index + 1
  98. return final_data