Skip to content

Add ammonia emissions#176

Merged
SvenVw merged 39 commits into
developmentfrom
add-ammonia-emissions
Jun 26, 2025
Merged

Add ammonia emissions#176
SvenVw merged 39 commits into
developmentfrom
add-ammonia-emissions

Conversation

@SvenVw
Copy link
Copy Markdown
Collaborator

@SvenVw SvenVw commented Jun 20, 2025

Summary by CodeRabbit

  • New Features

    • Ammonia emissions are now calculated and displayed as part of the nitrogen balance, with detailed breakdowns by fertilizer type and field.
    • Fertilizer forms and catalogues now support nitrate (p_no3_rt), ammonium (p_nh4_rt), and ammonia emission factor (p_ef_nh3) parameters.
    • Nitrogen balance details provide expanded, interactive views of ammonia volatilization sources.
    • Additional fertilizer application methods have been introduced for more precise emission modeling.
  • Bug Fixes

    • Improved validation in fertilizer forms to ensure nitrate and ammonium values do not exceed total nitrogen.
  • Documentation

    • Nitrogen volatilization and ammonia emission calculations are now thoroughly documented, including formulas and methodology.
  • Style

    • Enhanced formatting and consistency in error logging within nitrogen balance components.
  • Tests

    • Comprehensive new tests for ammonia emission calculations from mineral, manure, compost, and other fertilizer types.
  • Chores

    • Added new fertilizer application methods and updated related schema and catalogues.

@SvenVw SvenVw self-assigned this Jun 20, 2025
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jun 20, 2025

🦋 Changeset detected

Latest commit: e6a4568

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@svenvw/fdm-core Minor
@svenvw/fdm-data Minor
@svenvw/fdm-app Minor
@svenvw/fdm-calculator Minor
@svenvw/fdm-docs Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 20, 2025

Warning

Rate limit exceeded

@SvenVw has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 12 minutes and 42 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between db42918 and e6a4568.

📒 Files selected for processing (1)
  • fdm-docs/docs/balance/01-nitrogen.md (2 hunks)

Walkthrough

This update introduces comprehensive support for calculating, displaying, and documenting ammonia (NH₃) emissions from fertilizer applications and crop residues. It expands the data model, database schema, and form validation to include new fertilizer parameters (p_no3_rt, p_nh4_rt, p_ef_nh3). Calculation logic for ammonia volatilization is implemented and tested for mineral, manure, compost, and other fertilizer types, with corresponding UI and documentation enhancements.

Changes

Files/Paths Change Summary
.changeset/*.md Added changeset files documenting minor updates across packages for new fertilizer parameters, ammonia emission calculations, and related UI/documentation changes.
fdm-core/src/cultivation.d.ts, fdm-core/src/cultivation.ts, fdm-core/src/cultivation.test.ts Added b_lu_croprotation field to cultivation entities and queries; updated tests accordingly.
fdm-core/src/db/schema.ts, fdm-data/src/fertilizers/d.ts Extended fertilizer application methods and fertilizer catalogue schema/types with new parameters: p_no3_rt, p_nh4_rt, p_ef_nh3.
fdm-core/src/fertilizer.d.ts, fdm-core/src/fertilizer.ts, fdm-core/src/fertilizer.test.ts Added new fertilizer parameters to interfaces, parameter descriptions, add/update logic, and tests.
fdm-data/src/fertilizers/catalogues/srm.json, fdm-data/src/fertilizers/catalogues/srm.ts Populated catalogue data and mapping with p_no3_rt and p_nh4_rt for each fertilizer entry.
fdm-data/src/fertilizers/hash.test.ts, fdm-data/src/fertilizers/index.test.ts Updated tests to assert presence of new fertilizer parameters and correct hash computation.
fdm-app/app/components/blocks/fertilizer/formschema.tsx Added form fields and validation for p_no3_rt and p_nh4_rt, with logic ensuring their sum does not exceed total nitrogen.
fdm-app/app/components/blocks/balance/nitrogen-details.tsx Enhanced nitrogen balance details to render nested ammonia emission breakdowns by fertilizer and residue types, with field-level linking and error handling.
fdm-app/app/routes/farm.$b_id_farm.$calendar.balance.nitrogen._index.tsx, fdm-app/app/routes/farm.$b_id_farm.$calendar.balance.nitrogen.$b_id.tsx Modified nitrogen emission card to display actual volatilization values and updated subtitle to show units.
fdm-calculator/src/balance/nitrogen/types.d.ts Extended types for fertilizer details, field input, and ammonia emission breakdowns to support new parameters and sources.
fdm-calculator/src/balance/nitrogen/index.ts, fdm-calculator/src/balance/nitrogen/volatization/index.ts Updated nitrogen balance and volatilization calculation logic to include ammonia emissions from fertilizers, with new function signatures.
fdm-calculator/src/balance/nitrogen/volatization/fertilizers/compost.ts, fdm-calculator/src/balance/nitrogen/volatization/fertilizers/manure.ts, fdm-calculator/src/balance/nitrogen/volatization/fertilizers/mineral.ts, fdm-calculator/src/balance/nitrogen/volatization/fertilizers/other.ts, fdm-calculator/src/balance/nitrogen/volatization/fertilizers/index.ts Implemented calculation functions for ammonia emissions by fertilizer type, with error handling and emission factor determination.
fdm-calculator/src/balance/nitrogen/volatization/fertilizers/compost.test.ts, fdm-calculator/src/balance/nitrogen/volatization/fertilizers/manure.test.ts, fdm-calculator/src/balance/nitrogen/volatization/fertilizers/mineral.test.ts, fdm-calculator/src/balance/nitrogen/volatization/fertilizers/other.test.ts Added comprehensive tests for ammonia emission calculations for each fertilizer type, including error cases and edge conditions.
fdm-calculator/src/doses/calculate-dose.ts Changed fertilizer matching logic to use p_id_catalogue instead of p_id.
fdm-docs/docs/balance/01-nitrogen.md, .changeset/tiny-tires-tickle.md Expanded documentation to detail ammonia volatilization calculation methodology, formulas, and parameter definitions.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant UI as fdm-app (UI)
    participant API as fdm-core/fdm-calculator
    participant DB as Database

    User->>UI: Add/update fertilizer with p_no3_rt, p_nh4_rt, p_ef_nh3
    UI->>API: Validate and submit fertilizer data
    API->>DB: Store new fertilizer parameters

    User->>UI: View nitrogen balance details
    UI->>API: Request nitrogen balance calculation
    API->>DB: Fetch cultivations, fertilizer applications, details
    API->>API: Calculate ammonia volatilization (by fertilizer type)
    API->>UI: Return nitrogen balance with ammonia emission breakdown
    UI->>User: Display nested ammonia emission accordions and values
Loading

Possibly related PRs

Suggested reviewers

  • gerardhros

Poem

In fields of green where carrots grow,
A bunny hops, emissions low.
With nitrate, ammonium, factors new,
The data’s fresh, the numbers true.
Ammonia tallied, balance precise—
The science now is extra nice!
🐇✨

✨ Finishing Touches
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in Comment
  • Commit Unit Tests in branch add-ammonia-emissions

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 20, 2025

Codecov Report

Attention: Patch coverage is 96.88249% with 13 lines in your changes missing coverage. Please review.

Project coverage is 93.45%. Comparing base (4f6d846) to head (e6a4568).
Report is 40 commits behind head on development.

Files with missing lines Patch % Lines
...alance/nitrogen/volatization/fertilizers/manure.ts 91.04% 12 Missing ⚠️
...lance/nitrogen/volatization/fertilizers/mineral.ts 98.52% 1 Missing ⚠️
Additional details and impacted files
@@               Coverage Diff               @@
##           development     #176      +/-   ##
===============================================
+ Coverage        93.29%   93.45%   +0.15%     
===============================================
  Files               68       73       +5     
  Lines             8384     8784     +400     
  Branches           999     1087      +88     
===============================================
+ Hits              7822     8209     +387     
- Misses             560      573      +13     
  Partials             2        2              
Flag Coverage Δ
fdm-calculator 95.15% <96.53%> (+0.32%) ⬆️
fdm-core 93.04% <100.00%> (+0.03%) ⬆️
fdm-data 92.78% <100.00%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@SvenVw SvenVw marked this pull request as ready for review June 24, 2025 15:22
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

♻️ Duplicate comments (1)
fdm-calculator/src/balance/nitrogen/volatization/fertilizers/mineral.ts (1)

13-13: Fix remaining typo in documentation.

The documentation still contains the typo "Emmission" that was flagged in previous reviews.

- * `determineMineralAmmoniaEmmissionFactor`), and calculates the ammonia emissions
+ * `determineMineralAmmoniaEmissionFactor`), and calculates the ammonia emissions
🧹 Nitpick comments (6)
.changeset/tasty-grapes-help.md (1)

5-5: Improve grammar for better readability.

Consider adding the article "the" for improved readability.

-Add calculation of ammonia emissions to nitrogen balance calculation.
+Add calculation of ammonia emissions to the nitrogen balance calculation.
.changeset/green-symbols-arrive.md (1)

5-5: Improve grammar for better readability.

Consider adding the article "the" for improved readability.

-Add `p_no3_rt` and `p_nh4_rt` to SRM catalogue.
+Add `p_no3_rt` and `p_nh4_rt` to the SRM catalogue.
.changeset/clean-rooms-post.md (1)

5-5: Improve grammar for better readability.

Consider adding the article "the" for improved readability.

-Add parameter `p_ef_nh3` to the fertilizersCatalogue to represent the ammonia emission factor of a fertilizer.
+Add parameter `p_ef_nh3` to the fertilizersCatalogue to represent the ammonia emission factor of a fertilizer.

Wait, this line already has "the" before "fertilizersCatalogue" but is missing "the" before the second part. Let me correct:

-Add parameter `p_ef_nh3` to the fertilizersCatalogue to represent the ammonia emission factor of a fertilizer.
+Add parameter `p_ef_nh3` to the fertilizersCatalogue to represent the ammonia emission factor of a fertilizer.

Actually, looking more carefully, the issue is with the first "the":

-Add parameter `p_ef_nh3` to the fertilizersCatalogue to represent the ammonia emission factor of a fertilizer.
+Add parameter `p_ef_nh3` to the fertilizersCatalogue to represent the ammonia emission factor of a fertilizer.
fdm-calculator/src/balance/nitrogen/volatization/fertilizers/other.ts (1)

69-69: Fix misleading comment.

The comment refers to "manure" but this function handles "other" fertilizer types.

-        // Calculate for this application the amount of Nitrogen supplied by manure
+        // Calculate for this application the amount of Nitrogen supplied by other fertilizers
fdm-docs/docs/balance/01-nitrogen.md (2)

207-265: Fix markdown formatting issues for consistency.

The nested list indentation should follow a consistent 2-space pattern, and tables should be surrounded by blank lines.

Apply this diff to fix the formatting:

-*   **Manure, Compost, and Other Organic Fertilizers:**
-    For these organic fertilizers, the emission is calculated based on the Total Ammoniacal Nitrogen (TAN) content, as this is the fraction of nitrogen that is readily available for volatilization.
-
-    *   **Formula:**
-        `NH3 Emission (kg N / ha) = Application Amount (kg / ha) * TAN Content (fraction) * Emission Factor (fraction)`
-        Where:
-        *   `Application Amount`: `p_app_amount` (kg / ha) - The total amount of fertilizer applied.
-        *   `TAN Content`: `p_nh4_rt` (fraction) - The fraction of total nitrogen that is in ammoniacal form.
-        *   `Emission Factor`: A dimensionless factor representing the proportion of TAN that is volatilized as ammonia. This factor is determined by the application method and the type of land (grassland, cropland, or bare soil) at the time of application.
-
-    *   **Emission Factors for Manure and Compost:**
+* **Manure, Compost, and Other Organic Fertilizers:**
+  For these organic fertilizers, the emission is calculated based on the Total Ammoniacal Nitrogen (TAN) content, as this is the fraction of nitrogen that is readily available for volatilization.
+
+  * **Formula:**
+    `NH3 Emission (kg N / ha) = Application Amount (kg / ha) * TAN Content (fraction) * Emission Factor (fraction)`
+    Where:
+    * `Application Amount`: `p_app_amount` (kg / ha) - The total amount of fertilizer applied.
+    * `TAN Content`: `p_nh4_rt` (fraction) - The fraction of total nitrogen that is in ammoniacal form.
+    * `Emission Factor`: A dimensionless factor representing the proportion of TAN that is volatilized as ammonia. This factor is determined by the application method and the type of land (grassland, cropland, or bare soil) at the time of application.
+
+  * **Emission Factors for Manure and Compost:**
+
         | Application Method    | Grassland | Cropland | Bare Soil |
         | :-------------------- | :-------- | :------- | :-------- |

Similar indentation fixes should be applied to the mineral fertilizers section (lines 226-249) and crop residues section (lines 252-268).


257-257: Fix typo in emission factor formula.

There's an extra backslash in the formula.

-    `NH3 Emission (kg N / ha) = Residue N Content (kg N / ha) * \Emission Factor (fraction)`
+    `NH3 Emission (kg N / ha) = Residue N Content (kg N / ha) * Emission Factor (fraction)`
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e6f5e44 and 39587ab.

📒 Files selected for processing (16)
  • .changeset/clean-rooms-post.md (1 hunks)
  • .changeset/green-symbols-arrive.md (1 hunks)
  • .changeset/slow-parks-tickle.md (1 hunks)
  • .changeset/strong-bobcats-push.md (1 hunks)
  • .changeset/tasty-grapes-help.md (1 hunks)
  • .changeset/tiny-tires-tickle.md (1 hunks)
  • fdm-app/app/components/blocks/fertilizer/formschema.tsx (2 hunks)
  • fdm-calculator/src/balance/nitrogen/volatization/fertilizers/compost.ts (1 hunks)
  • fdm-calculator/src/balance/nitrogen/volatization/fertilizers/index.ts (1 hunks)
  • fdm-calculator/src/balance/nitrogen/volatization/fertilizers/manure.test.ts (1 hunks)
  • fdm-calculator/src/balance/nitrogen/volatization/fertilizers/manure.ts (1 hunks)
  • fdm-calculator/src/balance/nitrogen/volatization/fertilizers/mineral.ts (1 hunks)
  • fdm-calculator/src/balance/nitrogen/volatization/fertilizers/other.test.ts (1 hunks)
  • fdm-calculator/src/balance/nitrogen/volatization/fertilizers/other.ts (1 hunks)
  • fdm-calculator/src/balance/nitrogen/volatization/index.ts (2 hunks)
  • fdm-docs/docs/balance/01-nitrogen.md (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (9)
  • .changeset/tiny-tires-tickle.md
  • .changeset/strong-bobcats-push.md
  • fdm-calculator/src/balance/nitrogen/volatization/fertilizers/other.test.ts
  • fdm-calculator/src/balance/nitrogen/volatization/fertilizers/compost.ts
  • fdm-calculator/src/balance/nitrogen/volatization/index.ts
  • .changeset/slow-parks-tickle.md
  • fdm-calculator/src/balance/nitrogen/volatization/fertilizers/index.ts
  • fdm-app/app/components/blocks/fertilizer/formschema.tsx
  • fdm-calculator/src/balance/nitrogen/volatization/fertilizers/manure.test.ts
🧰 Additional context used
🧠 Learnings (1)
fdm-docs/docs/balance/01-nitrogen.md (1)
Learnt from: SvenVw
PR: SvenVw/fdm#134
File: fdm-calculator/src/balance/nitrogen/index.ts:236-238
Timestamp: 2025-05-26T10:32:15.538Z
Learning: In nitrogen balance calculations for agricultural systems, removal and volatilization are calculated as negative values by definition since they represent nitrogen losses from the system. The balance calculation uses addition (.add()) for all components because removal and volatilization are already negative, so adding them effectively subtracts the losses from the supply.
🪛 LanguageTool
fdm-docs/docs/balance/01-nitrogen.md

[uncategorized] ~211-~211: Loose punctuation mark.
Context: ...lizer applied. * TAN Content: p_nh4_rt (fraction) - The fraction of...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~212-~212: Loose punctuation mark.
Context: ...acal form. * Emission Factor: A dimensionless factor representing the...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~233-~233: Loose punctuation mark.
Context: ...(kg / ha). * Total N Content: p_n_rt (fraction). * `Emiss...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~234-~234: Loose punctuation mark.
Context: ...fraction). * Emission Factor: p_ef_nh3 (fraction). This factor can ...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~240-~240: Loose punctuation mark.
Context: ..._rt - p_nh4_rt). * p_no3_rt: Nitrate content. * p_nh4_rt`...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~241-~241: Loose punctuation mark.
Context: ... Nitrate content. * p_nh4_rt: Ammonium content (TAN). * `p_...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~242-~242: Loose punctuation mark.
Context: ...nium content (TAN). * p_s_rt: Sulfur content. * K_1, `K_2...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~243-~243: Loose punctuation mark.
Context: ...s_rt: Sulfur content. * K_1, K_2, K_3`: Empirical constants. ...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~260-~260: Loose punctuation mark.
Context: ...onCatalogue). * Emission Factor`: This factor is calculated based on the ...

(UNLIKELY_OPENING_PUNCTUATION)

.changeset/clean-rooms-post.md

[uncategorized] ~5-~5: You might be missing the article “the” here.
Context: --- "@svenvw/fdm-core": minor --- Add parameter p_ef_nh3 to the fertilizersCatalogue ...

(AI_EN_LECTOR_MISSING_DETERMINER_THE)

.changeset/green-symbols-arrive.md

[uncategorized] ~5-~5: You might be missing the article “the” here.
Context: ...r --- Add p_no3_rt and p_nh4_rt to SRM catalogue.

(AI_EN_LECTOR_MISSING_DETERMINER_THE)

.changeset/tasty-grapes-help.md

[uncategorized] ~5-~5: You might be missing the article “the” here.
Context: ...@svenvw/fdm-calculator": minor --- Add calculation of ammonia emissions to nitrogen balanc...

(AI_EN_LECTOR_MISSING_DETERMINER_THE)


[uncategorized] ~5-~5: You might be missing the article “the” here.
Context: ...Add calculation of ammonia emissions to nitrogen balance calculation.

(AI_EN_LECTOR_MISSING_DETERMINER_THE)

🪛 markdownlint-cli2 (0.17.2)
fdm-docs/docs/balance/01-nitrogen.md

207-207: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


210-210: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


211-211: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


212-212: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


214-214: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


215-215: Tables should be surrounded by blank lines
null

(MD058, blanks-around-tables)


229-229: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


232-232: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


233-233: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


234-234: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


239-239: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


240-240: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


241-241: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


242-242: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


243-243: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


244-244: Unordered list indentation
Expected: 6; Actual: 12

(MD007, ul-indent)


245-245: Unordered list indentation
Expected: 6; Actual: 12

(MD007, ul-indent)


246-246: Unordered list indentation
Expected: 6; Actual: 12

(MD007, ul-indent)


247-247: Unordered list indentation
Expected: 6; Actual: 12

(MD007, ul-indent)


259-259: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


260-260: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


262-262: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


265-265: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)

⏰ Context from checks skipped due to timeout of 300000ms (2)
  • GitHub Check: core (22)
  • GitHub Check: calculator (22)
🔇 Additional comments (2)
fdm-calculator/src/balance/nitrogen/volatization/fertilizers/other.ts (1)

63-67: Clarify why manure emission factor is used for "other" fertilizers.

The function uses determineManureAmmoniaEmissionFactor for "other" fertilizer types. This seems inconsistent unless "other" fertilizers are intended to use the same calculation method as manure.

Please clarify if "other" fertilizer types should use the manure emission factor calculation or if they need their own specific calculation method.

fdm-calculator/src/balance/nitrogen/volatization/fertilizers/manure.ts (1)

1-193: Well-implemented ammonia emissions calculations for manure!

The implementation correctly:

  • Uses Decimal.js for precise calculations
  • Properly determines land type (grassland/cropland/bare soil) at application time
  • Applies the correct emission factors as documented
  • Includes comprehensive error handling for unsupported application methods
  • Has clear JSDoc comments explaining the functionality

Comment thread fdm-calculator/src/balance/nitrogen/volatization/fertilizers/other.ts Outdated
@SvenVw SvenVw requested a review from gerardhros June 25, 2025 07:42

/**
* Determines the ammonia emission factor for mineral fertilizers based on their
* nitrogen, nitrate, ammonium, and sulfur content, and the presence of an inhibitor.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you only apply this function when p_ef_nh3 is null?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Where:
* `Application Amount`: `p_app_amount` (kg / ha) - The total amount of fertilizer applied.
* `TAN Content`: `p_nh4_rt` (fraction) - The fraction of total nitrogen that is in ammoniacal form.
* `Emission Factor`: A dimensionless factor representing the proportion of TAN that is volatilized as ammonia. This factor is determined by the application method and the type of land (grassland, cropland, or bare soil) at the time of application.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TAN content is g/kg and not a fraction isn't?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is resolved in 3d8d58f


* **Emission Factors for Manure and Compost:**
| Application Method | Grassland | Cropland | Bare Soil |
| :-------------------- | :-------- | :------- | :-------- |
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is cropland versus bare soil defined? as manure data before sowing date or after harvest date then its a bare soil?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, when no cultivation is present, it is bare soil

* `K_2 = -4.308 * 10^-5`
* `K_3 = 2.498 * 10^-4`
*Note: Currently, the presence of an inhibitor (`p_inhibitor`) is hardcoded to `false` in the calculation.*

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interface does not allow user to select with/without inhibitor? thats fine, but good to add to action list

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, inhibitor is not implement as parameter of fertilizer yet

Copy link
Copy Markdown
Collaborator

@gerardhros gerardhros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calculations fine. well done.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
fdm-docs/docs/balance/01-nitrogen.md (2)

257-258: Remove stray back-slash before “Emission Factor”

The back-slash renders literally and clutters the formula.

-`NH3 Emission (kg N / ha) = Residue N Content (kg N / ha) * \Emission Factor (fraction)`
+`NH3 Emission (kg N / ha) = Residue N Content (kg N / ha) * Emission Factor (fraction)`

214-223: Markdown lint: add blank lines around the emission-factor table

mdlint flags MD058; inserting an empty line before and after the table silences the warning and improves readability.

-    *   **Emission Factors for Manure and Compost:**
+    *   **Emission Factors for Manure and Compost:**
+
         | Application Method    | Grassland | Cropland | Bare Soil |
         | :-------------------- | :-------- | :------- | :-------- |
@@
         | Incorporation 2 Tracks| N/A       | N/A      | 0.46      |
+
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ddd7e7a and 3d8d58f.

📒 Files selected for processing (1)
  • fdm-docs/docs/balance/01-nitrogen.md (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
fdm-docs/docs/balance/01-nitrogen.md (1)
Learnt from: SvenVw
PR: SvenVw/fdm#134
File: fdm-calculator/src/balance/nitrogen/index.ts:236-238
Timestamp: 2025-05-26T10:32:15.538Z
Learning: In nitrogen balance calculations for agricultural systems, removal and volatilization are calculated as negative values by definition since they represent nitrogen losses from the system. The balance calculation uses addition (.add()) for all components because removal and volatilization are already negative, so adding them effectively subtracts the losses from the supply.
🪛 LanguageTool
fdm-docs/docs/balance/01-nitrogen.md

[uncategorized] ~211-~211: Loose punctuation mark.
Context: ...lizer applied. * TAN Content: p_nh4_rt (g N / kg) - The amount of t...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~212-~212: Loose punctuation mark.
Context: ...acal form. * Emission Factor: A dimensionless factor representing the...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~233-~233: Loose punctuation mark.
Context: ...(kg / ha). * Total N Content: p_n_rt (fraction). * `Emiss...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~234-~234: Loose punctuation mark.
Context: ...fraction). * Emission Factor: p_ef_nh3 (fraction). This factor can ...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~240-~240: Loose punctuation mark.
Context: ..._rt - p_nh4_rt). * p_no3_rt: Nitrate content. * p_nh4_rt`...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~241-~241: Loose punctuation mark.
Context: ... Nitrate content. * p_nh4_rt: Ammonium content (TAN). * `p_...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~242-~242: Loose punctuation mark.
Context: ...nium content (TAN). * p_s_rt: Sulfur content. * K_1, `K_2...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~243-~243: Loose punctuation mark.
Context: ...s_rt: Sulfur content. * K_1, K_2, K_3`: Empirical constants. ...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~260-~260: Loose punctuation mark.
Context: ...onCatalogue). * Emission Factor`: This factor is calculated based on the ...

(UNLIKELY_OPENING_PUNCTUATION)

🪛 markdownlint-cli2 (0.17.2)
fdm-docs/docs/balance/01-nitrogen.md

207-207: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


210-210: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


211-211: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


212-212: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


214-214: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


215-215: Tables should be surrounded by blank lines
null

(MD058, blanks-around-tables)


229-229: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


232-232: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


233-233: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


234-234: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


239-239: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


240-240: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


241-241: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


242-242: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


243-243: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


244-244: Unordered list indentation
Expected: 6; Actual: 12

(MD007, ul-indent)


245-245: Unordered list indentation
Expected: 6; Actual: 12

(MD007, ul-indent)


246-246: Unordered list indentation
Expected: 6; Actual: 12

(MD007, ul-indent)


247-247: Unordered list indentation
Expected: 6; Actual: 12

(MD007, ul-indent)


259-259: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


260-260: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


262-262: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


265-265: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)

⏰ Context from checks skipped due to timeout of 300000ms (2)
  • GitHub Check: calculator (22)
  • GitHub Check: core (22)
🔇 Additional comments (2)
fdm-docs/docs/balance/01-nitrogen.md (2)

229-234: Verify and document the units for p_n_rt, p_no3_rt, p_nh4_rt

Earlier sections define these parameters in g N / kg, but the text here calls them a “fraction”.
Either:

  1. They should be divided by 1000 in the formula (like the supply calculation), or
  2. The catalogue now stores them already as fractions – in which case earlier descriptions need updating.

Please confirm which representation is correct and adjust the documentation (and variable descriptions) for consistency.


262-266: Clarify lower bound for residue-based emission factor

Emission Factor = 0.41 × b_lu_n_residue – 5.42 becomes negative when b_lu_n_residue < 13.2 g N / kg.
Negative fractions are physically impossible; indicate that the factor is clamped to ≥ 0 (if that’s how the code behaves) or reference the rule that prevents low-N crops from producing negative values.

Comment thread fdm-docs/docs/balance/01-nitrogen.md
SvenVw and others added 2 commits June 26, 2025 09:25
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Sven Verweij <37927107+SvenVw@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
fdm-docs/docs/balance/01-nitrogen.md (1)

207-214: Unit-conversion step fixed 👍 — but indentation now violates MD007

The / 1000 conversion has been added (good catch), yet the inner bullet list is indented with 4 spaces instead of the 2-space increment configured by markdownlint (see static-analysis warnings on the same lines).
Consider reducing the indent to keep CI/preview clean.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3d8d58f and db42918.

📒 Files selected for processing (1)
  • fdm-docs/docs/balance/01-nitrogen.md (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
fdm-docs/docs/balance/01-nitrogen.md (1)
Learnt from: SvenVw
PR: SvenVw/fdm#134
File: fdm-calculator/src/balance/nitrogen/index.ts:236-238
Timestamp: 2025-05-26T10:32:15.538Z
Learning: In nitrogen balance calculations for agricultural systems, removal and volatilization are calculated as negative values by definition since they represent nitrogen losses from the system. The balance calculation uses addition (.add()) for all components because removal and volatilization are already negative, so adding them effectively subtracts the losses from the supply.
🪛 LanguageTool
fdm-docs/docs/balance/01-nitrogen.md

[uncategorized] ~211-~211: Loose punctuation mark.
Context: ...lizer applied. * TAN Content: p_nh4_rt (g N / kg) - The amount of t...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~212-~212: Loose punctuation mark.
Context: ...acal form. * Emission Factor: A dimensionless factor representing the...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~234-~234: Loose punctuation mark.
Context: ...(kg / ha). * Total N Content: p_n_rt (fraction). * `Emiss...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~235-~235: Loose punctuation mark.
Context: ...fraction). * Emission Factor: p_ef_nh3 (fraction). This factor can ...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~241-~241: Loose punctuation mark.
Context: ..._rt - p_nh4_rt). * p_no3_rt: Nitrate content. * p_nh4_rt`...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~242-~242: Loose punctuation mark.
Context: ... Nitrate content. * p_nh4_rt: Ammonium content (TAN). * `p_...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~243-~243: Loose punctuation mark.
Context: ...nium content (TAN). * p_s_rt: Sulfur content. * K_1, `K_2...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~244-~244: Loose punctuation mark.
Context: ...s_rt: Sulfur content. * K_1, K_2, K_3`: Empirical constants. ...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~261-~261: Loose punctuation mark.
Context: ...onCatalogue). * Emission Factor`: This factor is calculated based on the ...

(UNLIKELY_OPENING_PUNCTUATION)

🪛 markdownlint-cli2 (0.17.2)
fdm-docs/docs/balance/01-nitrogen.md

207-207: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


210-210: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


211-211: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


212-212: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


214-214: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


230-230: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


233-233: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


234-234: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


235-235: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


240-240: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


241-241: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


242-242: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


243-243: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


244-244: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)


245-245: Unordered list indentation
Expected: 6; Actual: 12

(MD007, ul-indent)


246-246: Unordered list indentation
Expected: 6; Actual: 12

(MD007, ul-indent)


247-247: Unordered list indentation
Expected: 6; Actual: 12

(MD007, ul-indent)


248-248: Unordered list indentation
Expected: 6; Actual: 12

(MD007, ul-indent)


260-260: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


261-261: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


263-263: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


266-266: Unordered list indentation
Expected: 4; Actual: 8

(MD007, ul-indent)

⏰ Context from checks skipped due to timeout of 300000ms (3)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: core (22)
  • GitHub Check: calculator (22)

Comment thread fdm-docs/docs/balance/01-nitrogen.md
Comment thread fdm-docs/docs/balance/01-nitrogen.md
@SvenVw SvenVw merged commit 5ffa05a into development Jun 26, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

2 participants