from pathlib import Path
import json,html,math
P=Path(__file__).resolve().parent;D=json.loads((P/'c01-short-gas.json').read_text());S=['<svg xmlns="http://www.w3.org/2000/svg" width="1120" height="780" viewBox="0 0 1120 780" role="img" aria-label="Bounded dimensionless short-line gas curve comparison"><title>Bounded dimensionless short-line gas curve comparison</title><rect width="100%" height="100%" fill="#fbfcfd"/><style>text{font-family:Arial,sans-serif;fill:#182d3b}</style>']
def txt(x,y,t,size=16):S.append(f'<text x="{x}" y="{y}" font-size="{size}">{html.escape(str(t))}</text>')
X=lambda x:95+740*x/.7;Y=lambda y:515-390*y
for x in [0,.1,.2,.3,.4,.5,.6,.7]:S.append(f'<line x1="{X(x)}" y1="105" x2="{X(x)}" y2="515" stroke="#d6e0e5"/>');txt(X(x)-10,540,f'{x:g}',13)
for y in [0,.2,.4,.6,.8,1]:S.append(f'<line x1="95" y1="{Y(y)}" x2="835" y2="{Y(y)}" stroke="#d6e0e5"/>');txt(53,Y(y)+4,f'{y:g}',13)
colors=['#a94e44','#327099','#4f8750','#8d62a0','#ac7b2f','#252a35']
for i,c in enumerate(D['curves']):
 dash=' stroke-dasharray="8 6"' if 'dotted' in c['convention'] else ''
 S.append('<polyline fill="none" stroke="'+colors[i]+'" stroke-width="2.5"'+dash+' points="'+' '.join(f'{X(t["x"]):.2f},{Y(t["pressure_ratio_P2_P1"]):.2f}' for t in c['samples'])+'"/>');S.append(f'<line x1="850" y1="{140+i*37}" x2="866" y2="{140+i*37}" stroke="{colors[i]}" stroke-width="3"'+dash+'/>');txt(870,145+i*37,f'fL/D = {c["fL_over_D_label"]}'+(' (dotted)' if dash else ''),14)
for k,dash in [('k1_ratio',''),('k1_4_ratio',' stroke-dasharray="8 6"')]:S.append('<polyline fill="none" stroke="#182d3b" stroke-width="2"'+dash+' points="'+' '.join(f'{X(t["x"]):.2f},{Y(t[k]):.2f}' for t in D['critical_relations']['data'])+'"/>')
txt(35,35,'Short-line gas curves retain their dimensionless basis',24);txt(95,80,'Outlet ratio P₂/P₁',17);txt(240,581,'x = (G/P₁) √(P₁ V₁ / g), as printed',18);txt(870,396,'Solid: isothermal k = 1',14);txt(870,425,'Dotted: k = 1.4',14);txt(870,454,'No inferred heat regime',14)
txt(95,623,'Critical lines: solid Pcr/P₁ = x; dotted Pcr/P₁ = 0.7715 x.',17);txt(95,655,'Samples only within saved spans; linear joins, no family interpolation or extrapolation.',16);txt(95,686,'Diagram: P₁ reservoir, P₀ entrance, P₂ outlet. Text calls P₁ inlet; P₀ is undefined.',16);txt(95,717,'Do not set P₀ = P₁. Dimensional G and fL/D require an adopted station, unit and friction basis.',16);txt(95,748,'Estimated reading allowances: ±0.015 pressure ratio and ±0.005 x; not acceptance tolerances.',16)
S.append('</svg>');(P/'c01-short-gas.svg').write_text('\n'.join(S)+'\n')
