//--------------------------------------------------------------------- private float?GetCellLength(IMetadata metadata, ref string cellLengthStr) { float mapCellLength = 0; float cellWidth = 0; bool hasCellWidth = metadata.TryGetValue(AbscissaResolution.Name, ref cellWidth); float cellHeight = 0; bool hasCellHeight = metadata.TryGetValue(OrdinateResolution.Name, ref cellHeight); if (hasCellWidth && hasCellHeight) { if (cellWidth != cellHeight) { throw CellLengthException("Cell width ({0}) in map is not = to cell height ({1})", cellWidth, cellHeight); } if (cellWidth <= 0.0) { throw CellLengthException("Cell width ({0}) in map is not > 0", cellWidth); } string units = null; if (!metadata.TryGetValue(PlanarDistanceUnits.Name, ref units)) { UI.WriteLine("Map doesn't have units for cell dimensions; assuming meters"); units = PlanarDistanceUnits.Meters; } if (units == PlanarDistanceUnits.Meters) { mapCellLength = cellWidth; } else if (units == PlanarDistanceUnits.InternationalFeet) { mapCellLength = (float)(cellWidth * 0.3048); } else if (units == PlanarDistanceUnits.SurveyFeet) { mapCellLength = (float)(cellWidth * (1200.0 / 3937)); } else { throw CellLengthException("Map has unknown units for cell dimensions: {0}", units); } cellLengthStr = string.Format("{0} meters{1}", mapCellLength, (units == PlanarDistanceUnits.Meters) ? "" : string.Format(" ({0} {1})", cellWidth, units)); return(mapCellLength); } else if (hasCellWidth && !hasCellHeight) { throw CellLengthException("Map has cell width (x-dimension) but no height (y-dimension)."); } else if (!hasCellWidth && hasCellHeight) { throw CellLengthException("Map has cell height (y-dimension) but no width (x-dimension)."); } return(null); }
//--------------------------------------------------------------------- private void Run(PlugIn plugIn) { UI.WriteLine("Running {0} ...", plugIn.Name); plugIn.Run(); }
//--------------------------------------------------------------------- private void ProcessMetadata(IMetadata metadata, Scenario scenario) { landscapeMapMetadata = metadata; string warning = ""; float? mapCellLength = null; string mapCellLengthStr = ""; try { mapCellLength = GetCellLength(metadata, ref mapCellLengthStr); } catch (ApplicationException exc) { string message = exc.Message; if (!message.StartsWith(cellLengthExceptionPrefix)) { throw; } message = message.Replace(cellLengthExceptionPrefix, ""); if (scenario.CellLength.HasValue) { warning = message; } else { throw new ApplicationException("Error: " + message); } } if (scenario.CellLength.HasValue) { cellLength = scenario.CellLength.Value; UI.WriteLine("Cell length: {0} meters", cellLength); if (mapCellLength.HasValue) { if (cellLength == mapCellLength.Value) { UI.WriteLine("Cell length in map: {0}", mapCellLengthStr); } else { UI.WriteLine("Warning: Cell length in map: {0}", mapCellLengthStr); } } else { if (warning.Length > 0) { UI.WriteLine("Warning: {0}", warning); } else { UI.WriteLine("Map has no cell length"); } } } else { // No CellLength parameter in scenario file if (mapCellLength.HasValue) { UI.WriteLine("Cell length in map: {0}", mapCellLengthStr); cellLength = mapCellLength.Value; } else { string[] message = new string[] { "Error: Ecoregion map doesn't have cell dimensions; therefore, the", " CellLength parameter must be in the scenario file." }; throw new MultiLineException(message); } } cellArea = (float)((cellLength * cellLength) / 10000); }
//--------------------------------------------------------------------- private void LoadEcoregions(string path) { UI.WriteLine("Loading ecoregions from file \"{0}\" ...", path); Ecoregions.DatasetParser parser = new Ecoregions.DatasetParser(); ecoregions = Data.Load <Ecoregions.IDataset>(path, parser); }
//--------------------------------------------------------------------- private void LoadSpecies(string path) { UI.WriteLine("Loading species data from file \"{0}\" ...", path); Species.DatasetParser parser = new Species.DatasetParser(); species = Data.Load <Species.IDataset>(path, parser); }
//--------------------------------------------------------------------- /// <summary> /// Runs a model scenario. /// </summary> public void Run(string scenarioPath) { siteVarRegistry.Clear(); Scenario scenario = LoadScenario(scenarioPath); startTime = scenario.StartTime; endTime = scenario.EndTime; timeSinceStart = 0; currentTime = startTime; InitializeRandomNumGenerator(scenario.RandomNumberSeed); LoadSpecies(scenario.Species); LoadEcoregions(scenario.Ecoregions); UI.WriteLine("Initializing landscape from ecoregions map \"{0}\" ...", scenario.EcoregionsMap); Ecoregions.Map ecoregionsMap = new Ecoregions.Map(scenario.EcoregionsMap, ecoregions, rasterDriverMgr); ProcessMetadata(ecoregionsMap.Metadata, scenario); using (IInputGrid <EcoregionCode> grid = ecoregionsMap.OpenAsInputGrid()) { UI.WriteLine("Map dimensions: {0} = {1:#,##0} cell{2}", grid.Dimensions, grid.Count, (grid.Count == 1 ? "" : "s")); landscape = new Landscape(grid, scenario.BlockSize.HasValue ? scenario.BlockSize.Value : 1); } UI.WriteLine("Sites: {0:#,##0} active ({1:p1}), {2:#,##0} inactive ({3:p1})", landscape.ActiveSiteCount, (landscape.Count > 0 ? ((double)landscape.ActiveSiteCount) / landscape.Count : 0), landscape.InactiveSiteCount, (landscape.Count > 0 ? ((double)landscape.InactiveSiteCount) / landscape.Count : 0)); ecoregionSiteVar = ecoregionsMap.CreateSiteVar(landscape); plugInsWith2PhaseInit = new List <PlugIns.I2PhaseInitialization>(); plugInsWithCleanUp = new List <PlugIns.ICleanUp>(); try { UI.WriteLine("Loading {0} plug-in ...", scenario.Succession.Info.Name); succession = Loader.Load <SuccessionPlugIn>(scenario.Succession.Info); succession.Initialize(scenario.Succession.InitFile, this); succession.InitializeSites(scenario.InitialCommunities, scenario.InitialCommunitiesMap); PlugIn[] disturbancePlugIns = LoadAndInitPlugIns(scenario.Disturbances); PlugIn[] otherPlugIns = LoadAndInitPlugIns(scenario.OtherPlugIns); // Perform 2nd phase of initialization for those plug-ins // that have it. foreach (PlugIns.I2PhaseInitialization plugIn in plugInsWith2PhaseInit) { plugIn.InitializePhase2(); } // Run output plug-ins for TimeSinceStart = 0 (time step 0) foreach (PlugIn plugIn in otherPlugIns) { if (plugIn.PlugInType.IsMemberOf("output")) { Run(plugIn); } } //******************// for Rob // Main time loop // //******************// for (currentTime++, timeSinceStart++; currentTime <= endTime; currentTime++, timeSinceStart++) { bool isSuccTimestep = IsPlugInTimestep(succession); List <PlugIn> distPlugInsToRun; distPlugInsToRun = GetPlugInsToRun(disturbancePlugIns); bool isDistTimestep = distPlugInsToRun.Count > 0; List <PlugIn> otherPlugInsToRun; otherPlugInsToRun = GetPlugInsToRun(otherPlugIns); if (!isSuccTimestep && !isDistTimestep && otherPlugInsToRun.Count == 0) { continue; } UI.WriteLine("Current time: {0}", currentTime); if (isDistTimestep) { if (scenario.DisturbancesRandomOrder) { Util.Random.Shuffle(distPlugInsToRun); } foreach (PlugIn distPlugIn in distPlugInsToRun) { Run(distPlugIn); } } if (isSuccTimestep || isDistTimestep) { Run(succession); } foreach (PlugIn otherPlugIn in otherPlugInsToRun) { Run(otherPlugIn); } } // main time loop } finally { foreach (PlugIns.ICleanUp plugIn in plugInsWithCleanUp) { plugIn.CleanUp(); } } }
public static int Main(string[] args) { try { UI.TextWriter = Console.Out; Assembly assembly = Assembly.GetExecutingAssembly(); Version version = assembly.GetName().Version; string release = AssemblyInfo.GetRelease(assembly); if (release == "official") { release = ""; } else if (release.StartsWith("candidate")) { release = string.Format(" (release {0})", release); } else { release = string.Format(" ({0} release)", release); } UI.WriteLine("Landis-II {0}.{1}{2}", version.Major, version.Minor, release); UI.WriteLine("Copyright 2004-2005 University of Wisconsin"); UI.WriteLine(); if (args.Length == 0) { UI.WriteLine("Error: No scenario file specified."); return(1); } if (args.Length > 1) { UI.WriteLine("Error: Extra argument(s) on command line:"); StringBuilder argsList = new StringBuilder(); argsList.Append(" "); for (int i = 1; i < args.Length; ++i) { argsList.AppendFormat(" {0}", args[i]); } UI.WriteLine(argsList.ToString()); return(1); } Landis.Model.Run(args[0]); return(0); } catch (ApplicationException exc) { UI.WriteLine(exc.Message); return(1); } catch (Exception exc) { UI.WriteLine("Internal error occurred within the program:"); UI.WriteLine(" {0}", exc.Message); if (exc.InnerException != null) { UI.WriteLine(" {0}", exc.InnerException.Message); } UI.WriteLine(); UI.WriteLine("Stack trace:"); UI.WriteLine(exc.StackTrace); return(1); } }