1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.41 |
static const char RCSid[] = "$Id: gendaymtx.c,v 2.40 2024/04/26 23:10:59 greg Exp $"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* gendaymtx.c |
6 |
|
|
* |
7 |
|
|
* Generate a daylight matrix based on Perez Sky Model. |
8 |
|
|
* |
9 |
|
|
* Most of this code is borrowed (see copyright below) from Ian Ashdown's |
10 |
|
|
* excellent re-implementation of Jean-Jacques Delaunay's gendaylit.c |
11 |
|
|
* |
12 |
|
|
* Created by Greg Ward on 1/16/13. |
13 |
|
|
*/ |
14 |
|
|
|
15 |
|
|
/********************************************************************* |
16 |
|
|
* |
17 |
|
|
* H32_gendaylit.CPP - Perez Sky Model Calculation |
18 |
|
|
* |
19 |
|
|
* Version: 1.00A |
20 |
|
|
* |
21 |
|
|
* History: 09/10/01 - Created. |
22 |
|
|
* 11/10/08 - Modified for Unix compilation. |
23 |
|
|
* 11/10/12 - Fixed conditional __max directive. |
24 |
|
|
* 1/11/13 - Tweaks and optimizations (G.Ward) |
25 |
|
|
* |
26 |
|
|
* Compilers: Microsoft Visual C/C++ Professional V10.0 |
27 |
|
|
* |
28 |
|
|
* Author: Ian Ashdown, P.Eng. |
29 |
|
|
* byHeart Consultants Limited |
30 |
|
|
* 620 Ballantree Road |
31 |
|
|
* West Vancouver, B.C. |
32 |
|
|
* Canada V7S 1W3 |
33 |
|
|
* e-mail: [email protected] |
34 |
|
|
* |
35 |
|
|
* References: Perez, R., P. Ineichen, R. Seals, J. Michalsky, and R. |
36 |
|
|
* Stewart. 1990. ìModeling Daylight Availability and |
37 |
|
|
* Irradiance Components from Direct and Global |
38 |
|
|
* Irradiance,î Solar Energy 44(5):271-289. |
39 |
|
|
* |
40 |
|
|
* Perez, R., R. Seals, and J. Michalsky. 1993. |
41 |
|
|
* ìAll-Weather Model for Sky Luminance Distribution - |
42 |
|
|
* Preliminary Configuration and Validation,î Solar Energy |
43 |
|
|
* 50(3):235-245. |
44 |
|
|
* |
45 |
|
|
* Perez, R., R. Seals, and J. Michalsky. 1993. "ERRATUM to |
46 |
|
|
* All-Weather Model for Sky Luminance Distribution - |
47 |
|
|
* Preliminary Configuration and Validation,î Solar Energy |
48 |
|
|
* 51(5):423. |
49 |
|
|
* |
50 |
|
|
* NOTE: This program is a completely rewritten version of |
51 |
|
|
* gendaylit.c written by Jean-Jacques Delaunay (1994). |
52 |
|
|
* |
53 |
|
|
* Copyright 2009-2012 byHeart Consultants Limited. All rights |
54 |
|
|
* reserved. |
55 |
|
|
* |
56 |
|
|
* Redistribution and use in source and binary forms, with or without |
57 |
|
|
* modification, are permitted for personal and commercial purposes |
58 |
|
|
* provided that redistribution of source code must retain the above |
59 |
|
|
* copyright notice, this list of conditions and the following |
60 |
|
|
* disclaimer: |
61 |
|
|
* |
62 |
|
|
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED |
63 |
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
64 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
65 |
|
|
* DISCLAIMED. IN NO EVENT SHALL byHeart Consultants Limited OR |
66 |
|
|
* ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
67 |
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
68 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
69 |
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
70 |
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
71 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
72 |
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
73 |
|
|
* POSSIBILITY OF SUCH DAMAGE. |
74 |
|
|
* |
75 |
|
|
*********************************************************************/ |
76 |
|
|
|
77 |
|
|
/* Zenith is along the Z-axis */ |
78 |
|
|
/* X-axis points east */ |
79 |
|
|
/* Y-axis points north */ |
80 |
|
|
/* azimuth is measured as degrees or radians east of North */ |
81 |
|
|
|
82 |
|
|
/* Include files */ |
83 |
|
|
#define _USE_MATH_DEFINES |
84 |
|
|
#include <stdlib.h> |
85 |
|
|
#include <ctype.h> |
86 |
greg |
2.30 |
#include "platform.h" |
87 |
greg |
2.1 |
#include "rtmath.h" |
88 |
greg |
2.23 |
#include "rtio.h" |
89 |
greg |
2.1 |
#include "color.h" |
90 |
greg |
2.30 |
#include "sun.h" |
91 |
greg |
2.41 |
#include "loadEPW.h" |
92 |
greg |
2.1 |
|
93 |
greg |
2.38 |
char *progname; /* Program name */ |
94 |
greg |
2.1 |
const double DC_SolarConstantE = 1367.0; /* Solar constant W/m^2 */ |
95 |
|
|
const double DC_SolarConstantL = 127.5; /* Solar constant klux */ |
96 |
|
|
|
97 |
|
|
double altitude; /* Solar altitude (radians) */ |
98 |
|
|
double azimuth; /* Solar azimuth (radians) */ |
99 |
|
|
double apwc; /* Atmospheric precipitable water content */ |
100 |
|
|
double dew_point = 11.0; /* Surface dew point temperature (deg. C) */ |
101 |
|
|
double diff_illum; /* Diffuse illuminance */ |
102 |
|
|
double diff_irrad; /* Diffuse irradiance */ |
103 |
|
|
double dir_illum; /* Direct illuminance */ |
104 |
|
|
double dir_irrad; /* Direct irradiance */ |
105 |
|
|
int julian_date; /* Julian date */ |
106 |
|
|
double perez_param[5]; /* Perez sky model parameters */ |
107 |
|
|
double sky_brightness; /* Sky brightness */ |
108 |
|
|
double sky_clearness; /* Sky clearness */ |
109 |
|
|
double solar_rad; /* Solar radiance */ |
110 |
|
|
double sun_zenith; /* Sun zenith angle (radians) */ |
111 |
|
|
int input = 0; /* Input type */ |
112 |
greg |
2.11 |
int output = 0; /* Output type */ |
113 |
greg |
2.1 |
|
114 |
|
|
extern double dmax( double, double ); |
115 |
|
|
extern double CalcAirMass(); |
116 |
|
|
extern double CalcDiffuseIllumRatio( int ); |
117 |
|
|
extern double CalcDiffuseIrradiance(); |
118 |
|
|
extern double CalcDirectIllumRatio( int ); |
119 |
|
|
extern double CalcDirectIrradiance(); |
120 |
|
|
extern double CalcEccentricity(); |
121 |
|
|
extern double CalcPrecipWater( double ); |
122 |
|
|
extern double CalcRelHorzIllum( float *parr ); |
123 |
|
|
extern double CalcRelLuminance( double, double ); |
124 |
|
|
extern double CalcSkyBrightness(); |
125 |
|
|
extern double CalcSkyClearness(); |
126 |
|
|
extern int CalcSkyParamFromIllum(); |
127 |
|
|
extern int GetCategoryIndex(); |
128 |
|
|
extern void CalcPerezParam( double, double, double, int ); |
129 |
|
|
extern void CalcSkyPatchLumin( float *parr ); |
130 |
|
|
extern void ComputeSky( float *parr ); |
131 |
|
|
|
132 |
greg |
2.40 |
|
133 |
|
|
extern double solar_sunset(int month, int day); |
134 |
|
|
extern double solar_sunrise(int month, int day); |
135 |
|
|
|
136 |
greg |
2.1 |
/* Degrees into radians */ |
137 |
|
|
#define DegToRad(deg) ((deg)*(PI/180.)) |
138 |
|
|
|
139 |
|
|
/* Radiuans into degrees */ |
140 |
|
|
#define RadToDeg(rad) ((rad)*(180./PI)) |
141 |
|
|
|
142 |
|
|
/* Perez sky model coefficients */ |
143 |
|
|
|
144 |
|
|
/* Reference: Perez, R., R. Seals, and J. Michalsky, 1993. "All- */ |
145 |
|
|
/* Weather Model for Sky Luminance Distribution - */ |
146 |
|
|
/* Preliminary Configuration and Validation," Solar */ |
147 |
|
|
/* Energy 50(3):235-245, Table 1. */ |
148 |
|
|
|
149 |
|
|
static const double PerezCoeff[8][20] = |
150 |
|
|
{ |
151 |
|
|
/* Sky clearness (epsilon): 1.000 to 1.065 */ |
152 |
|
|
{ 1.3525, -0.2576, -0.2690, -1.4366, -0.7670, |
153 |
|
|
0.0007, 1.2734, -0.1233, 2.8000, 0.6004, |
154 |
|
|
1.2375, 1.0000, 1.8734, 0.6297, 0.9738, |
155 |
|
|
0.2809, 0.0356, -0.1246, -0.5718, 0.9938 }, |
156 |
|
|
/* Sky clearness (epsilon): 1.065 to 1.230 */ |
157 |
|
|
{ -1.2219, -0.7730, 1.4148, 1.1016, -0.2054, |
158 |
|
|
0.0367, -3.9128, 0.9156, 6.9750, 0.1774, |
159 |
|
|
6.4477, -0.1239, -1.5798, -0.5081, -1.7812, |
160 |
|
|
0.1080, 0.2624, 0.0672, -0.2190, -0.4285 }, |
161 |
|
|
/* Sky clearness (epsilon): 1.230 to 1.500 */ |
162 |
|
|
{ -1.1000, -0.2515, 0.8952, 0.0156, 0.2782, |
163 |
|
|
-0.1812, - 4.5000, 1.1766, 24.7219, -13.0812, |
164 |
|
|
-37.7000, 34.8438, -5.0000, 1.5218, 3.9229, |
165 |
|
|
-2.6204, -0.0156, 0.1597, 0.4199, -0.5562 }, |
166 |
|
|
/* Sky clearness (epsilon): 1.500 to 1.950 */ |
167 |
|
|
{ -0.5484, -0.6654, -0.2672, 0.7117, 0.7234, |
168 |
|
|
-0.6219, -5.6812, 2.6297, 33.3389, -18.3000, |
169 |
|
|
-62.2500, 52.0781, -3.5000, 0.0016, 1.1477, |
170 |
|
|
0.1062, 0.4659, -0.3296, -0.0876, -0.0329 }, |
171 |
|
|
/* Sky clearness (epsilon): 1.950 to 2.800 */ |
172 |
|
|
{ -0.6000, -0.3566, -2.5000, 2.3250, 0.2937, |
173 |
|
|
0.0496, -5.6812, 1.8415, 21.0000, -4.7656 , |
174 |
|
|
-21.5906, 7.2492, -3.5000, -0.1554, 1.4062, |
175 |
|
|
0.3988, 0.0032, 0.0766, -0.0656, -0.1294 }, |
176 |
|
|
/* Sky clearness (epsilon): 2.800 to 4.500 */ |
177 |
|
|
{ -1.0156, -0.3670, 1.0078, 1.4051, 0.2875, |
178 |
|
|
-0.5328, -3.8500, 3.3750, 14.0000, -0.9999, |
179 |
|
|
-7.1406, 7.5469, -3.4000, -0.1078, -1.0750, |
180 |
|
|
1.5702, -0.0672, 0.4016, 0.3017, -0.4844 }, |
181 |
|
|
/* Sky clearness (epsilon): 4.500 to 6.200 */ |
182 |
|
|
{ -1.0000, 0.0211, 0.5025, -0.5119, -0.3000, |
183 |
|
|
0.1922, 0.7023, -1.6317, 19.0000, -5.0000, |
184 |
|
|
1.2438, -1.9094, -4.0000, 0.0250, 0.3844, |
185 |
|
|
0.2656, 1.0468, -0.3788, -2.4517, 1.4656 }, |
186 |
|
|
/* Sky clearness (epsilon): 6.200 to ... */ |
187 |
|
|
{ -1.0500, 0.0289, 0.4260, 0.3590, -0.3250, |
188 |
|
|
0.1156, 0.7781, 0.0025, 31.0625, -14.5000, |
189 |
|
|
-46.1148, 55.3750, -7.2312, 0.4050, 13.3500, |
190 |
|
|
0.6234, 1.5000, -0.6426, 1.8564, 0.5636 } |
191 |
|
|
}; |
192 |
|
|
|
193 |
|
|
/* Perez irradiance component model coefficients */ |
194 |
|
|
|
195 |
|
|
/* Reference: Perez, R., P. Ineichen, R. Seals, J. Michalsky, and R. */ |
196 |
|
|
/* Stewart. 1990. ìModeling Daylight Availability and */ |
197 |
|
|
/* Irradiance Components from Direct and Global */ |
198 |
|
|
/* Irradiance,î Solar Energy 44(5):271-289. */ |
199 |
|
|
|
200 |
|
|
typedef struct |
201 |
|
|
{ |
202 |
|
|
double lower; /* Lower bound */ |
203 |
|
|
double upper; /* Upper bound */ |
204 |
|
|
} CategoryBounds; |
205 |
|
|
|
206 |
|
|
/* Perez sky clearness (epsilon) categories (Table 1) */ |
207 |
|
|
static const CategoryBounds SkyClearCat[8] = |
208 |
|
|
{ |
209 |
|
|
{ 1.000, 1.065 }, /* Overcast */ |
210 |
|
|
{ 1.065, 1.230 }, |
211 |
|
|
{ 1.230, 1.500 }, |
212 |
|
|
{ 1.500, 1.950 }, |
213 |
|
|
{ 1.950, 2.800 }, |
214 |
|
|
{ 2.800, 4.500 }, |
215 |
|
|
{ 4.500, 6.200 }, |
216 |
greg |
2.12 |
{ 6.200, 12.01 } /* Clear */ |
217 |
greg |
2.1 |
}; |
218 |
|
|
|
219 |
|
|
/* Luminous efficacy model coefficients */ |
220 |
|
|
typedef struct |
221 |
|
|
{ |
222 |
|
|
double a; |
223 |
|
|
double b; |
224 |
|
|
double c; |
225 |
|
|
double d; |
226 |
|
|
} ModelCoeff; |
227 |
|
|
|
228 |
|
|
/* Diffuse luminous efficacy model coefficients (Table 4, Eqn. 7) */ |
229 |
|
|
static const ModelCoeff DiffuseLumEff[8] = |
230 |
|
|
{ |
231 |
|
|
{ 97.24, -0.46, 12.00, -8.91 }, |
232 |
|
|
{ 107.22, 1.15, 0.59, -3.95 }, |
233 |
|
|
{ 104.97, 2.96, -5.53, -8.77 }, |
234 |
|
|
{ 102.39, 5.59, -13.95, -13.90 }, |
235 |
|
|
{ 100.71, 5.94, -22.75, -23.74 }, |
236 |
|
|
{ 106.42, 3.83, -36.15, -28.83 }, |
237 |
|
|
{ 141.88, 1.90, -53.24, -14.03 }, |
238 |
|
|
{ 152.23, 0.35, -45.27, -7.98 } |
239 |
|
|
}; |
240 |
|
|
|
241 |
|
|
/* Direct luminous efficacy model coefficients (Table 4, Eqn. 8) */ |
242 |
|
|
static const ModelCoeff DirectLumEff[8] = |
243 |
|
|
{ |
244 |
|
|
{ 57.20, -4.55, -2.98, 117.12 }, |
245 |
|
|
{ 98.99, -3.46, -1.21, 12.38 }, |
246 |
|
|
{ 109.83, -4.90, -1.71, -8.81 }, |
247 |
|
|
{ 110.34, -5.84, -1.99, -4.56 }, |
248 |
|
|
{ 106.36, -3.97, -1.75, -6.16 }, |
249 |
|
|
{ 107.19, -1.25, -1.51, -26.73 }, |
250 |
|
|
{ 105.75, 0.77, -1.26, -34.44 }, |
251 |
|
|
{ 101.18, 1.58, -1.10, -8.29 } |
252 |
|
|
}; |
253 |
|
|
|
254 |
greg |
2.3 |
#ifndef NSUNPATCH |
255 |
greg |
2.10 |
#define NSUNPATCH 4 /* max. # patches to spread sun into */ |
256 |
greg |
2.3 |
#endif |
257 |
|
|
|
258 |
greg |
2.35 |
#define SUN_ANG_DEG 0.533 /* sun full-angle in degrees */ |
259 |
|
|
|
260 |
greg |
2.10 |
int nsuns = NSUNPATCH; /* number of sun patches to use */ |
261 |
|
|
double fixed_sun_sa = -1; /* fixed solid angle per sun? */ |
262 |
|
|
|
263 |
greg |
2.1 |
int verbose = 0; /* progress reports to stderr? */ |
264 |
|
|
|
265 |
|
|
int outfmt = 'a'; /* output format */ |
266 |
|
|
|
267 |
|
|
int rhsubdiv = 1; /* Reinhart sky subdivisions */ |
268 |
|
|
|
269 |
greg |
2.4 |
COLOR skycolor = {.96, 1.004, 1.118}; /* sky coloration */ |
270 |
|
|
COLOR suncolor = {1., 1., 1.}; /* sun color */ |
271 |
|
|
COLOR grefl = {.2, .2, .2}; /* ground reflectance */ |
272 |
greg |
2.1 |
|
273 |
|
|
int nskypatch; /* number of Reinhart patches */ |
274 |
|
|
float *rh_palt; /* sky patch altitudes (radians) */ |
275 |
|
|
float *rh_pazi; /* sky patch azimuths (radians) */ |
276 |
|
|
float *rh_dom; /* sky patch solid angle (sr) */ |
277 |
|
|
|
278 |
greg |
2.36 |
#define vector(v,alt,azi) ( (v)[1] = cos(alt), \ |
279 |
|
|
(v)[0] = (v)[1]*sin(azi), \ |
280 |
|
|
(v)[1] *= cos(azi), \ |
281 |
|
|
(v)[2] = sin(alt) ) |
282 |
greg |
2.1 |
|
283 |
|
|
#define rh_vector(v,i) vector(v,rh_palt[i],rh_pazi[i]) |
284 |
|
|
|
285 |
|
|
#define rh_cos(i) tsin(rh_palt[i]) |
286 |
|
|
|
287 |
greg |
2.36 |
#define solar_minute(jd,hr) ((24*60)*((jd)-1)+(int)((hr)*60.+.5)) |
288 |
|
|
|
289 |
greg |
2.1 |
extern int rh_init(void); |
290 |
|
|
extern float * resize_dmatrix(float *mtx_data, int nsteps, int npatch); |
291 |
greg |
2.36 |
extern void OutputSun(int id, int goodsun, FILE *fp, FILE *mfp); |
292 |
greg |
2.1 |
extern void AddDirect(float *parr); |
293 |
|
|
|
294 |
greg |
2.14 |
|
295 |
|
|
static const char * |
296 |
|
|
getfmtname(int fmt) |
297 |
|
|
{ |
298 |
|
|
switch (fmt) { |
299 |
|
|
case 'a': |
300 |
|
|
return("ascii"); |
301 |
|
|
case 'f': |
302 |
|
|
return("float"); |
303 |
|
|
case 'd': |
304 |
|
|
return("double"); |
305 |
|
|
} |
306 |
|
|
return("unknown"); |
307 |
|
|
} |
308 |
|
|
|
309 |
|
|
|
310 |
greg |
2.1 |
int |
311 |
|
|
main(int argc, char *argv[]) |
312 |
|
|
{ |
313 |
greg |
2.41 |
EPWheader *epw = NULL; /* EPW/WEA input file */ |
314 |
|
|
EPWrecord erec; /* current EPW/WEA input record */ |
315 |
|
|
float dpthist[2]; /* previous dew point temps */ |
316 |
|
|
double dir, dif; |
317 |
greg |
2.14 |
int doheader = 1; /* output header? */ |
318 |
greg |
2.8 |
double rotation = 0; /* site rotation (degrees) */ |
319 |
greg |
2.1 |
double elevation; /* site elevation (meters) */ |
320 |
greg |
2.36 |
int leap_day = 0; /* add leap day? */ |
321 |
greg |
2.37 |
int sun_hours_only = 0; /* only output sun hours? */ |
322 |
greg |
2.1 |
int dir_is_horiz; /* direct is meas. on horizontal? */ |
323 |
greg |
2.34 |
FILE *sunsfp = NULL; /* output file for individual suns */ |
324 |
greg |
2.36 |
FILE *modsfp = NULL; /* modifier output file */ |
325 |
greg |
2.1 |
float *mtx_data = NULL; /* our matrix data */ |
326 |
greg |
2.28 |
int avgSky = 0; /* compute average sky r.t. matrix? */ |
327 |
greg |
2.27 |
int ntsteps = 0; /* number of time steps */ |
328 |
greg |
2.28 |
int tstorage = 0; /* number of allocated time steps */ |
329 |
|
|
int nstored = 0; /* number of time steps in matrix */ |
330 |
greg |
2.1 |
int last_monthly = 0; /* month of last report */ |
331 |
|
|
int mtx_offset; |
332 |
|
|
int i, j; |
333 |
greg |
2.40 |
double timeinterval = 0; |
334 |
greg |
2.1 |
|
335 |
|
|
progname = argv[0]; |
336 |
|
|
/* get options */ |
337 |
|
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
338 |
|
|
switch (argv[i][1]) { |
339 |
greg |
2.4 |
case 'g': /* ground reflectance */ |
340 |
|
|
grefl[0] = atof(argv[++i]); |
341 |
|
|
grefl[1] = atof(argv[++i]); |
342 |
|
|
grefl[2] = atof(argv[++i]); |
343 |
greg |
2.1 |
break; |
344 |
greg |
2.4 |
case 'v': /* verbose progress reports */ |
345 |
greg |
2.1 |
verbose++; |
346 |
|
|
break; |
347 |
greg |
2.14 |
case 'h': /* turn off header */ |
348 |
|
|
doheader = 0; |
349 |
|
|
break; |
350 |
greg |
2.4 |
case 'o': /* output format */ |
351 |
greg |
2.1 |
switch (argv[i][2]) { |
352 |
|
|
case 'f': |
353 |
|
|
case 'd': |
354 |
|
|
case 'a': |
355 |
|
|
outfmt = argv[i][2]; |
356 |
|
|
break; |
357 |
|
|
default: |
358 |
|
|
goto userr; |
359 |
|
|
} |
360 |
|
|
break; |
361 |
greg |
2.11 |
case 'O': /* output type */ |
362 |
|
|
switch (argv[i][2]) { |
363 |
|
|
case '0': |
364 |
|
|
output = 0; |
365 |
|
|
break; |
366 |
|
|
case '1': |
367 |
|
|
output = 1; |
368 |
|
|
break; |
369 |
|
|
default: |
370 |
|
|
goto userr; |
371 |
|
|
} |
372 |
|
|
if (argv[i][3]) |
373 |
|
|
goto userr; |
374 |
|
|
break; |
375 |
greg |
2.4 |
case 'm': /* Reinhart subdivisions */ |
376 |
greg |
2.1 |
rhsubdiv = atoi(argv[++i]); |
377 |
|
|
break; |
378 |
greg |
2.4 |
case 'c': /* sky color */ |
379 |
greg |
2.1 |
skycolor[0] = atof(argv[++i]); |
380 |
|
|
skycolor[1] = atof(argv[++i]); |
381 |
|
|
skycolor[2] = atof(argv[++i]); |
382 |
|
|
break; |
383 |
greg |
2.36 |
case 'D': /* output suns to file */ |
384 |
|
|
if (strcmp(argv[++i], "-")) { |
385 |
|
|
sunsfp = fopen(argv[i], "w"); |
386 |
|
|
if (sunsfp == NULL) { |
387 |
|
|
fprintf(stderr, |
388 |
|
|
"%s: cannot open '%s' for output\n", |
389 |
|
|
progname, argv[i]); |
390 |
|
|
exit(1); |
391 |
|
|
} |
392 |
|
|
break; /* still may output matrix */ |
393 |
|
|
} |
394 |
|
|
sunsfp = stdout; /* sending to stdout, so... */ |
395 |
|
|
/* fall through */ |
396 |
greg |
2.34 |
case 'n': /* no matrix output */ |
397 |
|
|
avgSky = -1; |
398 |
|
|
rhsubdiv = 1; |
399 |
|
|
/* fall through */ |
400 |
greg |
2.4 |
case 'd': /* solar (direct) only */ |
401 |
greg |
2.1 |
skycolor[0] = skycolor[1] = skycolor[2] = 0; |
402 |
greg |
2.33 |
grefl[0] = grefl[1] = grefl[2] = 0; |
403 |
greg |
2.1 |
break; |
404 |
greg |
2.36 |
case 'M': /* send sun modifiers to file */ |
405 |
|
|
if ((modsfp = fopen(argv[++i], "w")) == NULL) { |
406 |
greg |
2.34 |
fprintf(stderr, "%s: cannot open '%s' for output\n", |
407 |
greg |
2.35 |
progname, argv[i]); |
408 |
greg |
2.34 |
exit(1); |
409 |
|
|
} |
410 |
|
|
break; |
411 |
greg |
2.4 |
case 's': /* sky only (no direct) */ |
412 |
|
|
suncolor[0] = suncolor[1] = suncolor[2] = 0; |
413 |
greg |
2.1 |
break; |
414 |
greg |
2.37 |
case 'u': /* solar hours only */ |
415 |
|
|
sun_hours_only = 1; |
416 |
|
|
break; |
417 |
greg |
2.8 |
case 'r': /* rotate distribution */ |
418 |
|
|
if (argv[i][2] && argv[i][2] != 'z') |
419 |
|
|
goto userr; |
420 |
|
|
rotation = atof(argv[++i]); |
421 |
|
|
break; |
422 |
greg |
2.10 |
case '5': /* 5-phase calculation */ |
423 |
|
|
nsuns = 1; |
424 |
greg |
2.19 |
fixed_sun_sa = PI/360.*atof(argv[++i]); |
425 |
greg |
2.21 |
if (fixed_sun_sa <= 0) { |
426 |
|
|
fprintf(stderr, "%s: missing solar disk size argument for '-5' option\n", |
427 |
greg |
2.35 |
progname); |
428 |
greg |
2.21 |
exit(1); |
429 |
|
|
} |
430 |
greg |
2.19 |
fixed_sun_sa *= fixed_sun_sa*PI; |
431 |
greg |
2.10 |
break; |
432 |
greg |
2.27 |
case 'A': /* compute average sky */ |
433 |
|
|
avgSky = 1; |
434 |
|
|
break; |
435 |
greg |
2.40 |
case 'i': |
436 |
|
|
timeinterval = atof(argv[++i]); |
437 |
|
|
break; |
438 |
greg |
2.1 |
default: |
439 |
|
|
goto userr; |
440 |
|
|
} |
441 |
greg |
2.41 |
if ((i < argc-1) | (i > argc)) |
442 |
greg |
2.1 |
goto userr; |
443 |
greg |
2.41 |
epw = EPWopen(argv[i]); |
444 |
|
|
if (epw == NULL) |
445 |
greg |
2.1 |
exit(1); |
446 |
greg |
2.36 |
if ((modsfp != NULL) & (sunsfp == NULL)) |
447 |
|
|
fprintf(stderr, "%s: warning -M output will be empty without -D\n", |
448 |
|
|
progname); |
449 |
greg |
2.1 |
if (verbose) { |
450 |
|
|
if (i == argc-1) |
451 |
|
|
fprintf(stderr, "%s: reading weather tape '%s'\n", |
452 |
|
|
progname, argv[i]); |
453 |
|
|
else |
454 |
|
|
fprintf(stderr, "%s: reading weather tape from <stdin>\n", |
455 |
|
|
progname); |
456 |
|
|
} |
457 |
|
|
/* read weather tape header */ |
458 |
greg |
2.41 |
s_latitude = epw->loc.latitude; |
459 |
|
|
s_longitude = -epw->loc.longitude; |
460 |
|
|
s_meridian = -15.*epw->loc.timezone; |
461 |
|
|
elevation = epw->loc.elevation; |
462 |
|
|
switch (epw->isWEA) { /* translate units */ |
463 |
|
|
case WEAnot: |
464 |
|
|
case WEAradnorm: |
465 |
greg |
2.1 |
input = 1; /* radiometric quantities */ |
466 |
|
|
dir_is_horiz = 0; /* direct is perpendicular meas. */ |
467 |
|
|
break; |
468 |
greg |
2.41 |
case WEAradhoriz: |
469 |
greg |
2.1 |
input = 1; /* radiometric quantities */ |
470 |
|
|
dir_is_horiz = 1; /* solar measured horizontally */ |
471 |
|
|
break; |
472 |
greg |
2.41 |
case WEAphotnorm: |
473 |
greg |
2.1 |
input = 2; /* photometric quantities */ |
474 |
|
|
dir_is_horiz = 0; /* direct is perpendicular meas. */ |
475 |
|
|
break; |
476 |
|
|
default: |
477 |
|
|
goto fmterr; |
478 |
|
|
} |
479 |
|
|
rh_init(); /* initialize sky patches */ |
480 |
|
|
if (verbose) { |
481 |
greg |
2.41 |
fprintf(stderr, "%s: location '%s, %s'\n", progname, |
482 |
|
|
epw->loc.city, epw->loc.country); |
483 |
greg |
2.1 |
fprintf(stderr, "%s: (lat,long)=(%.1f,%.1f) degrees north, west\n", |
484 |
|
|
progname, s_latitude, s_longitude); |
485 |
greg |
2.35 |
if (avgSky >= 0) |
486 |
|
|
fprintf(stderr, "%s: %d sky patches\n", |
487 |
|
|
progname, nskypatch); |
488 |
|
|
if (sunsfp) |
489 |
greg |
2.39 |
fprintf(stderr, "%s: outputting suns to %s\n", |
490 |
|
|
progname, sunsfp==stdout ? "stdout" : "file"); |
491 |
greg |
2.8 |
if (rotation != 0) |
492 |
|
|
fprintf(stderr, "%s: rotating output %.0f degrees\n", |
493 |
|
|
progname, rotation); |
494 |
greg |
2.1 |
} |
495 |
greg |
2.2 |
/* convert quantities to radians */ |
496 |
|
|
s_latitude = DegToRad(s_latitude); |
497 |
|
|
s_longitude = DegToRad(s_longitude); |
498 |
|
|
s_meridian = DegToRad(s_meridian); |
499 |
greg |
2.27 |
/* initial allocation */ |
500 |
greg |
2.28 |
mtx_data = resize_dmatrix(mtx_data, tstorage=2, nskypatch); |
501 |
greg |
2.41 |
dpthist[0] = -100; |
502 |
greg |
2.1 |
/* process each time step in tape */ |
503 |
greg |
2.41 |
while ((j = EPWread(epw, &erec)) > 0) { |
504 |
|
|
int mo = erec.date.month+1; |
505 |
|
|
int da = erec.date.day; |
506 |
|
|
double hr = erec.date.hour; |
507 |
greg |
2.40 |
double sda, sta, st; |
508 |
greg |
2.37 |
int sun_in_sky; |
509 |
greg |
2.1 |
/* compute solar position */ |
510 |
greg |
2.36 |
if ((mo == 2) & (da == 29)) { |
511 |
|
|
julian_date = 60; |
512 |
|
|
leap_day = 1; |
513 |
|
|
} else |
514 |
|
|
julian_date = jdate(mo, da) + leap_day; |
515 |
greg |
2.1 |
sda = sdec(julian_date); |
516 |
|
|
sta = stadj(julian_date); |
517 |
greg |
2.40 |
st = hr + sta; |
518 |
|
|
|
519 |
|
|
if (timeinterval > 0) { |
520 |
|
|
if (fabs(solar_sunrise(mo, da) - st) <= timeinterval/120) |
521 |
|
|
st = (st + timeinterval/120 + solar_sunrise(mo, da))/2; |
522 |
|
|
else if (fabs(solar_sunset(mo, da) - st) < timeinterval/120) |
523 |
|
|
st = (st - timeinterval/120 + solar_sunset(mo, da))/2; |
524 |
|
|
} |
525 |
|
|
altitude = salt(sda, st); |
526 |
greg |
2.37 |
sun_in_sky = (altitude > -DegToRad(SUN_ANG_DEG/2.)); |
527 |
|
|
if (sun_hours_only && !sun_in_sky) |
528 |
|
|
continue; /* skipping nighttime points */ |
529 |
greg |
2.40 |
azimuth = sazi(sda, st) + PI - DegToRad(rotation); |
530 |
greg |
2.36 |
|
531 |
greg |
2.41 |
switch (epw->isWEA) { /* translate units */ |
532 |
|
|
case WEAnot: |
533 |
|
|
case WEAradnorm: |
534 |
|
|
if (!EPWisset(&erec,dirirrad) | |
535 |
|
|
!EPWisset(&erec,horizdiffirrad)) { |
536 |
|
|
fprintf(stderr, "%s: missing required irradiances at line %d\n", |
537 |
|
|
progname, epw->lino); |
538 |
|
|
exit(1); |
539 |
|
|
} |
540 |
|
|
dir = erec.dirirrad; |
541 |
|
|
dif = erec.horizdiffirrad; |
542 |
|
|
break; |
543 |
|
|
case WEAradhoriz: |
544 |
|
|
dir = erec.globhorizirrad - erec.horizdiffirrad; |
545 |
|
|
dif = erec.horizdiffirrad; |
546 |
|
|
break; |
547 |
|
|
case WEAphotnorm: |
548 |
|
|
dir = erec.dirillum; |
549 |
|
|
dif = erec.diffillum; |
550 |
|
|
break; |
551 |
|
|
} |
552 |
|
|
if (EPWisset(&erec,dptemp)) { /* 3-hour dew point temp */ |
553 |
|
|
if (dpthist[0] < -99) |
554 |
|
|
dpthist[0] = dpthist[1] = erec.dptemp; |
555 |
|
|
dew_point = (1./3.)*(dpthist[0] + dpthist[1] + erec.dptemp); |
556 |
|
|
dpthist[0] = dpthist[1]; dpthist[1] = erec.dptemp; |
557 |
|
|
} |
558 |
greg |
2.37 |
mtx_offset = 3*nskypatch*nstored; |
559 |
|
|
nstored += !avgSky | !nstored; |
560 |
|
|
/* make space for next row */ |
561 |
|
|
if (nstored > tstorage) { |
562 |
|
|
tstorage += (tstorage>>1) + nstored + 7; |
563 |
|
|
mtx_data = resize_dmatrix(mtx_data, tstorage, nskypatch); |
564 |
|
|
} |
565 |
|
|
ntsteps++; /* keep count of time steps */ |
566 |
|
|
|
567 |
greg |
2.36 |
if (dir+dif <= 1e-4) { /* effectively nighttime? */ |
568 |
|
|
if (!avgSky | !mtx_offset) |
569 |
|
|
memset(mtx_data+mtx_offset, 0, |
570 |
|
|
sizeof(float)*3*nskypatch); |
571 |
greg |
2.37 |
/* output black sun? */ |
572 |
|
|
if (sunsfp && sun_in_sky) |
573 |
greg |
2.36 |
OutputSun(solar_minute(julian_date,hr), 0, |
574 |
|
|
sunsfp, modsfp); |
575 |
|
|
continue; |
576 |
|
|
} |
577 |
greg |
2.38 |
if (!sun_in_sky && dir > (input==1 ? 20. : 20.*WHTEFFICACY)) |
578 |
|
|
fprintf(stderr, |
579 |
|
|
"%s: warning - unusually bright at %.1f on %d-%d\n", |
580 |
|
|
progname, hr, mo, da); |
581 |
greg |
2.1 |
/* convert measured values */ |
582 |
greg |
2.38 |
if (dir_is_horiz && altitude > FTINY) |
583 |
greg |
2.1 |
dir /= sin(altitude); |
584 |
|
|
if (input == 1) { |
585 |
|
|
dir_irrad = dir; |
586 |
|
|
diff_irrad = dif; |
587 |
|
|
} else /* input == 2 */ { |
588 |
|
|
dir_illum = dir; |
589 |
|
|
diff_illum = dif; |
590 |
|
|
} |
591 |
|
|
/* compute sky patch values */ |
592 |
|
|
ComputeSky(mtx_data+mtx_offset); |
593 |
greg |
2.37 |
/* output sun if requested */ |
594 |
|
|
if (sunsfp && sun_in_sky) |
595 |
greg |
2.36 |
OutputSun(solar_minute(julian_date,hr), 1, |
596 |
|
|
sunsfp, modsfp); |
597 |
greg |
2.35 |
|
598 |
greg |
2.34 |
if (avgSky < 0) /* no matrix? */ |
599 |
|
|
continue; |
600 |
|
|
|
601 |
greg |
2.4 |
AddDirect(mtx_data+mtx_offset); |
602 |
greg |
2.27 |
/* update cumulative sky? */ |
603 |
|
|
for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) |
604 |
|
|
mtx_data[i] += mtx_data[mtx_offset+i]; |
605 |
greg |
2.34 |
/* monthly reporting */ |
606 |
|
|
if (verbose && mo != last_monthly) |
607 |
|
|
fprintf(stderr, "%s: stepping through month %d...\n", |
608 |
|
|
progname, last_monthly=mo); |
609 |
greg |
2.36 |
/* note whether leap-day was given */ |
610 |
greg |
2.34 |
} |
611 |
greg |
2.41 |
if (j != EOF) { |
612 |
|
|
fprintf(stderr, "%s: error on input\n", progname); |
613 |
|
|
exit(1); |
614 |
|
|
} |
615 |
|
|
EPWclose(epw); epw = NULL; |
616 |
greg |
2.34 |
if (!ntsteps) { |
617 |
|
|
fprintf(stderr, "%s: no valid time steps on input\n", progname); |
618 |
|
|
exit(1); |
619 |
greg |
2.1 |
} |
620 |
greg |
2.34 |
if (avgSky < 0) /* no matrix output? */ |
621 |
|
|
goto alldone; |
622 |
|
|
|
623 |
greg |
2.27 |
dif = 1./(double)ntsteps; /* average sky? */ |
624 |
|
|
for (i = 3*nskypatch*(avgSky&(ntsteps>1)); i--; ) |
625 |
|
|
mtx_data[i] *= dif; |
626 |
greg |
2.1 |
/* write out matrix */ |
627 |
greg |
2.14 |
if (outfmt != 'a') |
628 |
|
|
SET_FILE_BINARY(stdout); |
629 |
greg |
2.1 |
#ifdef getc_unlocked |
630 |
|
|
flockfile(stdout); |
631 |
|
|
#endif |
632 |
|
|
if (verbose) |
633 |
|
|
fprintf(stderr, "%s: writing %smatrix with %d time steps...\n", |
634 |
greg |
2.28 |
progname, outfmt=='a' ? "" : "binary ", nstored); |
635 |
greg |
2.14 |
if (doheader) { |
636 |
|
|
newheader("RADIANCE", stdout); |
637 |
|
|
printargs(argc, argv, stdout); |
638 |
|
|
printf("LATLONG= %.8f %.8f\n", RadToDeg(s_latitude), |
639 |
|
|
-RadToDeg(s_longitude)); |
640 |
|
|
printf("NROWS=%d\n", nskypatch); |
641 |
greg |
2.28 |
printf("NCOLS=%d\n", nstored); |
642 |
greg |
2.14 |
printf("NCOMP=3\n"); |
643 |
greg |
2.29 |
if ((outfmt == 'f') | (outfmt == 'd')) |
644 |
|
|
fputendian(stdout); |
645 |
greg |
2.18 |
fputformat((char *)getfmtname(outfmt), stdout); |
646 |
greg |
2.14 |
putchar('\n'); |
647 |
|
|
} |
648 |
greg |
2.1 |
/* patches are rows (outer sort) */ |
649 |
|
|
for (i = 0; i < nskypatch; i++) { |
650 |
|
|
mtx_offset = 3*i; |
651 |
|
|
switch (outfmt) { |
652 |
|
|
case 'a': |
653 |
greg |
2.28 |
for (j = 0; j < nstored; j++) { |
654 |
greg |
2.3 |
printf("%.3g %.3g %.3g\n", mtx_data[mtx_offset], |
655 |
greg |
2.1 |
mtx_data[mtx_offset+1], |
656 |
|
|
mtx_data[mtx_offset+2]); |
657 |
|
|
mtx_offset += 3*nskypatch; |
658 |
|
|
} |
659 |
greg |
2.28 |
if (nstored > 1) |
660 |
greg |
2.2 |
fputc('\n', stdout); |
661 |
greg |
2.1 |
break; |
662 |
|
|
case 'f': |
663 |
greg |
2.28 |
for (j = 0; j < nstored; j++) { |
664 |
greg |
2.23 |
putbinary(mtx_data+mtx_offset, sizeof(float), 3, |
665 |
greg |
2.1 |
stdout); |
666 |
|
|
mtx_offset += 3*nskypatch; |
667 |
|
|
} |
668 |
|
|
break; |
669 |
|
|
case 'd': |
670 |
greg |
2.28 |
for (j = 0; j < nstored; j++) { |
671 |
greg |
2.1 |
double ment[3]; |
672 |
|
|
ment[0] = mtx_data[mtx_offset]; |
673 |
|
|
ment[1] = mtx_data[mtx_offset+1]; |
674 |
|
|
ment[2] = mtx_data[mtx_offset+2]; |
675 |
greg |
2.23 |
putbinary(ment, sizeof(double), 3, stdout); |
676 |
greg |
2.1 |
mtx_offset += 3*nskypatch; |
677 |
|
|
} |
678 |
|
|
break; |
679 |
|
|
} |
680 |
|
|
if (ferror(stdout)) |
681 |
|
|
goto writerr; |
682 |
|
|
} |
683 |
greg |
2.34 |
alldone: |
684 |
|
|
if (fflush(NULL) == EOF) |
685 |
greg |
2.1 |
goto writerr; |
686 |
|
|
if (verbose) |
687 |
|
|
fprintf(stderr, "%s: done.\n", progname); |
688 |
|
|
exit(0); |
689 |
|
|
userr: |
690 |
greg |
2.37 |
fprintf(stderr, "Usage: %s [-v][-h][-A][-d|-s|-n][-u][-D file [-M modfile]][-r deg][-m N][-g r g b][-c r g b][-o{f|d}][-O{0|1}] [tape.wea]\n", |
691 |
greg |
2.1 |
progname); |
692 |
|
|
exit(1); |
693 |
|
|
fmterr: |
694 |
greg |
2.34 |
fprintf(stderr, "%s: weather tape format error in header\n", progname); |
695 |
greg |
2.1 |
exit(1); |
696 |
|
|
writerr: |
697 |
|
|
fprintf(stderr, "%s: write error on output\n", progname); |
698 |
|
|
exit(1); |
699 |
|
|
} |
700 |
|
|
|
701 |
greg |
2.40 |
|
702 |
greg |
2.1 |
/* Return maximum of two doubles */ |
703 |
|
|
double dmax( double a, double b ) |
704 |
|
|
{ return (a > b) ? a : b; } |
705 |
|
|
|
706 |
|
|
/* Compute sky patch radiance values (modified by GW) */ |
707 |
|
|
void |
708 |
|
|
ComputeSky(float *parr) |
709 |
|
|
{ |
710 |
|
|
int index; /* Category index */ |
711 |
|
|
double norm_diff_illum; /* Normalized diffuse illuimnance */ |
712 |
|
|
int i; |
713 |
|
|
|
714 |
|
|
/* Calculate atmospheric precipitable water content */ |
715 |
|
|
apwc = CalcPrecipWater(dew_point); |
716 |
|
|
|
717 |
greg |
2.6 |
/* Calculate sun zenith angle (don't let it dip below horizon) */ |
718 |
|
|
/* Also limit minimum angle to keep circumsolar off zenith */ |
719 |
|
|
if (altitude <= 0.0) |
720 |
|
|
sun_zenith = DegToRad(90.0); |
721 |
|
|
else if (altitude >= DegToRad(87.0)) |
722 |
|
|
sun_zenith = DegToRad(3.0); |
723 |
|
|
else |
724 |
|
|
sun_zenith = DegToRad(90.0) - altitude; |
725 |
greg |
2.1 |
|
726 |
|
|
/* Compute the inputs for the calculation of the sky distribution */ |
727 |
|
|
|
728 |
|
|
if (input == 0) /* XXX never used */ |
729 |
|
|
{ |
730 |
|
|
/* Calculate irradiance */ |
731 |
|
|
diff_irrad = CalcDiffuseIrradiance(); |
732 |
|
|
dir_irrad = CalcDirectIrradiance(); |
733 |
|
|
|
734 |
|
|
/* Calculate illuminance */ |
735 |
|
|
index = GetCategoryIndex(); |
736 |
|
|
diff_illum = diff_irrad * CalcDiffuseIllumRatio(index); |
737 |
|
|
dir_illum = dir_irrad * CalcDirectIllumRatio(index); |
738 |
|
|
} |
739 |
|
|
else if (input == 1) |
740 |
|
|
{ |
741 |
|
|
sky_brightness = CalcSkyBrightness(); |
742 |
|
|
sky_clearness = CalcSkyClearness(); |
743 |
|
|
|
744 |
greg |
2.9 |
/* Limit sky clearness */ |
745 |
|
|
if (sky_clearness > 11.9) |
746 |
|
|
sky_clearness = 11.9; |
747 |
greg |
2.40 |
else if (sky_clearness < 1.0) |
748 |
|
|
sky_clearness = 1.0; |
749 |
greg |
2.9 |
|
750 |
|
|
/* Limit sky brightness */ |
751 |
|
|
if (sky_brightness < 0.01) |
752 |
greg |
2.11 |
sky_brightness = 0.01; |
753 |
greg |
2.40 |
else if (sky_brightness > 0.6) |
754 |
|
|
sky_brightness = 0.6; |
755 |
greg |
2.9 |
|
756 |
greg |
2.1 |
/* Calculate illuminance */ |
757 |
|
|
index = GetCategoryIndex(); |
758 |
|
|
diff_illum = diff_irrad * CalcDiffuseIllumRatio(index); |
759 |
|
|
dir_illum = dir_irrad * CalcDirectIllumRatio(index); |
760 |
|
|
} |
761 |
|
|
else if (input == 2) |
762 |
|
|
{ |
763 |
|
|
/* Calculate sky brightness and clearness from illuminance values */ |
764 |
|
|
index = CalcSkyParamFromIllum(); |
765 |
|
|
} |
766 |
|
|
|
767 |
greg |
2.11 |
if (output == 1) { /* hack for solar radiance */ |
768 |
|
|
diff_illum = diff_irrad * WHTEFFICACY; |
769 |
|
|
dir_illum = dir_irrad * WHTEFFICACY; |
770 |
|
|
} |
771 |
greg |
2.1 |
/* Compute ground radiance (include solar contribution if any) */ |
772 |
greg |
2.3 |
parr[0] = diff_illum; |
773 |
greg |
2.1 |
if (altitude > 0) |
774 |
greg |
2.3 |
parr[0] += dir_illum * sin(altitude); |
775 |
greg |
2.4 |
parr[2] = parr[1] = parr[0] *= (1./PI/WHTEFFICACY); |
776 |
|
|
multcolor(parr, grefl); |
777 |
greg |
2.1 |
|
778 |
greg |
2.32 |
if (bright(skycolor) <= 1e-4) { /* 0 sky component? */ |
779 |
|
|
memset(parr+3, 0, sizeof(float)*3*(nskypatch-1)); |
780 |
|
|
return; |
781 |
|
|
} |
782 |
greg |
2.1 |
/* Calculate Perez sky model parameters */ |
783 |
|
|
CalcPerezParam(sun_zenith, sky_clearness, sky_brightness, index); |
784 |
|
|
|
785 |
|
|
/* Calculate sky patch luminance values */ |
786 |
|
|
CalcSkyPatchLumin(parr); |
787 |
|
|
|
788 |
|
|
/* Calculate relative horizontal illuminance */ |
789 |
|
|
norm_diff_illum = CalcRelHorzIllum(parr); |
790 |
|
|
|
791 |
greg |
2.13 |
/* Check for zero sky -- make uniform in that case */ |
792 |
|
|
if (norm_diff_illum <= FTINY) { |
793 |
|
|
for (i = 1; i < nskypatch; i++) |
794 |
|
|
setcolor(parr+3*i, 1., 1., 1.); |
795 |
|
|
norm_diff_illum = PI; |
796 |
|
|
} |
797 |
greg |
2.1 |
/* Normalization coefficient */ |
798 |
|
|
norm_diff_illum = diff_illum / norm_diff_illum; |
799 |
|
|
|
800 |
|
|
/* Apply to sky patches to get absolute radiance values */ |
801 |
|
|
for (i = 1; i < nskypatch; i++) { |
802 |
greg |
2.7 |
scalecolor(parr+3*i, norm_diff_illum*(1./WHTEFFICACY)); |
803 |
greg |
2.1 |
multcolor(parr+3*i, skycolor); |
804 |
|
|
} |
805 |
|
|
} |
806 |
|
|
|
807 |
greg |
2.40 |
|
808 |
|
|
double |
809 |
|
|
solar_sunset(int month, int day) |
810 |
|
|
{ |
811 |
|
|
float W; |
812 |
|
|
W = -1 * (tan(s_latitude) * tan(sdec(jdate(month, day)))); |
813 |
|
|
return(12 + (M_PI / 2 - atan2(W, sqrt(1 - W * W))) * 180 / (M_PI * 15)); |
814 |
|
|
} |
815 |
|
|
|
816 |
|
|
|
817 |
|
|
double |
818 |
|
|
solar_sunrise(int month, int day) |
819 |
|
|
{ |
820 |
|
|
float W; |
821 |
|
|
W = -1 * (tan(s_latitude) * tan(sdec(jdate(month, day)))); |
822 |
|
|
return(12 - (M_PI / 2 - atan2(W, sqrt(1 - W * W))) * 180 / (M_PI * 15)); |
823 |
|
|
} |
824 |
|
|
|
825 |
|
|
|
826 |
greg |
2.1 |
/* Add in solar direct to nearest sky patches (GW) */ |
827 |
|
|
void |
828 |
|
|
AddDirect(float *parr) |
829 |
|
|
{ |
830 |
|
|
FVECT svec; |
831 |
greg |
2.3 |
double near_dprod[NSUNPATCH]; |
832 |
|
|
int near_patch[NSUNPATCH]; |
833 |
|
|
double wta[NSUNPATCH], wtot; |
834 |
greg |
2.1 |
int i, j, p; |
835 |
|
|
|
836 |
greg |
2.4 |
if (dir_illum <= 1e-4 || bright(suncolor) <= 1e-4) |
837 |
greg |
2.1 |
return; |
838 |
greg |
2.10 |
/* identify nsuns closest patches */ |
839 |
|
|
if (nsuns > NSUNPATCH) |
840 |
|
|
nsuns = NSUNPATCH; |
841 |
|
|
else if (nsuns <= 0) |
842 |
|
|
nsuns = 1; |
843 |
|
|
for (i = nsuns; i--; ) |
844 |
greg |
2.1 |
near_dprod[i] = -1.; |
845 |
|
|
vector(svec, altitude, azimuth); |
846 |
|
|
for (p = 1; p < nskypatch; p++) { |
847 |
|
|
FVECT pvec; |
848 |
|
|
double dprod; |
849 |
|
|
rh_vector(pvec, p); |
850 |
|
|
dprod = DOT(pvec, svec); |
851 |
greg |
2.10 |
for (i = 0; i < nsuns; i++) |
852 |
greg |
2.1 |
if (dprod > near_dprod[i]) { |
853 |
greg |
2.10 |
for (j = nsuns; --j > i; ) { |
854 |
greg |
2.1 |
near_dprod[j] = near_dprod[j-1]; |
855 |
|
|
near_patch[j] = near_patch[j-1]; |
856 |
|
|
} |
857 |
|
|
near_dprod[i] = dprod; |
858 |
|
|
near_patch[i] = p; |
859 |
|
|
break; |
860 |
|
|
} |
861 |
|
|
} |
862 |
|
|
wtot = 0; /* weight by proximity */ |
863 |
greg |
2.10 |
for (i = nsuns; i--; ) |
864 |
greg |
2.1 |
wtot += wta[i] = 1./(1.002 - near_dprod[i]); |
865 |
|
|
/* add to nearest patch radiances */ |
866 |
greg |
2.10 |
for (i = nsuns; i--; ) { |
867 |
greg |
2.2 |
float *pdest = parr + 3*near_patch[i]; |
868 |
greg |
2.10 |
float val_add = wta[i] * dir_illum / (WHTEFFICACY * wtot); |
869 |
|
|
|
870 |
|
|
val_add /= (fixed_sun_sa > 0) ? fixed_sun_sa |
871 |
|
|
: rh_dom[near_patch[i]] ; |
872 |
greg |
2.4 |
*pdest++ += val_add*suncolor[0]; |
873 |
|
|
*pdest++ += val_add*suncolor[1]; |
874 |
|
|
*pdest++ += val_add*suncolor[2]; |
875 |
greg |
2.2 |
} |
876 |
greg |
2.1 |
} |
877 |
|
|
|
878 |
greg |
2.35 |
/* Output a sun to indicated file if appropriate for this time step */ |
879 |
|
|
void |
880 |
greg |
2.36 |
OutputSun(int id, int goodsun, FILE *fp, FILE *mfp) |
881 |
greg |
2.35 |
{ |
882 |
|
|
double srad; |
883 |
|
|
FVECT sv; |
884 |
|
|
|
885 |
greg |
2.36 |
srad = DegToRad(SUN_ANG_DEG/2.); |
886 |
|
|
srad = goodsun ? dir_illum/(WHTEFFICACY * PI*srad*srad) : 0; |
887 |
greg |
2.35 |
vector(sv, altitude, azimuth); |
888 |
|
|
fprintf(fp, "\nvoid light solar%d\n0\n0\n", id); |
889 |
|
|
fprintf(fp, "3 %.3e %.3e %.3e\n", srad*suncolor[0], |
890 |
|
|
srad*suncolor[1], srad*suncolor[2]); |
891 |
|
|
fprintf(fp, "\nsolar%d source sun%d\n0\n0\n", id, id); |
892 |
|
|
fprintf(fp, "4 %.6f %.6f %.6f %.4f\n", sv[0], sv[1], sv[2], SUN_ANG_DEG); |
893 |
greg |
2.36 |
|
894 |
|
|
if (mfp != NULL) /* saving modifier IDs? */ |
895 |
|
|
fprintf(mfp, "solar%d\n", id); |
896 |
greg |
2.35 |
} |
897 |
|
|
|
898 |
greg |
2.1 |
/* Initialize Reinhart sky patch positions (GW) */ |
899 |
|
|
int |
900 |
|
|
rh_init(void) |
901 |
|
|
{ |
902 |
|
|
#define NROW 7 |
903 |
|
|
static const int tnaz[NROW] = {30, 30, 24, 24, 18, 12, 6}; |
904 |
|
|
const double alpha = (PI/2.)/(NROW*rhsubdiv + .5); |
905 |
|
|
int p, i, j; |
906 |
|
|
/* allocate patch angle arrays */ |
907 |
|
|
nskypatch = 0; |
908 |
|
|
for (p = 0; p < NROW; p++) |
909 |
|
|
nskypatch += tnaz[p]; |
910 |
|
|
nskypatch *= rhsubdiv*rhsubdiv; |
911 |
|
|
nskypatch += 2; |
912 |
|
|
rh_palt = (float *)malloc(sizeof(float)*nskypatch); |
913 |
|
|
rh_pazi = (float *)malloc(sizeof(float)*nskypatch); |
914 |
|
|
rh_dom = (float *)malloc(sizeof(float)*nskypatch); |
915 |
|
|
if ((rh_palt == NULL) | (rh_pazi == NULL) | (rh_dom == NULL)) { |
916 |
|
|
fprintf(stderr, "%s: out of memory in rh_init()\n", progname); |
917 |
|
|
exit(1); |
918 |
|
|
} |
919 |
|
|
rh_palt[0] = -PI/2.; /* ground & zenith patches */ |
920 |
|
|
rh_pazi[0] = 0.; |
921 |
|
|
rh_dom[0] = 2.*PI; |
922 |
|
|
rh_palt[nskypatch-1] = PI/2.; |
923 |
|
|
rh_pazi[nskypatch-1] = 0.; |
924 |
|
|
rh_dom[nskypatch-1] = 2.*PI*(1. - cos(alpha*.5)); |
925 |
|
|
p = 1; /* "normal" patches */ |
926 |
|
|
for (i = 0; i < NROW*rhsubdiv; i++) { |
927 |
|
|
const float ralt = alpha*(i + .5); |
928 |
|
|
const int ninrow = tnaz[i/rhsubdiv]*rhsubdiv; |
929 |
greg |
2.3 |
const float dom = 2.*PI*(sin(alpha*(i+1)) - sin(alpha*i)) / |
930 |
|
|
(double)ninrow; |
931 |
greg |
2.1 |
for (j = 0; j < ninrow; j++) { |
932 |
|
|
rh_palt[p] = ralt; |
933 |
|
|
rh_pazi[p] = 2.*PI * j / (double)ninrow; |
934 |
|
|
rh_dom[p++] = dom; |
935 |
|
|
} |
936 |
|
|
} |
937 |
|
|
return nskypatch; |
938 |
|
|
#undef NROW |
939 |
|
|
} |
940 |
|
|
|
941 |
|
|
/* Resize daylight matrix (GW) */ |
942 |
|
|
float * |
943 |
|
|
resize_dmatrix(float *mtx_data, int nsteps, int npatch) |
944 |
|
|
{ |
945 |
|
|
if (mtx_data == NULL) |
946 |
|
|
mtx_data = (float *)malloc(sizeof(float)*3*nsteps*npatch); |
947 |
|
|
else |
948 |
|
|
mtx_data = (float *)realloc(mtx_data, |
949 |
|
|
sizeof(float)*3*nsteps*npatch); |
950 |
|
|
if (mtx_data == NULL) { |
951 |
|
|
fprintf(stderr, "%s: out of memory in resize_dmatrix(%d,%d)\n", |
952 |
|
|
progname, nsteps, npatch); |
953 |
|
|
exit(1); |
954 |
|
|
} |
955 |
|
|
return(mtx_data); |
956 |
|
|
} |
957 |
|
|
|
958 |
|
|
/* Determine category index */ |
959 |
|
|
int GetCategoryIndex() |
960 |
|
|
{ |
961 |
|
|
int index; /* Loop index */ |
962 |
|
|
|
963 |
|
|
for (index = 0; index < 8; index++) |
964 |
|
|
if ((sky_clearness >= SkyClearCat[index].lower) && |
965 |
|
|
(sky_clearness < SkyClearCat[index].upper)) |
966 |
|
|
break; |
967 |
|
|
|
968 |
|
|
return index; |
969 |
|
|
} |
970 |
|
|
|
971 |
|
|
/* Calculate diffuse illuminance to diffuse irradiance ratio */ |
972 |
|
|
|
973 |
|
|
/* Reference: Perez, R., P. Ineichen, R. Seals, J. Michalsky, and R. */ |
974 |
|
|
/* Stewart. 1990. ìModeling Daylight Availability and */ |
975 |
|
|
/* Irradiance Components from Direct and Global */ |
976 |
|
|
/* Irradiance,î Solar Energy 44(5):271-289, Eqn. 7. */ |
977 |
|
|
|
978 |
|
|
double CalcDiffuseIllumRatio( int index ) |
979 |
|
|
{ |
980 |
|
|
ModelCoeff const *pnle; /* Category coefficient pointer */ |
981 |
|
|
|
982 |
|
|
/* Get category coefficient pointer */ |
983 |
|
|
pnle = &(DiffuseLumEff[index]); |
984 |
|
|
|
985 |
|
|
return pnle->a + pnle->b * apwc + pnle->c * cos(sun_zenith) + |
986 |
|
|
pnle->d * log(sky_brightness); |
987 |
|
|
} |
988 |
|
|
|
989 |
|
|
/* Calculate direct illuminance to direct irradiance ratio */ |
990 |
|
|
|
991 |
|
|
/* Reference: Perez, R., P. Ineichen, R. Seals, J. Michalsky, and R. */ |
992 |
|
|
/* Stewart. 1990. ìModeling Daylight Availability and */ |
993 |
|
|
/* Irradiance Components from Direct and Global */ |
994 |
|
|
/* Irradiance,î Solar Energy 44(5):271-289, Eqn. 8. */ |
995 |
|
|
|
996 |
|
|
double CalcDirectIllumRatio( int index ) |
997 |
|
|
{ |
998 |
|
|
ModelCoeff const *pnle; /* Category coefficient pointer */ |
999 |
|
|
|
1000 |
|
|
/* Get category coefficient pointer */ |
1001 |
|
|
pnle = &(DirectLumEff[index]); |
1002 |
|
|
|
1003 |
|
|
/* Calculate direct illuminance from direct irradiance */ |
1004 |
|
|
|
1005 |
|
|
return dmax((pnle->a + pnle->b * apwc + pnle->c * exp(5.73 * |
1006 |
|
|
sun_zenith - 5.0) + pnle->d * sky_brightness), |
1007 |
|
|
0.0); |
1008 |
|
|
} |
1009 |
|
|
|
1010 |
|
|
/* Calculate sky brightness */ |
1011 |
|
|
|
1012 |
|
|
/* Reference: Perez, R., P. Ineichen, R. Seals, J. Michalsky, and R. */ |
1013 |
|
|
/* Stewart. 1990. ìModeling Daylight Availability and */ |
1014 |
|
|
/* Irradiance Components from Direct and Global */ |
1015 |
|
|
/* Irradiance,î Solar Energy 44(5):271-289, Eqn. 2. */ |
1016 |
|
|
|
1017 |
|
|
double CalcSkyBrightness() |
1018 |
|
|
{ |
1019 |
|
|
return diff_irrad * CalcAirMass() / (DC_SolarConstantE * |
1020 |
|
|
CalcEccentricity()); |
1021 |
|
|
} |
1022 |
|
|
|
1023 |
|
|
/* Calculate sky clearness */ |
1024 |
|
|
|
1025 |
|
|
/* Reference: Perez, R., P. Ineichen, R. Seals, J. Michalsky, and R. */ |
1026 |
|
|
/* Stewart. 1990. ìModeling Daylight Availability and */ |
1027 |
|
|
/* Irradiance Components from Direct and Global */ |
1028 |
|
|
/* Irradiance,î Solar Energy 44(5):271-289, Eqn. 1. */ |
1029 |
|
|
|
1030 |
|
|
double CalcSkyClearness() |
1031 |
|
|
{ |
1032 |
|
|
double sz_cubed; /* Sun zenith angle cubed */ |
1033 |
|
|
|
1034 |
|
|
/* Calculate sun zenith angle cubed */ |
1035 |
greg |
2.11 |
sz_cubed = sun_zenith*sun_zenith*sun_zenith; |
1036 |
greg |
2.1 |
|
1037 |
|
|
return ((diff_irrad + dir_irrad) / diff_irrad + 1.041 * |
1038 |
|
|
sz_cubed) / (1.0 + 1.041 * sz_cubed); |
1039 |
|
|
} |
1040 |
|
|
|
1041 |
|
|
/* Calculate diffuse horizontal irradiance from Perez sky brightness */ |
1042 |
|
|
|
1043 |
|
|
/* Reference: Perez, R., P. Ineichen, R. Seals, J. Michalsky, and R. */ |
1044 |
|
|
/* Stewart. 1990. ìModeling Daylight Availability and */ |
1045 |
|
|
/* Irradiance Components from Direct and Global */ |
1046 |
|
|
/* Irradiance,î Solar Energy 44(5):271-289, Eqn. 2 */ |
1047 |
|
|
/* (inverse). */ |
1048 |
|
|
|
1049 |
|
|
double CalcDiffuseIrradiance() |
1050 |
|
|
{ |
1051 |
|
|
return sky_brightness * DC_SolarConstantE * CalcEccentricity() / |
1052 |
|
|
CalcAirMass(); |
1053 |
|
|
} |
1054 |
|
|
|
1055 |
|
|
/* Calculate direct normal irradiance from Perez sky clearness */ |
1056 |
|
|
|
1057 |
|
|
/* Reference: Perez, R., P. Ineichen, R. Seals, J. Michalsky, and R. */ |
1058 |
|
|
/* Stewart. 1990. ìModeling Daylight Availability and */ |
1059 |
|
|
/* Irradiance Components from Direct and Global */ |
1060 |
|
|
/* Irradiance,î Solar Energy 44(5):271-289, Eqn. 1 */ |
1061 |
|
|
/* (inverse). */ |
1062 |
|
|
|
1063 |
|
|
double CalcDirectIrradiance() |
1064 |
|
|
{ |
1065 |
|
|
return CalcDiffuseIrradiance() * ((sky_clearness - 1.0) * (1 + 1.041 |
1066 |
greg |
2.11 |
* sun_zenith*sun_zenith*sun_zenith)); |
1067 |
greg |
2.1 |
} |
1068 |
|
|
|
1069 |
|
|
/* Calculate sky brightness and clearness from illuminance values */ |
1070 |
|
|
int CalcSkyParamFromIllum() |
1071 |
|
|
{ |
1072 |
|
|
double test1 = 0.1; |
1073 |
|
|
double test2 = 0.1; |
1074 |
|
|
int counter = 0; |
1075 |
|
|
int index = 0; /* Category index */ |
1076 |
|
|
|
1077 |
|
|
/* Convert illuminance to irradiance */ |
1078 |
|
|
diff_irrad = diff_illum * DC_SolarConstantE / |
1079 |
|
|
(DC_SolarConstantL * 1000.0); |
1080 |
|
|
dir_irrad = dir_illum * DC_SolarConstantE / |
1081 |
|
|
(DC_SolarConstantL * 1000.0); |
1082 |
|
|
|
1083 |
|
|
/* Calculate sky brightness and clearness */ |
1084 |
|
|
sky_brightness = CalcSkyBrightness(); |
1085 |
|
|
sky_clearness = CalcSkyClearness(); |
1086 |
|
|
|
1087 |
|
|
/* Limit sky clearness */ |
1088 |
|
|
if (sky_clearness > 12.0) |
1089 |
|
|
sky_clearness = 12.0; |
1090 |
|
|
|
1091 |
|
|
/* Limit sky brightness */ |
1092 |
greg |
2.9 |
if (sky_brightness < 0.01) |
1093 |
greg |
2.1 |
sky_brightness = 0.01; |
1094 |
|
|
|
1095 |
greg |
2.40 |
if (sky_clearness < 1.0000) |
1096 |
|
|
{ |
1097 |
|
|
sky_clearness = 1.0000; |
1098 |
|
|
} |
1099 |
|
|
|
1100 |
|
|
if (sky_brightness > 0.6) |
1101 |
|
|
{ |
1102 |
|
|
sky_brightness = 0.6; |
1103 |
|
|
} |
1104 |
|
|
|
1105 |
greg |
2.1 |
while (((fabs(diff_irrad - test1) > 10.0) || |
1106 |
|
|
(fabs(dir_irrad - test2) > 10.0)) && !(counter == 5)) |
1107 |
|
|
{ |
1108 |
|
|
test1 = diff_irrad; |
1109 |
|
|
test2 = dir_irrad; |
1110 |
|
|
counter++; |
1111 |
|
|
|
1112 |
|
|
/* Convert illuminance to irradiance */ |
1113 |
|
|
index = GetCategoryIndex(); |
1114 |
|
|
diff_irrad = diff_illum / CalcDiffuseIllumRatio(index); |
1115 |
greg |
2.26 |
dir_irrad = CalcDirectIllumRatio(index); |
1116 |
|
|
if (dir_irrad > 0.1) |
1117 |
|
|
dir_irrad = dir_illum / dir_irrad; |
1118 |
greg |
2.1 |
|
1119 |
|
|
/* Calculate sky brightness and clearness */ |
1120 |
|
|
sky_brightness = CalcSkyBrightness(); |
1121 |
|
|
sky_clearness = CalcSkyClearness(); |
1122 |
|
|
|
1123 |
|
|
/* Limit sky clearness */ |
1124 |
|
|
if (sky_clearness > 12.0) |
1125 |
|
|
sky_clearness = 12.0; |
1126 |
|
|
|
1127 |
|
|
/* Limit sky brightness */ |
1128 |
greg |
2.9 |
if (sky_brightness < 0.01) |
1129 |
greg |
2.1 |
sky_brightness = 0.01; |
1130 |
greg |
2.40 |
|
1131 |
|
|
if (sky_clearness < 1.0000) |
1132 |
|
|
{ |
1133 |
|
|
sky_clearness = 1.0000; |
1134 |
|
|
} |
1135 |
|
|
|
1136 |
|
|
if (sky_brightness > 0.6) |
1137 |
|
|
{ |
1138 |
|
|
sky_brightness = 0.6; |
1139 |
|
|
} |
1140 |
greg |
2.1 |
} |
1141 |
|
|
|
1142 |
|
|
return GetCategoryIndex(); |
1143 |
|
|
} |
1144 |
|
|
|
1145 |
|
|
/* Calculate relative luminance */ |
1146 |
|
|
|
1147 |
|
|
/* Reference: Perez, R., R. Seals, and J. Michalsky. 1993. */ |
1148 |
|
|
/* ìAll-Weather Model for Sky Luminance Distribution - */ |
1149 |
|
|
/* Preliminary Configuration and Validation,î Solar Energy */ |
1150 |
|
|
/* 50(3):235-245, Eqn. 1. */ |
1151 |
|
|
|
1152 |
|
|
double CalcRelLuminance( double gamma, double zeta ) |
1153 |
|
|
{ |
1154 |
|
|
return (1.0 + perez_param[0] * exp(perez_param[1] / cos(zeta))) * |
1155 |
|
|
(1.0 + perez_param[2] * exp(perez_param[3] * gamma) + |
1156 |
|
|
perez_param[4] * cos(gamma) * cos(gamma)); |
1157 |
|
|
} |
1158 |
|
|
|
1159 |
|
|
/* Calculate Perez sky model parameters */ |
1160 |
|
|
|
1161 |
|
|
/* Reference: Perez, R., R. Seals, and J. Michalsky. 1993. */ |
1162 |
|
|
/* ìAll-Weather Model for Sky Luminance Distribution - */ |
1163 |
|
|
/* Preliminary Configuration and Validation,î Solar Energy */ |
1164 |
|
|
/* 50(3):235-245, Eqns. 6 - 8. */ |
1165 |
|
|
|
1166 |
|
|
void CalcPerezParam( double sz, double epsilon, double delta, |
1167 |
|
|
int index ) |
1168 |
|
|
{ |
1169 |
|
|
double x[5][4]; /* Coefficents a, b, c, d, e */ |
1170 |
|
|
int i, j; /* Loop indices */ |
1171 |
|
|
|
1172 |
|
|
/* Limit sky brightness */ |
1173 |
|
|
if (epsilon > 1.065 && epsilon < 2.8) |
1174 |
|
|
{ |
1175 |
|
|
if (delta < 0.2) |
1176 |
|
|
delta = 0.2; |
1177 |
|
|
} |
1178 |
|
|
|
1179 |
|
|
/* Get Perez coefficients */ |
1180 |
|
|
for (i = 0; i < 5; i++) |
1181 |
|
|
for (j = 0; j < 4; j++) |
1182 |
|
|
x[i][j] = PerezCoeff[index][4 * i + j]; |
1183 |
|
|
|
1184 |
|
|
if (index != 0) |
1185 |
|
|
{ |
1186 |
|
|
/* Calculate parameter a, b, c, d and e (Eqn. 6) */ |
1187 |
|
|
for (i = 0; i < 5; i++) |
1188 |
|
|
perez_param[i] = x[i][0] + x[i][1] * sz + delta * (x[i][2] + |
1189 |
|
|
x[i][3] * sz); |
1190 |
|
|
} |
1191 |
|
|
else |
1192 |
|
|
{ |
1193 |
|
|
/* Parameters a, b and e (Eqn. 6) */ |
1194 |
|
|
perez_param[0] = x[0][0] + x[0][1] * sz + delta * (x[0][2] + |
1195 |
|
|
x[0][3] * sz); |
1196 |
|
|
perez_param[1] = x[1][0] + x[1][1] * sz + delta * (x[1][2] + |
1197 |
|
|
x[1][3] * sz); |
1198 |
|
|
perez_param[4] = x[4][0] + x[4][1] * sz + delta * (x[4][2] + |
1199 |
|
|
x[4][3] * sz); |
1200 |
|
|
|
1201 |
|
|
/* Parameter c (Eqn. 7) */ |
1202 |
|
|
perez_param[2] = exp(pow(delta * (x[2][0] + x[2][1] * sz), |
1203 |
|
|
x[2][2])) - x[2][3]; |
1204 |
|
|
|
1205 |
|
|
/* Parameter d (Eqn. 8) */ |
1206 |
|
|
perez_param[3] = -exp(delta * (x[3][0] + x[3][1] * sz)) + |
1207 |
|
|
x[3][2] + delta * x[3][3]; |
1208 |
|
|
} |
1209 |
|
|
} |
1210 |
|
|
|
1211 |
|
|
/* Calculate relative horizontal illuminance (modified by GW) */ |
1212 |
|
|
|
1213 |
|
|
/* Reference: Perez, R., R. Seals, and J. Michalsky. 1993. */ |
1214 |
|
|
/* ìAll-Weather Model for Sky Luminance Distribution - */ |
1215 |
|
|
/* Preliminary Configuration and Validation,î Solar Energy */ |
1216 |
|
|
/* 50(3):235-245, Eqn. 3. */ |
1217 |
|
|
|
1218 |
|
|
double CalcRelHorzIllum( float *parr ) |
1219 |
|
|
{ |
1220 |
|
|
int i; |
1221 |
|
|
double rh_illum = 0.0; /* Relative horizontal illuminance */ |
1222 |
|
|
|
1223 |
|
|
for (i = 1; i < nskypatch; i++) |
1224 |
greg |
2.7 |
rh_illum += parr[3*i+1] * rh_cos(i) * rh_dom[i]; |
1225 |
greg |
2.1 |
|
1226 |
greg |
2.7 |
return rh_illum; |
1227 |
greg |
2.1 |
} |
1228 |
|
|
|
1229 |
|
|
/* Calculate earth orbit eccentricity correction factor */ |
1230 |
|
|
|
1231 |
|
|
/* Reference: Sen, Z. 2008. Solar Energy Fundamental and Modeling */ |
1232 |
|
|
/* Techniques. Springer, p. 72. */ |
1233 |
|
|
|
1234 |
|
|
double CalcEccentricity() |
1235 |
|
|
{ |
1236 |
|
|
double day_angle; /* Day angle (radians) */ |
1237 |
|
|
double E0; /* Eccentricity */ |
1238 |
|
|
|
1239 |
|
|
/* Calculate day angle */ |
1240 |
|
|
day_angle = (julian_date - 1.0) * (2.0 * PI / 365.0); |
1241 |
|
|
|
1242 |
|
|
/* Calculate eccentricity */ |
1243 |
|
|
E0 = 1.00011 + 0.034221 * cos(day_angle) + 0.00128 * sin(day_angle) |
1244 |
|
|
+ 0.000719 * cos(2.0 * day_angle) + 0.000077 * sin(2.0 * |
1245 |
|
|
day_angle); |
1246 |
|
|
|
1247 |
|
|
return E0; |
1248 |
|
|
} |
1249 |
|
|
|
1250 |
|
|
/* Calculate atmospheric precipitable water content */ |
1251 |
|
|
|
1252 |
|
|
/* Reference: Perez, R., P. Ineichen, R. Seals, J. Michalsky, and R. */ |
1253 |
|
|
/* Stewart. 1990. ìModeling Daylight Availability and */ |
1254 |
|
|
/* Irradiance Components from Direct and Global */ |
1255 |
|
|
/* Irradiance,î Solar Energy 44(5):271-289, Eqn. 3. */ |
1256 |
|
|
|
1257 |
|
|
/* Note: The default surface dew point temperature is 11 deg. C */ |
1258 |
|
|
/* (52 deg. F). Typical values are: */ |
1259 |
|
|
|
1260 |
|
|
/* Celsius Fahrenheit Human Perception */ |
1261 |
|
|
/* > 24 > 75 Extremely uncomfortable */ |
1262 |
|
|
/* 21 - 24 70 - 74 Very humid */ |
1263 |
|
|
/* 18 - 21 65 - 69 Somewhat uncomfortable */ |
1264 |
|
|
/* 16 - 18 60 - 64 OK for most people */ |
1265 |
|
|
/* 13 - 16 55 - 59 Comfortable */ |
1266 |
|
|
/* 10 - 12 50 - 54 Very comfortable */ |
1267 |
|
|
/* < 10 < 49 A bit dry for some */ |
1268 |
|
|
|
1269 |
|
|
double CalcPrecipWater( double dpt ) |
1270 |
|
|
{ return exp(0.07 * dpt - 0.075); } |
1271 |
|
|
|
1272 |
|
|
/* Calculate relative air mass */ |
1273 |
|
|
|
1274 |
|
|
/* Reference: Kasten, F. 1966. "A New Table and Approximation Formula */ |
1275 |
|
|
/* for the Relative Optical Air Mass," Arch. Meteorol. */ |
1276 |
|
|
/* Geophys. Bioklimataol. Ser. B14, pp. 206-233. */ |
1277 |
|
|
|
1278 |
|
|
/* Note: More sophisticated relative air mass models are */ |
1279 |
|
|
/* available, but they differ significantly only for */ |
1280 |
|
|
/* sun zenith angles greater than 80 degrees. */ |
1281 |
|
|
|
1282 |
|
|
double CalcAirMass() |
1283 |
|
|
{ |
1284 |
|
|
return (1.0 / (cos(sun_zenith) + 0.15 * pow(93.885 - |
1285 |
|
|
RadToDeg(sun_zenith), -1.253))); |
1286 |
|
|
} |
1287 |
|
|
|
1288 |
|
|
/* Calculate Perez All-Weather sky patch luminances (modified by GW) */ |
1289 |
|
|
|
1290 |
|
|
/* NOTE: The sky patches centers are determined in accordance with the */ |
1291 |
|
|
/* BRE-IDMP sky luminance measurement procedures. (See for example */ |
1292 |
|
|
/* Mardaljevic, J. 2001. "The BRE-IDMP Dataset: A New Benchmark */ |
1293 |
|
|
/* for the Validation of Illuminance Prediction Techniques," */ |
1294 |
|
|
/* Lighting Research & Technology 33(2):117-136.) */ |
1295 |
|
|
|
1296 |
|
|
void CalcSkyPatchLumin( float *parr ) |
1297 |
|
|
{ |
1298 |
|
|
int i; |
1299 |
|
|
double aas; /* Sun-sky point azimuthal angle */ |
1300 |
|
|
double sspa; /* Sun-sky point angle */ |
1301 |
|
|
double zsa; /* Zenithal sun angle */ |
1302 |
|
|
|
1303 |
|
|
for (i = 1; i < nskypatch; i++) |
1304 |
|
|
{ |
1305 |
|
|
/* Calculate sun-sky point azimuthal angle */ |
1306 |
|
|
aas = fabs(rh_pazi[i] - azimuth); |
1307 |
|
|
|
1308 |
|
|
/* Calculate zenithal sun angle */ |
1309 |
|
|
zsa = PI * 0.5 - rh_palt[i]; |
1310 |
|
|
|
1311 |
|
|
/* Calculate sun-sky point angle (Equation 8-20) */ |
1312 |
|
|
sspa = acos(cos(sun_zenith) * cos(zsa) + sin(sun_zenith) * |
1313 |
|
|
sin(zsa) * cos(aas)); |
1314 |
|
|
|
1315 |
|
|
/* Calculate patch luminance */ |
1316 |
|
|
parr[3*i] = CalcRelLuminance(sspa, zsa); |
1317 |
|
|
if (parr[3*i] < 0) parr[3*i] = 0; |
1318 |
|
|
parr[3*i+2] = parr[3*i+1] = parr[3*i]; |
1319 |
|
|
} |
1320 |
|
|
} |