/// <summary> save crop data and default values
        /// </summary>
        /// <param name="outpath">out path</param>
        /// <param name="cropRoationWorksteps"> crop rotation worksteps</param>
        private static void SaveCropData(string outpath, IOrderedEnumerable <Cultivation.CropRoationWorkstep> cropRoationWorksteps)
        {
            JObject rss =
                new JObject(
                    new JProperty("fert-params",
                                  new JObject(
                                      new JProperty("AN", new JArray("include-from-file", "mineral-fertilisers/AN.json")),
                                      new JProperty("CADLM", new JArray("include-from-file", "organic-fertilisers/CADLM.json")))),
                    new JProperty("crops",
                                  new JObject(
                                      new JProperty("WG", new JObject(
                                                        new JProperty("is-winter-crop", true),
                                                        new JProperty("cropParams", new JObject(
                                                                          new JProperty("species", new JArray("include-from-file", "crops/barley.json")),
                                                                          new JProperty("cultivar", new JArray("include-from-file", "crops/barley/winter-barley.json")))),
                                                        new JProperty("residueParams", new JArray("include-from-file", "crop-residues/barley.json")))),
                                      new JProperty("SG", new JObject(
                                                        new JProperty("is-winter-crop", false),
                                                        new JProperty("cropParams", new JObject(
                                                                          new JProperty("species", new JArray("include-from-file", "crops/barley.json")),
                                                                          new JProperty("cultivar", new JArray("include-from-file", "crops/barley/spring-barley.json")))),
                                                        new JProperty("residueParams", new JArray("include-from-file", "crop-residues/barley.json")))))),
                    new JProperty("cropRotation",
                                  new JArray(
                                      new JObject(
                                          new JProperty("worksteps",
                                                        new JArray(
                                                            from p in cropRoationWorksteps
                                                            select Cultivation.CropRoationWorkstepToJSON(p)))))),
                    new JProperty("CropParameters", new JArray("include-from-file", "general/crop.json"))
                    );

            File.WriteAllText(outpath + Path.DirectorySeparatorChar + CROP_FILENAME, rss.ToString());
        }
        /// <summary> extract workstep data and them save to file
        /// </summary>
        /// <param name="outpath"></param>
        /// <param name="agMipJson"></param>
        public static void ExtractCropData(string outpath, JObject agMipJson)
        {
            List <Cultivation.CropRoationWorkstep> cropRoationWorksteps = new List <Cultivation.CropRoationWorkstep>();
            IList <JToken> eventData = agMipJson["experiments"].First["management"]["events"].Children().ToList();
            //double yield = (double)agMipJson["experiments"].First["observed"]["hwam"].ToObject(typeof(double)); //  (dry wt) kg/ha

            string   plantingDateStr = agMipJson["experiments"].First["management"]["pdate"].ToString();
            string   harvestDateStr  = agMipJson["experiments"].First["management"]["hadate"].ToString();
            DateTime plantingDate    = DateTime.ParseExact(plantingDateStr, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
            DateTime harvestDate     = DateTime.ParseExact(harvestDateStr, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);

            bool isWinterCrop = harvestDate.DayOfYear < plantingDate.DayOfYear;

            foreach (JToken token in eventData)
            {
                string date         = token["date"].ToString();
                string eventName    = token["event"].ToString();
                string crop         = "BAR";
                double plantsPerSqm = 0;
                if (token.Contains("crid"))
                {
                    crop = token["event"].ToString();
                }
                double feAmount = 0;
                if (token["feamn"] != null)
                {
                    feAmount = (double)token["feamn"].ToObject(typeof(double));
                }
                if (token["plpop"] != null)
                {
                    plantsPerSqm = (double)token["plpop"].ToObject(typeof(double));
                }
                cropRoationWorksteps.Add(Cultivation.ExtractCropRoationWorkstep(date, crop, feAmount, eventName, isWinterCrop, plantsPerSqm));
            }

            Cultivation.CropRoationWorkstep harvestEvent = new Cultivation.CropRoationWorkstep();
            harvestEvent.WorkstepType = "Harvest";
            harvestEvent.Isodate      = DateTime.ParseExact(harvestDateStr, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
            cropRoationWorksteps.Add(harvestEvent);
            var sortedSteps = cropRoationWorksteps.OrderBy(c => c.Isodate);

            SaveCropData(outpath, sortedSteps);
        }