private void OnCLEMInitialiseActivity(object sender, EventArgs e) { InsideMultiHarvestSequence = false; // activity is performed in CLEMDoCutAndCarry not CLEMGetResources this.AllocationStyle = ResourceAllocationStyle.Manual; fileCrop = simulation.FindAllDescendants().Where(a => a.Name == ModelNameFileCrop).FirstOrDefault() as IFileCrop; if (fileCrop == null) { throw new ApsimXException(this, String.Format("Unable to locate crop data reader [x={0}] requested by [a={1}]", this.ModelNameFileCrop ?? "Unknown", this.Name)); } LinkedResourceItem = Resources.FindResourceType <ResourceBaseWithTransactions, IResourceType>(this, StoreItemName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop); if ((LinkedResourceItem as Model).Parent is GrazeFoodStore) { // set manager of graze food store if linked (LinkedResourceItem as GrazeFoodStoreType).Manager = (Parent as IPastureManager); addReason = "Growth"; } // look up tree until we find a parent to allow nested crop products for rotate vs mixed cropping/products parentManagementActivity = FindAncestor <CropActivityManageCrop>(); // Retrieve harvest data from the forage file for the entire run. // only get entries where a harvest happened (Amtkg > 0) HarvestData = fileCrop.GetCropDataForEntireRun(parentManagementActivity.LinkedLandItem.SoilType, CropName, clock.StartDate, clock.EndDate).Where(a => a.AmtKg > 0).OrderBy(a => a.Year * 100 + a.Month).ToList <CropDataType>(); if ((HarvestData == null) || (HarvestData.Count == 0)) { Summary.WriteWarning(this, $"Unable to locate any harvest data in [x={fileCrop.Name}] using [x={fileCrop.FileName}] for soil type [{parentManagementActivity.LinkedLandItem.SoilType}] and crop name [{CropName}] between the dates [{clock.StartDate.ToShortDateString()}] and [{clock.EndDate.ToShortDateString()}]"); } IsTreeCrop = (TreesPerHa == 0) ? false : true; //using this boolean just makes things more readable. UnitsToHaConverter = (parentManagementActivity.LinkedLandItem.Parent as Land).UnitsOfAreaToHaConversion; // locate a cut and carry limiter associated with this event. limiter = LocateCutAndCarryLimiter(this); // check if harvest type tags have been provided HarvestTagsUsed = HarvestData.Where(a => a.HarvestType != "").Count() > 0; if (LinkedResourceItem is GrazeFoodStoreType) { double firstMonthsGrowth = 0; CropDataType cropData = HarvestData.Where(a => a.Year == clock.StartDate.Year && a.Month == clock.StartDate.Month).FirstOrDefault(); if (cropData != null) { firstMonthsGrowth = cropData.AmtKg; } (LinkedResourceItem as GrazeFoodStoreType).SetupStartingPasturePools(UnitsToHaConverter * (Parent as CropActivityManageCrop).Area * UnitsToHaConverter, firstMonthsGrowth); addReason = "Growth"; } }
private void OnCLEMStartOfTimeStep(object sender, EventArgs e) { // if harvest tags provided for this crop then they will be used to define previous, next etc // while date month > harvest record look for previous and delete past events if (HarvestData.Count() > 0) { int clockYrMth = CalculateYearMonth(Clock.Today); int position; // passed -1, current 0, future 1 do { int harvestYrMth = CalculateYearMonth(HarvestData.First().HarvestDate); position = (clockYrMth > harvestYrMth) ? -1 : ((clockYrMth == harvestYrMth) ? 0 : 1); // check for valid sequence if (HarvestTagsUsed && HarvestData.FirstOrDefault().HarvestType != "") { if (previousTag == HarvestData.FirstOrDefault().HarvestType) { string warn = $"Invalid sequence of HarvetTags detected in [a={this.Name}]\r\nEnsure tags are ordered first, last in sequence."; if (!Warnings.Exists(warn)) { Summary.WriteWarning(this, warn); Warnings.Add(warn); } } previousTag = HarvestData.FirstOrDefault().HarvestType; } switch (position) { case -1: if (HarvestTagsUsed) { switch (HarvestData.FirstOrDefault().HarvestType) { case "first": if (!performedHarvest) { InsideMultiHarvestSequence = true; StartCurrentSequenceHarvest = HarvestData.FirstOrDefault(); EndCurrentSequenceHarvest = HarvestData.Where(a => a.HarvestType == "last").FirstOrDefault(); } break; case "last": // hit tagged last to delete as we've passed this date so out of multi harvest sequence InsideMultiHarvestSequence = false; PreviousHarvest = HarvestData.FirstOrDefault(); break; default: break; } } else { PreviousHarvest = HarvestData.FirstOrDefault(); } HarvestData.RemoveAt(0); break; case 0: performedHarvest = true; if (HarvestTagsUsed) { switch (HarvestData.FirstOrDefault().HarvestType) { case "first": // hit tagged first for current time step InsideMultiHarvestSequence = true; StartCurrentSequenceHarvest = HarvestData.FirstOrDefault(); PreviousHarvest = null; EndCurrentSequenceHarvest = HarvestData.Where(a => a.HarvestType == "last").FirstOrDefault(); break; default: NextHarvest = HarvestData.FirstOrDefault(); PreviousHarvest = null; break; } } else { NextHarvest = HarvestData.FirstOrDefault(); PreviousHarvest = null; } break; case 1: if (HarvestTagsUsed) { switch (HarvestData.FirstOrDefault().HarvestType) { case "first": // hit tagged first for next harvest NextHarvest = HarvestData.FirstOrDefault(); break; default: NextHarvest = HarvestData.FirstOrDefault(); break; } } else { NextHarvest = HarvestData.FirstOrDefault(); } break; default: break; } } while (HarvestData.Count > 0 && position == -1); } }