public static List <InputModelState> ConvertModelStates( Madingley.Common.ModelState modelState, Madingley.Common.Configuration c, Madingley.Common.Environment e) { var numLatCells = (UInt32)((e.TopLatitude - e.BottomLatitude) / e.CellSize); var numLonCells = (UInt32)((e.RightmostLongitude - e.LeftmostLongitude) / e.CellSize); // Set up a grid of grid cells var gridCellCohorts = new GridCellCohortHandler[numLatCells, numLonCells]; var gridCellStocks = new GridCellStockHandler[numLatCells, numLonCells]; gridCellCohorts[0, 0] = new GridCellCohortHandler(c.CohortFunctionalGroupDefinitions.Data.Count()); var cellList = e.FocusCells.ToArray(); var gridCells = modelState.GridCells.ToArray(); for (var ii = 0; ii < cellList.Count(); ii++) { var gridCell = gridCells[ii]; gridCellCohorts[cellList[ii].Item1, cellList[ii].Item2] = ConvertCohorts(gridCell.Cohorts); gridCellStocks[cellList[ii].Item1, cellList[ii].Item2] = ConvertStocks(gridCell.Stocks); } var inputModelState = new InputModelState(gridCellCohorts, gridCellStocks); return(new List <InputModelState>() { inputModelState }); }
public static void Serialize(Madingley.Common.Environment environment, TextWriter sw) { Action <Newtonsoft.Json.JsonWriter, Tuple <int, int> > JsonAddPropertyFocusCell = (jsonWriter, value) => { jsonWriter.WriteStartArray(); Common.Writer.WriteInt(jsonWriter, value.Item1); Common.Writer.WriteInt(jsonWriter, value.Item2); jsonWriter.WriteEndArray(); }; Action <Newtonsoft.Json.JsonWriter, IEnumerable <KeyValuePair <string, double[]> > > JsonAddPropertyCellEnvironment = (jsonWriter, value) => { Common.Writer.WriteKeyValuePairs(jsonWriter, value, (JsonWriter, key, val) => Common.Writer.PropertyInlineArray(JsonWriter, key, val, Common.Writer.WriteDouble)); }; using (var writer = new Newtonsoft.Json.JsonTextWriter(sw)) { writer.Formatting = Newtonsoft.Json.Formatting.Indented; writer.WriteStartObject(); Common.Writer.PropertyDouble(writer, "CellSize", environment.CellSize); Common.Writer.PropertyDouble(writer, "BottomLatitude", environment.BottomLatitude); Common.Writer.PropertyDouble(writer, "TopLatitude", environment.TopLatitude); Common.Writer.PropertyDouble(writer, "LeftmostLongitude", environment.LeftmostLongitude); Common.Writer.PropertyDouble(writer, "RightmostLongitude", environment.RightmostLongitude); Common.Writer.PropertyKeyValuePairs(writer, "Units", environment.Units, Common.Writer.PropertyString); Common.Writer.PropertyBoolean(writer, "SpecificLocations", environment.SpecificLocations); Common.Writer.PropertyInlineArray(writer, "FocusCells", environment.FocusCells, JsonAddPropertyFocusCell); Common.Writer.PropertyArray(writer, "CellEnvironment", environment.CellEnvironment, JsonAddPropertyCellEnvironment); Common.Writer.PropertyInlineArray(writer, "FileNames", environment.FileNames, Common.Writer.WriteString); writer.WriteEndObject(); } }
public static MadingleyModelInitialisation ConvertInitialisation( Madingley.Common.ModelState m, Madingley.Common.Configuration d, Madingley.Common.Environment e) { var i = new MadingleyModelInitialisation("", "", "", ""); i.GlobalModelTimeStepUnit = d.GlobalModelTimeStepUnit; i.NumTimeSteps = (uint)d.NumTimeSteps; i.BurninTimeSteps = (uint)d.BurninTimeSteps; i.ImpactTimeSteps = (uint)d.ImpactTimeSteps; i.RecoveryTimeSteps = (uint)d.RecoveryTimeSteps; i.CellSize = e.CellSize; i.BottomLatitude = (float)e.BottomLatitude; i.TopLatitude = (float)e.TopLatitude; i.LeftmostLongitude = (float)e.LeftmostLongitude; i.RightmostLongitude = (float)e.RightmostLongitude; i.RunCellsInParallel = d.RunCellsInParallel; i.RunSimulationsInParallel = d.RunSimulationsInParallel; i.RunRealm = d.RunRealm; i.DrawRandomly = d.DrawRandomly; i.ExtinctionThreshold = d.ExtinctionThreshold; i.MaxNumberOfCohorts = d.MaxNumberOfCohorts; i.DispersalOnly = d.DispersalOnly; i.PlanktonDispersalThreshold = d.PlanktonDispersalThreshold; i.SpecificLocations = e.SpecificLocations; i.InitialisationFileStrings = new SortedList <string, string>(); i.InitialisationFileStrings["OutputDetail"] = "high"; i.InitialisationFileStrings["DispersalOnlyType"] = d.DispersalOnlyType; i.CohortFunctionalGroupDefinitions = ConvertFunctionalGroupDefinitions(d.CohortFunctionalGroupDefinitions); i.StockFunctionalGroupDefinitions = ConvertFunctionalGroupDefinitions(d.StockFunctionalGroupDefinitions); if (m != null) { i.EnviroStack = ConvertEnvironment(m.GridCells); } else { i.EnviroStack = ConvertEnvironment(e.CellEnvironment); } i.CellList = e.FocusCells.Select(a => new UInt32[] { (uint)a.Item1, (uint)a.Item2 }).ToList(); i.TrackProcesses = true; i.TrackCrossCellProcesses = true; i.TrackGlobalProcesses = true; i.Units = new SortedList <string, string>(e.Units); i.ImpactCellIndices = d.ImpactCellIndices.Select(ii => (uint)ii).ToList(); i.ImpactAll = d.ImpactAll; if (m != null) { i.ModelStates = ConvertModelStates(m, d, e); i.InputState = true; i.InputGlobalDiagnosticVariables = new SortedList <string, double>(m.GlobalDiagnosticVariables); } else { i.ModelStates = null; i.InputState = false; } return(i); }
public MadingleyModelOutput( string outputFilesSuffix, Madingley.Common.Environment environment, Madingley.Common.Configuration configuration, MadingleyModelInitialisation outputSettings, Madingley.Common.ModelState modelState) { var modelInitialisation = Converters.ConvertInitialisation( outputSettings, configuration, environment); this.ProcessTracker = new Madingley.Common.IProcessTracker[environment.FocusCells.Count()][]; for (var cellIndex = 0; cellIndex < environment.FocusCells.Count(); cellIndex++) { var processTracker = new GEMProcessTracker(cellIndex, this); this.ProcessTracker[cellIndex] = new Madingley.Common.IProcessTracker[] { processTracker }; } var globalProcessTracker = new GEMGlobalProcessTracker(this); this.GlobalProcessTracker = new Madingley.Common.IGlobalProcessTracker[] { globalProcessTracker }; var crossCellProcessTracker = new GEMCrossCellProcessTracker(this); this.CrossCellProcessTracker = new Madingley.Common.ICrossCellProcessTracker[] { crossCellProcessTracker }; this.model = new MadingleyModel( modelInitialisation, outputFilesSuffix, configuration.Simulation, modelState); }
public static MadingleyModelInitialisation ConvertInitialisation( MadingleyModelInitialisation outputSettings, Madingley.Common.Configuration configuration, Madingley.Common.Environment e) { var i = new MadingleyModelInitialisation(); i.GlobalModelTimeStepUnit = configuration.GlobalModelTimeStepUnit; i.NumTimeSteps = (uint)configuration.NumTimeSteps; i.CellSize = (float)e.CellSize; i.BottomLatitude = (float)e.BottomLatitude; i.TopLatitude = (float)e.TopLatitude; i.LeftmostLongitude = (float)e.LeftmostLongitude; i.RightmostLongitude = (float)e.RightmostLongitude; i.PlanktonDispersalThreshold = outputSettings.PlanktonDispersalThreshold; i.InitialisationFileStrings = new SortedList <string, string>(); i.SpecificLocations = e.SpecificLocations; i.InitialisationFileStrings.Add("OutputDetail", outputSettings.InitialisationFileStrings["OutputDetail"]); i.CohortFunctionalGroupDefinitions = ConvertFunctionalGroupDefinitions(configuration.CohortFunctionalGroupDefinitions); i.StockFunctionalGroupDefinitions = ConvertFunctionalGroupDefinitions(configuration.StockFunctionalGroupDefinitions); i.EnviroStack = e.CellEnvironment.Select(env => new SortedList <string, double[]>(env)).ToArray(); i.CellList = e.FocusCells.Select(a => new UInt32[] { (UInt32)a.Item1, (UInt32)a.Item2 }).ToList(); i.OutputPath = outputSettings.OutputPath; i.TrackProcesses = outputSettings.TrackProcesses; i.TrackCrossCellProcesses = outputSettings.TrackCrossCellProcesses; i.TrackGlobalProcesses = outputSettings.TrackGlobalProcesses; i.ProcessTrackingOutputs = outputSettings.ProcessTrackingOutputs; i.ModelMassBins = outputSettings.ModelMassBins; i.LiveOutputs = outputSettings.LiveOutputs; i.TrackMarineSpecifics = outputSettings.TrackMarineSpecifics; i.OutputMetrics = outputSettings.OutputMetrics; i.OutputStateTimestep = outputSettings.OutputStateTimestep; return(i); }
public static void RunTraditional( Madingley.Common.Configuration configuration, Madingley.Common.Environment environment, Func <Madingley.Common.RunState, Madingley.Common.Configuration, Madingley.Common.Environment, Madingley.Common.ModelState, Madingley.Common.IOutput> factory) { var progress = new ProgressReporter(); var cancellationToken = CancellationToken.None; Run <Object>(null, configuration, environment, factory, progress, cancellationToken).Last(); }
public static IEnumerable <Tuple <Madingley.Common.RunState, ReturnType> > Run <ReturnType>( Madingley.Common.RunState state, Madingley.Common.Configuration configuration, Madingley.Common.Environment environment, Func <Madingley.Common.RunState, Madingley.Common.Configuration, Madingley.Common.Environment, Madingley.Common.ModelState, Madingley.Common.IOutput> factory, IProgress <double> progress, CancellationToken cancellation) { var startTimeStep = state != null ? (uint)state.ModelState.TimestepsComplete : (uint)0; var modelState = state != null ? state.ModelState : null; var madingleyModel = new MadingleyModel( modelState, configuration, environment); var beginRunModelStateData = madingleyModel.CreateModelStateData(startTimeStep); var output = factory.Invoke(state, configuration, environment, beginRunModelStateData); // Run the simulation return(madingleyModel.Initialise <ReturnType>(startTimeStep, output, progress, cancellation)); }
public static Madingley.Common.Environment Deserialize(TextReader sr) { Func<Newtonsoft.Json.JsonTextReader, Tuple<int, int>> JsonReadFocusCell = (reader) => { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartArray); reader.Read(); var item1 = Common.Reader.ReadInt(reader); reader.Read(); var item2 = Common.Reader.ReadInt(reader); reader.Read(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.EndArray); return Tuple.Create(item1, item2); }; Func<Newtonsoft.Json.JsonTextReader, IDictionary<string, double[]>> JsonReadCellEnvironment = (reader) => { var ret = new Dictionary<string, double[]>(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var key = Convert.ToString(reader.Value); reader.Read(); var value = Common.Reader.ReadArray(reader, Common.Reader.ReadDouble); ret.Add(key, value.ToArray()); } return ret; }; var environment = new Madingley.Common.Environment(); using (var reader = new Newtonsoft.Json.JsonTextReader(sr)) { reader.Read(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "CellSize": environment.CellSize = Common.Reader.ReadDouble(reader); break; case "BottomLatitude": environment.BottomLatitude = Common.Reader.ReadDouble(reader); break; case "TopLatitude": environment.TopLatitude = Common.Reader.ReadDouble(reader); break; case "LeftmostLongitude": environment.LeftmostLongitude = Common.Reader.ReadDouble(reader); break; case "RightmostLongitude": environment.RightmostLongitude = Common.Reader.ReadDouble(reader); break; case "Units": environment.Units = Common.Reader.ReadKeyValuePairs(reader, Common.Reader.ReadString); break; case "SpecificLocations": environment.SpecificLocations = Common.Reader.ReadBoolean(reader); break; case "FocusCells": environment.FocusCells = Common.Reader.ReadArray(reader, JsonReadFocusCell).ToList(); break; case "CellEnvironment": environment.CellEnvironment = Common.Reader.ReadArray(reader, JsonReadCellEnvironment).ToList(); break; case "FileNames": environment.FileNames = Common.Reader.ReadArray(reader, Common.Reader.ReadString).ToList(); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } } return environment; }
/// <summary> /// Reads the initalization file to get information for the set of simulations to be run /// </summary> /// <param name="simulationInitialisationFilename">The name of the initialization file with information on the simulations to be run</param> /// <param name="definitionsFilename">Definitions file name</param> /// <param name="environmentDataRoot">The path to folder which contains the data inputs</param> /// <param name="inputPath">The path to folder which contains the model inputs</param> public static Tuple <Madingley.Common.Environment, SortedList <string, EnviroData> > Load(string simulationInitialisationFilename, string definitionsFilename, string environmentDataRoot, string inputPath) { // Write to console Console.WriteLine("Initializing model...\n"); // Construct file names var simulationInitialisationFileName = System.IO.Path.Combine(inputPath, simulationInitialisationFilename); var definitionsFileName = System.IO.Path.Combine(inputPath, definitionsFilename); string SimulationFileString = "msds:csv?file=" + simulationInitialisationFileName + "&openMode=readOnly"; string DefinitionsFileString = "msds:csv?file=" + definitionsFileName + "&openMode=readOnly"; var e = new Madingley.Common.Environment(); e.FileNames.Add(simulationInitialisationFileName); e.FileNames.Add(definitionsFileName); var EnviroStack = new SortedList <string, EnviroData>(); // Read in the simulation data DataSet InternalData = DataSet.Open(SimulationFileString); // Get the names of parameters in the initialization file var VarParameters = InternalData.Variables[1].GetData(); // Get the values for the parameters var VarValues = InternalData.Variables[0].GetData(); // Loop over the parameters for (int row = 0; row < VarParameters.Length; row++) { // Switch based on the name of the parameter, and write the value to the appropriate field switch (VarParameters.GetValue(row).ToString().ToLower()) { case "grid cell size": e.CellSize = Convert.ToDouble(VarValues.GetValue(row)); break; case "bottom latitude": e.BottomLatitude = Convert.ToDouble(VarValues.GetValue(row)); break; case "top latitude": e.TopLatitude = Convert.ToDouble(VarValues.GetValue(row)); break; case "leftmost longitude": e.LeftmostLongitude = Convert.ToDouble(VarValues.GetValue(row)); break; case "rightmost longitude": e.RightmostLongitude = Convert.ToDouble(VarValues.GetValue(row)); break; case "specific location file": if (VarValues.GetValue(row).ToString() != "") { e.SpecificLocations = true; var specificLocationsFile = VarValues.GetValue(row).ToString(); var fileName = System.IO.Path.Combine(inputPath, "Initial Model State Setup", specificLocationsFile); e.FileNames.Add(fileName); ReadSpecificLocations(e, fileName); } break; } } InternalData.Dispose(); // Read in the definitions data InternalData = DataSet.Open(DefinitionsFileString); // Get the names of parameters in the initialization file VarParameters = InternalData.Variables[1].GetData(); // Get the values for the parameters VarValues = InternalData.Variables[0].GetData(); // Loop over the parameters for (int row = 0; row < VarParameters.Length; row++) { // Switch based on the name of the parameter, and write the value to the appropriate field switch (VarParameters.GetValue(row).ToString().ToLower()) { case "environmental data file": { // Read environmental data layers var environmentalLayerFile = VarValues.GetValue(row).ToString(); var fileName = System.IO.Path.Combine(inputPath, "Environmental Data Layer List", environmentalLayerFile); e.FileNames.Add(fileName); EnviroStack = ReadEnvironmentalLayers(e, fileName, environmentDataRoot); } break; } } InternalData.Dispose(); return(Tuple.Create(e, EnviroStack)); }
/// <summary> /// Reads the environmental layers listed in the specified file containing a list of environmental layers /// </summary> /// <param name="e">Environment object.</param> /// <param name="inputEnvironmentalLayerFileName">The name of the file containing the list of environmental layers</param> /// <param name="environmentDataRoot">The path to folder which contains the data inputs</param> public static SortedList <string, EnviroData> ReadEnvironmentalLayers(Madingley.Common.Environment e, string inputEnvironmentalLayerFileName, string environmentDataRoot) { Console.WriteLine("Reading in environmental data:"); // Declare lists to hold the information required to read the environmental layers List <string> Sources = new List <string>(); List <string> Folders = new List <string>(); List <string> Filenames = new List <string>(); List <string> DatasetNames = new List <string>(); List <string> FileTypes = new List <string>(); List <string> LayerName = new List <string>(); List <string> StaticLayer = new List <string>(); List <string> Extensions = new List <string>(); List <string> Resolutions = new List <string>(); List <string> MethodUnits = new List <string>(); // Variable to store the file name of the environmental data files string TempFilename; // Construct the full URI for the file containing the list of environmental layers string FileString = "msds:csv?file=" + inputEnvironmentalLayerFileName + "&openMode=readOnly"; StreamReader r_env = new StreamReader(inputEnvironmentalLayerFileName); var EnviroStack = new SortedList <string, EnviroData>(); string l; char[] comma = ",".ToCharArray(); string[] f; int col; // Read in the data l = r_env.ReadLine(); while (!r_env.EndOfStream) { l = r_env.ReadLine(); // Split fields by commas f = l.Split(comma); //zero the column index col = 0; // Lists of the different fields Sources.Add(f[col++]); Folders.Add(f[col++]); Filenames.Add(f[col++]); Extensions.Add(f[col++]); DatasetNames.Add(f[col++]); FileTypes.Add(f[col++]); LayerName.Add(f[col++]); StaticLayer.Add(f[col++]); Resolutions.Add(f[col++]); MethodUnits.Add(f[col++]); } for (int ii = 0; ii < MethodUnits.Count; ii++) { e.Units.Add(LayerName[ii], MethodUnits[ii]); } // Check that there are the same number of values for all parameters Debug.Assert(Folders.Count() == Filenames.Count() && Filenames.Count() == DatasetNames.Count() && DatasetNames.Count() == FileTypes.Count() && FileTypes.Count() == LayerName.Count(), "Error in Environmental Data Layer import lists - unequal number of filenames, dataset names, filetypes and datalayer names"); // Loop over parameter values for (int ii = 0; ii < Filenames.Count(); ii++) { Console.Write("\r{0} Variable {1} of {2}: {3}\n", Sources[ii], ii + 1, Filenames.Count, Filenames[ii]); // If the layers are not static, then suffix the file name with '1' - not currently implemented if (StaticLayer[ii].ToLower().Equals("n")) { Debug.Fail("This option is currently not supported"); Filenames[ii] = Filenames[ii] + "1"; } if (Sources[ii].ToLower().Equals("local")) { // For layers where the file format is ESRI ASCII grid, the dataset name is the same as the file name if (FileTypes[ii].ToLower().Equals("esriasciigrid")) { DatasetNames[ii] = Filenames[ii]; } // Generate the appropriate file name for the environmental data layer #if true TempFilename = System.IO.Path.Combine(environmentDataRoot, Folders[ii], Filenames[ii]); Filenames[ii] = System.IO.Path.ChangeExtension(TempFilename, Extensions[ii]); #else if (Folders[ii].ToLower().Equals("input")) { TempFilename = "input/Data/" + Filenames[ii]; } else { TempFilename = Folders[ii] + "/" + Filenames[ii]; } Filenames[ii] = TempFilename + Extensions[ii]; #endif // Read in and store the environmental data if (File.Exists(Filenames[ii])) { EnviroStack.Add(LayerName[ii], new EnviroData(Filenames[ii], DatasetNames[ii], FileTypes[ii], Resolutions[ii], MethodUnits[ii])); } else { throw new Exception(string.Format("ERROR: Cannot find data file: {0} ({1}), have you added the data files from https://github.com/Madingley/Madingley/releases?", Filenames[ii], System.IO.Path.GetFullPath(Filenames[ii]))); } } else if (Sources[ii].ToLower().Equals("fetchclimate")) { if (!EnviroStack.ContainsKey(LayerName[ii])) { if (e.SpecificLocations) { EnviroStack.Add(LayerName[ii], new EnviroData(DatasetNames[ii], Resolutions[ii], e.BottomLatitude, e.LeftmostLongitude, e.TopLatitude, e.RightmostLongitude, e.CellSize, e.FocusCells, EnvironmentalDataSource.ANY)); } else { EnviroStack.Add(LayerName[ii], new EnviroData(DatasetNames[ii], Resolutions[ii], e.BottomLatitude, e.LeftmostLongitude, e.TopLatitude, e.RightmostLongitude, e.CellSize, EnvironmentalDataSource.ANY)); } } } } return(EnviroStack); }
/// <summary> /// Read in the specified locations in which to run the model /// </summary> /// <param name="environment">Environment object</param> /// <param name="fileName">The name of the file with specific locations information</param> public static void ReadSpecificLocations(Madingley.Common.Environment environment, string fileName) { var CellList = new List <Tuple <int, int> >(); List <double> LatitudeList = new List <double>(); List <double> LongitudeList = new List <double>(); Console.WriteLine("Reading in specific location data"); Console.WriteLine(""); // construct file name string FileString = "msds:csv?file=" + fileName + "&openMode=readOnly"; // Read in the data DataSet InternalData = DataSet.Open(FileString); foreach (Variable v in InternalData.Variables) { //Get the name of the variable currently referenced in the dataset string HeaderName = v.Name; //Copy the values for this variable into an array var TempValues = v.GetData(); switch (HeaderName.ToLower()) { // Add the latitude and longitude values to the appropriate list case "latitude": for (int ii = 0; ii < TempValues.Length; ii++) { LatitudeList.Add(Convert.ToDouble(TempValues.GetValue(ii).ToString())); } break; case "longitude": for (int ii = 0; ii < TempValues.Length; ii++) { LongitudeList.Add(Convert.ToDouble(TempValues.GetValue(ii).ToString())); } break; default: Console.WriteLine("Variable defined in the specific location file but not processed: ", HeaderName); break; } } InternalData.Dispose(); // Loop over cells defined in the specific locations file for (int ii = 0; ii < LatitudeList.Count; ii++) { // Get the longitude and latitude indices for the current grid cell var latitudeIndex = (int)Math.Floor((LatitudeList.ElementAt(ii) - environment.BottomLatitude) / environment.CellSize); var longitudeIndex = (int)Math.Floor((LongitudeList.ElementAt(ii) - environment.LeftmostLongitude) / environment.CellSize); // Add these indices to the list of active cells CellList.Add(Tuple.Create(latitudeIndex, longitudeIndex)); } environment.FocusCells = CellList; }
/// <summary> /// Reads the initalization file to get information for the set of simulations to be run /// </summary> /// <param name="simulationInitialisationFilename">The name of the initialization file with information on the simulations to be run</param> /// <param name="definitionsFilename">Definitions file name</param> /// <param name="environmentDataRoot">The path to folder which contains the data inputs</param> /// <param name="inputPath">The path to folder which contains the model inputs</param> public static Tuple<Madingley.Common.Environment, SortedList<string, EnviroData>> Load( string simulationInitialisationFilename, string definitionsFilename, string environmentDataRoot, string inputPath, bool readData) { // Write to console Console.WriteLine("Initializing model...\n"); // Construct file names var simulationInitialisationFileName = System.IO.Path.Combine(inputPath, simulationInitialisationFilename); var definitionsFileName = System.IO.Path.Combine(inputPath, definitionsFilename); string SimulationFileString = "msds:csv?file=" + simulationInitialisationFileName + "&openMode=readOnly"; string DefinitionsFileString = "msds:csv?file=" + definitionsFileName + "&openMode=readOnly"; var e = new Madingley.Common.Environment(); e.FileNames.Add(simulationInitialisationFileName); e.FileNames.Add(definitionsFileName); var EnviroStack = new SortedList<string, EnviroData>(); // Read in the simulation data DataSet InternalData = DataSet.Open(SimulationFileString); // Get the names of parameters in the initialization file var VarParameters = InternalData.Variables[1].GetData(); // Get the values for the parameters var VarValues = InternalData.Variables[0].GetData(); // Loop over the parameters for (int row = 0; row < VarParameters.Length; row++) { // Switch based on the name of the parameter, and write the value to the appropriate field switch (VarParameters.GetValue(row).ToString().ToLower()) { case "grid cell size": e.CellSize = Convert.ToDouble(VarValues.GetValue(row)); break; case "bottom latitude": e.BottomLatitude = Convert.ToDouble(VarValues.GetValue(row)); break; case "top latitude": e.TopLatitude = Convert.ToDouble(VarValues.GetValue(row)); break; case "leftmost longitude": e.LeftmostLongitude = Convert.ToDouble(VarValues.GetValue(row)); break; case "rightmost longitude": e.RightmostLongitude = Convert.ToDouble(VarValues.GetValue(row)); break; case "specific location file": if (VarValues.GetValue(row).ToString() != "") { e.SpecificLocations = true; var specificLocationsFile = VarValues.GetValue(row).ToString(); var fileName = System.IO.Path.Combine(inputPath, "Initial Model State Setup", specificLocationsFile); e.FileNames.Add(fileName); ReadSpecificLocations(e, fileName); } break; } } InternalData.Dispose(); // Read in the definitions data InternalData = DataSet.Open(DefinitionsFileString); // Get the names of parameters in the initialization file VarParameters = InternalData.Variables[1].GetData(); // Get the values for the parameters VarValues = InternalData.Variables[0].GetData(); // Loop over the parameters for (int row = 0; row < VarParameters.Length; row++) { // Switch based on the name of the parameter, and write the value to the appropriate field switch (VarParameters.GetValue(row).ToString().ToLower()) { case "environmental data file": { // Read environmental data layers var environmentalLayerFile = VarValues.GetValue(row).ToString(); var fileName = System.IO.Path.Combine(inputPath, "Environmental Data Layer List", environmentalLayerFile); e.FileNames.Add(fileName); EnviroStack = ReadEnvironmentalLayers(e, fileName, environmentDataRoot, readData); } break; } } InternalData.Dispose(); return Tuple.Create(e, EnviroStack); }
public static Madingley.Common.Environment Deserialize(TextReader sr) { Func <Newtonsoft.Json.JsonTextReader, Tuple <int, int> > JsonReadFocusCell = (reader) => { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartArray); reader.Read(); var item1 = Common.Reader.ReadInt(reader); reader.Read(); var item2 = Common.Reader.ReadInt(reader); reader.Read(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.EndArray); return(Tuple.Create(item1, item2)); }; Func <Newtonsoft.Json.JsonTextReader, IDictionary <string, double[]> > JsonReadCellEnvironment = (reader) => { var ret = new Dictionary <string, double[]>(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var key = Convert.ToString(reader.Value); reader.Read(); var value = Common.Reader.ReadArray(reader, Common.Reader.ReadDouble); ret.Add(key, value.ToArray()); } return(ret); }; var environment = new Madingley.Common.Environment(); using (var reader = new Newtonsoft.Json.JsonTextReader(sr)) { reader.Read(); Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.StartObject); while (reader.Read() && reader.TokenType != Newtonsoft.Json.JsonToken.EndObject) { Debug.Assert(reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName); Debug.Assert(reader.ValueType == typeof(string)); var property = Convert.ToString(reader.Value); reader.Read(); switch (property) { case "CellSize": environment.CellSize = Common.Reader.ReadDouble(reader); break; case "BottomLatitude": environment.BottomLatitude = Common.Reader.ReadDouble(reader); break; case "TopLatitude": environment.TopLatitude = Common.Reader.ReadDouble(reader); break; case "LeftmostLongitude": environment.LeftmostLongitude = Common.Reader.ReadDouble(reader); break; case "RightmostLongitude": environment.RightmostLongitude = Common.Reader.ReadDouble(reader); break; case "Units": environment.Units = Common.Reader.ReadKeyValuePairs(reader, Common.Reader.ReadString); break; case "SpecificLocations": environment.SpecificLocations = Common.Reader.ReadBoolean(reader); break; case "FocusCells": environment.FocusCells = Common.Reader.ReadArray(reader, JsonReadFocusCell).ToList(); break; case "CellEnvironment": environment.CellEnvironment = Common.Reader.ReadArray(reader, JsonReadCellEnvironment).ToList(); break; case "FileNames": environment.FileNames = Common.Reader.ReadArray(reader, Common.Reader.ReadString).ToList(); break; default: throw new Exception(string.Format("Unexpected property: {0}", property)); } } } return(environment); }
public static Madingley.Common.IOutput Create( Madingley.Common.RunState state, Madingley.Common.Configuration configuration, Madingley.Common.Environment environment, Madingley.Common.ModelState modelState) { // Specify the working directory string OutputDir = "Parameters"; OutputDir += System.DateTime.Now.Year + "-" + System.DateTime.Now.Month + "-" + System.DateTime.Now.Day + "_" + System.DateTime.Now.Hour + "." + System.DateTime.Now.Minute + "." + System.DateTime.Now.Second + "/"; // Create the working directory if this does not already exist System.IO.Directory.CreateDirectory(OutputDir); foreach (var sourceFileName in configuration.FileNames) { var fileName = System.IO.Path.GetFileName(sourceFileName); var destFileName = System.IO.Path.Combine(OutputDir, fileName); System.IO.File.Copy(sourceFileName, destFileName, true); } foreach (var sourceFileName in environment.FileNames) { var fileName = System.IO.Path.GetFileName(sourceFileName); var destFileName = System.IO.Path.Combine(OutputDir, fileName); System.IO.File.Copy(sourceFileName, destFileName, true); } // Set up the suffix for the output files var OutputFilesSuffix = "_"; // Add the scenario label to the suffix for the output files OutputFilesSuffix += configuration.ScenarioParameters[configuration.ScenarioIndex].Label + "_"; // Add the simulation index number to the suffix OutputFilesSuffix += configuration.Simulation.ToString(); var i = new MadingleyModelInitialisation(simulationInitialisationFile, definitionsFilename, outputsFilename, outputPath, inputPath); i.OutputPath = OutputDir; var output = new Madingley.Output.MadingleyModelOutput( OutputFilesSuffix, environment, configuration, i, modelState); if (state != null) { var existing = (Madingley.Output.MadingleyModelOutput)state.Output; output.model.Copy(existing.model); } return(output); }