SeaSJ.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. public float[][][] hmaxArray;
  32. }
  33. public class SeaSJ
  34. {
  35. // 生成符合正态分布的随机数
  36. public static double NormalDistributionRandom(double mu, double sigma)
  37. {
  38. Random rand = new Random();
  39. double u1 = 1.0 - rand.NextDouble(); // uniform(0,1] random doubles
  40. double u2 = 1.0 - rand.NextDouble();
  41. double normalRandom = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); // random normal(0,1)
  42. return mu + sigma * normalRandom; // random normal(mean,stdDev^2)
  43. }
  44. // 交付类方法
  45. public static List<double[]> GetDrift(NCread nCread,double[] initialPosition, double dt, double totalTime)
  46. {
  47. // run 获取轨迹的函数
  48. // initialPosition --> 初始位置; dt --> 时间步长; totalTime --> 总时长
  49. List<double[]> trajectory = CalculateDriftTrajectory(nCread,initialPosition, dt, totalTime);
  50. return trajectory;
  51. }
  52. public static List<double[]> CalculateDriftTrajectory(NCread nCread,double[] initialPosition, double dt, double totalTime)
  53. {
  54. int timeSteps = (int)(totalTime / dt);
  55. List<double[]> trajectory = new List<double[]>();
  56. trajectory.Add(new double[] { initialPosition[0], initialPosition[1] });
  57. for (int t = 1; t < timeSteps; t++)
  58. {
  59. double[] currentPos = trajectory[t - 1];
  60. // 动态获取当前位置的风力和洋流数据
  61. double[] windVelocity = GetWindVelocityFromAPI(nCread,currentPos[0], currentPos[1], t);
  62. double[] currentVelocity = GetCurrentVelocityFromAPI(nCread,currentPos[0], currentPos[1], t);
  63. // 计算漂移速度(m/s)
  64. double[] driftVelocity = {
  65. currentVelocity[0] + windVelocity[0],
  66. currentVelocity[1] + windVelocity[1]
  67. };
  68. // 更新位置(漂移速度单位是m/s,需要转换成经纬度的变化量)
  69. double[] newPosition = {
  70. currentPos[0] + (driftVelocity[0] * dt * 3600) / ((6371000 * Math.Cos(currentPos[0] * Math.PI / 180)) * 180 / Math.PI), // 纬度变化量
  71. currentPos[1] + (driftVelocity[1] * dt * 3600) / ((6371000 * Math.Cos(currentPos[1] * Math.PI / 180)) * 180 / Math.PI) // 经度变化量
  72. };
  73. trajectory.Add(newPosition);
  74. }
  75. return trajectory;//(纬度,经度)
  76. }
  77. public static double[] GetWindVelocityFromAPI(NCread windNCread,double latitude, double longitude, double temptime)
  78. {
  79. float[] longitudeArray = windNCread.longitudeArray;//经度一维数组来源自文件'Text_readNC.cs'
  80. float[] latitudeArray = windNCread.latitudeArray;//纬度一维数组来源自文件'Text_readNC.cs'
  81. float[][][] u10Array = windNCread.u10Array;//风的10米U(向东)分量三维数组来源自文件'Text_readNC.cs'
  82. float[][][] v10Array = windNCread.v10Array;//风的10米V(向北)分量三维数组来源自文件'Text_readNC.cs'
  83. int longitudeNum = 0;
  84. int latitudeNum = 0;
  85. int temptimeNum = 0;
  86. //定义NC文件中读取不到的坐标点海洋数据信息(后续可采用插值法等)
  87. //经度连续化
  88. for (int i = 0; i < 9; 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 < 6; i++)
  98. {
  99. if (latitude >= latitudeArray[i] && latitude < latitudeArray[i + 1])
  100. {
  101. latitude = latitudeArray[i];
  102. latitudeNum = i;
  103. }
  104. }
  105. //时间连续化
  106. for (int i = 0; i < 23 ; i ++)
  107. {
  108. if (temptime >= 3600 * i && temptime < (3600 * (i + 1)))
  109. {
  110. temptimeNum = i;
  111. }
  112. }
  113. double windX = (double)u10Array[temptimeNum][latitudeNum][longitudeNum];
  114. double windY = (double)v10Array[temptimeNum][latitudeNum][ longitudeNum];
  115. return new double[] { windX, windY };
  116. }
  117. public static double[] GetCurrentVelocityFromAPI(NCread CurrentNCread,double latitude, double longitude, double temptime)
  118. {
  119. float[] longitudeArray = CurrentNCread.longitudeArray;//经度一维数组来源自文件'Text_readNC.cs'
  120. float[] latitudeArray = CurrentNCread.latitudeArray;//纬度一维数组来源自文件'Text_readNC.cs'
  121. float[][][] u10Array = CurrentNCread.u10Array;//风的10米U(向东)分量三维数组来源自文件'Text_readNC.cs'
  122. float[][][] v10Array = CurrentNCread.v10Array;//风的10米V(向北)分量三维数组来源自文件'Text_readNC.cs'
  123. float[][][] p140208Array = CurrentNCread.p140208Array;//海洋上空的自由对流速度三维数组来源自文件'Text_readNC.cs'
  124. float[][][] mwdArray = CurrentNCread.mwdArray;//平均波向(单位:度;0度表示北方,90度表示东方)三维数组来源自文件'Text_readNC.cs'
  125. int longitudeNum = 0;
  126. int latitudeNum = 0;
  127. int temptimeNum = 0;
  128. //定义NC文件中读取不到的坐标点海洋数据信息(后续可采用插值法等)
  129. //经度连续化
  130. for (int i = 0; i < 9; i++)
  131. {
  132. if (longitude >= longitudeArray[i] && longitude < longitudeArray[i + 1])
  133. {
  134. longitude = longitudeArray[i];
  135. longitudeNum = i;
  136. }
  137. }
  138. //纬度连续化
  139. for (int i = 0; i < 6; i++)
  140. {
  141. if (latitude >= latitudeArray[i] && latitude < latitudeArray[i + 1])
  142. {
  143. latitude = latitudeArray[i];
  144. latitudeNum = i;
  145. }
  146. }
  147. //时间连续化
  148. for (int i = 0; i < 23; i++)
  149. {
  150. if (temptime >= 3600 * 1 && temptime < 3600 * (i + 1))
  151. {
  152. temptimeNum = i;
  153. }
  154. }
  155. double currentSpeed = (double)p140208Array[temptimeNum][latitudeNum][ longitudeNum];
  156. double currentDirection = (double)mwdArray[temptimeNum][latitudeNum][ longitudeNum];
  157. double currentDirectionInRadians = currentDirection * (Math.PI / 180);
  158. double currentX = currentSpeed * Math.Cos(currentDirectionInRadians);
  159. double currentY = currentSpeed * Math.Sin(currentDirectionInRadians);
  160. return new double[] { currentX, currentY };
  161. }
  162. //海浪高度获取
  163. public static double GetWaveHeightFromAPI(NCread WaveNCread,double latitude, double longitude, double temptime)//temptime:仿真时间(秒)
  164. {
  165. float[] longitudeArray = WaveNCread.longitudeArray;//经度一维数组来源自文件'Text_readNC.cs'
  166. float[] latitudeArray = WaveNCread.latitudeArray;//纬度一维数组来源自文件'Text_readNC.cs'
  167. float[][][] hmaxArray = WaveNCread.hmaxArray;//风的10米U(向东)分量三维数组来源自文件'Text_readNC.cs'
  168. int longitudeNum = 0;
  169. int latitudeNum = 0;
  170. int temptimeNum = 0;
  171. //定义NC文件中读取不到的坐标点海洋数据信息(后续可采用插值法等)
  172. //经度连续化
  173. for (int i = 0; i < 9; i++)
  174. {
  175. if (longitude >= longitudeArray[i] && longitude < longitudeArray[i + 1])
  176. {
  177. longitude = longitudeArray[i];
  178. longitudeNum = i;
  179. }
  180. }
  181. //纬度连续化
  182. for (int i = 0; i < 6; i++)
  183. {
  184. if (latitude >= latitudeArray[i] && latitude < latitudeArray[i + 1])
  185. {
  186. latitude = latitudeArray[i];
  187. latitudeNum = i;
  188. }
  189. }
  190. //时间连续化
  191. for (int i = 0; i < 23 ; i ++)
  192. {
  193. if (temptime >= 3600 * i && temptime < (3600 * (i + 1)))
  194. {
  195. temptimeNum = i;
  196. }
  197. }
  198. double WaveHeight = (double)hmaxArray[temptimeNum][ latitudeNum][ longitudeNum];
  199. return WaveHeight;
  200. }
  201. // 交付部分方法代码
  202. public static List<double[]> getminEnclosingRect(List<double[]> latLonList)
  203. {
  204. // 转换经纬度为墨卡托投影坐标,并添加到点集合
  205. List<Point2f> pointList = new List<Point2f>();
  206. foreach (var latLon in latLonList)
  207. {
  208. double x = Rectangular_Area_Search_Function.MokatuoLat(latLon[1]);
  209. double y = Rectangular_Area_Search_Function.MokatuoLon(latLon[0]);
  210. pointList.Add(new Point2f((float)y, (float)x));
  211. }
  212. // 获取凸包
  213. Point2f[] convexHull = Rectangular_Area_Search_Function.GetConvexHull(pointList);
  214. // 计算最小包围矩形
  215. List<double[]> minEnclosingRect = Rectangular_Area_Search_Function.MinEnclosingRectangle(convexHull);
  216. // 最小包围矩形顶点经纬度坐标
  217. List<double[]> startPoint = new List<double[]>();
  218. foreach (var minEnclosingRectPoint in minEnclosingRect)
  219. {
  220. double lat = Rectangular_Area_Search_Function.RMokatuoLat(minEnclosingRectPoint[0]);
  221. double lon = Rectangular_Area_Search_Function.RMokatuoLon(minEnclosingRectPoint[1]);
  222. startPoint.Add(new double[] { lat, lon });
  223. }
  224. return startPoint;
  225. }
  226. }