Magnetic_Field.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. from datetime import date, timedelta
  5. def find_max_min(arr):
  6. #print(arr)
  7. if max(arr) > abs(min(arr)):
  8. return arr / max(arr), max(arr / max(arr))
  9. else:
  10. return arr / abs(min(arr)), abs(min(arr / abs(min(arr))))
  11. def elipse(kx, ky, kz):
  12. u = np.linspace(0, 2 * np.pi, 20)
  13. v = np.linspace(0, np.pi, 20)
  14. return kx * np.outer(np.cos(u), np.sin(v)), ky * np.outer(np.sin(u), np.sin(v)), kz * np.outer(np.ones(np.size(u)), np.cos(v))
  15. def Magnetic_3d_picture(x, y, z, begin, end, step, df_B, count, numb):
  16. for i in range(1, step + 1):
  17. # x1, kx = find_max_min(x[begin + (i - 1) * count:begin + i * count])
  18. # y1, ky = find_max_min(y[begin + (i - 1) * count:begin + i * count])
  19. # z1, kz = find_max_min(z[begin + (i - 1) * count:begin + i * count])
  20. x1, kx = find_max_min(x[begin + (i - 1) * count:begin + i * count])
  21. y1, ky = find_max_min(y[begin + (i - 1) * count:begin + i * count])
  22. z1, kz = find_max_min(z[begin + (i - 1) * count:begin + i * count])
  23. x2, y2, z2 = elipse(kx, ky, kz)
  24. with open ("datatime.csv", "a") as file:
  25. pd.set_option('display.max_rows', None)
  26. file.write(f"{x1} {y1} {z1}\n")
  27. fig = plt.figure(figsize=(10, 10))
  28. ax = fig.add_subplot(projection='3d')
  29. ax.scatter(x1, y1, z1, c='black', marker=">", s = 10)
  30. ax.plot_wireframe(x2, y2, z2, linewidth=0.1, color='b')
  31. date_begin = date(2023, 1, 1) + timedelta(days=df_B.iloc[begin + (i - 1) * count]["Day"]-1)
  32. date_end = date(2023, 1, 1) + timedelta(days=df_B.iloc[begin + (i) * count]["Day"] - 1)
  33. plt.title("Days: " + str(date_begin) +" : " + str(date_end) + " Hours: " + str(df_B.iloc[begin + (i - 1) * count]["Hour"]) + " - " + str(df_B.iloc[begin - 1 + (i) * count]["Hour"]), size=20, weight="bold")
  34. #plt.suptitle(str(count)+" Days №"+str(df_B.iloc[begin + (i - 1) * count]["Day"]) + " - " + str(df_B.iloc[begin + (i) * count]["Day"] ))
  35. plt.xlabel('$Bx$', size=32, color='black', labelpad=25)
  36. ax.tick_params(axis="x", labelsize=24)
  37. plt.ylabel('$By$', size=32, color='black', labelpad=25)
  38. ax.tick_params(axis="y", labelsize=24)
  39. ax.set_zlabel('$Bz$', size=32, labelpad=25)
  40. ax.tick_params(axis="z", labelsize=24)
  41. #ax.view_init(30, -126) # изменение угла 3d графика
  42. #plt.savefig(f"/home/kmg/Pictures/Graphs/diagram_{numb}.png", dpi=300)
  43. plt.show()
  44. def Read_B_Table(file):
  45. #arr = ["Year", "Day", "Hour", "Minute", "Sec", "Bx_GSE", "By_GSE", "Bz_GSE"]
  46. arr = ["Year", "Day", "Hour", "Minute", "Bx_GSE", "By_GSE", "Bz_GSE"]
  47. df = pd.read_table(file, names=arr, sep="\s+")
  48. for i in arr:
  49. df.loc[(df[i] > 9999)] = 0
  50. #print(df.shape[0])
  51. return df
  52. def SMA(df1, df2, df3, window):
  53. rolling_mean_1 = pd.Series(df1).rolling(window=window).mean()
  54. rolling_mean_2 = pd.Series(df2).rolling(window=window).mean()
  55. rolling_mean_3 = pd.Series(df3).rolling(window=window).mean()
  56. fig = plt.figure(figsize=(12,12), dpi=100)
  57. ax1=fig.add_subplot(311)
  58. ax1.plot(df1, label='AMD 50 Day SMA', color='black')
  59. ax1.plot(rolling_mean_1, label='AMD 50 Day SMA', color='orange')
  60. ax1.plot([0 for i in range(len(df1))], color = 'magenta')
  61. plt.xlabel("Bx")
  62. ax2 = fig.add_subplot(312)
  63. ax2.plot(df2, label='AMD 50 Day SMA', color='black')
  64. ax2.plot(rolling_mean_2, label='AMD 50 Day SMA', color='orange')
  65. ax2.plot([0 for i in range(len(df2))], color='magenta')
  66. plt.xlabel("By")
  67. ax3 = fig.add_subplot(313)
  68. ax3.plot(df3, label='AMD 50 Day SMA', color='black')
  69. ax3.plot(rolling_mean_3, label='AMD 50 Day SMA', color='orange')
  70. ax3.plot([0 for i in range(len(df3))], color='magenta')
  71. plt.xlabel("Bz")
  72. plt.show()
  73. nuli = []
  74. for i, j in enumerate(rolling_mean_1): # находит переходы через ноль
  75. if rolling_mean_1[i] < 0 and rolling_mean_1[i - 1] > 0:
  76. print("из - в +",i, j)
  77. nuli.append(i)
  78. elif rolling_mean_1[i] > 0 and rolling_mean_1[i-1] < 0:
  79. print("из + в -",i, j)
  80. nuli.append(i)
  81. return rolling_mean_1, rolling_mean_2, rolling_mean_3
  82. def plot_SMA (smaX,smaY, smaZ, begin, end):
  83. fig = plt.figure(figsize=(10, 10))
  84. ax = fig.add_subplot(projection='3d')
  85. arr1 = []
  86. arr2 = []
  87. for i in range(begin, end):
  88. if smaX[i] > 0:
  89. arr1.append(i)
  90. else:
  91. arr2.append(i)
  92. ax.scatter(smaX[arr1], smaY[arr1], smaZ[arr1], c='black', marker=">", s=10)
  93. ax.scatter(smaX[arr2], smaY[arr2], smaZ[arr2], c='red', marker="<", s=10)
  94. plt.xlabel('$Bx$', size=24, color='black', labelpad=20)
  95. plt.ylabel('$By$', size=24, color='black', labelpad=20)
  96. ax.set_zlabel('$Bz$', size=24, labelpad=20)
  97. plt.show()
  98. return arr1, arr2
  99. def Magnetic_3d_picture_sma(x, y, z, begin, end, step, df_B, count, numb, arr1, arr2):
  100. for i in range(1, step + 1):
  101. # x1, kx = find_max_min(x[begin + (i - 1) * count:begin + i * count])
  102. # y1, ky = find_max_min(y[begin + (i - 1) * count:begin + i * count])
  103. # z1, kz = find_max_min(z[begin + (i - 1) * count:begin + i * count])
  104. x1, kx = find_max_min(x[begin + (i - 1) * count:begin + i * count])
  105. y1, ky = find_max_min(y[begin + (i - 1) * count:begin + i * count])
  106. z1, kz = find_max_min(z[begin + (i - 1) * count:begin + i * count])
  107. x2, y2, z2 = elipse(kx, ky, kz)
  108. with open ("datatime.csv", "a") as file:
  109. pd.set_option('display.max_rows', None)
  110. file.write(f"{x1} {y1} {z1}\n")
  111. fig = plt.figure(figsize=(10, 10))
  112. ax = fig.add_subplot(projection='3d')
  113. ax.scatter(x1[arr1], y1[arr1], z1[arr1], c='black', marker=">", s=10)
  114. ax.scatter(x1[arr2], y1[arr2], z1[arr2], c='red', marker=">", s=10)
  115. ax.plot_wireframe(x2, y2, z2, linewidth=0.1, color='b')
  116. date_begin = date(2023, 1, 1) + timedelta(days=df_B.iloc[begin + (i - 1) * count]["Day"]-1)
  117. date_end = date(2023, 1, 1) + timedelta(days=df_B.iloc[begin + (i) * count]["Day"] - 1)
  118. plt.title("Days: " + str(date_begin) +" : " + str(date_end) + " Hours: " + str(df_B.iloc[begin + (i - 1) * count]["Hour"]) + " - " + str(df_B.iloc[begin - 1 + (i) * count]["Hour"]), size=20, weight="bold")
  119. #plt.suptitle(str(count)+" Days №"+str(df_B.iloc[begin + (i - 1) * count]["Day"]) + " - " + str(df_B.iloc[begin + (i) * count]["Day"] ))
  120. plt.xlabel('$Bx$', size=32, color='black', labelpad=25)
  121. ax.tick_params(axis="x", labelsize=24)
  122. plt.ylabel('$By$', size=32, color='black', labelpad=25)
  123. ax.tick_params(axis="y", labelsize=24)
  124. ax.set_zlabel('$Bz$', size=32, labelpad=25)
  125. ax.tick_params(axis="z", labelsize=24)
  126. #ax.view_init(30, -126) # изменение угла 3d графика
  127. #plt.savefig(f"/home/kmg/Pictures/Graphs/diagram_{numb}.png", dpi=300)
  128. plt.show()
  129. def B_Diagrams(df, begin, end, numb):
  130. X = df["Bx_GSE"]
  131. Y = df["By_GSE"]
  132. Z = df["Bz_GSE"]
  133. count = end - begin
  134. #count = 5000
  135. # #720 # шаг по 3 часа для 15сек
  136. step = (end - begin) // count
  137. window = 7000 # окно для скользящей средней
  138. smaX, smaY, smaZ=SMA(X, Y, Z, window)
  139. arr1, arr2 = plot_SMA(smaX, smaY, smaZ, begin, end)
  140. Magnetic_3d_picture_sma(X, Y, Z, begin, end, step, df, count, numb, arr1, arr2)
  141. Magnetic_3d_picture(X, Y, Z, begin, end, step, df, count, numb)
  142. #Magnetic_3d_picture(smaX, smaY, smaZ, begin, end, step, df, count, numb)
  143. # combination_rotation_animation(X[begin:end], Y[begin:end], Z[begin:end], begin, step, 720)
  144. # Anime(X, Y, Z, step)
  145. if __name__ == "__main__":
  146. df_test = Read_B_Table("sun.lst")
  147. print()