// (C)2007 Attila Kovacs <attila@submm.caltech.edu>

import java.io.*;
import java.text.*;
import java.util.*;

public class ASCIIIntegral {
    

    public static void main(String[] args) {
	System.err.println();
	System.err.println("  ASCIIIntegral -- Calculate spectra for Labview time-series.");
	System.err.println("                   2006 Attila Kovacs <attila@submm.caltech.edu> <akovacs@mpifr-bonn.mpg.de>");	
	System.err.println();

	if(args.length == 0) usage();

	int channels = -1;	

	try { 
	    BufferedReader in = new BufferedReader(new FileReader(args[0]));
	    int index = args[0].lastIndexOf('.');
	    String fileNameStem = index > 0 ? args[0].substring(0, index) : args[0];
	    String fileNameExt = index > 0 ? args[0].substring(index) : "";
	    double fromFreq = Double.parseDouble(args[1]);
	    double toFreq = Double.parseDouble(args[2]);	    
	    String line;
	    double df = 0.0;

	    Vector buffer = new Vector();

	    System.err.println("  Reading data.");
	    while((line = in.readLine()) != null) {
		StringTokenizer tokens = new StringTokenizer(line);
		if(channels <= 0) {
		    channels = tokens.countTokens() - 1;
		    System.err.println("  " + channels + " channels found.");
		    df = Double.parseDouble(tokens.nextToken());
		}
		else {
		    tokens.nextToken();
		    double[] entry = new double[channels];
		    for(int i=0; i<entry.length; i++)
			entry[i] = Double.parseDouble(tokens.nextToken());
		    buffer.add(entry);
		}
	    }

	    double[][] spectrum = new double[channels][buffer.size()];
	    for(int t=0; t<buffer.size(); t++) {
		double[] entry = (double[]) buffer.get(t);
		for(int c=0; c<channels; c++) spectrum[c][t] = entry[c];
	    }

	    DecimalFormat e3 = new DecimalFormat("0.000E0");

	    System.err.println("  Measuring averages from " + fromFreq + " to " + toFreq + " Hz.");
	    
	    int fmin = (int) Math.ceil(fromFreq / df);
	    int fmax = (int) Math.floor(toFreq / df);
	    
	    /*
	    int[] amps = new int[channels];
	    for(int q=0, i=1; q<4; q++) for(int a=0; a<16; a++, i++) {
		int pins = LabocaPixel.ampChannels[q][a].length;
		for(int pin=0; pin<pins; pin++)
		    amps[LabocaPixel.ampChannels[q][a][pin]] = i;
	    }
	    */

	    PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(fileNameStem + ".int")));
	    
	    out.println("# PSD Integrals " + fromFreq + "--" + toFreq + " Hz."); 
	    out.println("# ");
	    out.println("# Ch\tCable\tAmp\tPSD Int.");

	    for(int c=0; c<channels; c++) {
		double integral = 0.0;

		for(int f=fmin; f<fmax; f++) integral += spectrum[c][f];
		out.println((c+1) + "\t" + "\t"
			    + e3.format(integral/df/(fmax-fmin)));
       	    }
	
	    out.flush();
	    out.close();

	    System.err.println();
	}
	catch(IOException e) { System.err.println("  " + e.getMessage()); }
    }

    public static void usage() {
	
	System.out.println("  Usage: integral <data-file> from to <window-size> [window-type]");
	System.out.println();
	System.out.println("       <data-file>    The ASCII spectrum file.");
	System.out.println("       from           Integrate spectrum from this frequency (Hz).");
	System.out.println("       to             Integrate spectrum to this frequency (Hz).");	
	System.out.println();
	System.exit(0);
    }
}
