package test;

import java.io.File;
import java.io.IOException;

import org.mbari.vdm.expd.NetCdfCreator;

import ucar.ma2.InvalidRangeException;
import ucar.nc2.Dimension;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;
import ucar.unidata.geoloc.LatLonPoint;
import ucar.unidata.geoloc.LatLonPointImpl;
import ucar.unidata.geoloc.LatLonRect;


public class NetcdfTest {

    public static void testNetcdfRead() throws IOException{
        File baseNcFile = new File( SearchableTreePanel.class.getResource("/grids").getFile(),
        "hrbath.nc");
        NetcdfFile ncFile = NetcdfFile.open(baseNcFile.getAbsolutePath());
        Dimension latDimension = ncFile.getRootGroup().findDimension("latitude");
        Variable latVariable = (Variable) latDimension.getCoordinateVariables().get(0);
        Dimension lonDimension = ncFile.getRootGroup().findDimension("longitude");
        Variable lonVariable = (Variable) lonDimension.getCoordinateVariables().get(0);
        int lastLatIndex = latVariable.getShape()[0] - 1;
        int lastLonIndex = lonVariable.getShape()[0] - 1;
        try {
            String secSpec = "1:"+ lastLatIndex + ":1";
            double[] latArray = (double[]) latVariable.read(secSpec).get1DJavaArray(double.class);
            secSpec = "1:"+ lastLonIndex + ":1";
            double[] lonArray = (double[]) lonVariable.read(secSpec).get1DJavaArray(double.class);
            LatLonPoint left = new LatLonPointImpl(latArray[0], lonArray[0]);
            LatLonPoint right = new LatLonPointImpl(latArray[1], lonArray[1]);
            LatLonRect rect = new LatLonRect(left, right);
            System.out.println("Rect bounds are: " + rect.getUpperLeftPoint() + ", " + rect.getLowerRightPoint());
        } catch (InvalidRangeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    
    public static void main(String[] args){
        try {
            NetcdfTest.testNetcdfRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
