Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate sealevel pressure for given altitude and pressure

I am writing a watch-face which displays the altitude. The altitude is calculated with SensorManager.getAltitude(seaLevelPressure, currentPresure).

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

For doing so 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 and the one used in SensorManager.getAltitude do not fit good together. If I do:

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

The calculated altitude is different to the given currentAltitude. For small values (<1000m) the difference is acceptable. But for example 4000m the difference is 250m, which is no longer acceptable.

Now my question: How do I have to calculate the sealevel, so that setCurrentAltitude() does not report different values?

Or do you know about other Java Classes which can be used for this? Keep in mind, the values should be calculated!

Thanks

like image 541
Joachim Fricker Avatar asked Sep 29 '22 15:09

Joachim Fricker


1 Answers

If you are calibrating from an altitude (after all you have 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.

Calibrating

You need a calibration altitude, and you can use GPS for this. It's altitude is not likely to be that accurate (see Android: How to get accurate altitude?), but you could use the lat and long to get altitude from this service: https://developers.google.com/maps/documentation/elevation/ but that's not good for people using the app in planes!

But if you're hitting a service up for that, then might as well get accurate sea-level pressure from a service: http://www.worldweatheronline.com/api/marine-weather-api.aspx

like image 142
weston Avatar answered Oct 03 '22 08:10

weston