// import statements
import static java.lang.Math.*;  // mathematics (degrees<-->radians)
import gov.sandia.gnem.slbmjni.SlbmInterface;  // RSTT library
import gov.sandia.gnem.slbmjni.SLBMException;  // RSTT error catching


// main class
class java_example
{
    
    // main method
    public static void main(String args[])
    {

        // wrap everything in a `try-catch` block since there are checked exceptions
        try
        {

            // java -Djava.library.path=../lib
            System.loadLibrary("slbmjni");

            // declare the relative path to the model file we're going to be using
            String modelPath = "../models/pdu2020012Du.geotess";

            // choose an arbitrary phase (Pn, Pg, Sn, or Lg)
            String phase = "Pn";

            // arbitrary source (Meteor Crater, AZ)
            double srcLatDeg =   35.0274;  // latitude (degrees)
            double srcLonDeg = -111.0228;  // longitude (degrees)
            double srcDepKm  =    1.2345;  // depth (km)

            // arbitrary receiver (Albuquerque, NM)
            double rcvLatDeg =   35.1053;  // latitude (degrees)
            double rcvLonDeg = -106.6294;  // longitude (degrees)
            double rcvDepKm  =   -1.6000;  // depth (km)

            // convert lat/lon from degrees to radians
            double srcLatRad = toRadians(srcLatDeg);
            double srcLonRad = toRadians(srcLonDeg);
            double rcvLatRad = toRadians(rcvLatDeg);
            double rcvLonRad = toRadians(rcvLonDeg);

            // instantiate an RSTT object
            SlbmInterface slbm = new SlbmInterface();

            // load velocity model
            slbm.loadVelocityModel(modelPath);

            // create a great circle from source to the receiver
            slbm.createGreatCircle(phase,
                srcLatRad, srcLonRad, srcDepKm,
                rcvLatRad, rcvLonRad, rcvDepKm);

            // get the distance and travel time from source --> receiver
            double distRad, distDeg, travelTimeSec;  // instantiate variables
            distRad       = slbm.getDistance();      // compute distance (rad) and store its value
            travelTimeSec = slbm.getTravelTime();    // compute travel time (sec) and store its value
            distDeg       = toDegrees(distRad);      // convert radians --> degrees

            // get the travel time uncertainty
            double travelTimeUncertSec,    // path-dependent travel time uncertainty (sec)
                   travelTime1DUncertSec;  // old-style 1D travel time uncertainty (sec)
            travelTimeUncertSec   = slbm.getTravelTimeUncertainty();    // compute uncertainty
            travelTime1DUncertSec = slbm.getTravelTimeUncertainty1D();  // compute 1D uncertainty

            // print our results
            System.out.printf("\n");
            System.out.printf("Java Example:\n");
            System.out.printf("--------------------------------\n");
            System.out.printf("Distance:          %9.4f deg\n", distDeg);
            System.out.printf("Travel Time:       %9.4f sec\n", travelTimeSec);
            System.out.printf("Uncertainty:       %9.4f sec\n", travelTimeUncertSec);
            System.out.printf("Uncertainty (1D):  %9.4f sec\n", travelTime1DUncertSec);

        }

        // catch possible exceptions
        catch (SLBMException e)
        {

            // print info about the exception
            e.printStackTrace();

            // return with an error
            System.exit(1);
            
        }

        // return successfully
        System.exit(0);

    }

}
