#!/usr/bin/env python3
# reference.py: the independent numbers the page's engine is held against.
#   pip install gsw seawater CoolProp && python3 reference.py
# Writes reference.json: for depths 0 to 11,000 m, pressure (TEOS-10 gsw.p_from_z, and
# Saunders 1981 via seawater.pres), how much a parcel of seawater shrinks under that pressure
# (TEOS-10 gsw.rho, and EOS-80 seawater.dens, which the page's engine re-implements in JS), and
# how much denser air gets (CoolProp's real-gas equation of state for air, not Boyle's law).
import json, warnings
warnings.filterwarnings('ignore')
import gsw, seawater as sw, CoolProp.CoolProp as CP

SA, SP, CT, T, LAT = 35.165, 35.0, 2.0, 2.0, 30.0
rows = []
for z in range(0, 11001, 500):
    p = float(gsw.p_from_z(-z, LAT))
    teos = 1 - float(gsw.rho(SA, CT, 0) / gsw.rho(SA, CT, p))
    # EOS-80 at the same inputs the page's engine uses: Saunders pressure, and 2 degC on the IPTS-68
    # scale (the seawater package takes ITS-90 and converts, T68 = 1.00024 * T90)
    ps = float(sw.pres(z, LAT)); t90 = T / 1.00024
    eos80 = 1 - float(sw.dens(SP, t90, 0) / sw.dens(SP, t90, ps))
    pabs = p * 1e4 + 101325
    air = CP.PropsSI('D', 'T', 275.15, 'P', pabs, 'Air') / CP.PropsSI('D', 'T', 275.15, 'P', 101325, 'Air')
    rows.append(dict(depth_m=z, p_dbar_gsw=round(p, 2), p_dbar_saunders=round(float(sw.pres(z, LAT)), 2),
                     water_shrink_teos10=round(teos, 6), water_shrink_eos80_seawater_py=round(eos80, 6),
                     air_density_ratio_coolprop=round(air, 2)))
json.dump(dict(generated_by="research/how-deep-can-fish-live/reference.py",
               conditions="S_P=35 (S_A=35.165 g/kg), 2 degC, latitude 30; air at 2 degC (275.15 K)",
               libraries={"gsw": gsw.__version__, "seawater": sw.__version__,
                          "CoolProp": CP.get_global_param_string("version")},
               rows=rows), open('reference.json', 'w'), indent=1)
print(len(rows), 'rows')
