open ddy file in Grasshopper c#

DDY Design Day weather file reader in Grasshopper C#

How to open a DDY (Design Day) file in Grasshopper.

Design day files are used for performing energy calculations in building design during represenative climatic extremes in summer and winter. Importing weather files into Grasshopper can be useful for energy analyses in Ladybug/Honeybee.

Download

Download the Grasshopper file here, which contains a C# component which you can run or edit. The code itself is pasted below, which could be used for any C# application.

You will also need your own DDY weather files. You can download them from here. Find the file you are interested in. Once you have downloaded the ZIP file, there should be a DDY file inside it.

C# code

  private void RunScript(string path, int y, ref object A)
  {

    int counter = 0;
    string ln;
    List<string> x = new List<string>();

    // Read the file
    System.IO.StreamReader f = new System.IO.StreamReader(path);
    while((ln = f.ReadLine()) != null)
    {
      x.Add(ln);
      counter++;
    }

    f.Close();



    var rtntree = new DataTree<String>();

    //strip comments
    for(int i = 0; i < x.Count; i++)
    {
      int ind = x[i].IndexOf('!');
      try{x[i] = x[i].Substring(0, ind);}catch{}
    }

    //reconstruct as one line
    string wholefile = "";
    foreach (string line in x) wholefile += line;

    //split into sections and fields
    List<List<string>> file = new List<List<string>>();

    string[] splitfile = wholefile.Split(';');
    foreach(string line in splitfile)
    {
      var section = new List<string>();

      string[] splitline = line.Split(',');
      foreach (string item in splitline)
      {
        section.Add(RemoveWhitespace(item));
      }
      file.Add(section);
    }

    //parse file
    var ddfile = new DDYFile();

    foreach (var s in file)
    {
      if(s[0] == "Site:Location")
      {
        ddfile.Site_Location.SiteName = s[1];
        ddfile.Site_Location.Latitude = Convert.ToDouble(s[2]);
        ddfile.Site_Location.Longitude = Convert.ToDouble(s[3]);
        ddfile.Site_Location.Gmt = Convert.ToDouble(s[4]);
        ddfile.Site_Location.Elevation = Convert.ToDouble(s[5]);
      }
      else if (s[0] == "SizingPeriod:DesignDay")
      {
        var dd = new DesignDay();
        dd.Name = s[1];
        dd.Month = TryToInt(s[2]);
        dd.DayOfMonth = TryToInt(s[3]);
        dd.DayType = s[4];
        dd.MaxTdb = TryToDouble(s[5]);
        dd.DailyTdbRange = TryToDouble(s[6]);
        dd.DailyTdbModifierType = s[7];
        dd.DailyTdbModifiedSchedule = s[8];
        dd.HumidityType = s[9];
        dd.TwbAtMaxTdb = TryToDouble(s[10]);
        dd.HumidityIndicatingDayScheduleName = s[11];
        dd.HumidityRatio = TryToDouble(s[12]);
        dd.Enthalpy = TryToDouble(s[13]);
        dd.DailyTwbRange = TryToDouble(s[14]);
        dd.BarometricPressure = TryToDouble(s[15]);
        dd.WindSpeed = TryToDouble(s[16]);
        dd.WindDirection = TryToDouble(s[17]);
        dd.Raining = TryToBool(s[18]);
        dd.Snowy = TryToBool(s[19]);
        dd.Dst = TryToBool(s[20]);
        dd.SolarModelIndicator = s[21];
        dd.BeamSolarDayScheduleName = s[22];
        dd.DiffuseSolarDayScheduleName = s[23];
        dd.OpticalDepthBeam = TryToDouble(s[24]);
        dd.OpticalDepthDiffuse = TryToDouble(s[25]);
        if(s.Count >= 27) dd.Clearness = TryToDouble(s[26]);

        ddfile.Design_Days.Add(dd);
      }
    }

    A = ddfile.Design_Days[y].ToListString();


  }

  // <Custom additional code> 

  public string RemoveWhitespace(string input)
  {
    return new string(input.ToCharArray()
      .Where(c => !Char.IsWhiteSpace(c))
      .ToArray());
  }

  public double TryToDouble(string input)
  {
    try
    {
      return Convert.ToDouble(input);
    }
    catch
    {
      return double.MinValue;
    }
  }

  public int TryToInt(string input)
  {
    try
    {
      return Convert.ToInt32(input);
    }
    catch
    {
      return Int32.MinValue;
    }
  }

  public bool TryToBool(string input)
  {
    try
    {
      return Convert.ToBoolean(input);
    }
    catch
    {
      return false; //should we always return false? or use a nullable type?
    }
  }

  public class DesignDay
  {
    public DesignDay(){}

    private string _name;
    private int _month;
    private int _dayOfMonth;
    private string _dayType;
    private double _maxTdb;
    private double _dailyTdbRange;
    private string _dailyTdbModifierType;
    private string _dailyTdbModifiedSchedule;
    private string _humidityType;
    private double _TwbAtMaxTdb;
    private string _humidityIndicatingDayScheduleName; //type unknown
    private double _humidityRatio;
    private double _enthalpy;
    private double _dailyTwbRange;
    private double _barometricPressure;
    private double _windSpeed;
    private double _windDirection;
    private bool _raining;
    private bool _snowy;
    private bool _dst; //daylight savings time
    private string _solarModelIndicator;
    private string _beamSolarDayScheduleName;
    private string _diffuseSolarDayScheduleName;
    private double _opticalDepthBeam;
    private double _opticalDepthDiffuse;
    private double _clearness;

    public string Name {get { return _name;} set {_name = value;}}
    public int Month { get { return _month; } set { _month = value;}}
    public int DayOfMonth{ get { return _dayOfMonth; } set { _dayOfMonth = value;}}
    public string DayType{ get { return _dayType; } set { _dayType = value;}}
    public double MaxTdb{ get { return _maxTdb; } set { _maxTdb = value;}}
    public double DailyTdbRange{ get { return _dailyTdbRange; } set { _dailyTdbRange = value;}}
    public string DailyTdbModifierType {get { return _dailyTdbModifierType;} set {_dailyTdbModifierType = value;}}
    public string DailyTdbModifiedSchedule{get { return _dailyTdbModifiedSchedule;} set {_dailyTdbModifiedSchedule = value;}}
    public string HumidityType{ get { return _humidityType; } set { _humidityType = value;}}
    public double TwbAtMaxTdb{get { return _TwbAtMaxTdb;} set {_TwbAtMaxTdb = value;}}
    public string HumidityIndicatingDayScheduleName{ get { return _humidityIndicatingDayScheduleName; } set { _humidityIndicatingDayScheduleName = value;}}
    public double HumidityRatio{ get { return _humidityRatio; } set { _humidityRatio = value;}}
    public double Enthalpy{ get { return _enthalpy; } set { _enthalpy = value;}}
    public double DailyTwbRange{ get { return _dailyTwbRange; } set { _dailyTwbRange = value;}}
    public double BarometricPressure{ get { return _barometricPressure; } set { _barometricPressure = value;}}
    public double WindSpeed{ get { return _windSpeed; } set { _windSpeed = value;}}
    public double WindDirection{ get { return _windDirection; } set { _windDirection = value;}}
    public bool Raining{ get { return _raining; } set { _raining = value;}}
    public bool Snowy{ get { return _snowy; } set { _snowy = value;}}
    public bool Dst{ get { return _dst; } set { _dst = value;}}
    public string SolarModelIndicator{ get { return _solarModelIndicator; } set { _solarModelIndicator = value;}}
    public string BeamSolarDayScheduleName{ get { return _beamSolarDayScheduleName; } set { _beamSolarDayScheduleName = value;}}
    public string DiffuseSolarDayScheduleName{ get { return _diffuseSolarDayScheduleName; } set { _diffuseSolarDayScheduleName = value;}}
    public double OpticalDepthBeam{ get { return _opticalDepthBeam; } set { _opticalDepthBeam = value;}}
    public double OpticalDepthDiffuse{ get { return _opticalDepthDiffuse; } set { _opticalDepthDiffuse = value;}}
    public double Clearness{ get { return _clearness; } set { _clearness = value;}}

    public override string ToString()
    {
      return "DesignDay item";
    }

    public List<string> ToListString()
    {
      var rtnlist = new List<string>();

      rtnlist.Add("Name: " + _name);
      rtnlist.Add("Month: " + _month.ToString());
      rtnlist.Add("DayOfMonth: " + _dayOfMonth.ToString());
      rtnlist.Add("DayType: " + _dayType.ToString());
      rtnlist.Add("MaxTdb: " + _maxTdb.ToString());
      rtnlist.Add("DailyTdbRange: " + _dailyTdbRange.ToString());
      rtnlist.Add("DailyTdbModifierType: " + _dailyTdbModifierType);
      rtnlist.Add("DailyTdbModifiedSchedule: " + _dailyTdbModifiedSchedule);
      rtnlist.Add("HumidityType: " + _humidityType);
      rtnlist.Add("TwbAtMaxTdb: " + _TwbAtMaxTdb.ToString());
      rtnlist.Add("HumidityIndicatingDayScheduleName: " + _humidityIndicatingDayScheduleName);
      rtnlist.Add("HumidityRatio: " + _humidityRatio.ToString());
      rtnlist.Add("Enthalpy: " + _enthalpy.ToString());
      rtnlist.Add("DailyTwbRange: " + _dailyTwbRange.ToString());
      rtnlist.Add("BarometricPressure: " + _barometricPressure.ToString());
      rtnlist.Add("WindSpeed: " + _windSpeed.ToString());
      rtnlist.Add("WindDirection: " + _windDirection.ToString());
      rtnlist.Add("Raining: " + _raining.ToString());
      rtnlist.Add("Snowy: " + _snowy.ToString());
      rtnlist.Add("Dst: " + _dst.ToString());
      rtnlist.Add("SolarModelIndicator: " + _solarModelIndicator);
      rtnlist.Add("BeamSolarDayScheduleName: " + _beamSolarDayScheduleName);
      rtnlist.Add("DiffuseSolarDayScheduleName: " + _diffuseSolarDayScheduleName);
      rtnlist.Add("OpticalDepthBeam: " + _opticalDepthBeam.ToString());
      rtnlist.Add("OpticalDepthDiffuse: " + _opticalDepthDiffuse.ToString());
      rtnlist.Add("Clearness: " + _clearness.ToString());
      return rtnlist;
    }
  }

  public class SiteLocation
  {
    public SiteLocation(){}

    private string _siteName;
    private double _latitude;
    private double _longitude;
    private double _gmt; //time zone
    private double _elevation;

    public string SiteName { get { return _siteName; } set { _siteName = value;}}
    public double Latitude { get { return _latitude; } set { _latitude = value;}}
    public double Longitude { get { return _longitude; } set { _longitude = value;}}
    public double Gmt { get { return _gmt; } set { _gmt = value;}}
    public double Elevation { get { return _elevation; } set { _elevation = value;}}

    public override string ToString()
    {
      return "SiteLocation " + _siteName;
    }
  }

  public class DDYFile
  {
    public DDYFile(){}

    private SiteLocation _siteLocation = new SiteLocation();
    private List<DesignDay> _designDays = new List<DesignDay>();

    public SiteLocation Site_Location { get { return _siteLocation; } set { _siteLocation = value;}}
    public List<DesignDay> Design_Days { get { return _designDays; } set { _designDays = value;}}

    public override string ToString()
    {
      return "DDY file for " + _siteLocation.SiteName + ", " + _designDays.Count.ToString() + " design days found";
    }
  }

  // </Custom additional code> 
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.