import statistics


def calculate_today_intermediate(region, db, local_forecast):
	"""
	Calculate and return intermediate forecast for today.
	Blends historical hour temperature and forecast from API.

	:param region: instance of Region class <object>.
	:param db: instance of DatabaseQueries class <object>.
	:param local_forecast: dictionary with local times <datetime.date> and temps <float>.
	:return: mean temperature for today <float>.
	"""
	
	# get all not null temps from today table
	today_historical_params = {'table': 'today',
						  'conditions': ['region_id'],
						  'condition_data': (region.id,),
						  'filters': ['hour_{}'.format(x) for x in range(0, 24, 3)]}
	# download historical temperature for today (returns a list of tuples)
	today_historical = db.select(**today_historical_params)

	# filter out None values
	today_historical = [temp for temp in today_historical[0] if temp != None]

	# get forecast for the rest of the day
	today_forecast = [v for k, v in local_forecast.items() if k.date() == list(local_forecast)[0].date()]

	# blend these together and count mean
	today_avg = round(statistics.mean(today_historical + today_forecast), 2)

	return today_avg