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


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

import nom.tam.fits.*;


public class APEXSZScan extends LabocaScan {
	
	public APEXSZScan() {
		instrumentName = "aszca";
	    rcpFile = instrumentName + File.separator + "master.rcp";
	    pixelDataFile = instrumentName + File.separator + "pixel.dat";
	    channelTemplate = new APEXSZPixel(-1);
	}
	
	public LabocaScan[] readScanDirectory(LabocaScan common, String dir, String ext) throws IOException, FitsException, HeaderCardException {
		int subscans = common.readScanInfo(dir + File.separator + "SCAN" + ext);
		common.readPar(dir + File.separator + "BOLOSZ-SZACBE-FEBEPAR" + ext);
		
		Vector<APEXSZScan> buffer = new Vector<APEXSZScan>();

		for(int i=0; i<subscans; i++) {
			try {
				APEXSZScan subscan = (APEXSZScan) common.clone();

				System.err.println(" Subscan {" + (i+1) + "}:");
				subscan.subscanNo = i;
				subscan.readDataPar(dir + File.separator + (i+1) + File.separator + "BOLOSZ-SZACBE-DATAPAR" + ext);
				subscan.readData(dir + File.separator + (i+1) + File.separator + "BOLOSZ-SZACBE-ARRAYDATA-1" + ext);
				subscan.readMonitor(dir + File.separator + (i+1) + File.separator + "MONITOR" + ext);
				subscan.validate();

				buffer.add(subscan);
			}
			catch(Exception e) {
				System.err.println(" ERROR: " + e.getMessage());
				e.printStackTrace();
			}
		}

		APEXSZScan[] set = new APEXSZScan[buffer.size()];
		for(int i=0; i<buffer.size(); i++) set[i] = buffer.get(i);
		subscans = buffer.size();

		return set;
	}

	
	public void yOrder() {
		for(int g=0; g<regionChannels.length; g++) {
			Vector<LabocaPixel> selection = new Vector<LabocaPixel>();
			for(int k=0; k<regionChannels[g].length; k++) selection.add(channel[regionChannels[g][k]]);
			
			Comparator<LabocaPixel> pinorder = new Comparator<LabocaPixel>() {
				public int compare(LabocaPixel c1, LabocaPixel c2) {
					return Double.compare(c1.position.y, c2.position.y);
				}				
			};
			
			Collections.sort(selection, pinorder);
			
			for(int k=0; k<regionChannels[g].length; k++) regionChannels[g][k] = selection.get(k).index;		
		}
		System.err.println("   Regions rearranged to y-order.");	
	}
	

	/*
	public void readMonitor(String fileName)  throws IOException, FitsException, HeaderCardException {
		Fits fits = new Fits(new File(fileName), fileName.endsWith(".gz"));

		BinaryTableHDU hdu = (BinaryTableHDU) fits.getHDU(1);
		readMonitor(hdu);
	}
	*/
	
	
	public void readMonitor(String fileName)  throws IOException, FitsException, HeaderCardException {
		
	}
	
	public void readMonitor(BinaryTableHDU hdu)  throws IOException, FitsException, HeaderCardException {
	}
	
	
	/*
	public void readMonitor(BinaryTableHDU hdu)  throws IOException, FitsException, HeaderCardException {
		System.err.print("   Reading Monitor Table...");
		Header header = hdu.getHeader();
		hdu.getData().getData();
		
		int iMJD = hdu.findColumn("MJD");
		int iLABEL = hdu.findColumn("MONPOINT");
		int iVALUE = hdu.findColumn("MONVALUE");

		int n = header.getIntValue("NAXIS2");
		for(int i=0; i<n; i++) {
			Object[] row = hdu.getRow(i);
			if(((String) row[iLABEL]).endsWith("USTAGETEMP")) {
				System.err.println(row[iLABEL] + "\t" + ((double[]) row[iVALUE])[0]);
			}
		}

		
	}
	*/
	
}
