コード例 #1
0
ファイル: Hamon.cs プロジェクト: quanted/hms
        public ITimeSeriesOutput Compute(ITimeSeriesInput inpt, ITimeSeriesOutput outpt, double lat, double lon, string startDate, string endDate, int timeZoneOffset, out string errorMsg)
        {
            errorMsg = "";
            double petHamon      = 0;
            double sunshineHours = 0;

            DataTable dt = new DataTable();

            switch (inpt.Source)
            {
            case "ncdc":
                NCDC ncd = new NCDC();
                dt = ncd.DownloadData(outpt, inpt);
                if (dt == null)
                {
                    errorMsg = "ERROR: Unable to download data. ";
                }
                else
                {
                    dt = ncd.Aggregate(inpt, dt);
                }
                break;

            case "daymet":
                dt = daymetData(inpt, outpt);
                dt = Utilities.Utility.aggregateData(inpt, dt, "hamon");
                break;

            case "custom":
                CustomData cd = new CustomData();
                dt = cd.ParseCustomData(inpt, outpt, inpt.Geometry.GeometryMetadata["userdata"].ToString(), "hamon");
                break;

            case "nldas":
            case "gldas":
            default:
                NLDAS2 nldas = new NLDAS2(inpt.Source, lat, lon, startDate, endDate);
                if (inpt.TemporalResolution == "hourly")
                {
                    dt = nldas.getDataHourly(timeZoneOffset, false, out errorMsg);
                    dt.Columns["THourly_C"].ColumnName = "TMean_C";
                    dt.Columns.Remove("SolarRad_MJm2day");
                    dt.Columns.Remove("SH_Hourly");
                    dt.Columns.Remove("WindSpeed_m/s");
                }
                else
                {
                    dt = nldas.getData1(timeZoneOffset, out errorMsg);
                    DataRow       dr1   = null;
                    List <Double> tList = new List <double>();
                    if (inpt.TemporalResolution == "weekly")
                    {
                        DataTable wkly = dt.Clone();
                        int       j    = 0;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (j == 0)
                            {
                                dr1               = wkly.NewRow();
                                dr1["Date"]       = dt.Rows[i]["Date"].ToString();
                                dr1["Julian_Day"] = dt.Rows[i]["Julian_Day"].ToString();
                                tList             = new List <double>();
                            }
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMin_C"].ToString()));
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMax_C"].ToString()));
                            if (j == 6 || i == dt.Rows.Count - 1)
                            {
                                dr1["TMin_C"]  = tList.Min().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMax_C"]  = tList.Max().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMean_C"] = (tList.Min() + tList.Max()) / 2.0;
                                wkly.Rows.Add(dr1);
                                j = -1;
                            }
                            j++;
                        }
                        dt = wkly;
                    }
                    else if (inpt.TemporalResolution == "monthly")
                    {
                        DataTable mnly     = dt.Clone();
                        int       curmonth = inpt.DateTimeSpan.StartDate.Month;
                        int       j        = 0;
                        bool      newmonth = true;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (newmonth)
                            {
                                dr1               = mnly.NewRow();
                                dr1["Date"]       = dt.Rows[i]["Date"].ToString();
                                dr1["Julian_Day"] = dt.Rows[i]["Julian_Day"].ToString();
                                tList             = new List <double>();
                                newmonth          = false;
                                curmonth          = Convert.ToDateTime(dt.Rows[i]["Date"]).Month;
                            }
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMin_C"].ToString()));
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMax_C"].ToString()));
                            if (i + 1 < dt.Rows.Count && (Convert.ToDateTime(dt.Rows[i + 1]["Date"]).Month != curmonth) || i == dt.Rows.Count - 1)
                            {
                                dr1["TMin_C"]  = tList.Min().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMax_C"]  = tList.Max().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMean_C"] = (tList.Min() + tList.Max()) / 2.0;
                                mnly.Rows.Add(dr1);
                                j        = -1;
                                newmonth = true;
                            }
                            j++;
                        }
                        dt = mnly;
                    }
                }
                break;
            }

            if (errorMsg != "")
            {
                Utilities.ErrorOutput err = new Utilities.ErrorOutput();
                return(err.ReturnError(errorMsg));
            }

            dt.Columns.Add("Sunshine_Hours");
            dt.Columns.Add("HamonPET_in");

            ITimeSeriesOutputFactory oFactory = new TimeSeriesOutputFactory();
            ITimeSeriesOutput        output   = oFactory.Initialize();

            output.Dataset    = "Evapotranspiration";
            output.DataSource = "hamon";
            output.Metadata   = new Dictionary <string, string>()
            {
                { "latitude", latitude.ToString() },
                { "longitude", longitude.ToString() },
                { "request_time", DateTime.Now.ToString() },
                { "column_1", "Date" },
                { "column_2", "Julian Day" },
                { "column_3", "Minimum Temperature" },
                { "column_4", "Maximum Temperature" },
                { "column_5", "Mean Temperature" },
                { "column_6", "Sunshine Hours" },
                { "column_7", "Potential Evapotranspiration" }
            };

            if (inpt.TemporalResolution == "hourly")
            {
                output.Metadata = new Dictionary <string, string>()
                {
                    { "latitude", latitude.ToString() },
                    { "longitude", longitude.ToString() },
                    { "request_time", DateTime.Now.ToString() },
                    { "column_1", "DateHour" },
                    { "column_2", "Julian Day" },
                    { "column_3", "Hourly Temperature" },
                    { "column_4", "Sunshine Hours" },
                    { "column_5", "Potential Evapotranspiration" }
                };
            }

            output.Data = new Dictionary <string, List <string> >();

            foreach (DataRow dr in dt.Rows)
            {
                double tmean = Convert.ToDouble(dr["TMean_C"].ToString());
                int    jday  = Convert.ToInt32(dr["Julian_Day"]);
                HamonMethod(tmean, jday, out petHamon, out sunshineHours, out errorMsg);
                dr["Sunshine_Hours"] = sunshineHours.ToString("F2", CultureInfo.InvariantCulture);
                dr["HamonPET_in"]    = petHamon.ToString("F4", CultureInfo.InvariantCulture);
                List <string> lv = new List <string>();
                foreach (Object g in dr.ItemArray.Skip(1))
                {
                    lv.Add(g.ToString());
                }
                output.Data.Add(dr[0].ToString(), lv);
            }
            return(output);
        }
コード例 #2
0
ファイル: PriestlyTaylor.cs プロジェクト: quanted/hms
        public ITimeSeriesOutput Compute(ITimeSeriesInput inpt, ITimeSeriesOutput outpt, double lat, double lon, string startDate, string endDate, int timeZoneOffset, out string errorMsg)
        {
            errorMsg = "";
            double petPT = 0;

            //NLDAS2 nldas = new NLDAS2(inpt.Source, lat, lon, startDate, endDate);

            DataTable dt = new DataTable();

            switch (inpt.Source)
            {
            case "daymet":
                dt = daymetData(inpt, outpt);
                dt = Utilities.Utility.aggregateData(inpt, dt, "priestlytaylor");
                break;

            case "custom":
                CustomData cd = new CustomData();
                dt = cd.ParseCustomData(inpt, outpt, inpt.Geometry.GeometryMetadata["userdata"].ToString(), "priestlytaylor");
                break;

            case "nldas":
            case "gldas":
            default:
                NLDAS2 nldas = new NLDAS2(inpt.Source, lat, lon, startDate, endDate);
                if (inpt.TemporalResolution == "hourly")
                {
                    NLDAS2    nldasday = new NLDAS2(inpt.Source, lat, lon, startDate, endDate);
                    DataTable dtd      = nldasday.getData2(timeZoneOffset, out errorMsg);
                    dt = nldas.getDataHourly(timeZoneOffset, false, out errorMsg);
                    dt.Columns["THourly_C"].ColumnName        = "TMean_C";
                    dt.Columns["SolarRad_MJm2day"].ColumnName = "SolarRadMean_MJm2day";
                    dt.Columns.Remove("SH_Hourly");
                    dt.Columns.Remove("WindSpeed_m/s");
                    dt.Columns.Add("TMin_C");
                    dt.Columns.Add("TMax_C");
                    int j = -1;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if ((inpt.Source == "nldas" && (i % 24 == 0)) || (inpt.Source == "gldas" && (i % 8 == 0)))
                        {
                            j++;
                        }
                        DataRow dr = dtd.Rows[j];
                        dt.Rows[i]["TMin_C"] = dr["TMin_C"];
                        dt.Rows[i]["TMax_C"] = dr["TMax_C"];
                    }
                    dtd = null;
                }
                else
                {
                    dt = nldas.getData2(timeZoneOffset, out errorMsg);
                    DataRow       dr1   = null;
                    List <Double> tList = new List <double>();
                    double        sol   = 0.0;
                    if (inpt.TemporalResolution == "weekly")
                    {
                        DataTable wkly = dt.Clone();
                        int       j    = 0;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (j == 0)
                            {
                                dr1               = wkly.NewRow();
                                dr1["Date"]       = dt.Rows[i]["Date"].ToString();
                                dr1["Julian_Day"] = dt.Rows[i]["Julian_Day"].ToString();
                                tList             = new List <double>();
                                sol               = 0.0;
                            }
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMin_C"].ToString()));
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMax_C"].ToString()));
                            sol += Convert.ToDouble(dt.Rows[i]["SolarRadMean_MJm2day"]);
                            if (j == 6 || i == dt.Rows.Count - 1)
                            {
                                dr1["TMin_C"]  = tList.Min().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMax_C"]  = tList.Max().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMean_C"] = (tList.Min() + tList.Max()) / 2.0;
                                dr1["SolarRadMean_MJm2day"] = Math.Round(sol / (j + 1), 2);
                                wkly.Rows.Add(dr1);
                                j = -1;
                            }
                            j++;
                        }
                        dt = wkly;
                    }
                    else if (inpt.TemporalResolution == "monthly")
                    {
                        DataTable mnly     = dt.Clone();
                        int       curmonth = inpt.DateTimeSpan.StartDate.Month;
                        int       j        = 0;
                        bool      newmonth = true;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (newmonth)
                            {
                                dr1               = mnly.NewRow();
                                dr1["Date"]       = dt.Rows[i]["Date"].ToString();
                                dr1["Julian_Day"] = dt.Rows[i]["Julian_Day"].ToString();
                                tList             = new List <double>();
                                sol               = 0.0;
                                newmonth          = false;
                                curmonth          = Convert.ToDateTime(dt.Rows[i]["Date"]).Month;
                            }
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMin_C"].ToString()));
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMax_C"].ToString()));
                            sol += Convert.ToDouble(dt.Rows[i]["SolarRadMean_MJm2day"]);
                            if (i + 1 < dt.Rows.Count && (Convert.ToDateTime(dt.Rows[i + 1]["Date"]).Month != curmonth) || i == dt.Rows.Count - 1)
                            {
                                dr1["TMin_C"]  = tList.Min().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMax_C"]  = tList.Max().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMean_C"] = (tList.Min() + tList.Max()) / 2.0;
                                dr1["SolarRadMean_MJm2day"] = Math.Round(sol / (j + 1), 2);
                                mnly.Rows.Add(dr1);
                                j        = -1;
                                newmonth = true;
                            }
                            j++;
                        }
                        dt = mnly;
                    }
                }
                break;
            }

            //dt = nldas.getData2(timeZoneOffset, out errorMsg);

            if (errorMsg != "")
            {
                Utilities.ErrorOutput err = new Utilities.ErrorOutput();
                return(err.ReturnError(errorMsg));
            }

            dt.Columns.Add("PriestleyTaylorPET_in");

            ITimeSeriesOutputFactory oFactory = new TimeSeriesOutputFactory();
            ITimeSeriesOutput        output   = oFactory.Initialize();

            output.Dataset    = "Evapotranspiration";
            output.DataSource = "priestlytaylor";
            output.Metadata   = new Dictionary <string, string>()
            {
                { "elevation", elevation.ToString() },
                { "latitude", latitude.ToString() },
                { "longitude", longitude.ToString() },
                { "albedo", albedo.ToString() },
                { "request_time", DateTime.Now.ToString() },
                { "column_1", "Date" },
                { "column_2", "Julian Day" },
                { "column_3", "Minimum Temperature" },
                { "column_4", "Maximum Temperature" },
                { "column_5", "Mean Temperature" },
                { "column_6", "Mean Solar Radiation" },
                { "column_7", "Potential Evapotranspiration" }
            };
            if (inpt.TemporalResolution == "hourly")
            {
                output.Metadata = new Dictionary <string, string>()
                {
                    { "latitude", latitude.ToString() },
                    { "longitude", longitude.ToString() },
                    { "request_time", DateTime.Now.ToString() },
                    { "column_1", "DateHour" },
                    { "column_2", "Julian Day" },
                    { "column_3", "Hourly Temperature" },
                    { "column_4", "Mean Solar Radiation" },
                    { "column_5", "Minimum Daily Temperature" },
                    { "column_6", "Maximum Daily Temperature" },
                    { "column_7", "Potential Evapotranspiration" }
                };
            }
            output.Data = new Dictionary <string, List <string> >();

            foreach (DataRow dr in dt.Rows)
            {
                double tmean    = Convert.ToDouble(dr["TMean_C"].ToString());
                double tmin     = Convert.ToDouble(dr["TMin_C"].ToString());
                double tmax     = Convert.ToDouble(dr["TMax_C"].ToString());
                double solarRad = Convert.ToDouble(dr["SolarRadMean_MJm2day"].ToString());
                int    jday     = Convert.ToInt32(dr["Julian_Day"].ToString());
                PriestlyTaylorMethod(tmin, tmax, tmean, solarRad, jday, out petPT, out errorMsg);
                dr["PriestleyTaylorPET_in"] = petPT.ToString("F4", CultureInfo.InvariantCulture);
                List <string> lv = new List <string>();
                foreach (Object g in dr.ItemArray.Skip(1))
                {
                    lv.Add(g.ToString());
                }
                output.Data.Add(dr[0].ToString(), lv);
            }
            return(output);
        }
コード例 #3
0
        public ITimeSeriesOutput Compute(ITimeSeriesInput inpt, ITimeSeriesOutput outpt, double lat, double lon, string startDate, string endDate, int timeZoneOffset, int model,
                                         out double aprecip, out string errorMsg)
        {
            errorMsg = "";
            //NLDAS2 nldas = new NLDAS2(inpt.Source, lat, lon, startDate, endDate);
            aprecip = 0.0;
            //AnnualPrecipitation = aprecip;
            double      relHMax = 0;
            double      relHMin = 0.0;
            double      petMC   = 0;
            string      strDate;
            CultureInfo CInfoUS = new CultureInfo("en-US");

            //DataTable dt = nldas.getData3(timeZoneOffset, out errorMsg);
            DataTable dt    = new DataTable();
            DataTable data3 = new DataTable();

            switch (inpt.Source)
            {
            case "daymet":
                dt          = daymetData(inpt, outpt);
                dt          = Utilities.Utility.aggregateData(inpt, dt, "mortoncrae");
                inpt.Source = "nldas";
                NLDAS2 nldas = new NLDAS2(inpt.Source, lat, lon, startDate, endDate);
                data3 = nldas.getData3(timeZoneOffset, out errorMsg);
                dt.Columns.Add("SHmin");
                dt.Columns.Add("SHmax");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["SHmin"] = data3.Rows[i]["SHmin"];
                    dt.Rows[i]["SHmax"] = data3.Rows[i]["SHmin"];
                }
                data3       = null;
                inpt.Source = "daymet";
                break;

            case "custom":
                CustomData cd = new CustomData();
                dt = cd.ParseCustomData(inpt, outpt, inpt.Geometry.GeometryMetadata["userdata"].ToString(), "mortoncrae");
                break;

            case "nldas":
            case "gldas":
            default:
                NLDAS2 nldas2 = new NLDAS2(inpt.Source, lat, lon, startDate, endDate);
                if (inpt.TemporalResolution == "hourly")
                {
                    NLDAS2    nldasday = new NLDAS2(inpt.Source, lat, lon, startDate, endDate);
                    DataTable dtd      = nldasday.getData3(timeZoneOffset, out errorMsg);
                    dt = nldas2.getDataHourly(timeZoneOffset, false, out errorMsg);
                    dt.Columns["THourly_C"].ColumnName        = "TMean_C";
                    dt.Columns["SolarRad_MJm2day"].ColumnName = "SolarRadMean_MJm2day";
                    dt.Columns.Remove("WindSpeed_m/s");
                    //dt.Columns["WindSpeed_m/s"].ColumnName = "WindSpeedMean_m/s";
                    dt.Columns.Remove("SH_Hourly");
                    dt.Columns.Add("TMin_C");
                    dt.Columns.Add("TMax_C");
                    dt.Columns.Add("SHmin");
                    dt.Columns.Add("SHmax");
                    int j = -1;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if ((inpt.Source == "nldas" && (i % 24 == 0)) || (inpt.Source == "gldas" && (i % 8 == 0)))
                        {
                            j++;
                        }
                        DataRow dr = dtd.Rows[j];
                        dt.Rows[i]["TMin_C"] = dr["TMin_C"];
                        dt.Rows[i]["TMax_C"] = dr["TMax_C"];
                        dt.Rows[i]["SHmin"]  = dr["SHmin"];
                        dt.Rows[i]["SHmax"]  = dr["SHmax"];
                    }
                    dtd = null;
                }
                else
                {
                    dt = nldas2.getData3(timeZoneOffset, out errorMsg);
                    DataRow       dr1   = null;
                    List <Double> tList = new List <double>();
                    List <Double> sList = new List <double>();
                    double        sol   = 0.0;
                    if (inpt.TemporalResolution == "weekly")
                    {
                        DataTable wkly = dt.Clone();
                        int       j    = 0;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (j == 0)
                            {
                                dr1               = wkly.NewRow();
                                dr1["Date"]       = dt.Rows[i]["Date"].ToString();
                                dr1["Julian_Day"] = dt.Rows[i]["Julian_Day"].ToString();
                                tList             = new List <double>();
                                sList             = new List <double>();
                                sol               = 0.0;
                            }
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMin_C"].ToString()));
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMax_C"].ToString()));
                            sol += Convert.ToDouble(dt.Rows[i]["SolarRadMean_MJm2day"]);
                            sList.Add(Convert.ToDouble(dt.Rows[i]["SHmin"].ToString()));
                            sList.Add(Convert.ToDouble(dt.Rows[i]["SHmax"].ToString()));
                            if (j == 6 || i == dt.Rows.Count - 1)
                            {
                                dr1["TMin_C"]  = tList.Min().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMax_C"]  = tList.Max().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMean_C"] = (tList.Min() + tList.Max()) / 2.0;
                                dr1["SolarRadMean_MJm2day"] = Math.Round(sol / (j + 1), 2);
                                dr1["SHmin"] = sList.Min().ToString();
                                dr1["SHmax"] = sList.Max().ToString();
                                wkly.Rows.Add(dr1);
                                j = -1;
                            }
                            j++;
                        }
                        dt = wkly;
                    }
                    else if (inpt.TemporalResolution == "monthly")
                    {
                        DataTable mnly     = dt.Clone();
                        int       curmonth = inpt.DateTimeSpan.StartDate.Month;
                        int       j        = 0;
                        bool      newmonth = true;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (newmonth)
                            {
                                dr1               = mnly.NewRow();
                                dr1["Date"]       = dt.Rows[i]["Date"].ToString();
                                dr1["Julian_Day"] = dt.Rows[i]["Julian_Day"].ToString();
                                tList             = new List <double>();
                                sList             = new List <double>();
                                sol               = 0.0;
                                newmonth          = false;
                                curmonth          = Convert.ToDateTime(dt.Rows[i]["Date"]).Month;
                            }
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMin_C"].ToString()));
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMax_C"].ToString()));
                            sol += Convert.ToDouble(dt.Rows[i]["SolarRadMean_MJm2day"]);
                            sList.Add(Convert.ToDouble(dt.Rows[i]["SHmin"].ToString()));
                            sList.Add(Convert.ToDouble(dt.Rows[i]["SHmax"].ToString()));
                            if (i + 1 < dt.Rows.Count && (Convert.ToDateTime(dt.Rows[i + 1]["Date"]).Month != curmonth) || i == dt.Rows.Count - 1)
                            {
                                dr1["TMin_C"]  = tList.Min().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMax_C"]  = tList.Max().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMean_C"] = (tList.Min() + tList.Max()) / 2.0;
                                dr1["SolarRadMean_MJm2day"] = Math.Round(sol / (j + 1), 2);
                                dr1["SHmin"] = sList.Min().ToString();
                                dr1["SHmax"] = sList.Max().ToString();
                                mnly.Rows.Add(dr1);
                                j        = -1;
                                newmonth = true;
                            }
                            j++;
                        }
                        dt = mnly;
                    }
                }
                //dt = nldas2.getData3(timeZoneOffset, out errorMsg);
                aprecip             = nldas2.getAnnualPrecipitation();
                AnnualPrecipitation = aprecip;
                break;
            }

            if (errorMsg != "")
            {
                Utilities.ErrorOutput err = new Utilities.ErrorOutput();
                return(err.ReturnError(errorMsg));
            }

            dt.Columns.Add("RHmin");
            dt.Columns.Add("RHmax");
            dt.Columns.Add("MortonCRAEPET_in");

            ITimeSeriesOutputFactory oFactory = new TimeSeriesOutputFactory();
            ITimeSeriesOutput        output   = oFactory.Initialize();

            output.Dataset    = "Evapotranspiration";
            output.DataSource = "mortoncrae";
            output.Metadata   = new Dictionary <string, string>()
            {
                { "elevation", elevation.ToString() },
                { "latitude", latitude.ToString() },
                { "longitude", longitude.ToString() },
                { "albedo", albedo.ToString() },
                { "emissivity", emissivity.ToString() },
                { "annual_precipitation", annualPrecipitation.ToString() },
                { "request_time", DateTime.Now.ToString() },
                { "column_1", "Date" },
                { "column_2", "Julian Day" },
                { "column_3", "Minimum Temperature" },
                { "column_4", "Maximum Temperature" },
                { "column_5", "Mean Temperature" },
                { "column_6", "Mean Solar Radiation" },
                { "column_7", "Minimum Relative Humidity" },
                { "column_8", "Maximum Relative Humidity" },
                { "column_9", "Potential Evapotranspiration" }
            };
            if (inpt.TemporalResolution == "hourly")
            {
                output.Metadata = new Dictionary <string, string>()
                {
                    { "latitude", latitude.ToString() },
                    { "longitude", longitude.ToString() },
                    { "request_time", DateTime.Now.ToString() },
                    { "column_1", "DateHour" },
                    { "column_2", "Julian Day" },
                    { "column_3", "Hourly Temperature" },
                    { "column_4", "Mean Solar Radiation" },
                    { "column_5", "Minimum Daily Temperature" },
                    { "column_6", "Maximum Daily Temperature" },
                    { "column_7", "Minimum Relative Humidity" },
                    { "column_8", "Maximum Relative Humidity" },
                    { "column_9", "Potential Evapotranspiration" }
                };
            }
            output.Data = new Dictionary <string, List <string> >();

            foreach (DataRow dr in dt.Rows)
            {
                double tmean = Convert.ToDouble(dr["TMean_C"].ToString());
                double tmin  = Convert.ToDouble(dr["TMin_C"].ToString());
                double tmax  = Convert.ToDouble(dr["TMax_C"].ToString());
                double shmin = Convert.ToDouble(dr["SHmin"].ToString());
                double shmax = Convert.ToDouble(dr["SHmax"].ToString());
                if (inpt.TemporalResolution == "hourly")
                {
                    DateTime days = DateTime.Parse(dr["DateHour"].ToString());
                    strDate = days.ToString("yyyy-MM-dd");
                }
                else
                {
                    strDate = dr["Date"].ToString().Trim();
                }
                DateTime time1    = DateTime.ParseExact(strDate, "yyyy-MM-dd", CInfoUS);
                double   solarRad = Convert.ToDouble(dr["SolarRadMean_MJm2day"].ToString());
                int      jday     = Convert.ToInt32(dr["Julian_Day"].ToString());

                MortonCRAEMethod(tmin, tmax, tmean, jday, time1, shmin, shmax, solarRad, model, out relHMin, out relHMax,
                                 out petMC, out errorMsg);
                if (inpt.Source == "daymet")
                {
                    double vapor = Convert.ToDouble(dr["VaPress"].ToString());
                    dr["RHmin"] = daymetHumid(tmin, vapor).ToString("F2", CultureInfo.InstalledUICulture);
                    dr["RHmax"] = daymetHumid(tmax, vapor).ToString("F2", CultureInfo.InstalledUICulture);
                }
                else
                {
                    dr["RHmin"] = relHMin.ToString("F2", CultureInfo.InstalledUICulture);
                    dr["RHmax"] = relHMax.ToString("F2", CultureInfo.InstalledUICulture);
                }
                dr["MortonCRAEPET_in"] = petMC.ToString("F4", CultureInfo.InvariantCulture);
            }
            if (inpt.Source == "daymet")
            {
                dt.Columns.Remove("VaPress");
            }
            dt.Columns.Remove("SHmin");
            dt.Columns.Remove("SHmax");

            foreach (DataRow dr in dt.Rows)
            {
                List <string> lv = new List <string>();
                foreach (Object g in dr.ItemArray.Skip(1))
                {
                    lv.Add(g.ToString());
                }
                output.Data.Add(dr[0].ToString(), lv);
            }
            return(output);
        }
コード例 #4
0
        public ITimeSeriesOutput Compute(ITimeSeriesInput inpt, ITimeSeriesOutput outpt, double lat, double lon, string startDate, string endDate, int timeZoneOffset, out string errorMsg)
        {
            errorMsg = "";
            //NLDAS2 nldas = new NLDAS2(inpt.Source, lat, lon, startDate, endDate);
            double      relHMax = 0;
            double      relHMin = 0.0;
            double      petSW   = 0;
            string      strDate;
            CultureInfo CInfoUS = new CultureInfo("en-US");

            //DataTable dt = nldas.getData4(timeZoneOffset, out errorMsg);
            DataTable dt      = new DataTable();
            DataTable daymets = new DataTable();

            switch (inpt.Source)
            {
            case "daymet":
                daymets     = daymetData(inpt, outpt);
                inpt.Source = "nldas";
                NLDAS2 nldas = new NLDAS2(inpt.Source, lat, lon, startDate, endDate);
                dt = nldas.getData4(timeZoneOffset, out errorMsg);
                for (int i = 0; i < daymets.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    dr["TMin_C"]  = daymets.Rows[i]["TMin_C"];
                    dr["TMax_C"]  = daymets.Rows[i]["TMax_C"];
                    dr["TMean_C"] = daymets.Rows[i]["TMean_C"];
                    dr["SolarRadMean_MJm2day"] = daymets.Rows[i]["SolarRadMean_MJm2day"];
                }
                daymets = null;
                break;

            case "custom":
                CustomData cd = new CustomData();
                dt = cd.ParseCustomData(inpt, outpt, inpt.Geometry.GeometryMetadata["userdata"].ToString(), "shuttleworthwallace");
                break;

            case "nldas":
            case "gldas":
            default:
                NLDAS2 nldas2 = new NLDAS2(inpt.Source, lat, lon, startDate, endDate);
                if (inpt.TemporalResolution == "hourly")
                {
                    NLDAS2    nldasday = new NLDAS2(inpt.Source, lat, lon, startDate, endDate);
                    DataTable dtd      = nldasday.getData4(timeZoneOffset, out errorMsg);
                    dt = nldas2.getDataHourly(timeZoneOffset, false, out errorMsg);
                    dt.Columns["THourly_C"].ColumnName        = "TMean_C";
                    dt.Columns["SolarRad_MJm2day"].ColumnName = "SolarRadMean_MJm2day";
                    dt.Columns["WindSpeed_m/s"].ColumnName    = "WindSpeedMean_m/s";
                    dt.Columns.Remove("SH_Hourly");
                    dt.Columns.Add("TMin_C");
                    dt.Columns.Add("TMax_C");
                    dt.Columns.Add("SHmin");
                    dt.Columns.Add("SHmax");
                    int j = -1;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if ((inpt.Source == "nldas" && (i % 24 == 0)) || (inpt.Source == "gldas" && (i % 8 == 0)))
                        {
                            j++;
                        }
                        DataRow dr = dtd.Rows[j];
                        dt.Rows[i]["TMin_C"] = dr["TMin_C"];
                        dt.Rows[i]["TMax_C"] = dr["TMax_C"];
                        dt.Rows[i]["SHmin"]  = dr["SHmin"];
                        dt.Rows[i]["SHmax"]  = dr["SHmax"];
                    }
                    dtd = null;
                }
                else
                {
                    dt = nldas2.getData4(timeZoneOffset, out errorMsg);
                    DataRow       dr1   = null;
                    List <Double> tList = new List <double>();
                    List <Double> sList = new List <double>();
                    double        sol   = 0.0;
                    double        wind  = 0.0;
                    if (inpt.TemporalResolution == "weekly")
                    {
                        DataTable wkly = dt.Clone();
                        int       j    = 0;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (j == 0)
                            {
                                dr1               = wkly.NewRow();
                                dr1["Date"]       = dt.Rows[i]["Date"].ToString();
                                dr1["Julian_Day"] = dt.Rows[i]["Julian_Day"].ToString();
                                tList             = new List <double>();
                                sList             = new List <double>();
                                sol               = 0.0;
                                wind              = 0.0;
                            }
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMin_C"].ToString()));
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMax_C"].ToString()));
                            sol  += Convert.ToDouble(dt.Rows[i]["SolarRadMean_MJm2day"]);
                            wind += Convert.ToDouble(dt.Rows[i]["WindSpeedMean_m/s"]);
                            sList.Add(Convert.ToDouble(dt.Rows[i]["SHmin"].ToString()));
                            sList.Add(Convert.ToDouble(dt.Rows[i]["SHmax"].ToString()));
                            if (j == 6 || i == dt.Rows.Count - 1)
                            {
                                dr1["TMin_C"]  = tList.Min().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMax_C"]  = tList.Max().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMean_C"] = (tList.Min() + tList.Max()) / 2.0;
                                dr1["SolarRadMean_MJm2day"] = Math.Round(sol / (j + 1), 2);
                                dr1["WindSpeedMean_m/s"]    = Math.Round(wind / (j + 1), 2);
                                dr1["SHmin"] = sList.Min().ToString();
                                dr1["SHmax"] = sList.Max().ToString();
                                wkly.Rows.Add(dr1);
                                j = -1;
                            }
                            j++;
                        }
                        dt = wkly;
                    }
                    else if (inpt.TemporalResolution == "monthly")
                    {
                        DataTable mnly     = dt.Clone();
                        int       curmonth = inpt.DateTimeSpan.StartDate.Month;
                        int       j        = 0;
                        bool      newmonth = true;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (newmonth)
                            {
                                dr1               = mnly.NewRow();
                                dr1["Date"]       = dt.Rows[i]["Date"].ToString();
                                dr1["Julian_Day"] = dt.Rows[i]["Julian_Day"].ToString();
                                tList             = new List <double>();
                                sList             = new List <double>();
                                sol               = 0.0;
                                wind              = 0.0;
                                newmonth          = false;
                                curmonth          = Convert.ToDateTime(dt.Rows[i]["Date"]).Month;
                            }
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMin_C"].ToString()));
                            tList.Add(Convert.ToDouble(dt.Rows[i]["TMax_C"].ToString()));
                            sol  += Convert.ToDouble(dt.Rows[i]["SolarRadMean_MJm2day"]);
                            wind += Convert.ToDouble(dt.Rows[i]["WindSpeedMean_m/s"]);
                            sList.Add(Convert.ToDouble(dt.Rows[i]["SHmin"].ToString()));
                            sList.Add(Convert.ToDouble(dt.Rows[i]["SHmax"].ToString()));
                            if (i + 1 < dt.Rows.Count && (Convert.ToDateTime(dt.Rows[i + 1]["Date"]).Month != curmonth) || i == dt.Rows.Count - 1)
                            {
                                dr1["TMin_C"]  = tList.Min().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMax_C"]  = tList.Max().ToString("F2", CultureInfo.InvariantCulture);
                                dr1["TMean_C"] = (tList.Min() + tList.Max()) / 2.0;
                                dr1["SolarRadMean_MJm2day"] = Math.Round(sol / (j + 1), 2);
                                dr1["WindSpeedMean_m/s"]    = Math.Round(wind / (j + 1), 2);
                                dr1["SHmin"] = sList.Min().ToString();
                                dr1["SHmax"] = sList.Max().ToString();
                                mnly.Rows.Add(dr1);
                                j        = -1;
                                newmonth = true;
                            }
                            j++;
                        }
                        dt = mnly;
                    }
                }
                break;
            }

            if (errorMsg != "")
            {
                Utilities.ErrorOutput err = new Utilities.ErrorOutput();
                return(err.ReturnError(errorMsg));
            }

            dt.Columns.Add("RHmin");
            dt.Columns.Add("RHmax");
            dt.Columns.Add("ShuttleworthWallacePET_in");

            ITimeSeriesOutputFactory oFactory = new TimeSeriesOutputFactory();
            ITimeSeriesOutput        output   = oFactory.Initialize();

            output.Dataset    = "Evapotranspiration";
            output.DataSource = "shuttleworthwallace";
            string leafareas = "{ ";

            foreach (int key in leafAreaIndex.Keys)
            {
                leafareas += String.Format("{0}: {1}, ", key, leafAreaIndex[key]);
            }
            leafareas      += "}";
            output.Metadata = new Dictionary <string, string>()
            {
                { "elevation", elevation.ToString() },
                { "latitude", latitude.ToString() },
                { "longitude", longitude.ToString() },
                { "albedo", albedo.ToString() },
                { "stomatal_resistance", resistanceStomatal.ToString() },
                { "surface_resistance", resistanceSurfaceSoil.ToString() },
                { "ground_roughness_length", groundRoughnessLength.ToString() },
                { "leaf_width", widthLeaf.ToString() },
                { "vegetation_height", vegetationHeight.ToString() },
                { "leaf_area_indices", leafareas },
                { "request_time", DateTime.Now.ToString() },
                { "column_1", "Date" },
                { "column_2", "Julian Day" },
                { "column_3", "Minimum Temperature" },
                { "column_3.1", "Maximum Temperature" },
                { "column_3.2", "Mean Temperature" },
                { "column_4", "Mean Solar Radiation" },
                { "column_5", "Mean Wind Speed" },
                { "column_6", "Minimum Relative Humidity" },
                { "column_7", "Maximum Relative Humidity" },
                { "column_8", "Potential Evapotranspiration" }
            };
            if (inpt.TemporalResolution == "hourly")
            {
                output.Metadata = new Dictionary <string, string>()
                {
                    { "latitude", latitude.ToString() },
                    { "longitude", longitude.ToString() },
                    { "request_time", DateTime.Now.ToString() },
                    { "column_1", "DateHour" },
                    { "column_2", "Julian Day" },
                    { "column_3", "Hourly Temperature" },
                    { "column_4", "Mean Solar Radiation" },
                    { "column_5", "Minimum Daily Temperature" },
                    { "column_6", "Maximum Daily Temperature" },
                    { "column_6.1", "Mean Wind Speed" },
                    { "column_7", "Minimum Relative Humidity" },
                    { "column_8", "Maximum Relative Humidity" },
                    { "column_9", "Potential Evapotranspiration" }
                };
            }
            output.Data = new Dictionary <string, List <string> >();

            foreach (DataRow dr in dt.Rows)
            {
                double tmean = Convert.ToDouble(dr["TMean_C"].ToString());
                double tmin  = Convert.ToDouble(dr["TMin_C"].ToString());
                double tmax  = Convert.ToDouble(dr["TMax_C"].ToString());
                double shmin = Convert.ToDouble(dr["SHmin"].ToString());
                double shmax = Convert.ToDouble(dr["SHmax"].ToString());
                if (inpt.TemporalResolution == "hourly")
                {
                    DateTime days = DateTime.Parse(dr["DateHour"].ToString());
                    strDate = days.ToString("yyyy-MM-dd");
                }
                else
                {
                    strDate = dr["Date"].ToString();
                }
                DateTime time1    = DateTime.ParseExact(strDate, "yyyy-MM-dd", CInfoUS);
                double   wind     = Convert.ToDouble(dr["WindSpeedMean_m/s"].ToString());
                double   solarRad = Convert.ToDouble(dr["SolarRadMean_MJm2day"].ToString());
                int      jday     = Convert.ToInt32(dr["Julian_Day"].ToString());

                ShuttleworthWallaceMethod(tmin, tmax, tmean, jday, time1, shmin, shmax, wind, solarRad,
                                          out relHMin, out relHMax, out petSW, out errorMsg);

                dr["RHmin"] = relHMin.ToString("F2", CultureInfo.InstalledUICulture);
                dr["RHmax"] = relHMax.ToString("F2", CultureInfo.InstalledUICulture);
                dr["ShuttleworthWallacePET_in"] = petSW.ToString("F4", CultureInfo.InvariantCulture);
            }

            dt.Columns.Remove("SHmin");
            dt.Columns.Remove("SHmax");

            foreach (DataRow dr in dt.Rows)
            {
                List <string> lv = new List <string>();
                foreach (Object g in dr.ItemArray.Skip(1))
                {
                    lv.Add(g.ToString());
                }
                output.Data.Add(dr[0].ToString(), lv);
            }
            return(output);
        }