Пример #1
0
        //private void replaceWithFieldTemplate(Scenario s, string pathToReplace)
        //{
        //    if (Directory.Exists(pathToReplace))
        //        throw new ArgumentException("Path to replace does not exist");

        //    // Burn current field dir and copy a new one from templates
        //    string SourcePath = Path.Combine(Directory.GetCurrentDirectory(), @"Assets\Fields", s.Field.Crop);
        //    if (!Directory.Exists(SourcePath))
        //        throw new ArgumentException("Error setting up field; specified crop does not exist");
        //    //string currFieldDir = pathToReplace;
        //    string DestinationPath = Path.Combine(Directory.GetParent(pathToReplace).ToString(), s.Field.Crop);

        //    Directory.Delete(pathToReplace, true);
        //    Directory.CreateDirectory(DestinationPath);

        //    //Create all of the directories
        //    foreach (string dirPath in Directory.GetDirectories(SourcePath, "*",
        //        SearchOption.AllDirectories))
        //        Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));

        //    //Copy all the files & Replaces any files with the same name
        //    foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
        //        SearchOption.AllDirectories))
        //        File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);

        //    // Reload file
        //    fDp.Load(Path.Combine(DestinationPath, fieldScenarioFilename));
        //}
        //private void _copyFieldTemplate(Scenario s)
        //{
        //    if (String.IsNullOrEmpty(fDp.LoadedPath))
        //        throw new NullReferenceException("Error setting up field; path not loaded");

        //    string dirName =
        //        new DirectoryInfo(Path.GetDirectoryName(fDp.LoadedPath)).Name;

        //    if(s.Field.Enabled && dirName != s.Field.Crop)
        //    {
        //        // Burn current field dir and copy a new one from templates
        //        string SourcePath = Path.Combine(Directory.GetCurrentDirectory(), @"Assets\Fields", s.Field.Crop);
        //        if (!Directory.Exists(SourcePath))
        //            throw new ArgumentException("Error setting up field; specified crop does not exist");
        //        string currFieldDir = Path.GetDirectoryName(fDp.LoadedPath);
        //        string DestinationPath = Path.Combine(Directory.GetParent(currFieldDir).ToString(), s.Field.Crop);

        //        Directory.Delete(Path.GetDirectoryName(fDp.LoadedPath), true);
        //        Directory.CreateDirectory(DestinationPath);

        //        //Create all of the directories
        //        foreach (string dirPath in Directory.GetDirectories(SourcePath, "*",
        //            SearchOption.AllDirectories))
        //            Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));

        //        //Copy all the files & Replaces any files with the same name
        //        foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
        //            SearchOption.AllDirectories))
        //            File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);

        //        // Reload file
        //        fDp.Load(Path.Combine(DestinationPath, fieldScenarioFilename));
        //    }
        //}
        private void writeField(Scenario s)
        {
            if (String.IsNullOrEmpty(fDp.LoadedPath))
            {
                throw new NullReferenceException("Cannot write field scenario; no file loaded");
            }

            // Delete fertigation file
            string fertPath = Path.Combine(
                Path.GetDirectoryName(fDp.LoadedPath),
                //"Database",
                //"Management",
                "fertigation.CS_management");

            if (File.Exists(fertPath))
            {
                File.Delete(fertPath);
            }

            // Set field area
            fDp.SetValue("field", "size", s.Field.Area_ha.ToString());

            // Set enabled
            fDp.SetValue("field", "enable", s.Field.Enabled.ToString().ToLower());

            // Set fixed management to apply fertigation from manure storage (lagoon)
            string fixedManagementPath = "";

            if (s.Fertigation.Enabled)
            {
                fixedManagementPath = fertPath;
            }
            fDp.SetValue(
                "parameter_filenames",
                "fixed_management",
                fixedManagementPath);


            if (File.Exists(s.PathToWeatherFile))
            {
                // Set weather
                fDp.SetValue(
                    "parameter_filenames",
                    "weather_database",
                    s.PathToWeatherFile);

                // Set soil
                DirectoryInfo weatherDatabase = Directory.GetParent(s.PathToWeatherFile);
                string        pathToSoilFile  = Path.Combine(
                    weatherDatabase.Parent.FullName.ToString(),
                    "Soil",
                    Path.GetFileNameWithoutExtension(s.PathToWeatherFile) + ".CS_soil");
                fDp.SetValue(
                    "parameter_filenames",
                    "soil",
                    pathToSoilFile);
            }

            fDp.Save(fDp.LoadedPath);
        }
Пример #2
0
        /// <summary>
        /// Write a Wsu.DairyCafo.DataAccess.Dto.Scenario to a Dairy-CropSyst
        /// parameter file.
        /// <note>If file specified by Scenario exists, then overwrites values.
        /// This function deletes all [fertigation:x] and [manure_storage:x]
        /// sections before writing new ones.</note>
        /// </summary>
        /// <param name="scenario"></param>
        public void Write(Scenario scenario)
        {
            if (String.IsNullOrEmpty(dDp.LoadedPath))
            {
                throw new NullReferenceException("Cannot write, no scenario file loaded");
            }

            // Clear scenario file before writing
            dDp.Clear();
            // NOTE: Use 'clean()' if user needs custom sections defined
            //clean();

            dDp.SetSection("version", defaults.GetVersionDefaults());

            //var sVals = defaults.GetScenarioDefaults();
            var sVals = new Dictionary <string, string>();

            sVals.Add("details_URL", scenario.DetailsUrl);
            sVals.Add("description", scenario.Description);
            sVals.Add("weather", scenario.PathToWeatherFile.ToString());
            sVals.Add("latitude", scenario.Latitude.ToString());
            sVals.Add("longitude", scenario.Longitude.ToString());
            sVals.Add("start_date", getYYYYDOYString(scenario.StartDate));
            sVals.Add("stop_date", getYYYYDOYString(scenario.StopDate));
            sVals.Add("accumulations", scenario.Accumulations.ToString());
            sVals.Add("simulation_period_mode", scenario.SimulationPeriodMode.ToString());
            sVals.Add("irrigation_pump_model", scenario.IrrigationPumpModel.ToString());
            sVals.Add("parameterized_scenario", scenario.ParameterizedScenario.ToString());
            sVals.Add("cow_description:count", scenario.GetCountCow().ToString());
            sVals.Add("barn:count", scenario.GetCountBarn().ToString());
            sVals.Add("manure_separator:count", scenario.GetCountManureSeparator().ToString());
            sVals.Add("manure_storage:count", scenario.GetCountManureStorage().ToString());
            sVals.Add("fertigation:count", scenario.GetCountFertigation().ToString());
            sVals.Add("receive_off_farm_biomass:count", scenario.GetCountReceiveOffFarmBiomass().ToString());
            dDp.SetSection("dairy scenario", sVals);

            writeCow(scenario);
            writeBarn(scenario);

            writeSeparatorsAndStorage(scenario);
            int numSeparators   = scenario.GetCountManureSeparator();
            int numStorageTanks = scenario.GetCountManureStorage();

            writeLagoon(scenario);
            int numReceiveOffFarmBiomass = writeReceiveOffFarmBiomass(scenario);

            writeFertigationManagement(scenario);

            int numFertigations = writeFertigations(scenario);

            if (!dDp.Save(dDp.LoadedPath, getSectionOrder(scenario)))
            {
                throw new OperationCanceledException("Failed to save file");
            }
        }