Java – How to calculate sea level pressure for a given altitude and pressure

How to calculate sea level pressure for a given altitude and pressure… here is a solution to the problem.

How to calculate sea level pressure for a given altitude and pressure

I’m writing a dial that shows height. Use SensorManager.getAltitude(seaLevelPressure, currentPresure) to calculate altitude.

But in order to initialize it, I need pressure at sea level. Unfortunately, there is no SensorManager.getSeaLevelPressure(currentPressure, currentAltitude).

To do this, I found the following formula (see http://rechneronline.de/barometer/).

private float calcSeaPressure(float pressure, int altitude) {
  float temperature = 9 + 273.15f;
  float tempGradient = 0.0065f;

float v3 = temperature + tempGradient * altitude;
  float sealevelPressure = (float) (pressure / Math.pow((1 - tempGradient * altitude / v3), .03416f / tempGradient));
  sealevelPressure = (float) Math.round(sealevelPressure * 100) / 100;
  return sealevelPressure;
}

But it seems that this algorithm does not fit well with the algorithm used in SensorManager.getAltitude. If I do:

public void setCurrentAltitude(int currentAltitude) {
  sealLevelPressure = calcSealevel(currentAltitude,currentPresure);
  altitude = SensorManager.getAltitude(seaLevelPressure, currentPresure)
}

The calculated altitude is different from the given currentAltitude. For smaller values (<1000m), the difference is acceptable. But for example, 4000m is a difference of 250m, which is unacceptable.

Now my question is: how do I have to calculate the sea level so that setCurrentAltitude() doesn’t report a different value?

Or do you know of other Java classes that can be used for this?
Remember that these values should be calculated!

Thanks

Solution

If you’re calibrating from a height (after all you have the setCurrentAltitude method), then this is the technique:

public void setCalibrationAltitudeAndPressure(float calibrationAltitude, float currentPressure) {
   this.calibrationAltitude = calibrationAltitude;
   this.calibrationPressure = currentPressure;
}

public float getCurrentAltitude(float currentPressure) {
    float altitudeDifference =
            sensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, currentPressure) -
            sensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, calibrationPresure);
    return calibrationAltitude + altitudeDifference;
}

Now you don’t need to care about sea level pressure.

Calibration

You need a calibrated height, and for this you can use GPS. Its height is unlikely to be that accurate (see Android: How to get accurate.) altitude? ), but you can get altitude from the service using latitude and longitude: https://developers.google.com/maps/documentation/elevation/ But this is not good for people who use the app on airplanes!

However, if you have prepared a service for this, it is better to get accurate sea level pressure from one: http://www.worldweatheronline.com/api/marine-weather-api.aspx

Related Problems and Solutions