;;; log-std.dai --- Standard log parameterizations.
;;
;; This file contain the log parameterizations providing the summary
;; information relevant to non-expert users of Daisy.

(deflog column table
  "A log table for a specific column."
  ;; Let the user choose a column.
  (declare column String
	   "Name of column to log.  Use \"*\" to log all columns.")
  (column "*")
  ;; Add it to the log files.
  (parameter_names &old column)
  ;; Use column name as part of file name.
  (declare colfid string
           "File component name indicating column logged.")
  (colfid (cond ((string-equal "${column}" "*")
                 "")
                (true
                 "${column}_"))))

(deflog crop column
  "A log table for a specific crop."
  ;; Let the user choose a crop.
  (declare crop String
	   "Name of crop to log.  Use \"*\" to log all crops.")
  (crop "*")
  ;; Add it to the log files.
  (parameter_names &old crop)
  ;; Use crop name as part of file name.
  (declare cropfid string
           "File component name indicating crop logged.")
  (cropfid (cond ((string-equal "${crop}" "*")
                  "crop")
                 (true
                  "${crop}")))
  (declare cropfid2 string "\
File component name indicating crop logged. Empty if not specified.")
  (cropfid2 (cond ((string-equal "${crop}" "*")
		   "")
		  (true
		   "${crop}_"))))

(deflog chemical column 
  "A log table for a specific chemical."
  ;; Let the user choose a chemical.
  (declare chemical String
	   "Name of chemical to log.")
  (chemical "*") ; Not very useful, N dominates.  But needed for ref.manual.
  ;; Add it to the log files.
  (parameter_names &old chemical)
  ;; Use chemical name as part of file name.
  (declare chemfid string
           "File name component indicating chemical logged.")
  (chemfid (cond ((string-equal "${chemical}" "*")
                  "chemicals")
                 (true
                  "${chemical}"))))

(deflog biopore column
  "A log table for a specific biopore class."
  ;; Let the user choose a biopore.
  (declare biopore String
	   "Name of biopore to log.  Use \"*\" to log all biopore classes.")
  (biopore "*")
  ;; Add it to the log files.
  (parameter_names &old biopore)
  ;; Use biopore name as part of file name.
  (declare bioporefid string
           "File component name indicating biopore logged.")
  (bioporefid (cond ((string-equal "${biopore}" "*")
                     "biopores")
                    (true
                     "${biopore}"))))

(deflog "Crop" crop
  "Crop development and production."
  (where "${colfid}${cropfid}.dlf")
  (when daily)
  (entries (number (path column "${column}" Vegetation crops crops "${crop}"
                         Devel component DS)
                   (spec phenology component DS)
		   (dimension [DS])
                   (documentation "\
Development stage of the crop. 0 is emergence, 1 is flowering, 2 is ripe."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         Canopy Height)
                   (spec fixed CanopyStandard Height)
                   (dimension "cm")
                   (documentation "\
Height of crop."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         Canopy CAI)
                   (tag "LAI")
                   (spec fixed CanopyStandard CAI)
		   (dimension [m^2/m^2])
                   (documentation "\
Leaf area index."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         Root Depth)
                   (spec fixed RootSystem Depth)
                   (negate true)
                   (dimension "cm")
                   (documentation "\
Effective depths of root zone."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         Prod WLeaf)
                   (dimension "Mg DM/ha")
                   (spec fixed Production WLeaf)
                   (documentation "\
Dry matter content of leaves."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         Prod WDead)
                   (dimension "Mg DM/ha")
                   (spec fixed Production WDead)
                   (documentation "\
Dry matter content of dead (yellow) leaves still on crop."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         Prod WStem)
                   (dimension "Mg DM/ha")
                   (spec fixed Production WStem)
                   (documentation "\
Dry matter content of stem."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         Prod WSOrg)
                   (dimension "Mg DM/ha")
                   (spec fixed Production WSOrg)
                   (documentation "\
Dry matter content of storage organ (grain, tubber)."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         Prod WRoot)
                   (dimension "Mg DM/ha")
                   (spec fixed Production WRoot)
                   (documentation "\
Dry matter content of roots."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         CrpN Fixated)
                   (handle sum)
                   (spec fixed CrpN Fixated)
                   (dimension "kg N/ha")
                   (documentation "\
Atmospheric nitrogen fixated by the crop (root biome)."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         Prod NLeaf)
                   (dimension "kg N/ha")
                   (spec fixed Production NLeaf)
                   (documentation "\
Nitrogen content of the leaves."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         Prod NDead)
                   (dimension "kg N/ha")
                   (spec fixed Production NDead)
                   (documentation "\
Nitrogen content of dead (yellow) leaves still on the crop."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         Prod NStem)
                   (dimension "kg N/ha")
                   (spec fixed Production NStem)
                   (documentation "\
Nitrogen content of the stem."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         Prod NSOrg)
                   (dimension "kg N/ha")
                   (spec fixed Production NSOrg)
                   (documentation "\
Nitrogen content of the storage organ (grain or tubber)."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         Prod NRoot)
                   (dimension "kg N/ha")
                   (spec fixed Production NRoot)
                   (documentation "\
Nitrogen content of the roots."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         Root water_stress)
                   (spec fixed RootSystem water_stress)
		   (dimension [])
                   (handle max)
                   (documentation "\
Highest water stress experienced in the period.

When the root system cannot fullfil the demand of water from the canopy, 
the crop experience water stress, and production suffers.

The value is between 0 (no water stress) and 1 (no production)."))
           (number (path column "${column}" Vegetation crops crops "${crop}"
                         CrpN nitrogen_stress)
                   (handle max)
                   (spec fixed CrpN nitrogen_stress)
		   (dimension [])
                   (documentation "\
Highest nitrogen stress experienced in the period.

When the crop nitrogen content is below a critical level, crop
experience nitrogen stress, and production suffers.

The value is between 0 (no nitrogen stress) and 1 (no production)."))
	   (number (path column "${column}" Vegetation crops crops "${crop}"
                         CStage table value)
		   (tag "Phenology")
		   (handle current)
		   (spec cstage table value)
		   (documentation "\
Current phenological stage.

For most crops, this is -1 (not yet emerged), 0 (vegetative), 
1 (reproductive), or 2 (ripe). Some crops may show other scaled, like BBCH."))))

(defsummary "Field water balance" balance
  "Water balance for the complete system."
  (precision 1)
  (require_top true)
  (input "Precipitation" "Irrigation")
  (output "Actual evapotranspiration"
          "Matrix percolation" "Biopore percolation"
          "Matrix drain flow" "Biopore drain flow" "Runoff")
  (content "Soil matrix water" "Biopore water" "Surface water"))

(deflog "Field water" column
  "Information about water input, output and content for the system.

The intended use of this log is large scale water balance, for
example reservoir management. It provide information about how
much water is in the field (down to a specified depth), where it
is located in the field (surface, soil matrix or in biopores), as
well as the sources, sinks and amounts of water entering or
leaving the system.  It does not provide information about
internal translocation of water between surface, soil matrix and
biopores, use see the 'Soil water' log instead for that.

For the balances of this log to work, you must include the entire
root zone, as well as the biopore zone."
  (where "${colfid}field_water.dlf")
  (when hourly)
  (summary "Field water balance")
  (entries (number (tag "Precipitation")
		   (documentation "\
Total amount of water entering the system as rain and snow.")
                   (path weather "*" precipitation)
                   (spec weather weather precipitation)
                   (handle sum)
                   (dimension "mm"))
           (number (tag "Irrigation")
		   (documentation "\
Total amount of water added to the system from irrigation.")
                   (path column "${column}" Bioclimate "*" irrigation_total)
                   (spec bioclimate default irrigation_total)
                   (handle sum)
                   (dimension "mm"))
           (number (tag "Potential evapotranspiration")
		   (documentation "\
There is energy enough to evaporate this amount of water.")
                   (path column "${column}" Bioclimate "*" total_ep)
                   (spec bioclimate default total_ep)            
                   (handle sum)
                   (dimension "mm"))
           (number (tag "Actual evapotranspiration")
		   (documentation "\
Amount of water removed by evaporation and transpiration.

The evaporation part covers water evaporated from the soil surface,
the snow pack, or intertercepted water on the canopy.  The
transpiration covers water removed from the soil through the root
system and the stomata on the leafs.

The actual evapotranspiration will be lower than the potential
evapotranspiration when the available water is insufficient.")
                   (path column "${column}" Bioclimate "*" total_ea)
                   (spec bioclimate default total_ea)            
                   (handle sum)
                   (dimension "mm"))
           (flux_bottom (tag "Matrix percolation")
			(documentation "\
This is the amount of water leaving the system through the soil bottom.
It can be negative if there is cabilary rise.")
                        (path column "${column}" SoilWater q)
                        (spec fixed SoilWater q)
                        (handle sum)
                        (negate true)
                        (dimension "mm"))
           (flux_bottom (tag "Biopore percolation")
			(documentation "\
This is the amount of water leaving the system through biopores.")
                        (path column "${column}" SoilWater q_p)
                        (spec fixed SoilWater q_p)
                        (handle sum)
                        (negate true)
                        (dimension "mm"))
           (interval (tag "Matrix drain flow")
		     (documentation "\
Amount of water leaving the system through the soil matrix to drain pipes.

Note that the total amount of water in the drain pipes may be higher
than this if you have specified the 'to' parameter, as the whole soil
profile will contribute to the drain flow, and only the contributions
from the soil interval being logged is counted here.")
                     (path column "${column}" SoilWater S_soil_drain)
                     (spec fixed SoilWater S_soil_drain)
                     (handle sum)
                     (dimension "mm"))
           (interval (tag "Biopore drain flow")
		     (documentation "\
Amount of water leaving the system through biopores to drain pipes.

Note that the total amount of water in the drain pipes may be higher
than this if you have specified the 'to' parameter, as the whole soil
profile will contribute to the drain flow, and only the contributions
from the soil interval being logged is counted here.")
                     (path column "${column}" SoilWater S_p_drain)
                     (spec fixed SoilWater S_p_drain)
                     (handle sum)
                     (dimension "mm"))
           (number (tag "Runoff")
		   (documentation "\
Amount of water running of the surface.

This is intended to simulate water runoff from a surface with a slop.
However, since Daisy is a one dimensional model, the water have
nowhere go to.  For typical setups, this value will be zero.")
                   (path column "${column}" Surface default runoff)
                   (spec surface default runoff)
                   (handle sum)
                   (dimension "mm"))
           (interval (tag "Biopore water")
                     (documentation "\
The total amount of biopore water in the soil interval")
                     (path column "${column}" SoilWater Theta_p)
                     (spec fixed SoilWater Theta_p)
                     (dimension "mm"))
           (interval (tag "Soil matrix water")
		     (documentation "\
Total water content of the soil.")
                     (path column "${column}" SoilWater Theta)
                     (spec fixed SoilWater Theta)
                     (dimension "mm"))
           (number (tag "Surface water")
		   (documentation "Total water stored on the surface.
This includes the snow pack, intercepted water, ponding, and water in
the litter layer.")
                   (path column "${column}" surface_water) 
                   (spec column default surface_water)
                   (dimension "mm"))))

(defsummary "Soil water balance" balance
  "Balance for all water in the soil."
  (precision 1)
  (input "Matrix infiltration" "Biopore infiltration" 
         "Subsoil irrigation" "Tillage")
  (output "Matrix percolation" "Biopore percolation" 
          "Matrix direct drain flow" "Biopore drain flow"
          "Root extraction" "Freezing")
  (content "Matrix water" "Biopore water"))

(defsummary "Biopore water balance" balance
  "Water balance for the biopore domain alone."
  (precision 1)
  (input "Biopore infiltration"
         "Matrix to biopores" "Matrix indirect drain flow")
  (output "Biopore percolation" "Biopores to matrix" "Biopore drain flow")
  (content "Biopore water"))

(defsummary "Matrix water balance" balance
  "Balance for the matrix domain in the soil."
  (precision 1)
  (input "Matrix infiltration" "Biopores to matrix" 
         "Subsoil irrigation" "Tillage")
  (output "Matrix percolation" "Matrix to biopores" 
          "Matrix direct drain flow" "Matrix indirect drain flow" 
          "Root extraction" "Freezing")
  (content "Matrix water"))

(deflog "Soil water" column
  "Information about water input, output and content for the soil.

The intended use of this log is small scale water management, for
example irrigation optimization.  Above ground water is
considered external to the system.  The balances provided by the
log will work on any subset of the soil that matches the
discretization."
  (where "${colfid}soil_water.dlf")
  (when hourly)
  (summary "Soil water balance" "Biopore water balance" "Matrix water balance")
  (entries (flux_top (tag "Matrix infiltration")
                     (documentation "\
Amount of water entering the soil through the matrix system.

This is the normal infiltration channel.")
                     (path column "${column}" SoilWater q)
                     (spec fixed SoilWater q)
                     (handle sum)
                     (negate true)
                     (dimension "mm"))
           (flux_bottom (tag "Matrix percolation")
                     (documentation "\
Amount of water leaving the soil through the matrix system.

This is the normal percolation channel.")
                        (path column "${column}" SoilWater q)
                        (spec fixed SoilWater q)
                        (handle sum)
                        (negate true)
                        (dimension "mm"))
           (flux_top (tag "Biopore infiltration")
                     (documentation "\
Amount of water entering the soil through the biopores.")
                     (path column "${column}" SoilWater q_p)
                     (spec fixed SoilWater q_p)
                     (handle sum)
                     (negate true)
                     (dimension "mm"))
           (flux_bottom (tag "Biopore percolation")
                     (documentation "\
Amount of water leaving the soil through the biopore system.")
                        (path column "${column}" SoilWater q_p)
                        (spec fixed SoilWater q_p)
                        (handle sum)
                        (negate true)
                        (dimension "mm"))
           (interval (tag "Subsoil irrigation")
                     (documentation "\
mount of water incorporated directly into the soil.

his include both subsoil irrigation and any water in fertilizer
irectly incorporated into the soil.")
                     (path column "${column}" SoilWater S_incorp)
                     (spec fixed SoilWater S_incorp)
                     (handle sum)
                     (negate true)
                     (dimension "mm"))
           (interval (tag "Matrix direct drain flow")
                     (documentation "\
Flow from matrix directly to the drain pipes.

This does not count water that flow to the drain pipes through biopores.

Note that water flow to the drain pipes from all soil layers, both
above and below the pipes, and even from the automatically inserted
aquitard horizon that lies below the normal soil horizons.  So if you
have specified less than the full soil profile, that is, if you have
set the 'from' or 'to' parameters, the amount you see logged here will
be less than the total amount of water in the drain pipes.

If the soil has no drain pipes this amount will be zero.")
                     (path column "${column}" SoilWater S_soil_drain)
                     (spec fixed SoilWater S_soil_drain)
                     (handle sum)
                     (dimension "mm"))
           (interval (tag "Matrix indirect drain flow")
                     (documentation "\
Flow from matrix indirectly to the drain pipes.

This only counts water that flow to the drain pipes through biopores.")
                     (path column "${column}" SoilWater S_indirect_drain)
                     (spec fixed SoilWater S_indirect_drain)
                     (handle sum)
                     (dimension "mm"))
           (interval (tag "Biopore drain flow")
                     (documentation "\
Water in the interval that flows from the biopores to the drain pipes.

Note that water flow to the drain pipes from all soil layers, both
above and below the pipes, and even from the automatically inserted
aquitard horizon that lies below the normal soil horizons.  So if you
have specified less than the full soil profile, that is, if you have
set the 'from' or 'to' parameters, the amount you see logged here will
be less than the total amount of water in the drain pipes.

If the soil has no biopores connected directly to the drain, this amount
will be zero.")
                     (path column "${column}" SoilWater S_p_drain)
                     (spec fixed SoilWater S_p_drain)
                     (handle sum)
                     (dimension "mm"))
           (interval (tag "Root extraction")
                     (documentation "\
Water in the soil interval extracted by the roots.

This number will be equal to the transpiration if the soil interval
being logged includes the entire root zone.")
                     (path column "${column}" SoilWater S_root)
                     (spec fixed SoilWater S_root)
                     (handle sum)
                     (dimension "mm"))
           (interval (tag "Freezing")
                     (documentation "\
Water in the soil interval turned into ice.

This number will be zero unless the 'enable_ice' SoilHeat parameter
has been set.")
                     (path column "${column}" SoilWater S_ice_water)
                     (spec fixed SoilWater S_ice_water)
                     (negate true)
                     (handle sum)
                     (dimension "mm"))
           (interval (tag "Tillage")
                     (documentation "\
Net amount of water being added to the soil interval by tillage operations.

This number will be zero if the soil interval includes the all the
soil affected by the tillage operation, which is the normal case.  The
number will be negative if more water have been added than removed by
the tillage operation.")
                     (path column "${column}" SoilWater tillage)
                     (spec fixed SoilWater tillage)
                     (handle sum)
                     (dimension "mm"))
           (interval (tag "Matrix to biopores")
                     (documentation "\
Amount of water leaving the soil matrix to the biopores.
This does not include water that flows to drain connected biopores.")
                     (path column "${column}" SoilWater S_M2B)
                     (spec fixed SoilWater S_M2B)
                     (handle sum)
                     (dimension "mm"))
           (interval (tag "Biopores to matrix")
                     (documentation "\
Amount of water leaving the biopores to the matrix.")
                     (path column "${column}" SoilWater S_B2M)
                     (spec fixed SoilWater S_B2M)
                     (handle sum)
                     (dimension "mm"))
           (interval (tag "Matrix water")
                     (documentation "\
The total amount of soil matrix water in the soil interval")
                     (path column "${column}" SoilWater Theta)
                     (spec fixed SoilWater Theta)
                     (dimension "mm"))
           (interval (tag "Biopore water")
                     (documentation "\
The total amount of biopore water in the soil interval")
                     (path column "${column}" SoilWater Theta_p)
                     (spec fixed SoilWater Theta_p)
                     (dimension "mm"))
           ))

(defsummary "Field nitrogen balance" balance
  "Nitrogen balance for the complete system."
  (precision 3)
  (require_top true)
  (input "Min-Surface-Fertilizer"
         "Min-Soil-Fertilizer"
	 "Org-Fertilizer"
         "Deposition"
         Fixated "Seed")
  (output "N2O-Nitrification" "NO3-Denitrification"
          "Matrix-Leaching" "Biopore-Leaching"
          "Soil-Drain" "Biopore-Drain"
          "Surface-Loss"
          "Harvest")
  (content "Min-Surface" "Min-Soil" "Min-Biopores"
	   "Org-Surface" "Org-Soil" "Crop"))

(defsummary "Field mineral nitrogen balance" balance
  "Mineral balance for the complete system."
  (precision 3)
  (require_top true)
  (input "Min-Surface-Fertilizer" "Min-Soil-Fertilizer"
	 "Deposition" "Mineralization")
  (output "Immobilization" "Matrix-Leaching" "Biopore-Leaching"
          "Soil-Drain" "Biopore-Drain" 
          "Surface-Loss" "Crop-Uptake"
          "N2O-Nitrification" "NO3-Denitrification")
  (content "Min-Surface" "Min-Soil" "Min-Biopores"))

(defsummary "Field crop nitrogen balance" balance
  "Crop nitrogen balance for the complete system."
  (precision 3)
  (require_top true)
  (input "Seed" Fixated "Crop-Uptake")
  (output "Harvest" "Residuals-Surface" "Residuals-Soil")
  (content "Crop"))

(defsummary "Field organic nitrogen balance" balance
  "Organic nitrogen balance for the complete system."
  (precision 3)
  (require_top true)
  (input "Residuals-Surface" "Residuals-Soil" "Org-Fertilizer"
         "Immobilization")
  (output "Mineralization")
  (content "Org-Surface" "Org-Soil"))

(deflog "Field nitrogen" column
  "Nitrogen input, output, transformation and content for the system.

The intended use of this log is large scale nitrogen balance, for
example reservoir management. It provide information about how
much nitrogen is in the field (down to a specified depth), where
it is located in the field (surface, soil matrix or in biopores),
what form is has (crop, soil organic matter, or mineral) as well
as the sources, sinks and amounts of nitrogen entering or leaving
the system, and transformation between the four forms.  It does
not provide information about internal translocation of nitrogen
between surface, soil matrix and biopores, use see the 'Soil
nitrogen' log instead for that.

For the balances of this log to work, you must include the entire
root zone"
  (declare unit String "Base unit.")
  (unit "kg N/ha")
  (where "${colfid}field_nitrogen.dlf")
  (summary "Field nitrogen balance" "Field mineral nitrogen balance" 
           "Field organic nitrogen balance" "Field crop nitrogen balance")
  (when hourly)
  (entries (number (path column "${column}" 
			 Chemistry "*" trace N spray)
                   (spec chemical default spray)
		   (handle sum)
                   (documentation "\
Mineral fertilizer added above the soil surface.")
                   (tag "Min-Surface-Fertilizer")
                   (dimension "${unit}"))
           (interval (path column "${column}"
			   Chemistry "*" trace N S_external)
                     (spec chemical default S_external)
                     (documentation "\
Mineral fertilizer incorporated into the soil.")
                     (tag "Min-Soil-Fertilizer")
                     (handle sum)
                     (dimension "${unit}"))
           (number (path column "${column}" 
			 Chemistry "*" trace N deposit)
                   (spec chemical default deposit)
                   (handle sum)
                   (documentation "\
Nitrogen added to the soil surface as atmospheric deposition.")
                   (tag "Deposition")
                   (dimension "${unit}"))
           (flux_bottom (path column "${column}"
			      Chemistry "*" trace N J_matrix)
                        (spec chemical default J_matrix)
                        (handle sum)
                        (negate true)
                        (documentation "\
Loss from leaching through the soil matrix below the specified depth.  
This can be negative in case of cabilary rise.")
                        (tag "Matrix-Leaching")
                        (dimension "${unit}"))
           (flux_bottom (path column "${column}"
			      Chemistry "*" trace N J_tertiary)
                        (spec chemical default J_tertiary)
                        (handle sum)
                        (negate true)
                        (documentation "\
Loss from leaching through biopores below the specified depth.")
                        (tag "Biopore-Leaching")
                        (dimension "${unit}"))
           (interval (path column "${column}"
			   Chemistry "*" trace N S_soil_drain)
                     (spec chemical default S_soil_drain)
                     (documentation "\
Loss of nitrogen from the soil matrix to drain pipes.")
                     (tag "Soil-Drain")
                     (handle sum)
                     (negate true)
                     (dimension "${unit}"))
           (interval (path column "${column}"
			   Chemistry "*" trace N S_p_drain)
                     (spec chemical default S_p_drain)
                     (documentation "\
Loss of nitrogen from the biopores to drain pipes.")
                     (tag "Biopore-Drain")
                     (handle sum)
                     (dimension "${unit}"))
           (number (path column "${column}" 
			 Chemistry "*" trace N top_loss)
                   (spec chemical default top_loss)
                   (handle sum)
                   (documentation "\
Loss of mineral nitrogen stored above surface.

This includes volatililzation, surface runoff, mineral nitrogen on
leaves removed at harvest, and chemical transformations.")
                   (tag "Surface-Loss")
                   (dimension "${unit}"))
           (number (path column "${column}" 
			 Chemistry "*" trace N top_storage)
                   (spec chemical default top_storage)
                   (documentation "\
Mineral nitrogen stored above the soil surface.
This includes leaves, ponded water, etc.")
                   (tag "Min-Surface")
                   (dimension "${unit}"))
           (interval (path column "${column}"
			   Chemistry "*" trace N M)
                     (spec chemical default M)
                     (documentation "\
Mineal nitrogen stored in the soil matrix.")
                     (tag "Min-Soil")
                     (dimension "${unit}"))
           (interval (path column "${column}"
			   Chemistry "*" trace N M_tertiary)
                     (spec chemical default M_tertiary)
                     (documentation "\
Mineal nitrogen stored in the biopores.")
                     (tag "Min-Biopores")
                     (dimension "${unit}"))
           (interval (path column "${column}"
			   Chemistry "*" trace N M_error)
                     (spec chemical default M_error)
                     (documentation "\
Mineral nitrogen 'borrowed from the future'. 

This can be non-zero if the processes that remove nitrogen from some
place in the soil are fast compared to the timestep.  If this
happens, you should rerun the simulation withh a shorter
timestep. Or readjust the system to slow down those processes
that remove mineral nitrogen.")
                     (tag "Error")
                     (dimension "${unit}"))
           (interval (path column "${column}" OrganicMatter "*" NH4_source)
                     (spec organic default NH4_source)
                     (documentation "\
NH4 produced in the soil matrix by mineralization of organic matter.")
                     (tag "Mineralization")
                     (handle sum)
                     (dimension "${unit}"))
           (interval (path column "${column}" OrganicMatter "*" NO3_source)
                     (spec organic default NO3_source)
                     (documentation "\
NO3 immobilized as a result of organic matter turnover.")
                     (tag "Immobilization")
                     (negate true)
                     (handle sum)
                     (dimension "${unit}"))
           (interval (path column "${column}"
                           Chemistry "*" trace N S_root)
                     (spec chemical default S_root)
                     (documentation "\
Nitrogen uptake by the roots.")
                     (tag "Crop-Uptake")
                     (negate true)
                     (handle sum)
                     (dimension "${unit}"))
           (number (path column "${column}" 
			 Chemistry "*" trace NH4 surface_decomposed)
                   (spec chemical default surface_decomposed)
                   (handle sum)
                   (documentation "\
NH4-fertilizer lost during the application.")
                   (tag "Volatilization")
                   (dimension "${unit}"))
	   (interval (path column "${column}" Chemistry "*"
                           combine "N" reaction nitrification N2O)
                     (documentation "\
N2O production in the soil matrix due to nitrification.

The N2O produced is assumed to escape to the atmosphere, and will
not be traced by the model.")
                     (tag "N2O-Nitrification")
                     (handle sum)
                     (dimension "${unit}")
                     (spec reaction nitrification N2O))
	   (interval (path column "${column}" Chemistry "*"
                           combine "N" reaction denitrification converted)
                     (documentation "\
Loss of NO3-N due to denitrification.")
                     (tag "NO3-Denitrification")
                     (handle sum)
                     (dimension "${unit}")
                     (spec reaction denitrification converted))
	   (interval (path column "${column}" Chemistry "*"
                           combine "N" reaction denitrification
			   denprod Parton1996 N2O)
                     (documentation "\
N2O-N released by denitrification.")
                     (tag "N2O-Denitrification")
                     (handle sum)
                     (dimension "${unit}")
                     (spec denprod Parton1996 N2O))
           (number (tag Fixated)
                   (path column "${column}"
			 Vegetation component N_fixated)
                   (documentation "\
Atmospheric nitrogen fixated by the crop.")
                   (spec vegetation component N_fixated)
                   (handle sum)
                   (dimension "${unit}"))
           (number (path column "${column}" OrganicMatter "*" fertilized_N)
                   (spec organic default fertilized_N)
                   (handle sum)
                   (documentation "\
Organically bound nitrogen supplied by fertilizers.")
                   (tag "Org-Fertilizer")
                   (dimension "${unit}"))
           (number (path column "${column}" seed_N)
                   (spec column default seed_N)
                   (handle sum)
                   (documentation "\
Nitrogen supplied in seeds when sowing.")
                   (tag "Seed")
                   (dimension "${unit}"))
           (number (path column "${column}" harvest_N)
                   (spec column default harvest_N)
                   (handle sum)
                   (documentation "\
Nitrogen removed as part of the harvest.")
                   (tag "Harvest")
                   (dimension "${unit}"))
           (number (path column "${column}" residuals_N_top)
                   (spec column default residuals_N_top)
                   (handle sum)
                   (documentation "\
Above ground plant residuals.

This includes both exfoiliation during crop growth, and any above
ground residuals left after harvest.")
                   (tag "Residuals-Surface")
                   (dimension "${unit}"))
           (number (path column "${column}" residuals_N_root)
                   (spec column default residuals_N_root)
                   (handle sum)
                   (documentation "\
Below ground plant residuals.

This includes root death and rhizodeposition during plant growth,
as well as the roots left after harvest.")
                   (tag "Residuals-Soil")
                   (dimension "${unit}"))
           (number (tag "Org-Surface")
                     (documentation "\
Nitrogen content of organic material on the soil surface.

This does not include nitrogen in the crop.")
                   (path column "${column}"
			 OrganicMatter "*" am "*" om "*" top_N)
                   (spec AOM component top_N)
                   (dimension "${unit}"))
           (interval (tag "Org-Soil")
                     (documentation "\
Nitrogen content of organic material below the soil surface.

This does not include nitrogen in living roots.")
                     (path column "${column}" OrganicMatter "*" total_N)
                     (spec organic default total_N)
                     (dimension "${unit}"))
           (number (tag "Crop")
                   (documentation "\
Nitrogen content of the crop, excluding dead leaves.")
                   (path column "${column}" Vegetation component N)
                   (spec vegetation component N)
                   (dimension "${unit}"))))

(defsummary N_balance balance
  "Unit is kg, precion is g."
  (precision 3))

(defsummary "Soil NO3- balance" N_balance
  "NO3- balance for a soil interval."
  (input "NO3-Matrix-In" "NO3-Biopore-In" "NO3-Incorp" "NO3-Tillage"
	 "NO3-Nitrification" )
  (output "NO3-Leak-Matrix" "NO3-Leak-Biopore" 
	  "NO3-Soil-Drain" "NO3-Biopore-Drain"
	  "NO3-Uptake" "NO3-Denitrification" "NO3-Immobilization")
  (content "NO3-Soil" "NO3-Biopores"))

(defsummary "Soil NH4+ balance" N_balance
  "NH4+ balance for a soil interval."
  (input "NH4-Matrix-In" "NH4-Biopore-In" "NH4-Incorp" "NH4-Tillage"
	 "NH4-Mineralization")
  (output "NH4-Leak-Matrix" "NH4-Leak-Biopore" 
	  "NH4-Soil-Drain" "NH4-Biopore-Drain"
	  "NH4-Uptake"  "NH4-Nitrification")
  (content "NH4-Soil" "NH4-Biopores"))


(defsummary "Soil mineral-N balance" N_balance
  "Mineral N balance for a soil interval."
  (input "NO3-Matrix-In" "NH4-Matrix-In" "NO3-Biopore-In" "NH4-Biopore-In" 
	 "NO3-Incorp" "NH4-Incorp"
	 "NO3-Tillage" "NH4-Tillage"
	 "NH4-Mineralization")
  (output "NO3-Leak-Matrix" "NH4-Leak-Matrix" 
	  "NO3-Leak-Biopore" "NH4-Leak-Biopore"
	  "NO3-Soil-Drain" "NH4-Soil-Drain" 
	  "NO3-Biopore-Drain" "NH4-Biopore-Drain"
	  "NO3-Uptake" "NH4-Uptake"
	  "NO3-Denitrification" "N2O-Nitrification"
	  "NO3-Immobilization")
  (content "NO3-Soil" "NH4-Soil" "NO3-Biopores" "NH4-Biopores"))

(defsummary "Soil organic-N balance" N_balance
  "Organic nitrogen balance for a soil interval."
  (input "Residuals-N" "Tillage-Org_N" "NO3-Immobilization" "Bioincorporation")
  (output "NH4-Mineralization")
  (content "AOM" "SOM" "SMB" "Buffer"))

(defsummary "Soil N balance" N_balance
  "Nitrogen balance for a soil interval."
  (input "NO3-Matrix-In" "NH4-Matrix-In" "NO3-Biopore-In" "NH4-Biopore-In" 
	 "NO3-Incorp" "NH4-Incorp"
         "NO3-Tillage" "NH4-Tillage"
	 "Residuals-N" "Tillage-Org_N" "Bioincorporation")
  (output "NO3-Leak-Matrix" "NH4-Leak-Matrix" 
	  "NO3-Leak-Biopore" "NH4-Leak-Biopore"
	  "NO3-Soil-Drain" "NH4-Soil-Drain" 
	  "NO3-Biopore-Drain" "NH4-Biopore-Drain"
	  "NO3-Uptake" "NH4-Uptake"
	  "NO3-Denitrification" "N2O-Nitrification"	  )
  (content "NO3-Soil" "NH4-Soil" "NO3-Biopores" "NH4-Biopores"
	   "AOM" "SOM" "SMB" "Buffer"))

(deflog "Soil nitrogen" column
  "Nitrogen input, output, transformation and content for the soil."
  (declare unit String "Base unit.")
  (unit "kg N/ha")
  (where "${colfid}soil_nitrogen.dlf")
  (summary "Soil NO3- balance" "Soil NH4+ balance" 
	   "Soil mineral-N balance" "Soil organic-N balance"
	   "Soil N balance")
  (when hourly)
  (entries ;; Year Month MDay Hour
           (flux_top (path column "${column}" Chemistry "*" trace NO3 J_matrix)
                     (documentation "\
NO3-N infiltration.")
                     (tag "NO3-Matrix-In")
                     (handle sum)
                     (negate true)
                     (dimension "${unit}")
                     (spec chemical default J_matrix))
           (flux_top (path column "${column}" Chemistry "*" 
			   trace NO3 J_tertiary)
                     (documentation "\
NO3-N infiltration.")
                     (tag "NO3-Biopore-In")
                     (handle sum)
                     (negate true)
                     (dimension "${unit}")
                     (spec chemical default J_tertiary))
           (flux_bottom (path column "${column}" Chemistry "*" trace NO3 J_matrix)
                        (documentation "\
NO3-N leaching.")
                        (tag "NO3-Leak-Matrix")
                        (negate true)
                        (handle sum)
                        (dimension "${unit}")
                        (spec chemical default J_matrix))
           (flux_bottom (path column "${column}" Chemistry "*"
			      trace NO3 J_tertiary)
                        (documentation "\
NO3-N leaching.")
                        (tag "NO3-Leak-Biopore")
                        (negate true)
                        (handle sum)
                        (dimension "${unit}")
                        (spec chemical default J_tertiary))
           (interval (path column "${column}" Chemistry "*" trace 
                           NO3 S_M2B)
                     (documentation "\
Movement of NO3-N from soil matrix to biopores.")
                     (tag "NO3-to-Biopores")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_M2B))
           (interval (path column "${column}" Chemistry "*" trace 
                           NO3 S_B2M)
                     (documentation "\
Movement of NO3-N from soil matrix to biopores.")
                     (tag "NO3-from-Biopores")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_B2M))
           (interval (path column "${column}" Chemistry "*" 
			   trace NO3 S_soil_drain)
                     (documentation "\
NO3-N lost to drains directly from the soil matrix.")
                     (tag "NO3-Soil-Drain")
                     (negate true)
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_soil_drain))
           (interval (path column "${column}" Chemistry "*" 
			   trace NO3 S_indirect_drain)
                     (documentation "\
NO3-N lost to drains from the soil matrix, through biopores.")
                     (tag "NO3-Indirect-Drain")
                     (negate true)
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_indirect_drain))
           (interval (path column "${column}" Chemistry "*" 
			   trace NO3 S_p_drain)
                     (documentation "\
NO3-N lost to drains from biopores.")
                     (tag "NO3-Biopore-Drain")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_p_drain))
           (interval (path column "${column}" Chemistry "*" 
			   trace NO3 S_external)
                     (documentation "\
NO3-N incorporated directly into the soil.")
                     (tag "NO3-Incorp")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_external))
           (interval (path column "${column}" Chemistry "*" 
			   trace NO3 tillage)
                     (documentation "\
NO3-N added by tillage operations.")
                     (tag "NO3-Tillage")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default tillage))
           (interval (path column "${column}" Chemistry "*" 
                           trace NO3 S_root)
                     (documentation "\
NO3-N removed by plant roots.")
                     (tag "NO3-Uptake")
                     (handle sum)
		     (negate true)
                     (dimension "${unit}")
                     (spec chemical default S_root))
           (interval (path column "${column}" Chemistry "*" trace NO3 M)
                     (documentation "\
NO3-N content of the soil matrix (excluding biopores).")
                     (tag "NO3-Soil")
                     (dimension "${unit}")
                     (spec chemical default M))
           (interval (path column "${column}" Chemistry "*"
			   trace NO3 M_tertiary)
                     (documentation "\
NO3-N content of the biopores.")
                     (tag "NO3-Biopores")
                     (dimension "${unit}")
                     (spec chemical default M))
           (interval (path column "${column}" Chemistry "*" trace NO3 M_error)
                     (documentation "\
NO3-N borrowed from the future.  

This may be temporarily non-zero if some process remove NO3 too
fast compared to the time step.")
                     (tag "NO3-Error")
                     (dimension "${unit}")
                     (spec chemical default M_error))
           (flux_top (path column "${column}" Chemistry "*" trace NH4 J_matrix)
                     (documentation "\
NH4-N infiltration.")
                     (tag "NH4-Matrix-In")
                     (handle sum)
                     (negate true)
                     (dimension "${unit}")
                     (spec chemical default J_matrix))
           (flux_top (path column "${column}" Chemistry "*" 
			   trace NH4 J_tertiary)
                     (documentation "\
NH4-N infiltration.")
                     (tag "NH4-Biopore-In")
                     (handle sum)
                     (negate true)
                     (dimension "${unit}")
                     (spec chemical default J_tertiary))
           (flux_bottom (path column "${column}" Chemistry "*" trace NH4 J_matrix)
                        (documentation "\
NH4-N leaching.")
                        (tag "NH4-Leak-Matrix")
                        (negate true)
                        (handle sum)
                        (dimension "${unit}")
                        (spec chemical default J_matrix))
           (flux_bottom (path column "${column}" Chemistry "*"
			      trace NH4 J_tertiary)
                        (documentation "\
NH4-N leaching.")
                        (tag "NH4-Leak-Biopore")
                        (negate true)
                        (handle sum)
                        (dimension "${unit}")
                        (spec chemical default J_tertiary))
           (interval (path column "${column}" Chemistry "*" trace 
                           NH4 S_M2B)
                     (documentation "\
Movement of NH4-N from soil matrix to biopores.")
                     (tag "NH4-to-Biopores")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_M2B))
           (interval (path column "${column}" Chemistry "*" trace 
                           NH4 S_B2M)
                     (documentation "\
Movement of NH4-N from soil matrix to biopores.")
                     (tag "NH4-from-Biopores")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_B2M))
           (interval (path column "${column}" Chemistry "*" 
			   trace NH4 S_soil_drain)
                     (documentation "\
NH4-N lost to drains directly from the soil matrix.")
                     (tag "NH4-Soil-Drain")
                     (negate true)
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_soil_drain))
           (interval (path column "${column}" Chemistry "*" 
			   trace NH4 S_indirect_drain)
                     (documentation "\
NH4-N lost to drains from the soil matrix, through biopores.")
                     (tag "NH4-Indirect-Drain")
                     (negate true)
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_indirect_drain))
           (interval (path column "${column}" Chemistry "*" 
			   trace NH4 S_p_drain)
                     (documentation "\
NH4-N lost to drains from biopores.")
                     (tag "NH4-Biopore-Drain")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_p_drain))
           (interval (path column "${column}" Chemistry "*" 
			   trace NH4 S_external)
                     (documentation "\
NH4-N incorporated directly into the soil.")
                     (tag "NH4-Incorp")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_external))
           (interval (path column "${column}" Chemistry "*" 
			   trace NH4 tillage)
                     (documentation "\
NH4-N added by tillage operations.")
                     (tag "NH4-Tillage")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default tillage))
           (interval (path column "${column}" Chemistry "*" 
                           trace NH4 S_root)
                     (documentation "\
NH4-N removed by plant roots.")
                     (tag "NH4-Uptake")
                     (handle sum)
		     (negate true)
                     (dimension "${unit}")
                     (spec chemical default S_root))
           (interval (path column "${column}" Chemistry "*" trace NH4 M)
                     (documentation "\
NH4-N content of the soil matrix (excluding biopores).")
                     (tag "NH4-Soil")
                     (dimension "${unit}")
                     (spec chemical default M))
           (interval (path column "${column}" Chemistry "*"
			   trace NH4 M_tertiary)
                     (documentation "\
NH4-N content of the biopores.")
                     (tag "NH4-Biopores")
                     (dimension "${unit}")
                     (spec chemical default M))
           (interval (path column "${column}" Chemistry "*" trace NH4 M_error)
                     (documentation "\
NH4-N borrowed from the future.  

This may be temporarily non-zero if some process remove NH4 too
fast compared to the time step.")
                     (tag "NH4-Error")
                     (dimension "${unit}")
                     (spec chemical default M_error))
	   (interval (path column "${column}" Chemistry "*"
                           combine "N" reaction denitrification converted)
                     (documentation "\
NO3-N removed by denitrification.")
                     (tag "NO3-Denitrification")
                     (handle sum)
                     (dimension "${unit}")
                     (spec reaction denitrification converted))
	   (interval (path column "${column}" Chemistry "*"
                           combine "N" reaction denitrification
			   denprod Parton1996 N2O)
                     (documentation "\
N2O-N released by denitrification.")
                     (tag "N2O-Denitrification")
                     (handle sum)
                     (dimension "${unit}")
                     (spec denprod Parton1996 N2O))
	   (interval (path column "${column}" Chemistry "*"
                           combine "N" reaction nitrification NH4)
                     (documentation "\
NH4-N removed by nitrification.")
                     (tag "NH4-Nitrification")
                     (handle sum)
                     (dimension "${unit}")
                     (spec reaction nitrification NH4))
	   (interval (path column "${column}" Chemistry "*"
                           combine "N" reaction nitrification NO3)
                     (documentation "\
NO3-N added by nitrification.")
                     (tag "NO3-Nitrification")
                     (handle sum)
                     (dimension "${unit}")
                     (spec reaction nitrification NO3))
	   (interval (path column "${column}" Chemistry "*"
                           combine "N" reaction nitrification N2O)
                     (documentation "\
NH4-N lost as N2O as a byproduct of the nitrification process.

NH4-Nitrification = NO3-Nitrification + N2O-Nitrification")
                     (tag "N2O-Nitrification")
                     (handle sum)
                     (dimension "${unit}")
                     (spec reaction nitrification N2O))
           (interval (path column "${column}" OrganicMatter "*" NH4_source)
                     (spec organic default NH4_source)
                     (documentation "\
NH4-N added from mineralization of organic matter in the soil.")
                     (tag "NH4-Mineralization")
                     (handle sum)
                     (dimension "${unit}"))
           (interval (path column "${column}" OrganicMatter "*" NO3_source)
                     (spec organic default NO3_source)
                     (documentation "\
NO3-N removed by the organic matter turnover processes.")
                     (tag "NO3-Immobilization")
		     (negate true)
                     (handle sum)
                     (dimension "${unit}"))
           (interval (path column "${column}" residuals_N_soil)
                     (spec column default residuals_N_soil)
                     (handle sum)
                     (documentation "\
Nitrogen in organic matter added to the soil from roots.")
                     (tag "Residuals-N")
                     (dimension "${unit}"))
           (interval (path column "${column}" OrganicMatter "*" tillage_N_soil)
                     (spec organic default tillage_N_soil)
                     (handle sum)
                     (documentation "\
Nitrogen in organic matter added to the soil by tillage operations.")
                     (tag "Tillage-Org_N")
                     (dimension "${unit}"))
           (interval (path column "${column}" OrganicMatter "*"
                           Bioincorporation N_added)
                     (spec fixed Bioincorporation N_added)
                     (documentation "\
Nitrogen in organic matter added to the soil by bioincroporation.

This includes dead leaves removed from the soil surface by earthworms.")
                     (tag "Bioincorporation")
                     (handle sum)
                     (dimension "${unit}"))
           (interval (documentation "\
Total amount of nitrogen in added organic matter in the soil.

AOM consists of fertilizer and plant residuals that has not yet been 
decomposed.")
                     (tag "AOM")
                     (path column "${column}" OrganicMatter "*" am "*" om "*" N)
                     (spec AOM component N)
                     (dimension "${unit}"))
           (interval (documentation "\
Total amount of nitrogen in soil humus.

This consist of dead organic matter that can no longer be traced
to its origins.")
                     (tag "SOM")
                     (path column "${column}" OrganicMatter "*" som "*" N)
                     (spec SOM component N)
                     (dimension "${unit}"))
           (interval (documentation "\
Nitrogen in soil microbiological organisms.

The living part of the soil (excluding plant roots).")
                     (tag "SMB")
                     (path column "${column}" OrganicMatter "*" smb "*" N)
                     (spec SMB component N)
                     (dimension "${unit}"))
           (interval (documentation "\
Nitrogen in humus added by fertilizer but not yet available for turnover.

This is usually zero.")
                     (tag "Buffer")
                     (path column "${column}" OrganicMatter "*" buffer N)
                     (spec organic default buffer N)
                     (dimension "${unit}"))))

(deflog "Field chemical" chemical
  "Content, transport and transformation of chemicals in the field."
  (declare unit String "Base unit.")
  (unit "g/ha")
  (where "${colfid}field_${chemfid}.dlf")
  (summary (balance (precision 3)
                    (input "Spray" "Deposit" "External"
			   "Litter Transform" "Surface Transform" 
                           "Soil Transform")
                    (output "Harvest" "Dissipate" "Litter Decompose"
                            "Surface Decompose" "Runoff"
                            "Soil Decompose" "Leak matrix" "Leak biopores"
                            "Soil drain" "Biopore drain"
			    "Uptake")
                    (content "Snow" "Canopy" "Litter" "Surface"
                             "Soil" "Biopores"
                             ;; "Error"
                             )))
  (when hourly)
  (entries ;; Year Month MDay Hour
           (number (path column "${column}" Chemistry "*" trace "${chemical}"
                         spray)
                   (documentation "\
Applied to the surface.

Chemicals can be added to the surface through the spray,
fertilize and irrigate management operations.")
                   (tag "Spray")
                   (handle sum)
                   (dimension "${unit}")
                   (spec chemical default spray))
           (number (path column "${column}" Chemistry "*" trace "${chemical}"
                         deposit)
                   (documentation "\
Athmospheric deposition.

This is usually zero when tracking pesticides, but can be
relevant for atmospheric polutants, and is also signficant when
for nitrogen.")
                   (tag "Deposit")
                   (handle sum)
                   (dimension "${unit}")
                   (spec chemical default deposit))
           (number (path column "${column}" Chemistry "*" trace "${chemical}"
                         canopy_harvest)
                   (documentation "\
Removed by harvest.

Some amount of the chemical may be left on the canopy, and
removed during harvest.  It is usually not sinificant for the
overall balance, but might be important to the consumers,
depending on the chemical.")
                   (tag Harvest)
                   (handle sum)
                   (dimension "${unit}")
                   (spec chemical default canopy_harvest))
           (number (path column "${column}" Chemistry "*" trace "${chemical}"
                         canopy_dissipate)
                   (documentation "\
Dissipating from the canopy.

Some chemicals evaporate or decompose when stored on the canopy.")
                   (tag Dissipate)
                   (handle sum)
                   (dimension "${unit}")
                   (spec chemical default canopy_dissipate))
           (number (path column "${column}" Chemistry "*" trace "${chemical}"
                         litter_decomposed)
                   (documentation "\
Decomposed while stored in the litter pack.")
                   (tag "Litter Decompose")
                   (handle sum)
                   (dimension "${unit}")
                   (spec chemical default litter_decomposed))
           (number (path column "${column}" Chemistry "*" trace "${chemical}"
                         litter_transform)
                   (documentation "\
Added from chemical transformation in the litter pack.")
                   (tag "Litter Transform")
                   (handle sum)
                   (dimension "${unit}")
                   (spec chemical default litter_transform))
           (number (path column "${column}" Chemistry "*" trace "${chemical}"
                         surface_decomposed)
                   (documentation "\
Decomposed while stored on the soil surface.")
                   (tag "Surface Decompose")
                   (handle sum)
                   (dimension "${unit}")
                   (spec chemical default surface_decomposed))
           (number (path column "${column}" Chemistry "*" trace "${chemical}"
                         surface_transform)
                   (documentation "\
Added from chemical transformation on the soil surface.")
                   (tag "Surface Transform")
                   (handle sum)
                   (dimension "${unit}")
                   (spec chemical default surface_transform))
           (number (path column "${column}" Chemistry "*" trace "${chemical}"
                         surface_runoff)
                   (documentation "\
Lost from soil surface due to lateral water flow.")
                   (tag Runoff)
                   (handle sum)
                   (dimension "${unit}")
                   (spec chemical default surface_runoff))
           (flux_bottom (path column "${column}" Chemistry "*" 
                              trace "${chemical}" J_matrix)
                        (documentation "\
Lost due to leaching.")
                        (tag "Leak matrix")
                        (negate true)
                        (handle sum)
                        (dimension "${unit}")
                        (spec chemical default J_matrix))
           (flux_bottom (path column "${column}" Chemistry "*" 
                              trace "${chemical}" J_tertiary)
                        (documentation "\
Lost due to leaching.")
                        (tag "Leak biopores")
                        (negate true)
                        (handle sum)
                        (dimension "${unit}")
                        (spec chemical default J_tertiary))
           (interval (path column "${column}"
			   Chemistry "*" trace "${chemical}" S_p_drain)
                     (spec chemical default S_p_drain)
                     (documentation "\
Lost from biopores to drain pipes.")
                     (tag "Biopore drain")
                     (handle sum)
                     (dimension "${unit}"))
           (interval (path column "${column}"
			   Chemistry "*" trace "${chemical}" S_soil_drain)
                     (spec chemical default S_soil_drain)
                     (documentation "\
Lost from the soil matrix to drain pipes.")
                     (tag "Soil drain")
                     (handle sum)
                     (negate true)
                     (dimension "${unit}"))
           (number (path column "${column}" Movement "*" 
                         Tertiary biopores classes drain 
                         solute_infiltration "${chemical}" value)
                   (spec biopore drain solute_infiltration value)
                   (tag "Surface drain")
                   (documentation "\
Amount moving directly from surface to drain, through biopores.

This never come in contact with the soil matrix.")
                   (handle sum)
                   (dimension "${unit}"))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" S_external)
                     (documentation "\
Added to the soil through some external mechanism, such as subsoil irrigation.")
                     (tag "External")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_external))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" S_root)
                     (documentation "\
Removed from the soil matrix through plant roots.")
                     (tag "Uptake")
                     (handle sum)
		     (negate true)
                     (dimension "${unit}")
                     (spec chemical default S_root))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" S_decompose)
                     (documentation "\
Decomposed while in the soil matrix.")
                     (tag "Soil Decompose")
                     (handle sum)
                     (negate true)
                     (dimension "${unit}")
                     (spec chemical default S_decompose))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" S_transform)
                     (documentation "\
Added through chemical transformation in the soil matrix.")
                     (tag "Soil Transform")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_transform))
           (number (path column "${column}" Chemistry "*" trace "${chemical}"
                         snow_storage)
                   (documentation "\
Amount stored in the snow pack.")
                   (tag Snow)
                   (handle current)
                   (dimension "${unit}")
                   (spec chemical default snow_storage))
           (number (path column "${column}" Chemistry "*" trace "${chemical}"
                         canopy_storage)
                   (documentation "\
Amount stored in on the canopy.")
                   (tag "Canopy")
                   (handle current)
                   (dimension "${unit}")
                   (spec chemical default canopy_storage))
           (number (path column "${column}" Chemistry "*" trace "${chemical}"
                         litter_storage)
                   (documentation "\
Amount stored in the surface litter (mulch, residuals, fertilizer).")
                   (tag Litter)
                   (handle current)
                   (dimension "${unit}")
                   (spec chemical default litter_storage))
           (number (path column "${column}" Chemistry "*" trace "${chemical}"
                         surface_storage)
                   (documentation "\
Amount stored on the soil surface.")
                   (tag "Surface")
                   (handle current)
                   (dimension "${unit}")
                   (spec chemical default surface_storage))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" M)
                     (documentation "\
Amount stored in the soil matrix.")
                     (tag "Soil")
                     (dimension "${unit}")
                     (spec chemical default M))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" M_tertiary)
		     (spec chemical default M_tertiary)
		     (documentation "\
Amount stored in the biopores.")
		     (tag "Biopores")
		     (dimension "${unit}")
		     (handle current))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" M_error)
                     (documentation "\
Amount borrowed from the future.

This can be temporaily non-zero if some process remove the
chemical fast compared to the timestep of the simulation.")
                     (tag "Error")
                     (dimension "${unit}")
                     (spec chemical default M_error))))

(defsummary "Soil chemical balance" balance
  (precision 3)
  (input "In-Matrix" "In-Biopores" "External" "Transform" "Tillage")
  (output "Decompose" "Leak-Matrix" "Leak-Biopores"
	  "Drain-Soil" "Drain-Biopores" "Uptake")
  (content "Content" "Biopores"))

(defsummary "Biopore chemical balance" balance
  (precision 3)
  (input "In-Biopores" "Matrix to biopores" "Drain-Biopores-Indirect")
  (output "Leak-Biopores" "Biopores to matrix" "Drain-Biopores")
  (content "Biopores"))

(defsummary "Matrix chemical balance" balance
  (precision 3)
  (input "In-Matrix" "Biopores to matrix" "External" "Transform" "Tillage")
  (output "Leak-Matrix" "Matrix to biopores" "Drain-Biopores-Indirect" "Decompose"
	  "Drain-Soil" "Uptake")
  (content "Content"))

(deflog "Soil chemical" chemical
  "Content, transport and transformation of chemicals in the soil."
  (declare unit String "Base unit.")
  (unit "g/ha")
  (where "${colfid}soil_${chemfid}.dlf")
  (summary "Soil chemical balance" "Matrix chemical balance"
	   "Biopore chemical balance" 
	   )
  (when hourly)
  (entries ;; Year Month MDay Hour
           (flux_top (path column "${column}" Chemistry "*" 
                           trace "${chemical}" J_matrix)
                     (documentation "\
Infiltration through soil matrix.")
                     (tag "In-Matrix")
                     (handle sum)
                     (negate true)
                     (dimension "${unit}")
                     (spec chemical default J_matrix))
           (flux_top (path column "${column}" Chemistry "*" 
                           trace "${chemical}" J_tertiary)
                     (documentation "\
Infiltration through biopores.")
                     (tag "In-Biopores")
                     (handle sum)
                     (negate true)
                     (dimension "${unit}")
                     (spec chemical default J_tertiary))
           (flux_bottom (path column "${column}" Chemistry "*" 
                              trace "${chemical}" J_matrix)
                        (documentation "\
Leaching through the soil matrix.")
                        (tag "Leak-Matrix")
                        (negate true)
                        (handle sum)
                        (dimension "${unit}")
                        (spec chemical default J_matrix))
           (flux_bottom (path column "${column}" Chemistry "*" 
                              trace "${chemical}" J_tertiary)
                        (documentation "\
Leaching.")
                        (tag "Leak-Biopores")
                        (negate true)
                        (handle sum)
                        (dimension "${unit}")
                        (spec chemical default J_tertiary))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" S_B2M)
                     (documentation "\
Enter the soil matrix from the biopores.")
                     (tag "Biopores to matrix")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_B2M))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" S_M2B)
                     (documentation "\
Enter the biopores from the matrix.")
                     (tag "Matrix to biopores")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_M2B))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" tillage)
                     (documentation "\
Added through tillage operations.")
                     (tag "Tillage")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default tillage))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" S_soil_drain)
                     (documentation "\
Lost to drains directly from the soil matrix.
This does not include what enters the drains through the biopores.")
                     (tag "Drain-Soil")
                     (negate true)
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_soil_drain))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" S_p_drain)
                     (documentation "\
Lost to drains from the biopores.")
                     (tag "Drain-Biopores")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_p_drain))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" S_indirect_drain)
                     (documentation "\
Lost to drains from matrix through biopores.
This does not include what enters the drain directly from the soil matrix.")
                     (tag "Drain-Biopores-Indirect")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_indirect_drain))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" S_external)
                     (documentation "\
Added externally, for example through subsoil irrigation.")
                     (tag "External")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_external))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" S_root)
                     (documentation "\
Lost with water uptake by plant roots.")
                     (tag "Uptake")
                     (handle sum)
		     (negate true)
                     (dimension "${unit}")
                     (spec chemical default S_root))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" S_decompose)
                     (documentation "\
Decomposed.")
                     (tag "Decompose")
                     (handle sum)
                     (negate true)
                     (dimension "${unit}")
                     (spec chemical default S_decompose))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" S_transform)
                     (documentation "\
Added by chemical teransformation.")
                     (tag "Transform")
                     (handle sum)
                     (dimension "${unit}")
                     (spec chemical default S_transform))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" M)
                     (documentation "\
Total amount in soil matrix in the specified soil interval.")
                     (tag "Content")
                     (dimension "${unit}")
                     (spec chemical default M))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" M_tertiary)
                     (documentation "\
Total amount in biopores in the specified soil interval.")
                     (tag "Biopores")
                     (dimension "${unit}")
                     (spec chemical default M_tertiary))
           (interval (path column "${column}" Chemistry "*" 
                           trace "${chemical}" M_error)
                     (documentation "\
Borrowed from the future.

This can be be non-zero if some process remove the chemical too
fast compared to the simulation timestep.")
                     (tag "Error")
                     (dimension "${unit}")
                     (spec chemical default M_error))))

;; log-std.dai ends here.
