using System;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SimulationCommon;

public class GetNCData
{
    public tempNCread tempreadNC = new tempNCread();
    //longitudeArray:经度一维数组;
    //latitudeArray:纬度一维数组;
    //tempArray:海面温度_24(时间)*7(纬度)*10(经度);
    public void GetData()
    {
        //中心点坐标应该选取伤员初始位置坐标
        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";

        // Create a GET request to the specified URL
        HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url1);
        request1.Method = "GET";

        // Send request and get response
        using (HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse())
        {
            using (StreamReader reader1 = new StreamReader(response1.GetResponseStream()))
            {
                string result1 = reader1.ReadToEnd();

                // Parse the JSON string into a JObject
                JObject resultObject1 = JObject.Parse(result1);

                // Extract the 'data' field from the parsed JObject
                JArray dataArray1 = (JArray)resultObject1["data"];

                // Determine the dimensions of the data array
                int timeSteps = dataArray1.Count;
                int latitudes = ((JArray)dataArray1[0]).Count;
                int longitudes = ((JArray)dataArray1[0][0]).Count;

                // Initialize float[][][] array
                float[][][] tempArray = new float[timeSteps][][];

                for (int t = 0; t < timeSteps; t++)
                {
                    tempArray[t] = new float[latitudes][];

                    for (int lat = 0; lat < latitudes; lat++)
                    {
                        tempArray[t][lat] = new float[longitudes];

                        for (int lon = 0; lon < longitudes; lon++)
                        {
                            tempArray[t][lat][lon] = (float)dataArray1[t][lat][lon] - 273.15f;
                        }
                    }
                }

                JArray lonArray = (JArray)resultObject1["data_lon"];
                JArray lanArray = (JArray)resultObject1["data_lat"];
                //JArray tArray = (JArray)resultObject["data_time"];

                int lons = lonArray.Count;
                int lans = lanArray.Count;
                //int times = tArray.Count;

                float[] longitudeArray = new float[lons];
                float[] latitudeArray = new float[lans];
                //float[] timeArray = new float[times];

                for (int i = 0; i < lons; i++)
                {
                    longitudeArray[i] = (float)lonArray[i];
                }

                for (int j = 0; j < lans; j++)
                {
                    latitudeArray[j] = (float)lanArray[j];
                }

                /* for (int k = 0; k < lons; k++)
                   {
                       timeArray[k] = (float)tArray[k];
                   }*/
                tempreadNC.latitudeArray = latitudeArray;
                tempreadNC.longitudeArray = longitudeArray;
                tempreadNC.tempArray = tempArray;
            }
        }

    }
    

}