def get_z_sum(interval_length,
              wet_or_dry,
              zindex_values,
              periods_per_year,
              calibration_start_year,
              calibration_end_year,
              input_start_year):

        tempZ = Stack()
        values_to_sum = Stack()
        summed_values = Stack()

        for zindex in zindex_values:

            # we need to skip Z-index values from the list if they don't exist, this can result from empty months in the final year of the data set
            if not np.isnan(zindex):
                tempZ.push(zindex)

        # remove periods before the start of the calibration interval
        initial_calibration_period_index = (calibration_start_year - input_start_year) * periods_per_year
        for i in range(initial_calibration_period_index):

            if tempZ.size > 0:
                tempZ.pop()
            else:
                break

        calibration_periods_left = (calibration_end_year - calibration_start_year + 1) * periods_per_year;

        # get the first interval length of values from the end of the calibration period working backwards, creating the first sum_value of interval periods
        sum_value = 0
        for i in range(interval_length):
            if tempZ.isEmpty():
                i = interval_length
            else:
                # pull a value off the end of the list
                z = tempZ.pop()

                # reduce the remaining number of calibration interval periods we have left to process
                calibration_periods_left -= 1

                '''
                /* assumes that nCalibrationPeriods is >= length, reasonable
                 ** This is a reasonable assumption and does not hurt if
                 ** anything if false--just calibrate over a slightly longer
                 ** interval, which is already way too short in that case */
                '''
                if (not np.isnan(z) and (MISSING_VALUE != z)):

                    # add to the sum_value
                    sum_value += z

                    # add to the array of values we've used for the initial sum_value
                    values_to_sum.add_first(z)

                else:
                    # reduce the loop counter so we don't skip a calibration interval period
                    i -= 1

        # if we're dealing with wet conditions then we want to be using positive numbers, and if dry conditions
        # then we need to be using negative numbers, so we introduce a sign variable to help with this
        if wet_or_dry == 'wet':
            sign = 1
        else:  # dry
            sign =-1

        # now for each remaining Z value, recalculate the sum_value based on last value in the list to sum_value and the next Z value
        max_sum = sum_value
        summed_values.add_first(sum_value)
        while (tempZ.size > 0) and (calibration_periods_left > 0):

            # take the next Z-index value off the end of the list
            z = tempZ.pop()

            # reduce by one period for each removal
            calibration_periods_left -= 1

            if not np.isnan(z) and (MISSING_VALUE != z) and not values_to_sum.isEmpty():

                # come up with a new sum_value for this new group of values to sum_value

                # remove the last value from both the sum_value and the values to sum_value array
                sum_value -= values_to_sum.peek()
                values_to_sum.pop()

                # add to the sum_value
                sum_value += z

                # update the values to sum_value and summed values lists
                values_to_sum.add_first(z);
                summed_values.add_first(sum_value);

            # update the maximum sum_value value if we have a new max
            if (sign * sum_value) > (sign * max_sum):
                max_sum = sum_value

        # highest reasonable is the highest (or lowest) value that is not due to some freak anomaly in the data.
        # "freak anomaly" is defined as a value that is either
        #   1) 25% higher than the 98th percentile
        #   2) 25% lower than the 2nd percentile
        if wet_or_dry == 'wet':
            safe_percentile_index = int(summed_values.size * 0.98)
        else:  # DRY
            safe_percentile_index = int(summed_values.size * 0.02)

        # sort the list of sums into ascending order and get the sum_value value referenced by the safe percentile index
        summed_values_list = summed_values.sorted_list()
        sum_at_safe_percentile = summed_values_list[safe_percentile_index]

        # find the highest reasonable value out of the summed values
        highest_reasonable_value = 0.0
        reasonable_tolerance_ratio = 1.25
        while (summed_values.size > 0):
            sum_value = summed_values.pop()
            if (sign * sum_value > 0):
                if ((sum_value / sum_at_safe_percentile) < reasonable_tolerance_ratio):
                    if (sign * sum_value > sign * highest_reasonable_value):
                        highest_reasonable_value = sum_value

        if wet_or_dry == 'wet':
            return highest_reasonable_value
        else:  # DRY
            return max_sum