"""Lab to Code — What the sensors in a refinery actually measure.

Two of the most common transmitters in a plant turn a pressure into something else:
- Level (LT): a differential-pressure transmitter reads the weight of the liquid column. h = dP / (rho * g)
- Flow (FT): an orifice plate creates a pressure drop. Q = Cd * A * sqrt(2 * dP / rho)
Typical values: crude oil density ~850 kg/m3; orifice discharge coefficient Cd ~0.61.
"""
from math import pi, sqrt

G = 9.81            # m/s^2
RHO_CRUDE = 850     # kg/m^3


def level_from_dp(dp_kpa, rho=RHO_CRUDE):
    return dp_kpa * 1000 / (rho * G)                     # meters of liquid


def flow_from_orifice(dp_kpa, d_mm, rho=RHO_CRUDE, cd=0.61):
    area = pi * (d_mm / 1000) ** 2 / 4                   # m^2
    q = cd * area * sqrt(2 * dp_kpa * 1000 / rho)        # m^3/s
    return q * 3600                                      # m^3/h


for dp in (25, 50):
    print(f"LT: {dp} kPa → {level_from_dp(dp):.1f} m of crude")
for dp in (10, 40):
    print(f"FT: {dp} kPa across a 100 mm orifice → {flow_from_orifice(dp, 100):.0f} m3/h")
