package org.mbari.seepsearch.tools;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.io.*;
import java.text.*;
import java.util.Arrays;
import javax.swing.*;
import org.mbari.util.*;
import org.mbari.seepsearch.grid.*;
import org.mbari.seepsearch.core.RovAltitudeFilter;
import org.mbari.seepsearch.ui.*;
import org.mbari.swing.FileProgressFrame;

/**
 * <p>Generates grids fo each year of ROV data from the data directories on
 * science in one step</p>
 *
 * <p>The Seep Search project is a sub-project of the Continental
 * Margin Fluids and Gases project (200010). It involves the analysis of
 * archived ROV data to obtain understanding of seep distritution in Monterey
 * Bay.</p>
 *
 * <p>The Purpose of this Java package is to intercompare ROV Navigation files,
 * VIMS data on seep community observations and the EM-300 bathymetry. We are
 * looking for locations where the ROV navigation files and the EM-300 data
 * have similar depths between the 2 data sets, which implies that the ROV is
 * observing the sea floor.</p><hr><br>
 *
 * <p>MBARI provides this documentation and code &quot;as is&quot;, with no
 * warranty, express or implied, of its quality or consistency. It is provided
 * without support and without obligation on the part of the Monterey Bay
 * Aquarium Research Institute to assist in its use, correction, modification,
 * or enhancement. This information should not be published or distributed to
 * third parties without specific written permission from MBARI.</p><br>
 *
 * Copyright 2002 MBARI.<br>
 * MBARI Proprietary Information. All rights reserved.<br><hr><br>
 *
 * $Log: RovNavGrid.java,v $
 * Revision 1.2  2002/04/17 21:48:55  brian
 * Adding Vars Database support. Moving to IRovNavigationIterator as the common interface between disparate datasets
 *
 * Revision 1.1  2002/03/05 00:41:10  brian
 * no message
 *
 * Revision 1.4  2002/02/27 23:55:28  brian
 * Modified GridUtil to ignore rov fixes equal to 0 pressure. Other minor bug fixes.
 *
 * Revision 1.3  2002/02/25 22:58:16  brian
 * Bug fixes.
 *
 * Revision 1.2  2002/02/06 00:39:07  brian
 * no message
 *
 * Revision 1.1  2002/01/30 00:23:56  brian
 * Added comments to all classes and methods. Tested the accuracy of AsciiRasterGridReader and AsciiRasterGridWriter. Also checked the the GridUtil was populating the spatial grids correctly and accurately. The data files generated matched up perfectly with the orginal text files. Also implemented an RovAltitudeFilter class to encapsulate different filtering methods.
 *<br><hr><br>
 *
 * @author  : $Author: brian $
 * @version : $Revision: 1.2 $
 */
public class RovNavGrid {

    public RovNavGrid() {
    }

    private void createGrid(double maxAltitude) throws IOException, GridException {

        final boolean isUtm = true;
        int zoneNumber = 10;
        final String inGrid = "s:/brian/projects/200010/gisdata/utm/monterey.asc";
        //final String rovDataPath = "s:/brian/projects/200010/rovdata/";
        final String rovDataPath = "c:/temp/test/";

        final String outPath = "s:/brian/projects/200010/rovdata/grids/ascii";
        NumberFormat nf = new DecimalFormat();
        nf.setMaximumFractionDigits(0);
        final String outPostFix = "_lt" + nf.format(maxAltitude) + "m";

        // Read the input file
        File input = new File(inGrid);
        AsciiNullGridReader reader = new AsciiNullGridReader(input);
        reader.read();
        RasterGrid grid = reader.getGrid();

        // Set the filterparamters
        RovAltitudeFilter filter = new RovAltitudeFilter();
        filter.setMaxAltitude(maxAltitude);
        filter.setMinAltitude(Double.NaN);


        for (int i = 1989; i < 2002; i++) {

            // Get the output file
            //File output = new File(outPath, "rov" + i + outPostFix + ".asc");
            File output = new File(outPath, "rovUtm" + outPostFix + ".asc");
            File dir1 = new File(rovDataPath, i + "");
            File dirVnta = new File(dir1, "vnta");
            File dirTibr = new File(dir1, "tibr");

            File[] filesVnta = null;
            if (dirVnta.exists()) {
                filesVnta = dirVnta.listFiles();
            }

            File[] filesTibr = null;
            if (dirTibr.exists()) {
                filesTibr = dirTibr.listFiles();
            }

            File[] navFiles = null;
            if ((filesVnta != null) & (filesTibr != null)) {
                navFiles = new File[filesVnta.length + filesTibr.length];
                System.arraycopy(filesVnta, 0, navFiles, 0, filesVnta.length);
                System.arraycopy(filesTibr, 0, navFiles, filesVnta.length, filesTibr.length);
            } else if ((filesVnta == null) & (filesTibr != null)) {
                navFiles = filesTibr;
            } else if ((filesVnta != null) & (filesTibr == null)) {
                navFiles = filesVnta;
            }

            FileProgressFrame pf = new FileProgressFrame();
            JProgressBar pb = pf.getJProgressBar();
            pb.setStringPainted(true);
            pb.setMinimum(0);
            pb.setMaximum(navFiles.length + 1);
            pb.setValue(0);
            pf.pack();
            pf.setVisible(true);
            Dimension d;
            Rectangle rect;

            for (int j = 0; j < navFiles.length; j++) {
                pb.setValue(j);
                pb.setString(navFiles[j].getName());
                d = pb.getSize();
                rect = new Rectangle(0, 0, d.width, d.height);
                pb.paintImmediately(rect);
                GridUtil.populateGeoGridWithGeoNavData(grid, navFiles[j], filter,
                    zoneNumber);
            }

            pb.setValue(navFiles.length);
            pb.setString("Writing " + output.getName());
            d = pb.getSize();
            rect = new Rectangle(0, 0, d.width, d.height);
            pb.paintImmediately(rect);

            if (i == 2001) {
                AsciiRasterGridWriter writer = new AsciiRasterGridWriter(output);
                writer.write(grid, 0, 7);
            }
            pb.setValue(navFiles.length + 1);
            pb.setString("");
            pb = null;
            pf.dispose();
            pf = null;
        }

    }

    public static void main(String[] args) {
        RovNavGrid rng = new RovNavGrid();
        try {
            double maxAltitude = Double.parseDouble(args[0]);
            rng.createGrid(maxAltitude);
        } catch (GridException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            System.exit(0);
        }
    }
}