SeaSJ.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using OpenCvSharp;
  2. namespace SimulationCommon;
  3. public class WeatherResponse
  4. {
  5. public Wind Wind { get; set; }
  6. }
  7. public class Wind
  8. {
  9. public double Speed { get; set; }
  10. public double Deg { get; set; }
  11. }
  12. public class CurrentResponse
  13. {
  14. public Current Current { get; set; }
  15. }
  16. public class Current
  17. {
  18. public double Speed { get; set; }
  19. public double Direction { get; set; }
  20. }
  21. public class NCread
  22. {
  23. //各数组来源文件'Text_readNC.cs';时间范围:2024-06-04T00:00:00 ... 2024-06-04T23:00:00;
  24. //地点范围:落水点为中心100公里
  25. public float[] longitudeArray = new float[10];//经度一维数组来源自文件'Text_readNC.cs'
  26. public float[] latitudeArray = new float[7];//纬度一维数组来源自文件'Text_readNC.cs'
  27. public float[][][] u10Array ;//风的10米U(向东)分量三维数组来源自文件'Text_readNC.cs'
  28. public float[][][] v10Array;//风的10米V(向北)分量三维数组来源自文件'Text_readNC.cs'
  29. public float[][][] p140208Array;//海洋上空的自由对流速度三维数组来源自文件'Text_readNC.cs'
  30. public float[][][] mwdArray;//平均波向(单位:度;0度表示北方,90度表示东方)三维数组来源自文件'Text_readNC.cs'
  31. }
  32. public class SeaSJ
  33. {
  34. // 生成符合正态分布的随机数
  35. public static double NormalDistributionRandom(double mu, double sigma)
  36. {
  37. Random rand = new Random();
  38. double u1 = 1.0 - rand.NextDouble(); // uniform(0,1] random doubles
  39. double u2 = 1.0 - rand.NextDouble();
  40. double normalRandom = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); // random normal(0,1)
  41. return mu + sigma * normalRandom; // random normal(mean,stdDev^2)
  42. }
  43. // 交付类方法
  44. public static List<double[]> GetDrift(NCread nCread,double[] initialPosition, double dt, double totalTime)
  45. {
  46. // run 获取轨迹的函数
  47. // initialPosition --> 初始位置; dt --> 时间步长; totalTime --> 总时长
  48. List<double[]> trajectory = CalculateDriftTrajectory(nCread,initialPosition, dt, totalTime);
  49. return trajectory;
  50. }
  51. public static List<double[]> CalculateDriftTrajectory(NCread nCread,double[] initialPosition, double dt, double totalTime)
  52. {
  53. int timeSteps = (int)(totalTime / dt);
  54. List<double[]> trajectory = new List<double[]>();
  55. trajectory.Add(new double[] { initialPosition[0], initialPosition[1] });
  56. for (int t = 1; t < timeSteps; t++)
  57. {
  58. double[] currentPos = trajectory[t - 1];
  59. // 动态获取当前位置的风力和洋流数据
  60. double[] windVelocity = GetWindVelocityFromAPI(nCread,currentPos[0], currentPos[1], t);
  61. double[] currentVelocity = GetCurrentVelocityFromAPI(nCread,currentPos[0], currentPos[1], t);
  62. // 计算漂移速度
  63. double[] driftVelocity =
  64. {
  65. currentVelocity[0] + windVelocity[0],
  66. currentVelocity[1] + windVelocity[1]
  67. };
  68. // 更新位置(假设漂移速度是海里每小时,需要转换成经纬度的变化量)
  69. double[] newPosition =
  70. {
  71. currentPos[0] + (driftVelocity[0] * dt / 60.0), // 纬度变化量
  72. currentPos[1] + (driftVelocity[1] * dt / 60.0) // 经度变化量
  73. };
  74. trajectory.Add(newPosition);
  75. }
  76. return trajectory;
  77. }
  78. public static double[] GetWindVelocityFromAPI(NCread windNCread,double latitude, double longitude, int time)
  79. {
  80. float[] longitudeArray = windNCread.longitudeArray;//经度一维数组来源自文件'Text_readNC.cs'
  81. float[] latitudeArray = windNCread.latitudeArray;//纬度一维数组来源自文件'Text_readNC.cs'
  82. float[][][] u10Array = windNCread.u10Array;//风的10米U(向东)分量三维数组来源自文件'Text_readNC.cs'
  83. float[][][] v10Array = windNCread.v10Array;//风的10米V(向北)分量三维数组来源自文件'Text_readNC.cs'
  84. int longitudeNum = 0;
  85. int latitudeNum = 0;
  86. //定义NC文件中读取不到的坐标点海洋数据信息(后续可采用插值法等)
  87. //经度连续化
  88. for (int i = 0; i < 10; i++)
  89. {
  90. if (longitude >= longitudeArray[i] && longitude < longitudeArray[i + 1])
  91. {
  92. longitude = longitudeArray[i];
  93. longitudeNum = i;
  94. }
  95. }
  96. //纬度连续化
  97. for (int i = 0; i < 7; i++)
  98. {
  99. if (latitude >= latitudeArray[i] && latitude < latitudeArray[i + 1])
  100. {
  101. latitude = latitudeArray[i];
  102. latitudeNum = i;
  103. }
  104. }
  105. double windX = (double)u10Array[time][latitudeNum][longitudeNum];
  106. double windY = (double)v10Array[time][latitudeNum][ longitudeNum];
  107. return new double[] { windX, windY };
  108. }
  109. public static double[] GetCurrentVelocityFromAPI(NCread windNCread,double latitude, double longitude, int time)
  110. {
  111. float[] longitudeArray = windNCread.longitudeArray;//经度一维数组来源自文件'Text_readNC.cs'
  112. float[] latitudeArray = windNCread.latitudeArray;//纬度一维数组来源自文件'Text_readNC.cs'
  113. float[][][] u10Array = windNCread.u10Array;//风的10米U(向东)分量三维数组来源自文件'Text_readNC.cs'
  114. float[][][] v10Array = windNCread.v10Array;//风的10米V(向北)分量三维数组来源自文件'Text_readNC.cs'
  115. int longitudeNum = 0;
  116. int latitudeNum = 0;
  117. //定义NC文件中读取不到的坐标点海洋数据信息(后续可采用插值法等)
  118. //经度连续化
  119. for (int i = 0; i < 10; i++)
  120. {
  121. if (longitude >= longitudeArray[i] && longitude < longitudeArray[i + 1])
  122. {
  123. longitude = longitudeArray[i];
  124. longitudeNum = i;
  125. }
  126. }
  127. //纬度连续化
  128. for (int i = 0; i < 7; i++)
  129. {
  130. if (latitude >= latitudeArray[i] && latitude < latitudeArray[i + 1])
  131. {
  132. latitude = latitudeArray[i];
  133. latitudeNum = i;
  134. }
  135. }
  136. double windX = (double)u10Array[time][ latitudeNum][longitudeNum];
  137. double windY = (double)v10Array[time][latitudeNum][ longitudeNum];
  138. return new double[] { windX, windY };
  139. }
  140. // 交付部分方法代码
  141. public static List<double[]> getminEnclosingRect(List<double[]> latLonList)
  142. {
  143. // 转换经纬度为墨卡托投影坐标,并添加到点集合
  144. List<Point2f> pointList = new List<Point2f>();
  145. foreach (var latLon in latLonList)
  146. {
  147. double x = Rectangular_Area_Search_Function.MokatuoLat(latLon[0]);
  148. double y = Rectangular_Area_Search_Function.MokatuoLon(latLon[1]);
  149. pointList.Add(new Point2f((float)x, (float)y));
  150. }
  151. // 获取凸包
  152. Point2f[] convexHull = Rectangular_Area_Search_Function.GetConvexHull(pointList);
  153. // 计算最小包围矩形
  154. List<double[]> minEnclosingRect = Rectangular_Area_Search_Function.MinEnclosingRectangle(convexHull);
  155. // 最小包围矩形顶点经纬度坐标
  156. List<double[]> startPoint = new List<double[]>();
  157. foreach (var minEnclosingRectPoint in minEnclosingRect)
  158. {
  159. double lat = Rectangular_Area_Search_Function.RMokatuoLat(minEnclosingRectPoint[0]);
  160. double lon = Rectangular_Area_Search_Function.RMokatuoLon(minEnclosingRectPoint[1]);
  161. startPoint.Add(new double[] { lat, lon });
  162. }
  163. return startPoint;
  164. }
  165. }