temp_readNC.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. //longitudeArray:经度一维数组;
  11. //latitudeArray:纬度一维数组;
  12. //tempArray:海面温度_24(时间)*7(纬度)*10(经度);
  13. public void GetData()
  14. {
  15. //中心点坐标应该选取伤员初始位置坐标
  16. string url1 = $"{Util.baseURl}rescue-platform-service/api/v1/dem/getNcInfos?ncPath=D:/data/haiyang/temp.nc&varName=d2m&centerLon=116.41992593821296&centerLat=40.18801994965735";
  17. // Create a GET request to the specified URL
  18. HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url1);
  19. request1.Method = "GET";
  20. // Send request and get response
  21. using (HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse())
  22. {
  23. using (StreamReader reader1 = new StreamReader(response1.GetResponseStream()))
  24. {
  25. string result1 = reader1.ReadToEnd();
  26. // Parse the JSON string into a JObject
  27. JObject resultObject1 = JObject.Parse(result1);
  28. // Extract the 'data' field from the parsed JObject
  29. JArray dataArray1 = (JArray)resultObject1["data"];
  30. // Determine the dimensions of the data array
  31. int timeSteps = dataArray1.Count;
  32. int latitudes = ((JArray)dataArray1[0]).Count;
  33. int longitudes = ((JArray)dataArray1[0][0]).Count;
  34. // Initialize float[][][] array
  35. float[][][] tempArray = new float[timeSteps][][];
  36. for (int t = 0; t < timeSteps; t++)
  37. {
  38. tempArray[t] = new float[latitudes][];
  39. for (int lat = 0; lat < latitudes; lat++)
  40. {
  41. tempArray[t][lat] = new float[longitudes];
  42. for (int lon = 0; lon < longitudes; lon++)
  43. {
  44. tempArray[t][lat][lon] = (float)dataArray1[t][lat][lon] - 273.15f;
  45. }
  46. }
  47. }
  48. JArray lonArray = (JArray)resultObject1["data_lon"];
  49. JArray lanArray = (JArray)resultObject1["data_lat"];
  50. //JArray tArray = (JArray)resultObject["data_time"];
  51. int lons = lonArray.Count;
  52. int lans = lanArray.Count;
  53. //int times = tArray.Count;
  54. float[] longitudeArray = new float[lons];
  55. float[] latitudeArray = new float[lans];
  56. //float[] timeArray = new float[times];
  57. for (int i = 0; i < lons; i++)
  58. {
  59. longitudeArray[i] = (float)lonArray[i];
  60. }
  61. for (int j = 0; j < lans; j++)
  62. {
  63. latitudeArray[j] = (float)lanArray[j];
  64. }
  65. /* for (int k = 0; k < lons; k++)
  66. {
  67. timeArray[k] = (float)tArray[k];
  68. }*/
  69. tempreadNC.latitudeArray = latitudeArray;
  70. tempreadNC.longitudeArray = longitudeArray;
  71. tempreadNC.tempArray = tempArray;
  72. }
  73. }
  74. }
  75. }