import cairosvg,math
W,H=1600,900
def txt(x,y,s,size=16,anchor="middle",weight="normal",fill="#111"):
    return f'<text x="{x}" y="{y}" font-family="Arial,Helvetica,sans-serif" font-size="{size}" text-anchor="{anchor}" font-weight="{weight}" fill="{fill}">{s}</text>'
def leader(x1,y1,x2,y2):
    return f'<line x1="{x1}" y1="{y1}" x2="{x2}" y2="{y2}" stroke="#333" stroke-width="1.5"/><circle cx="{x2}" cy="{y2}" r="3" fill="#333"/>'
def vagues(x,y,w,n,col="#a8c4dc"):
    return "".join(f'<path d="M {x+i*w/n} {y} q {w/n/4} -6 {w/n/2} 0 q {w/n/4} 6 {w/n/2} 0" stroke="{col}" stroke-width="2" fill="none"/>' for i in range(n))
def eolienne(x,y,h,r):
    s=[f'<rect x="{x-2}" y="{y}" width="4" height="{h}" fill="#444"/>',f'<circle cx="{x}" cy="{y}" r="3" fill="#333"/>']
    for a in (0,120,240):
        ax=x+r*math.sin(math.radians(a));ay=y-r*math.cos(math.radians(a))
        s.append(f'<line x1="{x}" y1="{y}" x2="{ax:.0f}" y2="{ay:.0f}" stroke="#333" stroke-width="2.5"/>')
    return "".join(s)
def dessin(gold):
    taille={1:1,2:2,3:3,4:1}[gold]
    s=[f'<svg xmlns="http://www.w3.org/2000/svg" width="{W}" height="{H}" viewBox="0 0 {W} {H}">',f'<rect width="{W}" height="{H}" fill="#eef3f7"/>']
    mer=(taille==3)
    s.append('<polygon points="0,700 480,700 700,260 900,180 1150,120 1600,100 1600,830 0,830" fill="#a89a7c"/>')
    s.append('<polygon points="900,180 1150,120 1600,100 1600,140 1160,160 910,215" fill="#7f9a6a"/>')
    s.append('<polygon points="480,700 700,260 735,262 520,700" fill="#8c7b5c"/>')
    if mer:
        s.append('<rect x="0" y="700" width="480" height="130" fill="#3b7fb5"/>')
        s.append(vagues(10,720,460,10,"#9fc3e0"))
        s.append(txt(240,790,"The sea: lower basin of the salt loop, and never a village below the lake",13,"middle","bold","#fff"))
    else:
        s.append('<polygon points="0,700 480,700 520,700 520,730 0,730" fill="#b5c49f"/>')
    s.append('<rect x="0" y="830" width="1600" height="70" fill="#17415f"/>')
    for i in range(12):
        x=960+i*50
        s.append(f'<line x1="{x}" y1="{145-i*2}" x2="{x+8}" y2="{125-i*2}" stroke="#2f6fb5" stroke-width="2" stroke-dasharray="3,3"/>')
    s.append('<path d="M 930 178 L 1590 108" stroke="#2c7fb8" stroke-width="5" fill="none"/>')
    s.append(txt(1260,80,"The Pacha catchment: the mountain linked to the lake by a belt canal of 20 to 45 km",14,"middle","bold"))
    s.append(txt(1260,98,"7 km² captured per kilometre, 0.5 M€ per kilometre, paid back in one to two years: the lever that lowers the kWh",12,"middle"))
    s.append('<ellipse cx="1080" cy="235" rx="150" ry="38" fill="#1b4f7a" stroke="#0a3d62" stroke-width="2"/>')
    s.append(vagues(950,235,260,8))
    s.append('<rect x="925" y="222" width="12" height="40" fill="#8a6a3e"/>')
    s.append(txt(1210,300,"Basin 3: the rain lake, 5 to 15 times the upper basin",14,"middle","bold"))
    s.append(txt(1210,318,{1:"2 million m³ (3 in monsoon), compacted clay dam, settling",2:"8 million m³ (12 in monsoon), compacted clay dam, settling",3:"4 million m³ of fresh water (12 in monsoon), above the sea, not a village"}[taille],12,"middle"))
    s.append('<path d="M 940 262 L 940 330 L 860 330" stroke="#5b8fb5" stroke-width="5" fill="none"/>')
    s.append('<rect x="730" y="330" width="130" height="18" rx="4" fill="#8a6a3e"/>')
    s.append(txt(722,318,"The lake fills the upper basin first,",11,"end"))
    s.append(txt(722,333,"the pumps make up the rest",11,"end"))
    if taille==3:
        s.append('<rect x="600" y="350" width="230" height="45" fill="#1b4f7a" stroke="#0a3d62" stroke-width="2"/>')
        s.append('<rect x="713" y="350" width="4" height="45" fill="#d8cdb4"/>')
        s.append(vagues(605,372,105,3));s.append(vagues(720,372,105,3,"#c8e0b0"))
        s.append(txt(845,378,"Upper basin in two cells, 2 × 400,000 m³: fresh (rain) and salt (sea loop)",12,"start","bold"))
    else:
        s.append('<rect x="600" y="350" width="230" height="45" fill="#1b4f7a" stroke="#0a3d62" stroke-width="2"/>')
        s.append(vagues(605,372,220,6))
        s.append(txt(845,378,{1:"Upper basin 200,000 m³, lined, daily drawdown",2:"Upper basin 800,000 m³, lined, daily drawdown"}[taille],12,"start","bold"))
    for i in range(4 if taille==1 else 8):
        x=1200+i*(60 if taille==1 else 45)
        s.append(eolienne(x,132-i*3,50,18))
    s.append(txt(1240,440,{1:"10 wind turbines of 6 MW and 16 MWp of trackers: the V11 makes the kWh",2:"40 wind turbines of 6 MW, 150 m towers, 16 MWp: the V14 makes the kWh",3:"10 wind turbines, 100 MWp and 1,000 hybrid panels: the full V15"}[taille],13,"middle","bold"))
    s.append(txt(1240,458,"When the lake has water, the pumps sleep and all the wind is sold directly",12,"middle"))
    s.append('<path d="M 640 395 L 520 700 L 440 700" stroke="#7fb3d9" stroke-width="9" fill="none"/>')
    s.append('<path d="M 640 395 L 520 700 L 440 700" stroke="#2c3e50" stroke-width="3" fill="none"/>')
    s.append(txt(640,662,{1:"Penstock 300 m, Pelton 12 MW, pumps 16 MW, rain Pelton 4.3 MW",2:"Two DN2000, 300 m, Pelton 48 MW, pumps 64 MW, rain Pelton 2 × 4.5 MW with 45 km of canal",3:"Steel-lined shaft 1,500 m, duplex Pelton 300 MW: 180 MW off-peak on rain"}[taille],14,"start","bold"))
    s.append(leader(636,657,572,600))
    s.append('<rect x="270" y="645" width="200" height="90" fill="#5a4024"/>')
    s.append('<rect x="315" y="630" width="110" height="20" fill="#8a6a3e"/>')
    s.append(txt(370,675,"Plant",13,"middle","bold","#fff"))
    s.append(txt(370,698,"peak: large Pelton, lake water first",10,"middle","normal","#fff"))
    s.append(txt(370,716,"day and night: the large Pelton on rain" if taille==3 else "day and night: rain Pelton on the excess",10,"middle","normal","#fff"))
    if taille==3:
        s.append('<rect x="30" y="600" width="250" height="40" fill="#1b4f7a" stroke="#0a3d62" stroke-width="2"/>')
        s.append(vagues(35,620,240,6,"#c8e0b0"))
        s.append(txt(155,660,"Fresh lower basin: the city's water, by gravity",12,"middle","bold"))
        s.append('<line x1="300" y1="690" x2="280" y2="620" stroke="#5b8fb5" stroke-width="5"/>')
        s.append('<rect x="40" y="520" width="40" height="60" fill="#c3ccd6" stroke="#9aa6b2"/>')
        s.append('<rect x="100" y="530" width="40" height="50" fill="#c3ccd6" stroke="#9aa6b2"/>')
        s.append(txt(155,495,"The coastal city: 200 million m³",11,"middle","bold"))
        s.append(txt(155,510,"of fresh water a year, without desalination",11,"middle","bold"))
    else:
        s.append('<rect x="40" y="745" width="400" height="55" fill="#1b4f7a" stroke="#0a3d62" stroke-width="2"/>')
        s.append(vagues(45,772,390,10))
        s.append(txt(240,818,{1:"Lower basin 200,000 m³, lined banks",2:"Lower basin 800,000 m³, lined banks"}[taille],13,"middle","bold"))
        s.append('<line x1="300" y1="730" x2="300" y2="745" stroke="#5b8fb5" stroke-width="6"/>')
        s.append('<path d="M 40 772 L 0 772" stroke="#5b8fb5" stroke-width="6"/>')
        s.append(txt(135,650,"Regulated stream to the valley:",12,"middle","bold"))
        s.append(txt(135,668,"the turbined rain water comes out at the bottom,",12,"middle"))
        s.append(txt(135,686,"all year round, for the villages and the fields",12,"middle"))
        s.append(leader(120,695,30,765))
    if gold in (1,2,3):
        s.append('<path d="M 1600 300 L 1480 300 L 1380 262 L 1240 262" stroke="#2c7fb8" stroke-width="7" fill="none" stroke-dasharray="14,8"/>')
        s.append('<path d="M 1600 300 L 1480 300" stroke="#1b4f7a" stroke-width="11" fill="none"/>')
        s.append(txt(1420,335,"Stream from another catchment, 0.3 to 10 m³/s",12,"middle","bold","#17415f"))
        s.append(txt(1420,352,"by tunnel or canal from the next valley: day and night, even in drought",11,"middle"))
        s.append('<path d="M 700 455 q 30 -20 60 0 q 30 20 60 0" stroke="#2c7fb8" stroke-width="4" fill="none"/>')
        s.append('<path d="M 820 455 L 980 262" stroke="#5b8fb5" stroke-width="3" fill="none" stroke-dasharray="5,4"/>')
        s.append(txt(700,478,"Stream below the canal: hydraulic ram (7-8% of the flow lifted, no energy)",11,"start"))
        s.append(txt(700,494,"or pump on surplus (30 m lift: 14% of the energy returned at 300 m)",11,"start"))
    else:
        s.append('<rect x="1420" y="165" width="160" height="55" fill="#dcd3c2" stroke="#8a6a3e" stroke-width="2"/>')
        s.append('<path d="M 1420 220 L 1580 220" stroke="#6b4f2a" stroke-width="5"/>')
        s.append('<path d="M 1430 222 q 10 20 0 40 M 1460 222 q 10 20 0 40" stroke="#2c7fb8" stroke-width="4" fill="none"/>')
        s.append('<path d="M 1445 258 L 1445 275 L 1240 275 L 1240 262" stroke="#2c7fb8" stroke-width="7" fill="none"/>')
        s.append(txt(1460,140,"Karst or volcanic massif",12,"middle","bold","#17415f"))
        s.append(txt(1460,155,"the spring rises on the impermeable layer",11,"middle"))
        s.append(txt(1500,187,"limestone or basalt: water seeps",10,"middle"))
        s.append(txt(1500,203,"impermeable layer: the spring",10,"middle"))
        s.append(txt(1420,335,"Spring captured by gravity, 1 to 10 m³/s, constant, clear water",12,"middle","bold","#17415f"))
        s.append(txt(1420,352,"Reversible Francis possible, no diversion canal",11,"middle"))
    s.append('<g transform="translate(1040,480)">')
    s.append('<rect x="0" y="0" width="520" height="150" fill="#ffffff" stroke="#17415f" stroke-width="2"/>')
    s.append(txt(260,24,{1:"V17 Gold 1: the Pacha 1 plus a perennial stream",2:"V17 Gold 2: the full Pacha 2 plus a small river",3:"V17 Gold 3: the Pacha 3 plus a mountain torrent",4:"V17 Gold 4: a Pacha plus a large spring"}[gold],15,"middle","bold","#17415f"))
    lignes={1:["Stream of 1 m³/s diverted to the lake: 31 million m³ a year, 23 GWh at 300 m","335 GWh instead of 312: 28.1 €/MWh instead of 29.4; 3 m³/s: 380 GWh at 25.9","Lake never empty (39 days a year without it), pumps 1 GWh instead of 32","Diversion 3 to 10 M€; borehole ruled out: 50 L/s lifted 100 m, +1 GWh","112 million m³ returned to the valley, reserved flow left in the stream"],2:["Small river of 3 to 10 m³/s: 95 to 315 million m³ a year","1,247 GWh at 23.1 €/MWh with 3 m³/s, 1,406 at 21.8 with 10, instead of 1,180 at 23.6","Rain Pelton of 17 to 35 MW, two units, running day and night","Diversion 5 to 25 M€ (tunnel through the ridge included)","359 to 573 million m³ regulated to the valley"],3:["Torrent of 3 m³/s at 1,500 m: each m³ is worth 3.7 kWh","+175 GWh sold and 210 GWh less surplus to buy: +17 M€ a year","Snowmelt torrent (peak in June): +196 GWh, +18 M€ a year","A 10 km tunnel (50 M€) still pays off; 10 m³/s: 2,155 GWh","Lake empty 20 days instead of 125, fresh water returned to the coast"],4:["Karst or volcanic spring of 1 to 10 m³/s, constant flow","Pacha 1 + 1 m³/s: 27.9 €/MWh; Pacha 2 + 10: 21.5; Pacha 3 + 5: +29 M€ a year","Clear water: reversible Francis possible, 0.5 €/MWh less","Intake and pipe 1.5 to 6 M€, no diversion canal","The lake sits on the impermeable layer, below the karst, where the spring emerges"]}[gold]
    for i,l in enumerate(lignes):
        s.append(txt(15,52+i*20,l,12,"start","bold" if i==1 else "normal"))
    s.append('</g>')
    titres={1:"Hydroloop Muhleisen 2026 V17 Gold 1: the Pacha 1 plus a perennial stream, 335 GWh at 28.1 €/MWh, a lake that no longer empties",2:"Hydroloop Muhleisen 2026 V17 Gold 2: the full Pacha 2 plus a small river, 1,247 to 1,406 GWh at 23.1 and 21.8 €/MWh",3:"Hydroloop Muhleisen 2026 V17 Gold 3: the Pacha 3 plus a mountain torrent turbined at 1,500 m, +17 to +18 M€ a year",4:"Hydroloop Muhleisen 2026 V17 Gold 4: a Pacha plus a large spring, the constant clear water of karst or volcano"}
    s.append(txt(800,860,titres[gold],21,"middle","bold","#fff"))
    s.append(txt(800,885,"Schematic cross-section, not to scale. Gold: the free water added to the rain. www.mulo.world, October 2026, CC BY 4.0",13,"middle","normal","#dfe7ee"))
    s.append('</svg>')
    return "\n".join(s)
for g in (1,2,3,4):
    cairosvg.svg2png(bytestring=dessin(g).encode("utf-8"),write_to=f"schema_hydroloop_muhleisen_2026_v17_gold_{g}.png",output_width=W,output_height=H)
print("ok")
