package leigh.astro.astrometrics;

import static java.lang.Math.cos;
import static java.lang.StrictMath.pow;
import static java.lang.StrictMath.sqrt;

/**
 * Provides a method for estimating the airmass of an observation.
 *
 * @author Jack Martin
 */
public class AirMass {
    /**
     * Calculate the airmass as an index, where 1 is equal to the airmass directly at the zenith at sea level at
     * standard pressure.
     *
     * @param altDeg The observer horizontal altitude for the observation, in degrees.
     * @return The airmass index.
     */
    public static double airMass(double altDeg) {

        double zenithRad = Math.toRadians(90 - altDeg);

        double seaLevel = 1 / ((cos(zenithRad) + (0.50572 * (Math.pow((96.07995 - zenithRad), -1.6364)))));

        return seaLevel;
    }

    /**
     * Calculate airmass as an index, adjusted for observers elevation above sea level. Airmass directly at
     * zenith can be less than one.
     *
     * @param altDeg The observer horizontal altitude for the observation, in degrees.
     * @param elevation The observer's elevation above sea level in kilometers.
     * @return The airmass index.
     */

    public static double airMass(double altDeg, double elevation) {

        double re = 6367.5;
        double yatm = 100;
        double z = Math.toRadians(90 - altDeg);

        double airMass = sqrt(pow(((re + elevation) / yatm), 2) * pow(StrictMath.cos(z), 2) + (2 * re) / pow(yatm, 2) *
                (yatm - elevation) - pow((elevation / yatm), 2) + 1) - (re + elevation) / yatm * StrictMath.cos(z);

        return airMass;
    }
}
