/// <summary>
        /// Runs the specified number of simulations for each of the specified scenarios
        /// </summary>
        /// <param name="simulationInitialisationFilename">Filename of the file from which to read initialisation information</param>
        /// <param name="scenarios">Contains scenario information for this set of simulations</param>
        /// <param name="outputPath">The path to which outputs should be written</param>
        public void RunAllSimulations(string simulationInitialisationFilename, string definitionsFilename, string outputsFilename, ScenarioParameterInitialisation scenarios, string outputPath)
        {
            // Declare an instance of the class for initializing the Madingley model
            MadingleyModelInitialisation InitialiseMadingley = new MadingleyModelInitialisation(simulationInitialisationFilename, definitionsFilename, outputsFilename, outputPath);
            // Specify the output path in this instance
            InitialiseMadingley.OutputPath = outputPath;

            // List to hold the names of the scenarios to run
            List<string> ScenarioNames = new List<string>();
            // String variable to hold the index suffix to apply to output files for a given simulation
            string OutputFilesSuffix;

            // Loop over scenario names and add the name of the scenario to the list of scenarion names
            foreach (var scenario in scenarios.scenarioParameters)
            {
                ScenarioNames.Add(scenario.Item1);
            }
            
            // Check whether there is only one simulation to run
            if (scenarios.scenarioNumber == 1 && scenarios.scenarioParameters.ElementAt(scenarios.scenarioNumber-1).Item2==1)
            {
                // For a single simulation

                // Set-up the suffix for the output files
                OutputFilesSuffix = "_";
                
                // Loop over the parameters for this scenario
                for (int i = 0; i < ScenarioNames.Count; i++)
                {
                    // Add the parameter information to the suffix for this simulation
                    OutputFilesSuffix +=  ScenarioNames[0] + "_";
                }
                // Add a zero index to the end of the suffix
                OutputFilesSuffix += "0";

                //Run the simulation
                RunSimulation(scenarios, 0, InitialiseMadingley, OutputFilesSuffix, 0);

                Console.ReadKey();
            }
            else
            {

                if (InitialiseMadingley.RunSimulationsInParallel)
                {
                    // Loop over specified scenarios iteratively
                    for (int ScenarioIndex = 0; ScenarioIndex < scenarios.scenarioNumber; ScenarioIndex++)
                    {
                        //Create an array of new MadingleyModel instances for simulations under this scenario combination
                        MadingleyModel[] MadingleyEcosystemModels = new MadingleyModel
                            [scenarios.scenarioParameters.ElementAt(ScenarioIndex).Item2];

                        for (int simulation = 0; simulation < scenarios.scenarioParameters.ElementAt(ScenarioIndex).Item2; simulation++)
                        {
                            // Set up the suffix for the output files
                            OutputFilesSuffix = "_";

                            // Add the scenario label to the suffix for the output files
                            OutputFilesSuffix += ScenarioNames[ScenarioIndex] + "_";

                            // Add the simulation index number to the suffix
                            OutputFilesSuffix += simulation.ToString();

                            // Initialize the instance of MadingleyModel
                            MadingleyEcosystemModels[simulation] = new MadingleyModel(InitialiseMadingley, scenarios, ScenarioIndex, OutputFilesSuffix,
                                InitialiseMadingley.GlobalModelTimeStepUnit, simulation);
                        }

                        // Loop over the specified number of simulations for each scenario
                        //for (int simulation = 0; simulation<  scenarios.scenarioSimulationsNumber[ScenarioIndex]; simulation++)
                        Parallel.For(0, scenarios.scenarioParameters.ElementAt(ScenarioIndex).Item2, simulation =>
                        {
                            // Declare and start a timer
                            StopWatch s = new StopWatch();
                            s.Start();

                            // Run the simulation
                            MadingleyEcosystemModels[simulation].RunMadingley(InitialiseMadingley);

                            // Stop the timer and write out the time taken to run this simulation
                            s.Stop();
                            Console.WriteLine("Model run finished");
                            Console.WriteLine("Total elapsed time was {0} seconds", s.GetElapsedTimeSecs());

                        });
                    }
                }
                else
                {
                    //Run simulations sequentially

                    // Loop over specified scenarios
                    for (int ScenarioIndex = 0; ScenarioIndex < scenarios.scenarioNumber; ScenarioIndex++)
                    {
                        // Loop over the specified number of simulations for each scenario
                        for (int simulation = 0; simulation < scenarios.scenarioParameters.ElementAt(ScenarioIndex).Item2; simulation++)
                        {
                            // Set up the suffix for the output files
                            OutputFilesSuffix = "_";

                            // Add the scenario label to the suffix for the output files
                            OutputFilesSuffix += ScenarioNames[ScenarioIndex] + "_";

                            // Add the simulation index number to the suffix
                            OutputFilesSuffix += simulation.ToString();

                            // Run the current simulation
                            RunSimulation(scenarios, ScenarioIndex, InitialiseMadingley, OutputFilesSuffix, simulation);
                        }
                    }
                }
               
            }

        }
        /// <summary>
        /// Sets up the model grid within a Madingley model run
        /// </summary>
        /// <param name="initialisation">An instance of the model initialisation class</param> 
        /// <param name="scenarioParameters">The parameters for the scenarios to run</param>
        /// <param name="scenarioIndex">The index of the scenario that this model is to run</param>
        public void SetUpModelGrid(MadingleyModelInitialisation initialisation,
            ScenarioParameterInitialisation scenarioParameters, int scenarioIndex, int simulation)
        {
            // If the intialisation file contains a column pointing to another file of specific locations, and if this column is not blank then read the
            // file indicated
            if (SpecificLocations)
            {
                // Set up the model grid using these locations
                EcosystemModelGrid = new ModelGrid(BottomLatitude, LeftmostLongitude, TopLatitude, RightmostLongitude,
                    CellSize, CellSize, _CellList, EnviroStack, CohortFunctionalGroupDefinitions, StockFunctionalGroupDefinitions,
                    GlobalDiagnosticVariables, initialisation.TrackProcesses, SpecificLocations, RunGridCellsInParallel,GlobalModelTimeStepUnit);
            }
            else
            {
                _CellList = new List<uint[]>();
                //Switched order so we create cell list first then initialise cells using list rather than grid.

                uint NumLatCells = (uint)((TopLatitude - BottomLatitude) / CellSize);
                uint NumLonCells = (uint)((RightmostLongitude - LeftmostLongitude) / CellSize);

                // Loop over all cells in the model
                for (uint ii = 0; ii < NumLatCells; ii += 1)
                {
                    for (uint jj = 0; jj < NumLonCells; jj += 1)
                    {
                        // Define a vector to hold the pair of latitude and longitude indices for this grid cell
                        uint[] cellIndices = new uint[2];

                        // Add the latitude and longitude indices to this vector
                        cellIndices[0] = ii;
                        cellIndices[1] = jj;

                        // Add the vector to the list of all active grid cells
                        _CellList.Add(cellIndices);

                    }
                }

                EcologyTimer = new StopWatch();
                EcologyTimer.Start();

                // Set up a full model grid (i.e. not for specific locations)
                // Set up the model grid using these locations
                EcosystemModelGrid = new ModelGrid(BottomLatitude, LeftmostLongitude, TopLatitude, RightmostLongitude,
                    CellSize, CellSize, _CellList, EnviroStack, CohortFunctionalGroupDefinitions, StockFunctionalGroupDefinitions,
                    GlobalDiagnosticVariables, initialisation.TrackProcesses, SpecificLocations, RunGridCellsInParallel, GlobalModelTimeStepUnit);

                List<int> cellsToRemove = new List<int>();
                if (initialisation.RunRealm == "terrestrial")
                {
                    for (int ii = 0; ii < _CellList.Count; ii += 1)
                    {
                        if ((EcosystemModelGrid.GetCellEnvironment(_CellList[ii][0], _CellList[ii][1])["Realm"][0] == 2.0) ||
                            (EcosystemModelGrid.GetCellEnvironment(_CellList[ii][0], _CellList[ii][1])["LandSeaMask"][0] == 0.0))
                        {
                            cellsToRemove.Add(ii);
                        }
                    }
                }
                else if (initialisation.RunRealm == "marine")
                {
                    for (int ii = 0; ii < _CellList.Count; ii += 1)
                    {
                        if (EcosystemModelGrid.GetCellEnvironment(_CellList[ii][0], _CellList[ii][1])["Realm"][0] == 1.0)
                        {
                            cellsToRemove.Add(ii);
                        }
                    }
                }

                for (int ii = (cellsToRemove.Count - 1); ii >= 0; ii--)
                {
                    _CellList.RemoveAt(cellsToRemove[ii]);
                }

                EcologyTimer.Stop();
                Console.WriteLine("Time to initialise cells: {0}", EcologyTimer.GetElapsedTimeSecs());

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Madingley Model memory usage post grid cell seed: {0}", GC.GetTotalMemory(true) / 1E9, " (G Bytes)\n");
                Console.ForegroundColor = ConsoleColor.White;

            }

            if (initialisation.InputState)
            {
                InputModelState = new InputModelState(initialisation.ModelStatePath[simulation],
                    initialisation.ModelStateFilename[simulation],EcosystemModelGrid, _CellList);
            }

            // When the last simulation for the current scenario
            // if ((scenarioParameters.scenarioSimulationsNumber.Count == 1) && (scenarioIndex == scenarioParameters.scenarioSimulationsNumber[scenarioIndex] - 1)) EnviroStack.Clear();
            // Seed stocks and cohorts in the grid cells
            // If input state from output from a previous simulation
            if (initialisation.InputState)
            {
                // Seed grid cell cohort and stocks
                EcosystemModelGrid.SeedGridCellStocksAndCohorts(_CellList, InputModelState, CohortFunctionalGroupDefinitions, StockFunctionalGroupDefinitions);

                //remove cohorts that do not contain any biomass
                foreach (uint[] CellPair in _CellList)
                {

                    GridCellCohortHandler workingGridCellCohorts = EcosystemModelGrid.GetGridCellCohorts(CellPair[0], CellPair[1]);

                    for (int kk = 0; kk < CohortFunctionalGroupDefinitions.GetNumberOfFunctionalGroups(); kk++)
                    {
                        // Loop through each cohort in the functional group
                        for (int ll = (workingGridCellCohorts[kk].Count - 1); ll >= 0; ll--)
                        {
                            // If cohort abundance is less than the extinction threshold then add to the list for extinction
                            if (workingGridCellCohorts[kk][ll].CohortAbundance.CompareTo(0) <= 0 || workingGridCellCohorts[kk][ll].IndividualBodyMass.CompareTo(0.0) == 0)
                            {
                                // Remove the extinct cohort from the list of cohorts
                                workingGridCellCohorts[kk].RemoveAt(ll);
                            }
                        }

                    }
                }

            }
            else
            {
                EcosystemModelGrid.SeedGridCellStocksAndCohorts(_CellList, CohortFunctionalGroupDefinitions, StockFunctionalGroupDefinitions,
                    GlobalDiagnosticVariables, ref NextCohortID, InitialisationFileStrings["OutputDetail"] == "high", DrawRandomly,
                    initialisation.DispersalOnly, InitialisationFileStrings["DispersalOnlyType"], RunGridCellsInParallel);
            }

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Madingley Model memory usage pre Collect: {0}", Math.Round(GC.GetTotalMemory(true) / 1E9, 2), " (GBytes)");
            Console.ForegroundColor = ConsoleColor.White;
            GC.Collect();

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Madingley Model memory usage post Collect: {0}", Math.Round(GC.GetTotalMemory(true) / 1E9, 5), " (GBytes)\n");
            Console.ForegroundColor = ConsoleColor.White;
        }
        /// <summary>
        /// Runs a single simulation of the Madingley model
        /// </summary>
        /// <param name="scenarios">Parameter information and simulation number for all scenarios to be run</param>
        /// <param name="scenarioIndex">The index of the scenario to be run in this simulation</param>
        /// <param name="initialiseMadingley">Model initialization information for all simulations</param>
        /// <param name="outputFileSuffix">Suffix to be applied to the names of files written out by this simulation</param>
        /// <param name="simulation">The index of the simulation being run</param>
        public void RunSimulation(ScenarioParameterInitialisation scenarios, int scenarioIndex, MadingleyModelInitialisation initialiseMadingley, 
            string outputFileSuffix, int simulation)
        {
            // Declare an instance of the class that runs a Madingley model simulation
            MadingleyModel MadingleyEcosystemModel;
            
            // Declare and start a timer
            StopWatch s = new StopWatch();
            s.Start();
            StopWatch t = new StopWatch();
            t.Start();

            // Initialize the instance of MadingleyModel
            MadingleyEcosystemModel = new MadingleyModel(initialiseMadingley, scenarios, scenarioIndex, outputFileSuffix, 
                initialiseMadingley.GlobalModelTimeStepUnit,simulation);
            t.Stop();

            // Run the simulation
            MadingleyEcosystemModel.RunMadingley(initialiseMadingley);

            // Stop the timer and write out the time taken to run this simulation
            s.Stop();
            Console.WriteLine("Model run finished");
            Console.WriteLine("Total elapsed time was {0} seconds", s.GetElapsedTimeSecs());
            Console.WriteLine("Model setup time was {0} seconds", t.GetElapsedTimeSecs());
            Console.WriteLine("Model run time was {0} seconds", s.GetElapsedTimeSecs() - t.GetElapsedTimeSecs());
        }