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

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

public class Sensitivity {

    int channels = -1;
    

    public static void main(String[] args) {
	System.err.println();
	System.err.println("  ASCIISpectrum -- 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();

	try { 
	    Sensitivity sensitivity = new Sensitivity(args[0], Integer.parseInt(args[1]), args[2]);
	}
	catch(IOException e) { e.printStackTrace(); }
    }

    public Sensitivity(String hotcoldName, int col, String noiseName) throws IOException {
	double[] span = readData(hotcoldName, col); 
	double[] var = readData(noiseName, 4);

	for(int c=0; c<span.length; c++) {
	    double rms = Math.sqrt(var[c]);
	    System.out.println((c+1) + "\t" + (span[c]/rms - 4.0) + "\t" + span[c] + "\t" + rms);
	}
    }


    public double[] readData(String fileName, int col) throws IOException {
	channels = 320;
	double[] data = new double[channels];

	System.err.println("  Reading data.");

	BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
	String line = null;
	while((line = in.readLine()) != null) {
	    if(line.length() > 0) if(line.charAt(0) != '#') {
		StringTokenizer tokens = new StringTokenizer(line);
		int c = Integer.parseInt(tokens.nextToken()) - 1;
		int column=2;
		while(column < col) { tokens.nextToken(); column++; }
		data[c] = Double.parseDouble(tokens.nextToken());
	    }
	}

	in.close();

	return data;
    }


    public static void usage() {
	
	System.out.println("  Usage: spectrum <responsivities> <col> <noise-int>");
	System.out.println();
	System.out.println("       <responsivities>  The responsivities table.");
	System.out.println("       <col>             The column to use.");
	System.out.println("       <noise-int>       The noise integral table.");
	System.out.println();
	System.exit(0);
    }
}
