Skip to content

Commit

Permalink
Adapter: added remaining object serialisation (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraser Greenroyd authored Nov 28, 2023
2 parents 08828e0 + 385547e commit 5fb194e
Show file tree
Hide file tree
Showing 19 changed files with 748 additions and 30 deletions.
7 changes: 5 additions & 2 deletions LadybugTools_Adapter/CRUD/CreateLadybug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ public static void CreateLadybug(List<ILadybugTools> objects, LadybugConfig conf
{
jsonObjects.Add(lbtObject.FromBHoM());
}

string json = "[" + string.Join(", ", jsonObjects) + "]";
string json = "{}";
if (jsonObjects.Count > 1)
json = $"[{string.Join(", ", jsonObjects)}]";
else if (jsonObjects.Count == 1)
json = jsonObjects[0];
File.WriteAllText(config.JsonFile.GetFullFileName(), json);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public static string FromHourlyContinuousCollection(BH.oM.LadybugTools.HourlyCon
valuesAsString = "\"" + string.Join("\", \"", collection.Values) + "\"";

string type = @"""type"" : ""HourlyContinuous""";
string values = @"""values"" : [ " + valuesAsString + " ]";
string header = @"""header"" : " + FromHeader(collection.Header).ToJson();
string values = $@"""values"" : [{valuesAsString}]";
string header = $@"""header"" : {FromHeader(collection.Header).ToJson()}";

return "{ " + type + ", " + values + ", " + header + " }";
}
Expand Down
20 changes: 20 additions & 0 deletions LadybugTools_Adapter/Convert/FromBHoM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ private static string Jsonify(this EPW epw)
return FromEPW(epw);
}

private static string Jsonify(this ExternalComfort externalComfort)
{
return FromExternalComfort(externalComfort);
}

private static string Jsonify(this Header header)
{
return FromHeader(header).ToJson();
Expand All @@ -82,6 +87,21 @@ private static string Jsonify(this Location location)
return FromLocation(location).ToJson();
}

private static string Jsonify(this Shelter shelter)
{
return FromShelter(shelter);
}

private static string Jsonify(this SimulationResult simulationResult)
{
return FromSimulationResult(simulationResult);
}

private static string Jsonify(this Typology typology)
{
return FromTypology(typology);
}

private static Dictionary<string, object> Jsonify(this ILadybugTools obj)
{
BH.Engine.Base.Compute.RecordError($"The type: {obj.GetType()} is not convertible to ladybug serialisable json yet.");
Expand Down
81 changes: 81 additions & 0 deletions LadybugTools_Adapter/Convert/Geometry/Point.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using BH.oM.LadybugTools;
using BH.oM.Geometry;

namespace BH.Adapter.LadybugTools
{
public static partial class Convert
{
public static Point ToPoint(Dictionary<string, object> oldObject)
{
double x = 0.0;
double y = 0.0;
double z = 0.0;

try
{
x = (double)oldObject["x"];
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError($"An error occurred when reading x of the Point. returning x as default ({x}).\n The error: {ex}");
}

try
{
y = (double)oldObject["y"];
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError($"An error occurred when reading y of the Point. returning y as default ({y}).\n The error: {ex}");
}

try
{
z = (double)oldObject["z"];
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError($"An error occurred when reading z of the Point. returning z as default ({z}).\n The error: {ex}");
}

return new Point()
{
X = x,
Y = y,
Z = z
};
}

public static string FromPoint(Point point)
{
string type = @"""Point3D""";
string xyz = $@"""x"" : {point.X}, ""y"" : {point.Y}, ""z"" : {point.Z}";
return @"{""type"" : " + type + ", " + xyz + "}";
}
}
}
112 changes: 112 additions & 0 deletions LadybugTools_Adapter/Convert/Geometry/Shelter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using BH.Engine.Serialiser;
using BH.oM.Base;
using BH.oM.Geometry;
using BH.oM.LadybugTools;

namespace BH.Adapter.LadybugTools
{
public static partial class Convert
{
public static oM.LadybugTools.Shelter ToShelter(Dictionary<string, object> oldObject)
{
List<Point> points = new List<Point>();
List<double> radiationPorosity = Enumerable.Repeat(0.0, 8760).ToList();
List<double> windPorosity = Enumerable.Repeat(0.0, 8760).ToList();
if (oldObject.ContainsKey("vertices"))
{
foreach (var vertex in oldObject["vertices"] as List<object>)
{
Dictionary<string, object> point;
if (vertex.GetType() == typeof(CustomObject))
{
point = (vertex as CustomObject).CustomData;
}
else
{
point = vertex as Dictionary<string, object>;
}

points.Add(ToPoint(point));
}
}

try
{
List<double> values = new List<double>();
foreach (object value in oldObject["radiation_porosity"] as List<object>)
{
values.Add(double.Parse(value.ToString()));
}
radiationPorosity = values;
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the radiation porosity of the Shelter. returning as default (List of 0s of length 8760).\n The error: {ex}");
}

try
{
List<double> values = new List<double>();
foreach (object value in oldObject["wind_porosity"] as List<object>)
{
values.Add(double.Parse(value.ToString()));
}
windPorosity = values;
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the wind porosity of the Shelter. returning as default (List of 0s of length 8760).\n The error: {ex}");
}

return new oM.LadybugTools.Shelter()
{
Vertices = points,
RadiationPorosity = radiationPorosity,
WindPorosity = windPorosity

};
}

public static string FromShelter(oM.LadybugTools.Shelter shelter)
{
string radiationPorosity = $@"""radiation_porosity"": [{string.Join(", ", shelter.RadiationPorosity)}]";
string windPorosity = $@"""wind_porosity"": [{string.Join(", ", shelter.WindPorosity)}]";

List<string> points = new List<string>();
foreach (Point point in shelter.Vertices)
{
points.Add(FromPoint(point));
}
string vertices = $@"""vertices"": [{string.Join(", ", points)}]";

return @"{ ""type"": ""Shelter""," + vertices + ", " + radiationPorosity + ", " + windPorosity + "}";
}
}
}
134 changes: 134 additions & 0 deletions LadybugTools_Adapter/Convert/Simulation/ExternalComfort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Base;
using BH.oM.LadybugTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BH.Adapter.LadybugTools
{
public static partial class Convert
{
public static ExternalComfort ToExternalComfort(Dictionary<string, object> oldObject)
{
SimulationResult simulationResult = new SimulationResult();
Typology typology = new Typology();
List<HourlyContinuousCollection> simulatedProperties = new List<HourlyContinuousCollection>();

List<string> properties = new List<string>()
{
"dry_bulb_temperature",
"relative_humidity",
"wind_speed",
"mean_radiant_temperature",
"universal_thermal_climate_index"
};

try
{
if (oldObject["simulation_result"].GetType() == typeof(CustomObject))
oldObject["simulation_result"] = ((CustomObject)oldObject["simulation_result"]).CustomData;
simulationResult = ToSimulationResult(oldObject["simulation_result"] as Dictionary<string, object>);
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the SimulationResult of the ExternalComfort. Returning a default SimulationResult. \n The error: {ex}");
}

try
{
if (oldObject["typology"].GetType() == typeof(CustomObject))
oldObject["typology"] = ((CustomObject)oldObject["typology"]).CustomData;
typology = ToTypology(oldObject["typology"] as Dictionary<string, object>);
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the Typology of the ExternalComfort. Returning a default Typology. \n The error: {ex}");
}

foreach (string property in properties)
{
if (oldObject.ContainsKey(property))
{
try
{
if (oldObject[property].GetType() == typeof(CustomObject))
oldObject[property] = ((CustomObject)oldObject[property]).CustomData;
simulatedProperties.Add(ToHourlyContinuousCollection(oldObject[property] as Dictionary<string, object>));
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError($"An error occurred while parsing the collection {property} of the ExternalComfort. Returning an empty collection in its place.\n The error: {ex}");
simulatedProperties.Add(new HourlyContinuousCollection() { Values = Enumerable.Repeat<string>(null, 8760).ToList() });
}
}
else
{
BH.Engine.Base.Compute.RecordError($"The incoming json for ExternalComfort does not contain the key: {property}. Returning an empty collection in its place.");
simulatedProperties.Add(new HourlyContinuousCollection() { Values = Enumerable.Repeat<string>(null, 8760).ToList() });
}
}

return new ExternalComfort()
{
SimulationResult = simulationResult,
Typology = typology,
DryBulbTemperature = simulatedProperties[0],
RelativeHumidity = simulatedProperties[1],
WindSpeed = simulatedProperties[2],
MeanRadiantTemperature = simulatedProperties[3],
UniversalThermalClimateIndex = simulatedProperties[4]
};
}

public static string FromExternalComfort(ExternalComfort externalComfort)
{
string type = "\"type\": \"ExternalComfort\", ";
string simulationResult = $"\"simulation_result\": {FromSimulationResult(externalComfort.SimulationResult)}, ";
string typology = $"\"typology\": {FromTypology(externalComfort.Typology)}";
List<string> properties = new List<string>();

if (externalComfort.DryBulbTemperature != null)
properties.Add("\"dry_bulb_temperature\": " + FromHourlyContinuousCollection(externalComfort.DryBulbTemperature));

if (externalComfort.RelativeHumidity != null)
properties.Add("\"relative_humidity\": " + FromHourlyContinuousCollection(externalComfort.RelativeHumidity));

if (externalComfort.WindSpeed != null)
properties.Add("\"wind_speed\": " + FromHourlyContinuousCollection(externalComfort.WindSpeed));

if (externalComfort.MeanRadiantTemperature != null)
properties.Add("\"mean_radiant_temperature\": " + FromHourlyContinuousCollection(externalComfort.MeanRadiantTemperature));

if (externalComfort.UniversalThermalClimateIndex != null)
properties.Add("\"universal_thermal_climate_index\": " + FromHourlyContinuousCollection(externalComfort.UniversalThermalClimateIndex));

if (properties.Count > 0)
properties[0] = ", " + properties[0];
string simulatedProperties = string.Join(", ", properties);
return "{" + type + simulationResult + typology + simulatedProperties + "}";
}
}
}
Loading

0 comments on commit 5fb194e

Please sign in to comment.