public TimelapseDataWeek GetTimelapseData(TimelapseDataRequest request) { USCounty county = DmRepo.GetCountyForFips(request.CountyFips); Dictionary <string, DroughtMonitorWeek> results = new Dictionary <string, DroughtMonitorWeek>(); DateTime date = Convert.ToDateTime(request.DmWeek); results.Add("COUNTY", DmRepo.FindBy(county, date).FirstOrDefault()); results.Add("STATE", DmRepo.FindBy(county.State, date).FirstOrDefault()); results.Add("US", DmRepo.FindUS(date).FirstOrDefault()); ICollection <AvailableWaterDataByStation> types = WaterRepo.FetchBestDataTypesForStationDate(WaterRepo.GetClosestStations(request.Latitude, request.Longitude, 1), date); AvailableWaterDataByStation discharge = types.Where(t => t.ParameterCode == "00060").FirstOrDefault(); double averageDischarge = 0; if (discharge != null) { double total = 0; ICollection <WaterDataValue> values = WaterRepo.FetchByDateRange(discharge.StationID, discharge.DataID, date, date.AddDays(7)); foreach (WaterDataValue wvalue in values) { if (wvalue.Value != -999999) { total += wvalue.Value; } } averageDischarge = total / (double)values.Count; } return(new TimelapseDataWeek { DMData = results, AverageDischarge = averageDischarge }); }
} //End Load DM Data private DroughtMonitorWeek LoadDMDataValues(DMDataType type, DateTime date, int CountyFIPS) { DroughtMonitorWeek week = null; switch (type) { case DMDataType.COUNTY: week = DmRepository.FindBy(DmRepository.GetCountyForFips(CountyFIPS), date).FirstOrDefault(); break; case DMDataType.STATE: week = DmRepository.FindBy(DmRepository.GetCountyForFips(CountyFIPS).State, date).FirstOrDefault(); break; case DMDataType.US: week = DmRepository.FindUS(date).FirstOrDefault(); break; } if (week == null) { USCounty county = DmRepository.GetCountyForFips(CountyFIPS); week = new DroughtMonitorWeek() { D0 = 0, D1 = 0, D2 = 0, D3 = 0, D4 = 0, NonDrought = 0, Week = date, Type = type, County = county, State = county.State }; } else { week.Type = type; // Normalize data to be out of 100% week.D0 = (float)Math.Round((week.D0 - week.D1), 2); week.D1 = (float)Math.Round((week.D1 - week.D2), 2); week.D2 = (float)Math.Round((week.D2 - week.D3), 2); week.D3 = (float)Math.Round((week.D3 - week.D4), 2); } return(week); } //End LoadDMDataValues
public ICollection <TimeLapseFrame> CreateTimeLapseFramesFromIDs(long[] photoIDs) { List <TimeLapseFrame> frames = new List <TimeLapseFrame>(); long firstID = photoIDs[0]; // stupid LINQ USCounty county = DMRepository.GetCountyForFips(PhotoRepository.Find(p => p.ID == firstID, p => p.Site) .FirstOrDefault().Site.CountyFips); var properties = PhotoRepo.GetPhotoProperties(photoIDs, new string[] { "ID", "Captured" }); foreach (var property in properties) { var frame = new TimeLapseFrame() { FrameTime = (DateTime)property["Captured"], PhotoID = (long)property["ID"] }; frames.Add(frame); } return(frames); }
public USCounty GetCounty(long id) { using (SqlConnection conn = new SqlConnection(_connectionString)) { conn.Open(); using (SqlCommand command = new SqlCommand("select * from Counties where ID = @cid", conn)) { command.Parameters.AddWithValue("@cid", id); SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { USCounty county = new USCounty(); county.ID = reader.GetInt64(0); county.Fips = reader.GetInt32(1); county.Name = reader.GetString(2); county.State = GetState(reader.GetInt64(3)); return(county); } } } throw new ArgumentException(string.Format("id {0} is not recognized", id)); }
public ICollection <DroughtMonitorWeek> FindBy(USCounty county, DateTime?week = null, int weeksPrevious = 0) { week = DroughtMonitorWeek.ConvertDateToTuesday(week.Value); using (SqlConnection conn = new SqlConnection(_connectionString)) { conn.Open(); using (SqlCommand command = new SqlCommand(null, conn)) { if (week == null && weeksPrevious == 0) { command.CommandText = string.Format("select DroughtCategory, DroughtValue, PublishedDate, County_ID from CountyDMValues where County_ID = @county order by PublishedDate, County_ID"); command.Parameters.AddWithValue("@county", county.ID); } else if (week == null && weeksPrevious != 0) { command.CommandText = string.Format("select DroughtCategory, DroughtValue, PublishedDate, County_ID from CountyDMValues where County_ID = @county and PublishedDate >= @rangestart order by PublishedDate, County_ID"); command.Parameters.AddWithValue("@county", county.ID); command.Parameters.AddWithValue("@rangestart", DateTime.Now.AddDays(7 * (0 - weeksPrevious)).ToString("yyyy/MM/dd")); } else if (week != null) { command.CommandText = string.Format("select DroughtCategory, DroughtValue, PublishedDate, County_ID from CountyDMValues where County_ID = @county and PublishedDate >= @rangestart and PublishedDate <= @rangeend order by PublishedDate, County_ID"); command.Parameters.AddWithValue("@county", county.ID); command.Parameters.AddWithValue("@rangestart", week.Value.AddDays(7 * (0 - weeksPrevious)).ToString("yyyy/MM/dd")); command.Parameters.AddWithValue("@rangeend", week.Value.ToString("yyyy/MM/dd")); } else { throw new Exception("No command was specified"); } return(ProcessQuery(command, DMDataType.COUNTY)); } } }