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

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

public class ASCIICorrelated {
    

    public static void main(String[] args) {
	int[] bad = { 18, 22, 24, 29, 30, 32, 33, 34, 35, 37, 40, 46, 47, 53, 59, 60,
		      64, 66, 69, 74, 87, 88, 117, 118, 120, 124, 125, 130, 132, 135,
		      142, 144, 145, 146, 151, 152, 158, 170, 171, 182, 188, 189, 190,
		      193, 195, 200, 201, 204, 208, 210, 214, 222, 224, 226, 229, 247,
		      250, 252, 254, 260, 272, 277, 280, 284, 293, 295, 299, 300, 302,
		      304, 306, 309, 311, 318};
	
	
	System.err.println();
	System.err.println("  ASCIICorrelated -- Caluclate 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;
	
	boolean[] isBad = new boolean[320];
	for(int b=0; b<bad.length; b++) isBad[bad[b]-1] = true;
	
	try { 
	    BufferedReader in = new BufferedReader(new FileReader(args[0]));

	    System.out.println(in.readLine());
	    String line;
	    
	    while((line = in.readLine()) != null) {
		StringTokenizer tokens = new StringTokenizer(line);
		if(channels <= 0) channels = tokens.countTokens();
		else if(channels == tokens.countTokens()) {
		    double correlated = 0;
		    for(int c=0; c<channels; c++) if(!isBad[c]) 
			correlated += Double.parseDouble(tokens.nextToken());
		    System.out.println(correlated / (channels - bad.length));
		}
	    }
	    
	    System.err.println();
	}
	catch(IOException e) { System.err.println("  " + e.getMessage()); }
    }
    
    public static void usage() {
	
	System.out.println("  Usage: correlated <laboca-data-file>");
	System.out.println();
	System.out.println("       <data-file>    The Labview-style ASCII time series file.");

    }
}
