-
Notifications
You must be signed in to change notification settings - Fork 4
/
IESParser.cs
208 lines (200 loc) · 8.6 KB
/
IESParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
/*
* Copyright 2013 Kyle Morin
* This file is part of IESParser.
*
* IESParser is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* IESParser 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with IESParser.
* If not, see http://www.gnu.org/licenses/.
*/
namespace ConsoleApplication1
{
class IESParser
{
#region Properties
public string IESversion;
public string Manufac;
public string Lumcat;
public string Luminaire;
public string Lamp;
public List<string> Other;
public string Tilt;
public int NumOfLamps;
public int LumensPerLamp;
public int CDMultiplier;
public int NumVertAngles;
public int NumHorizAngles;
public int BallastFactor;
public double inputWatts;
public List<double> VertAnglesList = new List<double>();
public List<double> HorizAnglesList = new List<double>();
public Dictionary<Dictionary<double, double>, double> m = new Dictionary<Dictionary<double, double>, double>();
private int horizTotalCount = 0;
public int linecount = 0;
string combinedString = "";
#endregion
///<summary>
/// accepts a file input (in form of string) and outputs a matrix of values in the form of
/// Dictionary(Dictionary(double,double),double)
/// The key of the dictionary is the grid coordinates for the (y,x) values to search for later for interpolation.
/// The Valuestore is the actual candela value given by the IES file at the given (y,x) coordinate.
///
/// Based on test .ies files using the IESNA:LM-63-2002 format. *Have tested with many different files
/// from many different manufactuers, but due to the exponential number of possibilities, this parser
/// might not work 100% every time. Just use caution.
///</summary>
///<param name="iesFile">FilePath for .ies file to parse</param>
public Dictionary<Dictionary<double, double>, double> ParseIES(string iesFile)
{
FileStream fs = new FileStream(iesFile, FileMode.Open);
StreamReader sr = new StreamReader(fs);
int count = 0;
int res = 0;
string readLine = "";
while (!sr.EndOfStream)
{
readLine = sr.ReadLine();
if (readLine.Contains("IESNA:"))
{
IESversion = readLine;
}
if (readLine.Contains("[MANUFAC]"))
{
Manufac = readLine;
}
if (readLine.Contains("[LUMCAT]"))
{
Lumcat = readLine;
}
if (readLine.Contains("[LUMINAIRE]"))
{
Luminaire = readLine;
}
if (readLine.Contains("[LAMP]"))
{
Lamp = readLine;
}
if (res == 0 && int.TryParse(readLine.Substring(0, 1), out res))
{
string[] firstLine = readLine.Trim().Split(' ');
int.TryParse(firstLine.First(), out NumOfLamps);
int.TryParse(firstLine[1], out LumensPerLamp);
int.TryParse(firstLine[3], out NumVertAngles);
int.TryParse(firstLine[4], out NumHorizAngles);
}
else if (res > 0)
{
string[] line = readLine.TrimEnd().Split(' ');
if (line.Count() < 4 && readLine.StartsWith(" ") == false)
double.TryParse(line.Last(), out inputWatts);
if (readLine.StartsWith("0") || readLine.StartsWith(" ") && HorizAnglesList.Count < NumHorizAngles)
{
if (line.Count() > NumHorizAngles && VertAnglesList.Count < NumVertAngles)
{
foreach (string s in line)
{
double dblRes;
double.TryParse(s, out dblRes);
if (VertAnglesList.Contains(dblRes)) { }
else
VertAnglesList.Add(dblRes);
}
}
if (line.Last() == "90")
{
if (line.Count() > NumHorizAngles)
foreach (string s in line)
{
double dblRes;
double.TryParse(s, out dblRes);
if (VertAnglesList.Contains(dblRes)) { }
else
VertAnglesList.Add(dblRes);
}
else if (line.Count() < NumVertAngles && line.First() == "0")
foreach (string s in line)
{
double dblRes;
double.TryParse(s, out dblRes);
HorizAnglesList.Add(dblRes);
}
}
}
else
{
if (!readLine.StartsWith("0") && NumHorizAngles > 0 && line.Last() != inputWatts.ToString())
{
if (line.Last() == "0")
{
combinedString += readLine.Trim() + "%";
}
else
{
combinedString += readLine.Trim();
}
}
}
}
count++;
}
string[] values = combinedString.Trim().Split('%');
foreach (string v in values)
{
string[] cdValues = v.Trim().Split(' ');
foreach (string value in cdValues)
{
double dblRes;
if(double.TryParse(value, out dblRes))
{
Dictionary<double, double> dictKey = new Dictionary<double, double>();
dictKey.Add(VertAnglesList.ElementAt(linecount), HorizAnglesList.ElementAt(horizTotalCount));
m.Add(dictKey, dblRes);
linecount++;
}
}
horizTotalCount++;
linecount = 0;
}
sr.Close();
fs.Close();
return m;
}
/// <summary>
/// Results printer for the matrix. *In progress*
/// Idea is to take the resulting Dictionary from the parser and output to a human readable table
/// for verification, printing, etc.
/// </summary>
/// <param name="m">Dictionary result from the parser</param>
/// <returns>human readable string table to printout to console or MessageBox, etc..</returns>
public string ResultIESMatrix(Dictionary<Dictionary<double, double>, double> m)
{
string sx = "";
string sy = "";
foreach (KeyValuePair<Dictionary<double, double>, double> i in m)
{
double x = 0;
double y = 0;
foreach (KeyValuePair<double, double> pair in i.Key)
{
y = pair.Key;
x = pair.Value;
}
double candelaValue = i.Value;
}
return "";
}
}
}