Class: RadianceMeasure

Inherits:
OpenStudio::Measure::ModelMeasure
  • Object
show all
Defined in:
lib/measures/radiance_measure/measure.rb

Overview

start the measure

Defined Under Namespace

Modules: OS

Instance Method Summary collapse

Instance Method Details

#arguments(model) ⇒ Object

define the arguments that the user will input



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
# File 'lib/measures/radiance_measure/measure.rb', line 48

def arguments(model)
  args = OpenStudio::Measure::OSArgumentVector.new

  apply_schedules = OpenStudio::Measure::OSArgument.makeBoolArgument('apply_schedules', true)
  apply_schedules.setDisplayName('Apply schedules')
  apply_schedules.setDefaultValue('true')
  apply_schedules.setDescription('Update lighting load schedules for Radiance-daylighting control response')
  args << apply_schedules

  chs = OpenStudio::StringVector.new
  chs << 'Default'
  chs << 'Min'
  chs << 'Max'
  use_cores = OpenStudio::Measure::OSArgument.makeChoiceArgument('use_cores', chs, true)
  use_cores.setDisplayName('Cores')
  use_cores.setDefaultValue('Default')
  use_cores.setDescription('Number of CPU cores to use for Radiance jobs. Default is to use all but one core, NOTE: this option is ignored on Windows.')
  args << use_cores

  chs = OpenStudio::StringVector.new
  chs << 'Model'
  chs << 'Testing'
  chs << 'High'
  rad_settings = OpenStudio::Measure::OSArgument.makeChoiceArgument('rad_settings', chs, true)
  rad_settings.setDisplayName('Radiance Settings')
  rad_settings.setDefaultValue('Model')
  rad_settings.setDescription('The measure gets the Radiance simulation parameters from the "Model" by default. "High" will force high-quality simulation paramaters, and "Testing" uses very crude parameters for a fast simulation but produces very inaccurate results.')
  args << rad_settings

  debug_mode = OpenStudio::Measure::OSArgument.makeBoolArgument('debug_mode', false)
  debug_mode.setDisplayName('Debug Mode')
  debug_mode.setDefaultValue('false')
  debug_mode.setDescription('Generate additional log messages, images for each window group, and save all window group output.')
  args << debug_mode

  cleanup_data = OpenStudio::Measure::OSArgument.makeBoolArgument('cleanup_data', false)
  cleanup_data.setDisplayName('Cleanup Data')
  cleanup_data.setDefaultValue('false')
  cleanup_data.setDescription('Delete Radiance input and (most) output data, post-simulation (lighting schedules are passed to OpenStudio model (and daylight metrics are passed to OpenStudio-server, if applicable)')
  args << cleanup_data

  return args
end

#descriptionObject

human readable description



38
39
40
# File 'lib/measures/radiance_measure/measure.rb', line 38

def description
  return 'This measure uses Radiance instead of EnergyPlus for daylighting calculations with OpenStudio.'
end

#got_2xObject

Check OpenStudio version



240
241
242
243
244
245
246
# File 'lib/measures/radiance_measure/measure.rb', line 240

def got_2x
  v2 = false
  if OpenStudio::VersionString.new(OpenStudio.openStudioVersion).major >= 2
    v2 = true
  end
  return v2
end

#merge_countObject

check for number of rmtxop processes



249
250
251
252
253
254
255
256
# File 'lib/measures/radiance_measure/measure.rb', line 249

def merge_count
  if OS.windows
    merges = `WMIC PROCESS WHERE Name="rmtxop.exe"`.split("\n\n")
    return merges.size
  else
    return `pgrep rmtxop`.split.size
  end
end

#modeler_descriptionObject

human readable description of modeling approach



43
44
45
# File 'lib/measures/radiance_measure/measure.rb', line 43

def modeler_description
  return 'The OpenStudio model is converted to Radiance format. All spaces containing daylighting objects (illuminance map, daylighting control point, and optionally glare sensors) will have annual illuminance calculated using Radiance, and the OS model\'s lighting schedules can be overwritten with those based on daylight responsive lighting controls.'
end

#modelToRadPreprocess(model) ⇒ Object



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
209
210
211
212
213
214
215
216
217
# File 'lib/measures/radiance_measure/measure.rb', line 115

def modelToRadPreprocess(model)
  result = OpenStudio::Model::Model.new
  result.getBuilding
  result.getTimestep
  result.getRunPeriod

  if !model.getOptionalWeatherFile.empty?
    result.getWeatherFile
  end

  thermal_zones = {}

  model.getSpaces.each do |space|
    space.hardApplyConstructions
    space.hardApplySpaceType(true)
    space.hardApplySpaceLoadSchedules

    # make all surfaces with surface boundary condition adiabatic
    space.surfaces.each do |surface|
      adjacentSurface = surface.adjacentSurface
      if !adjacentSurface.empty?

        # make sure to hard apply constructions in other space before messing with surface in other space
        adjacentSpace = adjacentSurface.get.space
        if !adjacentSpace.empty?
          adjacentSpace.get.hardApplyConstructions
        end

        # resets both surfaces
        surface.resetAdjacentSurface

        # set both to adiabatic
        surface.setOutsideBoundaryCondition('Adiabatic')
        adjacentSurface.get.setOutsideBoundaryCondition('Adiabatic')

        # remove interior windows
        surface.subSurfaces.each(&:remove)

        adjacentSurface.get.subSurfaces.each(&:remove)
      end
    end

    new_space = space.clone(result).to_Space.get

    thermalZone = space.thermalZone
    if !thermalZone.empty?

      new_thermal_zone = thermal_zones[thermalZone.get.name.to_s]
      if new_thermal_zone.nil?
        new_thermal_zone = OpenStudio::Model::ThermalZone.new(result)
        new_thermal_zone.setName(thermalZone.get.name.to_s)
        new_thermal_zone.setUseIdealAirLoads(true)
        thermal_zones[thermalZone.get.name.to_s] = new_thermal_zone
      end

      new_space.setThermalZone(new_thermal_zone)
    end
  end

  result.getShadingSurfaceGroups.each(&:remove)

  result.getSpaceItems.each do |spaceItem|
    if !spaceItem.to_People.empty?
      # keep people
    elsif !spaceItem.to_Lights.empty?
      # keep lights
    elsif !spaceItem.to_Luminaire.empty?
      # keep luminaires
    else
      spaceItem.remove
    end
  end

  result.getOutputVariables.each(&:remove)

  # add the OVars we need for Radiance
  output_variables = [
    'Site Exterior Horizontal Sky Illuminance',
    'Site Exterior Beam Normal Illuminance',
    'Site Solar Altitude Angle',
    'Site Solar Azimuth Angle',
    'Site Sky Diffuse Solar Radiation Luminous Efficacy',
    'Site Beam Solar Radiation Luminous Efficacy',
    'Zone People Occupant Count',
    'Zone Lights Electricity Rate'
  ]

  output_variables.each do |var|
    output_variable = OpenStudio::Model::OutputVariable.new(var, result)
    output_variable.setReportingFrequency('Hourly')
  end

  # only report weather periods
  simulation_control = result.getSimulationControl
  simulation_control.setRunSimulationforSizingPeriods(false)
  simulation_control.setRunSimulationforWeatherFileRunPeriods(true)
  simulation_control.setSolarDistribution('MinimalShadowing')

  # purge unused
  result.purgeUnusedResourceObjects

  return result
end

#nameObject

human readable name



33
34
35
# File 'lib/measures/radiance_measure/measure.rb', line 33

def name
  return 'Radiance Daylighting Measure'
end

#read_illuminance_file(filename, runner) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/measures/radiance_measure/measure.rb', line 92

def read_illuminance_file(filename, runner)
  m = Matrix[]
  data_section = false
  header = []
  data = []

  print_statement("Reading '#{filename}'", runner)
  raise "Could not find illuminance file #{filename}" unless File.exist?(filename)

  File.read(filename).each_line do |line|
    data_section = true if line =~ /^\s?\d/
    if data_section
      csv_line = CSV.parse_line(line.strip, col_sep: ' ')

      m = Matrix.rows(m.to_a << csv_line)
    else
      header << line.to_s
    end
  end

  return m, header
end

#run(model, runner, user_arguments) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
# File 'lib/measures/radiance_measure/measure.rb', line 258

def run(model, runner, user_arguments)
  super(model, runner, user_arguments)

  # record current directory
  current_dir = Dir.pwd

  runner.registerInfo("Begin Encoding.default_external = #{Encoding.default_external}")
  runner.registerInfo("Begin Encoding.default_internal = #{Encoding.default_internal}")

  OpenStudio::Logger.instance.standardOutLogger.enable

  # Enable debug-level log messages
  # OpenStudio::Logger::instance().standardOutLogger().setLogLevel(OpenStudio::Debug)

  # use the built-in error checking
  return false if !runner.validateUserArguments(arguments(model), user_arguments)

  # assign the user inputs to variables
  apply_schedules = runner.getBoolArgumentValue('apply_schedules', user_arguments)
  use_cores = runner.getStringArgumentValue('use_cores', user_arguments)
  rad_settings = runner.getStringArgumentValue('rad_settings', user_arguments)
  debug_mode = runner.getBoolArgumentValue('debug_mode', user_arguments)
  cleanup_data = runner.getBoolArgumentValue('cleanup_data', user_arguments)

  # Energyplus "pre-run" model dir
  epout_dir = 'eplus_preprocess'
  if !File.exist?(epout_dir)
    FileUtils.mkdir_p(epout_dir)
  end

  # Radiance model dir
  rad_dir = 'radiance'
  if !File.exist?(rad_dir)
    FileUtils.mkdir_p(rad_dir)
  end

  ## Radiance Utilities

  # print statement and execute as system call
  def exec_statement(s, runner)
    if OS.windows
      s = s.tr('/', '\\')
    end
    runner.registerInfo(s.to_s)
    # additional puts for OSApp until v2.0...
    puts "[Radiance Measure #{Time.now.getutc}]: \$ #{s}"
    result = system(s)
    return result
  end

  # print statement for OS-Server and OSApp
  def print_statement(s, runner)
    # if /mswin/.match(RUBY_PLATFORM) || /mingw/.match(RUBY_PLATFORM)
    if OS.windows
      s = s.tr('/', '\\')
    end
    runner.registerInfo(s.to_s)
    # additional puts for OSApp until v2.0...
    puts "[Radiance Measure #{Time.now.getutc}]: #{s}"
  end

  # UNIX-style which
  def which(cmd)
    exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
    ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
      exts.each do |ext|
        exe = "#{path}/#{cmd}#{ext}"
        return exe if File.executable? exe
      end
    end
    return nil
  end

  # set up MP option
  coreCount = OpenStudio::System.numberOfProcessors
  sim_cores = '1'

  case use_cores
  when 'Max'
    sim_cores = coreCount
  when 'Min'
    sim_cores = 1
  else
    sim_cores = coreCount - 1
  end
  if OS.windows # /mswin/.match(RUBY_PLATFORM) || /mingw/.match(RUBY_PLATFORM)
    print_statement('Radiance multiprocessing features are not supported on Windows.', runner)
    sim_cores = 1
  end
  print_statement("Using #{sim_cores} core(s) for Radiance jobs", runner)

  # help those poor Windows users out
  programExtension = ''
  perlExtension = ''
  catCommand = 'cat'
  osQuote = "\'"
  if OS.windows # /mswin/.match(RUBY_PLATFORM) || /mingw/.match(RUBY_PLATFORM)
    programExtension = '.exe'
    perlExtension = '.pl'
    catCommand = 'type'
    osQuote = '"'
  end

  ## END Radiance Utilities

  runner.registerInfo('Running in debug mode') if debug_mode

  # this is radiance path, run commands from this directory for some reason
  path = nil

  # this is "radpath", in the (Radiance) parlance of our times...
  raypath = nil

  # setup environment for Radiance and Perl
  if !got_2x

    print_statement('BCL Radiance measure version, running on legacy OpenStudio', runner)

    co = OpenStudio::Runmanager::ConfigOptions.new(true)
    co.fastFindRadiance
    radiancePath = co.getTools.getLastByName('rad').localBinPath.parent_path

    path = OpenStudio::Path.new(radiancePath).to_s
    rad_bin_path = (OpenStudio::Path.new(radiancePath).parent_path /
        OpenStudio::Path.new('bin')).to_s
    raypath = (OpenStudio::Path.new(radiancePath).parent_path /
    OpenStudio::Path.new('lib')).to_s

    epw2weapath = (OpenStudio::Path.new(radiancePath) / OpenStudio::Path.new('epw2wea')).to_s

    if OS.windows # /mswin/.match(RUBY_PLATFORM) || /mingw/.match(RUBY_PLATFORM)

      perlpath = ''
      if OpenStudio.applicationIsRunningFromBuildDirectory
        perlpath = OpenStudio.getApplicationRunDirectory.parent_path.parent_path /
                   OpenStudio::Path.new('strawberry-perl-5.16.2.1-32bit-portable-reduced/perl/bin')
      else
        perlpath = OpenStudio.getApplicationRunDirectory.parent_path /
                   OpenStudio::Path.new('strawberry-perl-5.16.2.1-32bit-portable-reduced/perl/bin')
      end
      print_statement("Adding path for local perl: #{perlpath}", runner)
      ENV['PATH'] = "#{path};#{ENV['PATH']};#{perlpath}"
      ENV['RAYPATH'] = "#{path};#{raypath};."
    else
      ENV['PATH'] = "#{path}:#{ENV['PATH']}"
      ENV['RAYPATH'] = "#{path}:#{raypath}:."
    end

  end

  if got_2x

    radiance_directory = nil
    begin
      radiance_directory = OpenStudio.getRadianceDirectory.to_s
    rescue NameError
    end

    if !radiance_directory
      # DLM: we did not ship radiance before adding the getRadianceDirectory method, rely on rtrace in the path
      rtrace_exe = which('rtrace')
      if rtrace_exe
        radiance_directory = File.dirname(File.dirname(rtrace_exe))
      else
        runner.registerError('Cannot find required program rtrace.')
        exit false
      end
    end

    print_statement("Found Radiance at: #{radiance_directory}", runner)
    path = radiance_directory
    rad_bin_path = File.join(radiance_directory, 'bin')
    raypath = File.join(radiance_directory, 'lib')

    perl_directory = nil
    begin
      perl_exe = OpenStudio.getPerlExecutable
      perl_directory = perl_exe.parent_path.to_s
    rescue NameError
    end

    if !perl_directory
      # DLM: we did not ship perl before adding the getPerlExecutable method, rely on perl in the path
      perl_exe = which('perl')
      if perl_exe
        perl_directory = File.dirname(perl_exe)
      else
        runner.registerError('Cannot find required program perl.')
        exit false
      end
    end

    print_statement("Found Perl at: #{perl_directory}", runner)

    energyplus_exe = nil
    begin
      energyplus_exe = OpenStudio.getEnergyPlusExecutable
    rescue NameError
    end
    print_statement("Found EnergyPlus at: #{energyplus_exe}", runner)

    # ENV['ENERGYPLUS_EXE_PATH']

    ENV['PATH'] = [File.join(radiance_directory, 'bin'), perl_directory, ENV['PATH']].join(File::PATH_SEPARATOR)
    ENV['RAYPATH'] = [File.join(radiance_directory, 'bin'), File.join(radiance_directory, 'lib'), '.', ENV['RAYPATH']].join(File::PATH_SEPARATOR)

  end

  # Radiance version detection and environment reportage

  begin
    # need to help Open3 on Windows (path sep issues)
    returnDir = Dir.pwd
    Dir.chdir(rad_bin_path)

    ver = Open3.capture2("rcontrib#{programExtension} -version")
    # aaaannnddd, we're back...
    Dir.chdir(returnDir)
  rescue StandardError
    StandardError
    raise('Error determining Radiance version, system is mis-configured.')
  end

  print_statement("Radiance version: #{ver[0]}", runner)
  print_statement("Radiance binary dir: #{rad_bin_path}", runner)
  print_statement("Radiance library dir: #{raypath}", runner)

  print_statement('Running on Windows (sorry)', runner) if OS.windows && debug_mode
  print_statement('Running on unix', runner) if OS.unix && debug_mode

  if !got_2x && Dir.glob(epw2weapath + programExtension).empty?

    runner.registerError("Cannot find epw2wea tool in radiance installation at '#{radiancePath}'. You may need to install a newer version of Radiance.")
    exit false

  end

  if !which('perl')
    runner.registerError('Perl could not be found in path, exiting')
    exit false
  end

  # get the weather

  if !got_2x

    epw_path = nil

    # try runner first
    if runner.lastEpwFilePath.is_initialized
      test = runner.lastEpwFilePath.get.to_s
      if File.exist?(test)
        epw_path = test
      end
    end

    # try model second
    if !epw_path
      if model.weatherFile.is_initialized
        test = model.weatherFile.get.path
        if test.is_initialized
          # have a file name from the model
          if File.exist?(test.get.to_s)
            epw_path = test.get
          else
            # If this is an always-run Measure, need to check for file in different path
            alt_weath_path = File.expand_path(File.join(File.dirname(__FILE__), \
                                                        '../../../resources'))
            alt_epw_path = File.expand_path(File.join(alt_weath_path, test.get.to_s))
            server_epw_path = File.expand_path(File.join(File.dirname(__FILE__), \
                                                         "../../weather/#{File.basename(test.get.to_s)}"))
            if File.exist?(alt_epw_path)
              epw_path = OpenStudio::Path.new(alt_epw_path)
            elsif File.exist? server_epw_path
              epw_path = OpenStudio::Path.new(server_epw_path)
            else
              runner.registerError("Model has been assigned a weather file, but the file is not in \
              the specified location of '#{test.get}'. server_epw_path: #{server_epw_path}, test \
              basename: #{File.basename(test.get.to_s)}, test: #{test}")
              return false
            end
          end
        else
          runner.registerError('Model has a weather file assigned, but the weather file path has \
          been deleted.')
          return false
        end
      else
        runner.registerError('Model has not been assigned a weather file.')
        return false
      end
    end

  else

    ## weather is in the E+ pre-process for OS2.x

  end

  ## Translate Model to Radiance

  radPath = nil

  if !got_2x
    # save osm for input to eplus pre-process
    modelPath = OpenStudio::Path.new('eplusin.osm')
    model.save(modelPath, true)

    # find EnergyPlus
    co = OpenStudio::Runmanager::ConfigOptions.new
    co.fastFindEnergyPlus

    # make a workflow (EnergyPlus "pre-run" to get constructions and weather)
    workflow = OpenStudio::Runmanager::Workflow.new('ModelToRadPreprocess->ModelToIdf->ExpandObjects->EnergyPlus')
    workflow.add(co.getTools)

    # add model-to-rad workflow
    modelToRad = OpenStudio::Runmanager::Workflow.new('ModelToRad')
    workflow.addWorkflow(modelToRad)

    # minimize file path lengths
    workflow.addParam(OpenStudio::Runmanager::JobParam.new('flatoutdir'))

    # make the run manager
    runDir = OpenStudio::Path.new(epout_dir)
    runmanager_path = OpenStudio::Path.new('runmanager.db')
    runmanager = OpenStudio::Runmanager::RunManager.new(runmanager_path, true, true, false, false)

    OpenStudio.makeParentFolder(runDir, OpenStudio::Path.new, true)
    print_statement('Creating workflow', runner)

    jobtree = workflow.create(OpenStudio.system_complete(runDir), \
                              OpenStudio.system_complete(modelPath), OpenStudio::Path.new(epw_path))
    runmanager.enqueue(jobtree, true)
    print_statement("Running jobs in #{runDir}", runner)
    runmanager.setPaused(false)
    runmanager.waitForFinished

    if jobtree.treeErrors.succeeded
      print_statement('OpenStudio to Radiance translation complete', runner)

    else
      jobtree.treeErrors.errors.each do |err|
        print_statement("ERROR: #{err}", runner)
      end
      print_statement('Model issue(s) caused EnergyPlus preprocess failure, aborting.', runner)
      abort

    end

    radPath = modelPath.parent_path / OpenStudio::Path.new('radiance')
    radPath = OpenStudio.system_complete(radPath)

  else # OS 2.x

    print_statement('Translating OpenStudio 2.x model to Radiance format...', runner)

    # Run eplus pre-process for sql output for Radiance translator (got all that?)

    epout_dir = 'eplus_preprocess'
    if !File.exist?(epout_dir)
      FileUtils.mkdir_p(epout_dir)
    end

    modelPath = OpenStudio::Path.new(File.join(epout_dir, 'eplusin.osm'))
    weather_file = model.getOptionalWeatherFile
    weather_file_path = weather_file.get.path
    weather_file_path = runner.workflow.findFile(weather_file_path.get)

    wf = OpenStudio::WorkflowJSON.new
    wf.setSeedFile('eplusin.osm')
    wf.setWeatherFile(weather_file_path.get)
    wf_path = File.join(Dir.pwd, epout_dir, 'temp_in.osw')
    osw = wf.saveAs(wf_path)

    preprocessed_model = modelToRadPreprocess(model.clone.to_Model)
    preprocessed_model.save(modelPath, true)

    cli = OpenStudio.getOpenStudioCLI
    runner.registerInfo("OpenStudio CLI version: #{OpenStudio.openStudioLongVersion}")
    system("\"#{cli}\" run -w \"#{wf_path}\"")

    sql_file = OpenStudio::SqlFile.new(File.join(Dir.pwd, epout_dir, 'run/eplusout.sql')) # fix hard coded path
    old_sql_file = model.sqlFile
    model.setSqlFile(sql_file)

    # Finally, translate the OSM
    ft = OpenStudio::Radiance::RadianceForwardTranslator.new
    radPath = OpenStudio::Path.new('radiance')
    radPath = OpenStudio.system_complete(radPath)
    ft.translateModel(radPath, model)

    if old_sql_file.empty?
      model.resetSqlFile
    else
      model.resetSqlFile
      model.setSqlFile(old_sql_file.get)
    end

  end

  ##  Radiance crap
  windowControls = Dir.glob('scene/glazing/WG*.rad')

  # set up output dirs
  FileUtils.mkdir_p("#{radPath}/output/dc") unless File.exist?("#{radPath}/output/dc")
  FileUtils.mkdir_p("#{radPath}/output/ts") unless File.exist?("#{radPath}/output/ts")
  FileUtils.mkdir_p("#{radPath}/output/dc/merged_space/maps") unless File.exist?("#{radPath}/output/dc/merged_space/maps")
  FileUtils.mkdir_p("#{radPath}/sql") unless File.exist?("#{radPath}/sql")
  FileUtils.mkdir_p("#{radPath}/wx") unless File.exist?("#{radPath}/wx")
  FileUtils.mkdir_p("#{radPath}/octrees") unless File.exist?("#{radPath}/octrees")

  if !got_2x
    # copy Radiance model up
    FileUtils.copy_entry("#{epout_dir}/4-ModelToRad-0", rad_dir)
    FileUtils.cp("#{epout_dir}/3-EnergyPlus-0/eplusout.sql", "#{rad_dir}/sql")
    # remove the E+ run dir so we don't confuse users
    FileUtils.rm_rf(epout_dir)
  else
    FileUtils.cp("#{epout_dir}/run/eplusout.sql", "#{radPath}/sql")
    # remove the E+ run dir so we don't confuse users
    FileUtils.rm_rf(epout_dir)
  end

  # Set Radiance simulation settings
  # TODO: read settings directly from model

  options_tregVars = ''
  options_dmx = ''
  options_vmx = ''

  if rad_settings == 'Testing'
    options_tregVars = '-e MF:1 -f tregenza.cal -b tbin -bn Ntbins'
    options_dmx = '-ab 1 -ad 128 -as 56 -dj 1 -dp 1 -dt 0.1 -dc 0.1 -lw 0.1 '
    options_vmx = '-ab 1 -ad 128 -as 56 -dj 1 -dp 1 -dt 0.1 -dc 0.1 -lw 0.1'
  end

  if rad_settings == 'High'
    options_tregVars = '-e MF:1 -f tregenza.cal -b tbin -bn Ntbins'
    options_dmx = '-ab 3 -ad 1024 -as 512 -dj 1 -dp 1 -dt 0 -dc 1 -lw 0.0001 '
    options_vmx = '-ab 10 -ad 65536 -as 512 -dj 1 -dp 1 -dt 0 -dc 1 -lw 1.52e-05'
  end

  options_klemsDensity = ''
  options_skyvecDensity = '1'

  if rad_settings == 'Model'
    File.open("#{radPath}/options/treg.opt", 'r') do |file|
      tempIO = file.read
      tempSettings = tempIO.split(' ')
      options_klemsDensity = "#{tempSettings[0]} #{tempSettings[1]}"
      options_skyvecDensity = tempSettings[3].split(':')[1]
      options_tregVars = tempSettings[2..].join(' ')
    end

    File.open("#{radPath}/options/dmx.opt", 'r') do |file|
      tempIO = file.read
      options_dmx = tempIO
    end

    File.open("#{radPath}/options/vmx.opt", 'r') do |file|
      tempIO = file.read
      options_vmx = tempIO
    end

  end

  # configure multiprocessing
  procsUsed = ''
  if OS.windows
    procsUsed = ''
  else
    procsUsed = "-n #{sim_cores}"
  end

  # core functions

  def calculateDaylightCoeffecients(radPath, sim_cores, t_catCommand, options_tregVars,
                                    options_klemsDensity, options_skyvecDensity, options_dmx,
                                    options_vmx, rad_settings, procsUsed, runner, debug_mode)

    # get calculation points array size (needed for rmtxop later)
    mapFile = File.open('numeric/merged_space.map', 'r')
    rfluxmtxDim = mapFile.readlines.size.to_s

    # sort out window groups, controls
    haveWG0 = ''
    haveWG1 = ''
    windowGroupCheck = File.open('bsdf/mapping.rad')
    windowGroupCheck.each do |row|
      next if row[0] == '#'

      wg = row.split(',')[0]

      case wg
      when 'WG0'
        haveWG0 = 'True'
      when 'WG1'
        haveWG1 = 'True'
      end
    end
    windowGroupCheck.close

    if rfluxmtxDim.to_i > 1000 && rfluxmtxDim.to_i < 2999
      print_statement("WARN: Model contains a large number of Radiance calculation points (#{rfluxmtxDim}), will produce large results files and potential memory issues.", runner)
    elsif rfluxmtxDim.to_i > 3000
      print_statement("ERROR: Too many calculation points in model (#{rfluxmtxDim}). Consider reducing the number or resolution of illuminance maps in this model.", runner)
      exit false
    else
      print_statement("Passing #{rfluxmtxDim} calculation points to Radiance", runner)
    end

    # process individual window groups
    print_statement('Computing daylight coefficient matrices', runner)
    exec_statement('oconv materials/materials.rad model.rad > octrees/model_dc.oct', runner)

    windowMaps = File.open('bsdf/mapping.rad')

    windowMaps.each do |row|
      next if row[0] == '#'

      wg = row.split(',')[0]

      rad_command = ''

      if wg == 'WG0' # window group zero (all uncontrolled windows)

        print_statement('Computing view matrix for uncontrolled windows (WG0)', runner)

        # make WG0 octree (with shade-controlled window groups blacked out, if any)
        input_files = ''
        if haveWG1 == 'True'
          input_files = 'materials/materials.rad materials/materials_WG0.rad model.rad'
        else
          input_files = 'materials/materials.rad model.rad skies/dc_sky.rad'
        end

        # for the calc, include unit sky
        exec_statement("oconv #{input_files} skies/dc_sky.rad > octrees/model_WG0.oct", runner)

        if debug_mode
          # for check images (insert sky later, in genImages())
          exec_statement("oconv #{input_files} > octrees/debug_model_WG0.oct", runner)
        end

        # use more aggro simulation parameters because this is basically a view matrix
        rtrace_args = options_vmx.to_s

        ### foo (-faf)
        rad_command = "#{t_catCommand} numeric/merged_space.map | rcontrib #{rtrace_args} #{procsUsed} -I+ -fo #{options_tregVars} -faa -o output/dc/WG0.vmx -m skyglow octrees/model_WG0.oct"
        exec_statement(rad_command, runner)

      else # controlled window group

        print_statement("Processing shade-controlled window group '#{wg}'", runner)

        if row.split(',')[4].rstrip == 'SWITCHABLE' # has switchable glazing

          print_statement("Window Group '#{wg}' has switchable glazing control, calculating two view matrices", runner)

          # black out WG0 and all other WG shades
          # start with base materials, then black everything out
          base_mats = 'materials/materials.rad materials/materials_blackout.rad'

          # do view matrices, one for each tint state
          rtrace_args = options_vmx.to_s

          ['clear', 'tinted'].each do |state|
            # for the calc
            exec_statement("oconv #{base_mats} materials/#{wg}_#{state}.mat model.rad skies/dc_sky.rad > octrees/model_#{wg}_#{state}.oct", runner)
            if debug_mode
              # for check images
              exec_statement("oconv #{base_mats} materials/#{wg}_#{state}.mat model.rad > octrees/debug_model_#{wg}_#{state}.oct", runner)
            end
            print_statement("Computing view matrix for window group '#{wg}' in #{state} state", runner)
            exec_statement("#{t_catCommand} \"numeric/merged_space.map\" | rcontrib #{rtrace_args} #{procsUsed} -I+ -fo #{options_tregVars} -faa -o \"output/dc/#{wg}_#{state}.vmx\" -m skyglow octrees/model_#{wg}_#{state}.oct", runner)
          end

        else # has shades

          # use more chill sim parameters
          rtrace_args = options_dmx.to_s

          # do daylight matrices for controlled windows
          print_statement("Computing daylight matrix for window group '#{wg}'", runner)

          if debug_mode

            # make octrees for debug images
            # load materials, then black out all materials, then add in scene geometry and glazing (no shades)
            input_files = 'materials/materials.rad materials/materials_blackout.rad'
            scene_files = []
            Dir.glob('scene/*.rad').each { |f| scene_files << f }
            Dir.glob('scene/glazing/*.rad').each { |f| scene_files << f }
            # now reset window group glazing material and make an octree
            exec_statement("oconv #{input_files} materials/#{wg}.mat #{scene_files.join(' ')} > octrees/debug_model_#{wg}.oct", runner)
            # now reset window group shade material to actual and make an octree
            exec_statement("oconv #{input_files} materials/#{wg}.mat materials/#{wg}_SHADE.mat #{scene_files.join(' ')} scene/shades/#{wg}_SHADE.rad > octrees/debug_model_#{wg}_shade.oct", runner)

          end

          ### foo (-faa)
          rad_command = "rfluxmtx #{rtrace_args} -n #{sim_cores} -faa -v scene/shades/#{wg}_SHADE.rad skies/dc_sky.rad -i octrees/model_dc.oct > \"output/dc/#{wg}.dmx\""
          exec_statement(rad_command, runner)

        end

      end
    end

    # do remaining view matrices, if applicable

    shade_check = Dir.glob('scene/shades/WG*.rad')
    if !shade_check.empty?

      # compute view matrices for shade controlled window groups all at once

      # use fine params
      rtrace_args = options_vmx.to_s

      print_statement('Computing view matri(ces) for all remaining window groups', runner)

      # get the shaded window groups' shade polygons

      wgInput = []

      # get the SHADE polygons for sampling (NOT the GLAZING ones!)
      # this will automatically omit switchable glazing-controlled window groups. ;)

      Dir.glob('scene/shades/WG*.rad') do |file|
        wgInput << file
      end

      # make the receiver file
      exec_statement("#{t_catCommand} \"materials/materials_vmx.rad\" #{wgInput.join(' ')} > receivers_vmx.rad", runner)

      # make the octree
      scene_files = []
      Dir.glob('scene/*.rad').each { |f| scene_files << f }
      exec_statement("oconv materials/materials.rad #{scene_files.join(' ')} > octrees/model_vmx.oct", runner)

      # make rfluxmtx do all the work
      ### foo (-faf)
      rad_command = "rfluxmtx #{rtrace_args} -n #{sim_cores} -ds .15 -faa -y #{rfluxmtxDim} -I -v - receivers_vmx.rad -i octrees/model_vmx.oct < numeric/merged_space.map"
      exec_statement(rad_command, runner)

      FileUtils.rm_f('receivers_vmx.rad')

    end

    if haveWG1 == 'True'

      # compute daylight coefficient matrix for window group control points

      rtrace_args = options_dmx.to_s
      exec_statement('oconv "materials/materials.rad" model.rad skies/dc_sky.rad > octrees/model_wc.oct', runner)
      print_statement('Computing DCs for window control points', runner)

      ### foo (keep this one as ASCII)
      rad_command = "#{t_catCommand} \"numeric/window_controls.map\" | rcontrib #{rtrace_args} #{procsUsed} -I+ -faa -fo #{options_tregVars} " \
                    '-o "output/dc/window_controls.vmx" -m skyglow octrees/model_wc.oct'
      exec_statement(rad_command, runner)

    end

    print_statement('Daylight coefficient matrices computed.', runner)
  end

  # annual simulation dealio
  def runSimulation(t_space_names_to_calculate, t_sqlFile, t_simCores, t_options_skyvecDensity,
                    t_site_latitude, t_site_longitude, t_site_stdmeridian, t_radPath,
                    t_spaceWidths, t_spaceHeights, t_radGlareSensorViews, runner, debug_mode)

    print_statement('Performing annual daylight simulation(s)', runner)

    rawValues = {}
    values = {}
    dcVectors = {}

    # sort out window groups, controls
    haveWG0 = 'False'
    haveWG1 = 'False'
    windowGroupCheck = File.open('bsdf/mapping.rad')
    windowGroupCheck.each do |row|
      next if row[0] == '#'

      wg = row.split(',')[0]

      case wg
      when 'WG0'
        haveWG0 = 'True'
      when 'WG1'
        haveWG1 = 'True'
      end
    end
    windowGroupCheck.close

    # Run the simulation

    simulations = []

    rad_command = "gendaymtx -m #{t_options_skyvecDensity} \"wx/in.wea\" > annual-sky.mtx"
    exec_statement(rad_command, runner)

    windowMaps = File.open('bsdf/mapping.rad')

    # do annual sim for each window group and state

    windowMaps.each do |row|
      # skip header
      next if row[0] == '#'

      wg = row.split(',')[0]

      # do uncontrolled windows (WG0)
      if wg == 'WG0'
        # if row.split(",")[2] == "n/a" || row.split(",")[2] == "AlwaysOff"
        # keep header, convert to illuminance, but no transpose
        ### foo (-ff)

        while merge_count > 1
          puts 'waiting in rmtxop queue...'
          sleep(5)
        end
        rad_command = "dctimestep output/dc/#{wg}.vmx annual-sky.mtx | rmtxop -fa -c 47.4 120 11.6 - > output/ts/#{wg}.ill"
        exec_statement(rad_command, runner)

      else

        # do all controlled window groups

        if row.split(',')[4].rstrip == 'SWITCHABLE'

          # make single phase illuminance sched for each state
          states = ['clear', 'tinted']
          states.each_index do |i|
            while merge_count > 1
              puts 'waiting in rmtxop queue...'
              sleep(5)
            end
            print_statement("Calculating annual iluminance for window group '#{wg}', state: #{states.index(states[i])} (switchable glazing - #{states[i]})", runner)
            ### foo (-ff)
            exec_statement("dctimestep output/dc/#{wg}_#{states[i]}.vmx annual-sky.mtx | rmtxop -fa -c 47.4 120 11.6 - > output/ts/#{wg}_#{states.index(states[i])}.ill", runner)
          end

        else

          wgXMLs = row.split(',')[4..]
          if wgXMLs.size > 2
            print_statement("WARN: Window Group #{wg} has #{wgXMLs.size} BSDFs (2 max supported by OpenStudio application).", runner)
          end

          wgXMLs.each_index do |i|
            # rad_command = "dctimestep output/dc/#{wg}.vmx bsdf/#{wgXMLs[i].strip} output/dc/#{wg}.dmx annual-sky.mtx | rmtxop -fa -c 47.4 120 11.6 - > output/ts/#{wg}_INDEX#{wgXMLs.index[i]}_#{wgXMLs[i].split[0]}.ill"
            while merge_count > 1
              puts 'waiting in rmtxop queue...'
              sleep(5)
            end
            print_statement("Calculating annual iluminance for window group '#{wg}', state: #{wgXMLs.index(wgXMLs[i])} (BSDF filename: '#{wgXMLs[i].split[0]}'):", runner)
            rad_command = "dctimestep output/dc/#{wg}.vmx bsdf/#{wgXMLs[i].strip} output/dc/#{wg}.dmx annual-sky.mtx | rmtxop -fa -c 47.4 120 11.6 - > output/ts/#{wg}_#{wgXMLs.index(wgXMLs[i])}.ill"
            ### orig ^^^
            # ##rad_command = "dctimestep output/dc/#{wg}.vmx bsdf/#{wgXMLs[i].strip} output/dc/#{wg}.dmx annual-sky.mtx | rmtxop -ff -c 47.4 120 11.6 - > output/ts/#{wg}_#{wgXMLs.index(wgXMLs[i])}.ill"
            exec_statement(rad_command, runner)
          end
        end
      end
    end

    if haveWG1 == 'True'

      # get annual values for window control sensors (note: convert to illuminance, no transpose, strip header)
      ### foo leave at -fa
      while merge_count > 1
        puts 'waiting in rmtxop queue...'
        sleep(5)
      end
      exec_statement('dctimestep output/dc/window_controls.vmx annual-sky.mtx | rmtxop -fa -c 47.4 120 11.6 - | getinfo - > output/ts/window_controls.ill', runner)
      print_statement('Blending window group results per shade control schedule', runner)

      # do that window group/state merge thing

      wg_index = 0

      print_statement('getting window shade control(s) values', runner) if debug_mode
      filename = 'output/ts/window_controls.ill'
      windowControls, _header = read_illuminance_file(filename, runner)
      print_statement("windowControls matrix is #{windowControls.row_count} rows x #{windowControls.column_count} columns", runner) if debug_mode

      windowGroups = File.open('bsdf/mapping.rad')
      windowGroups.each do |wg|
        next if wg[0] == '#'              # skip header

        windowGroup = wg.split(',')[0]
        next if windowGroup == 'WG0'      # skip unshaded windows

        wg_index += 1

        wgIllumFiles = Dir.glob("output/ts/#{windowGroup}_*.ill").sort

        shadeControlType = wg.split(',')[2].to_s
        shadeControlSetpointWatts = wg.split(',')[3].to_f
        shadeControlSetpoint = shadeControlSetpointWatts * 179 # Radiance's luminous efficacy factor
        wg_normal = wg.split(',')[1]
        wg_normal_x = wg_normal.split(' ')[0].to_f
        wg_normal_y = wg_normal.split(' ')[1].to_f
        wg_normal_z = wg_normal.split(' ')[2].to_f

        # DLM: hacktastic way to implement these options for now
        case shadeControlType
        when 'AlwaysOn'
          shadeControlSetpoint = -1000
        when 'AlwaysOff'
          shadeControlSetpoint = 10000000000
        end

        print_statement("Processing Window Group '#{windowGroup}', (exterior normal: '#{wg_normal_x * -1} #{wg_normal_y * -1} #{wg_normal_z * -1}', shade control setpoint: #{shadeControlSetpoint.round(0)} lux)", runner)

        ill0, header = read_illuminance_file(wgIllumFiles[0], runner)
        ill1, _header = read_illuminance_file(wgIllumFiles[1], runner)

        wgMerge = Matrix.build(ill0.row_count, ill0.column_count) { 0 }
        print_statement("wgmerge is #{wgMerge.row_count} rows x #{wgMerge.column_count} columns", runner) if debug_mode

        wgShadeSchedule = []
        print_statement("window group = '#{wg.split(',')[0]}', window controls matrix index = '#{wg_index - 1}'", runner) if debug_mode
        windowControls.row(wg_index - 1).each_with_index do |illuminance, row_index|
          window_illuminance = illuminance.to_f

          if window_illuminance < shadeControlSetpoint
            print_statement("E(#{windowGroup}) is #{window_illuminance.round(0)} lux at index #{row_index}: STATE=0 (up/clear)", runner) if debug_mode && row_index > 149 && row_index < 162 # print shade decisions for one day

            ill0.column(row_index).each_with_index do |value, column_index|
              wgMerge.send(:[]=, column_index, row_index, value)
            end

            wgShadeSchedule << "#{row_index},#{window_illuminance.round(0)},#{shadeControlSetpoint.round(0)},0\n"
          else
            print_statement("E(#{windowGroup}) is #{window_illuminance.round(0)} lux at index #{row_index}: STATE=1 (dn/tinted)", runner) if debug_mode && row_index > 149 && row_index < 162 # print shade decisions for one day

            ill1.column(row_index).each_with_index do |value, column_index|
              wgMerge.send(:[]=, column_index, row_index, value.to_f)
            end

            wgShadeSchedule << "#{row_index},#{window_illuminance.round(0)},#{shadeControlSetpoint.round(0)},1\n"
          end
        end

        wgIllum = File.open("output/ts/m_#{windowGroup}.ill", 'w')
        wgShade = File.open("output/ts/#{windowGroup}.shd", 'w')
        header.each { |head| wgIllum.print head.to_s }
        wgMerge.to_a.each { |array_ts| wgIllum.print " #{array_ts.join(' ')}\n" } # NOTE: leading space, for compatibility with default rfluxmtx output
        wgShadeSchedule.each { |sh| wgShade.print sh.to_s }
        wgIllum.close
        wgShade.close
        FileUtils.rm_f Dir.glob('*.tmp')
      end

    end

    # make whole-building illuminance file

    print_statement('Creating whole-building daylight results file...', runner)

    # get the controlled window group results (m_*.ill), if any
    mergeWindows = Dir.glob('output/ts/m_*.ill')
    if !mergeWindows.empty?
      print_statement("Gathering shade-controlled window group results (#{mergeWindows.size} total)", runner)
    else
      print_statement('INFO: Model has 0 controlled window groups', runner)
    end

    # get the uncontrolled windows results, if any
    if File.exist?('output/ts/WG0.ill')
      mergeWindows.insert(0, 'output/ts/WG0.ill')
    else
      print_statement('INFO: Model has no uncontrolled windows.', runner)
    end

    if mergeWindows.empty?
      print_statement('ERROR: no illuminance results.', runner)
      exit false
    elsif mergeWindows.size == 1
      # go straight to final building results file format
      while merge_count > 1
        puts 'waiting in rmtxop queue...'
        sleep(5)
      end
      print_statement('Finalizing output...', runner)
      exec_statement("rmtxop -fa #{mergeWindows[0]} -t | getinfo - > output/merged_space.ill", runner)
    else
      # make initial building results from first window group
      while merge_count > 1
        puts 'waiting in rmtxop queue...'
        sleep(5)
      end
      print_statement("Starting final building illumimance file with #{mergeWindows[0]}...", runner)
      exec_statement("rmtxop -fa #{mergeWindows[0]} -t > output/final_merge.tmp", runner)
      # add remaining groups, one at a time
      mergeWindows[1..].each do |merge|
        print_statement("adding #{merge}...", runner)
        temp_fname = rand(36**15).to_s(36)
        while merge_count > 1
          puts 'waiting in rmtxop queue...'
          sleep(5)
        end
        exec_statement("rmtxop -fa output/final_merge.tmp + #{merge} -t > #{temp_fname}", runner)
        FileUtils.mv temp_fname, 'output/final_merge.tmp', force: true
      end
      # strip header
      while merge_count > 1
        puts 'waiting in rmtxop queue...'
        sleep(5)
      end
      print_statement('Finalizing output...', runner)
      exec_statement('rmtxop -fa output/final_merge.tmp -t | getinfo - > output/merged_space.ill', runner)
      FileUtils.rm_f 'output/final_merge.tmp'
      print_statement('Done.', runner)
    end

    ## window merge end

    rawValues = parseResults(simulations, t_space_names_to_calculate, t_spaceWidths, t_spaceHeights, t_radGlareSensorViews, t_radPath, runner, debug_mode)

    dcVectors = nil

    # for each environment period (design days, annual, or arbitrary) you will create a directory for results
    t_sqlFile.availableEnvPeriods.each do |envPeriod|
      # DLM: all of these might be available directly from the EpwFile after Jason DeGraw's work
      diffHorizIllumAll, dirNormIllumAll, diffEfficacyAll, dirNormEfficacyAll, solarAltitudeAll, solarAzimuthAll, diffHorizUnits, dirNormUnits = getTimeSeries(t_sqlFile, envPeriod)

      # check that we have all timeseries
      if !diffHorizIllumAll || !dirNormIllumAll || !diffEfficacyAll || !dirNormEfficacyAll || !solarAltitudeAll || !solarAzimuthAll
        runner.registerError('Missing required timeseries')
        exit false
      end

      simDateTimes, simTimes, diffHorizIllum, dirNormIllum, diffEfficacy, dirNormEfficacy, solarAltitude, solarAzimuth, firstReportDateTime = \
        buildSimulationTimes(t_sqlFile, envPeriod, diffHorizIllumAll, dirNormIllumAll, diffEfficacyAll, dirNormEfficacyAll, solarAltitudeAll, solarAzimuthAll)

      simTimes.each_index do |i|
        datetime = simDateTimes[i]
        hours = ((datetime.date.dayOfYear - 1) * 24) + datetime.time.hours()
        values[i] = rawValues[hours]
      end
    end

    return values, dcVectors
  end

  # function renamed from execSimulation() to parseResults()
  def parseResults(t_cmds, t_space_names_to_calculate, t_spaceWidths, t_spaceHeights, t_radGlareSensorViews, t_radPath, runner, debug_mode)
    print_statement('Parsing daylighting results', runner)

    allValues = []
    values = []

    # read illuminance values from file
    values = []
    valuesFile = File.open("#{t_radPath}/output/merged_space.ill")
    valuesFile.each do |row|
      values << row.split(' ')
    end

    allhours = []

    # write out illuminance to individual space/map files
    8760.times do |hour|
      index = 0
      splitvalues = {}

      t_space_names_to_calculate.each do |space_name|
        space_size = t_spaceWidths[space_name] * t_spaceHeights[space_name]
        space = []
        illum = []
        glaresensors ||= {} # TODO: you can probably remove this
        glaresensors[space_name] ||= {}

        if !values.empty?
          subspace = values.slice(index, space_size)
          index += space_size

          print_statement("starting illuminance map for '#{space_name}'. space_size: #{space_size}, index is now at: #{index}, ", runner) if debug_mode && (hour == 0)

          space = []
          subspace.each do |subspacevalue|
            space << subspacevalue[hour].to_f.round(1)
          end

          if File.exist?("#{t_radPath}/numeric/#{space_name}.sns")
            if index >= values.size
              print_statement("Index is #{index} but values.size is only #{values.size}", runner)
            elsif hour >= values[index].size
              print_statement("Hour is #{hour} but values.size[index] is only #{values[index].size}", runner)
            end
            illum = [values[index][hour].to_f.round(1)]
            index += 1
            print_statement("finished space map and daylight sensor values, and index is now: #{index}", runner) if hour == 0 && debug_mode
          end

          # get ALL glare sensors for space
          if t_radGlareSensorViews[space_name] && !t_radGlareSensorViews[space_name].keys.empty?
            t_radGlareSensorViews[space_name].each do |sensor, views|
              sensor_index = t_radGlareSensorViews[space_name].keys.index(sensor)

              print_statement("glare sensor '#{sensor}' has #{views.size} views", runner) if hour == 0 && debug_mode

              views['view_definitions'].each_index do |view_index|
                print_statement("### DEBUG: index is #{index}; view_index is #{view_index}", runner) if hour == 0 && debug_mode
                t_radGlareSensorViews[space_name][sensor][hour] ||= {}
                t_radGlareSensorViews[space_name][sensor][hour]["#{sensor_index}_#{view_index}"] ||= {}
                view_values = values.slice(index, 1).first

                adjustedval = 0.00
                if view_values[hour].to_f != 0.00
                  adjustedval = [(0.0000622 * view_values[hour].to_f) + 0.184, 0].max.round(2)
                end
                t_radGlareSensorViews[space_name][sensor][hour]["#{sensor_index}_#{view_index}"]['dgp'] = adjustedval.round(2)
                t_radGlareSensorViews[space_name][sensor][hour]["#{sensor_index}_#{view_index}"]['raw'] = view_values[hour].to_f.round(2)

                index += 1
              end
            end
          end
        else
          print_statement("An error has occurred; no results for space '#{space_name}'.", runner)
          space = Array.new(space_size, 0)

          if File.exist?("#{t_radPath}/numeric/#{space_name}.sns")
            illum = Array.new(1, 0)
          end

        end

        # make an array that will have all the views
        splitvalues[space_name] = [space, illum]
        # iterate over each sensor and combine the views together
        new_hash = {}

        t_radGlareSensorViews[space_name]&.each do |sensor, v|
          new_hash[sensor] = v[hour]
        end
        splitvalues[space_name] += [new_hash]
      end

      allhours[hour] = splitvalues
    end

    allhours

    File.open('output/glare.json', 'w') { |f| f << JSON.pretty_generate(t_radGlareSensorViews) }
    File.open('output/radout.json', 'w') { |f| f << JSON.pretty_generate(all_hours: allhours) }

    print_statement('Returning annual results', runner)
    return allhours
  end

  def getTimeSeries(t_sqlFile, t_envPeriod)
    diffHorizIllumAll = []; dirNormIllumAll = []
    diffEfficacyAll = []; dirNormEfficacyAll = []
    solarAltitudeAll = []; solarAzimuthAll = []
    diffHorizUnits = nil; dirNormUnits = nil

    # get the solar data
    t_sqlFile.timeSeries(t_envPeriod, 'Hourly', 'Site Exterior Horizontal Sky Illuminance').each do |timeseries|
      diffHorizIllumAll = timeseries.values
      diffHorizUnits = timeseries.units if !diffHorizUnits
    end
    t_sqlFile.timeSeries(t_envPeriod, 'Hourly', 'Site Exterior Beam Normal Illuminance').each do |timeseries|
      dirNormIllumAll = timeseries.values
      dirNormUnits = timeseries.units if !dirNormUnits
    end
    t_sqlFile.timeSeries(t_envPeriod, 'Hourly', 'Site Sky Diffuse Solar Radiation Luminous Efficacy').each do |timeseries|
      diffEfficacyAll = timeseries.values
      diffEfficacyUnits = timeseries.units if !diffEfficacyUnits
    end
    t_sqlFile.timeSeries(t_envPeriod, 'Hourly', 'Site Beam Solar Radiation Luminous Efficacy').each do |timeseries|
      dirNormEfficacyAll = timeseries.values
      dirNormEfficacyUnits = timeseries.units if !dirNormEfficacyUnits
    end
    t_sqlFile.timeSeries(t_envPeriod, 'Hourly', 'Site Solar Altitude Angle').each do |timeseries|
      solarAltitudeAll = timeseries.values
      solarAltitudeUnits = timeseries.units if !solarAltitudeUnits
    end
    t_sqlFile.timeSeries(t_envPeriod, 'Hourly', 'Site Solar Azimuth Angle').each do |timeseries|
      solarAzimuthAll = timeseries.values
      solarAzimuthUnits = timeseries.units if !solarAzimuthUnits
    end

    return diffHorizIllumAll, dirNormIllumAll, diffEfficacyAll, dirNormEfficacyAll, solarAltitudeAll, solarAzimuthAll, diffHorizUnits, dirNormUnits
  end

  def buildSimulationTimes(t_sqlFile, t_envPeriod, t_diffHorizIllumAll, t_dirNormIllumAll, t_diffEfficacyAll, t_dirNormEfficacyAll, t_solarAltitudeAll, t_solarAzimuthAll)
    # we want simulation at these indices only
    simDateTimes = OpenStudio::DateTimeVector.new
    simTimes = []
    diffHorizIllum = []
    dirNormIllum = []
    diffEfficacy = []
    dirNormEfficacy = []
    solarAltitude = []
    solarAzimuth = []
    firstReportDateTime = nil

    t_sqlFile.timeSeries(t_envPeriod, 'Hourly', 'Site Exterior Horizontal Sky Illuminance').each do |timeseries|
      firstReportDateTime = timeseries.firstReportDateTime
      daysFromFirstReport = timeseries.daysFromFirstReport
      (0...daysFromFirstReport.size).each do |i|
        dateTime = firstReportDateTime + OpenStudio::Time.new(daysFromFirstReport[i]) #   - 0.5/24.0 subtract 1/2 hr to center of interval
        if dateTime.time.seconds == 59
          # rounding error, let's help
          dateTime += OpenStudio::Time.new(0, 0, 0, 1)
        end

        if dateTime.time.seconds == 1
          # rounding error, let's help
          dateTime -= OpenStudio::Time.new(0, 0, 0, 1)
        end

        simTimes << "#{dateTime.date.monthOfYear.value} #{dateTime.date.dayOfMonth} #{dateTime.time}"
        simDateTimes << dateTime
        diffHorizIllum << t_diffHorizIllumAll[i]
        dirNormIllum << t_dirNormIllumAll[i]
        diffEfficacy << t_diffEfficacyAll[i]
        dirNormEfficacy << t_dirNormEfficacyAll[i]
        solarAltitude << t_solarAltitudeAll[i]
        solarAzimuth << t_solarAzimuthAll[i]
      end
    end

    return simDateTimes, simTimes, diffHorizIllum, dirNormIllum, diffEfficacy, dirNormEfficacy, solarAltitude, solarAzimuth, firstReportDateTime
  end

  def writeTimeSeriesToSql(sqlfile, simDateTimes, illum, space_name, ts_name, ts_units)
    data = OpenStudio::Vector.new(illum.length)
    illum.length.times do |n|
      data[n] = illum[n].to_f
    rescue Exception => e
      print_statement("Error inserting data: #{illum[n]} inserting 0 instead", runner)
      data[n] = 0
    end

    illumTS = OpenStudio::TimeSeries.new(simDateTimes, data, ts_units)
    sqlfile.insertTimeSeriesData(
      'Average', 'Zone', 'Zone', space_name, ts_name, OpenStudio::ReportingFrequency.new('Hourly'),
      OpenStudio::OptionalString.new,
      ts_units, illumTS
    )
  end

  def annualSimulation(t_sqlFile, t_epwFile, t_space_names_to_calculate, t_radMaps, t_spaceWidths, t_spaceHeights, t_radMapPoints, \
                       t_radGlareSensorViews, t_simCores, t_site_latitude, t_site_longitude, t_site_stdmeridian, t_outPath, t_building, t_values, t_dcVectors, runner)

    sqlOutPath = OpenStudio::Path.new("#{Dir.pwd}/output/radout.sql")
    if OpenStudio.exists(sqlOutPath)
      OpenStudio.remove(sqlOutPath)
    end

    # for each environment period (design days, annual, or arbitrary) you will create a directory for results
    t_sqlFile.availableEnvPeriods.each do |envPeriod|
      print_statement("envPeriod = '#{envPeriod}'", runner)

      diffHorizIllumAll, dirNormIllumAll, diffEfficacyAll, dirNormEfficacyAll, solarAltitudeAll, solarAzimuthAll, diffHorizUnits, dirNormUnits = getTimeSeries(t_sqlFile, envPeriod)

      # check that we have all timeseries
      if !diffHorizIllumAll || !dirNormIllumAll || !diffEfficacyAll || !dirNormEfficacyAll || !solarAltitudeAll || !solarAzimuthAll
        runner.registerError('Missing required timeseries')
        exit false
      end

      # make timeseries
      simDateTimes, simTimes, diffHorizIllum, dirNormIllum, diffEfficacy, dirNormEfficacy, solarAltitude, solarAzimuth, firstReportDateTime = \
        buildSimulationTimes(t_sqlFile, envPeriod, diffHorizIllumAll, dirNormIllumAll, diffEfficacyAll, dirNormEfficacyAll, solarAltitudeAll, solarAzimuthAll)

      sqlOutFile = OpenStudio::SqlFile.new(sqlOutPath,
                                           t_epwFile,
                                           OpenStudio::DateTime.now,
                                           OpenStudio::Calendar.new(firstReportDateTime.date.year))

      sqlOutFile.removeIndexes

      t_space_names_to_calculate.each do |space_name|
        illuminanceMatrixMaps = OpenStudio::MatrixVector.new
        daylightSensorIlluminance = []
        meanIlluminanceMap = []
        minDGP = []
        meanDGP = []
        maxDGP = []

        print_statement("Processing space '#{space_name}'", runner)

        timeSeriesIllum = []
        timeSeriesGlare = []

        simTimes.each_index do |i|
          spaceWidth = t_spaceWidths[space_name]
          spaceHeight = t_spaceHeights[space_name]

          illuminanceMatrixMaps << OpenStudio::Matrix.new(spaceWidth, spaceHeight, 0)
          daylightSensorIlluminance << 0
          meanIlluminanceMap << 0
          minDGP << 0
          meanDGP << 0
          maxDGP << 0

          # these must be declared in the thread otherwise will get overwritten on each loop
          tsDateTime = simTimes[i]

          # Split up values by space

          illumValues, illumSensorValues, glareSensorValues = t_values[i][space_name]

          # Debug
          # File.open('glareSensorValues.out', 'w') { |f| f.write(glareSensorValues.to_s) }

          timeSeriesIllum[i] = "#{tsDateTime.to_s.tr(' ', ',')},#{dirNormIllum[i]},#{diffHorizIllum[i]},#{illumSensorValues.join(',')},#{illumValues.join(',')}"

          # add glare sensor values
          if t_radGlareSensorViews[space_name] && !glareSensorValues.nil?
            timeSeriesGlare[i] = tsDateTime.to_s.tr(' ', ',')
            glareSensorValues.each_key do |key|
              glare_values = glareSensorValues[key].map { |_, v| v['dgp'] }
              timeSeriesGlare[i] += ",#{key},#{glare_values.average.round(2)},#{glare_values.min.round(2)},#{glare_values.max.round(2)},raw,#{glare_values.join(',')}"
            end
          end

          m = OpenStudio::Matrix.new(spaceWidth, spaceHeight, 0)

          if !illumSensorValues.empty?
            daylightSensorIlluminance[i] = illumSensorValues[0]
          end

          n = 0
          sumIllumMap = 0
          illumValues.each do |val|
            x = (n % spaceWidth).to_i
            y = (n / spaceWidth).to_i
            sumIllumMap += val.to_f
            m[x, y] = val.to_f
            n += 1
          end

          illuminanceMatrixMaps[i] = m

          if n != 0
            meanIlluminanceMap[i] = sumIllumMap / n.to_f
          end
        end

        # Write results

        FileUtils.mkdir_p("#{Dir.pwd}/output/ts/#{space_name}/maps") unless File.exist?("#{Dir.pwd}/output/ts/#{space_name}/maps")
        f = File.open("#{Dir.pwd}/output/ts/#{space_name}/maps/#{space_name}_map.ill", 'w')
        space = nil
        t_building.spaces.each do |s|
          this_name = s.name.get.tr(' ', '_').tr(':', '_')
          if this_name == space_name
            space = s
            break
          end
        end

        illuminanceMaps = space.illuminanceMaps

        # TODO: use all if not empty
        if !illuminanceMaps.empty?

          map = illuminanceMaps[0]

          xmin = map.originXCoordinate
          xmax = xmin + map.xLength
          nx = map.numberofXGridPoints
          ymin = map.originYCoordinate
          ymax = ymin + map.yLength
          ny = map.numberofYGridPoints
          z = map.originZCoordinate

          xSpacing = (xmax - xmin) / nx
          ySpacing = (ymax - ymin) / ny

          print_statement('Writing Radiance results file', runner)

          # illuminance to csv

          f.print "## OpenStudio Daylight Simulation Results file\n"
          f.print "## Header: xmin ymin z xmax ymin z xmax ymax z xspacing yspacing\n"
          f.print "## Data: month,day,time,directNormalIllumimance(external),diffuseHorizontalIlluminance(external),daylightSensorIlluminance,pointIlluminance [lux]\n"
          f.print "#{xmin} #{ymin} #{z} #{xmax} #{ymin} #{z} #{xmax} #{ymax} #{z} #{xSpacing} #{ySpacing}\n"
          timeSeriesIllum.each { |ts| f.print "#{ts}\n" }
          f.close

          # glare to csv

          FileUtils.mkdir_p("#{Dir.pwd}/output/ts/#{space_name}/maps") unless File.exist?("#{Dir.pwd}/output/ts/#{space_name}/maps")
          f = File.open("#{Dir.pwd}/output/ts/#{space_name}/maps/#{space_name}.glr", 'w')
          space = nil
          t_building.spaces.each do |s|
            this_name = s.name.get.tr(' ', '_').tr(':', '_')
            if this_name == space_name
              space = s
              break
            end
          end

          if t_radGlareSensorViews[space_name]
            f.print "## OpenStudio Daylight Simulation (glare) Results file\n"
            f.print "## Space name: '#{space_name}\n"
            f.print "## Data: month,day,time,sensor_name,DGPs(avg),DGPs(min),DGPs(max),raw,[raw values]...\n"
            timeSeriesGlare.each { |ts| f.print "#{ts}\n" }
            f.close
          end

          # all results to sql

          print_statement('Writing Radiance results database', runner)

          writeTimeSeriesToSql(sqlOutFile, simDateTimes, dirNormIllum, space_name, 'Direct Normal Illuminance', 'lux')
          writeTimeSeriesToSql(sqlOutFile, simDateTimes, diffHorizIllum, space_name, 'Global Horizontal Illuminance', 'lux')
          writeTimeSeriesToSql(sqlOutFile, simDateTimes, daylightSensorIlluminance, space_name, 'Daylight Sensor Illuminance', 'lux')
          writeTimeSeriesToSql(sqlOutFile, simDateTimes, meanIlluminanceMap, space_name, 'Mean Illuminance Map', 'lux')

          # I really have no idea how to populate these fields
          sqlOutFile.insertZone(space_name,
                                0,
                                0, 0, 0,
                                0, 0, 0,
                                0,
                                0,
                                0,
                                0, 0,
                                0, 0,
                                0, 0,
                                0,
                                0,
                                0,
                                0,
                                0,
                                0,
                                0,
                                0,
                                true)

          xs = OpenStudio::DoubleVector.new

          nx.times do |n|
            xs << xmin + (n * xSpacing)
          end

          ys = OpenStudio::DoubleVector.new

          ny.times do |n|
            ys << ymin + (n * ySpacing)
          end

          sqlOutFile.insertIlluminanceMap(space_name, "#{space_name} DAYLIGHT MAP", t_epwFile.wmoNumber,
                                          simDateTimes, xs, ys, map.originZCoordinate,
                                          illuminanceMatrixMaps)

        end
      end

      sqlOutFile.createIndexes

      sqlOutFile.close
    end
  end

  # makeSchedules()

  # write new lighting power schedules for the model

  def makeSchedules(model, sqlFile, runner)
    print_statement('Updating lighting load schedules', runner)

    # only run period in pre process job
    environmentName = 'Run Period 1'

    # loop through each thermal zone
    model.getThermalZones.each do |thermalZone|
      spaces = thermalZone.spaces

      if spaces.empty?
        print_statement("ThermalZone '#{thermalZone.name}' has no spaces, skipping.", runner)
        next
      end

      # get people schedule for zone
      # TODO: require people for occupancy controls
      peopleTimeseries = sqlFile.timeSeries('Run Period 1'.upcase, 'Hourly', 'Zone People Occupant Count', thermalZone.name.get.upcase)

      if peopleTimeseries.empty?
        print_statement("Cannot find timeseries 'Zone People Occupant Count' for ThermalZone '#{thermalZone.name}'.", runner)
      end

      # get lights schedule for zone
      lightsTimeseries = sqlFile.timeSeries('Run Period 1'.upcase, 'Hourly', 'Zone Lights Electricity Rate', thermalZone.name.get.upcase)

      if lightsTimeseries.empty?
        newname = thermalZone.name.get.sub(/^OS:/, '')
        print_statement("Cannot find timeseries 'Zone Lights Electricity Rate' for ThermalZone '#{thermalZone.name}', skipping.", runner)
        next
      end

      lightsTimeseries = lightsTimeseries.get

      # get illuminance map
      illuminanceMap = thermalZone.illuminanceMap

      if illuminanceMap.empty?
        print_statement("Cannot find IlluminanceMap for ThermalZone '#{thermalZone.name}', skipping.", runner)
        next
      end

      illuminanceMap = illuminanceMap.get

      # get the space
      space = illuminanceMap.space

      if space.empty?
        print_statement("Cannot find Space for IlluminanceMap '#{illuminanceMap.name}' in ThermalZone '#{thermalZone.name}', skipping.", runner)
        next
      end

      space = space.get

      space_name = space.name.get.tr(' ', '_').tr(':', '_')

      radSqlPath = OpenStudio::Path.new('output/radout.sql')

      # load the illuminance map
      # assume this will be reported in 1 hour timesteps starting on 1/1
      averageIlluminances = []
      radSqlFile = OpenStudio::SqlFile.new(radSqlPath)

      # use the daylight sensor input
      spacename = space.name.get.tr(' ', '_').tr(':', '_')
      envPeriods = radSqlFile.availableEnvPeriods

      if envPeriods.empty?
        print_statement('No available environment periods in radiance sql file, skipping', runner)
        next
      end

      daylightSensor = radSqlFile.timeSeries(envPeriods[0], 'Hourly', 'Daylight Sensor Illuminance', space_name)

      if daylightSensor.empty?
        print_statement('Daylight sensor data could not be loaded, skipping', runner)
        next
      end

      values = daylightSensor.get.values

      values.length.times do |i|
        val = values[i]

        if val < 0
          val = 0
        end
        averageIlluminances << val
        # end
      end

      daylightSetpoint = 0.0

      primaryDaylightingControl = thermalZone.primaryDaylightingControl
      if !primaryDaylightingControl.empty?
        daylightSetpoint = primaryDaylightingControl.get.illuminanceSetpoint
      end

      secondaryDaylightingControl = thermalZone.secondaryDaylightingControl
      if !secondaryDaylightingControl.empty?
        if daylightSetpoint == 0.0
          daylightSetpoint = secondaryDaylightingControl.get.illuminanceSetpoint
        else
          print_statement("Ignoring secondary daylighting control in ThermalZone '#{thermalZone.name}'", runner)
        end
      end

      if daylightSetpoint == 0.0
        space.daylightingControls.each do |i|
          daylightSetpoint = i.illuminanceSetpoint
          if daylightSetpoint != 0.0
            break
          end
        end
      end

      if daylightSetpoint == 0.0
        print_statement("Illuminance setpoint is not defined in Space '#{space.name}' or in ThermalZone '#{thermalZone.name}', skipping.", runner)
        next
      end

      print_statement("ThermalZone '#{thermalZone.name}' illuminance setpoint is: #{daylightSetpoint.round(0)} lux", runner)

      originalLightsValues = lightsTimeseries.values
      lightsValues = OpenStudio::Vector.new(averageIlluminances.size)
      averageIlluminances.each_index do |i|
        dimmingResponse = [(daylightSetpoint - averageIlluminances[i]) / daylightSetpoint, 0].max
        lightsValues[i] = dimmingResponse * originalLightsValues[i]
      end

      # get max lighting power
      lightingLevel = OpenStudio.maximum(lightsValues)

      if lightingLevel <= 0.0
        print_statement("Thermal Zone '#{thermalZone.name}' lighting power is less than or equal to 0, skipping", runner)
        next
      end

      print_statement("Thermal Zone '#{thermalZone.name}' lighting power is: #{lightingLevel.round(0)} W", runner)

      # normalize lights values
      averageIlluminances.each_index do |i|
        lightsValues[i] = lightsValues[i] / lightingLevel
      end

      startDate = OpenStudio::Date.new(OpenStudio::MonthOfYear.new(1), 1)
      interval = OpenStudio::Time.new(0, 1, 0)
      timeseries = OpenStudio::TimeSeries.new(startDate, interval, lightsValues, 'W')

      schedule = OpenStudio::Model::ScheduleInterval.fromTimeSeries(timeseries, model)

      if schedule.empty?
        print_statement("Could not create modified lighting schedule for Thermal Zone '#{thermalZone.name}', skipping", runner)
        next
      end

      schedule = schedule.get

      schedule.setName("#{thermalZone.name.get} Lights Schedule")

      # remove all lights in this zone
      spaces.each do |space|
        space.hardApplySpaceType(true)
        space.lights.each(&:remove)
        space.luminaires.each(&:remove)
      end

      # add a new lights object to first space in this zone and set schedule
      lightsDefinition = OpenStudio::Model::LightsDefinition.new(model)
      lightsDefinition.setLightingLevel(lightingLevel)

      lights = OpenStudio::Model::Lights.new(lightsDefinition)
      lights.setSchedule(schedule)
      lights.setSpace(spaces[0])
    end
  end

  def daylightMetrics(model, sqlFile, runner)
    # load the Radiance output data

    radoutPath = OpenStudio::Path.new('output/radout.sql')
    radoutPath = OpenStudio.system_complete(radoutPath)

    radoutFile = OpenStudio::SqlFile.new(radoutPath)
    if !sqlFile.connectionOpen
      print_statement("SqlFile #{sqlPath} connection is not open", runner)
      return false
    end

    $METHOD = 1

    # get exterior illuminance timeseries from E+ run
    exteriorIlluminanceTimeseries = sqlFile.timeSeries('Run Period 1'.upcase, 'Hourly', 'Site Exterior Horizontal Sky Illuminance')

    # summary report string
    summary_report = ''

    building_average_space = []
    building_average = ''
    building_average_space_cda = []
    building_average_cda = ''
    building_average_space_udi = []
    building_average_udi = ''

    # loop through all the spaces
    # building = model.getBuilding
    # building.spaces.each do |space|

    daylightAnalysisSpaces = []
    spaces = model.getSpaces
    spaces.each do |sp|
      if !sp.illuminanceMaps.empty?
        daylightAnalysisSpaces << sp
      end
    end

    daylightAnalysisSpaces.each do |space|
      space_name = space.name.get.tr(' ', '_').tr(':', '_')

      thermalZone = space.thermalZone
      next if thermalZone.empty?

      thermalZone = thermalZone.get

      map_name = "#{space_name} DAYLIGHT MAP"
      map_index = radoutFile.illuminanceMapIndex(map_name)
      next if map_index.empty?

      daylightSetpoint = nil
      primaryDaylightingControl = thermalZone.primaryDaylightingControl
      if primaryDaylightingControl.empty?
        print_statement("Thermal Zone \"#{thermalZone}\" has no primary daylighting control, skipping", runner)
        next
      else
        daylightSetpoint = primaryDaylightingControl.get.illuminanceSetpoint
      end

      print_statement("Calculating Daylight Metrics for Space '#{space_name}'", runner)

      da_daylit = []
      da_occupied = []
      da_daylit_occupied = []
      cda_daylit = []
      cda_occupied = []
      cda_daylit_occupied = []
      udi_daylit = []
      udi_occupied = []
      udi_daylit_occupied = []
      sda_credit = []

      # get people timeseries from E+ run for this zone
      peopleTimeseries = sqlFile.timeSeries('Run Period 1'.upcase, 'Hourly', 'Zone People Occupant Count', thermalZone.name.get.upcase)
      runner.registerWarning("No people schedule for space '#{space_name}'. Occupancy-based daylight metrics not calculated.") if peopleTimeseries.empty?

      # loop over all timesteps, return type is std::vector< std::pair<int, DateTime> >
      hourly_report_indices_dates = radoutFile.illuminanceMapHourlyReportIndicesDates(map_name)
      hourly_report_indices_dates.each do |hourly_report_index_date|
        # initialize metrics to nil for timestep
        da_daylit << nil
        da_occupied << nil
        da_daylit_occupied << nil
        cda_daylit << nil
        cda_occupied << nil
        cda_daylit_occupied << nil
        udi_daylit << nil
        udi_occupied << nil
        udi_daylit_occupied << nil
        sda_credit << nil

        # extract timestep and index
        hourly_report_index = hourly_report_index_date.first
        hourly_report_date = hourly_report_index_date.second

        # SDA credit hour? (using 8:00-17:00 as the qualifying range, to comply with the 10 hour/day, 3,650 annual hours expectation)
        sda_hour = false
        if hourly_report_date.to_s.split(' ')[1].split(':')[0].to_i >= 8 && hourly_report_date.to_s.split(' ')[1].split(':')[0].to_i <= 17
          sda_hour = true
        end

        # daylit hour?
        daylit_hour = false
        if !exteriorIlluminanceTimeseries.empty?
          val = exteriorIlluminanceTimeseries[0].value(hourly_report_date)
          if val > 0
            daylit_hour = true
          end
        end

        # occupied hour?
        occupied_hour = false
        if !peopleTimeseries.empty?
          val = peopleTimeseries.get.value(hourly_report_date)
          if val > 0
            occupied_hour = true
          end
        end

        da = 0

        case $METHOD
        when 0

          # get map values
          map_values = radoutFile.illuminanceMap(hourly_report_index)

          # compute number of map points with illuminance greater than setpoint
          size1 = map_values.size1
          size2 = map_values.size2
          num = size1 * size2
          num_da = 0
          for i in (0...size1)
            for j in (0...size2)
              map_value = map_values[i, j]
              if map_value >= daylightSetpoint
                num_da += 1
              end
            end
          end

          da = num_da.to_f / num

        when 1

          x = OpenStudio::DoubleVector.new
          y = OpenStudio::DoubleVector.new
          map_values = OpenStudio::DoubleVector.new

          radoutFile.illuminanceMap(hourly_report_index, x, y, map_values)

          # compute DA, conDA, UDI, and SDA
          num = map_values.size
          num_da = 0
          num_cda = 0
          num_udi = 0
          num_sda = 0
          map_values.each do |map_value|
            if map_value >= daylightSetpoint
              num_da += 1
              num_cda += 1
            end
            if map_value > 0 && map_value < daylightSetpoint
              num_cda += map_value / daylightSetpoint
            end
            if map_value >= 100 && map_value <= 3000
              num_udi += 1
            end
            if map_value >= 300 && sda_hour == true
              num_sda += 1
            end
          end

          da = num_da.to_f / num
          cda = num_cda.to_f / num
          udi = num_udi.to_f / num
          sda = num_sda.to_f / num

        end

        # assign to timeseries
        if daylit_hour
          da_daylit[-1] = da
          cda_daylit[-1] = cda
          udi_daylit[-1] = udi
        end

        if occupied_hour
          da_occupied[-1] = da
          cda_occupied[-1] = cda
          udi_occupied[-1] = udi
        end

        if daylit_hour && occupied_hour
          da_daylit_occupied[-1] = da
          cda_daylit_occupied[-1] = cda
          udi_daylit_occupied[-1] = udi
        end

        if sda_hour
          sda_credit[-1] = sda
        end
      end

      # compute annual metrics for space

      # Daylight Autonomy
      da_daylit_sum = 0
      da_daylit_num = 0
      da_daylit.each do |da|
        if !da.nil?
          da_daylit_sum += da
          da_daylit_num += 1
        end
      end
      annual_da_daylit = da_daylit_sum.to_f / da_daylit_num
      summary_report += "#{space_name},DA(#{daylightSetpoint.round(0)}),Daylit Hours,#{annual_da_daylit.round(2)},#{da_daylit_sum.round(0)},#{da_daylit_num}\n"
      if !peopleTimeseries.empty?
        da_occupied_sum = 0
        da_occupied_num = 0
        da_occupied.each do |da|
          if !da.nil?
            da_occupied_sum += da
            da_occupied_num += 1
          end
        end
        # annual_da_occupied = (da_occupied_num == 0.0 || da_occupied_sum == 0.0) ? 0.0 : da_occupied_sum.to_f / da_occupied_num.to_f
        annual_da_occupied = da_occupied_sum.to_f / da_occupied_num
        summary_report += "#{space_name},DA(#{daylightSetpoint.round(0)}),Occupied Hours,#{annual_da_occupied.round(2)},#{da_occupied_sum.round(0)},#{da_occupied_num}\n"

        da_daylit_occupied_sum = 0
        da_daylit_occupied_num = 0
        da_daylit_occupied.each do |da|
          if !da.nil?
            da_daylit_occupied_sum += da
            da_daylit_occupied_num += 1
          end
        end
        # annual_da_daylit_occupied = (da_daylit_occupied_num == 0.0 || da_daylit_occupied_sum == 0.0) ? 0.0 : da_daylit_occupied_sum.to_f / da_daylit_occupied_num.to_f
        annual_da_daylit_occupied = da_daylit_occupied_sum.to_f / da_daylit_occupied_num
        summary_report += "#{space_name},DA(#{daylightSetpoint.round(0)}),Daylit and Occupied Hours,#{annual_da_daylit_occupied.round(2)},#{da_daylit_occupied_sum.round(0)},#{da_daylit_occupied_num}\n"
      end

      # Continuous Daylight Autonomy
      cda_daylit_sum = 0
      cda_daylit_num = 0
      cda_daylit.each do |cda|
        if !cda.nil?
          cda_daylit_sum += cda
          cda_daylit_num += 1
        end
      end
      annual_cda_daylit = cda_daylit_sum.to_f / cda_daylit_num
      summary_report += "#{space_name},conDA(#{daylightSetpoint.round(0)}),Daylit Hours,#{annual_cda_daylit.round(2)},#{cda_daylit_sum.round(0)},#{cda_daylit_num}\n"

      if !peopleTimeseries.empty?
        cda_occupied_sum = 0
        cda_occupied_num = 0
        cda_occupied.each do |cda|
          if !cda.nil?
            cda_occupied_sum += cda
            cda_occupied_num += 1
          end
        end
        annual_cda_occupied = cda_occupied_sum.to_f / cda_occupied_num
        summary_report += "#{space_name},conDA(#{daylightSetpoint.round(0)}),Occupied Hours,#{annual_cda_occupied.round(2)},#{cda_occupied_sum.round(0)},#{cda_occupied_num}\n"

        cda_daylit_occupied_sum = 0
        cda_daylit_occupied_num = 0
        cda_daylit_occupied.each do |cda|
          if !cda.nil?
            cda_daylit_occupied_sum += cda
            cda_daylit_occupied_num += 1
          end
        end
        annual_cda_daylit_occupied = cda_daylit_occupied_sum.to_f / cda_daylit_occupied_num
        summary_report += "#{space_name},conDA(#{daylightSetpoint.round(0)}),Daylit and Occupied Hours,#{annual_cda_daylit_occupied.round(2)},#{cda_daylit_occupied_sum.round(0)},#{cda_daylit_occupied_num}\n"
      end

      # Useful Daylight Illuminance
      udi_daylit_sum = 0
      udi_daylit_num = 0
      udi_daylit.each do |udi|
        if !udi.nil?
          udi_daylit_sum += udi
          udi_daylit_num += 1
        end
      end
      annual_udi_daylit = udi_daylit_sum.to_f / udi_daylit_num
      summary_report += "#{space_name},UDI(100-3000),Daylit Hours,#{annual_udi_daylit.round(2)},#{udi_daylit_sum.round(0)},#{udi_daylit_num}\n"
      if !peopleTimeseries.empty?
        udi_occupied_sum = 0
        udi_occupied_num = 0
        udi_occupied.each do |udi|
          if !udi.nil?
            udi_occupied_sum += udi
            udi_occupied_num += 1
          end
        end
        annual_udi_occupied = udi_occupied_sum.to_f / udi_occupied_num
        summary_report += "#{space_name},UDI(100-3000),Occupied Hours,#{annual_udi_occupied.round(2)},#{udi_occupied_sum.round(0)},#{udi_occupied_num}\n"

        udi_daylit_occupied_sum = 0
        udi_daylit_occupied_num = 0
        udi_daylit_occupied.each do |udi|
          if !udi.nil?
            udi_daylit_occupied_sum += udi
            udi_daylit_occupied_num += 1
          end
        end
        annual_udi_daylit_occupied = udi_daylit_occupied_sum.to_f / udi_daylit_occupied_num
        summary_report += "#{space_name},UDI(100-3000),Daylit and Occupied Hours,#{annual_udi_daylit_occupied.round(2)},#{cda_daylit_occupied_sum.round(0)},#{cda_daylit_occupied_num}\n"
      end

      # Spatial Daylight Autonomy (FWIW)
      sda_sum = 0
      sda_num = 0
      sda_credit.each do |sda|
        if !sda.nil?
          sda_sum += sda
          sda_num += 1
        end
      end
      annual_sda = sda_sum.to_f / sda_num
      summary_report += "#{space_name},sDA(300),8AM-5PM (10 hours/day per IESNA LM-83-12),#{annual_sda.round(2)},#{sda_sum.round(0)},#{sda_num}\n"

      # Make building average metrics

      # DA
      building_average_space << annual_da_daylit
      # cDA
      building_average_space_cda << annual_cda_daylit
      # UDI
      building_average_space_udi << annual_udi_daylit
    end

    # DLM: can we make some more metrics that are area weighted rather than just space weighted?
    building_average_space_sum = 0.0
    building_average_space.each { |e| building_average_space_sum += e }
    building_average_space_cda_sum = 0.0
    building_average_space_cda.each { |e| building_average_space_cda_sum += e }
    building_average_space_udi_sum = 0.0
    building_average_space_udi.each { |e| building_average_space_udi_sum += e }

    # catch zero condition
    if building_average_space_sum == 0.0
      building_average = 0.0
      building_average_cda = 0.0
      building_average_udi = 0.0
      print_statement('Warning: Daylight Autonomy for building is zero, check daylighting control point(s) setup.', runner)
    else
      building_average = building_average_space_sum / building_average_space.length
      runner.registerValue('DA', building_average.round(2))
      building_average_cda = building_average_space_cda_sum / building_average_space_cda.length
      runner.registerValue('cDA', building_average_cda.round(2))
      building_average_udi = building_average_space_udi_sum / building_average_space_udi.length
      runner.registerValue('UDI', building_average_udi.round(2))
    end

    File.open('output/daylight_metrics.csv', 'w') do |file|
      file.puts '# OpenStudio Daylight Metrics Report'
      file.puts "# Building average daylight metrics (daylit spaces): DA = #{building_average.round(2)} cDA = #{building_average_cda.round(2)} UDI = #{building_average_udi.round(2)}"
      file.puts '#[space_name],[metric(setpoint)],[input_hours_range],[metric_value],[hours_met],[input_hours]'
      file.puts summary_report
    end
  end

  def genImages(radPath, runner, site_latitude, site_longitude, site_meridian, catCommand, debug_mode)
    print_statement('Generating images', runner)

    # generate some equinox skies for renderings, so we have them. (Sol voce)
    ['09', '12', '15'].each do |hour|
      exec_statement("gensky 03 21 #{hour} -a #{site_latitude} -o #{site_longitude} -m #{site_meridian} +s > skies/render_sky_input", runner)
      File.open('skies/render_sky_skyfuncs', 'w') do |file|
        # blue sky
        file.puts "skyfunc glow skyglow\n0\n0\n4 0.900 0.900 1.150 0\n\n"
        file.puts "skyglow source sky\n0\n0\n4 0 0 1 180\n\n"
        # brown ground
        file.puts "skyfunc glow groundglow\n0\n0\n4 1.400 0.900 0.600 0\n\n"
        file.puts "groundglow source ground\n0\n0\n4 0 0 -1 180\n\n"
      end
      exec_statement("#{catCommand} skies/render_sky_input skies/render_sky_skyfuncs > skies/site_0321_#{hour}.sky", runner)
    end

    image_hour = '09' # preset hour
    # make octree
    rad_command = "oconv materials/materials.rad model.rad skies/site_0321_#{image_hour}.sky > octrees/images.oct"
    exec_statement(rad_command, runner)

    # do views

    # daylighting control views, unfiltered
    views_daylighting_control = Dir.glob('views/*_dc.vfh')
    views_daylighting_control.each do |dc|
      rad_command = "rpict -av .3 .3 .3 -ab 1 -vf #{dc} octrees/images.oct | ra_bmp - #{dc}_#{image_hour}.bmp"
      exec_statement(rad_command, runner)

      if debug_mode

        # do "debug" images (individual window groups)
        debug_images = Dir.glob('octrees/debug*.oct')
        debug_images.each do |debug|
          condition = debug.split('/')[1].split('.')[0]
          exec_statement("oconv -i #{debug} skies/site_0321_#{image_hour}.sky > octrees/debug_temp.oct", runner)
          exec_statement("rpict -av .3 .3 .3 -ab 1 -vf #{dc} octrees/debug_temp.oct | ra_bmp - #{dc}_#{condition}_#{image_hour}_DEBUG.bmp", runner)
        end

      end
    end

    # glare sensor views are tonemapped
    views_glare_sensor = Dir.glob('views/*_gs.vfv')
    views_glare_sensor.each do |gv|
      rad_command = "rpict -av .3 .3 .3 -ab 1 -vf #{gv} octrees/images.oct > temp.hdr"
      exec_statement(rad_command, runner)
      rad_command = "pcond -h temp.hdr | ra_bmp - #{gv}.bmp"
      exec_statement(rad_command, runner)
      FileUtils.rm_f('temp.hdr')
    end
  end

  ## ## ## ## ## ##

  # actually do the thing

  sqlOutFile = ''
  radoutFile = ''

  # settle in, it's gonna be a bumpy ride...
  Dir.chdir(radPath.to_s)
  print_statement("Working directory: '#{Dir.pwd}'", runner)

  weather_file = nil
  weather_file_path = nil
  sqlPath = nil

  if got_2x

    weather_file = model.getOptionalWeatherFile
    weather_file_path = weather_file.get.path

    weather_file_path = runner.workflow.findFile(weather_file_path.get)
    if weather_file_path.empty?
      runner.registerError("Cannot find weather file '#{weather_file_path.get}'")
      return false
    end
    weather_file_path = weather_file_path.get

    sqlPath = OpenStudio::Path.new('sql/eplusout.sql')
    sqlPath = OpenStudio.system_complete(sqlPath)

  else

    # try runner first
    if runner.lastEpwFilePath.is_initialized
      test = runner.lastEpwFilePath.get.to_s
      if File.exist?(test)
        weather_file_path = test
      end
    end

    # try model second
    if !weather_file_path
      if model.weatherFile.is_initialized
        test = model.weatherFile.get.path
        if test.is_initialized
          # have a file name from the model
          if File.exist?(test.get.to_s)
            weather_file_path = test.get
          else
            # If this is an always-run Measure, need to check for file in different path
            alt_weath_path = File.expand_path(File.join(File.dirname(__FILE__), \
                                                        '../../../resources'))
            alt_epw_path = File.expand_path(File.join(alt_weath_path, test.get.to_s))
            server_epw_path = File.expand_path(File.join(File.dirname(__FILE__), \
                                                         "../../weather/#{File.basename(test.get.to_s)}"))
            if File.exist?(alt_epw_path)
              weather_file_path = OpenStudio::Path.new(alt_epw_path)
            elsif File.exist? server_epw_path
              weather_file_path = OpenStudio::Path.new(server_epw_path)
            else
              runner.registerError("Model has been assigned a weather file, but the file is not in \
              the specified location of '#{test.get}'. server_epw_path: #{server_epw_path}, test \
              basename: #{File.basename(test.get.to_s)}, test: #{test}")
              return false
            end
          end
        else
          runner.registerError('Model has a weather file assigned, but the weather file path has \
          been deleted.')
          return false
        end
      else
        runner.registerError('Model has not been assigned a weather file.')
        return false
      end
    end

    sqlPath = OpenStudio::Path.new('sql/eplusout.sql')
    sqlPath = OpenStudio.system_complete(sqlPath)

  end

  weather_file = OpenStudio::EpwFile.load(weather_file_path)
  if weather_file.empty?
    runner.registerError("Cannot load weather file '#{weather_file_path.get}'")
    return false
  end
  weather_file = weather_file.get

  # load the sql file
  sqlFile = OpenStudio::SqlFile.new(sqlPath)
  if !sqlFile.connectionOpen
    runner.registerError("SqlFile #{sqlPath} connection is not open")
    return false
  end

  # set the sql file
  model.setSqlFile(sqlFile)
  if model.sqlFile.empty?
    runner.registerError("Model's SqlFile is not initialized")
    return false
  end

  # get the top level simulation object
  simulation = model.getSimulationControl

  # reduce/convert epw data to Daysim-style ".wea" input format
  exec_statement("epw2wea \"#{weather_file_path}\" wx/in.wea", runner)

  site = model.getSite

  site_name = site.name.to_s
  site_latitude = site.latitude
  site_longitude = site.longitude
  site_meridian = site.timeZone.to_f * 15

  # get the facility and building
  facility = model.getFacility
  building = model.getBuilding
  building_transformation = building.transformation

  # create space geometry, hash of space name to file contents
  radSpaces = {}
  radSensors = {}
  radGlareSensorViews = {}
  radMaps = {}
  radMapHandles = {}
  radMapPoints = {}
  radViewPoints = {}
  radDaylightingControls = {}
  radDaylightingControlPoints = {}
  spaceWidths = {}
  spaceHeights = {}

  # loop through the model
  space_names = []

  building.spaces.each do |space|
    space_name = space.name.get.tr(' ', '_').tr(':', '_')
    space_names << space_name

    space_transformation = space.transformation

    # get output illuminance map points
    space.illuminanceMaps.each do |map|
      radMaps[space_name] = ''
      radMapHandles[space_name] = map.handle
      radMapPoints[space_name] = OpenStudio::Radiance::RadianceForwardTranslator.getReferencePoints(map)
      spaceWidths[space_name] = map.numberofXGridPoints
      spaceHeights[space_name] = map.numberofYGridPoints
    end

    # get daylighting control points
    space.daylightingControls.each do |control|
      radDaylightingControls[space_name] = ''
      radDaylightingControlPoints[space_name] = OpenStudio::Radiance::RadianceForwardTranslator.getReferencePoint(control)
    end

    # get glare sensors
    print_statement("### DEBUG: there are #{space.glareSensors.size} glare sensors in this space ('#{space_name}')", runner) if debug_mode
    space.glareSensors.each do |sensor|
      tmp_sensor_name = sensor.name.get.tr(' ', '_').tr(':', '_')
      radGlareSensorViews[space_name] ||= {}
      radGlareSensorViews[space_name][tmp_sensor_name] ||= {}
      radGlareSensorViews[space_name][tmp_sensor_name]['view_definitions'] = OpenStudio::Radiance::RadianceForwardTranslator.getViewVectors(sensor)

      print_statement("### DEBUG: glare sensor '#{tmp_sensor_name}' has #{OpenStudio::Radiance::RadianceForwardTranslator.getViewVectors(sensor).size} views.", runner) if debug_mode
    end
  end

  space_names_to_calculate = []

  # only do spaces with illuminance maps
  space_names_to_calculate = []
  space_names.each do |space_name|
    if !radMaps[space_name].nil?
      space_names_to_calculate << space_name
    end
  end

  # merge window group control points
  window_groups = Dir.glob('numeric/WG*.pts')
  if !window_groups.empty?
    File.open('numeric/window_controls.map', 'w') do |f|
      windows = Dir.glob('numeric/WG*.pts').sort
      windows.each do |wg|
        f.write IO.read(wg)
      end
    end
  end

  # merge calculation points
  File.open('numeric/merged_space.map', 'w') do |f|
    space_names_to_calculate.each do |space_name|
      f.write IO.read("numeric/#{space_name}.map")
      if File.exist?("numeric/#{space_name}.sns")
        f.write IO.read("numeric/#{space_name}.sns")
      end
      glare_sensors = Dir.glob("numeric/#{space_name}*.glr").sort
      if !glare_sensors.empty?
        glare_sensors.each do |sensor|
          print_statement("added glare sensor '#{sensor}' to calculation points", runner)
          f.write IO.read(sensor)
        end
      end
    end
  end

  runner.registerInfo("'Cleanup data' option selected, will delete ancillary Radiance data and all Radiance input files, post-simulation.") if debug_mode && cleanup_data
  #
  # get the daylight coefficient matrices
  calculateDaylightCoeffecients(radPath, sim_cores, catCommand, options_tregVars,
                                options_klemsDensity, options_skyvecDensity, options_dmx, options_vmx, rad_settings, procsUsed, runner, debug_mode)

  # make merged building-wide illuminance schedule(s)
  values, dcVectors = runSimulation(space_names_to_calculate, sqlFile, sim_cores,
                                    options_skyvecDensity, site_latitude, site_longitude, site_meridian, radPath, spaceWidths,
                                    spaceHeights, radGlareSensorViews, runner, debug_mode)

  # make space-level illuminance schedules and radout.sql results database
  # hoping this is no longer necessary...
  annualSimulation(sqlFile, weather_file, space_names_to_calculate, radMaps, spaceWidths, spaceHeights,
                   radMapPoints, radGlareSensorViews, sim_cores, site_latitude, site_longitude,
                   site_meridian, radPath, building, values, dcVectors, runner)

  # make new lighting power schedules based on Radiance daylight data
  if apply_schedules
    makeSchedules(model, sqlFile, runner)
  else
    print_statement("Lighting schedules have not been modified for daylighting ('Apply Schedules' option was not selected).", runner)
  end
  # compute daylight metrics for model
  daylightMetrics(model, sqlFile, runner)

  # remove illuminance map and daylighting controls from model, so they are not re-simulated in E+
  print_statement('Removing daylighting controls for EnergyPlus run...', runner)
  model.getThermalZones.each do |thermalZone|
    thermalZone.resetPrimaryDaylightingControl
    thermalZone.resetSecondaryDaylightingControl
    thermalZone.resetIlluminanceMap
  end

  # make check images
  genImages(radPath, runner, site_latitude, site_longitude, site_meridian, catCommand, debug_mode) if debug_mode

  # cleanup
  FileUtils.rm_f('annual-sky.mtx')
  unless debug_mode
    rm_list = 'output/ts/m_*.ill', 'output/ts/window_controls.ill', 'output/ts/WG*.ill', 'octrees/*.oct', 'output/ts/*.shd'
    FileUtils.rm_f Dir.glob(rm_list)
  end
  if cleanup_data
    runner.registerInfo('Deleting most Radiance I/O to preserve disk space')
    runner.registerInfo('Deleting debug files') if debug_mode
    clean_list = 'bsdf', 'materials', 'numeric', 'octrees', 'options', 'scene', 'skies', 'sql', 'views', 'wx', 'output/dc', 'output/ts'
    FileUtils.rm_rf(clean_list)
  end

  # report initial condition of model
  daylightAnalysisSpaces = []
  spaces = model.getSpaces
  spaces.each do |sp|
    if !sp.illuminanceMaps.empty?
      daylightAnalysisSpaces << sp
    end
  end
  runner.registerInitialCondition("Input building model contains #{model.getSpaces.size} spaces.")

  # report final condition of model
  runner.registerFinalCondition("Measure ran Radiance on the #{daylightAnalysisSpaces.size} spaces containing daylighting objects.")

  runner.registerInfo("End Encoding.default_external = #{Encoding.default_external}")
  runner.registerInfo("End Encoding.default_internal = #{Encoding.default_internal}")

  return true
ensure
  Dir.chdir(current_dir)
end