"""Generate qualified static span data and hypothetical comparisons. No capacity approval."""
from pathlib import Path
import json
P=Path(__file__).resolve().parent
DNS=[25,40,50,80,100,150,200,250,300,350,400,450,500,600]
A=[[3850,2300,3450,2250],[4750,3000,4100,2800],[5350,3600,4550,3300],[6550,4600,5450,4200],[7500,5550,6100,4900],[9150,6800,7100,5800],[10500,8050,7950,6700],[11800,9050,8700,7400],[12900,9800,9150,7800],[15150,11850,10850,9300],[16250,12850,11200,9750],[17250,13750,11500,10150],[18200,14450,11750,10400],[18950,16050,12150,10950]]
B=[[3900,2200,3450,2100],[4850,2800,4000,2600],[5450,3300,4300,3000],[6700,4050,4950,3500],[7650,4800,5300,4000],[9400,5750,5950,4600],[10750,6800,6450,5200],[12000,7600,6950,5650],[13000,8250,7350,6050],[13750,8700,7600,6300],[14700,9450,7750,6550],[15650,10150,7850,6750],[16450,11000,8400,7300],[18050,12700,9050,8050]]
COLS=['vapour_bare','vapour_insulated','liquid_bare','liquid_insulated']
def lookup(material,wall,dn,temp_C,service,insulated,support_count):
 if support_count<=2:raise ValueError('More than two supports required')
 if dn not in DNS:raise ValueError('No nominal-size interpolation supplied')
 if material=='carbon_steel' and wall=='STD_or_heavier' and temp_C<=350:setting='A'
 elif material=='austenitic_stainless' and wall=='40S_or_heavier' and dn<400 and temp_C<=350:setting='A'
 elif material=='duplex_stainless' and wall=='10S_or_heavier' and temp_C<=280:setting='A'
 elif material=='austenitic_stainless' and wall=='10S' and temp_C<=350:setting='B'
 else:raise ValueError('Material/wall/temperature/size outside selected basis')
 if service not in ['vapour','liquid']:raise ValueError('Unknown service')
 col=COLS.index(service+('_insulated' if insulated else '_bare'))
 L=(A if setting=='A' else B)[DNS.index(dn)][col]
 return {'set':setting,'DN':dn,'column':COLS[col],'L_mm':L,'L1_mm':.65*L,'L2_mm':.75*L,'cell_limit':'deflection' if setting=='A' and col==0 and dn>=350 else 'longitudinal_bending_stress'}
def main():
 data={'schema':'qualified-static-span-lookup/v1','id':'c10-shape-span-lookup','units':'mm; DN is nominal designation, not outside diameter','columns':COLS,'sets':{'A':{'scope':['carbon steel STD/heavier <=350C','austenitic stainless40S/heavier DN<400 <=350C','duplex stainless10S/heavier <=280C'],'rows':[{'DN':dn,'spans_mm':row,'deflection_limited_columns':['vapour_bare'] if dn>=350 else []} for dn,row in zip(DNS,A)]},'B':{'scope':['austenitic stainless10S <=350C'],'rows':[{'DN':dn,'spans_mm':row,'deflection_limited_columns':[]} for dn,row in zip(DNS,B)]}},'conditions':{'support_count':'more than two','default_cell_limit':'longitudinal bending stress except marked A bare-vapour cells DN350..600','insulation':'70mm atDN25 to200mm atDN600; insulation/sheeting density190kg/m3; intermediate thickness not specified','drainage':'slope<1.5mm/m requires additional span check','liquid':'no universal fluid-density permission inferred','shape':'L1=.65L only illustrated planar-bend dimension path; L2=.75L only illustrated spatial-offset dimension construction; no arbitrary developed/chord substitution','weights':'actual insulation/contents/concentrated loads separately compatible','capacity':'local wall, hardware, structure, test, thermal and dynamic assessment separate'},'geometry':{'L':'applicable straight span table entry','L1':'bend-following dimension construction between marked stations on two legs; no leg split proportions specified','L2':'source projected spatial-offset dimension construction and lower projected endpoint; retain original indicated stations; no offset height or projection angle inferred','factors':{'L1':.65,'L2':.75},'not_frequency_coefficients':True}}
 (P/'c10-shape-span-lookup.json').write_text(json.dumps(data,indent=2)+'\n')
 results=[]
 for material,wall in [('carbon_steel','STD_or_heavier'),('austenitic_stainless','10S')]:
  r=lookup(material,wall,100,200,'liquid',True,4)
  r.update({'assumed_material':material,'assumed_wall':wall,'assumed_temperature_C':200,'assumed_support_count':4,'assumed_proposed_L1_mm':3000,'assumed_proposed_L2_mm':3500,'L1_below_lookup':3000<=r['L1_mm'],'L2_below_lookup':3500<=r['L2_mm'],'assumed_slope_mm_per_m':1.,'additional_drainage_check_required':True,'status':'hypothetical lookup comparison only'})
  results.append(r)
 (P/'c10-shape-span-calculations.json').write_text(json.dumps({'cases':results,'equations':['L1=.65*L','L2=.75*L'],'no_installation_acceptance':True},indent=2)+'\n')
if __name__=='__main__':main()
