Skip to content
Snippets Groups Projects
Commit 5222f8b8 authored by rink's avatar rink
Browse files

add press to altitude

parent 2b92b4b4
No related branches found
No related tags found
No related merge requests found
import numpy as np
from metpy.units import units
from metpy.calc import thickness_hydrostatic
class MyGenericException(Exception):
def __init__(self, message):
self.message = message
def haversine_np(lon1, lat1, lon2, lat2):
......@@ -53,3 +60,49 @@ def get_breaks(t, threshold):
idxs = np.nonzero(d > threshold)
return idxs
def pressure_to_altitude(pres, temp, prof_pres, prof_temp, sfc_pres, sfc_temp, sfc_elev):
if not np.all(np.diff(prof_pres) > 0):
raise MyGenericException("target pressure profile must be monotonic increasing")
if pres < prof_pres[0]:
raise MyGenericException("target pressure less than top of pressure profile")
if temp is None:
temp = np.interp(pres, prof_pres, prof_temp)
i_top = np.argmax(np.extract(prof_pres <= pres, prof_pres)) + 1
pres_s = prof_pres.tolist()
temp_s = prof_temp.tolist()
pres_s = [pres] + pres_s[i_top:]
temp_s = [temp] + temp_s[i_top:]
prof_pres = np.array(pres_s)
prof_temp = np.array(temp_s)
i_bot = prof_pres.shape[0] - 1
if sfc_pres > prof_pres[i_bot]:
pres_s = pres_s + [sfc_pres]
temp_s = temp_s + [sfc_temp]
else:
idx = np.argmax(np.extract(prof_pres <= sfc_pres, prof_pres))
if sfc_temp is None:
sfc_temp = np.interp(sfc_pres, prof_pres, prof_temp)
pres_s = prof_pres.tolist()
temp_s = prof_temp.tolist()
pres_s = pres_s[0:idx] + [sfc_pres]
temp_s = temp_s[0:idx] + [sfc_temp]
prof_pres = np.array(pres_s)
prof_temp = np.array(temp_s)
prof_pres = prof_pres * units.hectopascal
prof_temp = prof_temp * units.kelvin
sfc_elev = sfc_elev * units.meter
z = thickness_hydrostatic(prof_pres, prof_temp) + sfc_elev
return z
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment