Skip to content

Commit

Permalink
Return metadata for underlying collections when generating plots (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesramsden-bh authored Aug 1, 2024
2 parents 2aa23d9 + 8cbd052 commit 85dc718
Show file tree
Hide file tree
Showing 23 changed files with 1,153 additions and 50 deletions.
102 changes: 81 additions & 21 deletions LadybugTools_Adapter/AdapterActions/Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
using BH.Engine.Base;
using System.Drawing;
using BH.Engine.Serialiser;
using BH.Engine.LadyBugTools;
using System.Reflection;

namespace BH.Adapter.LadybugTools
Expand Down Expand Up @@ -312,12 +311,25 @@ private List<object> RunCommand(HeatPlotCommand command, ActionConfig actionConf
if (colourMap.ColourMapValidity())
colourMap = colourMap.ToColourMap().FromColourMap();

string returnFile = Path.GetTempFileName();

// run the process
string cmdCommand = $"{m_environment.Executable} {script} -e \"{epwFile}\" -dtk \"{command.EPWKey.ToText()}\" -cmap \"{colourMap}\" -p \"{command.OutputLocation}\"";
string cmdCommand = $"{m_environment.Executable} {script} -e \"{epwFile}\" -dtk \"{command.EPWKey.ToText()}\" -cmap \"{colourMap}\" -r \"{returnFile.Replace('\\', '/')}\" -p \"{command.OutputLocation}\"";
string result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);

if (!File.Exists(result))
{
BH.Engine.Base.Compute.RecordError($"An error occurred while running the command: {result}");
File.Delete(returnFile);
return new List<object>();
}

CustomObject obj = (CustomObject)BH.Engine.Serialiser.Convert.FromJson(System.IO.File.ReadAllText(returnFile));
File.Delete(returnFile);
PlotInformation info = Convert.ToPlotInformation(obj, new CollectionData());

m_executeSuccess = true;
return new List<object>() { result };
return new List<object>() { info };
}

/**************************************************/
Expand Down Expand Up @@ -351,12 +363,25 @@ private List<object> RunCommand(WindroseCommand command, ActionConfig actionConf
if (colourMap.ColourMapValidity())
colourMap = colourMap.ToColourMap().FromColourMap();

string returnFile = Path.GetTempFileName();

// run the process
string cmdCommand = $"{m_environment.Executable} {script} -e \"{epwFile}\" -ap \"{command.AnalysisPeriod.FromBHoM().Replace("\"", "\\\"")}\" -cmap \"{colourMap}\" -bins \"{command.NumberOfDirectionBins}\" -p \"{command.OutputLocation}\"";
string cmdCommand = $"{m_environment.Executable} {script} -e \"{epwFile}\" -ap \"{command.AnalysisPeriod.FromBHoM().Replace("\"", "\\\"")}\" -cmap \"{colourMap}\" -bins \"{command.NumberOfDirectionBins}\" -r \"{returnFile.Replace('\\', '/')}\" -p \"{command.OutputLocation}\"";
string result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);

if (!File.Exists(result))
{
BH.Engine.Base.Compute.RecordError($"An error occurred while running the command: {result}");
File.Delete(returnFile);
return new List<object>();
}

CustomObject obj = (CustomObject)BH.Engine.Serialiser.Convert.FromJson(System.IO.File.ReadAllText(returnFile));
File.Delete(returnFile);
PlotInformation info = Convert.ToPlotInformation(obj, new WindroseData());

m_executeSuccess = true;
return new List<object> { result };
return new List<object> { info };
}

/**************************************************/
Expand Down Expand Up @@ -397,6 +422,7 @@ private List<object> RunCommand(UTCIHeatPlotCommand command, ActionConfig action
BH.Engine.Base.Compute.RecordError($"When overriding bin colours 10 colours must be provided, but {command.BinColours.Count} colours were provided instead.");
return null;
}

List<string> colours = command.BinColours.Select(x => x.ToHexCode()).ToList();

string hexColours = $"[\"{string.Join("\",\"", colours)}\"]";
Expand All @@ -418,25 +444,29 @@ private List<object> RunCommand(UTCIHeatPlotCommand command, ActionConfig action

string script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped\\plot", "utci_heatmap.py");

string returnFile = Path.GetTempFileName();

// run the process
string cmdCommand = $"{m_environment.Executable} \"{script}\" -e \"{epwFile}\" -in \"{argFile}\" -ws \"{command.WindSpeedMultiplier}\" -sp \"{command.OutputLocation}\"";
string result = "";
string cmdCommand = $"{m_environment.Executable} \"{script}\" -e \"{epwFile}\" -in \"{argFile}\" -ws \"{command.WindSpeedMultiplier}\" -r \"{returnFile.Replace('\\', '/')}\" -sp \"{command.OutputLocation}\"";
string result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);

try
{
result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError(ex, "An error occurred while running some python.");
}
finally
string resultFile = result.Split('\n').Last();

if (!File.Exists(resultFile))
{
BH.Engine.Base.Compute.RecordError($"An error occurred while running the command: {result}");
File.Delete(returnFile);
File.Delete(argFile);
return new List<object>();
}

CustomObject obj = (CustomObject)BH.Engine.Serialiser.Convert.FromJson(System.IO.File.ReadAllText(returnFile));
File.Delete(returnFile);
File.Delete(argFile);
PlotInformation info = Convert.ToPlotInformation(obj, new UTCIData());

m_executeSuccess = true;
return new List<object> { result.Split('\n').Last() };
return new List<object> { info };
}

/**************************************************/
Expand Down Expand Up @@ -473,12 +503,27 @@ private List<object> RunCommand(DiurnalPlotCommand command, ActionConfig actionC

string script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped\\plot", "diurnal.py");

string returnFile = Path.GetTempFileName();

// run the process
string cmdCommand = $"{m_environment.Executable} {script} -e \"{epwFile}\" -dtk \"{command.EPWKey.ToText()}\" -c \"{command.Colour.ToHexCode()}\" -t \"{command.Title}\" -ap \"{command.Period.ToString().ToLower()}\" -p \"{command.OutputLocation}\"";
string cmdCommand = $"{m_environment.Executable} {script} -e \"{epwFile}\" -dtk \"{command.EPWKey.ToText()}\" -c \"{command.Colour.ToHexCode()}\" -t \"{command.Title}\" -ap \"{command.Period.ToString().ToLower()}\" -r \"{returnFile.Replace('\\', '/')}\" -p \"{command.OutputLocation}\"";
string result = Engine.Python.Compute.RunCommandStdout(command: cmdCommand, hideWindows: true);

string resultFile = result.Split('\n').Last();

if (!File.Exists(resultFile))
{
BH.Engine.Base.Compute.RecordError($"An error occurred while running the command: {result}");
File.Delete(returnFile);
return new List<object>();
}

CustomObject obj = (CustomObject)BH.Engine.Serialiser.Convert.FromJson(System.IO.File.ReadAllText(returnFile));
File.Delete(returnFile);
PlotInformation info = Convert.ToPlotInformation(obj, new CollectionData());

m_executeSuccess = true;
return new List<object>() { result.Split('\n').Last() };
return new List<object>() { info };
}

/**************************************************/
Expand Down Expand Up @@ -513,12 +558,27 @@ private List<object> RunCommand(SunPathPlotCommand command, ActionConfig actionC

string script = Path.Combine(Engine.LadybugTools.Query.PythonCodeDirectory(), "LadybugTools_Toolkit\\src\\ladybugtools_toolkit\\bhom\\wrapped\\plot", "sunpath.py");

string returnFile = Path.GetTempFileName();

//run the process
string cmdCommand = $"{m_environment.Executable} {script} -e \"{epwFile}\" -s {command.SunSize} -ap \"{command.AnalysisPeriod.FromBHoM().Replace("\"", "\\\"")}\" -p \"{command.OutputLocation}\"";
string cmdCommand = $"{m_environment.Executable} {script} -e \"{epwFile}\" -s {command.SunSize} -ap \"{command.AnalysisPeriod.FromBHoM().Replace("\"", "\\\"")}\" -r \"{returnFile.Replace('\\', '/')}\" -p \"{command.OutputLocation}\"";
string result = Engine.Python.Compute.RunCommandStdout(cmdCommand, hideWindows: true);

string resultFile = result.Split('\n').Last();

if (!File.Exists(resultFile))
{
BH.Engine.Base.Compute.RecordError($"An error occurred while running the command: {result}");
File.Delete(returnFile);
return new List<object>();
}

CustomObject obj = (CustomObject)BH.Engine.Serialiser.Convert.FromJson(System.IO.File.ReadAllText(returnFile));
File.Delete(returnFile);
PlotInformation info = Convert.ToPlotInformation(obj, new SunPathData());

m_executeSuccess = true;
return new List<object>() { result.Split('\n').Last() };
return new List<object>() { info };
}

/**************************************************/
Expand Down
Loading

0 comments on commit 85dc718

Please sign in to comment.