package org.mbari.vdm.expd.test;

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

import junit.framework.TestCase;

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

import ucar.nc2.NetcdfFile;
import ucar.unidata.geoloc.LatLonRect;


public class NetCdfCreatorTest extends TestCase {
    private NetcdfFile montereyNc;
    
    protected void setUp() throws Exception {
        File newNcFile = new File( SearchableTreePanel.class.getResource("/grids")
            .getFile(), "monterey.nc");
        montereyNc = NetcdfFile.open(newNcFile.getAbsolutePath());
        super.setUp();
    }

    protected void tearDown() throws Exception {
        montereyNc.close();
        super.tearDown();
    }

    /*
     * Test method for 'org.mbari.vdm.expd.NetCdfCreator.getNcBounds(NetcdfFile)'
     */
    public void testGetNcBounds() {
        LatLonRect rect = null;
        try{
             rect = NetCdfCreator.getNcBounds(montereyNc);
        }catch(IOException ioe){
            ioe.printStackTrace();
            fail("IO Exception");
        }
        assertEquals(rect.getUpperLeftPoint().getLatitude(), 37.058820039803244);
        assertEquals(rect.getUpperLeftPoint().getLongitude(),-122.50701057157069); 
        assertEquals(rect.getLowerRightPoint().getLatitude(), 36.440722047047);
        assertEquals(rect.getLowerRightPoint().getLongitude(), -121.78050533395772);
    }

    public void testGetDimensionArray(){
        DimensionParts dim = null;
        try {
            dim = NetCdfCreator.getDimensionArray(montereyNc, "latitude");
            assertNotNull(dim);
            assertTrue(dim.array.getShape()[0] > 2000);
            dim = NetCdfCreator.getDimensionArray(montereyNc, "longitude");
            assertNotNull(dim);
        } catch (IOException e) {
            e.printStackTrace();
            fail("IO Exception");
        }
        try{
            NetCdfCreator.getDimensionArray(montereyNc, "Nonexistent");
            fail();
        }catch (Exception e){
            //do nothing, this is the correct behavior
        }
    }
}
