Feature: Agricultural Calculators
Relevant source files
The following files were used as context for generating this wiki page:
The Agricultural Calculators feature provides a suite of scientific tools designed to help grape farmers make data-driven decisions. It replaces manual calculations with reliable digital tools for optimizing irrigation, nutrition, and canopy management. This feature is a core component of VineSight's mission to digitize farming operations based on scientific research.
The calculators are organized into categories, each addressing a specific aspect of vineyard management. The results from these calculators can be saved to the user's farm history and are used to inform other parts of the application, such as the AI Assistant and Analytics & Reports.
Sources: context/PRD.md:13-17, src/app/calculators/page.tsx:11-15, src/components/calculators/NutrientCalculator.tsx:210-218
Architecture Overview
The calculator feature is structured with a clear separation between the user interface and the business logic. The main entry point is the CalculatorsPage, which displays a categorized list of available calculators. When a user selects a calculator, a specific React component for that tool is rendered. The core calculation logic is encapsulated within corresponding service classes in the src/lib/ directory.
Sources: src/app/calculators/page.tsx:75-87, src/lib/nutrient-calculator.ts
This diagram illustrates the high-level architecture of the calculator module.
Calculator Categories
The calculators are grouped into three main categories to provide a structured user experience.
| Category | Description | Icon | Calculators |
|---|---|---|---|
| Irrigation & Water Use | Calculate water needs and irrigation system requirements. | Droplets | Water Needs (ETc), MAD, System Discharge |
| Crop & Growth Analysis | Monitor vine development and leaf coverage. | Leaf | Leaf Area Index (LAI) |
| Nutrition Management | Calculate fertilizer requirements and nutrient planning. | Beaker | Fertilizer Calculator |
Sources: src/app/calculators/page.tsx:17-59
Detailed Calculator Breakdown
Irrigation & Water Use
This category contains tools essential for efficient water management.
Water Needs Calculator (ETc)
This calculator determines the daily water requirements for the crop based on evapotranspiration (ETc).
- Formula:
ETc = ETo * Kc - Inputs: Max/Min Temperature, Humidity, Wind Speed, Rainfall, and the current crop growth stage.
- Outputs: Reference Evapotranspiration (
ETo), Crop Coefficient (Kc), Crop Evapotranspiration (ETc), and an irrigation recommendation in mm/day.
Sources: context/PRD.md:25, src/lib/i18n.ts:119-131, context/TYPES_AND_INTERFACES.md:78-93
MAD (Maximum Allowable Deficit) Calculator
This tool calculates the maximum amount of water that can be depleted from the soil before the crop experiences stress.
- Formula:
(100/(DBL) * Root Depth * Root Width * Water Retention * 100) / 10000 - Inputs: Distance Between Lines (DBL), Root Depth, Root Width, and Water Retention.
- Related Calculation: The result is used in the Refill Tank Calculator, which determines the amount of water to apply based on the growth period.
Sources: context/PRD.md:27-34, src/app/calculators/page.tsx:29-33
System Discharge Calculator
This calculator helps in planning the irrigation system by calculating the discharge rate. Two different formulas are available.
- Formula 1:
(P/H * Drippers per plant * Discharge per hour) / 10000whereP/A = (DBL * DBP) / 1000 - Formula 2:
((100 / DBL) * (100/ DBD) * Discharge per hour) / 10000 - Inputs: Distance Between Lines (DBL), Distance Between Plants (DBP), Drippers per plant, Discharge per hour, Distance Between Drippers (DBD).
Sources: context/PRD.md:36-45
The following diagram shows the data flow for the irrigation calculators.
Nutrition Management
Fertilizer Calculator
This calculator provides NPK and micronutrient recommendations based on soil test results, yield targets, and the current growth stage of the grapes.
Sources: src/lib/nutrient-calculator.ts:5-8, src/components/calculators/NutrientCalculator.tsx:26-29
Data Flow and Logic
The calculation process involves several steps, from gathering inputs to generating a comprehensive fertilizer program.
Key Data Structures
The NutrientCalculator relies on a set of well-defined interfaces for its inputs and outputs.
Inputs (NutrientCalculationInputs)
| Field | Type | Description |
|---|---|---|
farmId | number | The ID of the selected farm. |
targetYield | number | The desired yield in tons per acre. |
currentGrowthStage | GrapeGrowthStage | The current stage of the grapevines. |
soilTest | SoilTestResults | An object containing detailed soil analysis data. |
grapeVariety | 'table' | 'wine' | 'raisin' | The type of grape being cultivated. |
farmArea | number | The area of the farm in acres. |
Sources: src/lib/nutrient-calculator.ts:18-29
Outputs (NutrientResults)
| Field | Type | Description |
|---|---|---|
recommendations | object | Detailed recommendations for N, P, K, secondary, and micronutrients. |
fertilizerProgram | FertilizerProgram[] | A list of recommended fertilizers, rates, and costs. |
totalCost | number | The total estimated cost for the fertilizer program. |
applicationSchedule | ApplicationSchedule[] | A month-by-month schedule for applying fertilizers. |
soilHealthAssessment | SoilHealthAssessment | An assessment of soil pH, organic matter, and cation balance. |
Sources: src/lib/nutrient-calculator.ts:37-50
Class Diagram
The NutrientCalculator class and its associated data structures are central to this feature.
Sources: src/lib/nutrient-calculator.ts
Soil Health Monitoring
This is a comprehensive analysis tool that provides an in-depth assessment of soil health based on physical, chemical, and biological properties.
- Purpose: To provide a holistic view of soil health, identify limitations, and generate long-term management strategies.
- Inputs (
SoilHealthInputs): The component takes a large number of inputs organized intophysical,chemical, andbiologicalcategories. This includes soil texture, pH, organic matter, nutrient levels, microbial biomass, and enzyme activity. - Logic: An external
SoilHealthAnalyzerclass is used to process the inputs. The analysis generates an overall health score, sub-scores for each category, alerts for critical issues, and detailed recommendations. - UI: The
SoilHealthMonitoringComponentfeatures a multi-tab interface to separate data input, results, and actionable recommendations. It uses visual elements like progress bars, badges, and trend icons to present complex data in an understandable format.
Sources: [src/components/calculators/SoilHealthMonitoring.tsx:30-104, 110-116]
The following diagram shows the structure of the SoilHealthResults object.
Sources: [src/components/calculators/SoilHealthMonitoring.tsx:111-112, 300-470]
Internationalization (i18n)
The user interface for all calculators supports multiple languages, including English, Hindi, and Marathi. UI labels, descriptions, and results are managed through a centralized translation file.
This is achieved using i18next with resource keys defined for each calculator and its fields.
// Example from src/lib/i18n.ts
const resources = {
en: {
translation: {
calculators: {
title: 'Agricultural Calculators',
etc: {
title: 'ETc Calculator',
maxTemp: 'Max Temperature (°C)',
},
lai: {
title: 'LAI Calculator',
leavesPerShoot: 'Leaves per Shoot',
},
nutrient: {
title: 'Nutrient Calculator',
targetYield: 'Target Yield (tons/ha)',
},
},
},
},
hi: {
translation: {
calculators: {
title: 'कृषि कैलकुलेटर',
etc: {
title: 'ETc कैलकुलेटर',
maxTemp: 'अधिकतम तापमान (°C)',
},
// ... other translations
},
},
},
// ... Marathi translations
}
Sources: [src/lib/i18n.ts:116-155, 309-348, 502-541]
Conclusion
The Agricultural Calculators are a cornerstone feature of the VineSight application, empowering farmers with scientific tools for precision agriculture. By separating complex business logic from the user interface and providing multi-language support, the module is both powerful and accessible. The data generated by these calculators is integral to the platform, feeding into analytics, reporting, and AI-driven recommendations to help farmers optimize their operations, increase yield, and improve profitability.
Sources: context/PRD.md:13-17, src/app/reports/page.tsx:437-440