
package org.mbari.vdm;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.mbari.vdm.expd.DimensionParts;
import org.mbari.vdm.expd.NetCdfCreator;

import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;

public class NetcdfSumAndReduce {
    
    

    /**
     * This is a very simple algorithm to be used on x/y grids with one data variable. 
     * The reduction of dimension resolution is only
     * allowed to for multiples of the current resolution. This allows for a simple
     * algorithm that basically drops lines in the current grid. Here's a pictorial
     * example for a 6x6 grid with a xy scale factor of 2x2:
     * 
     * _____________     ____________
     * |_|_|_|_|_|_|     |   |   |   |
     * |_|_|_|_|_|_|     |___|___|___|
     * |_|_|_|_|_|_|  => |   |   |   |
     * |_|_|_|_|_|_|     |___|___|___|
     * |_|_|_|_|_|_|     |   |   |   |
     * |_|_|_|_|_|_|     |___|___|___|
     * 
     * 
     * For grids with an odd number of cells, the last row/column of cells will be ignored.
     * So, a 5x5 grid with a scale factor of 2x2 will result in a 2x2 grid.
     * 
     * All z-values from a group of cells will be summed and input into the new cell
     * 
     * @param file
     * @param xInc
     * @param yInc
     * @throws IOException 
     */
    public static void sumAndReduce(NetcdfFile file, String newFileName, int xFactor, int yFactor) throws IOException {
        List dimensions = file.getRootGroup().getDimensions();
        List variables = file.getRootGroup().getVariables();
        Variable theVariable = null;
        for (Iterator iter = variables.iterator(); iter.hasNext();) {
            Variable element = (Variable) iter.next();
            //the latitude and longitude may show up as variables, but they'll only have
            //themselves listed as a dimension.
            if(element.getDimensions().size() > 1){
                if(theVariable != null){
                    System.out.println("Warning: More than one variable found in nc file");
                }
                theVariable = element;
            }
        }
        DimensionParts latitude = NetCdfCreator.getDimensionArray(file, "latitude");
        DimensionParts longitude = NetCdfCreator.getDimensionArray(file, "longitude");
        double[] latArray = (double[]) latitude.array.copyTo1DJavaArray();
        double[] lonArray = (double[]) longitude.array.copyTo1DJavaArray();
        for(int i = 0; i < latArray.length; i += yFactor){
            for(int j = 0; j < lonArray.length; j += xFactor){
                
            }
        }
    }
    
    
    public static void bin(NetcdfFile file, File newFileName, double newResolutionInMeters){
        
    }

    public static void main(String[] args) {
        try {
            NetcdfFile file = NetcdfFile.open(NetcdfSumAndReduce.class.getResource(
                "/grids/vesicomyidae.nc").getFile().toString());
            NetcdfSumAndReduce.sumAndReduce(file, "lowres_vesicomyidae.nc", 2, 2);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}
