ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/gendaylit.c
Revision: 2.17
Committed: Fri Aug 31 16:01:45 2018 UTC (5 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2
Changes since 2.16: +61 -9 lines
Log Message:
New versions of evalglare and gendaylit from Jan Wienold

File Contents

# User Rev Content
1 greg 2.9 /* Copyright (c) 1994,2006 *Fraunhofer Institut for Solar Energy Systems
2     * Heidenhofstr. 2, D-79110 Freiburg, Germany
3 greg 2.1 * *Agence de l'Environnement et de la Maitrise de l'Energie
4     * Centre de Valbonne, 500 route des Lucioles, 06565 Sophia Antipolis Cedex, France
5     * *BOUYGUES
6     * 1 Avenue Eugene Freyssinet, Saint-Quentin-Yvelines, France
7 greg 2.17 * print colored output if activated in command line (-C). Based on model from A. Diakite, TU-Berlin. Implemented by J. Wienold, August 26 2018
8 greg 2.1 */
9    
10 greg 2.15 #define _USE_MATH_DEFINES
11 greg 2.1 #include <stdio.h>
12     #include <string.h>
13     #include <math.h>
14     #include <stdlib.h>
15    
16     #include "color.h"
17 greg 2.12 #include "sun.h"
18 greg 2.1 #include "paths.h"
19    
20 greg 2.9 #define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
21 greg 2.1
22     double normsc();
23    
24 greg 2.17 /*static char *rcsid="$Header: /usr/local/cvs/radiance/ray/src/gen/gendaylit.c,v 2.16 2014/07/30 17:30:27 greg Exp $";*/
25 greg 2.9
26     float coeff_perez[] = {
27 greg 2.12 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,
28     0.9738,0.2809,0.0356,-0.1246,-0.5718,0.9938,-1.2219,-0.7730,1.4148,1.1016,-0.2054,0.0367,-3.9128,0.9156,
29     6.9750,0.1774,6.4477,-0.1239,-1.5798,-0.5081,-1.7812,0.1080,0.2624,0.0672,-0.2190,-0.4285,-1.1000,-0.2515,
30     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,
31     -2.6204,-0.0156,0.1597,0.4199,-0.5562,-0.5484,-0.6654,-0.2672,0.7117,0.7234,-0.6219,-5.6812,2.6297,
32     33.3389,-18.3000,-62.2500,52.0781,-3.5000,0.0016,1.1477,0.1062,0.4659,-0.3296,-0.0876,-0.0329,-0.6000,
33     -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,
34     1.4062,0.3988,0.0032,0.0766,-0.0656,-0.1294,-1.0156,-0.3670,1.0078,1.4051,0.2875,-0.5328,-3.8500,3.3750,
35     14.0000,-0.9999,-7.1406,7.5469,-3.4000,-0.1078,-1.0750,1.5702,-0.0672,0.4016,0.3017,-0.4844,-1.0000,
36     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,
37     0.2656,1.0468,-0.3788,-2.4517,1.4656,-1.0500,0.0289,0.4260,0.3590,-0.3250,0.1156,0.7781,0.0025,31.0625,
38     -14.5000,-46.1148,55.3750,-7.2312,0.4050,13.3500,0.6234,1.5000,-0.6426,1.8564,0.5636};
39    
40    
41     float defangle_theta[] = {
42     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,
43     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,
44     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,
45     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,
46     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,
47     24, 24, 24, 24, 24, 24, 24, 24, 12, 12, 12, 12, 12, 12, 0};
48    
49     float defangle_phi[] = {
50     0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 168, 180, 192, 204, 216, 228, 240, 252, 264,
51     276, 288, 300, 312, 324, 336, 348, 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 168, 180,
52     192, 204, 216, 228, 240, 252, 264, 276, 288, 300, 312, 324, 336, 348, 0, 15, 30, 45, 60, 75, 90, 105,
53     120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345, 0, 15, 30, 45, 60, 75,
54     90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345, 0, 20, 40, 60,
55     80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320, 340, 0, 30, 60, 90, 120, 150, 180, 210,
56     240, 270, 300, 330, 0, 60, 120, 180, 240, 300, 0};
57 greg 2.17 /* default values for Berlin */
58     float locus[] = {
59     -4.843e9,2.5568e6,0.24282e3,0.23258,-4.843e9,2.5568e6,0.24282e3,0.23258,-1.2848,1.7519,-0.093786};
60 greg 2.1
61    
62    
63 greg 2.14 /* Perez sky parametrization: epsilon and delta calculations from the direct and diffuse irradiances */
64 greg 2.1 double sky_brightness();
65     double sky_clearness();
66    
67     /* calculation of the direct and diffuse components from the Perez parametrization */
68 greg 2.9 double diffuse_irradiance_from_sky_brightness();
69 greg 2.1 double direct_irradiance_from_sky_clearness();
70    
71 greg 2.12 /* Perez global horizontal, diffuse horizontal and direct normal luminous efficacy models : */
72     /* input w(cm)=2cm, solar zenith angle(degrees); output efficacy(lm/W) */
73 greg 2.1
74     double glob_h_effi_PEREZ();
75     double glob_h_diffuse_effi_PEREZ();
76     double direct_n_effi_PEREZ();
77 greg 2.12
78 greg 2.1 /*likelihood check of the epsilon, delta, direct and diffuse components*/
79     void check_parametrization();
80     void check_irradiances();
81     void check_illuminances();
82     void illu_to_irra_index();
83 greg 2.9 void print_error_sky();
84 greg 2.1
85 greg 2.12 double calc_rel_lum_perez(double dzeta,double gamma,double Z,double epsilon,double Delta,float coeff_perez[]);
86 greg 2.9 void coeff_lum_perez(double Z, double epsilon, double Delta, float coeff_perez[]);
87 greg 2.1 double radians(double degres);
88     double degres(double radians);
89     void theta_phi_to_dzeta_gamma(double theta,double phi,double *dzeta,double *gamma, double Z);
90     double integ_lv(float *lv,float *theta);
91    
92 greg 2.9 void printdefaults();
93 greg 2.12 void check_sun_position();
94 greg 2.9 void computesky();
95     void printhead(int ac, char** av);
96 greg 2.14 void usage_error(char* msg);
97 greg 2.9 void printsky();
98 greg 2.1
99 greg 2.9 FILE * frlibopen(char* fname);
100 greg 2.1
101     /* astronomy and geometry*/
102     double get_eccentricity();
103     double air_mass();
104    
105 greg 2.14 double solar_sunset(int month, int day);
106     double solar_sunrise(int month, int day);
107 greg 2.12 double stadj();
108     int jdate(int month, int day);
109    
110 greg 2.1 const double AU = 149597890E3;
111     const double solar_constant_e = 1367; /* solar constant W/m^2 */
112 greg 2.14 const double solar_constant_l = 127500; /* solar constant lux */
113 greg 2.1
114     const double half_sun_angle = 0.2665;
115     const double half_direct_angle = 2.85;
116    
117 greg 2.14 const double skyclearinf = 1.0; /* limitations for the variation of the Perez parameters */
118     const double skyclearsup = 12.01;
119 greg 2.1 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 greg 2.9 double skyclearness = 0;
134     double skybrightness = 0;
135 greg 2.12 double solarradiance;
136     double diffuseilluminance, directilluminance, diffuseirradiance, directirradiance, globalirradiance;
137     double sunzenith, daynumber, atm_preci_water=2;
138 greg 2.1
139 greg 2.12 /*double sunaltitude_border = 0;*/
140 greg 2.9 double diffnormalization = 0;
141 greg 2.12 double dirnormalization = 0;
142 greg 2.1 double *c_perez;
143    
144 greg 2.12 int output=0; /* define the unit of the output (sky luminance or radiance): */
145     /* visible watt=0, solar watt=1, lumen=2 */
146     int input=0; /* define the input for the calulation */
147 greg 2.17 int color_output=0;
148 greg 2.9 int suppress_warnings=0;
149    
150 greg 2.1 /* default values */
151 greg 2.12 int cloudy = 0; /* 1=standard, 2=uniform */
152     int dosun = 1;
153 greg 2.1 double zenithbr = -1.0;
154     double betaturbidity = 0.1;
155     double gprefl = 0.2;
156     int S_INTER=0;
157    
158 greg 2.17
159 greg 2.1 /* computed values */
160     double sundir[3];
161 greg 2.9 double groundbr = 0;
162 greg 2.1 double F2;
163     double solarbr = 0.0;
164     int u_solar = 0; /* -1=irradiance, 1=radiance */
165 greg 2.12 float timeinterval = 0;
166    
167     char *progname;
168     char errmsg[128];
169    
170 greg 2.14 double st;
171 greg 2.1
172    
173 greg 2.9 int main(int argc, char** argv)
174 greg 2.1 {
175     int i;
176    
177     progname = argv[0];
178     if (argc == 2 && !strcmp(argv[1], "-defaults")) {
179     printdefaults();
180 greg 2.9 return 0;
181 greg 2.1 }
182     if (argc < 4)
183 greg 2.14 usage_error("arg count");
184 greg 2.1 if (!strcmp(argv[1], "-ang")) {
185 greg 2.9 altitude = atof(argv[2]) * (M_PI/180);
186     azimuth = atof(argv[3]) * (M_PI/180);
187 greg 2.1 month = 0;
188     } else {
189     month = atoi(argv[1]);
190     if (month < 1 || month > 12)
191 greg 2.14 usage_error("bad month");
192 greg 2.1 day = atoi(argv[2]);
193     if (day < 1 || day > 31)
194 greg 2.14 usage_error("bad day");
195 greg 2.1 hour = atof(argv[3]);
196     if (hour < 0 || hour >= 24)
197 greg 2.14 usage_error("bad hour");
198 greg 2.1 tsolar = argv[3][0] == '+';
199     }
200     for (i = 4; i < argc; i++)
201     if (argv[i][0] == '-' || argv[i][0] == '+')
202     switch (argv[i][1]) {
203     case 's':
204     cloudy = 0;
205     dosun = argv[i][0] == '+';
206     break;
207     case 'R':
208     u_solar = argv[i][1] == 'R' ? -1 : 1;
209     solarbr = atof(argv[++i]);
210     break;
211     case 'c':
212     cloudy = argv[i][0] == '+' ? 2 : 1;
213     dosun = 0;
214     break;
215 greg 2.17 case 'C':
216     if (argv[i][2] == 'I' && argv[i][3] == 'E' ) {
217     locus[0] = -4.607e9;
218     locus[1] = 2.9678e6;
219     locus[2] = 0.09911e3;
220     locus[3] = 0.244063;
221     locus[4] = -2.0064e9;
222     locus[5] = 1.9018e6;
223     locus[6] = 0.24748e3;
224     locus[7] = 0.23704;
225     locus[8] = -3.0;
226     locus[9] = 2.87;
227     locus[10] = -0.275;
228     }else{ color_output = 1;
229     }
230     break;
231     case 'l':
232     locus[0] = atof(argv[++i]);
233     locus[1] = atof(argv[++i]);
234     locus[2] = atof(argv[++i]);
235     locus[3] = atof(argv[++i]);
236     locus[4] = locus[0];
237     locus[5] = locus[1];
238     locus[6] = locus[2];
239     locus[7] = locus[3];
240     locus[8] = atof(argv[++i]);
241     locus[9] = atof(argv[++i]);
242     locus[10] = atof(argv[++i]);
243     break;
244    
245 greg 2.1 case 't':
246     betaturbidity = atof(argv[++i]);
247     break;
248 greg 2.9 case 'w':
249     suppress_warnings = 1;
250     break;
251 greg 2.1 case 'b':
252     zenithbr = atof(argv[++i]);
253     break;
254     case 'g':
255     gprefl = atof(argv[++i]);
256     break;
257     case 'a':
258 greg 2.9 s_latitude = atof(argv[++i]) * (M_PI/180);
259 greg 2.1 break;
260     case 'o':
261 greg 2.9 s_longitude = atof(argv[++i]) * (M_PI/180);
262 greg 2.1 break;
263     case 'm':
264 greg 2.9 s_meridian = atof(argv[++i]) * (M_PI/180);
265 greg 2.1 break;
266    
267     case 'O':
268 greg 2.14 output = atof(argv[++i]); /*define the unit of the output of the program:
269     sky and sun luminance/radiance
270     (0==W visible, 1==W solar radiation, 2==lm) */
271 greg 2.1 break;
272    
273     case 'P':
274     input = 0; /* Perez parameters: epsilon, delta */
275     skyclearness = atof(argv[++i]);
276     skybrightness = atof(argv[++i]);
277     break;
278    
279     case 'W': /* direct normal Irradiance [W/m^2] */
280     input = 1; /* diffuse horizontal Irrad. [W/m^2] */
281     directirradiance = atof(argv[++i]);
282 greg 2.9 diffuseirradiance = atof(argv[++i]);
283 greg 2.1 break;
284    
285     case 'L': /* direct normal Illuminance [Lux] */
286     input = 2; /* diffuse horizontal Ill. [Lux] */
287     directilluminance = atof(argv[++i]);
288 greg 2.9 diffuseilluminance = atof(argv[++i]);
289 greg 2.1 break;
290    
291     case 'G': /* direct horizontal Irradiance [W/m^2] */
292     input = 3; /* diffuse horizontal Irrad. [W/m^2] */
293     directirradiance = atof(argv[++i]);
294 greg 2.9 diffuseirradiance = atof(argv[++i]);
295     break;
296    
297 greg 2.12 case 'E': /* Erbs model based on the */
298     input = 4; /* global-horizontal irradiance [W/m^2] */
299     globalirradiance = atof(argv[++i]);
300     break;
301    
302     case 'i':
303     timeinterval = atof(argv[++i]);
304     break;
305 greg 2.9
306 greg 2.1
307     default:
308     sprintf(errmsg, "unknown option: %s", argv[i]);
309 greg 2.14 usage_error(errmsg);
310 greg 2.1 }
311     else
312 greg 2.14 usage_error("bad option");
313 greg 2.1
314 greg 2.14 if (month && !tsolar && fabs(s_meridian-s_longitude) > 45*M_PI/180)
315     fprintf(stderr,"%s: warning: %.1f hours btwn. standard meridian and longitude\n",
316 greg 2.9 progname, (s_longitude-s_meridian)*12/M_PI);
317 greg 2.1
318    
319 greg 2.12 /* dynamic memory allocation for the pointers */
320 greg 2.9 if ( (c_perez = calloc(5, sizeof(double))) == NULL )
321 greg 2.14 { fprintf(stderr,"Out of memory error in function main"); return 1; }
322 greg 2.1
323 greg 2.14
324 greg 2.1 printhead(argc, argv);
325     computesky();
326     printsky();
327 greg 2.9 return 0;
328 greg 2.14
329 greg 2.1 }
330    
331    
332 greg 2.12
333    
334    
335     void computesky()
336 greg 2.1 {
337    
338 greg 2.6 int j;
339 greg 2.12
340     float *lv_mod; /* 145 luminance values */
341 greg 2.9 float *theta_o, *phi_o;
342 greg 2.1 double dzeta, gamma;
343     double normfactor;
344 greg 2.12 double erbs_s0, erbs_kt;
345 greg 2.1
346    
347     /* compute solar direction */
348 greg 2.12
349 greg 2.1 if (month) { /* from date and time */
350     int jd;
351 greg 2.14 double sd;
352 greg 2.1
353     jd = jdate(month, day); /* Julian date */
354     sd = sdec(jd); /* solar declination */
355     if (tsolar) /* solar time */
356     st = hour;
357     else
358     st = hour + stadj(jd);
359 greg 2.12
360    
361     if(timeinterval) {
362    
363     if(timeinterval<0) {
364     fprintf(stderr, "time interval negative\n");
365     exit(1);
366     }
367 greg 2.14
368     if(fabs(solar_sunrise(month,day)-st)<=timeinterval/120) {
369     st= (st+timeinterval/120+solar_sunrise(month,day))/2;
370     if(suppress_warnings==0)
371     { fprintf(stderr, "Solar position corrected at time step %d %d %.3f\n",month,day,hour); }
372 greg 2.12 }
373    
374 greg 2.14 if(fabs(solar_sunset(month,day)-st)<timeinterval/120) {
375     st= (st-timeinterval/120+solar_sunset(month,day))/2;
376     if(suppress_warnings==0)
377     { fprintf(stderr, "Solar position corrected at time step %d %d %.3f\n",month,day,hour); }
378 greg 2.12 }
379 greg 2.14
380     if((st<solar_sunrise(month,day)-timeinterval/120) || (st>solar_sunset(month,day)+timeinterval/120)) {
381     if(suppress_warnings==0)
382     { fprintf(stderr, "Warning: sun position too low, printing error sky at %d %d %.3f\n",month,day,hour); }
383     altitude = salt(sd, st);
384     azimuth = sazi(sd, st);
385     print_error_sky();
386     exit(0);
387     }
388 greg 2.12 }
389 greg 2.14 else
390 greg 2.12
391 greg 2.14 if(st<solar_sunrise(month,day) || st>solar_sunset(month,day)) {
392     if(suppress_warnings==0)
393     { fprintf(stderr, "Warning: sun altitude below zero at time step %i %i %.2f, printing error sky\n",month,day,hour); }
394     altitude = salt(sd, st);
395     azimuth = sazi(sd, st);
396     print_error_sky();
397     exit(0);
398     }
399 greg 2.12
400 greg 2.1 altitude = salt(sd, st);
401     azimuth = sazi(sd, st);
402    
403     daynumber = (double)jdate(month, day);
404 greg 2.12
405 greg 2.1 }
406 greg 2.9
407    
408 greg 2.12
409    
410 greg 2.9
411     if (!cloudy && altitude > 87.*M_PI/180.) {
412    
413     if (suppress_warnings==0) {
414     fprintf(stderr,
415 greg 2.1 "%s: warning - sun too close to zenith, reducing altitude to 87 degrees\n",
416     progname);
417 greg 2.9 }
418     altitude = 87.*M_PI/180.;
419 greg 2.1 }
420 greg 2.9
421 greg 2.14
422    
423 greg 2.1 sundir[0] = -sin(azimuth)*cos(altitude);
424     sundir[1] = -cos(azimuth)*cos(altitude);
425     sundir[2] = sin(altitude);
426    
427    
428     /* calculation for the new functions */
429 greg 2.9 sunzenith = 90 - altitude*180/M_PI;
430 greg 2.14
431 greg 2.1
432 greg 2.9 /* compute the inputs for the calculation of the light distribution over the sky*/
433 greg 2.12 if (input==0) /* P */
434 greg 2.1 {
435     check_parametrization();
436 greg 2.9 diffuseirradiance = diffuse_irradiance_from_sky_brightness(); /*diffuse horizontal irradiance*/
437 greg 2.1 directirradiance = direct_irradiance_from_sky_clearness();
438     check_irradiances();
439    
440     if (output==0 || output==2)
441     {
442 greg 2.9 diffuseilluminance = diffuseirradiance*glob_h_diffuse_effi_PEREZ();/*diffuse horizontal illuminance*/
443 greg 2.1 directilluminance = directirradiance*direct_n_effi_PEREZ();
444     check_illuminances();
445     }
446     }
447    
448    
449 greg 2.12 else if (input==1) /* W */
450 greg 2.1 {
451     check_irradiances();
452     skybrightness = sky_brightness();
453     skyclearness = sky_clearness();
454 greg 2.14
455 greg 2.1 check_parametrization();
456 greg 2.14
457 greg 2.1 if (output==0 || output==2)
458     {
459 greg 2.9 diffuseilluminance = diffuseirradiance*glob_h_diffuse_effi_PEREZ();/*diffuse horizontal illuminance*/
460 greg 2.1 directilluminance = directirradiance*direct_n_effi_PEREZ();
461     check_illuminances();
462     }
463    
464     }
465    
466    
467 greg 2.12 else if (input==2) /* L */
468 greg 2.1 {
469     check_illuminances();
470     illu_to_irra_index();
471     check_parametrization();
472     }
473    
474    
475 greg 2.12 else if (input==3) /* G */
476 greg 2.1 {
477     if (altitude<=0)
478     {
479 greg 2.9 if (suppress_warnings==0)
480 greg 2.14 fprintf(stderr, "Warning: sun altitude < 0, proceed with irradiance values of zero\n");
481 greg 2.9 directirradiance = 0;
482     diffuseirradiance = 0;
483     } else {
484 greg 2.12
485     directirradiance=directirradiance/sin(altitude);
486 greg 2.1 }
487 greg 2.12
488 greg 2.1 check_irradiances();
489     skybrightness = sky_brightness();
490     skyclearness = sky_clearness();
491     check_parametrization();
492    
493     if (output==0 || output==2)
494     {
495 greg 2.9 diffuseilluminance = diffuseirradiance*glob_h_diffuse_effi_PEREZ();/*diffuse horizontal illuminance*/
496 greg 2.1 directilluminance = directirradiance*direct_n_effi_PEREZ();
497     check_illuminances();
498     }
499    
500     }
501    
502 greg 2.12
503     else if (input==4) /* E */ /* Implementation of the Erbs model. W.Sprenger (04/13) */
504     {
505    
506     if (altitude<=0)
507     {
508     if (suppress_warnings==0 && globalirradiance > 50)
509     fprintf(stderr, "Warning: global irradiance higher than 50 W/m^2 while the sun altitude is lower than zero\n");
510     globalirradiance = 0; diffuseirradiance = 0; directirradiance = 0;
511    
512     } else {
513    
514     erbs_s0 = solar_constant_e*get_eccentricity()*sin(altitude);
515    
516     if (globalirradiance>erbs_s0)
517     {
518     if (suppress_warnings==0)
519     fprintf(stderr, "Warning: global irradiance is higher than the time-dependent solar constant s0\n");
520     globalirradiance=erbs_s0*0.999;
521     }
522    
523     erbs_kt=globalirradiance/erbs_s0;
524    
525     if (erbs_kt<=0.22) diffuseirradiance=globalirradiance*(1-0.09*erbs_kt);
526     else if (erbs_kt<=0.8) diffuseirradiance=globalirradiance*(0.9511-0.1604*erbs_kt+4.388*pow(erbs_kt,2)-16.638*pow(erbs_kt,3)+12.336*pow(erbs_kt,4));
527     else if (erbs_kt<1) diffuseirradiance=globalirradiance*(0.165);
528    
529     directirradiance=globalirradiance-diffuseirradiance;
530    
531     printf("# erbs_s0, erbs_kt, irr_dir_h, irr_diff: %.3f %.3f %.3f %.3f\n", erbs_s0, erbs_kt, directirradiance, diffuseirradiance);
532 greg 2.14 printf("# WARNING: the -E option is only recommended for a rough estimation!\n");
533 greg 2.12
534     directirradiance=directirradiance/sin(altitude);
535    
536     }
537    
538     check_irradiances();
539     skybrightness = sky_brightness();
540     skyclearness = sky_clearness();
541     check_parametrization();
542    
543     if (output==0 || output==2)
544     {
545     diffuseilluminance = diffuseirradiance*glob_h_diffuse_effi_PEREZ();/*diffuse horizontal illuminance*/
546     directilluminance = directirradiance*direct_n_effi_PEREZ();
547     check_illuminances();
548     }
549    
550     }
551    
552    
553    
554 greg 2.1
555 greg 2.14 else { fprintf(stderr,"error at the input arguments"); exit(1); }
556 greg 2.1
557    
558    
559 greg 2.9 /* normalization factor for the relative sky luminance distribution, diffuse part*/
560 greg 2.12
561 greg 2.1 if ( (lv_mod = malloc(145*sizeof(float))) == NULL)
562     {
563     fprintf(stderr,"Out of memory in function main");
564     exit(1);
565     }
566    
567     /* read the angles */
568 greg 2.9 theta_o = defangle_theta;
569     phi_o = defangle_phi;
570 greg 2.12
571 greg 2.1
572 greg 2.9 /* parameters for the perez model */
573 greg 2.1 coeff_lum_perez(radians(sunzenith), skyclearness, skybrightness, coeff_perez);
574    
575 greg 2.12
576    
577 greg 2.9 /*calculation of the modelled luminance */
578 greg 2.1 for (j=0;j<145;j++)
579     {
580     theta_phi_to_dzeta_gamma(radians(*(theta_o+j)),radians(*(phi_o+j)),&dzeta,&gamma,radians(sunzenith));
581 greg 2.12
582 greg 2.1 *(lv_mod+j) = calc_rel_lum_perez(dzeta,gamma,radians(sunzenith),skyclearness,skybrightness,coeff_perez);
583 greg 2.12
584     /* fprintf(stderr,"theta, phi, lv_mod %f\t %f\t %f\n", *(theta_o+j),*(phi_o+j),*(lv_mod+j)); */
585 greg 2.1 }
586 greg 2.12
587 greg 2.1 /* integration of luminance for the normalization factor, diffuse part of the sky*/
588 greg 2.12
589 greg 2.1 diffnormalization = integ_lv(lv_mod, theta_o);
590    
591    
592    
593 greg 2.9 /*normalization coefficient in lumen or in watt*/
594 greg 2.1 if (output==0)
595     {
596 greg 2.9 diffnormalization = diffuseilluminance/diffnormalization/WHTEFFICACY;
597 greg 2.1 }
598     else if (output==1)
599     {
600 greg 2.9 diffnormalization = diffuseirradiance/diffnormalization;
601 greg 2.1 }
602     else if (output==2)
603     {
604 greg 2.9 diffnormalization = diffuseilluminance/diffnormalization;
605 greg 2.1 }
606    
607 greg 2.9 else {fprintf(stderr,"Wrong output specification.\n"); exit(1);}
608 greg 2.1
609    
610    
611    
612 greg 2.9 /* calculation for the solar source */
613 greg 2.1 if (output==0)
614 greg 2.9 solarradiance = directilluminance/(2*M_PI*(1-cos(half_sun_angle*M_PI/180)))/WHTEFFICACY;
615 greg 2.1
616     else if (output==1)
617 greg 2.9 solarradiance = directirradiance/(2*M_PI*(1-cos(half_sun_angle*M_PI/180)));
618 greg 2.1
619     else
620 greg 2.9 solarradiance = directilluminance/(2*M_PI*(1-cos(half_sun_angle*M_PI/180)));
621 greg 2.1
622    
623    
624 greg 2.14 /* Compute the ground radiance */
625     zenithbr=calc_rel_lum_perez(0.0,radians(sunzenith),radians(sunzenith),skyclearness,skybrightness,coeff_perez);
626     zenithbr*=diffnormalization;
627 greg 2.9
628 greg 2.14 if (skyclearness==1)
629 greg 2.1 normfactor = 0.777778;
630    
631 greg 2.14 if (skyclearness>=6)
632 greg 2.1 {
633 greg 2.9 F2 = 0.274*(0.91 + 10.0*exp(-3.0*(M_PI/2.0-altitude)) + 0.45*sundir[2]*sundir[2]);
634     normfactor = normsc()/F2/M_PI;
635 greg 2.1 }
636    
637 greg 2.14 if ( (skyclearness>1) && (skyclearness<6) )
638 greg 2.1 {
639     S_INTER=1;
640 greg 2.9 F2 = (2.739 + .9891*sin(.3119+2.6*altitude)) * exp(-(M_PI/2.0-altitude)*(.4441+1.48*altitude));
641     normfactor = normsc()/F2/M_PI;
642 greg 2.1 }
643    
644 greg 2.14 groundbr = zenithbr*normfactor;
645 greg 2.1
646 greg 2.14 if (dosun&&(skyclearness>1))
647 greg 2.9 groundbr += 6.8e-5/M_PI*solarradiance*sundir[2];
648 greg 2.1
649 greg 2.14 groundbr *= gprefl;
650 greg 2.1
651    
652 greg 2.14
653     if(*(c_perez+1)>0)
654     {
655     if(suppress_warnings==0)
656     { fprintf(stderr, "Warning: positive Perez parameter B (= %lf), printing error sky\n",*(c_perez+1));}
657     print_error_sky();
658     exit(0);
659     }
660    
661 greg 2.1
662     return;
663     }
664    
665    
666    
667    
668 greg 2.12
669     double solar_sunset(int month,int day)
670     {
671     float W;
672     extern double s_latitude;
673     W=-1*(tan(s_latitude)*tan(sdec(jdate(month, day))));
674     return(12+(M_PI/2 - atan2(W,sqrt(1-W*W)))*180/(M_PI*15));
675     }
676    
677    
678 greg 2.14
679    
680 greg 2.12 double solar_sunrise(int month,int day)
681     {
682     float W;
683     extern double s_latitude;
684     W=-1*(tan(s_latitude)*tan(sdec(jdate(month, day))));
685     return(12-(M_PI/2 - atan2(W,sqrt(1-W*W)))*180/(M_PI*15));
686     }
687    
688    
689    
690    
691 greg 2.14 void printsky()
692     {
693    
694     printf("# Local solar time: %.2f\n", st);
695     printf("# Solar altitude and azimuth: %.1f %.1f\n", altitude*180/M_PI, azimuth*180/M_PI);
696 greg 2.12
697    
698 greg 2.1 if (dosun&&(skyclearness>1))
699 greg 2.9 {
700 greg 2.1 printf("\nvoid light solar\n");
701     printf("0\n0\n");
702     printf("3 %.3e %.3e %.3e\n", solarradiance, solarradiance, solarradiance);
703     printf("\nsolar source sun\n");
704     printf("0\n0\n");
705     printf("4 %f %f %f %f\n", sundir[0], sundir[1], sundir[2], 2*half_sun_angle);
706 greg 2.9 } else if (dosun) {
707 greg 2.1 printf("\nvoid light solar\n");
708     printf("0\n0\n");
709     printf("3 0.0 0.0 0.0\n");
710     printf("\nsolar source sun\n");
711     printf("0\n0\n");
712     printf("4 %f %f %f %f\n", sundir[0], sundir[1], sundir[2], 2*half_sun_angle);
713 greg 2.9 }
714 greg 2.17 /* print colored output if activated in command line (-C). Based on model from A. Diakite, TU-Berlin. Implemented by J. Wienold, August 26 2018 */
715     if (color_output==1 && skyclearness < 4.5 && skyclearness >1.065 )
716     {
717     fprintf(stderr, " warning: sky clearness(epsilon)= %f \n",skyclearness);
718     fprintf(stderr, " warning: intermediate sky!! \n");
719     fprintf(stderr, " warning: color model for intermediate sky pending \n");
720     fprintf(stderr, " warning: no color output ! \n");
721     color_output=0;
722     }
723     if (color_output==1)
724     {
725     printf("\nvoid colorfunc skyfunc\n");
726     printf("4 skybright_r skybright_g skybright_b perezlum_c.cal\n");
727 greg 2.1 printf("0\n");
728 greg 2.17 printf("22 %.3e %.3e %lf %lf %lf %lf %lf %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n", diffnormalization, groundbr,
729 greg 2.9 *(c_perez+0),*(c_perez+1),*(c_perez+2),*(c_perez+3),*(c_perez+4),
730 greg 2.17 sundir[0], sundir[1], sundir[2],skyclearness,locus[0],locus[1],locus[2],locus[3],locus[4],locus[5],locus[6],locus[7],locus[8],locus[9],locus[10]);
731     }else{
732     printf("\nvoid brightfunc skyfunc\n");
733     printf("2 skybright perezlum.cal\n");
734     printf("0\n");
735     printf("10 %.3e %.3e %lf %lf %lf %lf %lf %f %f %f \n", diffnormalization, groundbr,
736     *(c_perez+0),*(c_perez+1),*(c_perez+2),*(c_perez+3),*(c_perez+4),
737     sundir[0], sundir[1], sundir[2]);
738     }
739 greg 2.12
740 greg 2.1 }
741    
742    
743 greg 2.14
744     void print_error_sky()
745     {
746    
747    
748     sundir[0] = -sin(azimuth)*cos(altitude);
749     sundir[1] = -cos(azimuth)*cos(altitude);
750     sundir[2] = sin(altitude);
751    
752     printf("# Local solar time: %.2f\n", st);
753     printf("# Solar altitude and azimuth: %.1f %.1f\n", altitude*180/M_PI, azimuth*180/M_PI);
754    
755     printf("\nvoid brightfunc skyfunc\n");
756     printf("2 skybright perezlum.cal\n");
757     printf("0\n");
758     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]);
759     }
760    
761    
762    
763    
764    
765 greg 2.9 void printdefaults() /* print default values */
766 greg 2.1 {
767     printf("-g %f\t\t\t# Ground plane reflectance\n", gprefl);
768     if (zenithbr > 0.0)
769     printf("-b %f\t\t\t# Zenith radiance (watts/ster/m^2\n", zenithbr);
770     else
771     printf("-t %f\t\t\t# Atmospheric betaturbidity\n", betaturbidity);
772 greg 2.9 printf("-a %f\t\t\t# Site latitude (degrees)\n", s_latitude*(180/M_PI));
773     printf("-o %f\t\t\t# Site longitude (degrees)\n", s_longitude*(180/M_PI));
774     printf("-m %f\t\t\t# Standard meridian (degrees)\n", s_meridian*(180/M_PI));
775 greg 2.1 }
776    
777    
778 greg 2.14
779    
780     void usage_error(char* msg) /* print usage error and quit */
781 greg 2.1 {
782     if (msg != NULL)
783 greg 2.12 fprintf(stderr, "%s: Use error - %s\n\n", progname, msg);
784     fprintf(stderr, "Usage: %s month day hour [...]\n", progname);
785     fprintf(stderr, " or: %s -ang altitude azimuth [...]\n", progname);
786     fprintf(stderr, " followed by: -P epsilon delta [options]\n");
787     fprintf(stderr, " or: [-W|-L|-G] direct_value diffuse_value [options]\n");
788     fprintf(stderr, " or: -E global_irradiance [options]\n\n");
789     fprintf(stderr, " Description:\n");
790 greg 2.1 fprintf(stderr, " -P epsilon delta (these are the Perez parameters) \n");
791     fprintf(stderr, " -W direct-normal-irradiance diffuse-horizontal-irradiance (W/m^2)\n");
792     fprintf(stderr, " -L direct-normal-illuminance diffuse-horizontal-illuminance (lux)\n");
793     fprintf(stderr, " -G direct-horizontal-irradiance diffuse-horizontal-irradiance (W/m^2)\n");
794 greg 2.12 fprintf(stderr, " -E global-horizontal-irradiance (W/m^2)\n\n");
795     fprintf(stderr, " Output specification with option:\n");
796 greg 2.1 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");
797 greg 2.17 fprintf(stderr, " gendaylit version 2.5 (2018/04/18) \n\n");
798 greg 2.1 exit(1);
799     }
800    
801    
802    
803 greg 2.14
804 greg 2.9 double normsc() /* compute normalization factor (E0*F2/L0) */
805 greg 2.1 {
806     static double nfc[2][5] = {
807     /* clear sky approx. */
808     {2.766521, 0.547665, -0.369832, 0.009237, 0.059229},
809     /* intermediate sky approx. */
810     {3.5556, -2.7152, -1.3081, 1.0660, 0.60227},
811     };
812     register double *nf;
813     double x, nsc;
814     register int i;
815     /* polynomial approximation */
816     nf = nfc[S_INTER];
817 greg 2.9 x = (altitude - M_PI/4.0)/(M_PI/4.0);
818 greg 2.1 nsc = nf[i=4];
819     while (i--)
820     nsc = nsc*x + nf[i];
821    
822     return(nsc);
823     }
824    
825    
826    
827 greg 2.14
828    
829 greg 2.9 void printhead(int ac, char** av) /* print command header */
830 greg 2.1 {
831     putchar('#');
832     while (ac--) {
833     putchar(' ');
834     fputs(*av++, stdout);
835     }
836     putchar('\n');
837     }
838    
839    
840    
841    
842 greg 2.14
843    
844 greg 2.1 /* Perez models */
845    
846     /* Perez global horizontal luminous efficacy model */
847     double glob_h_effi_PEREZ()
848     {
849    
850     double value;
851     double category_bounds[10], a[10], b[10], c[10], d[10];
852     int category_total_number, category_number, i;
853 greg 2.12
854     check_parametrization();
855    
856    
857     /*if ((skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<skybriginf || skybrightness>skybrigsup) && suppress_warnings==0)
858     fprintf(stderr, "Warning: skyclearness or skybrightness out of range in function glob_h_effi_PEREZ \n"); */
859    
860    
861 greg 2.1 /* initialize category bounds (clearness index bounds) */
862    
863     category_total_number = 8;
864    
865     category_bounds[1] = 1;
866     category_bounds[2] = 1.065;
867     category_bounds[3] = 1.230;
868     category_bounds[4] = 1.500;
869     category_bounds[5] = 1.950;
870     category_bounds[6] = 2.800;
871     category_bounds[7] = 4.500;
872     category_bounds[8] = 6.200;
873     category_bounds[9] = 12.01;
874    
875    
876     /* initialize model coefficients */
877     a[1] = 96.63;
878     a[2] = 107.54;
879     a[3] = 98.73;
880     a[4] = 92.72;
881     a[5] = 86.73;
882     a[6] = 88.34;
883     a[7] = 78.63;
884     a[8] = 99.65;
885    
886     b[1] = -0.47;
887     b[2] = 0.79;
888     b[3] = 0.70;
889     b[4] = 0.56;
890     b[5] = 0.98;
891     b[6] = 1.39;
892     b[7] = 1.47;
893     b[8] = 1.86;
894    
895     c[1] = 11.50;
896     c[2] = 1.79;
897     c[3] = 4.40;
898     c[4] = 8.36;
899     c[5] = 7.10;
900     c[6] = 6.06;
901     c[7] = 4.93;
902     c[8] = -4.46;
903    
904     d[1] = -9.16;
905     d[2] = -1.19;
906     d[3] = -6.95;
907     d[4] = -8.31;
908     d[5] = -10.94;
909     d[6] = -7.60;
910     d[7] = -11.37;
911     d[8] = -3.15;
912    
913    
914    
915     for (i=1; i<=category_total_number; i++)
916     {
917     if ( (skyclearness >= category_bounds[i]) && (skyclearness < category_bounds[i+1]) )
918     category_number = i;
919     }
920    
921     value = a[category_number] + b[category_number]*atm_preci_water +
922 greg 2.9 c[category_number]*cos(sunzenith*M_PI/180) + d[category_number]*log(skybrightness);
923 greg 2.1
924     return(value);
925     }
926    
927    
928 greg 2.14
929    
930 greg 2.1 /* global horizontal diffuse efficacy model, according to PEREZ */
931     double glob_h_diffuse_effi_PEREZ()
932     {
933     double value;
934     double category_bounds[10], a[10], b[10], c[10], d[10];
935     int category_total_number, category_number, i;
936    
937 greg 2.12 check_parametrization();
938 greg 2.1
939 greg 2.12
940     /*if ((skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<skybriginf || skybrightness>skybrigsup) && suppress_warnings==0)
941 greg 2.14 fprintf(stderr, "Warning: skyclearness or skybrightness out of range in function glob_h_diffuse_PEREZ \n"); */
942 greg 2.12
943 greg 2.1 /* initialize category bounds (clearness index bounds) */
944    
945     category_total_number = 8;
946    
947 greg 2.12 //XXX: category_bounds > 0.1
948 greg 2.1 category_bounds[1] = 1;
949     category_bounds[2] = 1.065;
950     category_bounds[3] = 1.230;
951     category_bounds[4] = 1.500;
952     category_bounds[5] = 1.950;
953     category_bounds[6] = 2.800;
954     category_bounds[7] = 4.500;
955     category_bounds[8] = 6.200;
956     category_bounds[9] = 12.01;
957    
958    
959     /* initialize model coefficients */
960     a[1] = 97.24;
961     a[2] = 107.22;
962     a[3] = 104.97;
963     a[4] = 102.39;
964     a[5] = 100.71;
965     a[6] = 106.42;
966     a[7] = 141.88;
967     a[8] = 152.23;
968    
969     b[1] = -0.46;
970     b[2] = 1.15;
971     b[3] = 2.96;
972     b[4] = 5.59;
973     b[5] = 5.94;
974     b[6] = 3.83;
975     b[7] = 1.90;
976     b[8] = 0.35;
977    
978     c[1] = 12.00;
979     c[2] = 0.59;
980     c[3] = -5.53;
981     c[4] = -13.95;
982     c[5] = -22.75;
983     c[6] = -36.15;
984     c[7] = -53.24;
985     c[8] = -45.27;
986    
987     d[1] = -8.91;
988     d[2] = -3.95;
989     d[3] = -8.77;
990     d[4] = -13.90;
991     d[5] = -23.74;
992     d[6] = -28.83;
993     d[7] = -14.03;
994     d[8] = -7.98;
995    
996    
997    
998 greg 2.9 category_number = -1;
999 greg 2.1 for (i=1; i<=category_total_number; i++)
1000     {
1001     if ( (skyclearness >= category_bounds[i]) && (skyclearness < category_bounds[i+1]) )
1002     category_number = i;
1003     }
1004    
1005 greg 2.9 if (category_number == -1) {
1006     if (suppress_warnings==0)
1007 greg 2.14 fprintf(stderr, "Warning: sky clearness (= %.3f) too high, printing error sky\n", skyclearness);
1008 greg 2.9 print_error_sky();
1009 greg 2.14 exit(0);
1010 greg 2.9 }
1011    
1012    
1013     value = a[category_number] + b[category_number]*atm_preci_water + c[category_number]*cos(sunzenith*M_PI/180) +
1014 greg 2.1 d[category_number]*log(skybrightness);
1015    
1016     return(value);
1017 greg 2.12
1018 greg 2.1 }
1019    
1020    
1021 greg 2.12
1022 greg 2.14
1023    
1024    
1025 greg 2.1 /* direct normal efficacy model, according to PEREZ */
1026    
1027     double direct_n_effi_PEREZ()
1028    
1029     {
1030     double value;
1031     double category_bounds[10], a[10], b[10], c[10], d[10];
1032     int category_total_number, category_number, i;
1033    
1034    
1035 greg 2.14 /*if ((skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<skybriginf || skybrightness>skybrigsup) && suppress_warnings==0)
1036     fprintf(stderr, "Warning: skyclearness or skybrightness out of range in function direct_n_effi_PEREZ \n");*/
1037 greg 2.1
1038    
1039     /* initialize category bounds (clearness index bounds) */
1040    
1041     category_total_number = 8;
1042    
1043     category_bounds[1] = 1;
1044     category_bounds[2] = 1.065;
1045     category_bounds[3] = 1.230;
1046     category_bounds[4] = 1.500;
1047     category_bounds[5] = 1.950;
1048     category_bounds[6] = 2.800;
1049     category_bounds[7] = 4.500;
1050     category_bounds[8] = 6.200;
1051     category_bounds[9] = 12.1;
1052    
1053    
1054     /* initialize model coefficients */
1055     a[1] = 57.20;
1056     a[2] = 98.99;
1057     a[3] = 109.83;
1058     a[4] = 110.34;
1059     a[5] = 106.36;
1060     a[6] = 107.19;
1061     a[7] = 105.75;
1062     a[8] = 101.18;
1063    
1064     b[1] = -4.55;
1065     b[2] = -3.46;
1066     b[3] = -4.90;
1067     b[4] = -5.84;
1068     b[5] = -3.97;
1069     b[6] = -1.25;
1070     b[7] = 0.77;
1071     b[8] = 1.58;
1072    
1073     c[1] = -2.98;
1074     c[2] = -1.21;
1075     c[3] = -1.71;
1076     c[4] = -1.99;
1077     c[5] = -1.75;
1078     c[6] = -1.51;
1079     c[7] = -1.26;
1080     c[8] = -1.10;
1081    
1082     d[1] = 117.12;
1083     d[2] = 12.38;
1084     d[3] = -8.81;
1085     d[4] = -4.56;
1086     d[5] = -6.16;
1087     d[6] = -26.73;
1088     d[7] = -34.44;
1089     d[8] = -8.29;
1090    
1091    
1092    
1093     for (i=1; i<=category_total_number; i++)
1094     {
1095     if ( (skyclearness >= category_bounds[i]) && (skyclearness < category_bounds[i+1]) )
1096     category_number = i;
1097     }
1098    
1099 greg 2.9 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;
1100 greg 2.1
1101     if (value < 0) value = 0;
1102    
1103     return(value);
1104     }
1105    
1106    
1107     /*check the range of epsilon and delta indexes of the perez parametrization*/
1108     void check_parametrization()
1109     {
1110 greg 2.14
1111 greg 2.9 if (skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<skybriginf || skybrightness>skybrigsup)
1112 greg 2.1 {
1113 greg 2.9
1114     /* limit sky clearness or sky brightness, 2009 11 13 by J. Wienold */
1115 greg 2.14
1116 greg 2.9 if (skyclearness<skyclearinf){
1117 greg 2.14 /* if (suppress_warnings==0)
1118     fprintf(stderr,"Range warning: sky clearness too low (%lf)\n", skyclearness); */
1119 greg 2.9 skyclearness=skyclearinf;
1120     }
1121     if (skyclearness>skyclearsup){
1122 greg 2.14 /* if (suppress_warnings==0)
1123     fprintf(stderr,"Range warning: sky clearness too high (%lf)\n", skyclearness); */
1124     skyclearness=skyclearsup-0.001;
1125 greg 2.9 }
1126     if (skybrightness<skybriginf){
1127 greg 2.14 /* if (suppress_warnings==0)
1128     fprintf(stderr,"Range warning: sky brightness too low (%lf)\n", skybrightness); */
1129 greg 2.9 skybrightness=skybriginf;
1130     }
1131     if (skybrightness>skybrigsup){
1132 greg 2.14 /* if (suppress_warnings==0)
1133     fprintf(stderr,"Range warning: sky brightness too high (%lf)\n", skybrightness); */
1134 greg 2.9 skybrightness=skybrigsup;
1135 greg 2.1 }
1136 greg 2.9
1137     return; }
1138 greg 2.1 else return;
1139     }
1140    
1141    
1142 greg 2.14
1143    
1144    
1145 greg 2.9 /* validity of the direct and diffuse components */
1146 greg 2.1 void check_illuminances()
1147     {
1148 greg 2.9 if (directilluminance < 0) {
1149 greg 2.14 if(suppress_warnings==0)
1150     { fprintf(stderr,"Warning: direct illuminance < 0. Using 0.0\n"); }
1151 greg 2.9 directilluminance = 0.0;
1152     }
1153     if (diffuseilluminance < 0) {
1154 greg 2.14 if(suppress_warnings==0)
1155     { fprintf(stderr,"Warning: diffuse illuminance < 0. Using 0.0\n"); }
1156 greg 2.9 diffuseilluminance = 0.0;
1157     }
1158 greg 2.14
1159     if (directilluminance+diffuseilluminance==0 && altitude > 0) {
1160     if(suppress_warnings==0)
1161     { fprintf(stderr,"Warning: zero illuminance at sun altitude > 0, printing error sky\n"); }
1162     print_error_sky();
1163     exit(0);
1164     }
1165    
1166     if (directilluminance > solar_constant_l) {
1167     if(suppress_warnings==0)
1168     { fprintf(stderr,"Warning: direct illuminance exceeds solar constant\n"); }
1169     print_error_sky();
1170     exit(0);
1171 greg 2.1 }
1172     }
1173    
1174    
1175     void check_irradiances()
1176     {
1177 greg 2.9 if (directirradiance < 0) {
1178 greg 2.14 if(suppress_warnings==0)
1179     { fprintf(stderr,"Warning: direct irradiance < 0. Using 0.0\n"); }
1180 greg 2.9 directirradiance = 0.0;
1181     }
1182     if (diffuseirradiance < 0) {
1183 greg 2.14 if(suppress_warnings==0)
1184     { fprintf(stderr,"Warning: diffuse irradiance < 0. Using 0.0\n"); }
1185 greg 2.9 diffuseirradiance = 0.0;
1186     }
1187 greg 2.14
1188     if (directirradiance+diffuseirradiance==0 && altitude > 0) {
1189     if(suppress_warnings==0)
1190     { fprintf(stderr,"Warning: zero irradiance at sun altitude > 0, printing error sky\n"); }
1191     print_error_sky();
1192     exit(0);
1193     }
1194    
1195 greg 2.9 if (directirradiance > solar_constant_e) {
1196 greg 2.14 if(suppress_warnings==0)
1197     { fprintf(stderr,"Warning: direct irradiance exceeds solar constant\n"); }
1198     print_error_sky();
1199     exit(0);
1200 greg 2.9 }
1201 greg 2.1 }
1202    
1203    
1204    
1205     /* Perez sky's brightness */
1206     double sky_brightness()
1207     {
1208     double value;
1209    
1210 greg 2.9 value = diffuseirradiance * air_mass() / ( solar_constant_e*get_eccentricity());
1211 greg 2.1
1212     return(value);
1213     }
1214    
1215    
1216     /* Perez sky's clearness */
1217     double sky_clearness()
1218     {
1219 greg 2.9 double value;
1220 greg 2.1
1221 greg 2.9 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) ;
1222 greg 2.1
1223 greg 2.9 return(value);
1224 greg 2.1 }
1225    
1226    
1227    
1228     /* diffus horizontal irradiance from Perez sky's brightness */
1229 greg 2.9 double diffuse_irradiance_from_sky_brightness()
1230 greg 2.1 {
1231     double value;
1232    
1233     value = skybrightness / air_mass() * ( solar_constant_e*get_eccentricity());
1234    
1235     return(value);
1236     }
1237    
1238    
1239     /* direct normal irradiance from Perez sky's clearness */
1240     double direct_irradiance_from_sky_clearness()
1241     {
1242     double value;
1243    
1244 greg 2.9 value = diffuse_irradiance_from_sky_brightness();
1245     value = value * ( (skyclearness-1) * (1+1.041*sunzenith*M_PI/180*sunzenith*M_PI/180*sunzenith*M_PI/180) );
1246 greg 2.1
1247     return(value);
1248     }
1249    
1250    
1251 greg 2.12
1252    
1253 greg 2.9 void illu_to_irra_index()
1254 greg 2.1 {
1255 greg 2.9 double test1=0.1, test2=0.1, d_eff;
1256 greg 2.1 int counter=0;
1257    
1258 greg 2.14 diffuseirradiance = diffuseilluminance*solar_constant_e/(solar_constant_l);
1259     directirradiance = directilluminance*solar_constant_e/(solar_constant_l);
1260 greg 2.1 skyclearness = sky_clearness();
1261     skybrightness = sky_brightness();
1262 greg 2.9 check_parametrization();
1263 greg 2.12
1264    
1265 greg 2.9 while ( ((fabs(diffuseirradiance-test1)>10) || (fabs(directirradiance-test2)>10)
1266 greg 2.12 || (!(skyclearness<skyclearinf || skyclearness>skyclearsup))
1267     || (!(skybrightness<skybriginf || skybrightness>skybrigsup)) )
1268     && !(counter==9) )
1269 greg 2.1 {
1270 greg 2.12
1271 greg 2.9 test1=diffuseirradiance;
1272 greg 2.1 test2=directirradiance;
1273     counter++;
1274    
1275 greg 2.9 diffuseirradiance = diffuseilluminance/glob_h_diffuse_effi_PEREZ();
1276     d_eff = direct_n_effi_PEREZ();
1277 greg 2.12
1278    
1279 greg 2.9 if (d_eff < 0.1)
1280     directirradiance = 0;
1281 greg 2.12 else
1282 greg 2.9 directirradiance = directilluminance/d_eff;
1283 greg 2.1
1284     skybrightness = sky_brightness();
1285     skyclearness = sky_clearness();
1286 greg 2.9 check_parametrization();
1287 greg 2.12
1288 greg 2.1 }
1289    
1290    
1291     return;
1292     }
1293    
1294 greg 2.9 static int get_numlin(float epsilon)
1295 greg 2.1 {
1296 greg 2.9 if (epsilon < 1.065)
1297     return 0;
1298     else if (epsilon < 1.230)
1299     return 1;
1300     else if (epsilon < 1.500)
1301     return 2;
1302     else if (epsilon < 1.950)
1303     return 3;
1304     else if (epsilon < 2.800)
1305     return 4;
1306     else if (epsilon < 4.500)
1307     return 5;
1308     else if (epsilon < 6.200)
1309     return 6;
1310     return 7;
1311 greg 2.1 }
1312    
1313     /* sky luminance perez model */
1314 greg 2.9 double calc_rel_lum_perez(double dzeta,double gamma,double Z,double epsilon,double Delta,float coeff_perez[])
1315 greg 2.1 {
1316 greg 2.12
1317 greg 2.1 float x[5][4];
1318     int i,j,num_lin;
1319     double c_perez[5];
1320    
1321     if ( (epsilon < skyclearinf) || (epsilon >= skyclearsup) )
1322     {
1323 greg 2.14 fprintf(stderr,"Error: epsilon out of range in function calc_rel_lum_perez!\n");
1324 greg 2.1 exit(1);
1325     }
1326    
1327     /* correction de modele de Perez solar energy ...*/
1328     if ( (epsilon > 1.065) && (epsilon < 2.8) )
1329     {
1330     if ( Delta < 0.2 ) Delta = 0.2;
1331     }
1332 greg 2.12
1333    
1334 greg 2.9 num_lin = get_numlin(epsilon);
1335 greg 2.12
1336 greg 2.1 for (i=0;i<5;i++)
1337     for (j=0;j<4;j++)
1338     {
1339     x[i][j] = *(coeff_perez + 20*num_lin + 4*i +j);
1340 greg 2.12 /* fprintf(stderr,"x %d %d vaut %f\n",i,j,x[i][j]); */
1341 greg 2.1 }
1342    
1343    
1344     if (num_lin)
1345     {
1346     for (i=0;i<5;i++)
1347     c_perez[i] = x[i][0] + x[i][1]*Z + Delta * (x[i][2] + x[i][3]*Z);
1348     }
1349     else
1350     {
1351     c_perez[0] = x[0][0] + x[0][1]*Z + Delta * (x[0][2] + x[0][3]*Z);
1352     c_perez[1] = x[1][0] + x[1][1]*Z + Delta * (x[1][2] + x[1][3]*Z);
1353     c_perez[4] = x[4][0] + x[4][1]*Z + Delta * (x[4][2] + x[4][3]*Z);
1354     c_perez[2] = exp( pow(Delta*(x[2][0]+x[2][1]*Z),x[2][2])) - x[2][3];
1355     c_perez[3] = -exp( Delta*(x[3][0]+x[3][1]*Z) )+x[3][2]+Delta*x[3][3];
1356     }
1357    
1358    
1359     return (1 + c_perez[0]*exp(c_perez[1]/cos(dzeta)) ) *
1360     (1 + c_perez[2]*exp(c_perez[3]*gamma) +
1361     c_perez[4]*cos(gamma)*cos(gamma) );
1362     }
1363    
1364    
1365    
1366     /* coefficients for the sky luminance perez model */
1367 greg 2.9 void coeff_lum_perez(double Z, double epsilon, double Delta, float coeff_perez[])
1368 greg 2.1 {
1369     float x[5][4];
1370     int i,j,num_lin;
1371    
1372     if ( (epsilon < skyclearinf) || (epsilon >= skyclearsup) )
1373     {
1374 greg 2.14 fprintf(stderr,"Error: epsilon out of range in function coeff_lum_perez!\n");
1375 greg 2.1 exit(1);
1376     }
1377    
1378     /* correction du modele de Perez solar energy ...*/
1379     if ( (epsilon > 1.065) && (epsilon < 2.8) )
1380     {
1381     if ( Delta < 0.2 ) Delta = 0.2;
1382     }
1383 greg 2.12
1384    
1385 greg 2.9 num_lin = get_numlin(epsilon);
1386    
1387 greg 2.12 /*fprintf(stderr,"numlin %d\n", num_lin);*/
1388 greg 2.1
1389     for (i=0;i<5;i++)
1390     for (j=0;j<4;j++)
1391     {
1392     x[i][j] = *(coeff_perez + 20*num_lin + 4*i +j);
1393     /* printf("x %d %d vaut %f\n",i,j,x[i][j]); */
1394     }
1395    
1396    
1397     if (num_lin)
1398     {
1399     for (i=0;i<5;i++)
1400     *(c_perez+i) = x[i][0] + x[i][1]*Z + Delta * (x[i][2] + x[i][3]*Z);
1401    
1402     }
1403     else
1404     {
1405     *(c_perez+0) = x[0][0] + x[0][1]*Z + Delta * (x[0][2] + x[0][3]*Z);
1406     *(c_perez+1) = x[1][0] + x[1][1]*Z + Delta * (x[1][2] + x[1][3]*Z);
1407     *(c_perez+4) = x[4][0] + x[4][1]*Z + Delta * (x[4][2] + x[4][3]*Z);
1408     *(c_perez+2) = exp( pow(Delta*(x[2][0]+x[2][1]*Z),x[2][2])) - x[2][3];
1409     *(c_perez+3) = -exp( Delta*(x[3][0]+x[3][1]*Z) )+x[3][2]+Delta*x[3][3];
1410    
1411    
1412     }
1413    
1414    
1415     return;
1416     }
1417    
1418    
1419 greg 2.12
1420 greg 2.1 /* degrees into radians */
1421     double radians(double degres)
1422     {
1423 greg 2.9 return degres*M_PI/180.0;
1424 greg 2.1 }
1425    
1426 greg 2.12
1427 greg 2.1 /* radian into degrees */
1428     double degres(double radians)
1429     {
1430 greg 2.9 return radians/M_PI*180.0;
1431 greg 2.1 }
1432    
1433 greg 2.12
1434 greg 2.1 /* calculation of the angles dzeta and gamma */
1435     void theta_phi_to_dzeta_gamma(double theta,double phi,double *dzeta,double *gamma, double Z)
1436     {
1437     *dzeta = theta; /* dzeta = phi */
1438     if ( (cos(Z)*cos(theta)+sin(Z)*sin(theta)*cos(phi)) > 1 && (cos(Z)*cos(theta)+sin(Z)*sin(theta)*cos(phi) < 1.1 ) )
1439     *gamma = 0;
1440     else if ( (cos(Z)*cos(theta)+sin(Z)*sin(theta)*cos(phi)) > 1.1 )
1441     {
1442     printf("error in calculation of gamma (angle between point and sun");
1443 greg 2.14 exit(1);
1444 greg 2.1 }
1445     else
1446     *gamma = acos(cos(Z)*cos(theta)+sin(Z)*sin(theta)*cos(phi));
1447     }
1448    
1449    
1450    
1451     double integ_lv(float *lv,float *theta)
1452     {
1453     int i;
1454     double buffer=0.0;
1455 greg 2.12
1456 greg 2.1 for (i=0;i<145;i++)
1457 greg 2.12 {
1458 greg 2.1 buffer += (*(lv+i))*cos(radians(*(theta+i)));
1459 greg 2.12 }
1460    
1461 greg 2.9 return buffer*2*M_PI/144;
1462 greg 2.1 }
1463    
1464    
1465    
1466     /* enter day number(double), return E0 = square(R0/R): eccentricity correction factor */
1467    
1468     double get_eccentricity()
1469     {
1470     double day_angle;
1471     double E0;
1472    
1473 greg 2.9 day_angle = 2*M_PI*(daynumber - 1)/365;
1474 greg 2.1 E0 = 1.00011+0.034221*cos(day_angle)+0.00128*sin(day_angle)+
1475     0.000719*cos(2*day_angle)+0.000077*sin(2*day_angle);
1476    
1477     return (E0);
1478     }
1479    
1480    
1481     /* enter sunzenith angle (degrees) return relative air mass (double) */
1482     double air_mass()
1483     {
1484     double m;
1485     if (sunzenith>90)
1486     {
1487 greg 2.14 if(suppress_warnings==0)
1488     { fprintf(stderr, "Warning: air mass has reached the maximal value\n"); }
1489     sunzenith=90;
1490 greg 2.1 }
1491 greg 2.9 m = 1/( cos(sunzenith*M_PI/180)+0.15*exp( log(93.885-sunzenith)*(-1.253) ) );
1492 greg 2.1 return(m);
1493     }
1494    
1495    
1496