/// <summary> /// Calculates the abundance index over a selected period of time for a specific taxon. /// </summary> /// <param name="periodicityId">The time period id.</param> /// <param name="taxonId">The taxon id.</param> /// <param name="allSelectedSpeciesTimeSeriesData">Calculated time series data for all selected taxa.</param> /// <returns>A list of values representing the abundance index.</returns> public List <AbundanceIndexData> CalculateAbundanceIndex(int periodicityId, int taxonId, TimeStepSpeciesObservationCountList allSelectedSpeciesTimeSeriesData) { List <AbundanceIndexData> abundanceIndexList = new List <AbundanceIndexData>(); TimeStepSpeciesObservationCountList singleSpeciesTimeSeriesData = GetSpeciesObservationCountFromWebService(UserContext, periodicityId, taxonId); //TimeStepSpeciesObservationCountList allSelectedSpeciesTimeSeriesData = GetSpeciesObservationCountFromWebService(UserContext, periodicityId, MySettings.Filter.Taxa.TaxonIds.ToList()); long singleSpeciesTotalSum = singleSpeciesTimeSeriesData.Sum(t => t.ObservationCount); long allSelectedSpeciesTotalSum = allSelectedSpeciesTimeSeriesData.Sum(t => t.ObservationCount); double divisor = (double)singleSpeciesTotalSum / (allSelectedSpeciesTotalSum - singleSpeciesTotalSum); foreach (ITimeStepSpeciesObservationCount timeStepSpeciesObservationCount in allSelectedSpeciesTimeSeriesData) { double singleSpeciesPeriodObservationCount = 0.0; // Find the current timeStep value. foreach (TimeStepSpeciesObservationCount timeStep in singleSpeciesTimeSeriesData) { if (timeStep.Name == timeStepSpeciesObservationCount.Name) { singleSpeciesPeriodObservationCount = timeStep.ObservationCount; break; } } double dividend = singleSpeciesPeriodObservationCount / (timeStepSpeciesObservationCount.ObservationCount - singleSpeciesPeriodObservationCount); double logArgument = dividend / divisor; double value = Math.Log10(logArgument); AbundanceIndexData abundanceIndexData = new AbundanceIndexData(); abundanceIndexData.TaxonId = taxonId; abundanceIndexData.TimeStep = timeStepSpeciesObservationCount.Name; if (!double.IsNaN(value) && !double.IsInfinity(value)) { abundanceIndexData.AbundanceIndex = value; } abundanceIndexData.Count = (int)singleSpeciesPeriodObservationCount; abundanceIndexData.TotalCount = (int)timeStepSpeciesObservationCount.ObservationCount; abundanceIndexList.Add(abundanceIndexData); } return(abundanceIndexList); }
/// <summary> /// Calculates the abundance index over a selected period of time for a specific taxon. /// </summary> /// <param name="periodicityId">The time period id.</param> /// <param name="taxonId">The taxon id.</param> /// <returns>A list of values representing the abundance index.</returns> public List <KeyValuePair <string, string> > GetAbundanceIndexDiagramResult(int periodicityId, int taxonId) { Dictionary <string, string> observationCountData = new Dictionary <string, string>(); // Todo Update when temporal filter is connected to diagram result sprint 13 if (true) // MySettings.Filter.Temporal.IsActive) { TimeStepSpeciesObservationCountList sumSpeciesPeriod = GetSpeciesObservationCountFromWebService(UserContext, periodicityId, taxonId); TimeStepSpeciesObservationCountList sumPeriod = GetSpeciesObservationCountFromWebService(UserContext, periodicityId, MySettings.Filter.Taxa.TaxonIds.ToList()); long sumSpeciesTotal = sumSpeciesPeriod.Sum(t => t.ObservationCount); long sumTotal = sumPeriod.Sum(t => t.ObservationCount); double divisor = (double)sumSpeciesTotal / (sumTotal - sumSpeciesTotal); for (int i = 0; i < sumPeriod.Count; ++i) { double sumSpeciesPeriodObservationCount = 0.0; foreach (TimeStepSpeciesObservationCount timeStep in sumSpeciesPeriod) { if (timeStep.Name == sumPeriod[i].Name) { sumSpeciesPeriodObservationCount = timeStep.ObservationCount; break; } } double dividend = sumSpeciesPeriodObservationCount / (sumPeriod[i].ObservationCount - sumSpeciesPeriodObservationCount); double logArgument = dividend / divisor; double value = Math.Log10(logArgument); observationCountData.Add(sumPeriod[i].Name, !double.IsNaN(value) && !double.IsInfinity(value) ? Convert.ToString(value) : string.Empty); } } List <KeyValuePair <string, string> > result = observationCountData.ToList(); return(result); }