コード例 #1
0
ファイル: PMDConverter.cs プロジェクト: windripple/popolo
        /// <summary>地点情報読み込み処理</summary>
        /// <param name="pwData">PWeatherDataオブジェクト</param>
        /// <param name="bStrm">読み取りStream</param>
        private static void getHourlyData(ref WeatherDataTable pwData, BufferedStream bStrm)
        {
            LocationInformation locationInfo = pwData.Location;
            byte[] buffer;
            //年月日情報
            buffer = new byte[8];
            //最終行の場合は終了
            bStrm.Read(buffer, 0, 8);
            string dTime = System.Text.Encoding.GetEncoding(932).GetString(buffer);
            int year = int.Parse(dTime.Substring(0, 4));
            int month = int.Parse(dTime.Substring(4, 2));
            int day = int.Parse(dTime.Substring(6, 2));
            DateTime cTime = new DateTime(year, month, day, 1, 0, 0);

            //1時間データまでシーク
            bStrm.Seek(2, SeekOrigin.Current);

            //24時間データを取得
            buffer = new byte[56];
            WeatherData wd = new WeatherData();
            wd.Source = WeatherData.DataSource.CalculatedValue;
            bool sunRise = false;
            bool hasDR = false;
            for (int i = 0; i < 24; i++)
            {
                WeatherRecord wr = new WeatherRecord();
                wr.DataDTime = cTime;

                //データ取得
                bStrm.Read(buffer, 0, 56);
                string data = System.Text.Encoding.GetEncoding(932).GetString(buffer);

                //気圧[Pa]
                double atm = wd.Value = double.Parse(data.Substring(0, 5)) * 0.01d;
                WeatherData.DataSource atmSource = getDataSource(data.Substring(5, 1));
                wd.Source = atmSource;
                wr.SetData(WeatherRecord.RecordType.AtmosphericPressure, wd);

                //乾球温度[C]
                double dbt = wd.Value = double.Parse(data.Substring(12, 4)) * 0.1;
                WeatherData.DataSource dbtSource = getDataSource(data.Substring(16, 1));
                wd.Source = dbtSource;
                wr.SetData(WeatherRecord.RecordType.DryBulbTemperature, wd);

                //相対湿度[%]
                double rhd = wd.Value = double.Parse(data.Substring(21, 3));
                WeatherData.DataSource rhdSource = getDataSource(data.Substring(24, 1));
                wd.Source = rhdSource;
                wr.SetData(WeatherRecord.RecordType.RelativeHumidity, wd);

                //風向[degree]
                wd.Value = getWindowDirection(int.Parse(data.Substring(25, 2)));
                wd.Source = getDataSource(data.Substring(27, 1));
                wr.SetData(WeatherRecord.RecordType.WindDirection, wd);

                //風速[m/s]
                wd.Value = double.Parse(data.Substring(28, 3)) * 0.1;
                wd.Source = getDataSource(data.Substring(31, 1));
                wr.SetData(WeatherRecord.RecordType.WindSpeed, wd);

                //雲量10分比[-]
                wd.Value = double.Parse(data.Substring(32, 2)) * 0.1;
                wd.Source = getDataSource(data.Substring(34, 1));
                wr.SetData(WeatherRecord.RecordType.TotalSkyCover, wd);

                //天気記号
                wd.Value = double.Parse(data.Substring(35, 2));
                wd.Source = getDataSource(data.Substring(37, 1));
                wr.SetData(WeatherRecord.RecordType.WeatherCode, wd);

                //露点温度[C]
                wd.Value = double.Parse(data.Substring(38, 4)) * 0.1;
                wd.Source = getDataSource(data.Substring(42, 1));
                wr.SetData(WeatherRecord.RecordType.DewPointTemperature, wd);

                //全天日射量[W/m2]
                double ghRad = double.Parse(data.Substring(47, 3)) * 277.7777778 * 0.01;
                wd.Value = ghRad;
                WeatherData.DataSource ghRadSource = getDataSource(data.Substring(50, 1));
                wd.Source = ghRadSource;
                wr.SetData(WeatherRecord.RecordType.GlobalHorizontalRadiation, wd);

                //降水量[mm]
                wd.Value = double.Parse(data.Substring(51, 4)) * 0.1;
                wd.Source = getDataSource(data.Substring(55, 1));
                wr.SetData(WeatherRecord.RecordType.PrecipitationLevel, wd);

                //推定可能なデータを計算して埋める********************************************************
                //絶対湿度[kg/kg(DA)]
                if (dbtSource != WeatherData.DataSource.MissingValue && rhdSource != WeatherData.DataSource.MissingValue)
                {
                    wd.Value = MoistAir.GetAirStateFromDBRH(dbt, rhd, MoistAir.Property.HumidityRatio, atm);
                    wd.Source = WeatherData.DataSource.CalculatedValue;
                    wr.SetData(WeatherRecord.RecordType.HumidityRatio, wd);
                }

                //直散分離
                //太陽の存在確認
                bool sr = (0 < Sun.GetSunAltitude(locationInfo.Latitude, locationInfo.Longitude, 135d, cTime));

                //直散分離
                double dsRad, dhRad;
                //日出・日没調整
                if (!sunRise && sr) Sun.EstimateDiffuseAndDirectNormalRadiation(ghRad, locationInfo.Latitude, locationInfo.Longitude, 135d, cTime, out dsRad, out dhRad);
                else if (sunRise && !sr) Sun.EstimateDiffuseAndDirectNormalRadiation(ghRad, locationInfo.Latitude, locationInfo.Longitude, 135d, cTime.AddHours(-1), out dsRad, out dhRad);
                else Sun.EstimateDiffuseAndDirectNormalRadiation(ghRad, locationInfo.Latitude, locationInfo.Longitude, 135d, cTime.AddHours(-0.5), out dsRad, out dhRad);
                sunRise = sr;

                //24h「観測しない」が続いた場合は欠測扱い
                hasDR = (ghRadSource != WeatherData.DataSource.MissingValue || hasDR);
                if (i != 23 || hasDR)
                {
                    //直達日射量[W/m2]
                    wd.Value = dsRad;
                    wd.Source = WeatherData.DataSource.PredictedValue;
                    wr.SetData(WeatherRecord.RecordType.DirectNormalRadiation, wd);
                    //天空日射量[W/m2]
                    wd.Value = dhRad;
                    wd.Source = WeatherData.DataSource.PredictedValue;
                    wr.SetData(WeatherRecord.RecordType.DiffuseHorizontalRadiation, wd);
                }

                //空白データを欠測データとして埋める******************************************************
                wr.FillMissingData();

                //1時間進める
                cTime = cTime.AddHours(1);

                //気象レコード追加
                pwData.AddWeatherRecord(wr);
            }
        }
コード例 #2
0
ファイル: TMY1Converter.cs プロジェクト: windripple/popolo
        /// <summary>ファイルを元にWeatherDataTableを構成する</summary>
        /// <param name="filePath">読み取りファイルのパス</param>
        /// <param name="success">読み取り成功の真偽</param>
        /// <returns>構成されたPWeatherDataオブジェクト</returns>
        public static WeatherDataTable ToPWeatherData(string filePath, out bool success)
        {
            success = false;

            //読み出しファイルの存在確認
            if (!File.Exists(filePath)) return null;

            WeatherDataTable wdTable = new WeatherDataTable();

            using(StreamReader sReader = new StreamReader(filePath))
            {
                string str;
                bool firstLine = true;
                while ((str = sReader.ReadLine()) != null)
                {
                    //初回は地点情報を設定
                    if (firstLine)
                    {
                        LocationInformation lInfo = new LocationInformation();
                        lInfo.ID = int.Parse(str.Substring(0, 5));
                        lInfo.Name = lInfo.EnglishName = "TMY1" + lInfo.ID.ToString("F0");
                        wdTable.Location = lInfo;

                        firstLine = false;
                    }

                    WeatherRecord wRecord = new WeatherRecord();
                    WeatherData wData;

                    //日時
                    int year = int.Parse(str.Substring(5,2));
                    if (year < 20) year += 2000;
                    else year += 1900;
                    int month = int.Parse(str.Substring(7, 2));
                    int day = int.Parse(str.Substring(9, 2));
                    int hour = int.Parse(str.Substring(11, 2)) - 1;
                    int minute = int.Parse(str.Substring(13, 2));

                    wRecord.DataDTime = new DateTime(year, month, day, hour, minute, 0);

                    //直達日射[W/m2]
                    if (str.Substring(24, 4) == "9999") wData = new WeatherData(0d, getDSource1(str.Substring(23, 1)), -1);
                    else wData = new WeatherData(double.Parse(str.Substring(24, 4)) / 3.6d, getDSource1(str.Substring(23, 1)), -1);
                    wRecord.SetData(WeatherRecord.RecordType.DirectNormalRadiation, wData);

                    //水平面天空日射[W/m2]
                    if (str.Substring(29, 4) == "9999") wData = new WeatherData(0d, getDSource1(str.Substring(28, 1)), -1);
                    else wData = new WeatherData(double.Parse(str.Substring(29, 4)) / 3.6d, getDSource1(str.Substring(28, 1)), -1);
                    wRecord.SetData(WeatherRecord.RecordType.DiffuseHorizontalRadiation, wData);

                    //水平面全天日射[W/m2]
                    if (str.Substring(54, 4) == "9999") wData = new WeatherData(0d, getDSource1(str.Substring(54, 1)), -1);
                    else wData = new WeatherData(double.Parse(str.Substring(54, 4)) / 3.6d, getDSource1(str.Substring(53, 1)), -1);
                    wRecord.SetData(WeatherRecord.RecordType.GlobalHorizontalRadiation, wData);

                    //雲高さ[m]
                    if (str.Substring(72, 4) == "7777") wData = new WeatherData(0d, WeatherData.DataSource.MissingValue, -1);
                    if (str.Substring(72, 4) == "8888") wData = new WeatherData(0d, WeatherData.DataSource.MissingValue, -1);
                    else wData = new WeatherData(double.Parse(str.Substring(72, 4)) * 10, WeatherData.DataSource.MeasuredValue, -1);
                    wRecord.SetData(WeatherRecord.RecordType.CeilingHeight, wData);

                    //視認距離[km]
                    if (str.Substring(81, 4) == "8888") wData = new WeatherData(160d, WeatherData.DataSource.MeasuredValue, -1);
                    else wData = new WeatherData(double.Parse(str.Substring(81, 4)) * 10, WeatherData.DataSource.MeasuredValue, -1);
                    wRecord.SetData(WeatherRecord.RecordType.Visibility, wData);

                    //気圧[kPa]
                    wData = new WeatherData(double.Parse(str.Substring(98, 5)) / 100, WeatherData.DataSource.MeasuredValue, -1);
                    double atm = wData.Value;
                    wRecord.SetData(WeatherRecord.RecordType.AtmosphericPressure, wData);

                    //外気乾球温度[C]
                    wData = new WeatherData(double.Parse(str.Substring(103, 4)) / 10, WeatherData.DataSource.MeasuredValue, -1);
                    double dbt = wData.Value;
                    wRecord.SetData(WeatherRecord.RecordType.DryBulbTemperature, wData);

                    //露点温度[C]
                    wData = new WeatherData(double.Parse(str.Substring(107, 4)) / 10, WeatherData.DataSource.MeasuredValue, -1);
                    double dpt = wData.Value;
                    wRecord.SetData(WeatherRecord.RecordType.DewPointTemperature, wData);

                    //その他の空気状態
                    double ahd = MoistAir.GetSaturatedHumidityRatio(dpt, MoistAir.Property.DryBulbTemperature, atm);
                    MoistAir mAir = MoistAir.GetAirStateFromDBHR(dbt, ahd, atm);

                    //相対湿度[%]
                    wRecord.SetData(WeatherRecord.RecordType.RelativeHumidity, new WeatherData(mAir.RelativeHumidity, WeatherData.DataSource.CalculatedValue, -1));
                    //絶対湿度[kg/kg(DA)]
                    wRecord.SetData(WeatherRecord.RecordType.HumidityRatio, new WeatherData(mAir.HumidityRatio, WeatherData.DataSource.CalculatedValue, -1));

                    //風向
                    wData = new WeatherData(double.Parse(str.Substring(111, 3)), WeatherData.DataSource.MeasuredValue, -1);
                    wRecord.SetData(WeatherRecord.RecordType.WindDirection, wData);

                    //風速[m/s]
                    wData = new WeatherData(double.Parse(str.Substring(114, 4))  / 10d, WeatherData.DataSource.MeasuredValue, -1);
                    wRecord.SetData(WeatherRecord.RecordType.WindSpeed, wData);

                    //雲量
                    double dbl = double.Parse(str.Substring(118, 2));
                    if (dbl == 99) wData = new WeatherData(double.Parse(str.Substring(118, 2)), WeatherData.DataSource.MissingValue, -1);
                    else wData = new WeatherData(double.Parse(str.Substring(118, 2)), WeatherData.DataSource.MeasuredValue, -1);
                    wRecord.SetData(WeatherRecord.RecordType.TotalSkyCover, wData);

                    //雲量2
                    wData = new WeatherData(double.Parse(str.Substring(120, 2)), WeatherData.DataSource.MeasuredValue, -1);
                    wRecord.SetData(WeatherRecord.RecordType.OpaqueSkyCover, wData);

                    //欠測補充
                    wRecord.FillMissingData();

                    //気象レコード追加
                    wdTable.AddWeatherRecord(wRecord);
                }
            }

            success = true;
            return wdTable;
        }