示例#1
0
 public static GridCell ConvertGridCell(Madingley.Common.GridCell c)
 {
     return(new GridCell(
                ConvertGridCellCohorts(c.Cohorts),
                ConvertGridCellStocks(c.Stocks),
                new SortedList <string, double[]>(c.Environment),
                (float)c.Latitude,
                (float)c.Longitude));
 }
示例#2
0
        public static Madingley.Common.GridCell[] ConvertGrid(Func <uint, uint, GridCell> gridCells, List <uint[]> cellList)
        {
            var gridCellData = new Madingley.Common.GridCell[cellList.Count];

            for (var ii = 0; ii < cellList.Count; ii++)
            {
                var cell = gridCells(cellList[ii][0], cellList[ii][1]);

                gridCellData[ii] = ConvertCellData(cell);
            }

            return(gridCellData);
        }
示例#3
0
        public static Madingley.Common.ModelState Deserialize(TextReader sr)
        {
            Func<Newtonsoft.Json.JsonTextReader, Madingley.Common.Cohort> JsonReadCohort = (reader) =>
            {
                var ret = new Madingley.Common.Cohort();

                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 "FunctionalGroupIndex": ret.FunctionalGroupIndex = Common.Reader.ReadInt(reader); break;
                        case "BirthTimeStep": ret.BirthTimeStep = Common.Reader.ReadInt(reader); break;
                        case "MaturityTimeStep": ret.MaturityTimeStep = Common.Reader.ReadInt(reader); break;
                        case "IDs": ret.IDs = Common.Reader.ReadArray(reader, Common.Reader.ReadInt).ToList(); break;
                        case "JuvenileMass": ret.JuvenileMass = Common.Reader.ReadDouble(reader); break;
                        case "AdultMass": ret.AdultMass = Common.Reader.ReadDouble(reader); break;
                        case "IndividualBodyMass": ret.IndividualBodyMass = Common.Reader.ReadDouble(reader); break;
                        case "IndividualReproductivePotentialMass": ret.IndividualReproductivePotentialMass = Common.Reader.ReadDouble(reader); break;
                        case "MaximumAchievedBodyMass": ret.MaximumAchievedBodyMass = Common.Reader.ReadDouble(reader); break;
                        case "Abundance": ret.Abundance = Common.Reader.ReadDouble(reader); break;
                        case "Merged": ret.Merged = Common.Reader.ReadBoolean(reader); break;
                        case "ProportionTimeActive": ret.ProportionTimeActive = Common.Reader.ReadDouble(reader); break;
                        case "TrophicIndex": ret.TrophicIndex = Common.Reader.ReadDouble(reader); break;
                        case "LogOptimalPreyBodySizeRatio": ret.LogOptimalPreyBodySizeRatio = Common.Reader.ReadDouble(reader); break;
                        default: throw new Exception(string.Format("Unexpected property: {0}", property));
                    }
                }

                return ret;
            };

            Func<Newtonsoft.Json.JsonTextReader, Madingley.Common.Stock> JsonReadStock = (reader) =>
            {
                var ret = new Madingley.Common.Stock();

                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 "FunctionalGroupIndex": ret.FunctionalGroupIndex = Common.Reader.ReadInt(reader); break;
                        case "IndividualBodyMass": ret.IndividualBodyMass = Common.Reader.ReadDouble(reader); break;
                        case "TotalBiomass": ret.TotalBiomass = Common.Reader.ReadDouble(reader); break;
                        default: throw new Exception(string.Format("Unexpected property: {0}", property));
                    }
                }

                return ret;
            };

            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;
            };

            Func<Newtonsoft.Json.JsonTextReader, Madingley.Common.GridCell> JsonReadGridCell = (reader) =>
            {
                var ret = new Madingley.Common.GridCell();

                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 "Latitude": ret.Latitude = Common.Reader.ReadDouble(reader); break;
                        case "Longitude": ret.Longitude = Common.Reader.ReadDouble(reader); break;
                        case "Cohorts": ret.Cohorts = Common.Reader.ReadArray(reader, r => Common.Reader.ReadArray(r, JsonReadCohort)).ToList(); break;
                        case "Stocks": ret.Stocks = Common.Reader.ReadArray(reader, r => Common.Reader.ReadArray(r, JsonReadStock)).ToList(); break;
                        case "Environment": ret.Environment = JsonReadCellEnvironment(reader); break;
                        default: throw new Exception(string.Format("Unexpected property: {0}", property));
                    }
                }

                return ret;
            };

            var modelState = new Madingley.Common.ModelState();

            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 "TimestepsComplete": modelState.TimestepsComplete = Common.Reader.ReadInt(reader); break;
                        case "GlobalDiagnosticVariables": modelState.GlobalDiagnosticVariables = Common.Reader.ReadKeyValuePairs(reader, Common.Reader.ReadDouble); break;
                        case "GridCells": modelState.GridCells = Common.Reader.ReadArray(reader, JsonReadGridCell).ToList(); break;
                        case "NextCohortID": modelState.NextCohortID = Common.Reader.ReadLong(reader); break;
                        default: throw new Exception(string.Format("Unexpected property: {0}", property));
                    }
                }
            }

            return modelState;
        }
        public static Madingley.Common.GridCell[] ConvertGrid(Func<uint, uint, GridCell> gridCells, List<uint[]> cellList)
        {
            var gridCellData = new Madingley.Common.GridCell[cellList.Count];

            for (var ii = 0; ii < cellList.Count; ii++)
            {
                var cell = gridCells(cellList[ii][0], cellList[ii][1]);

                gridCellData[ii] = ConvertCellData(cell);
            }

            return gridCellData;
        }