//---------------------------------------------------------------------
 /// <summary>
 /// Initializes a new instance from a specified reader and a formatted
 /// message.
 /// </summary>
 public LineReaderException(LineReader      reader,
     string          message,
     params object[] mesgArgs)
     : base(MakeLocationMessage(reader), string.Format(message, mesgArgs))
 {
     SetLocation(reader);
 }
 //---------------------------------------------------------------------
 /// <summary>
 /// Creates a new InputLine for a particular LineReader.
 /// </summary>
 public InputLine(LineReader reader)
 {
     Require.ArgumentNotNull(reader);
     this.reader = reader;
     this.expectedNames = new List<string>();
     GetNext();
 }
        //---------------------------------------------------------------------

        private BaseHarvest.IParameters ParseFile(string filename)
        {
            try {
                reader = OpenFile(filename);
                return parser.Parse(reader);
            }
            finally {
                reader.Close();
            }
        }
		public void Init()
		{
			parser = new ParameterParser();

			Ecoregions.DatasetParser ecoregionsParser = new Ecoregions.DatasetParser();
			reader = OpenFile("Ecoregions.txt");
			try {
				ParameterParser.EcoregionsDataset = ecoregionsParser.Parse(reader);
			}
			finally {
				reader.Close();
			}
		}
        public void Init()
        {
            parser = new ParametersParser();

            Species.DatasetParser speciesParser = new Species.DatasetParser();
            reader = OpenFile("Species.txt");
            try {
                ParametersParser.SpeciesDataset = speciesParser.Parse(reader);
            }
            finally {
                reader.Close();
            }
        }
        public void Communities_Timestep10()
        {
            reader = OpenFile("Communities.txt");
            IDataset dataset = parser.Parse(reader);

            ICommunity community = dataset.Find(100);
            Assert.IsNotNull(community);
            ISpeciesCohorts<AgeCohort.ICohort> oakCohorts = community.Cohorts[oak];
            Assert.AreEqual(5, oakCohorts.Count);
            Assert.AreEqual(10, oakCohorts[0]);
            Assert.AreEqual(20, oakCohorts[1]);
            Assert.AreEqual(30, oakCohorts[2]);
            Assert.AreEqual(100, oakCohorts[3]);
            Assert.AreEqual(150, oakCohorts[4]);
        }
        public void Init()
        {
            Species.DatasetParser speciesParser = new Species.DatasetParser();
            reader = OpenFile("Species.txt");
            try {
                speciesDataset = speciesParser.Parse(reader);
            }
            finally {
                reader.Close();
            }

            parser = new ParametersParser(speciesDataset,
                                          startTime,
                                          endTime);
        }
        public void Communities_Timestep10()
        {
            reader = OpenFile("Communities.txt");
            IDataset dataset = parser.Parse(reader);

            ICommunity community = dataset.Find(100);
            Assert.IsNotNull(community);

            ISpeciesCohorts oakCohorts = community.Cohorts[oak];
            Assert.AreEqual(5, oakCohorts.Count);

            List<ushort> actualAges = new List<ushort>(oakCohorts.Count);
            foreach (ICohort cohort in oakCohorts)
                actualAges.Add(cohort.Age);
            ushort[] expectedAges = new ushort[] { 150, 100, 30, 20, 10 };
            AssertAreEqual(expectedAges, actualAges.ToArray());
        }
        //---------------------------------------------------------------------

        private void TryParse(string filename)
        {
            int? errorLineNum = Testing.FindErrorMarker(Data.MakeInputPath(filename));
            try {
                reader = OpenFile(filename);
                ExtensionInfo extension = parser.Parse(reader);
            }
            catch (System.Exception e) {
                Data.Output.WriteLine();
                Data.Output.WriteLine(e.Message.Replace(Data.Directory, Data.DirPlaceholder));
                LineReaderException lrExc = e as LineReaderException;
                if (lrExc != null && errorLineNum.HasValue)
                    Assert.AreEqual(errorLineNum.Value, lrExc.LineNumber);
                throw;
            }
            finally {
                reader.Close();
            }
        }
		//---------------------------------------------------------------------

		private void TryParse(string filename,
		                      int    errorLineNum)
		{
			try {
				reader = OpenFile(filename);
				IScenario scenario = parser.Parse(reader);
			}
			catch (System.Exception e) {
				Data.Output.WriteLine(e.Message.Replace(Data.Directory, dataDirPlaceholder));
				LineReaderException lrExc = e as LineReaderException;
				if (lrExc != null)
					Assert.AreEqual(errorLineNum, lrExc.LineNumber);
				Data.Output.WriteLine();
				throw;
			}
			finally {
				reader.Close();
			}
		}
        //---------------------------------------------------------------------
        private void CheckExpectedLines(LineReader reader,
            string     expectedLinesFile)
        {
            string path = System.IO.Path.Combine(dataDir, expectedLinesFile);
            List<ExpectedLine> lines = ExpectedLine.ReadLines(path);

            foreach (ExpectedLine expectedLine in lines) {
                string line = reader.ReadLine();
                Assert.IsNotNull(line);
                Assert.AreEqual(expectedLine.Number, reader.LineNumber);
                Assert.AreEqual(expectedLine.Text, line);
            }
            Assert.IsNull(reader.ReadLine());
            Assert.AreEqual(LineReader.EndOfInput, reader.LineNumber);
        }
 //---------------------------------------------------------------------
 private IParameters ParseFile(string filename)
 {
     reader = OpenFile(filename);
     IParameters parameters = parser.Parse(reader);
     reader.Close();
     return parameters;
 }
        //---------------------------------------------------------------------

        private ExtensionInfo ParseFile(string filename)
        {
        	try {
	            reader = OpenFile(filename);
	            return parser.Parse(reader);
        	}
        	finally {
	            reader.Close();
        	}
        }
 //---------------------------------------------------------------------
 /// <summary>
 /// Initializes a new instance from a specified reader and an inner
 /// exception.
 /// </summary>
 public LineReaderException(LineReader       reader,
     System.Exception exception)
     : base(MakeLocationMessage(reader), exception)
 {
     SetLocation(reader);
 }
        //---------------------------------------------------------------------
        private void ReadAndCheckParameters(string filename)
        {
            IParameters parameters;
            try {
                reader = OpenFile(filename);
                parameters = parser.Parse(reader);
            }
            finally {
                reader.Close();
            }

            try {
                //  Now that we know the data file is properly formatted, read
                //  data from it and compare it against parameter object.
                reader = OpenFile(filename);
                InputLine = new InputLine(reader);

                Assert.AreEqual(parser.LandisDataValue, ReadInputVar<string>("LandisData"));

                CheckBiomassParameters(parameters, null);
                //  Note: The null parameter above indicates that no parameter
                //  or table are expected after the biomass parameters.
            }
            finally {
                InputLine = null;
                reader.Close();
            }
        }
 //---------------------------------------------------------------------
 private void SetLocation(LineReader reader)
 {
     this.line = reader.LineNumber;
     this.source = reader.SourceName;
 }
 //---------------------------------------------------------------------
 private static string MakeLocationMessage(LineReader reader)
 {
     StringBuilder message = new StringBuilder();
     message.Append("Error at ");
     if (reader.LineNumber == LineReader.EndOfInput)
         message.Append("end");
     else
         message.Append(string.Format("line {0}", reader.LineNumber));
     if (! string.IsNullOrEmpty(reader.SourceName))
         message.Append(" of " + reader.SourceName);
     return message.ToString();
 }
        public void Reclass()
        {
            reader = OpenFile("Reclass.txt");
            IParameters parameters = parser.Parse(reader);

            Assert.AreEqual(20, parameters.Timestep);

            int speciesCount = ParametersParser.SpeciesDataset.Count;

            Assert.AreEqual(speciesCount, parameters.ReclassCoefficients.Length);
            foreach (Species.ISpecies species in ParametersParser.SpeciesDataset) {
                double expectedCoeff = 0.0;
                if (species.Name == "abiebals")
                    expectedCoeff = 0.5;
                else if (species.Name == "tiliamer")
                    expectedCoeff = 0.1;
                Assert.AreEqual(expectedCoeff,
                                parameters.ReclassCoefficients[species.Index]);
            }

            Assert.AreEqual(2, parameters.ReclassMaps.Length);
            foreach (IMapDefinition mapDefn in parameters.ReclassMaps) {
                if (mapDefn.Name == "reclass1") {
                    foreach (IForestType forestType in mapDefn.ForestTypes) {
                        if (forestType.Name == "MapleHardwood")
                            CheckForestType(forestType,
                                            new string[] {"acersacc", "betualle"},
                                            new string[] {"pinubank"});
                        else if (forestType.Name == "Other")
                            CheckForestType(forestType,
                                            new string[] {"pinustro", "poputrem", "betupapy"},
                                            null);
                        else
                            Assert.Fail("Forest type is not MapleHardwood or Other");
                    }
                }
                else if (mapDefn.Name == "reclass2") {
                    foreach (IForestType forestType in mapDefn.ForestTypes) {
                        if (forestType.Name == "MapleHardwood")
                            CheckForestType(forestType,
                                            new string[] {"acersacc", "betualle", "tiliamer", "fraxamer"},
                                            null);
                        else if (forestType.Name == "EarlySucc")
                            CheckForestType(forestType,
                                            new string[] {"poputrem", "betupapy", "pinubank"},
                                            null);
                        else if (forestType.Name == "Other")
                            CheckForestType(forestType,
                                            new string[] {"pinustro"},
                                            null);
                        else
                            Assert.Fail("Forest type is not MapleHardwood, EarlySucc or Other");
                    }
                }
                else
                    Assert.Fail("Name of map definition is not reclass1 or reclass2");
            }
        }
 //---------------------------------------------------------------------
 private void TryParse(string filename,
     int    errorLineNum)
 {
     try {
         reader = OpenFile(filename);
         // This method is only called on bad files, so we expect the
         // statement below to throw an exception.  Since we knowingly
         // ignore the variable "landUses", disable the CS0219 warning
         // "The variable '...' is assigned but its value is never used'.
     #pragma warning disable 0219
         IList<LandUse> landUses = parser.Parse(reader);
     #pragma warning restore 0219
     }
     catch (System.Exception e) {
         Data.Output.WriteLine(e.Message.Replace(Data.Directory, Data.DirPlaceholder));
         LineReaderException lrExc = e as LineReaderException;
         if (lrExc != null)
             Assert.AreEqual(errorLineNum, lrExc.LineNumber);
         Data.Output.WriteLine();
         throw;
     }
     finally {
         reader.Close();
     }
 }
 //---------------------------------------------------------------------
 private IList<LandUse> ParseFile(string filename)
 {
     reader = OpenFile(filename);
     IList<LandUse> landUses = parser.Parse(reader);
     reader.Close();
     return landUses;
 }
		//---------------------------------------------------------------------

		private IScenario ParseFile(string filename)
		{
			reader = OpenFile(filename);
			IScenario scenario = parser.Parse(reader);
			reader.Close();
			return scenario;
		}
 //---------------------------------------------------------------------
 /// <summary>
 /// Initializes a new instance from a specified reader and message.
 /// </summary>
 public LineReaderException(LineReader reader,
     string     message)
     : base(MakeLocationMessage(reader), message)
 {
     SetLocation(reader);
 }
        //---------------------------------------------------------------------
        private void ReadAndCheckParameters(string filename)
        {
            IParameters parameters;
            try {
                reader = Data.OpenFile(filename);
                parameters = parser.Parse(reader);
            }
            finally {
                reader.Close();
            }

            try {
                //  Now that we know the data file is properly formatted, read
                //  data from it and compare it against parameter object.
                reader = Data.OpenFile(filename);
                InputLine = new InputLine(reader);

                Assert.AreEqual(parser.LandisDataValue, ReadInputVar<string>("LandisData"));

                Assert.AreEqual(ReadInputVar<int>(ParametersParser.Names.Timestep), parameters.Timestep);
                Assert.AreEqual(ReadInputVar<SeedingAlgorithms>(ParametersParser.Names.SeedingAlgorithm),
                                parameters.SeedAlgorithm);

                CheckBiomassParameters(parameters, ParametersParser.Names.AgeOnlyDisturbanceParms,
                                                   ParametersParser.Names.ClimateChange);

                if (parameters.AgeOnlyDisturbanceParms != null)
                    Assert.AreEqual(ReadInputVar<string>(ParametersParser.Names.AgeOnlyDisturbanceParms),
                                    parameters.AgeOnlyDisturbanceParms);

                if (InputLine.VariableName == ParametersParser.Names.ClimateChange) {
                    InputLine.GetNext();  // Skip table name
                    foreach (ParametersUpdate parameterUpdate in parameters.ClimateChangeUpdates) {
                        StringReader currentLine = new StringReader(InputLine.ToString());
                        Assert.AreEqual(InputValidation.ParseYear(ReadInputValue<string>(currentLine)),
                                        parameterUpdate.Year);
                        Assert.AreEqual(ReadInputValue<string>(currentLine),
                                        parameterUpdate.File);
                        InputLine.GetNext();
                    }
                }
            }
            finally {
                InputLine = null;
                reader.Close();
            }
        }