///// <summary>
        ///// Read HYSPLIT particle data
        ///// </summary>
        ///// <param name="dataExtent">ref data extent</param>
        ///// <returns>discrete data</returns>
        //public double[,] ReadParticleData(ref Global.Extent dataExtent)
        //{
        //    double[,] DiscreteData = new double[3, particleNum];

        //    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
        //    BinaryReader br = new BinaryReader(fs);
        //    byte[] aBytes;
        //    int i, j;
        //    Single lon, lat, alt;
        //    Single minX, maxX, minY, maxY;
        //    minX = 0;
        //    maxX = 0;
        //    minY = 0;
        //    maxY = 0;

        //    br.ReadBytes(28);
        //    for (i = 0; i < particleNum; i++)
        //    {
        //        br.ReadBytes(8);
        //        for (j = 0; j < pollutantNum; j++)
        //        {
        //            br.ReadBytes(4);
        //        }
        //        br.ReadBytes(8);
        //        aBytes = br.ReadBytes(4);
        //        Array.Reverse(aBytes);
        //        lat = BitConverter.ToSingle(aBytes, 0);
        //        aBytes = br.ReadBytes(4);
        //        Array.Reverse(aBytes);
        //        lon = BitConverter.ToSingle(aBytes, 0);
        //        aBytes = br.ReadBytes(4);
        //        Array.Reverse(aBytes);
        //        alt = BitConverter.ToSingle(aBytes, 0);

        //        DiscreteData[0, i] = lon;
        //        DiscreteData[1, i] = lat;
        //        DiscreteData[2, i] = alt;

        //        br.ReadBytes(40);

        //        if (i == 0)
        //        {
        //            minX = lon;
        //            maxX = minX;
        //            minY = lat;
        //            maxY = minY;
        //        }
        //        else
        //        {
        //            if (minX > lon)
        //            {
        //                minX = lon;
        //            }
        //            else if (maxX < lon)
        //            {
        //                maxX = lon;
        //            }
        //            if (minY > lat)
        //            {
        //                minY = lat;
        //            }
        //            else if (maxY < lat)
        //            {
        //                maxY = lat;
        //            }
        //        }
        //    }

        //    dataExtent.minX = minX;
        //    dataExtent.maxX = maxX;
        //    dataExtent.minY = minY;
        //    dataExtent.maxY = maxY;

        //    br.Close();
        //    fs.Close();

        //    return DiscreteData;
        //}

        /// <summary>
        /// Read station data
        /// </summary>
        /// <param name="timeIdx">Time index</param>
        /// <param name="varIdx">Variable index</param>
        /// <param name="levelIdx">Level index</param>
        /// <returns>Station data</returns>
        public StationData GetStationData(int timeIdx, int varIdx, int levelIdx)
        {
            StationData   stationData  = new StationData();
            List <string> stations     = new List <string>();
            int           particleNum  = Parameters[timeIdx][0];
            int           pollutantNum = Parameters[timeIdx][1];
            int           pos          = Parameters[timeIdx][2];

            double[,] DiscreteData = new double[3, particleNum];

            FileStream   fs = new FileStream(this.FileName, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);

            byte[] aBytes;
            int    i, j;
            Single lon, lat, alt;
            Single minX, maxX, minY, maxY;

            minX = 0;
            maxX = 0;
            minY = 0;
            maxY = 0;

            br.BaseStream.Seek(pos, SeekOrigin.Begin);
            br.ReadBytes(28);
            for (i = 0; i < particleNum; i++)
            {
                br.ReadBytes(8);
                for (j = 0; j < pollutantNum; j++)
                {
                    br.ReadBytes(4);
                }
                br.ReadBytes(8);
                aBytes = br.ReadBytes(4);
                Array.Reverse(aBytes);
                lat    = BitConverter.ToSingle(aBytes, 0);
                aBytes = br.ReadBytes(4);
                Array.Reverse(aBytes);
                lon    = BitConverter.ToSingle(aBytes, 0);
                aBytes = br.ReadBytes(4);
                Array.Reverse(aBytes);
                alt = BitConverter.ToSingle(aBytes, 0);

                DiscreteData[0, i] = lon;
                DiscreteData[1, i] = lat;
                DiscreteData[2, i] = alt;
                stations.Add("P" + (i + 1).ToString());

                br.ReadBytes(40);

                if (i == 0)
                {
                    minX = lon;
                    maxX = minX;
                    minY = lat;
                    maxY = minY;
                }
                else
                {
                    if (minX > lon)
                    {
                        minX = lon;
                    }
                    else if (maxX < lon)
                    {
                        maxX = lon;
                    }
                    if (minY > lat)
                    {
                        minY = lat;
                    }
                    else if (maxY < lat)
                    {
                        maxY = lat;
                    }
                }
            }
            MeteoInfoC.Global.Extent dataExtent = new MeteoInfoC.Global.Extent();
            dataExtent.minX = minX;
            dataExtent.maxX = maxX;
            dataExtent.minY = minY;
            dataExtent.maxY = maxY;

            br.Close();
            fs.Close();

            stationData.Data       = DiscreteData;
            stationData.DataExtent = dataExtent;
            stationData.Stations   = stations;

            return(stationData);
        }
示例#2
0
        /// <summary>
        /// Read station data
        /// </summary>
        /// <param name="timeIdx">Time index</param>
        /// <param name="varIdx">Variable index</param>
        /// <param name="levelIdx">Level index</param>
        /// <returns>Station data</returns>
        public StationData GetStationData(int timeIdx, int varIdx, int levelIdx)
        {
            StationData     stationData = new StationData();
            string          aStid;
            int             i;
            double          lon, lat, var;
            string          aLine;
            List <double[]> disDataList = new List <double[]>();

            double[,] DiscreteData;
            double minX, maxX, minY, maxY;

            minX = 0;
            maxX = 0;
            minY = 0;
            maxY = 0;
            string        lonStr, latStr, varStr;
            List <string> stIDList = new List <string>();

            //Get variable position in the record
            int    startIdx = 0, vLen = 1, scaleFactor = 1;
            string undefStr = "";

            switch (varIdx)
            {
            case 0:        //Wind direction
                startIdx = 60;
                vLen     = 3;
                undefStr = "999";
                break;

            case 1:        //Wind speed
                startIdx    = 65;
                vLen        = 4;
                undefStr    = "9999";
                scaleFactor = 10;
                break;

            case 2:        //visibility
                startIdx = 78;
                vLen     = 6;
                undefStr = "999999";
                break;

            case 5:        //Temperature
                startIdx    = 87;
                vLen        = 5;
                undefStr    = "+9999";
                scaleFactor = 10;
                break;

            case 6:        //Dew point temperature
                startIdx    = 93;
                vLen        = 5;
                undefStr    = "+9999";
                scaleFactor = 10;
                break;

            case 7:        //Sea level pressure
                startIdx    = 99;
                vLen        = 5;
                undefStr    = "99999";
                scaleFactor = 10;
                break;
            }

            //Loop
            for (i = 0; i < dataList.Count; i++)
            {
                aLine  = dataList[i];
                aStid  = aLine.Substring(4, 6);
                latStr = aLine.Substring(28, 6);
                if (latStr == "+99999")
                {
                    continue;
                }
                lat    = double.Parse(latStr) / 1000;
                lonStr = aLine.Substring(34, 7);
                if (lonStr == "+999999")
                {
                    continue;
                }
                lon = double.Parse(lonStr) / 1000;

                var = MissingValue;
                switch (varIdx)
                {
                case 3:                        //Present weather
                    if (aLine.Contains("MW1")) //Manual report
                    {
                        startIdx = aLine.IndexOf("MW1") + 3;
                        vLen     = 2;
                        varStr   = aLine.Substring(startIdx, vLen);
                        var      = double.Parse(varStr);
                    }
                    else if (aLine.Contains("AW1"))        //Automatic report
                    {
                        startIdx = aLine.IndexOf("AW1") + 3;
                        vLen     = 2;
                        varStr   = aLine.Substring(startIdx, vLen);
                        try { var = double.Parse(varStr); }
                        catch { break; }
                    }
                    break;

                case 4:        //Cloud cover
                    if (aLine.Contains("GF1"))
                    {
                        startIdx = aLine.IndexOf("GF1") + 3;
                        vLen     = 2;
                        undefStr = "99";
                        varStr   = aLine.Substring(startIdx, vLen);
                        if (varStr != undefStr)
                        {
                            var = double.Parse(varStr);
                        }
                    }
                    break;

                default:
                    varStr = aLine.Substring(startIdx, vLen);
                    if (varStr != undefStr)
                    {
                        var = double.Parse(varStr) / scaleFactor;
                    }

                    break;
                }

                //DiscreteData[0, i] = lon;
                //DiscreteData[1, i] = lat;
                //DiscreteData[2, i] = var;
                disDataList.Add(new double[] { lon, lat, var });
                stIDList.Add(aStid);

                if (i == 0)
                {
                    minX = lon;
                    maxX = minX;
                    minY = lat;
                    maxY = minY;
                }
                else
                {
                    if (minX > lon)
                    {
                        minX = lon;
                    }
                    else if (maxX < lon)
                    {
                        maxX = lon;
                    }
                    if (minY > lat)
                    {
                        minY = lat;
                    }
                    else if (maxY < lat)
                    {
                        maxY = lat;
                    }
                }
            }
            MeteoInfoC.Global.Extent dataExtent = new MeteoInfoC.Global.Extent();
            dataExtent.minX = minX;
            dataExtent.maxX = maxX;
            dataExtent.minY = minY;
            dataExtent.maxY = maxY;

            //Return
            DiscreteData = new double[3, disDataList.Count];
            for (i = 0; i < disDataList.Count; i++)
            {
                DiscreteData[0, i] = disDataList[i][0];
                DiscreteData[1, i] = disDataList[i][1];
                DiscreteData[2, i] = disDataList[i][2];
            }

            stationData.Data       = DiscreteData;
            stationData.DataExtent = dataExtent;
            stationData.Stations   = stIDList;

            return(stationData);
        }