Files
2026-11-16 01:39:59 +07:00

82 lines
4.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* crop_water.js — ความต้องการน้ำของพืช (ETc = Kc × ETo)
pure module: ไม่แตะ DOM ใช้ได้ทั้งเบราว์เซอร์และ Node test
หน่วย:
- ETo (mm/day) มาจาก data/climate/eto_by_region.json (Penman-Monteith รายจังหวัด)
- 1 ไร่ = 1,600 ตร.ม. · น้ำลึก 1 มม. บน 1 ไร่ = 1.6 ลบ.ม.
*/
export const RAI_IN_SQUARE_METRE = 1600;
export const CUBIC_METRE_PER_MILLIMETRE_PER_RAI = RAI_IN_SQUARE_METRE / 1000; // 1.6
function requirePositive(value, label) {
const number = Number(value);
if (!Number.isFinite(number) || number <= 0) {
throw new RangeError(label + " ต้องเป็นตัวเลขมากกว่า 0");
}
return number;
}
/**
* @param {object} input
* @param {number} input.eto_mm_per_day ค่าอ้างอิงการระเหยของพืช (มม./วัน)
* @param {number} input.crop_coefficient สัมประสิทธิ์พืช Kc (พืชอ้างอิง = 1.00)
* @param {number} input.area_rai พื้นที่ปลูก (ไร่)
* @param {number} input.irrigation_efficiency_percent ประสิทธิภาพการให้น้ำ (%)
* @returns {{value:number, unit:string, steps:Array, warnings:string[], details:object}}
*/
export function calculateCropWaterDemand(input) {
const eto = requirePositive(input.eto_mm_per_day, "ETo");
const coefficient = requirePositive(input.crop_coefficient, "Kc");
const area = requirePositive(input.area_rai, "พื้นที่ปลูก");
const efficiency = requirePositive(input.irrigation_efficiency_percent, "ประสิทธิภาพการให้น้ำ");
if (efficiency > 100) {
throw new RangeError("ประสิทธิภาพการให้น้ำต้องไม่เกิน 100%");
}
const etc_mm_per_day = eto * coefficient;
const net_m3_per_day = etc_mm_per_day * CUBIC_METRE_PER_MILLIMETRE_PER_RAI * area;
const gross_mm_per_day = etc_mm_per_day / (efficiency / 100);
const gross_m3_per_day = gross_mm_per_day * CUBIC_METRE_PER_MILLIMETRE_PER_RAI * area;
const warnings = [];
if (coefficient < 0.2 || coefficient > 1.3) {
warnings.push("Kc = " + coefficient + " อยู่นอกช่วง 0.2–1.3 ที่พบทั่วไป — ตรวจค่ากับตาราง Kc ของพืชก่อนใช้");
}
if (efficiency < 40) {
warnings.push("ประสิทธิภาพการให้น้ำต่ำกว่า 40% — น้ำที่ต้องสูบจะมากกว่าที่พืชใช้หลายเท่า");
}
const steps = [
{ label: "ETo ของพื้นที่/เดือนที่เลือก", value: Number(eto.toFixed(2)), unit: "มม./วัน" },
{ label: "ETc = Kc × ETo = " + coefficient + " × " + eto, value: Number(etc_mm_per_day.toFixed(2)), unit: "มม./วัน" },
{ label: "1 มม. บน 1 ไร่", value: CUBIC_METRE_PER_MILLIMETRE_PER_RAI, unit: "ลบ.ม." },
{
label: "ปริมาณน้ำที่พืชใช้ = ETc × 1.6 × " + area,
value: Number(net_m3_per_day.toFixed(2)),
unit: "ลบ.ม./วัน"
},
{
label: "ปริมาณน้ำที่ต้องให้ = ÷ ประสิทธิภาพ " + efficiency + "%",
value: Number(gross_m3_per_day.toFixed(2)),
unit: "ลบ.ม./วัน"
}
];
return {
value: Number(gross_m3_per_day.toFixed(2)),
unit: "cubic_metre_per_day",
steps,
warnings,
details: {
eto_mm_per_day: Number(eto.toFixed(2)),
etc_mm_per_day: Number(etc_mm_per_day.toFixed(2)),
net_m3_per_day: Number(net_m3_per_day.toFixed(2)),
gross_m3_per_day: Number(gross_m3_per_day.toFixed(2)),
gross_m3_per_week: Number((gross_m3_per_day * 7).toFixed(2)),
gross_m3_per_month: Number((gross_m3_per_day * 30).toFixed(2))
}
};
}