     }
     
     /**
-     - parameter bgReadings: blood glucose readings to average.
+    This method uses BGReading, a CoreData managed object.
+     - parameter bgReadings: BGReading readings to average.
        readings may appear in any chronological order, the method reads their timeStamp
      - parameter date: date for quantity. Blood glucose readings after date are ignored.
      - parameter decayLifeSeconds: time for blood glucose from a reading to decay to 0.0.

            return self.getFromHa1cArray(index)
        }
    }


    /**
     - returns: readings between startDate (inclusive) and endDate (inclusive)

                                        date: Date,
                                        decayLifeSeconds: TimeInterval) -> Float {

        // create [BGReadingLight] from [BGReading]
        let bgReadingLights = TrendsAlgorithmModel.bgReadingLights(bgReadings: bgReadings)
        let averageDecayedBG = TrendsAlgorithmModel.averageDecayedBGReadingQuantity(bgReadingLights,
                                                                                    date: date,
                                                                                    decayLifeSeconds: decayLifeSeconds)
        return averageDecayedBG
    }

    /**
    This method uses BGReadingLight, and may be tested without a CoreData context.
     - parameter bgReadingLights: BGReadingLight readings to average.
       readings may appear in any chronological order, the method reads their timeStamp
     - parameter date: date for quantity. Blood glucose readings after date are ignored.
     - parameter decayLifeSeconds: time for blood glucose from a reading to decay to 0.0.
     Typically hemoglobin lifespan seconds.
     - returns: average of decayed BG reading.quantity
     */
    class func averageDecayedBGReadingQuantity(_ bgReadingLights: [BGReadingLight],
                                        date: Date,
                                        decayLifeSeconds: TimeInterval) -> Float {

        if bgReadingLights.count == 0 {
            return 0.0
        }

        var sumOfWeightedBgReadings: Float = 0.0
        var sumOfWeights: Float = 0.0

        for bgReadingLight in bgReadingLights {

            if bgReadingLight.timeStamp > date {
                // skip this reading, continue loop
                continue
            }

            let weight = TrendsAlgorithmModel.weightLinearDecayFirstDate(bgReadingLight.timeStamp,
                                                                         secondDate: date,
                                                                         decayLifeSeconds: decayLifeSeconds)

            sumOfWeightedBgReadings += weight * bgReadingLight.quantity
            sumOfWeights += weight
        }

        // avoid potential divide by 0
        if sumOfWeights == 0 {
            return 0.0
        }
        let average = sumOfWeightedBgReadings / sumOfWeights
        print("averageDecayedBGReadingQuantity \(average)")
        return average
    }
