//--------------------------------------------------------------------- public static void Register(LandUse landUse) { if (landUse == null) { throw new System.ArgumentNullException(); } registry[landUse.MapCode] = landUse; }
//--------------------------------------------------------------------- public override void Run() { if (SiteLog.Enabled) { SiteLog.TimestepSetUp(); } ProcessInputMap( delegate(Site site, LandUse newLandUse) { LandUse currentLandUse = SiteVars.LandUse[site]; if (newLandUse != currentLandUse) { SiteVars.LandUse[site] = newLandUse; string transition = string.Format("{0} --> {1}", currentLandUse.Name, newLandUse.Name); if (!currentLandUse.AllowEstablishment && newLandUse.AllowEstablishment) { string message = string.Format("Error: The land-use change ({0}) at pixel {1} requires re-enabling establishment, but that's not currently supported", transition, site.Location); throw new System.ApplicationException(message); } else if (currentLandUse.AllowEstablishment && !newLandUse.AllowEstablishment) { Reproduction.PreventEstablishment((ActiveSite)site); } if (isDebugEnabled) { log.DebugFormat(" LU at {0}: {1}", site.Location, transition); } newLandUse.LandCoverChange.ApplyTo((ActiveSite)site); if (SiteLog.Enabled) { SiteLog.WriteTotalsFor((ActiveSite)site); } return(transition); } else { return(null); } }); if (SiteLog.Enabled) { SiteLog.TimestepTearDown(); } }
//--------------------------------------------------------------------- //Modified to add inputMapPath, allowing users to specify raster paths to change at timestep public void ProcessInputMap(ProcessLandUseAt processLandUseAt) { string inputMapPath = MapNames.ReplaceTemplateVars(inputMapTemplate, Model.Core.CurrentTime); Model.Core.UI.WriteLine(" Reading map \"{0}\"...", inputMapPath); IInputRaster <MapPixel> inputMap; Dictionary <string, int> counts = new Dictionary <string, int>(); using (inputMap = Model.Core.OpenRaster <MapPixel>(inputMapPath)) { MapPixel pixel = inputMap.BufferPixel; foreach (Site site in Model.Core.Landscape.AllSites) { inputMap.ReadBufferPixel(); if (site.IsActive) { LandUse landUse = LandUseRegistry.LookUp(pixel.LandUseCode.Value); if (landUse == null) { string message = string.Format("Error: Unknown map code ({0}) at pixel {1}", pixel.LandUseCode.Value, site.Location); throw new System.ApplicationException(message); } string key = processLandUseAt(site, landUse); if (key != null) { int count; if (counts.TryGetValue(key, out count)) { count = count + 1; } else { count = 1; } counts[key] = count; } } } } foreach (string key in counts.Keys) { Model.Core.UI.WriteLine(" {0} ({1:#,##0})", key, counts[key]); } }
//--------------------------------------------------------------------- protected void ReadLandUses() { InputVar <string> name = new InputVar <string>("LandUse"); InputVar <ushort> mapCode = new InputVar <ushort>("MapCode"); InputVar <bool> allowHarvest = new InputVar <bool>("AllowHarvest?"); InputVar <string> landCoverChangeType = new InputVar <string>("LandCoverChange"); Dictionary <string, int> nameLineNumbers = new Dictionary <string, int>(); Dictionary <ushort, int> mapCodeLineNumbers = new Dictionary <ushort, int>(); PartialThinning.InitializeClass(); while (!AtEndOfInput) { int nameLineNum = LineNumber; ReadVar(name); int lineNumber; if (nameLineNumbers.TryGetValue(name.Value.Actual, out lineNumber)) { throw new InputValueException(name.Value.String, "The land use \"{0}\" was previously used on line {1}", name.Value.Actual, lineNumber); } else { nameLineNumbers[name.Value.Actual] = nameLineNum; } int mapCodeLineNum = LineNumber; ReadVar(mapCode); if (mapCodeLineNumbers.TryGetValue(mapCode.Value.Actual, out lineNumber)) { throw new InputValueException(mapCode.Value.String, "The map code \"{0}\" was previously used on line {1}", mapCode.Value.Actual, lineNumber); } else { mapCodeLineNumbers[mapCode.Value.Actual] = mapCodeLineNum; } ReadVar(allowHarvest); // By default, a land use allows trees to establish. bool allowEstablishment = true; if (ReadPreventEstablishment()) { allowEstablishment = false; } Dictionary <string, LandCover.IChange> landCoverChanges = new Dictionary <string, LandCover.IChange>(); List <LandCover.IChange> landCoverList = new List <LandCover.IChange>(); ReadVar(landCoverChangeType); LandCover.IChange landCoverChange = ProcessLandCoverChange(landCoverChangeType); landCoverChanges[landCoverChange.Type] = landCoverChange; landCoverList.Add(landCoverChange); while (ReadOptionalVar(landCoverChangeType)) //Get extra LandCoverChanges { if (landCoverChanges.TryGetValue(landCoverChangeType.Value.Actual, out landCoverChange)) { throw new InputValueException(landCoverChangeType.Value.String, "The land cover change \"{0}\" has already been defined for land use: {1}", landCoverChangeType.Value.Actual, name.Value.Actual); } else { landCoverChange = ProcessLandCoverChange(landCoverChangeType); landCoverChanges[landCoverChange.Type] = landCoverChange; landCoverList.Add(landCoverChange); } } LandCover.IChange[] changes = new LandCover.IChange[landCoverList.Count]; for (int i = 0; i < landCoverList.Count; i++) { changes[i] = landCoverList[i]; } LandUse landUse = new LandUse(name.Value.Actual, mapCode.Value.Actual, allowHarvest.Value.Actual, allowEstablishment, changes); LandUseRegistry.Register(landUse); } }
//--------------------------------------------------------------------- public override void Run() { if (SiteLog.Enabled) { SiteLog.TimestepSetUp(); } if (pauseFunction != null) { pauseFunction.PauseTimestep(); } ProcessInputMap( delegate(Site site, LandUse newLandUse) { LandUse currentLandUse = SiteVars.LandUse[site]; string siteKey = null; if (newLandUse != currentLandUse) { SiteVars.LandUse[site] = newLandUse; siteKey = string.Format("{0} --> {1}", currentLandUse.Name, newLandUse.Name); if (!currentLandUse.AllowEstablishment && newLandUse.AllowEstablishment) { Reproduction.EnableEstablishment((ActiveSite)site); } else if (currentLandUse.AllowEstablishment && !newLandUse.AllowEstablishment) { Reproduction.PreventEstablishment((ActiveSite)site); } if (isDebugEnabled) { log.DebugFormat(" LU at {0}: {1}", site.Location, siteKey); } for (int i = 0; i < newLandUse.LandCoverChanges.Length; i++) { LandCover.IChange LandCoverChange = newLandUse.LandCoverChanges[i]; LandCoverChange.ApplyTo((ActiveSite)site, true); } } else { /*if (!currentLandUse.AllowEstablishment) * { * Reproduction.PreventEstablishment((ActiveSite)site); * } * else * { * Reproduction.EnableEstablishment((ActiveSite)site); * }*/ for (int i = 0; i < currentLandUse.LandCoverChanges.Length; i++) { LandCover.IChange LandCoverChange = newLandUse.LandCoverChanges[i]; LandCoverChange.ApplyTo((ActiveSite)site, false); } } if (SiteLog.Enabled) { SiteLog.WriteTotalsFor((ActiveSite)site); } return(siteKey); }); if (SiteLog.Enabled) { SiteLog.TimestepTearDown(); } }
//--------------------------------------------------------------------- public static void Register(LandUse landUse) { if (landUse == null) throw new System.ArgumentNullException(); registry[landUse.MapCode] = landUse; }
//--------------------------------------------------------------------- protected void ReadLandUses() { InputVar <string> name = new InputVar <string>("LandUse"); InputVar <ushort> mapCode = new InputVar <ushort>("MapCode"); InputVar <bool> allowHarvest = new InputVar <bool>("AllowHarvest?"); InputVar <string> landCoverChangeType = new InputVar <string>("LandCoverChange"); Dictionary <string, int> nameLineNumbers = new Dictionary <string, int>(); Dictionary <ushort, int> mapCodeLineNumbers = new Dictionary <ushort, int>(); while (!AtEndOfInput) { int nameLineNum = LineNumber; ReadVar(name); int lineNumber; if (nameLineNumbers.TryGetValue(name.Value.Actual, out lineNumber)) { throw new InputValueException(name.Value.String, "The land use \"{0}\" was previously used on line {1}", name.Value.Actual, lineNumber); } else { nameLineNumbers[name.Value.Actual] = nameLineNum; } int mapCodeLineNum = LineNumber; ReadVar(mapCode); if (mapCodeLineNumbers.TryGetValue(mapCode.Value.Actual, out lineNumber)) { throw new InputValueException(mapCode.Value.String, "The map code \"{0}\" was previously used on line {1}", mapCode.Value.Actual, lineNumber); } else { mapCodeLineNumbers[mapCode.Value.Actual] = mapCodeLineNum; } ReadVar(allowHarvest); // By default, a land use allows trees to establish. bool allowEstablishment = true; ReadVar(landCoverChangeType); LandCover.IChange landCoverChange = null; if (landCoverChangeType.Value.Actual == LandCover.NoChange.TypeName) { landCoverChange = noLandCoverChange; } else if (landCoverChangeType.Value.Actual == LandCover.RemoveTrees.TypeName) { ICohortSelector selector = ReadSpeciesAndCohorts("LandUse", ParameterNames.Plant, ParameterNames.PreventEstablishment); ICohortCutter cohortCutter = CohortCutterFactory.CreateCutter(selector, Main.ExtType); Planting.SpeciesList speciesToPlant = ReadSpeciesToPlant(); landCoverChange = new LandCover.RemoveTrees(cohortCutter, speciesToPlant); if (ReadPreventEstablishment()) { allowEstablishment = false; } } else { throw new InputValueException(landCoverChangeType.Value.String, "\"{0}\" is not a type of land cover change", landCoverChangeType.Value.Actual); } LandUse landUse = new LandUse(name.Value.Actual, mapCode.Value.Actual, allowHarvest.Value.Actual, allowEstablishment, landCoverChange); LandUseRegistry.Register(landUse); } }