ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/gendaylit.c
(Generate patch)

Comparing ray/src/gen/gendaylit.c (file contents):
Revision 2.5 by greg, Wed Aug 10 22:30:31 2011 UTC vs.
Revision 2.20 by greg, Wed Nov 18 17:18:41 2020 UTC

# Line 1 | Line 1
1 < #ifndef lint
2 < static const char RCSid[] = "$Id$";
3 < #endif
4 < /*        Copyright (c) 1994    *Fraunhofer Institut for Solar Energy Systems
5 < *                              Oltmannstr 5, D-79100 Freiburg, Germany
1 > /*      Copyright (c) 1994,2006 *Fraunhofer Institut for Solar Energy Systems
2 > *                              Heidenhofstr. 2, D-79110 Freiburg, Germany
3   *                              *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 + *  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   */
9  
10 <
13 <
14 < /*
15 < *  gendaylit.c         program to generate the angular distribution of the daylight.
16 < *                      Our zenith is along the Z-axis, the X-axis
17 < *                      points east, and the Y-axis points north.
18 < */
19 <
10 > #define  _USE_MATH_DEFINES
11   #include  <stdio.h>
12   #include  <string.h>
13   #include  <math.h>
14   #include  <stdlib.h>
24 #include  <ctype.h>
15  
26 #include  "rtio.h"
27 #include  "fvect.h"
16   #include  "color.h"
17 + #include  "sun.h"
18   #include  "paths.h"
19  
20 < extern int jdate(int month, int day);
32 < extern double stadj(int  jd);
33 < extern double sdec(int  jd);
34 < extern double salt(double sd, double st);
35 < extern double sazi(double sd, double st);
20 > #define  DOT(v1,v2)     (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
21  
22   double  normsc();
23  
24 < #define DATFILE         "coeff_perez.dat"
24 > /*static        char *rcsid="$Header$";*/
25  
26 + float coeff_perez[] = {
27 +        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 < /* Perez sky parametrization : epsilon and delta calculations from the direct and diffuse irradiances */
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 > /* 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 >
61 >
62 >
63 > /* Perez sky parametrization: epsilon and delta calculations from the direct and diffuse irradiances */
64   double sky_brightness();
65   double sky_clearness();
46 void computesky();
66  
67   /* calculation of the direct and diffuse components from the Perez parametrization */
68 < double  diffus_irradiance_from_sky_brightness();
68 > double  diffuse_irradiance_from_sky_brightness();
69   double  direct_irradiance_from_sky_clearness();
70  
71 + /* 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  
53 /* Perez global horizontal, diffuse horizontal and direct normal luminous efficacy models : input w(cm)=2cm, solar zenith angle(degrees); output efficacy(lm/W) */
74   double  glob_h_effi_PEREZ();
75   double  glob_h_diffuse_effi_PEREZ();
76   double  direct_n_effi_PEREZ();
77 +
78   /*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 + void    print_error_sky();
84  
85 <
86 < /* Perez sky luminance model */
65 < int     lect_coeff_perez(char *filename,float **coeff_perez);
66 < double  calc_rel_lum_perez(double dzeta,double gamma,double Z,
67 <                double epsilon,double Delta,float *coeff_perez);
68 < /* coefficients for the sky luminance perez model */
69 < void    coeff_lum_perez(double Z, double epsilon, double Delta, float *coeff_perez);
85 > double  calc_rel_lum_perez(double dzeta,double gamma,double Z,double epsilon,double Delta,float coeff_perez[]);
86 > void    coeff_lum_perez(double Z, double epsilon, double Delta, float coeff_perez[]);
87   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);
74 float   *theta_ordered(char *filename);
75 float   *phi_ordered(char *filename);
76 void    skip_comments(FILE *fp);
91  
92 + void printdefaults();
93 + void check_sun_position();
94 + void computesky();
95 + void printhead(int ac, char** av);
96 + void usage_error(char* msg);
97 + void printsky();
98  
99 + FILE * frlibopen(char* fname);
100  
101   /* astronomy and geometry*/
102   double  get_eccentricity();
103   double  air_mass();
83 double  get_angle_sun_direction(double sun_zenith, double sun_azimut, double direction_zenith, double direction_azimut);
104  
105 + double  solar_sunset(int month, int day);
106 + double  solar_sunrise(int month, int day);
107  
86 /* date*/
87 int     jdate(int month, int day);
88
89
90
91
92
93 /* sun calculation constants */
94 extern double  s_latitude;
95 extern double  s_longitude;
96 extern double  s_meridian;
97
108   const double    AU = 149597890E3;
109   const double    solar_constant_e = 1367;    /* solar constant W/m^2 */
110 < const double    solar_constant_l = 127.5;   /* solar constant klux */
110 > const double    solar_constant_l = 127500;   /* solar constant lux */
111  
112   const double    half_sun_angle = 0.2665;
113   const double    half_direct_angle = 2.85;
114  
115 < const double    skyclearinf = 1.000;    /* limitations for the variation of the Perez parameters */
116 < const double    skyclearsup = 12.1;
115 > const double    skyclearinf = 1.0;          /* limitations for the variation of the Perez parameters */
116 > const double    skyclearsup = 12.01;
117   const double    skybriginf = 0.01;
118   const double    skybrigsup = 0.6;
119  
120  
121  
122   /* required values */
123 + int  year = 0;                                  /* year (optional) */
124   int     month, day;                             /* date */
125   double  hour;                                   /* time */
126   int     tsolar;                                 /* 0=standard, 1=solar */
# Line 118 | Line 129 | double  altitude, azimuth;                     /* or solar angles */
129  
130  
131   /* definition of the sky conditions through the Perez parametrization */
132 < double  skyclearness, skybrightness;
133 < double  solarradiance;  /*radiance of the sun disk and of the circumsolar area*/
134 < double  diffusilluminance, directilluminance, diffusirradiance, directirradiance;
135 < double  sunzenith, daynumber=150, atm_preci_water=2;
132 > double  skyclearness = 0;
133 > double  skybrightness = 0;
134 > double  solarradiance;
135 > double  diffuseilluminance, directilluminance, diffuseirradiance, directirradiance, globalirradiance;
136 > double  sunzenith, daynumber, atm_preci_water=2;
137  
138 < double  diffnormalization, dirnormalization;
138 > /*double  sunaltitude_border = 0;*/
139 > double  diffnormalization = 0;
140 > double  dirnormalization = 0;
141   double  *c_perez;
142  
143 < int     output=0;       /*define the unit of the output (sky luminance or radiance): visible watt=0, solar watt=1, lumen=2*/
144 < int     input=0;        /*define the input for the calulation*/
143 > int     output=0;       /* define the unit of the output (sky luminance or radiance): */
144 >                        /* visible watt=0, solar watt=1, lumen=2 */
145 > int     input=0;        /* define the input for the calulation */
146 > int     color_output=0;
147 > int     suppress_warnings=0;
148  
149          /* default values */
150 < int  cloudy = 0;                                /* 1=standard, 2=uniform */
151 < int  dosun = 1;
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 +
158          /* computed values */
159   double  sundir[3];
160 < double  groundbr;
160 > double  groundbr = 0;
161   double  F2;
162   double  solarbr = 0.0;
163   int     u_solar = 0;                            /* -1=irradiance, 1=radiance */
164 + float   timeinterval = 0;
165  
166 < char  *progname;
167 < char  errmsg[128];
166 > char    *progname;
167 > char    errmsg[128];
168  
169 + double  st;
170  
171 < main(argc, argv)
172 < int  argc;
153 < char  *argv[];
171 >
172 > int main(int argc, char** argv)
173   {
174          int  i;
175  
176          progname = argv[0];
177          if (argc == 2 && !strcmp(argv[1], "-defaults")) {
178                  printdefaults();
179 <                exit(0);
179 >                return 0;
180          }
181          if (argc < 4)
182 <                userror("arg count");
182 >                usage_error("arg count");
183          if (!strcmp(argv[1], "-ang")) {
184                  altitude = atof(argv[2]) * (M_PI/180);
185                  azimuth = atof(argv[3]) * (M_PI/180);
# Line 168 | Line 187 | char  *argv[];
187          } else {
188                  month = atoi(argv[1]);
189                  if (month < 1 || month > 12)
190 <                        userror("bad month");
190 >                        usage_error("bad month");
191                  day = atoi(argv[2]);
192                  if (day < 1 || day > 31)
193 <                        userror("bad day");
193 >                        usage_error("bad day");
194                  hour = atof(argv[3]);
195                  if (hour < 0 || hour >= 24)
196 <                        userror("bad hour");
196 >                        usage_error("bad hour");
197                  tsolar = argv[3][0] == '+';
198          }
199          for (i = 4; i < argc; i++)
# Line 184 | Line 203 | char  *argv[];
203                                  cloudy = 0;
204                                  dosun = argv[i][0] == '+';
205                                  break;
206 <                        case 'r':
206 >                        case 'y':
207 >                                year = atoi(argv[++i]);
208 >                                break;
209                          case 'R':
210                                  u_solar = argv[i][1] == 'R' ? -1 : 1;
211                                  solarbr = atof(argv[++i]);
# Line 193 | Line 214 | char  *argv[];
214                                  cloudy = argv[i][0] == '+' ? 2 : 1;
215                                  dosun = 0;
216                                  break;
217 +                        case 'C':
218 +                                if (argv[i][2] == 'I' && argv[i][3] == 'E' ) {
219 +                                locus[0] = -4.607e9;
220 +                                locus[1] = 2.9678e6;
221 +                                locus[2] = 0.09911e3;
222 +                                locus[3] = 0.244063;
223 +                                locus[4] = -2.0064e9;
224 +                                locus[5] = 1.9018e6;
225 +                                locus[6] = 0.24748e3;
226 +                                locus[7] = 0.23704;
227 +                                locus[8] = -3.0;
228 +                                locus[9] = 2.87;
229 +                                locus[10] = -0.275;
230 +                                 }else{ color_output = 1;
231 +                                 }
232 +                                break;
233 +                        case 'l':
234 +                                locus[0] = atof(argv[++i]);
235 +                                locus[1] = atof(argv[++i]);
236 +                                locus[2] = atof(argv[++i]);
237 +                                locus[3] = atof(argv[++i]);
238 +                                locus[4] = locus[0];
239 +                                locus[5] = locus[1];
240 +                                locus[6] = locus[2];
241 +                                locus[7] = locus[3];
242 +                                locus[8] = atof(argv[++i]);
243 +                                locus[9] = atof(argv[++i]);
244 +                                locus[10] = atof(argv[++i]);
245 +                                break;
246 +
247                          case 't':
248                                  betaturbidity = atof(argv[++i]);
249                                  break;
250 +                        case 'w':
251 +                                suppress_warnings = 1;
252 +                                break;                  
253                          case 'b':
254                                  zenithbr = atof(argv[++i]);
255                                  break;
# Line 211 | Line 265 | char  *argv[];
265                          case 'm':
266                                  s_meridian = atof(argv[++i]) * (M_PI/180);
267                                  break;
214
268                          
269                          case 'O':
270 <                                output = atof(argv[++i]);       /*define the unit of the output of the program :
271 <                                                                sky and sun luminance/radiance (0==W visible, 1==W solar radiation, 2==lm)
272 <                                                                default is set to 0*/
270 >                                output = atof(argv[++i]);       /*define the unit of the output of the program:
271 >                                                                sky and sun luminance/radiance
272 >                                                                (0==W visible, 1==W solar radiation, 2==lm) */
273                                  break;
274                                  
275                          case 'P':
# Line 228 | Line 281 | char  *argv[];
281                          case 'W':                                       /* direct normal Irradiance [W/m^2] */
282                                  input = 1;                              /* diffuse horizontal Irrad. [W/m^2] */
283                                  directirradiance = atof(argv[++i]);
284 <                                diffusirradiance = atof(argv[++i]);
284 >                                diffuseirradiance = atof(argv[++i]);
285                                  break;
286                                  
287                          case 'L':                                       /* direct normal Illuminance [Lux] */
288                                  input = 2;                              /* diffuse horizontal Ill. [Lux] */
289                                  directilluminance = atof(argv[++i]);
290 <                                diffusilluminance = atof(argv[++i]);
290 >                                diffuseilluminance = atof(argv[++i]);
291                                  break;
292                          
293                          case 'G':                                       /* direct horizontal Irradiance [W/m^2] */
294                                  input = 3;                              /* diffuse horizontal Irrad. [W/m^2] */
295                                  directirradiance = atof(argv[++i]);
296 <                                diffusirradiance = atof(argv[++i]);
296 >                                diffuseirradiance = atof(argv[++i]);
297                                  break;
245                                
298                          
299 +                        case 'E':                                       /* Erbs model based on the */
300 +                                input = 4;                              /* global-horizontal irradiance [W/m^2] */
301 +                                globalirradiance = atof(argv[++i]);
302 +                                break;
303 +                        
304 +                        case 'i':
305 +                                timeinterval = atof(argv[++i]);
306 +                                break;
307 +                        
308 +                        
309                          default:
310                                  sprintf(errmsg, "unknown option: %s", argv[i]);
311 <                                userror(errmsg);
311 >                                usage_error(errmsg);
312                          }
313                  else
314 <                        userror("bad option");
314 >                        usage_error("bad option");
315  
316 <        if (fabs(s_meridian-s_longitude) > 30*M_PI/180)
317 <                fprintf(stderr,
256 <                    "%s: warning: %.1f hours btwn. standard meridian and longitude\n",
316 >        if (month && !tsolar && fabs(s_meridian-s_longitude) > 45*M_PI/180)
317 >                fprintf(stderr,"%s: warning: %.1f hours btwn. standard meridian and longitude\n",
318                      progname, (s_longitude-s_meridian)*12/M_PI);
319  
320  
321 <        /* allocation dynamique de memoire pour les pointeurs */
322 <        if ( (c_perez = malloc(5*sizeof(double))) == NULL )
323 <        {
263 <                fprintf(stderr,"Out of memory error in function main !");
264 <                exit(1);
265 <        }
321 >        /* dynamic memory allocation for the pointers */
322 >        if ( (c_perez = calloc(5, sizeof(double))) == NULL )
323 >        { fprintf(stderr,"Out of memory error in function main"); return 1; }
324  
325 <
325 >        
326          printhead(argc, argv);
269
327          computesky();
328          printsky();
329 +        return 0;
330  
273        exit(0);
331   }
332  
333  
334 < void
335 < computesky()                    /* compute sky parameters */
334 >
335 >
336 >
337 > void computesky()
338   {
339  
340 <        /* new variables */
341 <        int     j, i;
342 <        float   *lv_mod;  /* 145 luminance values*/
343 <          /* 145 directions for the calculation of the normalization coefficient, coefficient Perez model */
285 <        float   *theta_o, *phi_o, *coeff_perez;
340 >        int     j;
341 >        
342 >        float   *lv_mod;  /* 145 luminance values */
343 >        float   *theta_o, *phi_o;
344          double  dzeta, gamma;
287        double  diffusion;
345          double  normfactor;
346 +        double  erbs_s0, erbs_kt;
347  
348  
291
349          /* compute solar direction */
350 <
350 >                
351          if (month) {                    /* from date and time */
352 <                int  jd;
296 <                double  sd, st;
352 >                double  sd;
353  
354 <                jd = jdate(month, day);         /* Julian date */
355 <                sd = sdec(jd);                  /* solar declination */
356 <                if (tsolar)                     /* solar time */
357 <                        st = hour;
354 >                st = hour;
355 >                if (year) {                     /* Michalsky algorithm? */
356 >                        double  mjd = mjdate(year, month, day, hour);
357 >                        if (tsolar)
358 >                                sd = msdec(mjd, NULL);
359 >                        else
360 >                                sd = msdec(mjd, &st);
361 >                } else {
362 >                        int  jd = jdate(month, day);    /* Julian date */
363 >                        sd = sdec(jd);                  /* solar declination */
364 >                        if (!tsolar)                    /* get solar time? */
365 >                                st = hour + stadj(jd);
366 >                }
367 >                                                        
368 >                if(timeinterval) {
369 >                        
370 >                        if(timeinterval<0) {
371 >                        fprintf(stderr, "time interval negative\n");
372 >                        exit(1);
373 >                        }
374 >                                                                        
375 >                        if(fabs(solar_sunrise(month,day)-st)<=timeinterval/120) {                      
376 >                         st= (st+timeinterval/120+solar_sunrise(month,day))/2;
377 >                         if(suppress_warnings==0)
378 >                         { fprintf(stderr, "Solar position corrected at time step %d %d %.3f\n",month,day,hour); }
379 >                        }
380 >                
381 >                        if(fabs(solar_sunset(month,day)-st)<timeinterval/120) {
382 >                         st= (st-timeinterval/120+solar_sunset(month,day))/2;
383 >                         if(suppress_warnings==0)
384 >                         { fprintf(stderr, "Solar position corrected at time step %d %d %.3f\n",month,day,hour); }
385 >                        }
386 >                        
387 >                        if((st<solar_sunrise(month,day)-timeinterval/120) || (st>solar_sunset(month,day)+timeinterval/120)) {
388 >                          if(suppress_warnings==0)
389 >                          { fprintf(stderr, "Warning: sun position too low, printing error sky at %d %d %.3f\n",month,day,hour); }
390 >                         altitude = salt(sd, st);
391 >                         azimuth = sazi(sd, st);
392 >                         print_error_sky();
393 >                         exit(0);
394 >                        }
395 >                }
396                  else
397 <                        st = hour + stadj(jd);
397 >                
398 >                if(st<solar_sunrise(month,day) || st>solar_sunset(month,day)) {
399 >                        if(suppress_warnings==0)
400 >                        { fprintf(stderr, "Warning: sun altitude below zero at time step %i %i %.2f, printing error sky\n",month,day,hour); }
401 >                        altitude = salt(sd, st);
402 >                        azimuth = sazi(sd, st);
403 >                        print_error_sky();
404 >                        exit(0);
405 >                }
406 >                
407                  altitude = salt(sd, st);
408                  azimuth = sazi(sd, st);
409                  
410                  daynumber = (double)jdate(month, day);
411 <
411 >                
412          }
413 +        
414 +        
415 +
416 +
417 +                        
418          if (!cloudy && altitude > 87.*M_PI/180.) {
419 <                fprintf(stderr,
419 >                
420 >                if (suppress_warnings==0) {
421 >                    fprintf(stderr,
422                      "%s: warning - sun too close to zenith, reducing altitude to 87 degrees\n",
423                      progname);
424 <                printf(
315 <                    "# warning - sun too close to zenith, reducing altitude to 87 degrees\n");
424 >                }
425                  altitude = 87.*M_PI/180.;
426          }
427 +        
428 +        
429 +        
430          sundir[0] = -sin(azimuth)*cos(altitude);
431          sundir[1] = -cos(azimuth)*cos(altitude);
432          sundir[2] = sin(altitude);
# Line 322 | Line 434 | computesky()                   /* compute sky parameters */
434                  
435          /* calculation for the new functions */
436          sunzenith = 90 - altitude*180/M_PI;
437 <        
326 <        
437 >                        
438  
439 < /* compute the inputs for the calculation of the light distribution over the sky*/
440 <        if (input==0)
439 >        /* compute the inputs for the calculation of the light distribution over the sky*/
440 >        if (input==0)           /* P */
441                  {
442                  check_parametrization();
443 <                diffusirradiance = diffus_irradiance_from_sky_brightness(); /*diffuse horizontal irradiance*/
443 >                diffuseirradiance = diffuse_irradiance_from_sky_brightness(); /*diffuse horizontal irradiance*/
444                  directirradiance = direct_irradiance_from_sky_clearness();
445                  check_irradiances();
446                  
447                  if (output==0 || output==2)
448                          {
449 <                        diffusilluminance = diffusirradiance*glob_h_diffuse_effi_PEREZ();/*diffuse horizontal illuminance*/
449 >                        diffuseilluminance = diffuseirradiance*glob_h_diffuse_effi_PEREZ();/*diffuse horizontal illuminance*/
450                          directilluminance = directirradiance*direct_n_effi_PEREZ();
451                          check_illuminances();
452                          }
453                  }
454          
455  
456 <        else if (input==1)
456 >        else if (input==1)      /* W */
457                  {
458                  check_irradiances();
459                  skybrightness = sky_brightness();
460                  skyclearness =  sky_clearness();
461 +                
462                  check_parametrization();
463 <
463 >                                                        
464                  if (output==0 || output==2)
465                          {
466 <                        diffusilluminance = diffusirradiance*glob_h_diffuse_effi_PEREZ();/*diffuse horizontal illuminance*/
466 >                        diffuseilluminance = diffuseirradiance*glob_h_diffuse_effi_PEREZ();/*diffuse horizontal illuminance*/
467                          directilluminance = directirradiance*direct_n_effi_PEREZ();
468                          check_illuminances();
469                          }
# Line 359 | Line 471 | computesky()                   /* compute sky parameters */
471                  }
472                          
473          
474 <        else if (input==2)
474 >        else if (input==2)      /* L */
475                  {              
476                  check_illuminances();
477                  illu_to_irra_index();
# Line 367 | Line 479 | computesky()                   /* compute sky parameters */
479                  }
480                  
481  
482 <        else if (input==3)
482 >        else if (input==3)      /* G */
483                  {
484                          if (altitude<=0)
485                          {
486 <                        fprintf(stderr, "solar zenith angle larger than 90� \n the models used are not more valid\n");
487 <                        exit(1);
486 >                                if (suppress_warnings==0)
487 >                                     fprintf(stderr, "Warning: sun altitude < 0, proceed with irradiance values of zero\n");
488 >                                directirradiance = 0;
489 >                                diffuseirradiance = 0;
490 >                        } else {
491 >                        
492 >                                directirradiance=directirradiance/sin(altitude);
493                          }
494 <
378 <                directirradiance=directirradiance/sin(altitude);
494 >                                
495                  check_irradiances();
496                  skybrightness = sky_brightness();
497                  skyclearness =  sky_clearness();
# Line 383 | Line 499 | computesky()                   /* compute sky parameters */
499  
500                  if (output==0 || output==2)
501                          {
502 <                        diffusilluminance = diffusirradiance*glob_h_diffuse_effi_PEREZ();/*diffuse horizontal illuminance*/
502 >                        diffuseilluminance = diffuseirradiance*glob_h_diffuse_effi_PEREZ();/*diffuse horizontal illuminance*/
503                          directilluminance = directirradiance*direct_n_effi_PEREZ();
504                          check_illuminances();
505                          }
506  
507                  }
508  
393        
394        else    {fprintf(stderr,"error in giving the input arguments"); exit(1);}
509  
510 +        else if (input==4)      /* E */         /* Implementation of the Erbs model. W.Sprenger (04/13) */
511 +                {
512 +                        
513 +                        if (altitude<=0)
514 +                        {
515 +                                if (suppress_warnings==0 && globalirradiance > 50)
516 +                                        fprintf(stderr, "Warning: global irradiance higher than 50 W/m^2 while the sun altitude is lower than zero\n");
517 +                                globalirradiance = 0; diffuseirradiance = 0; directirradiance = 0;
518 +                        
519 +                        } else {
520 +                        
521 +                        erbs_s0 = solar_constant_e*get_eccentricity()*sin(altitude);
522 +                        
523 +                        if (globalirradiance>erbs_s0)
524 +                        {
525 +                                if (suppress_warnings==0)
526 +                                        fprintf(stderr, "Warning: global irradiance is higher than the time-dependent solar constant s0\n");
527 +                                globalirradiance=erbs_s0*0.999;                
528 +                        }
529 +                        
530 +                        erbs_kt=globalirradiance/erbs_s0;
531 +                        
532 +                        if (erbs_kt<=0.22)      diffuseirradiance=globalirradiance*(1-0.09*erbs_kt);
533 +                        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));
534 +                        else if (erbs_kt<1)     diffuseirradiance=globalirradiance*(0.165);
535 +                        
536 +                        directirradiance=globalirradiance-diffuseirradiance;
537 +                        
538 +                        printf("# erbs_s0, erbs_kt, irr_dir_h, irr_diff: %.3f %.3f %.3f %.3f\n", erbs_s0, erbs_kt, directirradiance, diffuseirradiance);
539 +                        printf("# WARNING: the -E option is only recommended for a rough estimation!\n");
540 +                        
541 +                        directirradiance=directirradiance/sin(altitude);
542 +                                                                                                                        
543 +                        }
544 +                        
545 +                check_irradiances();
546 +                skybrightness = sky_brightness();
547 +                skyclearness =  sky_clearness();
548 +                check_parametrization();
549  
550 +                if (output==0 || output==2)
551 +                        {
552 +                        diffuseilluminance = diffuseirradiance*glob_h_diffuse_effi_PEREZ();/*diffuse horizontal illuminance*/
553 +                        directilluminance = directirradiance*direct_n_effi_PEREZ();
554 +                        check_illuminances();
555 +                        }
556 +
557 +                }
558 +                
559 +                
560 +                
561          
562 < /* normalization factor for the relative sky luminance distribution, diffuse part*/
562 >        else    { fprintf(stderr,"error at the input arguments"); exit(1); }
563  
400        /* allocation dynamique de memoire pour les pointeurs */
401        if ( (coeff_perez = malloc(8*20*sizeof(float))) == NULL )
402        {
403                fprintf(stderr,"Out of memory error in function main !");
404                exit(1);
405        }
564  
565 < /* read the coefficients for the Perez sky luminance model */
566 <        if (lect_coeff_perez(DATFILE, &coeff_perez) > 0)
567 <        {
410 <                fprintf(stderr,"lect_coeff_perez does not work\n");
411 <                exit(2);
412 <        }
413 <
565 >        
566 >        /* normalization factor for the relative sky luminance distribution, diffuse part*/
567 >        
568          if ( (lv_mod = malloc(145*sizeof(float))) == NULL)
569          {
570                  fprintf(stderr,"Out of memory in function main");
# Line 418 | Line 572 | computesky()                   /* compute sky parameters */
572          }
573  
574          /* read the angles */
575 <        theta_o = theta_ordered("defangle.dat");
576 <        phi_o = phi_ordered("defangle.dat");
575 >        theta_o = defangle_theta;
576 >        phi_o = defangle_phi;
577 >        
578  
579 < /* parameters for the perez model */
579 >        /* parameters for the perez model */
580          coeff_lum_perez(radians(sunzenith), skyclearness, skybrightness, coeff_perez);
581  
582 < /*calculation of the modelled luminance */
582 >        
583 >        
584 >        /*calculation of the modelled luminance */
585          for (j=0;j<145;j++)
586          {
587                  theta_phi_to_dzeta_gamma(radians(*(theta_o+j)),radians(*(phi_o+j)),&dzeta,&gamma,radians(sunzenith));
588 +                                
589                  *(lv_mod+j) = calc_rel_lum_perez(dzeta,gamma,radians(sunzenith),skyclearness,skybrightness,coeff_perez);
590 <                /*printf("theta, phi, lv_mod %lf\t %lf\t %lf\n", *(theta_o+j),*(phi_o+j),*(lv_mod+j));*/
590 >                
591 >                /* fprintf(stderr,"theta, phi, lv_mod %f\t %f\t %f\n", *(theta_o+j),*(phi_o+j),*(lv_mod+j)); */
592          }
593 <
593 >        
594          /* integration of luminance for the normalization factor, diffuse part of the sky*/
595 +        
596          diffnormalization = integ_lv(lv_mod, theta_o);
437        /*printf("perez integration %lf\n", diffnormalization);*/
597          
439        
598  
599  
600 < /*normalization coefficient in lumen or in watt*/
600 >        /*normalization coefficient in lumen or in watt*/
601          if (output==0)
602                  {
603 <                diffnormalization = diffusilluminance/diffnormalization/WHTEFFICACY;
603 >                diffnormalization = diffuseilluminance/diffnormalization/WHTEFFICACY;
604                  }
605          else if (output==1)
606                  {
607 <                diffnormalization = diffusirradiance/diffnormalization;
607 >                diffnormalization = diffuseirradiance/diffnormalization;
608                  }
609          else if (output==2)
610                  {
611 <                diffnormalization = diffusilluminance/diffnormalization;
611 >                diffnormalization = diffuseilluminance/diffnormalization;
612                  }
613  
614 <        else    {fprintf(stderr,"output argument : wrong number"); exit(1);}
614 >        else    {fprintf(stderr,"Wrong output specification.\n"); exit(1);}
615  
616  
617  
618  
619 < /* calculation for the solar source */  
619 >        /* calculation for the solar source */  
620          if (output==0)
621                  solarradiance = directilluminance/(2*M_PI*(1-cos(half_sun_angle*M_PI/180)))/WHTEFFICACY;
622                  
# Line 468 | Line 626 | computesky()                   /* compute sky parameters */
626          else
627                  solarradiance = directilluminance/(2*M_PI*(1-cos(half_sun_angle*M_PI/180)));
628          
471                        
629  
630  
631 < /* Compute the ground radiance */
632 < zenithbr=calc_rel_lum_perez(0.0,radians(sunzenith),radians(sunzenith),skyclearness,skybrightness,coeff_perez);
633 < zenithbr*=diffnormalization;
634 < /*
635 < fprintf(stderr, "gendaylit : the actual zenith radiance(W/m^2/sr) or luminance(cd/m^2) is : %.0lf\n", zenithbr);
479 < */
480 <
481 < if (skyclearness==1)
631 >        /* Compute the ground radiance */
632 >        zenithbr=calc_rel_lum_perez(0.0,radians(sunzenith),radians(sunzenith),skyclearness,skybrightness,coeff_perez);
633 >        zenithbr*=diffnormalization;
634 >        
635 >        if (skyclearness==1)
636          normfactor = 0.777778;
637                  
638 < if (skyclearness>=6)
638 >        if (skyclearness>=6)
639          {              
640          F2 = 0.274*(0.91 + 10.0*exp(-3.0*(M_PI/2.0-altitude)) + 0.45*sundir[2]*sundir[2]);
641          normfactor = normsc()/F2/M_PI;
642          }
643  
644 < if ( (skyclearness>1) && (skyclearness<6) )
644 >        if ( (skyclearness>1) && (skyclearness<6) )
645          {
646          S_INTER=1;
647          F2 = (2.739 + .9891*sin(.3119+2.6*altitude)) * exp(-(M_PI/2.0-altitude)*(.4441+1.48*altitude));
648          normfactor = normsc()/F2/M_PI;
649          }
650  
651 < groundbr = zenithbr*normfactor;
498 < printf("# Ground ambient level: %.1f\n", groundbr);
651 >        groundbr = zenithbr*normfactor;
652  
653 < if (dosun&&(skyclearness>1))
654 < groundbr += 6.8e-5/M_PI*solarradiance*sundir[2];                
653 >        if (dosun&&(skyclearness>1))
654 >        groundbr += 6.8e-5/M_PI*solarradiance*sundir[2];                
655  
656 < groundbr *= gprefl;
656 >        groundbr *= gprefl;
657  
658  
659 +                
660 +        if(*(c_perez+1)>0)
661 +        {
662 +          if(suppress_warnings==0)
663 +                {  fprintf(stderr, "Warning: positive Perez parameter B (= %lf), printing error sky\n",*(c_perez+1));}  
664 +          print_error_sky();
665 +          exit(0);
666 +        }
667  
668 +
669   return;
670   }
671  
# Line 511 | Line 673 | return;
673  
674  
675  
676 + double solar_sunset(int month,int day)
677 + {
678 +     float W;
679 +     extern double s_latitude;
680 +     W=-1*(tan(s_latitude)*tan(sdec(jdate(month, day))));
681 +     return(12+(M_PI/2 - atan2(W,sqrt(1-W*W)))*180/(M_PI*15));
682 + }
683  
684  
685 < printsky()                      /* print out sky */
685 >
686 >
687 > double solar_sunrise(int month,int day)
688   {
689 +     float W;
690 +     extern double s_latitude;
691 +     W=-1*(tan(s_latitude)*tan(sdec(jdate(month, day))));
692 +     return(12-(M_PI/2 - atan2(W,sqrt(1-W*W)))*180/(M_PI*15));
693 + }
694 +
695 +
696 +
697 +
698 + void printsky()
699 + {      
700 +        
701 +        printf("# Local solar time: %.2f\n", st);
702 +        printf("# Solar altitude and azimuth: %.1f %.1f\n", altitude*180/M_PI, azimuth*180/M_PI);
703 +
704 +
705          if (dosun&&(skyclearness>1))
706 <                {              
706 >        {              
707                  printf("\nvoid light solar\n");
708                  printf("0\n0\n");
709                  printf("3 %.3e %.3e %.3e\n", solarradiance, solarradiance, solarradiance);
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 <                }
527 <                
528 <        if (dosun&&(skyclearness==1))
529 <                {              
713 >        } else if (dosun) {
714                  printf("\nvoid light solar\n");
715                  printf("0\n0\n");
716                  printf("3 0.0 0.0 0.0\n");
717                  printf("\nsolar source sun\n");
718                  printf("0\n0\n");
719                  printf("4 %f %f %f %f\n", sundir[0], sundir[1], sundir[2], 2*half_sun_angle);
720 <                }
720 >        }
721 > /* print colored output if activated in command line (-C). Based on model from A. Diakite, TU-Berlin. Implemented by J. Wienold, August 26 2018 */      
722 >        if  (color_output==1 && skyclearness < 4.5 && skyclearness >1.065 )  
723 >        {
724 >        fprintf(stderr, "       warning: sky clearness(epsilon)= %f \n",skyclearness);
725 >        fprintf(stderr, "       warning: intermediate sky!! \n");
726 >        fprintf(stderr, "       warning: color model for intermediate sky pending  \n");
727 >        fprintf(stderr, "       warning: no color output ! \n");
728 >        color_output=0;
729 >        }
730 >        if (color_output==1)
731 >        {
732 >        printf("\nvoid colorfunc skyfunc\n");
733 >        printf("4 skybright_r skybright_g skybright_b perezlum_c.cal\n");
734 >        printf("0\n");
735 >        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,
736 >                *(c_perez+0),*(c_perez+1),*(c_perez+2),*(c_perez+3),*(c_perez+4),
737 >                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]);
738 >        }else{
739 >        printf("\nvoid brightfunc skyfunc\n");
740 >        printf("2 skybright perezlum.cal\n");
741 >        printf("0\n");
742 >        printf("10 %.3e %.3e %lf %lf %lf %lf %lf %f %f %f \n", diffnormalization, groundbr,
743 >                *(c_perez+0),*(c_perez+1),*(c_perez+2),*(c_perez+3),*(c_perez+4),
744 >                sundir[0], sundir[1], sundir[2]);
745 >         }
746          
747 + }
748  
749 +
750 +
751 + void print_error_sky()
752 + {
753 +
754 +
755 +        sundir[0] = -sin(azimuth)*cos(altitude);
756 +        sundir[1] = -cos(azimuth)*cos(altitude);
757 +        sundir[2] = sin(altitude);
758 +
759 +        printf("# Local solar time: %.2f\n", st);
760 +        printf("# Solar altitude and azimuth: %.1f %.1f\n", altitude*180/M_PI, azimuth*180/M_PI);
761 +
762          printf("\nvoid brightfunc skyfunc\n");
763          printf("2 skybright perezlum.cal\n");
764          printf("0\n");
765 <        printf("10 %.3e %.3e %lf %lf %lf %lf %lf %f %f %f \n", diffnormalization, groundbr,
543 <            *(c_perez+0),*(c_perez+1),*(c_perez+2),*(c_perez+3),*(c_perez+4),
544 <            sundir[0], sundir[1], sundir[2]);
765 >        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]);
766   }
767 +        
768  
769  
770 < printdefaults()                 /* print default values */
770 >
771 >
772 > void printdefaults()                    /* print default values */
773   {
774          printf("-g %f\t\t\t# Ground plane reflectance\n", gprefl);
775          if (zenithbr > 0.0)
# Line 558 | Line 782 | printdefaults()                        /* print default values */
782   }
783  
784  
785 < userror(msg)                    /* print usage error and quit */
786 < char  *msg;
785 >
786 >
787 > void usage_error(char* msg)                     /* print usage error and quit */
788   {
789          if (msg != NULL)
790 <                fprintf(stderr, "%s: Use error - %s\n", progname, msg);
791 <        fprintf(stderr, "Usage: %s month day hour [-P|-W|-L] direct_value diffus_value [options]\n", progname);
792 <        fprintf(stderr, "or   : %s -ang altitude azimuth [-P|-W|-L] direct_value diffus_value [options]\n", progname);
790 >                fprintf(stderr, "%s: Use error - %s\n\n", progname, msg);
791 >        fprintf(stderr, "Usage: %s      month day hour [-y year]        [...]\n", progname);
792 >        fprintf(stderr, "   or: %s -ang altitude azimuth                [...]\n", progname);
793 >        fprintf(stderr, "               followed by:      -P          epsilon delta [options]\n");
794 >        fprintf(stderr, "                        or:      [-W|-L|-G]  direct_value diffuse_value [options]\n");
795 >        fprintf(stderr, "                        or:      -E          global_irradiance [options]\n\n");
796 >        fprintf(stderr, "    Description:\n");
797          fprintf(stderr, "       -P epsilon delta  (these are the Perez parameters) \n");
798          fprintf(stderr, "       -W direct-normal-irradiance diffuse-horizontal-irradiance (W/m^2)\n");
799          fprintf(stderr, "       -L direct-normal-illuminance diffuse-horizontal-illuminance (lux)\n");
800          fprintf(stderr, "       -G direct-horizontal-irradiance diffuse-horizontal-irradiance (W/m^2)\n");
801 +        fprintf(stderr, "       -E global-horizontal-irradiance (W/m^2)\n\n");
802 +        fprintf(stderr, "       Output specification with option:\n");
803          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");
804 +        fprintf(stderr, "       gendaylit version 2.5 (2018/04/18)  \n\n");
805          exit(1);
806   }
807  
808  
809  
810 < double
811 < normsc()                        /* compute normalization factor (E0*F2/L0) */
810 >
811 > double normsc()           /* compute normalization factor (E0*F2/L0) */
812   {
813          static double  nfc[2][5] = {
814                                  /* clear sky approx. */
# Line 599 | Line 831 | normsc()                       /* compute normalization factor (E0*F2/L0)
831  
832  
833  
834 < printhead(ac, av)               /* print command header */
835 < register int  ac;
836 < register char  **av;
834 >
835 >
836 > void printhead(int ac, char** av)               /* print command header */
837   {
838          putchar('#');
839          while (ac--) {
# Line 614 | Line 846 | register char  **av;
846  
847  
848  
617 void
618 skip_comments(FILE *fp)         /* skip comments in file */
619 {
620        int     c;
621        
622        while ((c = getc(fp)) != EOF)
623                if (c == '#') {
624                        while ((c = getc(fp)) != EOF)
625                                if (c == '\n')
626                                        break;
627                } else if (!isspace(c)) {
628                        ungetc(c, fp);
629                        break;
630                }
631 }
849  
850  
634
851   /* Perez models */
852  
853   /* Perez global horizontal luminous efficacy model */
# Line 641 | Line 857 | double glob_h_effi_PEREZ()
857          double  value;
858          double  category_bounds[10], a[10], b[10], c[10], d[10];
859          int     category_total_number, category_number, i;
860 <
861 <
862 < if (skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<=skybriginf || skybrightness>skybrigsup)
863 < fprintf(stderr, "Warning : skyclearness or skybrightness out of range ; \n Check your input parameters\n");
864 <
860 >        
861 >        check_parametrization();
862 >        
863 >        
864 > /*if ((skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<skybriginf || skybrightness>skybrigsup) && suppress_warnings==0)
865 >     fprintf(stderr, "Warning: skyclearness or skybrightness out of range in function glob_h_effi_PEREZ \n"); */
866 >    
867 >    
868          /* initialize category bounds (clearness index bounds) */
869  
870          category_total_number = 8;
# Line 700 | Line 919 | fprintf(stderr, "Warning : skyclearness or skybrightne
919  
920  
921  
703
922          for (i=1; i<=category_total_number; i++)
923          {
924                  if ( (skyclearness >= category_bounds[i]) && (skyclearness < category_bounds[i+1]) )
# Line 714 | Line 932 | fprintf(stderr, "Warning : skyclearness or skybrightne
932   }
933  
934  
935 +
936 +
937   /* global horizontal diffuse efficacy model, according to PEREZ */
938   double glob_h_diffuse_effi_PEREZ()
939   {
# Line 721 | Line 941 | double glob_h_diffuse_effi_PEREZ()
941          double  category_bounds[10], a[10], b[10], c[10], d[10];
942          int     category_total_number, category_number, i;
943  
944 +        check_parametrization();
945          
946 < if (skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<=skybriginf || skybrightness>skybrigsup)
947 < fprintf(stderr, "Warning : skyclearness or skybrightness out of range ; \n Check your input parameters\n");
948 <
946 >        
947 > /*if ((skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<skybriginf || skybrightness>skybrigsup) && suppress_warnings==0)
948 > fprintf(stderr, "Warning: skyclearness or skybrightness out of range in function glob_h_diffuse_PEREZ \n"); */
949 >    
950   /* initialize category bounds (clearness index bounds) */
951  
952          category_total_number = 8;
953  
954 + //XXX:  category_bounds > 0.1
955          category_bounds[1] = 1;
956          category_bounds[2] = 1.065;
957          category_bounds[3] = 1.230;
# Line 779 | Line 1002 | fprintf(stderr, "Warning : skyclearness or skybrightne
1002  
1003  
1004  
1005 <
1005 >        category_number = -1;
1006          for (i=1; i<=category_total_number; i++)
1007          {
1008                  if ( (skyclearness >= category_bounds[i]) && (skyclearness < category_bounds[i+1]) )
1009                          category_number = i;
1010          }
1011  
1012 +        if (category_number == -1) {
1013 +                if (suppress_warnings==0)
1014 +                    fprintf(stderr, "Warning: sky clearness (= %.3f) too high, printing error sky\n", skyclearness);
1015 +                print_error_sky();
1016 +                exit(0);
1017 +        }
1018 +                
1019 +
1020          value = a[category_number] + b[category_number]*atm_preci_water  + c[category_number]*cos(sunzenith*M_PI/180) +
1021              d[category_number]*log(skybrightness);
1022  
1023          return(value);
1024 +
1025   }
1026  
1027  
1028 +
1029 +
1030 +
1031 +
1032   /* direct normal efficacy model, according to PEREZ */
1033  
1034   double direct_n_effi_PEREZ()
# Line 803 | Line 1039 | double         category_bounds[10], a[10], b[10], c[10], d[10
1039   int     category_total_number, category_number, i;
1040  
1041  
1042 < if (skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<=skybriginf || skybrightness>skybrigsup)
1043 < fprintf(stderr, "Warning : skyclearness or skybrightness out of range ; \n Check your input parameters\n");
1042 > /*if ((skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<skybriginf || skybrightness>skybrigsup) && suppress_warnings==0)
1043 >   fprintf(stderr, "Warning: skyclearness or skybrightness out of range in function direct_n_effi_PEREZ \n");*/
1044  
1045  
1046   /* initialize category bounds (clearness index bounds) */
# Line 878 | Line 1114 | return(value);
1114   /*check the range of epsilon and delta indexes of the perez parametrization*/
1115   void check_parametrization()
1116   {
1117 < if (skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<=skybriginf || skybrightness>skybrigsup)
1117 >
1118 > if (skyclearness<skyclearinf || skyclearness>skyclearsup || skybrightness<skybriginf || skybrightness>skybrigsup)
1119                  {
1120 <                fprintf(stderr,"sky clearness or sky brightness out of range %lf\t %lf\n", skyclearness, skybrightness);
1121 <                exit(1);        
1120 >
1121 > /*  limit sky clearness or sky brightness, 2009 11 13 by J. Wienold */
1122 >                
1123 >                if (skyclearness<skyclearinf){
1124 >                        /* if (suppress_warnings==0)
1125 >                            fprintf(stderr,"Range warning: sky clearness too low (%lf)\n", skyclearness); */
1126 >                        skyclearness=skyclearinf;
1127                  }
1128 +                if (skyclearness>skyclearsup){
1129 +                        /* if (suppress_warnings==0)
1130 +                            fprintf(stderr,"Range warning: sky clearness too high (%lf)\n", skyclearness); */
1131 +                        skyclearness=skyclearsup-0.001;
1132 +                }
1133 +                if (skybrightness<skybriginf){
1134 +                        /* if (suppress_warnings==0)
1135 +                            fprintf(stderr,"Range warning: sky brightness too low (%lf)\n", skybrightness); */
1136 +                        skybrightness=skybriginf;
1137 +                }
1138 +                if (skybrightness>skybrigsup){
1139 +                        /* if (suppress_warnings==0)
1140 +                            fprintf(stderr,"Range warning: sky brightness too high (%lf)\n", skybrightness); */
1141 +                        skybrightness=skybrigsup;
1142 +                }
1143 +
1144 +        return; }
1145          else return;
1146   }
1147  
1148  
1149 < /* likelihood of the direct and diffuse components */
1149 >
1150 >
1151 >
1152 > /* validity of the direct and diffuse components */
1153   void    check_illuminances()
1154   {
1155 <        if (!( (directilluminance>=0) && (directilluminance<=solar_constant_l*1000) && (diffusilluminance>0) ))
1156 <        {
1157 <        fprintf(stderr,"direct or diffuse illuminances out of range\n");
1158 <        exit(1);
1155 >        if (directilluminance < 0) {
1156 >                if(suppress_warnings==0)
1157 >                { fprintf(stderr,"Warning: direct illuminance < 0. Using 0.0\n"); }
1158 >                directilluminance = 0.0;
1159          }
1160 < return;
1160 >        if (diffuseilluminance < 0) {
1161 >                if(suppress_warnings==0)
1162 >                { fprintf(stderr,"Warning: diffuse illuminance < 0. Using 0.0\n"); }
1163 >                diffuseilluminance = 0.0;
1164 >        }
1165 >        
1166 >        if (directilluminance+diffuseilluminance==0 && altitude > 0) {
1167 >                if(suppress_warnings==0)
1168 >                { fprintf(stderr,"Warning: zero illuminance at sun altitude > 0, printing error sky\n"); }
1169 >                print_error_sky();
1170 >                exit(0);
1171 >        }
1172 >        
1173 >        if (directilluminance > solar_constant_l) {
1174 >                if(suppress_warnings==0)
1175 >                { fprintf(stderr,"Warning: direct illuminance exceeds solar constant\n"); }
1176 >                print_error_sky();
1177 >                exit(0);
1178 >        }
1179   }
1180  
1181  
1182   void    check_irradiances()
1183   {
1184 <        if (!( (directirradiance>=0) && (directirradiance<=solar_constant_e) && (diffusirradiance>0) ))
1185 <        {
1186 <        fprintf(stderr,"direct or diffuse irradiances out of range\n");
1187 <        exit(1);
1188 <        }      
1189 < return;
1184 >        if (directirradiance < 0) {
1185 >                if(suppress_warnings==0)
1186 >                { fprintf(stderr,"Warning: direct irradiance < 0. Using 0.0\n"); }
1187 >                directirradiance = 0.0;
1188 >        }
1189 >        if (diffuseirradiance < 0) {
1190 >                if(suppress_warnings==0)
1191 >                { fprintf(stderr,"Warning: diffuse irradiance < 0. Using 0.0\n"); }
1192 >                diffuseirradiance = 0.0;
1193 >        }
1194 >        
1195 >        if (directirradiance+diffuseirradiance==0 && altitude > 0) {
1196 >                if(suppress_warnings==0)
1197 >                { fprintf(stderr,"Warning: zero irradiance at sun altitude > 0, printing error sky\n"); }
1198 >                print_error_sky();
1199 >                exit(0);
1200 >        }
1201 >        
1202 >        if (directirradiance > solar_constant_e) {
1203 >                if(suppress_warnings==0)
1204 >                { fprintf(stderr,"Warning: direct irradiance exceeds solar constant\n"); }
1205 >                print_error_sky();
1206 >                exit(0);
1207 >        }
1208   }
1209          
1210  
# Line 916 | Line 1214 | double sky_brightness()
1214   {
1215   double value;
1216  
1217 < value = diffusirradiance * air_mass() / ( solar_constant_e*get_eccentricity());
1217 > value = diffuseirradiance * air_mass() / ( solar_constant_e*get_eccentricity());
1218  
1219   return(value);
1220   }
# Line 925 | Line 1223 | return(value);
1223   /* Perez sky's clearness */
1224   double sky_clearness()
1225   {
1226 < double value;
1226 >        double value;
1227  
1228 < value = ( (diffusirradiance + directirradiance)/(diffusirradiance) + 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) ;
1228 >        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) ;
1229  
1230 < return(value);
1230 >        return(value);
1231   }
1232  
1233  
1234  
1235   /* diffus horizontal irradiance from Perez sky's brightness */
1236 < double diffus_irradiance_from_sky_brightness()
1236 > double diffuse_irradiance_from_sky_brightness()
1237   {
1238          double value;
1239  
# Line 950 | Line 1248 | double direct_irradiance_from_sky_clearness()
1248   {
1249          double value;
1250  
1251 <        value = diffus_irradiance_from_sky_brightness();
1251 >        value = diffuse_irradiance_from_sky_brightness();
1252          value = value * ( (skyclearness-1) * (1+1.041*sunzenith*M_PI/180*sunzenith*M_PI/180*sunzenith*M_PI/180) );
1253  
1254          return(value);
1255   }
1256  
1257  
1258 < void illu_to_irra_index(void)
1258 >
1259 >
1260 > void illu_to_irra_index()
1261   {
1262 < double  test1=0.1, test2=0.1;
1262 > double  test1=0.1, test2=0.1, d_eff;
1263   int     counter=0;      
1264  
1265 < diffusirradiance = diffusilluminance*solar_constant_e/(solar_constant_l*1000);
1266 < directirradiance = directilluminance*solar_constant_e/(solar_constant_l*1000);
1265 > diffuseirradiance = diffuseilluminance*solar_constant_e/(solar_constant_l);
1266 > directirradiance = directilluminance*solar_constant_e/(solar_constant_l);
1267   skyclearness =  sky_clearness();
1268   skybrightness = sky_brightness();
1269 < if (skyclearness>12) skyclearness=12;
970 < if (skybrightness<0.05) skybrightness=0.01;
971 <        
972 <        
973 < while ( ((fabs(diffusirradiance-test1)>10) || (fabs(directirradiance-test2)>10)
974 <                || skyclearness>skyclearinf || skyclearness<skyclearsup
975 <                || skybrightness>skybriginf || skybrightness<skybrigsup )
976 <                 && !(counter==5) )
977 <        {
978 <                /*fprintf(stderr, "conversion illuminance into irradiance %lf\t %lf\n", diffusirradiance, directirradiance);*/
1269 > check_parametrization();
1270  
1271 <        test1=diffusirradiance;
1271 >
1272 > while ( ((fabs(diffuseirradiance-test1)>10) || (fabs(directirradiance-test2)>10)
1273 >                || (!(skyclearness<skyclearinf || skyclearness>skyclearsup))
1274 >                || (!(skybrightness<skybriginf || skybrightness>skybrigsup)) )
1275 >                 && !(counter==9) )
1276 >        {
1277 >        
1278 >        test1=diffuseirradiance;
1279          test2=directirradiance;
1280          counter++;
1281          
1282 <        diffusirradiance = diffusilluminance/glob_h_diffuse_effi_PEREZ();
1283 <        directirradiance = directilluminance/direct_n_effi_PEREZ();
986 <        /*fprintf(stderr, "conversion illuminance into irradiance %lf\t %lf\n", diffusirradiance, directirradiance);*/
1282 >        diffuseirradiance = diffuseilluminance/glob_h_diffuse_effi_PEREZ();
1283 >        d_eff = direct_n_effi_PEREZ();
1284          
1285 +        
1286 +        if (d_eff < 0.1)
1287 +                directirradiance = 0;
1288 +        else    
1289 +                directirradiance = directilluminance/d_eff;
1290 +        
1291          skybrightness = sky_brightness();
1292          skyclearness =  sky_clearness();
1293 <        if (skyclearness>12) skyclearness=12;
1294 <        if (skybrightness<0.05) skybrightness=0.01;
992 <
993 <                /*fprintf(stderr, "%lf\t %lf\n", skybrightness, skyclearness);*/
994 <
1293 >        check_parametrization();
1294 >                
1295          }
1296  
1297  
1298   return;
1299   }              
1300  
1301 <
1002 < int lect_coeff_perez(char *filename,float **coeff_perez)
1301 > static int get_numlin(float epsilon)
1302   {
1303 <        FILE *fcoeff_perez;
1304 <        float temp;
1305 <        int i,j;
1306 <
1307 <        if ((fcoeff_perez = frlibopen(filename)) == NULL)
1308 <        {
1309 <                fprintf(stderr,"file %s cannot be opened\n", filename);
1310 <                return 1; /* il y a un probleme de fichier */
1311 <        }
1312 <        else
1313 <        {
1314 <                /*printf("file %s  open\n", filename);*/
1315 <        }
1316 <        
1317 <        skip_comments(fcoeff_perez);
1019 <
1020 <        for (i=0;i<8;i++)
1021 <                for (j=0;j<20;j++)
1022 <                {
1023 <                        fscanf(fcoeff_perez,"%f",&temp);
1024 <                        *(*coeff_perez+i*20+j) = temp;
1025 <                }
1026 <        fclose(fcoeff_perez);
1027 <
1028 <        return 0; /* tout est OK */
1303 >        if (epsilon < 1.065)
1304 >                return 0;
1305 >        else if (epsilon < 1.230)
1306 >                return 1;
1307 >        else if (epsilon < 1.500)
1308 >                return 2;
1309 >        else if (epsilon < 1.950)
1310 >                return 3;
1311 >        else if (epsilon < 2.800)
1312 >                return 4;
1313 >        else if (epsilon < 4.500)
1314 >                return 5;
1315 >        else if (epsilon < 6.200)
1316 >                return 6;
1317 >        return 7;
1318   }
1319  
1031
1032
1320   /* sky luminance perez model */
1321 < double calc_rel_lum_perez(double dzeta,double gamma,double Z,
1035 < double epsilon,double Delta,float *coeff_perez)
1321 > double calc_rel_lum_perez(double dzeta,double gamma,double Z,double epsilon,double Delta,float coeff_perez[])
1322   {
1323 +                        
1324          float x[5][4];
1325          int i,j,num_lin;
1326          double c_perez[5];
1327  
1328          if ( (epsilon <  skyclearinf) || (epsilon >= skyclearsup) )
1329          {
1330 <                fprintf(stderr,"Epsilon out of range in function calc_rel_lum_perez !\n");
1330 >                fprintf(stderr,"Error: epsilon out of range in function calc_rel_lum_perez!\n");
1331                  exit(1);
1332          }
1333  
# Line 1049 | Line 1336 | double epsilon,double Delta,float *coeff_perez)
1336          {
1337                  if ( Delta < 0.2 ) Delta = 0.2;
1338          }
1339 <
1340 <        if ( (epsilon >= 1.000) && (epsilon < 1.065) ) num_lin = 0;
1341 <        if ( (epsilon >= 1.065) && (epsilon < 1.230) ) num_lin = 1;
1342 <        if ( (epsilon >= 1.230) && (epsilon < 1.500) ) num_lin = 2;
1056 <        if ( (epsilon >= 1.500) && (epsilon < 1.950) ) num_lin = 3;
1057 <        if ( (epsilon >= 1.950) && (epsilon < 2.800) ) num_lin = 4;
1058 <        if ( (epsilon >= 2.800) && (epsilon < 4.500) ) num_lin = 5;
1059 <        if ( (epsilon >= 4.500) && (epsilon < 6.200) ) num_lin = 6;
1060 <        if ( (epsilon >= 6.200) && (epsilon < 14.00) ) num_lin = 7;
1061 <
1339 >        
1340 >        
1341 >        num_lin = get_numlin(epsilon);
1342 >        
1343          for (i=0;i<5;i++)
1344                  for (j=0;j<4;j++)
1345                  {
1346                          x[i][j] = *(coeff_perez + 20*num_lin + 4*i +j);
1347 <                        /* printf("x %d %d vaut %f\n",i,j,x[i][j]); */
1347 >                        /* fprintf(stderr,"x %d %d vaut %f\n",i,j,x[i][j]); */
1348                  }
1349  
1350  
# Line 1090 | Line 1371 | double epsilon,double Delta,float *coeff_perez)
1371  
1372  
1373   /* coefficients for the sky luminance perez model */
1374 < void coeff_lum_perez(double Z, double epsilon, double Delta, float *coeff_perez)
1374 > void coeff_lum_perez(double Z, double epsilon, double Delta, float coeff_perez[])
1375   {
1376          float x[5][4];
1377          int i,j,num_lin;
1378  
1379          if ( (epsilon <  skyclearinf) || (epsilon >= skyclearsup) )
1380          {
1381 <                fprintf(stderr,"Epsilon out of range in function calc_rel_lum_perez !\n");
1381 >                fprintf(stderr,"Error: epsilon out of range in function coeff_lum_perez!\n");
1382                  exit(1);
1383          }
1384  
# Line 1106 | Line 1387 | void coeff_lum_perez(double Z, double epsilon, double
1387          {
1388                  if ( Delta < 0.2 ) Delta = 0.2;
1389          }
1390 +        
1391 +        
1392 +        num_lin = get_numlin(epsilon);
1393  
1394 <        if ( (epsilon >= 1.000) && (epsilon < 1.065) ) num_lin = 0;
1111 <        if ( (epsilon >= 1.065) && (epsilon < 1.230) ) num_lin = 1;
1112 <        if ( (epsilon >= 1.230) && (epsilon < 1.500) ) num_lin = 2;
1113 <        if ( (epsilon >= 1.500) && (epsilon < 1.950) ) num_lin = 3;
1114 <        if ( (epsilon >= 1.950) && (epsilon < 2.800) ) num_lin = 4;
1115 <        if ( (epsilon >= 2.800) && (epsilon < 4.500) ) num_lin = 5;
1116 <        if ( (epsilon >= 4.500) && (epsilon < 6.200) ) num_lin = 6;
1117 <        if ( (epsilon >= 6.200) && (epsilon < 14.00) ) num_lin = 7;
1394 >        /*fprintf(stderr,"numlin %d\n", num_lin);*/
1395  
1396          for (i=0;i<5;i++)
1397                  for (j=0;j<4;j++)
# Line 1146 | Line 1423 | void coeff_lum_perez(double Z, double epsilon, double
1423   }
1424  
1425  
1426 +
1427   /* degrees into radians */
1428   double radians(double degres)
1429   {
1430 <        return degres*M_PI/180.0;
1430 >        return degres*(M_PI/180.);
1431   }
1432  
1433 +
1434   /* radian into degrees */
1435   double degres(double radians)
1436   {
1437 <        return radians/M_PI*180.0;
1437 >        return radians*(180./M_PI);
1438   }
1439  
1440 +
1441   /* calculation of the angles dzeta and gamma */
1442   void theta_phi_to_dzeta_gamma(double theta,double phi,double *dzeta,double *gamma, double Z)
1443   {
# Line 1167 | Line 1447 | void theta_phi_to_dzeta_gamma(double theta,double phi,
1447          else if ( (cos(Z)*cos(theta)+sin(Z)*sin(theta)*cos(phi)) > 1.1 )
1448          {
1449                  printf("error in calculation of gamma (angle between point and sun");
1450 <                exit(3);
1450 >                exit(1);
1451          }
1452          else
1453                  *gamma = acos(cos(Z)*cos(theta)+sin(Z)*sin(theta)*cos(phi));
1454   }
1455  
1456  
1177 /********************************************************************************/
1178 /*      Fonction: theta_ordered                                                 */
1179 /*                                                                              */
1180 /*      In: char *filename                                                      */
1181 /*                                                                              */
1182 /*      Out: float *                                                            */
1183 /*                                                                              */
1184 /*      Update: 29/08/93                                                        */
1185 /*                                                                              */
1186 /*      Rem: theta en degres                                                    */
1187 /*                                                                              */
1188 /*      But: fournit les valeurs de theta du fichier d'entree a la memoire      */
1189 /*                                                                              */
1190 /********************************************************************************/
1191 float *theta_ordered(char *filename)
1192 {
1193        int i;
1194        float buffer,*ptr;
1195        FILE *file_in;
1457  
1458 <        if ( (file_in = frlibopen(filename)) == NULL )
1198 <        {
1199 <                fprintf(stderr,"Cannot open file %s in function theta_ordered\n",filename);
1200 <                exit(1);
1201 <        }
1202 <        
1203 <        skip_comments(file_in);
1204 <
1205 <        if ( (ptr = malloc(145*sizeof(float))) == NULL )
1206 <        {
1207 <                fprintf(stderr,"Out of memory in function theta_ordered\n");
1208 <                exit(1);
1209 <        }
1210 <
1211 <        for (i=0;i<145;i++)
1212 <        {
1213 <                fscanf(file_in,"%f",&buffer);
1214 <                *(ptr+i) = buffer;
1215 <                fscanf(file_in,"%f",&buffer);
1216 <        }
1217 <
1218 <        fclose(file_in);
1219 <        return ptr;
1220 < }
1221 <
1222 <
1223 < /********************************************************************************/
1224 < /*      Fonction: phi_ordered                                                   */
1225 < /*                                                                              */
1226 < /*      In: char *filename                                                      */
1227 < /*                                                                              */
1228 < /*      Out: float *                                                            */
1229 < /*                                                                              */
1230 < /*      Update: 29/08/93                                                        */
1231 < /*                                                                              */
1232 < /*      Rem: valeurs de Phi en DEGRES                                           */
1233 < /*                                                                              */
1234 < /*      But: mettre les angles contenus dans le fichier d'entree dans la memoire */
1235 < /*                                                                              */
1236 < /********************************************************************************/
1237 < float *phi_ordered(char *filename)
1458 > double integ_lv(float *lv,float *theta)
1459   {
1460          int i;
1461 <        float buffer,*ptr;
1241 <        FILE *file_in;
1242 <
1243 <        if ( (file_in = frlibopen(filename)) == NULL )
1244 <        {
1245 <                fprintf(stderr,"Cannot open file %s in function phi_ordered\n",filename);
1246 <                exit(1);
1247 <        }
1461 >        double buffer=0.0;
1462          
1249        skip_comments(file_in);
1250
1251        if ( (ptr = malloc(145*sizeof(float))) == NULL )
1252        {
1253                fprintf(stderr,"Out of memory in function phi_ordered");
1254                exit(1);
1255        }
1256
1463          for (i=0;i<145;i++)
1464          {
1465 <                fscanf(file_in,"%f",&buffer);
1260 <                fscanf(file_in,"%f",&buffer);
1261 <                *(ptr+i) = buffer;
1465 >                buffer += (*(lv+i))*cos(radians(*(theta+i)));
1466          }
1467 <
1468 <        fclose(file_in);
1265 <        return ptr;
1467 >                        
1468 >        return buffer*(2.*M_PI/145.);
1469   }
1470  
1471  
1269 /********************************************************************************/
1270 /*      Fonction: integ_lv                                                      */
1271 /*                                                                              */
1272 /*      In: float *lv,*theta                                                    */
1273 /*          int sun_pos                                                         */
1274 /*                                                                              */
1275 /*      Out: double                                                             */
1276 /*                                                                              */
1277 /*      Update: 29/08/93                                                        */
1278 /*                                                                              */
1279 /*      Rem:                                                                    */
1280 /*                                                                              */
1281 /*      But: calcul l'integrale de luminance relative sans la dir. du soleil    */
1282 /*                                                                              */
1283 /********************************************************************************/
1284 double integ_lv(float *lv,float *theta)
1285 {
1286        int i;
1287        double buffer=0.0;
1472  
1289        for (i=0;i<145;i++)
1290                buffer += (*(lv+i))*cos(radians(*(theta+i)));
1291
1292        return buffer*2*M_PI/144;
1293
1294 }
1295
1296
1297
1298
1299
1300
1473   /* enter day number(double), return E0 = square(R0/R):  eccentricity correction factor  */
1474  
1475   double get_eccentricity()
# Line 1310 | Line 1482 | double get_eccentricity()
1482              0.000719*cos(2*day_angle)+0.000077*sin(2*day_angle);
1483  
1484          return (E0);
1313
1485   }
1486  
1487  
# Line 1318 | Line 1489 | double get_eccentricity()
1489   double  air_mass()
1490   {
1491   double  m;
1321
1492   if (sunzenith>90)
1493          {
1494 <        fprintf(stderr, "solar zenith angle larger than 90� in fuction air_mass():\n the models used are not more valid\n");
1495 <        exit(1);
1494 >        if(suppress_warnings==0)
1495 >        { fprintf(stderr, "Warning: air mass has reached the maximal value\n"); }
1496 >        sunzenith=90;
1497          }
1327        
1498   m = 1/( cos(sunzenith*M_PI/180)+0.15*exp( log(93.885-sunzenith)*(-1.253) ) );
1499   return(m);
1500   }
1331
1332
1333 double get_angle_sun_direction(double sun_zenith, double sun_azimut, double direction_zenith, double direction_azimut)
1334
1335 {
1336
1337 double angle;
1338
1339
1340 if (sun_zenith == 0)
1341        puts("WARNING: zenith_angle = 0 in function get_angle_sun_vert_plan");
1342
1343 angle = acos( cos(sun_zenith*M_PI/180)*cos(direction_zenith*M_PI/180) + sin(sun_zenith*M_PI/180)*sin(direction_zenith*M_PI/180)*cos((sun_azimut-direction_azimut)*M_PI/180) );
1344 angle = angle*180/M_PI;
1345 return(angle);
1346 }
1347
1348
1349
1350
1351
1501  
1502  
1503  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines