# Metview Macro

# **************************** LICENSE START ***********************************
#
# Copyright 2012 ECMWF. This software is distributed under the terms
# of the Apache License version 2.0. In applying this license, ECMWF does not
# waive the privileges and immunities granted to it by virtue of its status as
# an Intergovernmental Organization or submit itself to any jurisdiction.
#
# ***************************** LICENSE END ************************************

function eps(level,param,date,area,tronc,steps)

	# 33 members, for one or several steps/dates
	# the result is run1(step1/date1,step2...),run2(step1,step2...),...

	cs=1
	cd=1

	if type(steps)='list' then
		if 'to' in steps then
			cs=((steps[3]-steps[1])/12)+1
		else
			cs=count(steps)
		end if
	end if

	if type(date)='list' then
		if 'to' in date then
			cd=date(date[3])-date(date[1])+1
		else
			cd=count(date)
		end if
	end if

	# ensemble control (0)
	runs = retrieve(
			type	:	"cf",
			stream	:	"ef",
			levelist	:	level,
			param	:	param,
			date	:	date,
			step	:	steps,
			resol	:	tronc,
			area	:	area,
			grid	:	[2.5,2.5]
			)

	# perturbed fcsts, 1 to 32
	ptbd = retrieve(
			type	:	"pf",
			stream	:	"ef",
			levelist	:	level,
			param	:	param,
			date	:	date,
			step	:	steps,
			ensemble	:	[1,"to",32],
			resol	:	tronc,
			area	:	area,
			grid	:	[2.5,2.5]
			)

	for i = 1 to 32 do
		for k = 0 to (cs*cd)-1 do
			runs = runs & ptbd[i+32*k]
		end for
	end for

	return runs

end eps
