/*******************************************************************************
 * Copyright (c) 2010 Attila Kovacs <attila_kovacs@post.harvard.edu>.
 * All rights reserved. 
 * 
 * This file is part of crush.
 * 
 *     crush is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     crush is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with crush.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Attila Kovacs <attila_kovacs@post.harvard.edu> - initial API and implementation
 ******************************************************************************/
// Copyright (c) 2009 Attila Kovacs 

package crush.sourcemodel;

import java.text.*;


import util.*;

public abstract class Region {
	SkyMap map;
	String id;
	String comment = "";
	
	public static int counter = 1;
	
	protected int fromi, fromj, toi, toj;
	
	public Region(SkyMap map) { this.map = map; id = "[" + (counter++) + "]"; }
	
	public Region(SkyMap map, String line) throws ParseException { this.map = map; parse(line); }
	
	public SkyMap getMap() { return map; }
	
	protected abstract void getLimits();
	
	public abstract boolean isInside(double i, double j);	
	
	public void clear() {
		getLimits();
		for(int i=fromi; i<=toi; i++) for(int j=fromj; j<=toj; j++) map.data[i][j] = 0.0;
	}
	
	public double getAverage() {
		getLimits();
		double ave = 0.0;
		int n = 0;

		for(int i=fromi; i<=toi; i++) for(int j=fromj; j<=toj; j++) 
			if(isInside(i, j) && map.flag[i][j] == 0) { ave += map.data[i][j]; n++; }

		ave /= n;
		return ave;
	}
	
	public DataPoint getFlux() {
		getLimits();
		double flux = 0.0, var = 0.0;

		double A = 1.0 / (map.getPointsPerSmoothingBeam() * map.janskyPerBeam.value);
		//double level = getLevel();

		for(int i=fromi; i<=toi; i++) for(int j=fromj; j<=toj; j++)
			if(isInside(i, j)) if(map.flag[i][j] == 0) {
				//flux += signal[i][j] - level;
				flux += map.data[i][j];
				var += 1.0 / map.weight[i][j];
			}
		
		return new DataPoint(A * flux, A * Math.sqrt(var));
	}
	
	public double getInsideLevel() {
		getLimits();
		double sum = 0.0, sumw = 0.0;
		for(int i=fromi; i<=toi; i++) for(int j=fromj; j<=toj; j++)
			if(isInside(i, j)) if(map.flag[i][j] == 0) {
				sum += map.weight[i][j] * map.data[i][j];
				sumw += map.weight[i][j];
			}
		return sum / sumw;			
	}
	
	public double getRMS() {
		getLimits();
		double var = 0.0;
		int n = 0;
		double level = getInsideLevel();

		for(int i=fromi; i<=toi; i++) for(int j=fromj; j<=toj; j++)
			if(isInside(i, j)) if(map.flag[i][j] == 0) {
				double value = map.data[i][j] - level;
				var += value * value;
				n++;
			}
		var /= (n-1);
		
		return Math.sqrt(var) / map.janskyPerBeam.value;
	}
	

	public double getMeanNoise() {
		getLimits();
		double var = 0.0;
		int n = 0;

		for(int i=fromi; i<=toi; i++) for(int j=fromj; j<=toj; j++)
			if(isInside(i, j)) if(map.flag[i][j] == 0) {
				double rms = map.getRMS(i,j);
				var += rms * rms;
				n++;
			}
		var /= (n-1);
		
		return Math.sqrt(var) / map.janskyPerBeam.value;
	}
	
	public double getMeanExposure() {	
		getLimits();
		double sum = 0.0;
		int n = 0;

		for(int i=fromi; i<=toi; i++) for(int j=fromj; j<=toj; j++)
			if(isInside(i, j)) if(map.flag[i][j] == 0) {
				sum += map.count[i][j];
				n++;
			}
		
		return n > 0 ? sum / n : 0.0;
	}
	
	public void flag() { flag(1); }
	
	public void flag(int pattern) {
		getLimits();
		for(int i=fromi; i<=toi; i++) for(int j=fromj; j<=toj; j++) if(isInside(i, j)) map.flag[i][j] |= pattern;
	}
	
	public void unflag(int pattern) {
		getLimits();
		for(int i=fromi; i<=toi; i++) for(int j=fromj; j<=toj; j++) if(isInside(i, j)) map.flag[i][j] &= ~pattern;
	}
	
	public abstract void parse(String line) throws ParseException;
	
}
