public ITimeSeriesOutput Compute(ITimeSeriesInput inpt, ITimeSeriesOutput outpt, double lat, double lon, string startDate, string endDate, int timeZoneOffset, out string errorMsg) { errorMsg = ""; double relHMax = 0; double relHMin = 0.0; double petGG = 0; //NLDAS2 nldas = new NLDAS2(inpt.Source, lat, lon, startDate, endDate); 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++) //dt.rows.count { DataRow dr = dt.Rows[i]; //Missing day error fixed? 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(), "grangergray"); 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.Remove("WindSpeed_m/s"); 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("GrangerGrayPET_In"); ITimeSeriesOutputFactory oFactory = new TimeSeriesOutputFactory(); ITimeSeriesOutput output = oFactory.Initialize(); output.Dataset = "Evapotranspiration"; output.DataSource = "grangergray"; 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_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()); 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()); GrangerGrayMethod(tmin, tmax, tmean, jday, shmin, shmax, wind, solarRad, out relHMin, out relHMax, out petGG, out errorMsg); dr["RHmin"] = relHMin.ToString("F2", CultureInfo.InstalledUICulture); dr["RHmax"] = relHMax.ToString("F2", CultureInfo.InstalledUICulture); dr["GrangerGrayPET_In"] = petGG.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); }
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); }