/******************************** LICENSE ********************************

 Copyright 2007 European Centre for Medium-Range Weather Forecasts (ECMWF)

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at 

    http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 ******************************** LICENSE ********************************/

/*! \file LevelSelection.cc
    \brief Implementation of the Template class LevelSelection.
    
    Magics Team - ECMWF 2004
    
    Started: Tue 9-Mar-2004
    
    Changes:
    
*/

#include "LevelSelection.h"

using namespace magics;

LevelSelection::LevelSelection() 
{
}


LevelSelection::~LevelSelection() 
{
}

/*!
 Class information are given to the output-stream.
*/		
void LevelSelection::print(ostream& out)  const
{
	out << "LevelSelection[";
	doublearray::print(out);
	out << "]";
}
void LevelSelection::thinLevels(int frequency, vector<double>& out) const
{
	double ref = reference();
	int count = 0;
	const_iterator level = this->begin();
	while ( level != end() ) {
		if (*level != ref )
			break;
		level++;
	}
	for (level; level != end(); ++level) {
		if ( count%frequency == 0 )
			out.push_back(*level);
		count++;
	}
	count = 0;
	const_reverse_iterator rlevel = rbegin();
		while ( rlevel != rend() ) {
			if (*rlevel != ref )
				break;
			rlevel++;
		}
	for (rlevel; rlevel != rend(); ++rlevel) {
		if ( count%frequency == 0 )
			out.push_back(*rlevel);
		count++;
	}
}

double LevelSelection::reference() const
{
	return empty() ? -9999: front();
}
