import java.util.*;

public class SharcFrame extends SharcHeader {
    float[] readout;
    
    float weight = 1.0F;
    float transmission = 1.0F;
    float velocity = 0.0F;
    float acceleration = 0.0F;

    float offset = 0.0F;

    boolean[] flagged;
    boolean[] masked;

    float[] rowGain;

    public SharcFrame() { super(); }

    public SharcFrame(SharcScan setscan) { super(setscan); }

    public SharcFrame duplicate() {
	SharcFrame copy = (SharcFrame) clone();

	int n = readout.length;

	copy.readout = new float[n];
	copy.flagged = new boolean[n];
	copy.masked = new boolean[n];

	for(int i=0; i<n; i++) {
	    copy.readout[i] = readout[i];
	    copy.flagged[i] = flagged[i];
	    copy.masked[i] = masked[i];
	}

	copy.rowGain = new float[rowGain.length];
	for(int i=0; i<rowGain.length; i++) copy.rowGain[i] = rowGain[i];

	return copy;
    }

    public void init() {
	super.init();
	readout = new float[12*32];
	flagged = new boolean[12*32];
	masked = new boolean[12*32];
	rowGain = new float[12];
	Arrays.fill(rowGain, 1.0F);
    }

    public void calc() {
	super.calc();
	calcTransmission();
	reset();
    }

    public void calcTransmission() {
	transmission = (float) Math.exp( -scan.tau / Math.sin(EL) );
    }

    public void reset() {
	offset = 0.0F;
	calcTransmission();
	for(int bol=0; bol<masked.length; bol++) flagged[bol] = masked[bol];
    }

    public void validate() {
	for(int bol=0; bol<readout.length; bol++)
	    readout[bol] *= -1.0 / ( samples * rowGain[bol/32] );	
	super.validate();
    }

    public void add(SharcFrame frame) {
	super.add(frame);
	for(int bol=0; bol<readout.length; bol++) readout[bol] += frame.readout[bol];
    }

    public void parse(Object[] row) throws IllegalStateException {
	super.parse(row);

	float[][] value = (float[][]) row[0];

	for(int r=0,bol=0; r<12; r++) for(int c=0; c<32; c++,bol++) {
	    readout[bol] += value.length == 12 ? value[r][c] : value[bol/12][bol%12];
	    readout[bol] -= scan.pixel[bol].offset;
	}
    }
}
