Beispiel #1
0
            //merge cropSequence args into given args
            private static ETArgs MergeArgs(ref ETArgs etArgs, CropSequenceValues cs)
            {
                ETArgs result = new ETArgs(etArgs);

                result.climate     = cs.climate;
                result.plant       = cs.plant;
                result.soil        = cs.soil;
                result.seedDate    = cs.seedDate;
                result.harvestDate = cs.harvestDate;
                if (cs.autoIrrigation != null)
                {
                    result.autoIrr = cs.autoIrrigation;
                }
                return(result);
            }
Beispiel #2
0
            /*!
             * Executes the crop sequence calculation.
             *
             * \author  Hunstock
             * \date    06.04.2017
             *
             * \param   start                       The start Date/Time for calculation.
             * \param   end                         The end Date/Time for calculation.
             * \param [in,out]  etArgs              The arguments for evaporation, transpiration and irrigation calculation.
             * \param [in,out]  irrigationAmount    The irrigation amounts. Here can be a modified CropSequenceResult passed.
             * \param   dryRun                      true to dry run and calculate the irrgation demand, false to real irrigate.
             *
             * \return  A CropSequenceResult, the irrigation demand, formatted for use in MIKE-Basin by DHI-WASY.
             */

            public CropSequenceResult runCropSequence(DateTime start, DateTime end, ref ETArgs etArgs, ref CropSequenceResult irrigationAmount, bool dryRun = false)
            {
                //measure calculation duration
                Stopwatch stopWatch = new Stopwatch();

                stopWatch.Start();

                CropSequenceResult localMbResult = new CropSequenceResult();

                if (irrigationAmount == null)
                {
                    irrigationAmount = new CropSequenceResult();
                }

                IList <CropSequenceValues> csPart = getCropSequence(start);

                foreach (CropSequenceValues cs in csPart)
                {
                    String _csIndex = csIndex(cs);

                    ETArgs tmpArgs = MergeArgs(ref etArgs, cs);
                    tmpArgs.start = start;
                    tmpArgs.end   = end;
                    if (tmpArgs.plant == null)
                    {
                        Debug.WriteLine("no plant for cropSequence: " + _csIndex + " skipping sequence " + start.ToString());
                        continue;
                    }

                    ETResult tmpResult = null;
                    if (_results.ContainsKey(_csIndex))
                    {
                        tmpResult = _results[_csIndex];
                    }

                    if (dryRun)
                    {
                        if (tmpResult != null)
                        {
                            tmpResult = new ETResult(tmpResult);
                            tmpResult.autoIrrigation      = 0;
                            tmpResult.autoNetIrrigation   = 0;
                            tmpResult.interceptionAutoIrr = 0;
                        }
                        Transpiration.ETCalc(ref tmpArgs, ref tmpResult, dryRun);
                    }
                    else
                    {
                        if (irrigationAmount.networkIdIrrigationDemand.ContainsKey(_csIndex))
                        {
                            //Debug.WriteLine("found irrigation: " + csIndex + ": " + mbResult.networkIdIrrigationDemand[csIndex].ToString());
                            //FIXME: convert irrigation to schedule and add to tmpArgs
                            DateTime startMin       = new DateTime(Math.Max(tmpArgs.seedDate.Ticks, tmpArgs.start != null ? ((DateTime)tmpArgs.start).Ticks : 0));
                            DateTime endMax         = new DateTime(Math.Min(tmpArgs.harvestDate.Ticks, tmpArgs.end != null ? ((DateTime)tmpArgs.end).Ticks : 0));
                            TimeSpan scheduleLength = endMax.Subtract(startMin);
                            DateTime scheduleMid    = startMin.AddMinutes(scheduleLength.TotalMinutes / 2);
                            if (tmpArgs.irrigationSchedule == null)
                            {
                                if (cs.irrigationType == null)
                                {
                                    tmpArgs.irrigationSchedule = new IrrigationSchedule(cs.autoIrrigation.type);
                                    tmpArgs.autoIrr.type       = cs.autoIrrigation.type;
                                }
                                else
                                {
                                    tmpArgs.irrigationSchedule = new IrrigationSchedule(cs.irrigationType);
                                }
                                tmpArgs.irrigationSchedule.schedule.Add(scheduleMid, irrigationAmount.networkIdIrrigationDemand[_csIndex].irrigationDemand.amount);
                            }
                        }
                        Transpiration.ETCalc(ref tmpArgs, ref tmpResult, dryRun);
                        _results[_csIndex] = tmpResult;
                    }

                    if (localMbResult.networkIdIrrigationDemand.ContainsKey(_csIndex))
                    {
                        localMbResult.networkIdIrrigationDemand[_csIndex].irrigationDemand.surfaceWater.amount += tmpResult.autoNetIrrigation;
                    }
                    else
                    {
                        localMbResult.networkIdIrrigationDemand[_csIndex]                  = new CropSequenceFieldResult();
                        localMbResult.networkIdIrrigationDemand[_csIndex].area             = cs.area;
                        localMbResult.networkIdIrrigationDemand[_csIndex].fieldId          = cs.fieldId;
                        localMbResult.networkIdIrrigationDemand[_csIndex].networkId        = cs.networkId;
                        localMbResult.networkIdIrrigationDemand[_csIndex].waterRights      = cs.waterRights;
                        localMbResult.networkIdIrrigationDemand[_csIndex].irrigationDemand = new IrrigationAmount(surfaceWaterAmount: tmpResult.autoNetIrrigation);
                    }

                    localMbResult.error += tmpResult.error;
                }
                stopWatch.Stop();
                TimeSpan ts = stopWatch.Elapsed;

                // Format and display the TimeSpan value.
                String elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}",
                                                   ts.Hours, ts.Minutes, ts.Seconds,
                                                   ts.Milliseconds);

                Debug.WriteLine("runCropSequence took " + elapsedTime + " start: " + start.ToString("yyyy-MM-dd") + " end: " + end.ToString("yyyy-MM-dd"));
                localMbResult.runtimeMs = stopWatch.ElapsedMilliseconds;

                return(localMbResult);
            }