bballkyle34/PoolChemCalculator
1
1import gradio as gr2 3# Pool chemistry calculation functions4def adjust_ph(pool_volume, current_ph, target_ph):5 # 1 oz muriatic acid per 10,000 gallons lowers pH by ~0.16 # 1 oz soda ash per 10,000 gallons raises pH by ~0.17 ph_difference = target_ph - current_ph8 gallons_per_10000 = pool_volume / 100009 if ph_difference < 0:10 oz_muriatic = abs(ph_difference) * 10 * gallons_per_1000011 return f"Add {oz_muriatic:.2f} oz of muriatic acid to lower pH."12 elif ph_difference > 0:13 oz_soda_ash = ph_difference * 10 * gallons_per_1000014 return f"Add {oz_soda_ash:.2f} oz of soda ash to raise pH."15 else:16 return "pH is already at target."17 18def adjust_chlorine(pool_volume, current_cl, target_cl):19 # 1 ppm chlorine increase per 10,000 gallons needs ~2 oz liquid chlorine (10% solution)20 cl_difference = target_cl - current_cl21 gallons_per_10000 = pool_volume / 1000022 if cl_difference > 0:23 oz_chlorine = cl_difference * 2 * gallons_per_1000024 return f"Add {oz_chlorine:.2f} oz of liquid chlorine (10%) to raise chlorine."25 else:26 return "Chlorine is at or above target (consider dilution if too high)."27 28def adjust_alkalinity(pool_volume, current_alk, target_alk):29 # 10 ppm alkalinity increase per 10,000 gallons needs ~1.4 lbs sodium bicarbonate30 alk_difference = target_alk - current_alk31 gallons_per_10000 = pool_volume / 1000032 if alk_difference > 0:33 lbs_bicarb = (alk_difference / 10) * 1.4 * gallons_per_1000034 return f"Add {lbs_bicarb:.2f} lbs of sodium bicarbonate to raise alkalinity."35 else:36 return "Alkalinity is at or above target."37 38def adjust_stabilizer(pool_volume, current_stab, target_stab):39 # 10 ppm stabilizer increase per 10,000 gallons needs ~1.3 lbs cyanuric acid40 stab_difference = target_stab - current_stab41 gallons_per_10000 = pool_volume / 1000042 if stab_difference > 0:43 lbs_cya = (stab_difference / 10) * 1.3 * gallons_per_1000044 return f"Add {lbs_cya:.2f} lbs of cyanuric acid to raise stabilizer."45 else:46 return "Stabilizer is at or above target (reduce via dilution if too high)."47 48def adjust_calcium(pool_volume, current_calc, target_calc):49 # 10 ppm calcium increase per 10,000 gallons needs ~0.9 lbs calcium chloride50 calc_difference = target_calc - current_calc51 gallons_per_10000 = pool_volume / 1000052 if calc_difference > 0:53 lbs_calcium = (calc_difference / 10) * 0.9 * gallons_per_1000054 return f"Add {lbs_calcium:.2f} lbs of calcium chloride to raise calcium hardness."55 else:56 return "Calcium hardness is at or above target."57 58def adjust_salinity(pool_volume, current_salt, target_salt):59 # 100 ppm salinity increase per 10,000 gallons needs ~2.5 lbs salt60 salt_difference = target_salt - current_salt61 gallons_per_10000 = pool_volume / 1000062 if salt_difference > 0:63 lbs_salt = (salt_difference / 100) * 2.5 * gallons_per_1000064 return f"Add {lbs_salt:.2f} lbs of salt to raise salinity."65 else:66 return "Salinity is at or above target (reduce via dilution if too high)."67 68# Main calculator function69def calculate_chemicals(pool_volume, current_ph, target_ph, current_cl, target_cl, 70 current_alk, target_alk, current_stab, target_stab, 71 current_calc, target_calc, current_salt, target_salt):72 ph_result = adjust_ph(pool_volume, current_ph, target_ph)73 cl_result = adjust_chlorine(pool_volume, current_cl, target_cl)74 alk_result = adjust_alkalinity(pool_volume, current_alk, target_alk)75 stab_result = adjust_stabilizer(pool_volume, current_stab, target_stab)76 calc_result = adjust_calcium(pool_volume, current_calc, target_calc)77 salt_result = adjust_salinity(pool_volume, current_salt, target_salt)78 return f"{ph_result}\n{cl_result}\n{alk_result}\n{stab_result}\n{calc_result}\n{salt_result}"79 80# Gradio interface81interface = gr.Interface(82 fn=calculate_chemicals,83 inputs=[84 gr.Number(label="Pool Volume (gallons)"),85 gr.Number(label="Current pH"),86 gr.Number(label="Target pH"),87 gr.Number(label="Current Free Chlorine (ppm)"),88 gr.Number(label="Target Free Chlorine (ppm)"),89 gr.Number(label="Current Total Alkalinity (ppm)"),90 gr.Number(label="Target Total Alkalinity (ppm)"),91 gr.Number(label="Current Stabilizer - Cyanuric Acid (ppm)"),92 gr.Number(label="Target Stabilizer - Cyanuric Acid (ppm)"),93 gr.Number(label="Current Calcium Hardness (ppm)"),94 gr.Number(label="Target Calcium Hardness (ppm)"),95 gr.Number(label="Current Salinity (ppm)"),96 gr.Number(label="Target Salinity (ppm)")97 ],98 outputs="text",99 title="Pool Chemistry Calculator",100 description="Enter your pool's volume and readings to calculate chemical adjustments."101)102 103# Launch the app locally104interface.launch()