ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/gendaylit.c
Revision: 2.9
Committed: Wed Jan 30 01:02:42 2013 UTC (11 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +281 -368 lines
Log Message:
Replaced gendaylit with new version by Wendelin Sprenger and Jan Wienold

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /* Copyright (c) 1994,2006 *Fraunhofer Institut for Solar Energy Systems
5 * Heidenhofstr. 2, D-79110 Freiburg, Germany
6 * *Agence de l'Environnement et de la Maitrise de l'Energie
7 * Centre de Valbonne, 500 route des Lucioles, 06565 Sophia Antipolis Cedex, France
8 * *BOUYGUES
9 * 1 Avenue Eugene Freyssinet, Saint-Quentin-Yvelines, France
10 *
11 * 24.1.2006 some adjustments for cygwin compilation, inclusion of RADIANCE3.7 libraries, by J. Wienold
12 * 2011/10/08 [email protected]:
13 * - integrated coeff_perez.dat and defangles.dat
14 * - avoid some segfaults caused by out of range parameters and
15 * - numerically dangerous range checks
16 */
17
18 /*
19 * gendaylit.c program to generate the angular distribution of the daylight.
20 * Our zenith is along the Z-axis, the X-axis
21 * points east, and the Y-axis points north.
22 */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <math.h>
27 #include <stdlib.h>
28
29 #include "color.h"
30 #include "paths.h"
31
32 #define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
33
34 double normsc();
35
36 /*static char *rcsid="$Header: /tmp_mnt/nfs/koll7/users/koll/jean/program/radiance/RAD/RCS/gendaylit.c,v 1.13 94/05/17 19:21:01 jean Exp Locker: jean $";*/
37
38 float coeff_perez[] = {
39 1.3525,-0.2576,-0.2690,-1.4366,-0.7670,0.0007,1.2734,-0.1233,2.8000,0.6004,1.2375,1.000,1.8734,0.6297,0.9738,0.2809,0.0356,-0.1246,-0.5718,0.9938,
40 -1.2219,-0.7730,1.4148,1.1016,-0.2054,0.0367,-3.9128,0.9156,6.9750,0.1774,6.4477,-0.1239,-1.5798,-0.5081,-1.7812,0.1080,0.2624,0.0672,-0.2190,-0.4285,
41 -1.1000,-0.2515,0.8952,0.0156,0.2782,-0.1812,-4.5000,1.1766,24.7219,-13.0812,-37.7000,34.8438,-5.0000,1.5218,3.9229,-2.6204,-0.0156,0.1597,0.4199,-0.5562,
42 -0.5484,-0.6654,-0.2672,0.7117,0.7234,-0.6219,-5.6812,2.6297,33.3389,-18.3000,-62.2500,52.0781,-3.5000,0.0016,1.1477,0.1062,0.4659,-0.3296,-0.0876,-0.0329,
43 -0.6000,-0.3566,-2.5000,2.3250,0.2937,0.0496,-5.6812,1.8415,21.0000,-4.7656,-21.5906,7.2492,-3.5000,-0.1554,1.4062,0.3988,0.0032,0.0766,-0.0656,-0.1294,
44 -1.0156,-0.3670,1.0078,1.4051,0.2875,-0.5328,-3.8500,3.3750,14.0000,-0.9999,-7.1406,7.5469,-3.4000,-0.1078,-1.0750,1.5702,-0.0672,0.4016,0.3017,-0.4844,
45 -1.0000,0.0211,0.5025,-0.5119,-0.3000,0.1922,0.7023,-1.6317,19.0000,-5.0000,1.2438,-1.9094,-4.0000,0.0250,0.3844,0.2656,1.0468,-0.3788,-2.4517,1.4656,
46 -1.0500,0.0289,0.4260,0.3590,-0.3250,0.1156,0.7781,0.0025,31.0625,-14.5000,-46.1148,55.3750,-7.2312,0.4050,13.3500,0.6234,1.5000,-0.6426,1.8564,0.5636};
47
48
49 float defangle_theta[] = {84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 12, 12, 12, 12, 12, 12, 0};
50
51 float defangle_phi[] = {0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 168, 180, 192, 204, 216, 228, 240, 252, 264, 276, 288, 300, 312, 324, 336, 348, 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 168, 180, 192, 204, 216, 228, 240, 252, 264, 276, 288, 300, 312, 324, 336, 348, 0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345, 0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345, 0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320, 340, 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 0, 60, 120, 180, 240, 300, 0};
52
53
54
55 /* Perez sky parametrization : epsilon and delta calculations from the direct and diffuse irradiances */
56 double sky_brightness();
57 double sky_clearness();
58
59 /* calculation of the direct and diffuse components from the Perez parametrization */
60 double diffuse_irradiance_from_sky_brightness();
61 double direct_irradiance_from_sky_clearness();
62
63
64 /* Perez global horizontal, diffuse horizontal and direct normal luminous efficacy models : input w(cm)=2cm, solar zenith angle(degrees); output efficacy(lm/W) */
65 double glob_h_effi_PEREZ();
66 double glob_h_diffuse_effi_PEREZ();
67 double direct_n_effi_PEREZ();
68 /*likelihood check of the epsilon, delta, direct and diffuse components*/
69 void check_parametrization();
70 void check_irradiances();
71 void check_illuminances();
72 void illu_to_irra_index();
73 void print_error_sky();
74
75
76 /* Perez sky luminance model */
77 double calc_rel_lum_perez(double dzeta,double gamma,double Z,
78 double epsilon,double Delta,float coeff_perez[]);
79 /* coefficients for the sky luminance perez model */
80 void coeff_lum_perez(double Z, double epsilon, double Delta, float coeff_perez[]);
81 double radians(double degres);
82 double degres(double radians);
83 void theta_phi_to_dzeta_gamma(double theta,double phi,double *dzeta,double *gamma, double Z);
84 double integ_lv(float *lv,float *theta);
85
86 void printdefaults();
87 void computesky();
88 void printhead(int ac, char** av);
89 void userror(char* msg);
90 void printsky();
91
92 FILE * frlibopen(char* fname);
93
94 /* astronomy and geometry*/
95 double get_eccentricity();
96 double air_mass();
97
98 extern int jdate(int month, int day);
99 extern double stadj(int jd);
100 extern double sdec(int jd);
101 extern double salt(double sd, double st);
102 extern double sazi(double sd, double st);
103
104
105 /* sun calculation constants */
106 extern double s_latitude;
107 extern double s_longitude;
108 extern double s_meridian;
109
110 const double AU = 149597890E3;
111 const double solar_constant_e = 1367; /* solar constant W/m^2 */
112 const double solar_constant_l = 127.5; /* solar constant klux */
113
114 const double half_sun_angle = 0.2665;
115 const double half_direct_angle = 2.85;
116
117 const double skyclearinf = 1.000; /* limitations for the variation of the Perez parameters */
118 const double skyclearsup = 12.1;
119 const double skybriginf = 0.01;
120 const double skybrigsup = 0.6;
121
122
123
124 /* required values */
125 int month, day; /* date */
126 double hour; /* time */
127 int tsolar; /* 0=standard, 1=solar */
128 double altitude, azimuth; /* or solar angles */
129
130
131
132 /* definition of the sky conditions through the Perez parametrization */
133 double skyclearness = 0;
134 double skybrightness = 0;
135 double solarradiance; /*radiance of the sun disk and of the circumsolar area*/
136 double diffuseilluminance, directilluminance, diffuseirradiance, directirradiance;
137 double sunzenith, daynumber=150, atm_preci_water=2;
138
139 double sunaltitude_border = 0;
140 double diffnormalization = 0;
141 double dirnormalization = 0;
142 double *c_perez;
143
144 int output=0; /*define the unit of the output (sky luminance or radiance): visible watt=0, solar watt=1, lumen=2*/
145 int input=0; /*define the input for the calulation*/
146
147 int suppress_warnings=0;
148
149 /* default values */
150 int cloudy = 0; /* 1=standard, 2=uniform */
151 int dosun = 1;
152 double zenithbr = -1.0;
153 double betaturbidity = 0.1;
154 double gprefl = 0.2;
155 int S_INTER=0;
156
157 /* computed values */
158 double sundir[3];
159 double groundbr = 0;
160 double F2;
161 double solarbr = 0.0;
162 int u_solar = 0; /* -1=irradiance, 1=radiance */
163
164 char *progname;
165 char errmsg[128];
166
167
168 int main(int argc, char** argv)
169 {
170 int i;
171
172 progname = argv[0];
173 if (argc == 2 && !strcmp(argv[1], "-defaults")) {
174 printdefaults();
175 return 0;
176 }
177 if (argc < 4)
178 userror("arg count");
179 if (!strcmp(argv[1], "-ang")) {
180 altitude = atof(argv[2]) * (M_PI/180);
181 azimuth = atof(argv[3]) * (M_PI/180);
182 month = 0;
183 } else {
184 month = atoi(argv[1]);
185 if (month < 1 || month > 12)
186 userror("bad month");
187 day = atoi(argv[2]);
188 if (day < 1 || day > 31)
189 userror("bad day");
190 hour = atof(argv[3]);
191 if (hour < 0 || hour >= 24)
192 userror("bad hour");
193 tsolar = argv[3][0] == '+';
194 }
195 for (i = 4; i < argc; i++)
196 if (argv[i][0] == '-' || argv[i][0] == '+')
197 switch (argv[i][1]) {
198 case 's':
199 cloudy = 0;
200 dosun = argv[i][0] == '+';
201 break;
202 case 'r':
203 case 'R':
204 u_solar = argv[i][1] == 'R' ? -1 : 1;
205 solarbr = atof(argv[++i]);
206 break;
207 case 'c':
208 cloudy = argv[i][0] == '+' ? 2 : 1;
209 dosun = 0;
210 break;
211 case 't':
212 betaturbidity = atof(argv[++i]);
213 break;
214 case 'w':
215 suppress_warnings = 1;
216 break;
217 case 'b':
218 zenithbr = atof(argv[++i]);
219 break;
220 case 'g':
221 gprefl = atof(argv[++i]);
222 break;
223 case 'a':
224 s_latitude = atof(argv[++i]) * (M_PI/180);
225 break;
226 case 'o':
227 s_longitude = atof(argv[++i]) * (M_PI/180);
228 break;
229 case 'm':
230 s_meridian = atof(argv[++i]) * (M_PI/180);
231 break;
232
233 case 'O':
234 output = atof(argv[++i]); /*define the unit of the output of the program :
235 sky and sun luminance/radiance (0==W visible, 1==W solar radiation, 2==lm) */
236 break;
237
238 case 'P':
239 input = 0; /* Perez parameters: epsilon, delta */
240 skyclearness = atof(argv[++i]);
241 skybrightness = atof(argv[++i]);
242 break;
243
244 case 'W': /* direct normal Irradiance [W/m^2] */
245 input = 1; /* diffuse horizontal Irrad. [W/m^2] */
246 directirradiance = atof(argv[++i]);
247 diffuseirradiance = atof(argv[++i]);
248 break;
249
250 case 'L': /* direct normal Illuminance [Lux] */
251 input = 2; /* diffuse horizontal Ill. [Lux] */
252 directilluminance = atof(argv[++i]);
253 diffuseilluminance = atof(argv[++i]);
254 break;
255
256 case 'G': /* direct horizontal Irradiance [W/m^2] */
257 input = 3; /* diffuse horizontal Irrad. [W/m^2] */
258 directirradiance = atof(argv[++i]);
259 diffuseirradiance = atof(argv[++i]);
260 break;
261
262 case 'l':
263 sunaltitude_border = atof(argv[++i]);
264 break;
265
266
267 default:
268 sprintf(errmsg, "unknown option: %s", argv[i]);
269 userror(errmsg);
270 }
271 else
272 userror("bad option");
273
274 if (fabs(s_meridian-s_longitude) > 30*M_PI/180)
275 fprintf(stderr,
276 "%s: warning: %.1f hours btwn. standard meridian and longitude\n",
277 progname, (s_longitude-s_meridian)*12/M_PI);
278
279
280 /* allocation dynamique de memoire pour les pointeurs */
281 if ( (c_perez = calloc(5, sizeof(double))) == NULL )
282 {
283 fprintf(stderr,"Out of memory error in function main !");
284 return 1;
285 }
286
287 printhead(argc, argv);
288
289 computesky();
290 printsky();
291
292 return 0;
293 }
294
295
296 void computesky() /* compute sky parameters */
297 {
298
299 /* new variables */
300 int j;
301 float *lv_mod; /* 145 luminance values*/
302 /* 145 directions for the calculation of the normalization coefficient, coefficient Perez model */
303 float *theta_o, *phi_o;
304 double dzeta, gamma;
305 double normfactor;
306
307
308
309 /* compute solar direction */
310
311 if (month) { /* from date and time */
312 int jd;
313 double sd, st;
314
315 jd = jdate(month, day); /* Julian date */
316 sd = sdec(jd); /* solar declination */
317 if (tsolar) /* solar time */
318 st = hour;
319 else
320 st = hour + stadj(jd);
321 altitude = salt(sd, st);
322 azimuth = sazi(sd, st);
323
324 daynumber = (double)jdate(month, day);
325
326 }
327
328
329
330
331
332 /* if loop for the -l option. 01/2013 Sprenger */
333
334 if (altitude*180/M_PI < sunaltitude_border) {
335
336 if (suppress_warnings==0)
337 fprintf(stderr, "Warning: sun altitude (%.3f degrees) below the border (%.3f degrees)\n",altitude*180/M_PI,sunaltitude_border);
338 print_error_sky();
339 exit(0);
340 }
341
342
343
344
345
346 if (!cloudy && altitude > 87.*M_PI/180.) {
347
348 if (suppress_warnings==0) {
349 fprintf(stderr,
350 "%s: warning - sun too close to zenith, reducing altitude to 87 degrees\n",
351 progname);
352 }
353 altitude = 87.*M_PI/180.;
354 }
355
356 sundir[0] = -sin(azimuth)*cos(altitude);
357 sundir[1] = -cos(azimuth)*cos(altitude);
358 sundir[2] = sin(altitude);
359
360
361 /* calculation for the new functions */
362 sunzenith = 90 - altitude*180/M_PI;
363
364
365
366 /* compute the inputs for the calculation of the light distribution over the sky*/
367 if (input==0)
368 {
369 check_parametrization();
370 diffuseirradiance = diffuse_irradiance_from_sky_brightness(); /*diffuse horizontal irradiance*/
371 directirradiance = direct_irradiance_from_sky_clearness();
372 check_irradiances();
373
374 if (output==0 || output==2)
375 {
376 diffuseilluminance = diffuseirradiance*glob_h_diffuse_effi_PEREZ();/*diffuse horizontal illuminance*/
377 directilluminance = directirradiance*direct_n_effi_PEREZ();
378 check_illuminances();
379 }
380 }
381
382
383 else if (input==1)
384 {
385 check_irradiances();
386 skybrightness = sky_brightness();
387 skyclearness = sky_clearness();
388 check_parametrization();
389
390 if (output==0 || output==2)
391 {
392 diffuseilluminance = diffuseirradiance*glob_h_diffuse_effi_PEREZ();/*diffuse horizontal illuminance*/
393 directilluminance = directirradiance*direct_n_effi_PEREZ();
394 check_illuminances();
395 }
396
397 }
398
399
400 else if (input==2)
401 {
402 check_illuminances();
403 illu_to_irra_index();
404 check_parametrization();
405 }
406
407
408 else if (input==3)
409 {
410 if (altitude<=0)
411 {
412 if (suppress_warnings==0)
413 fprintf(stderr, "Warning: solar zenith angle larger than 90 degrees; using zero irradiance to proceed\n");
414 directirradiance = 0;
415 diffuseirradiance = 0;
416 } else {
417 directirradiance=directirradiance/sin(altitude);
418 }
419 check_irradiances();
420 skybrightness = sky_brightness();
421 skyclearness = sky_clearness();
422 check_parametrization();
423
424 if (output==0 || output==2)
425 {
426 diffuseilluminance = diffuseirradiance*glob_h_diffuse_effi_PEREZ();/*diffuse horizontal illuminance*/
427 directilluminance = directirradiance*direct_n_effi_PEREZ();
428 check_illuminances();
429 }
430
431 }
432
433
434 else {fprintf(stderr,"error in giving the input arguments"); exit(1);}
435
436
437
438 /* normalization factor for the relative sky luminance distribution, diffuse part*/
439
440 if ( (lv_mod = malloc(145*sizeof(float))) == NULL)
441 {
442 fprintf(stderr,"Out of memory in function main");
443 exit(1);
444 }
445
446 /* read the angles */
447 theta_o = defangle_theta;
448 phi_o = defangle_phi;
449
450 /* parameters for the perez model */
451 coeff_lum_perez(radians(sunzenith), skyclearness, skybrightness, coeff_perez);
452
453 /*calculation of the modelled luminance */
454 for (j=0;j<145;j++)
455 {
456 theta_phi_to_dzeta_gamma(radians(*(theta_o+j)),radians(*(phi_o+j)),&dzeta,&gamma,radians(sunzenith));
457 *(lv_mod+j) = calc_rel_lum_perez(dzeta,gamma,radians(sunzenith),skyclearness,skybrightness,coeff_perez);
458 // printf("theta, phi, lv_mod %f\t %f\t %f\n", *(theta_o+j),*(phi_o+j),*(lv_mod+j));
459 }
460
461 /* integration of luminance for the normalization factor, diffuse part of the sky*/
462 diffnormalization = integ_lv(lv_mod, theta_o);
463 /*printf("perez integration %lf\n", diffnormalization);*/
464
465
466
467
468 /*normalization coefficient in lumen or in watt*/
469 if (output==0)
470 {
471 diffnormalization = diffuseilluminance/diffnormalization/WHTEFFICACY;
472 }
473 else if (output==1)
474 {
475 diffnormalization = diffuseirradiance/diffnormalization;
476 }
477 else if (output==2)
478 {
479 diffnormalization = diffuseilluminance/diffnormalization;
480 }
481
482 else {fprintf(stderr,"Wrong output specification.\n"); exit(1);}
483
484
485
486
487 /* calculation for the solar source */
488 if (output==0)
489 solarradiance = directilluminance/(2*M_PI*(1-cos(half_sun_angle*M_PI/180)))/WHTEFFICACY;
490
491 else if (output==1)
492 solarradiance = directirradiance/(2*M_PI*(1-cos(half_sun_angle*M_PI/180)));
493
494 else
495 solarradiance = directilluminance/(2*M_PI*(1-cos(half_sun_angle*M_PI/180)));
496
497
498
499
500 /* Compute the ground radiance */
501 zenithbr=calc_rel_lum_perez(0.0,radians(sunzenith),radians(sunzenith),skyclearness,skybrightness,coeff_perez);
502 zenithbr*=diffnormalization;
503
504 if (skyclearness==1)
505 normfactor = 0.777778;
506
507 if (skyclearness>=6)
508 {
509 F2 = 0.274*(0.91 + 10.0*exp(-3.0*(M_PI/2.0-altitude)) + 0.45*sundir[2]*sundir[2]);
510 normfactor = normsc()/F2/M_PI;
511 }
512
513 if ( (skyclearness>1) && (skyclearness<6) )
514 {
515 S_INTER=1;
516 F2 = (2.739 + .9891*sin(.3119+2.6*altitude)) * exp(-(M_PI/2.0-altitude)*(.4441+1.48*altitude));
517 normfactor = normsc()/F2/M_PI;
518 }
519
520 groundbr = zenithbr*normfactor;
521
522 if (dosun&&(skyclearness>1))
523 groundbr += 6.8e-5/M_PI*solarradiance*sundir[2];
524
525 groundbr *= gprefl;
526
527
528
529 return;
530 }
531
532
533
534
535 void print_error_sky()
536 {
537 sundir[0] = -sin(azimuth)*cos(altitude);
538 sundir[1] = -cos(azimuth)*cos(altitude);
539 sundir[2] = sin(altitude);
540
541 printf("\nvoid brightfunc skyfunc\n");
542 printf("2 skybright perezlum.cal\n");
543 printf("0\n");
544 printf("10 0.00 0.00 0.000 0.000 0.000 0.000 0.000 %f %f %f \n", sundir[0], sundir[1], sundir[2]);
545 }
546
547
548
549 void printsky() /* print out sky */
550 {
551 if (dosun&&(skyclearness>1))
552 {
553 printf("\nvoid light solar\n");
554 printf("0\n0\n");
555 printf("3 %.3e %.3e %.3e\n", solarradiance, solarradiance, solarradiance);
556 printf("\nsolar source sun\n");
557 printf("0\n0\n");
558 printf("4 %f %f %f %f\n", sundir[0], sundir[1], sundir[2], 2*half_sun_angle);
559 } else if (dosun) {
560 printf("\nvoid light solar\n");
561 printf("0\n0\n");
562 printf("3 0.0 0.0 0.0\n");
563 printf("\nsolar source sun\n");
564 printf("0\n0\n");
565 printf("4 %f %f %f %f\n", sundir[0], sundir[1], sundir[2], 2*half_sun_angle);
566 }
567
568 printf("\nvoid brightfunc skyfunc\n");
569 printf("2 skybright perezlum.cal\n");
570 printf("0\n");
571 printf("10 %.3e %.3e %lf %lf %lf %lf %lf %f %f %f \n", diffnormalization, groundbr,
572 *(c_perez+0),*(c_perez+1),*(c_perez+2),*(c_perez+3),*(c_perez+4),
573 sundir[0], sundir[1], sundir[2]);
574 }
575
576
577 void printdefaults() /* print default values */
578 {
579 printf("-g %f\t\t\t# Ground plane reflectance\n", gprefl);
580 if (zenithbr > 0.0)
581 printf("-b %f\t\t\t# Zenith radiance (watts/ster/m^2\n", zenithbr);
582 else
583 printf("-t %f\t\t\t# Atmospheric betaturbidity\n", betaturbidity);
584 printf("-a %f\t\t\t# Site latitude (degrees)\n", s_latitude*(180/M_PI));
585 printf("-o %f\t\t\t# Site longitude (degrees)\n", s_longitude*(180/M_PI));
586 printf("-m %f\t\t\t# Standard meridian (degrees)\n", s_meridian*(180/M_PI));
587 }
588
589
590 void userror(char* msg) /* print usage error and quit */
591 {
592 if (msg != NULL)
593 fprintf(stderr, "%s: Use error - %s\n", progname, msg);
594 fprintf(stderr, "Usage: %s month day hour [-P|-W|-L|-G] direct_value diffuse_value [options]\n", progname);
595 fprintf(stderr, "or: %s -ang altitude azimuth [-P|-W|-L|-G] direct_value diffuse_value [options]\n", progname);
596 fprintf(stderr, " -P epsilon delta (these are the Perez parameters) \n");
597 fprintf(stderr, " -W direct-normal-irradiance diffuse-horizontal-irradiance (W/m^2)\n");
598 fprintf(stderr, " -L direct-normal-illuminance diffuse-horizontal-illuminance (lux)\n");
599 fprintf(stderr, " -G direct-horizontal-irradiance diffuse-horizontal-irradiance (W/m^2)\n");
600 fprintf(stderr, " -O [0|1|2] (0=output in W/m^2/sr visible, 1=output in W/m^2/sr solar, 2=output in candela/m^2), default is 0 \n");
601 fprintf(stderr, " gendaylit version 2.00 (2013/01/28) \n");
602 exit(1);
603 }
604
605
606
607 double normsc() /* compute normalization factor (E0*F2/L0) */
608 {
609 static double nfc[2][5] = {
610 /* clear sky approx. */
611 {2.766521, 0.547665, -0.369832, 0.009237, 0.059229},
612 /* intermediate sky approx. */
613 {3.5556, -2.7152, -1.3081, 1.0660, 0.60227},
614 };
615 register double *nf;
616 double x, nsc;
617 register int i;
618 /* polynomial approximation */
619 nf = nfc[S_INTER];
620 x = (altitude - M_PI/4.0)/(M_PI/4.0);
621 nsc = nf[i=4];
622 while (i--)
623 nsc = nsc*x + nf[i];
624
625 return(nsc);
626 }
627
628
629
630 void printhead(int ac, char** av) /* print command header */
631 {
632 putchar('#');
633 while (ac--) {
634 putchar(' ');
635 fputs(*av++, stdout);
636 }
637 putchar('\n');
638 }
639
640
641
642
643
644
645 /* Perez models */
646
647 /* Perez global horizontal luminous efficacy model */
648 double glob_h_effi_PEREZ()
649 {
650
651 double value;
652 double category_bounds[10], a[10], b[10], c[10], d[10];
653 int category_total_number, category_number, i;
654
655
656 if ((skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<skybriginf || skybrightness>skybrigsup) && suppress_warnings==0)
657 fprintf(stderr, "Warning: skyclearness or skybrightness out of range in function glob_h_effi_PEREZ \n");
658
659 /* initialize category bounds (clearness index bounds) */
660
661 category_total_number = 8;
662
663 category_bounds[1] = 1;
664 category_bounds[2] = 1.065;
665 category_bounds[3] = 1.230;
666 category_bounds[4] = 1.500;
667 category_bounds[5] = 1.950;
668 category_bounds[6] = 2.800;
669 category_bounds[7] = 4.500;
670 category_bounds[8] = 6.200;
671 category_bounds[9] = 12.01;
672
673
674 /* initialize model coefficients */
675 a[1] = 96.63;
676 a[2] = 107.54;
677 a[3] = 98.73;
678 a[4] = 92.72;
679 a[5] = 86.73;
680 a[6] = 88.34;
681 a[7] = 78.63;
682 a[8] = 99.65;
683
684 b[1] = -0.47;
685 b[2] = 0.79;
686 b[3] = 0.70;
687 b[4] = 0.56;
688 b[5] = 0.98;
689 b[6] = 1.39;
690 b[7] = 1.47;
691 b[8] = 1.86;
692
693 c[1] = 11.50;
694 c[2] = 1.79;
695 c[3] = 4.40;
696 c[4] = 8.36;
697 c[5] = 7.10;
698 c[6] = 6.06;
699 c[7] = 4.93;
700 c[8] = -4.46;
701
702 d[1] = -9.16;
703 d[2] = -1.19;
704 d[3] = -6.95;
705 d[4] = -8.31;
706 d[5] = -10.94;
707 d[6] = -7.60;
708 d[7] = -11.37;
709 d[8] = -3.15;
710
711
712
713
714 for (i=1; i<=category_total_number; i++)
715 {
716 if ( (skyclearness >= category_bounds[i]) && (skyclearness < category_bounds[i+1]) )
717 category_number = i;
718 }
719
720 value = a[category_number] + b[category_number]*atm_preci_water +
721 c[category_number]*cos(sunzenith*M_PI/180) + d[category_number]*log(skybrightness);
722
723 return(value);
724 }
725
726
727 /* global horizontal diffuse efficacy model, according to PEREZ */
728 double glob_h_diffuse_effi_PEREZ()
729 {
730 double value;
731 double category_bounds[10], a[10], b[10], c[10], d[10];
732 int category_total_number, category_number, i;
733
734
735 if ((skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<skybriginf || skybrightness>skybrigsup) && suppress_warnings==0)
736 fprintf(stderr, "Warning: skyclearness or skybrightness out of range in function glob_h_diffuse_effi_PEREZ \n");
737
738 /* initialize category bounds (clearness index bounds) */
739
740 category_total_number = 8;
741
742 //XXX: category_bounds > 0.1
743 category_bounds[1] = 1;
744 category_bounds[2] = 1.065;
745 category_bounds[3] = 1.230;
746 category_bounds[4] = 1.500;
747 category_bounds[5] = 1.950;
748 category_bounds[6] = 2.800;
749 category_bounds[7] = 4.500;
750 category_bounds[8] = 6.200;
751 category_bounds[9] = 12.01;
752
753
754 /* initialize model coefficients */
755 a[1] = 97.24;
756 a[2] = 107.22;
757 a[3] = 104.97;
758 a[4] = 102.39;
759 a[5] = 100.71;
760 a[6] = 106.42;
761 a[7] = 141.88;
762 a[8] = 152.23;
763
764 b[1] = -0.46;
765 b[2] = 1.15;
766 b[3] = 2.96;
767 b[4] = 5.59;
768 b[5] = 5.94;
769 b[6] = 3.83;
770 b[7] = 1.90;
771 b[8] = 0.35;
772
773 c[1] = 12.00;
774 c[2] = 0.59;
775 c[3] = -5.53;
776 c[4] = -13.95;
777 c[5] = -22.75;
778 c[6] = -36.15;
779 c[7] = -53.24;
780 c[8] = -45.27;
781
782 d[1] = -8.91;
783 d[2] = -3.95;
784 d[3] = -8.77;
785 d[4] = -13.90;
786 d[5] = -23.74;
787 d[6] = -28.83;
788 d[7] = -14.03;
789 d[8] = -7.98;
790
791
792
793
794 category_number = -1;
795 for (i=1; i<=category_total_number; i++)
796 {
797 if ( (skyclearness >= category_bounds[i]) && (skyclearness < category_bounds[i+1]) )
798 category_number = i;
799 }
800
801 if (category_number == -1) {
802 if (suppress_warnings==0)
803 fprintf(stderr, "ERROR: Model parameters out of range\n");
804 print_error_sky();
805 exit(1);
806 }
807
808
809 value = a[category_number] + b[category_number]*atm_preci_water + c[category_number]*cos(sunzenith*M_PI/180) +
810 d[category_number]*log(skybrightness);
811
812 return(value);
813 }
814
815
816 /* direct normal efficacy model, according to PEREZ */
817
818 double direct_n_effi_PEREZ()
819
820 {
821 double value;
822 double category_bounds[10], a[10], b[10], c[10], d[10];
823 int category_total_number, category_number, i;
824
825
826 if ((skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<skybriginf || skybrightness>skybrigsup) && suppress_warnings==0)
827 fprintf(stderr, "Warning: skyclearness or skybrightness out of range in function direct_n_effi_PEREZ \n");
828
829
830 /* initialize category bounds (clearness index bounds) */
831
832 category_total_number = 8;
833
834 category_bounds[1] = 1;
835 category_bounds[2] = 1.065;
836 category_bounds[3] = 1.230;
837 category_bounds[4] = 1.500;
838 category_bounds[5] = 1.950;
839 category_bounds[6] = 2.800;
840 category_bounds[7] = 4.500;
841 category_bounds[8] = 6.200;
842 category_bounds[9] = 12.1;
843
844
845 /* initialize model coefficients */
846 a[1] = 57.20;
847 a[2] = 98.99;
848 a[3] = 109.83;
849 a[4] = 110.34;
850 a[5] = 106.36;
851 a[6] = 107.19;
852 a[7] = 105.75;
853 a[8] = 101.18;
854
855 b[1] = -4.55;
856 b[2] = -3.46;
857 b[3] = -4.90;
858 b[4] = -5.84;
859 b[5] = -3.97;
860 b[6] = -1.25;
861 b[7] = 0.77;
862 b[8] = 1.58;
863
864 c[1] = -2.98;
865 c[2] = -1.21;
866 c[3] = -1.71;
867 c[4] = -1.99;
868 c[5] = -1.75;
869 c[6] = -1.51;
870 c[7] = -1.26;
871 c[8] = -1.10;
872
873 d[1] = 117.12;
874 d[2] = 12.38;
875 d[3] = -8.81;
876 d[4] = -4.56;
877 d[5] = -6.16;
878 d[6] = -26.73;
879 d[7] = -34.44;
880 d[8] = -8.29;
881
882
883
884 for (i=1; i<=category_total_number; i++)
885 {
886 if ( (skyclearness >= category_bounds[i]) && (skyclearness < category_bounds[i+1]) )
887 category_number = i;
888 }
889
890 value = a[category_number] + b[category_number]*atm_preci_water + c[category_number]*exp(5.73*sunzenith*M_PI/180 - 5) + d[category_number]*skybrightness;
891
892 if (value < 0) value = 0;
893
894 return(value);
895 }
896
897
898 /*check the range of epsilon and delta indexes of the perez parametrization*/
899 void check_parametrization()
900 {
901 if (skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<skybriginf || skybrightness>skybrigsup)
902 {
903
904 /* limit sky clearness or sky brightness, 2009 11 13 by J. Wienold */
905 if (skyclearness<skyclearinf){
906 skyclearness=skyclearinf;
907 if (suppress_warnings==0)
908 fprintf(stderr,"Range warning: sky clearness too low (%lf)\n", skyclearness);
909 }
910 if (skyclearness>skyclearsup){
911 skyclearness=skyclearsup-0.1;
912 if (suppress_warnings==0)
913 fprintf(stderr,"Range warning: sky clearness too high (%lf)\n", skyclearness);
914 }
915 if (skybrightness<skybriginf){
916 skybrightness=skybriginf;
917 if (suppress_warnings==0)
918 fprintf(stderr,"Range warning: sky brightness too low (%lf)\n", skybrightness);
919 }
920 if (skybrightness>skybrigsup){
921 skybrightness=skybrigsup;
922 if (suppress_warnings==0)
923 fprintf(stderr,"Range warning: sky brightness too high (%lf)\n", skybrightness);
924 }
925
926 return; }
927 else return;
928 }
929
930
931 /* validity of the direct and diffuse components */
932 void check_illuminances()
933 {
934 if (directilluminance < 0) {
935 fprintf(stderr,"WARNING: direct illuminance < 0. Using 0.0\n");
936 directilluminance = 0.0;
937 }
938 if (diffuseilluminance < 0) {
939 fprintf(stderr,"WARNING: diffuse illuminance < 0. Using 0.0\n");
940 diffuseilluminance = 0.0;
941 }
942 if (directilluminance > solar_constant_l*1000.0) {
943 fprintf(stderr,"ERROR: direct illuminance exceeds solar constant\n");
944 exit(1);
945 }
946 }
947
948
949 void check_irradiances()
950 {
951 if (directirradiance < 0) {
952 fprintf(stderr,"WARNING: direct irradiance < 0. Using 0.0\n");
953 directirradiance = 0.0;
954 }
955 if (diffuseirradiance < 0) {
956 fprintf(stderr,"WARNING: diffuse irradiance < 0. Using 0.0\n");
957 diffuseirradiance = 0.0;
958 }
959 if (directirradiance > solar_constant_e) {
960 fprintf(stderr,"ERROR: direct irradiance exceeds solar constant\n");
961 exit(1);
962 }
963 }
964
965
966
967 /* Perez sky's brightness */
968 double sky_brightness()
969 {
970 double value;
971
972 value = diffuseirradiance * air_mass() / ( solar_constant_e*get_eccentricity());
973
974 return(value);
975 }
976
977
978 /* Perez sky's clearness */
979 double sky_clearness()
980 {
981 double value;
982
983 value = ( (diffuseirradiance + directirradiance)/(diffuseirradiance) + 1.041*sunzenith*M_PI/180*sunzenith*M_PI/180*sunzenith*M_PI/180 ) / (1 + 1.041*sunzenith*M_PI/180*sunzenith*M_PI/180*sunzenith*M_PI/180) ;
984
985 return(value);
986 }
987
988
989
990 /* diffus horizontal irradiance from Perez sky's brightness */
991 double diffuse_irradiance_from_sky_brightness()
992 {
993 double value;
994
995 value = skybrightness / air_mass() * ( solar_constant_e*get_eccentricity());
996
997 return(value);
998 }
999
1000
1001 /* direct normal irradiance from Perez sky's clearness */
1002 double direct_irradiance_from_sky_clearness()
1003 {
1004 double value;
1005
1006 value = diffuse_irradiance_from_sky_brightness();
1007 value = value * ( (skyclearness-1) * (1+1.041*sunzenith*M_PI/180*sunzenith*M_PI/180*sunzenith*M_PI/180) );
1008
1009 return(value);
1010 }
1011
1012
1013 void illu_to_irra_index()
1014 {
1015 double test1=0.1, test2=0.1, d_eff;
1016 int counter=0;
1017
1018 diffuseirradiance = diffuseilluminance*solar_constant_e/(solar_constant_l*1000);
1019 directirradiance = directilluminance*solar_constant_e/(solar_constant_l*1000);
1020 skyclearness = sky_clearness();
1021 skybrightness = sky_brightness();
1022 check_parametrization();
1023
1024 while ( ((fabs(diffuseirradiance-test1)>10) || (fabs(directirradiance-test2)>10)
1025 || skyclearness>skyclearinf || skyclearness<skyclearsup
1026 || skybrightness>skybriginf || skybrightness<skybrigsup )
1027 && !(counter==5) )
1028 {
1029
1030 test1=diffuseirradiance;
1031 test2=directirradiance;
1032 counter++;
1033
1034 diffuseirradiance = diffuseilluminance/glob_h_diffuse_effi_PEREZ();
1035 d_eff = direct_n_effi_PEREZ();
1036 if (d_eff < 0.1)
1037 directirradiance = 0;
1038 else
1039 directirradiance = directilluminance/d_eff;
1040
1041 skybrightness = sky_brightness();
1042 skyclearness = sky_clearness();
1043 check_parametrization();
1044
1045 }
1046
1047
1048 return;
1049 }
1050
1051 static int get_numlin(float epsilon)
1052 {
1053 if (epsilon < 1.065)
1054 return 0;
1055 else if (epsilon < 1.230)
1056 return 1;
1057 else if (epsilon < 1.500)
1058 return 2;
1059 else if (epsilon < 1.950)
1060 return 3;
1061 else if (epsilon < 2.800)
1062 return 4;
1063 else if (epsilon < 4.500)
1064 return 5;
1065 else if (epsilon < 6.200)
1066 return 6;
1067 return 7;
1068 }
1069
1070 /* sky luminance perez model */
1071 double calc_rel_lum_perez(double dzeta,double gamma,double Z,double epsilon,double Delta,float coeff_perez[])
1072 {
1073 float x[5][4];
1074 int i,j,num_lin;
1075 double c_perez[5];
1076
1077 if ( (epsilon < skyclearinf) || (epsilon >= skyclearsup) )
1078 {
1079 fprintf(stderr,"Epsilon out of range in function calc_rel_lum_perez!\n");
1080 exit(1);
1081 }
1082
1083 /* correction de modele de Perez solar energy ...*/
1084 if ( (epsilon > 1.065) && (epsilon < 2.8) )
1085 {
1086 if ( Delta < 0.2 ) Delta = 0.2;
1087 }
1088
1089 num_lin = get_numlin(epsilon);
1090
1091 for (i=0;i<5;i++)
1092 for (j=0;j<4;j++)
1093 {
1094 x[i][j] = *(coeff_perez + 20*num_lin + 4*i +j);
1095 /* printf("x %d %d vaut %f\n",i,j,x[i][j]); */
1096 }
1097
1098
1099 if (num_lin)
1100 {
1101 for (i=0;i<5;i++)
1102 c_perez[i] = x[i][0] + x[i][1]*Z + Delta * (x[i][2] + x[i][3]*Z);
1103 }
1104 else
1105 {
1106 c_perez[0] = x[0][0] + x[0][1]*Z + Delta * (x[0][2] + x[0][3]*Z);
1107 c_perez[1] = x[1][0] + x[1][1]*Z + Delta * (x[1][2] + x[1][3]*Z);
1108 c_perez[4] = x[4][0] + x[4][1]*Z + Delta * (x[4][2] + x[4][3]*Z);
1109 c_perez[2] = exp( pow(Delta*(x[2][0]+x[2][1]*Z),x[2][2])) - x[2][3];
1110 c_perez[3] = -exp( Delta*(x[3][0]+x[3][1]*Z) )+x[3][2]+Delta*x[3][3];
1111 }
1112
1113
1114 return (1 + c_perez[0]*exp(c_perez[1]/cos(dzeta)) ) *
1115 (1 + c_perez[2]*exp(c_perez[3]*gamma) +
1116 c_perez[4]*cos(gamma)*cos(gamma) );
1117 }
1118
1119
1120
1121 /* coefficients for the sky luminance perez model */
1122 void coeff_lum_perez(double Z, double epsilon, double Delta, float coeff_perez[])
1123 {
1124 float x[5][4];
1125 int i,j,num_lin;
1126
1127 if ( (epsilon < skyclearinf) || (epsilon >= skyclearsup) )
1128 {
1129 fprintf(stderr,"Epsilon out of range in function calc_rel_lum_perez !\n");
1130 exit(1);
1131 }
1132
1133 /* correction du modele de Perez solar energy ...*/
1134 if ( (epsilon > 1.065) && (epsilon < 2.8) )
1135 {
1136 if ( Delta < 0.2 ) Delta = 0.2;
1137 }
1138
1139 num_lin = get_numlin(epsilon);
1140
1141 //fprintf(stderr,"numlin %d\n", num_lin);
1142
1143 for (i=0;i<5;i++)
1144 for (j=0;j<4;j++)
1145 {
1146 x[i][j] = *(coeff_perez + 20*num_lin + 4*i +j);
1147 /* printf("x %d %d vaut %f\n",i,j,x[i][j]); */
1148 }
1149
1150
1151 if (num_lin)
1152 {
1153 for (i=0;i<5;i++)
1154 *(c_perez+i) = x[i][0] + x[i][1]*Z + Delta * (x[i][2] + x[i][3]*Z);
1155
1156 }
1157 else
1158 {
1159 *(c_perez+0) = x[0][0] + x[0][1]*Z + Delta * (x[0][2] + x[0][3]*Z);
1160 *(c_perez+1) = x[1][0] + x[1][1]*Z + Delta * (x[1][2] + x[1][3]*Z);
1161 *(c_perez+4) = x[4][0] + x[4][1]*Z + Delta * (x[4][2] + x[4][3]*Z);
1162 *(c_perez+2) = exp( pow(Delta*(x[2][0]+x[2][1]*Z),x[2][2])) - x[2][3];
1163 *(c_perez+3) = -exp( Delta*(x[3][0]+x[3][1]*Z) )+x[3][2]+Delta*x[3][3];
1164
1165
1166 }
1167
1168
1169 return;
1170 }
1171
1172
1173 /* degrees into radians */
1174 double radians(double degres)
1175 {
1176 return degres*M_PI/180.0;
1177 }
1178
1179 /* radian into degrees */
1180 double degres(double radians)
1181 {
1182 return radians/M_PI*180.0;
1183 }
1184
1185 /* calculation of the angles dzeta and gamma */
1186 void theta_phi_to_dzeta_gamma(double theta,double phi,double *dzeta,double *gamma, double Z)
1187 {
1188 *dzeta = theta; /* dzeta = phi */
1189 if ( (cos(Z)*cos(theta)+sin(Z)*sin(theta)*cos(phi)) > 1 && (cos(Z)*cos(theta)+sin(Z)*sin(theta)*cos(phi) < 1.1 ) )
1190 *gamma = 0;
1191 else if ( (cos(Z)*cos(theta)+sin(Z)*sin(theta)*cos(phi)) > 1.1 )
1192 {
1193 printf("error in calculation of gamma (angle between point and sun");
1194 exit(3);
1195 }
1196 else
1197 *gamma = acos(cos(Z)*cos(theta)+sin(Z)*sin(theta)*cos(phi));
1198 }
1199
1200
1201
1202 /********************************************************************************/
1203 /* Fonction: integ_lv */
1204 /* */
1205 /* In: float *lv,*theta */
1206 /* int sun_pos */
1207 /* */
1208 /* Out: double */
1209 /* */
1210 /* Update: 29/08/93 */
1211 /* */
1212 /* Rem: */
1213 /* */
1214 /* But: calcul l'integrale de luminance relative sans la dir. du soleil */
1215 /* */
1216 /********************************************************************************/
1217 double integ_lv(float *lv,float *theta)
1218 {
1219 int i;
1220 double buffer=0.0;
1221
1222 for (i=0;i<145;i++)
1223 buffer += (*(lv+i))*cos(radians(*(theta+i)));
1224
1225 return buffer*2*M_PI/144;
1226
1227 }
1228
1229
1230
1231
1232
1233
1234 /* enter day number(double), return E0 = square(R0/R): eccentricity correction factor */
1235
1236 double get_eccentricity()
1237 {
1238 double day_angle;
1239 double E0;
1240
1241 day_angle = 2*M_PI*(daynumber - 1)/365;
1242 E0 = 1.00011+0.034221*cos(day_angle)+0.00128*sin(day_angle)+
1243 0.000719*cos(2*day_angle)+0.000077*sin(2*day_angle);
1244
1245 return (E0);
1246
1247 }
1248
1249
1250 /* enter sunzenith angle (degrees) return relative air mass (double) */
1251 double air_mass()
1252 {
1253 double m;
1254
1255 if (sunzenith>90)
1256 {
1257 fprintf(stderr, "Solar zenith angle larger than 90 degrees in function air_mass()\n");
1258 exit(1);
1259 }
1260
1261 m = 1/( cos(sunzenith*M_PI/180)+0.15*exp( log(93.885-sunzenith)*(-1.253) ) );
1262 return(m);
1263 }
1264
1265
1266