//"Hispaniolan pine", "Pine"); //"Nonforest", "Not stocked"); //"Improved pine stock", "Pine"); /// <summary> /// build up a dataset with code instead of import an SIT formatted dataset /// </summary> private static void Method2() { string projectPath = @"C:\Program Files (x86)\Operational-Scale CBM-CFS3\Projects\myproject\myproject.mdb"; string dirname = System.IO.Path.GetDirectoryName(projectPath); if (!System.IO.Directory.Exists(dirname)) { System.IO.Directory.CreateDirectory(dirname); } //when initialize mapping is true, incoming sit //data must be named exactly as the definitions in the archive index Sitplugin sitplugin = new Sitplugin( outputPath: projectPath, initializeMapping: true); //define ten year age class size, with 20 values (200 years) int numAgeClasses = 20; sitplugin.AddAgeClasses(10, numAgeClasses); sitplugin.AddClassifier("admin"); sitplugin.AddClassifier("eco"); sitplugin.SetAdminEcoMapping("admin", "eco"); sitplugin.AddClassifier("species"); sitplugin.SetSpeciesClassifier("species"); sitplugin.AddClassifier("group"); sitplugin.AddInventory( classifierSet: "British Columbia,Pacific Maritime,Spruce,g1",//note exact naming is required when " initializeMapping: true" age: 100, area: 10, spatialReference: 100, HistoricDisturbance: "Wildfire", MostRecentDisturbance: "Clearcut harvesting with salvage"); List <double> FakeGrowthCurve = Enumerable.Range(0, numAgeClasses) .Select(a => 200 / (1 + Math.Exp(-0.5 * (a - numAgeClasses / 2.0)))).ToList(); sitplugin.AddYield( classifierSet: "British Columbia,Pacific Maritime,Spruce,?", leadingSpeciesClassifierValue: "Spruce", values: FakeGrowthCurve); sitplugin.AddDisturbanceEvent( classifierSet: "?,?,?,?",//when spatial reference is used, events do not use a classifier set disturbanceType: "Clearcut harvesting with salvage", timestep: 100, target: 5, targetType: "Area", spatialReference: 100); sitplugin.Import(); }
private static void MapSpecies(Sitplugin sitplugin, JToken mappingConfig) { sitplugin.SetSpeciesClassifier((string)mappingConfig["species_classifier"]); if (mappingConfig["species_mapping"] != null) { foreach (var item in mappingConfig["species_mapping"]) { sitplugin.MapSpecies( (string)item["user_species"], (string)item["default_species"]); } } }
private static void MapDisturbanceTypes(Sitplugin sitplugin, JToken mappingConfig) { if (mappingConfig != null && mappingConfig.HasValues) { if (mappingConfig["disturbance_type_mapping"] != null) { foreach (var item in mappingConfig["disturbance_type_mapping"]) { sitplugin.MapDisturbanceType( (string)item["user_dist_type"], (string)item["default_dist_type"]); } } } }
private static void MapSpatialUnits(Sitplugin sitplugin, JToken mappingConfig) { if (!Enum.TryParse((string)mappingConfig["mapping_mode"], out SpatialUnitClassifierMode mode)) { throw new ArgumentException("expected one of the SpatialUnitClassifierMode enum values"); } switch (mode) { case SpatialUnitClassifierMode.JoinedAdminEcoClassifier: sitplugin.SetSPUMapping((string)mappingConfig["spu_classifier"]); foreach (var item in mappingConfig["spu_mapping"]) { sitplugin.MapSpatialUnit( (string)item["user_spatial_unit"], (string)item["default_spatial_unit"]["admin_boundary"], (string)item["default_spatial_unit"]["eco_boundary"]); } break; case SpatialUnitClassifierMode.SeperateAdminEcoClassifiers: sitplugin.SetAdminEcoMapping( (string)mappingConfig["admin_classifier"], (string)mappingConfig["eco_classifier"]); if (mappingConfig["admin_mapping"] != null) { foreach (var item in mappingConfig["admin_mapping"]) { sitplugin.MapAdminBoundary( (string)item["user_admin_boundary"], (string)item["default_admin_boundary"]); } } if (mappingConfig["eco_mapping"] != null) { foreach (var item in mappingConfig["eco_mapping"]) { sitplugin.MapEcoBoundary( (string)item["user_eco_boundary"], (string)item["default_eco_boundary"]); } } break; case SpatialUnitClassifierMode.SingleDefaultSpatialUnit: sitplugin.SetSingleSpatialUnit((int)mappingConfig["default_spuid"]); break; } }
/// <summary> /// Import an SIT formatted dataset. /// mdb, accdb, xls, or xlsx are supported /// </summary> private static void Method1() { string excelPath = @"C:\Program Files (x86)\Operational-Scale CBM-CFS3\Tutorials\Tutorial 6\Tutorial6.xls"; string projectPath = @"C:\Program Files (x86)\Operational-Scale CBM-CFS3\Projects\tutorial6\tutorial6.mdb"; var data = Sitplugin.ParseSITData( path: excelPath, AgeClassTableName: "AgeClasses$", ClassifiersTableName: "Classifiers$", DisturbanceEventsTableName: "DistEvents$", DisturbanceTypesTableName: "DistType$", InventoryTableName: "Inventory$", TransitionRulesTableName: "Transitions$", YieldTableName: "Growth$"); Sitplugin sitplugin = new Sitplugin( outputPath: projectPath, initializeMapping: false, dataset: data); sitplugin.SetSingleSpatialUnit(42); //see archive index tblSPUDefault/tblAdminBoundaryDefault/tblEcoBoundaryDefault for code definitions sitplugin.SetSpeciesClassifier("Species");//the classifier name as defined in the spreadsheet sitplugin.SetNonForestClassifier("Forest type"); // in the following mappings, the left value is something that appears in the import data, // and the right value is something that appears in the archive index database. sitplugin.MapSpecies("Hispaniolan pine", "Pine"); sitplugin.MapSpecies("Nonforest", "Not stocked"); sitplugin.MapSpecies("Improved pine stock", "Pine"); sitplugin.MapNonForest("Afforestation", "Gleysolic"); sitplugin.MapNonForest("Natural forest", Sitplugin.ForestOnly); sitplugin.MapNonForest("Control", Sitplugin.ForestOnly); sitplugin.MapDisturbanceType("Fire", "Wildfire"); sitplugin.MapDisturbanceType("Firewood collection", "Firewood Collection - post logging"); sitplugin.MapDisturbanceType("Clearcut", "Clear-cut with slash-burn"); sitplugin.MapDisturbanceType("Afforestation", "Afforestation"); sitplugin.MapDisturbanceType("Hurricane", "Generic 50% mortality"); sitplugin.Import(); }
private static void MapNonForest(Sitplugin sitplugin, JToken mappingConfig) { if (mappingConfig == null || !mappingConfig.HasValues) { return; } sitplugin.SetNonForestClassifier((string)mappingConfig["nonforest_classifier"]); foreach (var item in mappingConfig["nonforest_mapping"]) { var default_nonforest_type = (string)item["default_nonforest_type"]; if (default_nonforest_type == null) { //allow null here so that users are not forced //to look up and use the localized "Forest Only" magic string default_nonforest_type = Sitplugin.ForestOnly; } sitplugin.MapNonForest((string)item["user_nonforest_type"], default_nonforest_type); } }
static void Main(string[] args) { var options = new CommandLineOptions(); var result = Parser.Default.ParseArguments <CommandLineOptions>(args); result.WithParsed(a => { if (a.CultureInfoName != null) { Global.StringUtilities.ci = new CultureInfo(a.CultureInfoName, true); } string json = ReadFile(a.ConfigPath); JsonConfigLoader jsonConfigLoader = new JsonConfigLoader(); Sitplugin sitplugin = jsonConfigLoader.Load(json); try { sitplugin.Import(a.AppendMode); } catch (CBMSIT.ProjectCreation.ProjectCreationException ex) { System.Console.WriteLine(ex.Message); throw ex; } }); }
public void Import(string jsonConfigTemplatePath, string outputPath, string inputPath) { string json = ""; using (var s = File.OpenRead(jsonConfigTemplatePath)) using (StreamReader reader = new StreamReader(s)) { json = reader.ReadToEnd(); } JObject jsonObj = JObject.Parse(json); jsonObj["output_path"] = outputPath; jsonObj["import_config"]["path"] = inputPath; var loader = new StandardImportToolPlugin.JsonConfigLoader(); log.Info(string.Format("running Operational-Scale CBM-CFS3" + " Standard import tool. input database {0}", inputPath)); Sitplugin sitplugin = loader.Load(jsonObj.ToString()); sitplugin.Import(); log.Info(string.Format("finished standard import tool. CBM" + " project db: {0}", outputPath)); }
/// <summary> /// use a json configuration /// </summary> private static void Method3() { //creates a config object equivalent to Method1 var config = new { output_path = @"C:\Program Files (x86)\Operational-Scale CBM-CFS3\Projects\tutorial6_json\tutorial6_json.mdb", import_config = new { path = @"C:\Program Files (x86)\Operational-Scale CBM-CFS3\Tutorials\Tutorial 6\Tutorial6.xls", ageclass_table_name = "AgeClasses$", classifiers_table_name = "Classifiers$", disturbance_events_table_name = "DistEvents$", disturbance_types_table_name = "DistType$", inventory_table_name = "Inventory$", transition_rules_table_name = "Transitions$", yield_table_name = "Growth$" }, mapping_config = new { spatial_units = new { mapping_mode = "SingleDefaultSpatialUnit", default_spuid = 42 }, disturbance_types = new { disturbance_type_mapping = new[] { new { user_dist_type = "Fire", default_dist_type = "Wildfire" }, new { user_dist_type = "Firewood collection", default_dist_type = "Firewood Collection - post logging" }, new { user_dist_type = "Clearcut", default_dist_type = "Clear-cut with slash-burn" }, new { user_dist_type = "Afforestation", default_dist_type = "Afforestation" }, new { user_dist_type = "Hurricane", default_dist_type = "Generic 50% mortality" }, } }, species = new { species_classifier = "Species", species_mapping = new[] { new { user_species = "Hispaniolan pine", default_species = "Pine" }, new { user_species = "Nonforest", default_species = "Not stocked" }, new { user_species = "Improved pine stock", default_species = "Pine" } } }, nonforest = new { nonforest_classifier = "Forest type", nonforest_mapping = new[] { new { user_nonforest_type = "Afforestation", default_nonforest_type = "Gleysolic" }, new { user_nonforest_type = "Natural forest", default_nonforest_type = Sitplugin.ForestOnly }, new { user_nonforest_type = "Control", default_nonforest_type = Sitplugin.ForestOnly } } } } }; var jsonObject = JObject.FromObject(config); string json = jsonObject.ToString(Newtonsoft.Json.Formatting.Indented); JsonConfigLoader jsonConfigLoader = new JsonConfigLoader(); Sitplugin sitplugin = jsonConfigLoader.Load(json); sitplugin.Import(); }
public Sitplugin Load(string json) { Sitplugin sitplugin = null; JObject obj = JObject.Parse(json); string outputPath = System.IO.Path.GetFullPath((string)obj["output_path"]); string archive_index_db_path = null; if (obj["archive_index_db_path"] != null) { archive_index_db_path = System.IO.Path.GetFullPath((string)obj["archive_index_db_path"]); } var mappingConfig = obj["mapping_config"]; if (mappingConfig == null) { throw new Exception("missing mapping_config section"); } bool initialize_mapping = mappingConfig["initialize_mapping"] == null ? false : (bool)mappingConfig["initialize_mapping"]; CBMSIT.UserData.UserDataSet userData = null; if (obj["import_config"] != null) { var importConfig = obj["import_config"]; if (importConfig["path"] != null) { userData = Sitplugin.ParseSITData( path: System.IO.Path.GetFullPath((string)importConfig["path"]), AgeClassTableName: (string)importConfig["ageclass_table_name"], ClassifiersTableName: (string)importConfig["classifiers_table_name"], DisturbanceEventsTableName: (string)importConfig["disturbance_events_table_name"], DisturbanceTypesTableName: (string)importConfig["disturbance_types_table_name"], InventoryTableName: (string)importConfig["inventory_table_name"], TransitionRulesTableName: (string)importConfig["transition_rules_table_name"], YieldTableName: (string)importConfig["yield_table_name"]); } else if (importConfig["ageclass_path"] != null) { userData = Sitplugin.ParseSITDataText( ageClassPath: System.IO.Path.GetFullPath((string)importConfig["ageclass_path"]), classifiersPath: System.IO.Path.GetFullPath((string)importConfig["classifiers_path"]), disturbanceEventsPath: System.IO.Path.GetFullPath((string)importConfig["disturbance_events_path"]), disturbanceTypesPath: System.IO.Path.GetFullPath((string)importConfig["disturbance_types_path"]), inventoryPath: System.IO.Path.GetFullPath((string)importConfig["inventory_path"]), transitionRulesPath: System.IO.Path.GetFullPath((string)importConfig["transition_rules_path"]), yieldPath: System.IO.Path.GetFullPath((string)importConfig["yield_path"])); } else { throw new Exception("error in import_config section"); } sitplugin = new Sitplugin(outputPath, initialize_mapping, userData, archive_index_database_path: archive_index_db_path); MapSpatialUnits(sitplugin, mappingConfig["spatial_units"]); MapSpecies(sitplugin, mappingConfig["species"]); MapNonForest(sitplugin, mappingConfig["nonforest"]); MapDisturbanceTypes(sitplugin, mappingConfig["disturbance_types"]); } else if (obj["data"] != null) { sitplugin = new Sitplugin(outputPath, initialize_mapping, archive_index_database_path: archive_index_db_path); sitplugin.AddAgeClasses( (int)obj["data"]["age_class"]["age_class_size"], (int)obj["data"]["age_class"]["num_age_classes"]); foreach (var c in obj["data"]["classifiers"]) { sitplugin.AddClassifier((string)c); } MapSpatialUnits(sitplugin, mappingConfig["spatial_units"]); MapSpecies(sitplugin, mappingConfig["species"]); MapNonForest(sitplugin, mappingConfig["nonforest"]); MapDisturbanceTypes(sitplugin, mappingConfig["disturbance_types"]); LoadConfigDataObjects(sitplugin, obj["data"]); } else { throw new Exception("expected one of 'import_config', or 'data' in configuration"); } return(sitplugin); }
private static void LoadConfigDataObjects(Sitplugin plugin, JToken dataConfig) { var parseClassifierSet = new Func <JToken, string>((t) => { return(String.Join(",", t.Select(a => (string)a))); }); foreach (var i in dataConfig["inventory"]) { plugin.AddInventory( classifierSet: parseClassifierSet(i["classifier_set"]), area: (double)i["area"], age: (int)i["age"], spatialReference: Unpack(i, "spatial_reference", -1), delay: Unpack(i, "delay", 0), unfcccLandClass: Unpack(i, "unfccc_land_class", 0), HistoricDisturbance: Unpack(i, "historic_disturbance", (string)null), MostRecentDisturbance: Unpack(i, "last_pass_disturbance", (string)null)); } foreach (var i in dataConfig["disturbance_events"]) { plugin.AddDisturbanceEvent( classifierSet: parseClassifierSet(i["classifier_set"]), disturbanceType: (string)i["disturbance_type"], timestep: (int)i["time_step"], target: (double)i["target"], targetType: (string)i["target_type"], sort: (string)i["sort"], spatialReference: Unpack(i, "spatial_reference", -1), ageMin: Unpack(i, "age_min", -1), ageMax: Unpack(i, "age_max", -1), efficiency: Unpack(i, "efficiency", 1.0), eligibility: i["eligibility"]?.Select(a => (double)a).ToArray()); } foreach (var i in dataConfig["yield"]) { plugin.AddYield( classifierSet: parseClassifierSet(i["classifier_set"]), leadingSpeciesClassifierValue: (string)i["leading_species_classifier_value"], values: i["values"].Select(a => (double)a).ToList()); } if (dataConfig["transition_rules"] != null) { foreach (var i in dataConfig["transition_rules"]) { plugin.AddTransitionRule( classifierSetSource: parseClassifierSet(i["classifier_set_source"]), classifierSetTarget: parseClassifierSet(i["classifier_set_target"]), disturbanceType: (string)i["disturbance_type"], percent: Unpack(i, "percent", 100.0), spatialReference: Unpack(i, "spatial_reference", -1), ageMin: Unpack(i, "age_min", -1), ageMax: Unpack(i, "age_max", -1), resetAge: Unpack(i, "reset_age", -1), regenDelay: Unpack(i, "regen_delay", 0)); } } }