temp_readNC.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using SimulationCommon;
  7. public class GetNCData
  8. {
  9. public tempNCread tempreadNC = new tempNCread();
  10. public double initlatitudes;
  11. public double initlongitudes;
  12. //longitudeArray:经度一维数组;
  13. //latitudeArray:纬度一维数组;
  14. //tempArray:海面温度_24(时间)*7(纬度)*10(经度);
  15. public bool GetData()
  16. {
  17. //中心点坐标应该选取伤员初始位置坐标
  18. string url1 = $"{Util.baseURl}rescue-platform-service/api/v1/dem/getNcInfos?ncPath=D:/data/haiyang/2023_u10_v10_temp.nc&varName=sst&centerLon=" + initlongitudes + "&centerLat=" + initlatitudes;
  19. // Create a GET request to the specified URL
  20. HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url1);
  21. request1.Method = "GET";
  22. // Send request and get response
  23. using (HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse())
  24. {
  25. using (StreamReader reader1 = new StreamReader(response1.GetResponseStream()))
  26. {
  27. string result1 = reader1.ReadToEnd();
  28. //Console.WriteLine("result1:" + result1);
  29. // Parse the JSON string into a JObject
  30. JObject resultObject1 = JObject.Parse(result1);
  31. // Extract the 'data' field from the parsed JObject
  32. JArray dataArray1 = (JArray)resultObject1["data"];
  33. if(dataArray1 == null)
  34. return false;
  35. // Determine the dimensions of the data array
  36. int times5 = dataArray1.Count;
  37. int latitudes5 = ((JArray)dataArray1[0]).Count;
  38. int longitudes5 = ((JArray)dataArray1[0][0]).Count;
  39. // Initialize float[][][] array
  40. float[][][] tempArray = new float[times5][][];
  41. for (int t = 0; t < times5; t++)
  42. {
  43. tempArray[t] = new float[latitudes5][];
  44. for (int lat = 0; lat < latitudes5; lat++)
  45. {
  46. tempArray[t][lat] = new float[longitudes5];
  47. for (int lon = 0; lon < longitudes5; lon++)
  48. {
  49. tempArray[t][lat][lon] = (float)dataArray1[t][lat][lon] - 273.15f;
  50. }
  51. }
  52. }
  53. JArray lonArray = (JArray)resultObject1["data_lon"];
  54. JArray lanArray = (JArray)resultObject1["data_lat"];
  55. //JArray tArray = (JArray)resultObject["data_time"];
  56. int lons = lonArray.Count;
  57. int lans = lanArray.Count;
  58. //int times = tArray.Count;
  59. float[] longitudeArray = new float[lons];
  60. float[] latitudeArray = new float[lans];
  61. //float[] timeArray = new float[times];
  62. for (int i = 0; i < lons; i++)
  63. {
  64. longitudeArray[i] = (float)lonArray[i];
  65. }
  66. for (int j = 0; j < lans; j++)
  67. {
  68. latitudeArray[j] = (float)lanArray[j];
  69. }
  70. /* for (int k = 0; k < lons; k++)
  71. {
  72. timeArray[k] = (float)tArray[k];
  73. }*/
  74. tempreadNC.latitudeArray = latitudeArray;
  75. tempreadNC.longitudeArray = longitudeArray;
  76. tempreadNC.tempArray = tempArray;
  77. }
  78. }
  79. return true;
  80. }
  81. }