APSIMInitiative / APSIM710

APSIM
https://www.apsim.info
31 stars 47 forks source link

Change the planting date in APSIM7.9 #2107

Closed Chuanwei-zhang-ucas closed 2 years ago

Chuanwei-zhang-ucas commented 2 years ago

Good day everyone, I am trying to change the start of planting date for maize to the first day of five-days mean temperature over 5 ℃, how do it in APSIM, any advice would be appreciate.

peter-devoil commented 2 years ago

Apsim's manager1 was a bit tedious to use for "array" style calculations.

Some manager2 code like below should do what you describe.

using System;
using ModelFramework;
using CSGeneral;

public class Script 
{      
   private ManagerUtility.Tracker<double> meanTTracker; 

   //initialise tracker, telling it how many days to track
   [EventHandler] public void OnInitialised()
   {
      meanTTracker = new ManagerUtility.Tracker<double>(5); 
   }
   // Calculate mean temperature
   [EventHandler] public void OnNewMet(NewMetType nm)
   {
      meanTTracker.Add(( nm.maxt + nm.mint ) / 2);
   }
   [Output, Description("Mean temperature for last 5 days")] public double meant_5  {
      get {
         return(meanTTracker.Average());
      }
      set {}
   }
}

This provides the variable "meant_5" that you can use in your sowing logic

Chuanwei-zhang-ucas commented 2 years ago

Thanks for your help, and how to use the "meant_5" to set the planting date in every year, can you please take me an example ? Sorry for my inexperience in APSIM.

2021-11-01 12:43:12"Peter de Voil" @.***>写道:

Apsim's manager1 was a bit tedious to use for "array" style calculations.

Some manager2 code like below should do what you describe.

using System; using ModelFramework; using CSGeneral;

public class Script {
private ManagerUtility.Tracker meanTTracker;

//initialise tracker, telling it how many days to track [EventHandler] public void OnInitialised() { meanTTracker = new ManagerUtility.Tracker(5); } // Calculate mean temperature [EventHandler] public void OnNewMet(NewMetType nm) { meanTTracker.Add(( nm.maxt + nm.mint ) / 2); } [Output, Description("Mean temperature for last 5 days")] public double meant_5 { get { return(meanTTracker.Average()); } set {} } }

This provides the variable "meant_5" that you can use in your sowing logic

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

peter-devoil commented 2 years ago

Sorry, here's some background: Apsim uses (perhaps several) manager modules to sow, (and harvest, fertilise etc) crops during a simulation. Each module is simply a script, written in a programming language that can interact with the simulation. Here's a partially complete introduction to the various languages.

Once you've implemented a script like the above, you can modify your sowing rule to include the variable it exports it in its criteria for sowing. For example, start with the "Sow using a variable rule" rule in the manager toolbox which contains:

      if (paddock_is_fallow() = 1 and FallowIn <> 'yes' and (NextCrop = 0 or NextCrop = '[crop]')) then
         if (date_within('[date1], [date2]') = 1) then
            if (rain[[rainnumdays]] >= [raincrit] AND esw >= [esw_amount]) OR
                ('[must_sow]' = 'yes' AND today = date('[date2]'))) THEN
               ChooseNextCrop = 'yes'   ! for rotations
               [crop] sow plants =[density], sowing_depth = [depth], cultivar = [cultivar], row_spacing = [row_spacing], crop_class = [class]
            endif
...

And modify line 3 in that logic to include your mean temperature test:

      if (paddock_is_fallow() = 1 and FallowIn <> 'yes' and (NextCrop = 0 or NextCrop = '[crop]')) then
         if (date_within('[date1], [date2]') = 1) then
            if (meant_5 > 5 AND rain[[rainnumdays]] >= [raincrit] AND esw >= [esw_amount]) OR
                ('[must_sow]' = 'yes' AND today = date('[date2]'))) THEN
               ChooseNextCrop = 'yes'   ! for rotations
               [crop] sow plants =[density], sowing_depth = [depth], cultivar = [cultivar], row_spacing = [row_spacing], crop_class = [class]
            endif
...

HTH

Chuanwei-zhang-ucas commented 2 years ago

Thank you. That's an excellent solution.

2021-11-02 06:20:15"Peter de Voil" @.***>写道:

Sorry, here's some background: Apsim uses (perhaps several) manager modules to sow, (and harvest, fertilise etc) crops during a simulation. Each module is simply a script, written in a programming language that can interact with the simulation. Here's a partially complete introduction to the various languages.

Once you've implemented a script like the above, you can modify your sowing rule to include the variable it exports it in its criteria for sowing. For example, start with the "Sow using a variable rule" rule in the manager toolbox which contains:

  if (paddock_is_fallow() = 1 and FallowIn <> 'yes' and (NextCrop = 0 or NextCrop = '[crop]')) then
     if (date_within('[date1], [date2]') = 1) then
        if (rain[[rainnumdays]] >= [raincrit] AND esw >= [esw_amount]) OR
            ('[must_sow]' = 'yes' AND today = date('[date2]'))) THEN
           ChooseNextCrop = 'yes'   ! for rotations
           [crop] sow plants =[density], sowing_depth = [depth], cultivar = [cultivar], row_spacing = [row_spacing], crop_class = [class]
        endif

...

And modify line 3 in that logic to include your mean temperature test:

  if (paddock_is_fallow() = 1 and FallowIn <> 'yes' and (NextCrop = 0 or NextCrop = '[crop]')) then
     if (date_within('[date1], [date2]') = 1) then
        if (meant_5 > 5 AND rain[[rainnumdays]] >= [raincrit] AND esw >= [esw_amount]) OR
            ('[must_sow]' = 'yes' AND today = date('[date2]'))) THEN
           ChooseNextCrop = 'yes'   ! for rotations
           [crop] sow plants =[density], sowing_depth = [depth], cultivar = [cultivar], row_spacing = [row_spacing], crop_class = [class]
        endif

...

HTH

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had any activity in the last 30 days. It will be closed in one week if no further activity occurs. Thank you for your contributions.

stale[bot] commented 2 years ago

This issue is being closed because there has been no recent activity. Feel free to re-open or open a new issue if needed.