APSIMInitiative / ApsimX

ApsimX is the next generation of APSIM
http://www.apsim.info
Other
132 stars 161 forks source link

How can I access the value of a manager script's parameter from another script? #3603

Closed BrianCollinss closed 5 years ago

BrianCollinss commented 5 years ago

I need to set fertilization at sowing and I need to do it without explicitly setting sowing date in fertilisation manager script. So I need to read the sowing date from 'Sowing' manager script. How can I do that? Sowing.Script.SowDate does not work.

'Models.Manager' does not contain a definition for 'Script' and no extension method 'Script' accepting a first argument of type 'Models.Manager' could be found (are you missing a using directive or an assembly reference?). Line number: 40

BrianCollinss commented 5 years ago

Hi @hol430 . Could you please help me with this?

hol430 commented 5 years ago

Can you post your .apsimx file?

BrianCollinss commented 5 years ago

I want to access sowing date of Sowing from ResetWaterOnSowing.

APSIM-NG Template for TE Study - OldISMs2.zip

BrianCollinss commented 5 years ago

I used this to check whether today is the planting date, instead of reading sowing date from that manager script. I am not sure if this is the ideal way of doing it.

DateUtilities.DatesEqual(DateUtilities.DMYtoISO(Wheat.SowingDate.ToShortDateString()), Clock.Today)

hol430 commented 5 years ago

Try this:

string sowDate = Apsim.GetVariableObject(Parent, "[Sowing].Script.SowDate").Value as string;
if (DateUtilities.DatesEqual(sowDate, Clock.Today))

It compiles of course, but I don't have your met files so have no idea if it works or not.

BrianCollinss commented 5 years ago

I got different results with my and your method. I have factors that modify sowing dates. I am not sure which one of our methods reads the 'real' sowing date, i.e. the one after it is modified by the factor.

Badgingarra Research Stn.zip

hol430 commented 5 years ago

Something like this should tell you if my method is returning the correct sowing date:

Simulation sim = Apsim.Parent(this, typeof(Simulation)) as Simulation;
string sowDate = Apsim.GetVariableObject(Parent, "[Sowing].Script.SowDate").Value as string;
Summary.WriteMessage(this, "SimulationName=" + sim.Name + ", SowDate=" + sowDate);
if (DateUtilities.DatesEqual(sowDate, Clock.Today))

As long as you know which simulation name is supposed to correspond to which sowing date, this should let you know whether my method is returning the correct sowing date.

Disclaimer: I haven't tested this.

BrianCollinss commented 5 years ago

Drew, your method keeps the date as it is in the original Sowing manager script, i.e. it does not take into account the modification of sowing date by factors. I think if I know how to convert date to dd-mmm my method would work fine, I am testing all the date convert functions one by one.

hol430 commented 5 years ago

Can you upload your met file so I can run the file? That behaviour sounds like a bug

BrianCollinss commented 5 years ago

I did. It is a couple of comments above this.

https://github.com/APSIMInitiative/ApsimX/files/2959469/Badgingarra.Research.Stn.zip

hol430 commented 5 years ago

Ah right - I missed that sorry

BrianCollinss commented 5 years ago

This is the easiest way I think and it resets on sowing date every year.

        string sowDate = Wheat.SowingDate.ToString("dd-MMM");
        if (DateUtilities.DatesEqual(sowDate, Clock.Today))

Cheers.

hol430 commented 5 years ago

It seems to be working ok for me - if I click on any of the simulations were sowing date has been set to 20-Apr, the script successfully figures out that the sowing date is 20-Apr. And in those sims where the sowing date is 20-May, the script seems to figure that out as well:

image

image

Are you seeing different behaviour, or is this not how the script is supposed to work?

BrianCollinss commented 5 years ago

You're right. It works as well. Thanks a lot Drew. That was really urgent for me.